Skip to content

Commit

Permalink
parse Vivaldi workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
liias committed Dec 15, 2023
1 parent 29ff01e commit 26c98d2
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/chromium_profiles_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::path::{Path, PathBuf};
use serde_json::{Map, Value};
use tracing::{debug, info};

use crate::{paths, utils, InstalledBrowserProfile};
use crate::{paths, utils, vivaldi_workspaces_parser, InstalledBrowserProfile};

pub fn find_chromium_profiles(
chromium_user_dir: &Path,
Expand Down Expand Up @@ -62,14 +62,44 @@ pub fn find_chromium_profiles(
})
.flatten();

let profile_dir = chromium_user_dir.join(profile.profile_dir_name.as_str());

let mut containers = Vec::new();

let containers_json_file = profile_dir.join("Preferences");
if !containers_json_file.exists() {
info!(
"Skipping containers for profile '{}', because it does not have Preferences file",
profile_dir.display()
);
} else {
// containers for this profile
containers =
vivaldi_workspaces_parser::preferences_json_map(containers_json_file.as_path());
}

let profile_dir_name = profile.profile_dir_name;

// Even if profile has containers, also add a non-container option
browser_profiles.push(InstalledBrowserProfile {
profile_cli_arg_value: profile_dir_name.to_string(),
profile_cli_container_name: None,
profile_name: profile_name,
profile_name: profile_name.to_string(),
profile_icon: profile_icon_path,
profile_restricted_url_patterns: vec![],
})
});

if !containers.is_empty() {
for container in containers {
browser_profiles.push(InstalledBrowserProfile {
profile_cli_arg_value: profile_name.to_string(),
profile_cli_container_name: Some(container.id.to_string()),
profile_name: profile_name.to_string() + " " + container.name.as_str(),
profile_icon: None,
profile_restricted_url_patterns: vec![],
})
}
}
}

return browser_profiles;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mod firefox_profiles_parser;
mod slack_profiles_parser;
mod slack_url_parser;
mod url_rule;
mod vivaldi_workspaces_parser;

// a browser (with profiles), or Spotify, Zoom, etc
pub struct GenericApp {
Expand Down
41 changes: 41 additions & 0 deletions src/vivaldi_workspaces_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::fs::File;
use std::io::BufReader;
use std::path::Path;

use serde_json::Value;

pub fn preferences_json_map(preferences_json_file_path: &Path) -> Vec<VivaldiWorkspace> {
// Open the file in read-only mode with buffer.
let file = File::open(preferences_json_file_path).unwrap();
let reader = BufReader::new(file);
let v: Value = serde_json::from_reader(reader).unwrap();
let vivaldi = &v["vivaldi"];
let workspaces = &vivaldi["workspaces"];
let workspaces_list = &workspaces["list"];
let workspaces_list_maybe = workspaces_list.as_array();
if workspaces_list_maybe.is_none() {
return Vec::new();
}

let workspaces_list_arr = workspaces_list_maybe.unwrap();
let mut containers: Vec<VivaldiWorkspace> = Vec::new();

for workspace in workspaces_list_arr {
// id is stored as 1.702675590916e+12
// but converting it to string converts it to 1702675590916
let id = workspace["id"].as_f64().unwrap();
let name = workspace["name"].as_str().unwrap();
// also has icon string of svg
let container = VivaldiWorkspace {
id: id.to_string(),
name: name.to_string(),
};
containers.push(container);
}
return containers;
}

pub struct VivaldiWorkspace {
pub id: String,
pub name: String,
}

0 comments on commit 26c98d2

Please sign in to comment.