Skip to content

Commit

Permalink
chore: Use v4_10 gtk4-rs feature
Browse files Browse the repository at this point in the history
Gtk.FileDialog allows selecting from directories that are not explicitly
permitted within the Flatpak manifest, and grants the app permission
upon a user doing so. Therefore, `is_accessible` and the related dialog
are no longer relevant. Do note that if not explicitly allowed, the
directory will show as a not-so-pretty sandboxed path.
  • Loading branch information
DaPigGuy authored and SeaDve committed Apr 30, 2023
1 parent 4cdf149 commit a8f6b7e
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ tracing = "0.1.36"
tracing-subscriber = "0.3.15"
once_cell = "1.9.0"
gettext-rs = { version = "0.7.0", features = ["gettext-system"] }
gtk = { package = "gtk4", version = "0.6.6" }
gtk = { package = "gtk4", version = "0.6.6", features = ["gnome_44"] }
gdk-wayland = { package = "gdk4-wayland", version = "0.6.3" }
gdk-x11 = { package = "gdk4-x11", version = "0.6.3" }
adw = { package = "libadwaita", version = "0.4.0", features = ["v1_2"] }
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ base_id = 'io.github.seadve.Kooha'

dependency('glib-2.0', version: '>= 2.66')
dependency('gio-2.0', version: '>= 2.66')
dependency('gtk4', version: '>= 4.6')
dependency('gtk4', version: '>= 4.10')
dependency('libadwaita-1', version: '>= 1.2')
dependency('gstreamer-1.0', version: '>= 1.20')
dependency('gstreamer-pbutils-1.0', version: '>= 1.20')
Expand Down
7 changes: 4 additions & 3 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use adw::subclass::prelude::*;
use anyhow::{Context, Result};
use gettextrs::gettext;
use gtk::{
gdk, gio,
gio,
glib::{self, clone, WeakRef},
prelude::*,
};
Expand Down Expand Up @@ -135,8 +135,9 @@ impl Application {
}

async fn try_show_uri(&self, uri: &str) {
if let Err(err) =
gtk::show_uri_full_future(self.main_window().as_ref(), uri, gdk::CURRENT_TIME).await
if let Err(err) = gtk::FileLauncher::new(Some(&gio::File::for_uri(uri)))
.launch_future(self.main_window().as_ref())
.await
{
if !err.matches(gio::IOErrorEnum::Cancelled) {
tracing::error!("Failed to launch default for uri `{}`: {:?}", uri, err);
Expand Down
2 changes: 1 addition & 1 deletion src/area_selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl AreaSelector {

let monitor_geometry = RootExt::display(transient_for)
.monitor_at_surface(&transient_for.surface())
.expect("No monitor found")
.context("No monitor found")?
.geometry();
this.set_default_width(
(monitor_geometry.width() as f64 * 0.4 - ASSUMED_HEADER_BAR_HEIGHT * 2.0) as i32,
Expand Down
3 changes: 2 additions & 1 deletion src/preferences_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mod imp {
klass.bind_template();

klass.install_action("preferences.select-saving-location", None, |obj, _, _| {
utils::app_settings().select_saving_location(Some(obj));
utils::app_settings().select_saving_location(obj);
});
}

Expand Down Expand Up @@ -217,6 +217,7 @@ fn profile_row_factory(
) -> gtk::SignalListItemFactory {
let factory = gtk::SignalListItemFactory::new();
factory.connect_setup(clone!(@weak profile_row => move |_, list_item| {
let list_item = list_item.downcast_ref::<gtk::ListItem>().unwrap();
let item_expression = list_item.property_expression("item");

let hbox = gtk::Box::builder().spacing(12).build();
Expand Down
89 changes: 28 additions & 61 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ use gtk::{
glib::{self, clone},
};

use std::{
fs,
path::{Path, PathBuf},
time::Duration,
};
use std::{fs, path::PathBuf, time::Duration};

use crate::{
config::APP_ID,
profile::{self, Profile},
utils,
};

#[gen_settings(file = "./data/io.github.seadve.Kooha.gschema.xml.in")]
Expand All @@ -33,57 +28,39 @@ impl Default for Settings {
impl Settings {
pub const NONE_PROFILE_ID: &'static str = "none";

/// Opens a `FileChooserDialog` to select a folder and updates
/// Opens a `FileDialog` to select a folder and updates
/// the settings with the selected folder.
pub fn select_saving_location(&self, transient_for: Option<&impl IsA<gtk::Window>>) {
let chooser = gtk::FileChooserDialog::builder()
pub fn select_saving_location(&self, transient_for: &impl IsA<gtk::Window>) {
let dialog = gtk::FileDialog::builder()
.modal(true)
.action(gtk::FileChooserAction::SelectFolder)
.title(&gettext("Select Recordings Folder"))
.initial_folder(&gio::File::for_path(self.saving_location()))
.build();
chooser.set_transient_for(transient_for);

if let Err(err) =
chooser.set_current_folder(Some(&gio::File::for_path(self.saving_location())))
{
tracing::warn!("Failed to set current folder: {:?}", err);
}

chooser.add_button(&gettext("_Cancel"), gtk::ResponseType::Cancel);
chooser.add_button(&gettext("_Select"), gtk::ResponseType::Accept);
chooser.set_default_response(gtk::ResponseType::Accept);

chooser.present();

let transient_for = transient_for.upcast_ref::<gtk::Window>();
let inner = &self.0;
chooser.connect_response(clone!(@weak inner => move |chooser, response| {
if response != gtk::ResponseType::Accept {
chooser.close();
return;
}

let Some(directory) = chooser.file().and_then(|file| file.path()) else {
present_message(
&gettext("No folder selected"),
&gettext("Please choose a folder and try again."),
Some(chooser),
);
return;
};

if !is_accessible(&directory) {
present_message(
// Translators: {} will be replaced with a path to the folder.
&gettext!("Cannot access “{}”", directory.display()),
&gettext("Please choose an accessible location and try again."),
Some(chooser),
);
return;
}

inner.set("saving-location", &directory).unwrap();
chooser.close();
}));
dialog.select_folder(
Some(transient_for),
gio::Cancellable::NONE,
clone!(@weak inner, @weak transient_for => move |response| {
match response {
Ok(folder) => {
inner.set("saving-location", folder.path().unwrap()).unwrap();
}
Err(err) => {
if err.matches(gtk::DialogError::Dismissed) || err.matches(gtk::DialogError::Cancelled) {
return;
}

present_message(
&gettext("Failed to select folder"),
&err.to_string(),
Some(&transient_for),
);
}
}
}),
);
}

pub fn saving_location(&self) -> PathBuf {
Expand Down Expand Up @@ -191,16 +168,6 @@ fn present_message(heading: &str, body: &str, transient_for: Option<&impl IsA<gt
dialog.present();
}

fn is_accessible(path: &Path) -> bool {
if !utils::is_flatpak() {
return true;
}

let home_folder = glib::home_dir();

path != home_folder && path.starts_with(&home_folder)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit a8f6b7e

Please sign in to comment.