summaryrefslogtreecommitdiff
path: root/src/Profiles.vala
blob: ec52800e2092cc7853759185b95087ec7315c862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Copyright 2019 Jens Georg.
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution.
 */

namespace Shotwell {
    class Profile : Object {
        public const string SYSTEM = "__shotwell_default_system";
        public Profile(string name, string id, string data_dir, bool active) {
            Object(name: name, id: id, data_dir: data_dir, active: active);
        }
        public string name {get; construct;}
        public string id {get; construct;}
        public string data_dir {get; construct;}
        public bool active {get; construct;}
    }

    class ProfileManager : Object, GLib.ListModel {
        // ListModel implementations
        Type get_item_type() {
            return typeof(Profile);
        }

        uint get_n_items() {
            // All that is in the ini file plus one for the default profile
            return profiles.get_groups().length + 1;
        }

        GLib.Object? get_item (uint position) {
            if (position == 0) {
                return new Profile(_("System Profile"), Profile.SYSTEM,
                            Path.build_path(Path.DIR_SEPARATOR_S, Environment.get_user_data_dir(), "shotwell"),
                            this.profile == null);
            }

            try {
                var group = profiles.get_groups()[position - 1];
                var id = profiles.get_value(group, "Id");
                var name = profiles.get_value(group, "Name");
                var active = this.profile == name;
                return new Profile(profiles.get_value(group, "Name"),
                                id,
                                get_data_dir_for_profile(id, group),
                                active);
            } catch (KeyFileError err) {
                if (err is KeyFileError.GROUP_NOT_FOUND) {
                    assert_not_reached();
                }

                warning("Profile configuration file corrupt: %s", err.message);
            }

            return null;
        }

        private static ProfileManager instance;
        public static ProfileManager get_instance() {
            if (instance == null)
                instance = new ProfileManager();

            return instance;
        }

        private ProfileManager() {
            Object();
        }

        private void write() {
            try {
                profiles.save_to_file(path);
            } catch (Error error) {
                critical("Failed to write profiles: %s", error.message);
            }
        }

        private KeyFile profiles;
        private string profile = null;
        private string path;
        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);
            path = Path.build_filename(path, "profiles.ini");

            try {
                profiles.load_from_file(path, KeyFileFlags.KEEP_COMMENTS);
            } catch (Error error) {
                debug("Could not read profiles: %s", error.message);
            }
        }

        public bool has_profile (string profile, out string group_name = null) {
            group_name = Base64.encode (profile.data);
            return profiles.has_group(group_name);
        }

        public void set_profile(string profile) {
            message("Using profile %s for this session", profile);
            assert(this.profile == null);

            this.profile = profile;

            add_profile(Uuid.string_random(), profile, null, null);
        }

        public void add_profile(string id, string name, string? library_folder, string? data_folder) {
            if (has_profile(name, out this.group_name)) {
                return;
            }

            try {
                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(name));

                write();
            } catch (Error err) {
                error("Failed to create profile: %s", err.message);                
            }

            if (library_folder != null) {
                errno = 0;
                var f = File.new_for_commandline_arg(library_folder);
                try {
                    f.make_directory_with_parents();
                } catch (Error err) {
                    warning ("Failed to create library folder: %s", err.message);
                }
                var settings_path = "/org/gnome/shotwell/profiles/" + id + "/preferences/files/";

    
                var settings = new Settings.with_path("org.gnome.shotwell.preferences.files", settings_path);
                settings.set_string("import-dir", library_folder);
            }
            
            items_changed(profiles.get_groups().length, 0, 1);
        }

        public string derive_data_dir(string? data_dir) {
            if (data_dir != null) {
                debug ("Using user-provided data dir %s", data_dir);

                try {
                    profiles.get_string(group_name, "DataDir");
                } catch (Error error) {
                    if (profile != null && profile != "") {
                        profiles.set_string(group_name ,"DataDir", data_dir);
                        debug("Using %s as data dir for profile %s", data_dir, profile);
                        write();
                    }
                }

                return data_dir;
            }

            return Path.build_filename(Environment.get_user_data_dir(), "shotwell", "profiles", id());
        }

        public string id() {
            // We are not running on any profile
            if (profile == null || profile == "")
                return "";

            try {
                return profiles.get_string(group_name, "Id");
            } catch (Error error) {
                assert_not_reached();
            }
        }

        private string get_data_dir_for_profile(string id, string group) throws KeyFileError {
            if ("DataDir" in profiles.get_keys(group)) {
                return profiles.get_value(group, "DataDir");
            } else {
                return Path.build_filename(Environment.get_user_data_dir(), "shotwell", "profiles", id);
            }
        }

        public void print_profiles() {
            print("Available profiles:\n");
            print("-------------------\n");
            try {
                foreach (var group in profiles.get_groups()) {
                    print("Profile name: %s\n", profiles.get_value(group, "Name"));
                    var id = profiles.get_value(group, "Id");
                    print("Profile Id: %s\n", id);
                    print("Data dir: %s\n", get_data_dir_for_profile(id, group));
                    print("\n");
                }
            } catch (Error error) {
                print("Failed to print profiles: %s", error.message);
            }
        }

        const string SCHEMAS[] = {
            "sharing",
            "printing",
            "plugins.enable-state",
            "preferences.ui",
            "preferences.slideshow",
            "preferences.window",
            "preferences.files",
            "preferences.editing",
            "preferences.export",        
        };

        void reset_all_keys(Settings settings) {
            SettingsSchema schema;
            ((Object)settings).get("settings-schema", out schema, null);
        
            foreach (var key in schema.list_keys()) {
                debug("Resetting key %s", key);
                settings.reset(key);
            }
        
            foreach (var c in settings.list_children()) {
                debug("Checking children %s", c);
                var child = settings.get_child (c);
                reset_all_keys (child);
            }
        }
        
        private void remove_settings_recursively(string id) {
            var source = SettingsSchemaSource.get_default();
            foreach (var schema in SCHEMAS) {
                var path = "/org/gnome/shotwell/profiles/%s/%s/".printf(id, schema.replace(".", "/"));
                var schema_name = "org.gnome.shotwell.%s".printf(schema);
                debug("%s @ %s", schema_name, path);
                var schema_definition = source.lookup(schema_name, false);
                var settings = new Settings.full (schema_definition, null, path);
                settings.delay();
                reset_all_keys (settings);
                foreach (var key in schema_definition.list_keys()) {
                    debug("Resetting key %s", key);
                    settings.reset(key);
                }
                settings.apply();
                Settings.sync();
            }        
        }

        public void remove(string id, bool remove_all) {
            debug("Request to remove profile %s, with files? %s", id, remove_all.to_string());
            int index = 1;
            string group = null;

            foreach (var g in profiles.get_groups()) {
                try {
                    if (profiles.get_value(g, "Id") == id) {
                        group = g;
                        break;
                    }
                    index++;
                } catch (KeyFileError error) {
                    assert_not_reached();
                }
            }

            if (group != null) {
                string? data_dir = null;

                try {
                    data_dir = get_data_dir_for_profile(id, group);
                    // Remove profile
                    string? key = null;
                    profiles.remove_comment(group, key);
                    profiles.remove_group(group);
                } catch (KeyFileError err) {
                    // We checked the existence of the group above.
                    assert_not_reached();
                }

                remove_settings_recursively(id);

                if (remove_all) {
                    try {
                        var file = File.new_for_commandline_arg(data_dir);
                        file.trash();
                    } catch (Error error) {
                        warning("Failed to remove data folder: %s", error.message);
                    }
                }

                Idle.add(() => {
                    items_changed(index, 1, 0);

                    return false;
                });
                write();
            }
        }
    }
}