Skip to content

Commit

Permalink
allow setting Browsers as default opener in Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
liias committed Sep 19, 2024
1 parent 938b07b commit f25be7e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/linux/linux_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::{BTreeMap, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::slice::Split;

use druid::image;
Expand Down Expand Up @@ -429,3 +430,53 @@ pub fn linux_get_unsandboxed_home_dir() -> PathBuf {
// TODO: escape sandbox if in snap/flatpak
return dirs::home_dir().unwrap();
}

// returns true if it was already default web browser (then nothing was done)
pub fn set_default_web_browser() -> bool {
if is_default_web_browser() {
return true;
}

let desktop_file_name = format!("{}.desktop", XDG_NAME);

let result = Command::new("xdg-mime")
.arg("default")
.arg(desktop_file_name.as_str())
.arg("https http")
.status();

if result.is_err() {
warn!("Could not set Browsers as default app");
}

return false;
}

pub fn is_default_web_browser() -> bool {
let desktop_file_name = format!("{}.desktop", XDG_NAME);

let https_default_app = query_default_app("https").unwrap_or("".to_string());
let http_default_app = query_default_app("http").unwrap_or("".to_string());

return https_default_app == desktop_file_name && http_default_app == desktop_file_name;
}

fn query_default_app(scheme: &str) -> Option<String> {
let result = Command::new("xdg-mime")
.arg("query")
.arg("default")
.arg(scheme)
.stdout(Stdio::piped())
.output();

if result.is_err() {
warn!("Could not query default app for scheme {}", scheme);
return None;
}

let output = result.unwrap();

// extract the raw bytes that we captured and interpret them as a string
let default_app = String::from_utf8(output.stdout).unwrap();
return Some(default_app);
}
14 changes: 12 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,32 @@ use crate::macos::macos_utils;
use crate::windows::windows_utils;
use crate::{paths, InstalledBrowser, SupportedAppRepository};

#[cfg(target_os = "linux")]
pub fn is_default_web_browser() -> bool {
return linux_utils::is_default_web_browser();
}

#[cfg(target_os = "macos")]
pub fn is_default_web_browser() -> bool {
return macos_utils::is_default_web_browser();
}

#[cfg(not(target_os = "macos"))]
#[cfg(target_os = "windows")]
pub fn is_default_web_browser() -> bool {
return true;
}

#[cfg(target_os = "linux")]
pub fn set_as_default_web_browser() -> bool {
return linux_utils::set_default_web_browser();
}

#[cfg(target_os = "macos")]
pub fn set_as_default_web_browser() -> bool {
return macos_utils::set_default_web_browser();
}

#[cfg(not(target_os = "macos"))]
#[cfg(target_os = "windows")]
pub fn set_as_default_web_browser() -> bool {
return true;
}
Expand Down

0 comments on commit f25be7e

Please sign in to comment.