[shotwell/wip/enhanced-profiles] Add initial profile editor



commit dd59bee3727e5baa3e84790a4aebabe194ecc9aa
Author: Jens Georg <mail jensge org>
Date:   Wed Aug 17 09:35:53 2022 +0200

    Add initial profile editor

 src/ProfileBrowser.vala | 121 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/Profiles.vala       |  47 ++++++++++++++++---
 2 files changed, 162 insertions(+), 6 deletions(-)
---
diff --git a/src/ProfileBrowser.vala b/src/ProfileBrowser.vala
index aa33464f..fdb3f908 100644
--- a/src/ProfileBrowser.vala
+++ b/src/ProfileBrowser.vala
@@ -2,6 +2,110 @@
 // SPDX-License-Identifier: LGPL-2.1-or-later
 
 namespace Shotwell {
+    class ProfileEditor : Gtk.Dialog {
+        public string profile_name {get; set;}
+        public string id{get; default = Uuid.string_random();}
+        public string library_folder{get; set;}
+        public string data_folder{get; set;}
+
+        public ProfileEditor() {
+            Object(use_header_bar : Resources.use_header_bar());
+        }
+
+        public override void constructed() {
+            base.constructed();
+
+            set_size_request(640, -1);
+
+            add_buttons(_("Create"), Gtk.ResponseType.OK, _("Cancel"), Gtk.ResponseType.CANCEL, null);
+            var create_button = get_widget_for_response(Gtk.ResponseType.OK);
+            create_button.get_style_context().add_class("suggested-action");
+            create_button.sensitive = false;
+            set_title(_("Create new Profile"));
+
+            data_folder = Path.build_filename(Environment.get_user_data_dir(), "shotwell", "profiles", id);
+            library_folder = Environment.get_user_special_dir(UserDirectory.PICTURES);
+
+            var grid = new Gtk.Grid();
+            grid.hexpand = true;
+            grid.vexpand = true;
+            grid.margin = 6;
+            grid.set_row_spacing(12);
+            grid.set_column_spacing(12);
+            var label = new Gtk.Label(_("Name"));
+            label.get_style_context().add_class("dim-label");
+            label.halign = Gtk.Align.END;
+            grid.attach(label, 0, 0, 1, 1);
+
+            var entry = new Gtk.Entry();
+            entry.hexpand = true;
+            entry.bind_property("text", this, "profile-name", GLib.BindingFlags.DEFAULT);
+            entry.bind_property("text", create_button, "sensitive", GLib.BindingFlags.DEFAULT, (binding, 
from, ref to) => {
+                to = from.get_string() != "";
+                return true;
+            });
+            grid.attach(entry, 1, 0, 2, 1);
+
+            label = new Gtk.Label(_("Library Folder"));
+            label.get_style_context().add_class("dim-label");
+            label.halign = Gtk.Align.END;
+            grid.attach(label, 0, 1, 1, 1);
+
+            entry = new Gtk.Entry();
+            entry.hexpand = true;
+            grid.attach(entry, 1, 1, 1, 1);
+            bind_property("library-folder", entry, "text", GLib.BindingFlags.SYNC_CREATE | 
GLib.BindingFlags.BIDIRECTIONAL);
+
+            var button = new Gtk.Button.from_icon_name("folder-symbolic", Gtk.IconSize.BUTTON);
+            button.hexpand = false;
+            button.vexpand = false;
+            button.halign = Gtk.Align.FILL;
+            button.clicked.connect(() => {
+                var dialog = new Gtk.FileChooserNative(_("Choose Library Folder"), this, 
Gtk.FileChooserAction.SELECT_FOLDER, _("_OK"), _("_Cancel"));
+                dialog.set_current_folder(library_folder);
+                var result = dialog.run();
+                dialog.hide();
+                if (result == Gtk.ResponseType.ACCEPT) {
+                    library_folder = dialog.get_current_folder_file().get_path();
+                }
+                dialog.destroy();
+            });
+            grid.attach(button, 2, 1, 1, 1);
+
+
+            label = new Gtk.Label(_("Data Folder"));
+            label.get_style_context().add_class("dim-label");
+            label.halign = Gtk.Align.END;
+            grid.attach(label, 0, 2, 1, 1);
+
+            entry = new Gtk.Entry();
+            entry.set_text(Environment.get_user_special_dir(UserDirectory.PICTURES));
+            entry.hexpand = true;
+            bind_property("data-folder", entry, "text", GLib.BindingFlags.SYNC_CREATE | 
GLib.BindingFlags.BIDIRECTIONAL);
+            grid.attach(entry, 1, 2, 1, 1);
+
+            button = new Gtk.Button.from_icon_name("folder-symbolic", Gtk.IconSize.BUTTON);
+            button.hexpand = false;
+            button.vexpand = false;
+            button.halign = Gtk.Align.FILL;
+            button.clicked.connect(() => {
+                var dialog = new Gtk.FileChooserNative(_("Choose Data Folder"), this, 
Gtk.FileChooserAction.SELECT_FOLDER, _("_OK"), _("_Cancel"));
+                dialog.set_current_folder(data_folder);
+                var result = dialog.run();
+                dialog.hide();
+                if (result == Gtk.ResponseType.ACCEPT) {
+                    data_folder = dialog.get_current_folder_file().get_path();
+                }
+                dialog.destroy();
+            });
+
+            grid.attach(button, 2, 2, 1, 1);
+
+            get_content_area().add(grid);
+
+            show_all();
+        }
+    }
     class ProfileBrowser : Gtk.Box {
         public ProfileBrowser() {
             Object(orientation: Gtk.Orientation.VERTICAL, vexpand: true, hexpand: true);
@@ -32,6 +136,19 @@ namespace Shotwell {
             list_box.bind_model(ProfileManager.get_instance(), on_widget_create);
             list_box.set_header_func(on_header);
 
+            var button = new Gtk.Button.with_label(_("Create new Profile"));
+            pack_start(button, false, false, 6);
+            button.clicked.connect(() => {
+                var editor = new ProfileEditor();
+                editor.set_transient_for((Gtk.Window)get_ancestor(typeof(Gtk.Window)));
+                var result = editor.run();
+                editor.hide();
+                if (result == Gtk.ResponseType.OK) {
+                    debug("Request to add new profile: %s %s %s %s", editor.id, editor.profile_name, 
editor.library_folder, editor.data_folder);
+                    ProfileManager.get_instance().add_profile(editor.id, editor.profile_name, 
editor.library_folder, editor.data_folder);
+                }
+                editor.destroy();
+            });
             add(scrollable);
             show_all();
         }
@@ -63,6 +180,8 @@ namespace Shotwell {
                 settings_path = "/org/gnome/shotwell/profiles/" + p.id + "/preferences/files/";
             }
 
+            print ("Showing settings at path %s\n", settings_path);
+
             var settings = new Settings.with_path("org.gnome.shotwell.preferences.files", settings_path);
             var import_dir = settings.get_string("import-dir");
             if (import_dir == "") {
@@ -127,6 +246,8 @@ namespace Shotwell {
                 });
             }
 
+            box.show_all();
+
             row.add (box);
 
             return row;
diff --git a/src/Profiles.vala b/src/Profiles.vala
index 392b40c8..a1e54ac2 100644
--- a/src/Profiles.vala
+++ b/src/Profiles.vala
@@ -34,8 +34,11 @@ namespace Shotwell {
                             this.profile == null);
             }
 
+            print("Get item called for position %u", position);
+
             var group = profiles.get_groups()[position - 1];
             var id = profiles.get_value(group, "Id");
+            print ("Id: %s\n", id);
             var name = profiles.get_value(group, "Name");
             var active = this.profile == name;
             return new Profile(profiles.get_value(group, "Name"),
@@ -71,6 +74,8 @@ namespace Shotwell {
         private string group_name;
 
         public override void constructed() {
+            base.constructed();
+
             profiles = new KeyFile();
             path = Path.build_filename(Environment.get_user_config_dir(), "shotwell");
             DirUtils.create_with_parents(path, 0700);
@@ -94,19 +99,45 @@ namespace Shotwell {
 
             this.profile = profile;
 
-            if (has_profile (this.profile, out this.group_name))
+            add_profile(Uuid.string_random(), profile, null, null);
+        }
+
+        public void add_profile(string id, string name, string? library_folder, string? data_folder) {
+            string  group_name;
+
+            if (has_profile(name, out group_name)) {
                 return;
+            }
 
             try {
-                profiles.set_string(group_name, "Name", profile);
-                profiles.set_string(group_name, "Id", Uuid.string_random());
+                profiles.set_string(group_name, "Name", name);
+                profiles.set_string(group_name, "Id", id);
+                if (data_folder != null) {
+                    profiles.set_string(group_name, "DataDir", data_folder);
+                }
 
                 // Need to set comment after setting keys since it does not create the group
-                profiles.set_comment(group_name, null, "Profile settings for \"%s\"".printf(profile));
+                profiles.set_comment(group_name, null, "Profile settings for \"%s\"".printf(name));
+
+                write();
+            } catch (Error err) {
+                error("Failed to create profile: %s", err.message);                
+            }
+
+            try {
+                if (library_folder != null) {
+                    var settings_path = "/org/gnome/shotwell/profiles/" + id + "/preferences/files/";
+                    print ("writing settings at path %s\n", settings_path);
+
+        
+                    var settings = new Settings.with_path("org.gnome.shotwell.preferences.files", 
settings_path);
+                    settings.set_string("import-dir", library_folder);
+                }
             } catch (Error err) {
                 error("Failed to create profile: %s", err.message);
             }
-            write();
+            
+            items_changed(profiles.get_groups().length, 0, 1);
         }
 
         public string derive_data_dir(string? data_dir) {
@@ -190,7 +221,11 @@ namespace Shotwell {
                 // TODO: Remove folder
                 }
 
-                items_changed(index, 1, 0);
+                Idle.add(() => {
+                    items_changed(index, 1, 0);
+
+                    return false;
+                });
                 write();
             }
         }


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