[fractal] file_chooser: add file filter



commit b088a0da0c45ff213ccfbd17720dc6d2b0c55061
Author: Julian Sparber <julian sparber net>
Date:   Wed Jan 9 14:32:54 2019 +0100

    file_chooser: add file filter

 fractal-gtk/src/actions/account_settings.rs |  5 ++++-
 fractal-gtk/src/actions/global.rs           |  2 +-
 fractal-gtk/src/actions/room_history.rs     |  2 +-
 fractal-gtk/src/actions/room_settings.rs    |  5 ++++-
 fractal-gtk/src/widgets/file_dialog.rs      | 11 +++++++++--
 5 files changed, 19 insertions(+), 6 deletions(-)
---
diff --git a/fractal-gtk/src/actions/account_settings.rs b/fractal-gtk/src/actions/account_settings.rs
index a3fdfe03..6f68ecb2 100644
--- a/fractal-gtk/src/actions/account_settings.rs
+++ b/fractal-gtk/src/actions/account_settings.rs
@@ -26,7 +26,10 @@ pub fn new(window: &gtk::Window, backend: &Sender<BKCommand>) -> gio::SimpleActi
     let backend = backend.clone();
     change_avatar.connect_activate(move |a, _| {
         let window = upgrade_weak!(window_weak);
-        if let Some(path) = open(&window, i18n("Select a new avatar").as_str()) {
+        let filter = gtk::FileFilter::new();
+        filter.add_mime_type("image/*");
+        FileFilterExt::set_name(&filter, Some(i18n("Images").as_str()));
+        if let Some(path) = open(&window, i18n("Select a new avatar").as_str(), &[filter]) {
             if let Some(file) = path.to_str() {
                 a.change_state(&ButtonState::Insensitive.into());
                 let _ = backend.send(BKCommand::SetUserAvatar(file.to_string()));
diff --git a/fractal-gtk/src/actions/global.rs b/fractal-gtk/src/actions/global.rs
index f6c5570e..831c5120 100644
--- a/fractal-gtk/src/actions/global.rs
+++ b/fractal-gtk/src/actions/global.rs
@@ -217,7 +217,7 @@ pub fn new(app: &gtk::Application, op: &Arc<Mutex<AppOp>>) {
     send_file.connect_activate(move |_, _| {
         let app = upgrade_weak!(app_weak);
         if let Some(window) = app.get_active_window() {
-            if let Some(path) = open(&window, i18n("Select a file").as_str()) {
+            if let Some(path) = open(&window, i18n("Select a file").as_str(), &[]) {
                 APPOP!(attach_message, (path));
             }
         }
diff --git a/fractal-gtk/src/actions/room_history.rs b/fractal-gtk/src/actions/room_history.rs
index f9c363ec..17d1ecbf 100644
--- a/fractal-gtk/src/actions/room_history.rs
+++ b/fractal-gtk/src/actions/room_history.rs
@@ -114,7 +114,7 @@ pub fn new(backend: Sender<BKCommand>, ui: UI) -> gio::SimpleActionGroup {
                     },
                     Ok(fname) => {
                         let window = upgrade_weak!(parent_weak, gtk::Continue(true));
-                        if let Some(path) = save(&window, &name) {
+                        if let Some(path) = save(&window, &name, &[]) {
                             // TODO use glib to copy file
                             if let Err(_) = fs::copy(fname.clone(), path) {
                                 ErrorDialog::new(false, &i18n("Couldn't save file"));
diff --git a/fractal-gtk/src/actions/room_settings.rs b/fractal-gtk/src/actions/room_settings.rs
index 45494841..4e9c64fc 100644
--- a/fractal-gtk/src/actions/room_settings.rs
+++ b/fractal-gtk/src/actions/room_settings.rs
@@ -31,7 +31,10 @@ pub fn new(window: &gtk::Window, backend: &Sender<BKCommand>) -> gio::SimpleActi
     change_avatar.connect_activate(move |a, data| {
         if let Some(id) = data.as_ref().map(|x| x.to_string()) {
             let window = upgrade_weak!(window_weak);
-            if let Some(path) = open(&window, i18n("Select a new avatar").as_str()) {
+            let filter = gtk::FileFilter::new();
+            FileFilterExt::set_name(&filter, Some(i18n("Images").as_str()));
+            filter.add_mime_type("image/*");
+            if let Some(path) = open(&window, i18n("Select a new avatar").as_str(), &[filter]) {
                 if let Some(file) = path.to_str() {
                     a.change_state(&ButtonState::Insensitive.into());
                     let _ = backend.send(BKCommand::SetRoomAvatar(id, file.to_string()));
diff --git a/fractal-gtk/src/widgets/file_dialog.rs b/fractal-gtk/src/widgets/file_dialog.rs
index 11f178da..99a47783 100644
--- a/fractal-gtk/src/widgets/file_dialog.rs
+++ b/fractal-gtk/src/widgets/file_dialog.rs
@@ -4,7 +4,7 @@ use gtk::prelude::*;
 use gtk::ResponseType;
 use std::path::PathBuf;
 
-pub fn save(parent: &gtk::Window, title: &str) -> Option<PathBuf> {
+pub fn save(parent: &gtk::Window, title: &str, filter: &[gtk::FileFilter]) -> Option<PathBuf> {
     let file_chooser = gtk::FileChooserNative::new(
         Some(i18n("Save media as").as_str()),
         Some(parent),
@@ -12,6 +12,9 @@ pub fn save(parent: &gtk::Window, title: &str) -> Option<PathBuf> {
         Some(i18n("_Save").as_str()),
         Some(i18n("_Cancel").as_str()),
     );
+    for f in filter {
+        file_chooser.add_filter(f);
+    }
 
     file_chooser.set_current_folder(dirs::download_dir().unwrap_or_default());
     file_chooser.set_current_name(title);
@@ -22,7 +25,7 @@ pub fn save(parent: &gtk::Window, title: &str) -> Option<PathBuf> {
     None
 }
 
-pub fn open(parent: &gtk::Window, title: &str) -> Option<PathBuf> {
+pub fn open(parent: &gtk::Window, title: &str, filter: &[gtk::FileFilter]) -> Option<PathBuf> {
     let file_chooser = gtk::FileChooserNative::new(
         Some(title),
         Some(parent),
@@ -31,6 +34,10 @@ pub fn open(parent: &gtk::Window, title: &str) -> Option<PathBuf> {
         Some(i18n("_Cancel").as_str()),
     );
 
+    for f in filter {
+        file_chooser.add_filter(f);
+    }
+
     let response = file_chooser.run();
     if gtk::ResponseType::from(response) == gtk::ResponseType::Accept {
         return file_chooser.get_filename();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]