Skip to content

Commit

Permalink
Merge pull request #58 from cosmic-utils/rewrite
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
elevenhsoft authored Sep 28, 2024
2 parents 55b4708 + 631f100 commit 7e82ae9
Show file tree
Hide file tree
Showing 10 changed files with 872 additions and 907 deletions.
123 changes: 123 additions & 0 deletions src/browser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
use std::path::PathBuf;

use crate::{common, fl, supported_browsers};

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BrowserType {
NoBrowser,
Firefox,
FirefoxFlatpak,
ZenFlatpak,
Chromium,
ChromiumFlatpak,
Falkon,
FalkonFlatpak,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Browser {
pub _type: BrowserType,
pub name: String,
pub exec: String,
pub test: PathBuf,
pub profile_path: PathBuf,
}

impl AsRef<str> for Browser {
fn as_ref(&self) -> &str {
&self.name
}
}

impl Browser {
pub fn new(
_type: BrowserType,
name: &str,
exec: &str,
test_path: &str,
profile_path: &str,
) -> Self {
let name = name.to_string();

let mut test = PathBuf::new();
let mut exe_path = PathBuf::new();

let base = common::home_dir();
let data_home = base.join(".local/share");

if exec.starts_with(".local/share/") {
let flatpak_path: Vec<&str> = exec.split(".local/share/").collect();
let path = data_home.join(flatpak_path[1]);
exe_path.push(path);
} else {
exe_path.push(exec)
}

if test_path.starts_with(".local/share") {
let flatpak_path: Vec<&str> = test_path.split(".local/share/").collect();
let path = data_home.join(flatpak_path[1]);
test.push(path);
} else {
test.push(test_path)
}

let exec = exe_path.to_str().unwrap().to_string();

let profile_path = base.join(profile_path);

Self {
_type,
name,
exec,
test,
profile_path,
}
}

pub fn web_browser(name: String) -> Option<Browser> {
let supported = get_supported_browsers();
supported.into_iter().find(|b| b.name == name)
}

pub fn is_installed(&self) -> bool {
!matches!(self._type, BrowserType::NoBrowser)
}
}

pub fn get_supported_browsers() -> Vec<Browser> {
let mut test_browsers: Vec<Browser> = Vec::new();

let native_browsers: Vec<Browser> = supported_browsers::native_browsers();
let flatpak_browsers: Vec<Browser> = supported_browsers::flatpak_browsers();
let nix_browsers: Vec<Browser> = supported_browsers::nix_browsers();

test_browsers.extend(native_browsers);
test_browsers.extend(flatpak_browsers);
test_browsers.extend(nix_browsers);

let mut browsers = Vec::new();

for browser in test_browsers {
let exists = browser.test.as_path().try_exists();

match exists {
Ok(found) => match found {
true => browsers.push(browser),
false => continue,
},
Err(_) => continue,
}
}

if browsers.is_empty() {
browsers.push(Browser::new(
BrowserType::NoBrowser,
&fl!("select-browser"),
"",
"",
"",
));
}

browsers
}
Loading

0 comments on commit 7e82ae9

Please sign in to comment.