1/*
2 *      Copyright 2011 Julien Lavergne <gilir@ubuntu.com>
3 *
4 *      This program is free software; you can redistribute it and/or modify
5 *      it under the terms of the GNU General Public License as published by
6 *      the Free Software Foundation; either version 2 of the License, or
7 *      (at your option) any later version.
8 *
9 *      This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *      You should have received a copy of the GNU General Public License
15 *      along with this program; if not, write to the Free Software
16 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 *      MA 02110-1301, USA.
18 */
19
20/* TODO Split correctly the settings for enable differents backends (.ini, gsettings ...) */
21
22namespace Lxsession
23{
24    public class LxsessionConfig: GLib.Object
25    {
26
27        /* Session identification */
28        public string session_name { get; set; default = "LXDE";}
29        public string desktop_env_name { get; set; default = "LXDE";}
30
31        /* Settings db */
32        public HashTable<string, string> config_item_db;
33        public HashTable<string, string> session_support_item_db;
34        public HashTable<string, string> xsettings_support_item_db;
35        public HashTable<string, string> state_support_item_db;
36        public HashTable<string, string> dbus_support_item_db;
37        public HashTable<string, string> keymap_support_item_db;
38        public HashTable<string, string> environment_support_item_db;
39
40        public LxsessionConfig ()
41        {
42            config_item_db = init_item_db();
43            session_support_item_db = init_item_db();
44            xsettings_support_item_db = init_item_db();
45            state_support_item_db = init_item_db();
46            dbus_support_item_db = init_item_db();
47            keymap_support_item_db = init_item_db();
48            environment_support_item_db = init_item_db();
49        }
50
51        private HashTable<string, string> init_item_db ()
52        {
53            var return_map = new HashTable<string, string> (str_hash, str_equal);
54            return return_map;
55        }
56
57        public void create_config_item (string categorie, string key1, string? key2, string type, string? variable)
58        {
59            /* only support string for now */
60            string item_key = categorie + ";" + key1 + ";" + key2 + ";";
61
62            config_item_db[item_key] = variable;
63
64            update_support_keys (categorie, key1, key2);
65        }
66
67        public void delete_config_item (string categorie, string key1, string? key2, string type)
68        {
69            /* only support string for now */
70            string item_key = categorie + ";" + key1 + ";" + key2 + ";";
71
72            if (config_item_db.contains(item_key) == true)
73            {
74                config_item_db.remove(item_key);
75                update_support_keys (categorie, key1, key2);
76
77            }
78        }
79
80        public void get_item(string categorie, string key1, string? key2, out string variable, out string type)
81        {
82            /* only support string for now */
83            string item_key = categorie + ";" + key1 + ";" + key2 + ";";
84
85            // DEBUG message ("get_item item_key: %s", item_key);
86
87            variable = config_item_db[item_key];
88            type = "string";
89        }
90
91        public string get_item_string (string categorie, string key1, string? key2)
92        {
93            string type_output;
94            string variable_output;
95            get_item(categorie, key1, key2, out variable_output, out type_output);
96
97            return variable_output;
98        }
99
100        public void set_config_item_value (string categorie, string key1, string? key2, string type, string dbus_arg)
101        {
102            /*
103                Update config_item_db, or create the config_item if it's not exist.
104            */
105            string item_key = categorie + ";" + key1 + ";" + key2 +";";
106
107            // DEBUG message ("key of read_value: %s", item_key);
108
109            if (config_item_db.contains(item_key) == true)
110            {
111                // message ("Enter if of read_value for %s, %s, %s, %s, %s: ", categorie, key1, key2, type, dbus_arg);
112                if (config_item_db[item_key] != dbus_arg)
113                {
114                    config_item_db[item_key] = dbus_arg;
115                    on_update_generic(dbus_arg, categorie, key1, key2);
116                }
117            }
118            else
119            {
120                create_config_item(categorie, key1, key2, type, dbus_arg);
121            }
122         }
123
124        public void set_config_item_value_on_starting (string categorie, string key1, string? key2, string type, string dbus_arg)
125        {
126            /*
127                Update config_item_db, or create the config_item if it's not exist.
128            */
129            string item_key = categorie + ";" + key1 + ";" + key2 +";";
130
131            // DEBUG message ("key of read_value: %s", item_key);
132
133            if (config_item_db.contains(item_key) == true)
134            {
135                // message ("Enter if of read_value for %s, %s, %s, %s, %s: ", categorie, key1, key2, type, dbus_arg);
136                if (config_item_db[item_key] != dbus_arg)
137                {
138                    config_item_db[item_key] = dbus_arg;
139                }
140            }
141            else
142            {
143                create_config_item(categorie, key1, key2, type, dbus_arg);
144            }
145         }
146
147        public HashTable<string, string> get_support_db(string categorie)
148        {
149            var support_db = new HashTable<string, string> (str_hash, str_equal);
150            /* Init for session, so it will not be null */
151            support_db = session_support_item_db;
152
153            switch (categorie)
154            {
155                case "Session":
156                    support_db = session_support_item_db;
157                    break;
158                case "Xsettings":
159                    support_db = xsettings_support_item_db;
160                    break;
161                case "GTK":
162                    support_db = xsettings_support_item_db;
163                    break;
164                case "Mouse":
165                    support_db = xsettings_support_item_db;
166                    break;
167                case "Keyboard":
168                    support_db = xsettings_support_item_db;
169                    break;
170                case "State":
171                    support_db = state_support_item_db;
172                    break;
173                case "Dbus":
174                    support_db = dbus_support_item_db;
175                    break;
176                case "Keymap":
177                    support_db = keymap_support_item_db;
178                    break;
179                case "Environment":
180                    support_db = environment_support_item_db;
181                    break;
182            }
183
184            return support_db;
185        }
186
187        public void update_support_keys (string categorie, string key1, string? key2)
188        {
189            var support_db = new HashTable<string, string> (str_hash, str_equal);
190            support_db = get_support_db(categorie);
191
192            if (support_db.contains(key1))
193            {
194                string[] list = support_db[key1].split_set(";",0);
195                if (key2 == null)
196                {
197                    /* Pass, the key2 is empty, so no detailled support available*/
198                }
199                else
200                {
201                    if (key2 in list)
202                    {
203                        /* Pass, already in support */
204                    }
205                    else
206                    {
207                        support_db[key1] = support_db[key1] + key2 + ";";
208                    }
209                }
210            }
211            else
212            {
213                support_db[key1] = key2 + ";";
214            }
215        }
216
217        public string get_support (string categorie)
218        {
219            string items = null;
220            var support_db = new HashTable<string, string> (str_hash, str_equal);
221            support_db = get_support_db(categorie);
222
223            foreach (string key in support_db.get_keys())
224            {
225                if (items == null)
226                {
227                    items = key + ";";
228                }
229                else
230                {
231                    items = items + key + ";" ;
232                }
233            }
234
235            return items;
236        }
237
238        public string get_support_key (string categorie, string key1)
239        {
240            string return_value = null;
241            var support_db = new HashTable<string, string> (str_hash, str_equal);
242            support_db = get_support_db(categorie);
243
244            message("Return support key: %s", support_db[key1]);
245            return_value =  support_db[key1];
246
247            return return_value;
248        }
249
250        public void init_signal ()
251        {
252            /* Connect to signals changes */
253            global_sig.generic_set_signal.connect(set_config_item_value);
254        }
255
256        public void guess_default()
257        {
258            /* Migrate old windows-manager settings to the new ones */
259            if (get_item_string("Session", "window_manager", null) == "openbox-lxde")
260            {
261                set_generic_default("Session", "windows_manager", "command", "string", "openbox");
262                set_generic_default("Session", "windows_manager", "session", "string", "LXDE");
263            }
264
265            /* Keep old behavior for autostarted application if this option is not specify */
266            set_generic_default("Session", "disable_autostart", null, "string", "no");
267
268            set_generic_default("Session", "polkit", "command", "string", "lxpolkit");
269            set_generic_default("Session", "clipboard", "command", "string", "lxclipboard");
270            set_generic_default("Session", "xsettings_manager", "command", "string", "build-in");
271            set_generic_default("Session", "proxy_manager", "command", "string", "build-in");
272            set_generic_default("Session", "keyring", "command", "string", "ssh-agent");
273
274            /* Set Xsettings default */
275
276            set_generic_default("GTK", "iXft", "Antialias", "string", "1");
277            set_generic_default("GTK", "iXft", "Hinting", "string", "1");
278            set_generic_default("GTK", "sXft", "HintStyle", "string", "hintslight");
279            set_generic_default("GTK", "sXft", "RGBA", "string", "rgb");
280
281            set_generic_default("GTK", "sNet", "ThemeName", "string", "Adwaita");
282            set_generic_default("GTK", "sNet", "IconThemeName", "string", "nuoveXT2");
283            set_generic_default("GTK", "iNet", "EnableEventSounds", "string", "1");
284            set_generic_default("GTK", "iNet", "EnableInputFeedbackSounds", "string", "1");
285            set_generic_default("GTK", "sGtk", "ColorScheme", "string", "");
286            set_generic_default("GTK", "sGtk", "FontName", "string", "Sans 10");
287            set_generic_default("GTK", "iGtk", "ToolbarStyle", "string", "3");
288            set_generic_default("GTK", "iGtk", "ToolbarIconSize", "string", "3");
289            set_generic_default("GTK", "iGtk", "ButtonImages", "string", "1");
290            set_generic_default("GTK", "iGtk", "MenuImages", "string", "1");
291            set_generic_default("GTK", "iGtk", "CursorThemeSize", "string", "18");
292            set_generic_default("GTK", "sGtk", "CursorThemeName", "string", "DMZ-White");
293/*
294            TODO    Add also the ones from the spec : http://www.freedesktop.org/wiki/Specifications/XSettingsRegistry/
295                    And the commented one of the desktop.conf.example
296
297*/
298            set_generic_default("Mouse", "AccFactor", null, "string", "20");
299            set_generic_default("Mouse", "AccThreshold", null, "string", "10");
300            set_generic_default("Mouse", "LeftHanded", null, "string", "0");
301
302            set_generic_default("Keyboard", "Delay", null, "string", "500");
303            set_generic_default("Keyboard", "Interval", null, "string", "30");
304            set_generic_default("Keyboard", "Beep", null, "string", "1");
305
306            /* Misc */
307            set_generic_default("State", "guess_default", null, "string", "true");
308            set_generic_default("Dbus", "lxde", null, "string", "true");
309            set_generic_default("Environment", "menu_prefix", null, "string", "lxde-");
310
311            /*  Distributions, if you want to ensure good transition from previous version of lxsession
312                you need to patch here to set the default for various new commands
313                See Lubuntu example below
314            */
315
316            if (this.session_name == "Lubuntu")
317            {
318                set_generic_default("Session", "quit_manager", "command", "string", "lxsession-logout");
319                set_generic_default("Session", "quit_manager", "image", "string", "/usr/local/share/lubuntu/images/logout-banner.png");
320                set_generic_default("Session", "quit_manager", "layout", "string", "top");
321
322                /* Migrate old windows-manager settings to the new ones */
323                if (get_item_string("Session", "window_manager", null) == "openbox-lubuntu")
324                {
325                    set_generic_default("Session", "windows_manager", "command", "string", "openbox");
326                    set_generic_default("Session", "windows_manager", "session", "string", "Lubuntu");
327                }
328
329                set_generic_default("Session", "workspace_manager", "command", "string", "obconf");
330                set_generic_default("Session", "audio_manager", "command", "string", "alsamixer");
331                set_generic_default("Session", "screenshot_manager", "command", "string", "scrot");
332                set_generic_default("Session", "upgrade_manager", "command", "string", "upgrade-manager");
333
334                set_generic_default("Session", "webbrowser", "command", "string", "firefox");
335                set_generic_default("Session", "email", "command", "string", "sylpheed");
336                set_generic_default("Session", "pdf_reader", "command", "string", "evince");
337                set_generic_default("Session", "video_player", "command", "string", "gnome-mplayer");
338                set_generic_default("Session", "audio_player", "command", "string", "audacious");
339                set_generic_default("Session", "images_display", "command", "string", "gpicview");
340                set_generic_default("Session", "text_editor", "command", "string", "leafpad");
341                set_generic_default("Session", "archive", "command", "string", "file-roller");
342                set_generic_default("Session", "calculator", "command", "string", "galculator");
343                set_generic_default("Session", "spreadsheet", "command", "string", "gnumeric");
344                set_generic_default("Session", "bittorent", "command", "string", "transmission-gtk");
345                set_generic_default("Session", "document", "command", "string", "abiword");
346                set_generic_default("Session", "webcam", "command", "string", "gucview");
347                set_generic_default("Session", "burn", "command", "string", "xfburn");
348                set_generic_default("Session", "notes", "command", "string", "xpad");
349                set_generic_default("Session", "disk_utility", "command", "string", "gnome-disks");
350                set_generic_default("Session", "tasks", "command", "string", "lxtask");
351
352            }
353            if (this.desktop_env_name == "LXDE")
354            {
355                /* We are under a LXDE generic desktop, guess some LXDE default */
356                set_generic_default("Session", "quit_manager", "command", "string", "lxsession-logout");
357                set_generic_default("Session", "quit_manager", "image", "string", "/usr/local/share/lxde/images/logout-banner.png");
358                set_generic_default("Session", "quit_manager", "layout", "string", "top");
359
360                set_generic_default("Session", "lock_manager", "command", "string", "lxlock");
361                set_generic_default("Session", "terminal_manager", "command", "string", "lxterminal");
362                set_generic_default("Session", "launcher_manager", "command", "string", "lxpanelctl");
363            }
364        }
365
366        public void set_generic_default(string categorie, string key1, string? key2, string type, string default_value)
367        {
368            switch (type)
369            {
370                case "string":
371                    if (get_item_string(categorie, key1, key2) == null)
372                    {
373                        message ("Settings default for %s, %s, %s : %s", categorie, key1, key2, default_value);
374                        set_config_item_value(categorie, key1, key2, type, default_value);
375                    }
376                    break;
377            }
378        }
379
380        public void on_update_generic (string dbus_arg, string categorie, string key1, string? key2)
381        {
382            string item_key = categorie + ";" + key1 + ";" + key2 +";";
383
384            string type = "string";
385
386            // message ("key of set_value: %s", item_key);
387
388            if (config_item_db.contains(item_key) == true)
389            {
390                switch (type)
391                {
392                    case "string":
393                        on_update_string_set (dbus_arg, categorie, key1, key2);
394                        break;
395                }
396            }
397        }
398
399        public virtual void on_update_string_set (string dbus_arg, string kf_categorie, string kf_key1, string? kf_key2)
400        {
401
402        }
403
404        public virtual void on_update_string_list_set (string[] dbus_arg, string kf_categorie, string kf_key1, string? kf_key2)
405        {
406
407        }
408
409        public virtual void on_update_int_set (int dbus_arg, string kf_categorie, string kf_key1, string? kf_key2)
410        {
411
412        }
413    }
414
415public class LxsessionConfigKeyFile: LxsessionConfig
416{
417    /* Settings locations */
418    public KeyFile kf = new KeyFile();
419    public string desktop_config_path { get; set; default = null;}
420    public string desktop_config_home_path { get; set; default = null;}
421    public GLib.File desktop_file ;
422    public GLib.File home_desktop_file ;
423    public GLib.FileMonitor monitor_desktop_file ;
424    public GLib.FileMonitor monitor_home_desktop_file;
425    public GLib.Cancellable monitor_cancel;
426
427    public LxsessionConfigKeyFile(string session_arg, string desktop_env_name_arg)
428    {
429        global_sig.reload_settings_daemon.connect(on_reload_settings_daemon);
430
431        init_desktop_files();
432
433        this.session_name = session_arg;
434        this.desktop_env_name = desktop_env_name_arg;
435
436        read_keyfile();
437
438        init_signal ();
439
440        /* Monitor desktop file */
441        setup_monitor_desktop_file();
442
443        /* Guess default */
444        if (get_item_string("State", "guess_default", null) != "false")
445        {
446            guess_default();
447        }
448    }
449
450    public void init_desktop_files()
451    {
452        desktop_config_path = get_config_path("desktop.conf");
453        desktop_config_home_path = get_config_home_path("desktop.conf");
454    }
455
456    public void setup_monitor_desktop_file()
457    {
458        try {
459            desktop_file = File.new_for_path(desktop_config_path);
460            monitor_desktop_file = desktop_file.monitor_file(GLib.FileMonitorFlags.NONE, monitor_cancel);
461            monitor_desktop_file.changed.connect(on_desktop_file_change);
462            message ("Monitoring: %s", desktop_config_path);
463
464            if ( desktop_file.get_path() == desktop_config_home_path)
465            {
466                 message ("Desktop file is already in config home, do nothing");
467            }
468            else
469            {
470                message ("Desktop file is not in config home, monitoring creation of it");
471                setup_creation_desktop_file();
472            }
473
474        } catch (GLib.Error err) {
475            message (err.message);
476        }
477    }
478
479    public void setup_creation_desktop_file()
480    {
481        try {
482            home_desktop_file = File.new_for_path(desktop_config_home_path);
483            monitor_home_desktop_file = home_desktop_file.monitor_file(GLib.FileMonitorFlags.NONE);
484            monitor_home_desktop_file.changed.connect(on_desktop_file_creation);
485            message ("Monitoring home path: %s", home_desktop_file.get_path());
486        } catch (GLib.Error err) {
487            message (err.message);
488        }
489    }
490
491    public void reload_xsettings ()
492    {
493        if (global_xsettings_manager == null)
494        {
495            var xsettings = new XSettingsOption();
496            global_xsettings_manager = xsettings;
497            global_xsettings_manager.activate();
498            message("Create a xsettings option");
499        }
500        else
501        {
502            global_xsettings_manager.reload();
503            message("Reload the xsettings option");
504        }
505    }
506
507    public void on_desktop_file_change ()
508    {
509        message("Desktop file change, reloading XSettings daemon");
510        reload_xsettings();
511    }
512
513    public void on_desktop_file_creation ()
514    {
515        message("Desktop file created in home directory, switch configuration to it");
516        desktop_config_path = desktop_config_home_path;
517        monitor_cancel.cancel();
518
519        reload_xsettings();
520        setup_monitor_desktop_file();
521    }
522
523    public string read_keyfile_string_value (KeyFile keyfile, string kf_categorie, string kf_key1, string? kf_key2, string? default_value)
524    {
525        string copy_value = null;
526        string return_value = null;
527        try
528        {
529            if (kf_key2 == null)
530            {
531                copy_value = keyfile.get_value (kf_categorie, kf_key1);
532            }
533            else
534            {
535                copy_value = keyfile.get_value (kf_categorie, kf_key1 + "/" + kf_key2);
536            }
537	    }
538        catch (KeyFileError err)
539        {
540		    message (err.message);
541        }
542
543        if (copy_value == null)
544        {
545            return_value = default_value;
546        }
547        else
548        {
549            if (default_value != copy_value)
550            {
551                return_value = copy_value;
552            }
553            else
554            {
555                return_value = default_value;
556            }
557        }
558
559        return return_value;
560    }
561
562    public int read_keyfile_int_value (KeyFile keyfile, string kf_categorie, string kf_key1, string? kf_key2, int default_value)
563    {
564        int return_value = 0;
565
566        try
567        {
568            if (kf_key2 == null)
569            {
570                return_value = keyfile.get_integer (kf_categorie, kf_key1);
571            }
572            else
573            {
574                return_value = keyfile.get_integer (kf_categorie, kf_key1 + "/" + kf_key2);
575            }
576	    }
577        catch (KeyFileError err)
578        {
579		    message (err.message);
580        }
581
582        if (return_value == 0)
583        {
584            return_value = default_value;
585        }
586        else
587        {
588            if (return_value == default_value)
589            {
590                return_value = default_value;
591            }
592        }
593        return return_value;
594    }
595
596    public string[] read_keyfile_string_list_value (KeyFile keyfile, string kf_categorie, string kf_key1, string? kf_key2, string[] default_value)
597    {
598        string[] copy_value = null;
599        string[] return_value = null;
600        try
601        {
602            if (kf_key2 == null)
603            {
604                copy_value = keyfile.get_string_list (kf_categorie, kf_key1);
605            }
606            else
607            {
608                copy_value = keyfile.get_string_list (kf_categorie, kf_key1 + "/" + kf_key2);
609            }
610	    }
611        catch (KeyFileError err)
612        {
613		    message (err.message);
614        }
615
616        if (copy_value == null)
617        {
618            return_value = default_value;
619        }
620        else
621        {
622            if (default_value != copy_value)
623            {
624                return_value = copy_value;
625            }
626            else
627            {
628                return_value = default_value;
629            }
630        }
631
632        return return_value;
633    }
634
635
636    public void read_key_value (KeyFile kf, string categorie, string key1, string? key2, string type)
637    {
638        string default_variable = null;
639        string final_variable = null;
640        string type_output = null;
641
642        string item_key = categorie + ";" + key1 + ";" + key2 +";";
643
644        switch (type)
645        {
646            case "string":
647                final_variable = read_keyfile_string_value(kf, categorie, key1, key2, default_variable);
648                break;
649        }
650
651        if (config_item_db.contains(item_key) == false)
652        {
653            // message ("Create new config key: %s", item_key);
654            create_config_item(categorie, key1, key2, type, final_variable);
655        }
656        else
657        {
658            get_item(categorie, key1, key2, out default_variable, out type_output);
659            set_config_item_value_on_starting(categorie, key1, key2, type, final_variable);
660        }
661
662    }
663
664    public void read_keyfile()
665    {
666        kf = load_keyfile (desktop_config_path);
667
668        /* Remove buggy keys */
669        if (read_keyfile_string_value(kf, "GTK", "iGtk", "ColorScheme", null) != null)
670        {
671            delete_config_item("GTK", "iGtk", "ColorScheme", "string");
672        }
673
674        /* Windows manager */
675        if (read_keyfile_string_value(kf, "Session", "windows_manager", "command", null) != null)
676        {
677            read_key_value(kf, "Session", "windows_manager", "command", "string");
678            read_key_value(kf, "Session", "windows_manager", "session", "string");
679            read_key_value(kf, "Session", "windows_manager", "extras", "string");
680        }
681        else
682        {
683            read_key_value(kf, "Session", "window_manager", null, "string");
684        }
685
686        /* Panel */
687        if (read_keyfile_string_value(kf, "Session", "panel", "command", null) != null)
688        {
689            read_key_value(kf, "Session", "panel", "command", "string");
690            read_key_value(kf, "Session", "panel", "session", "string");
691        }
692
693        /* Dock */
694        if (read_keyfile_string_value(kf, "Session", "dock", "command", null) != null)
695        {
696            read_key_value(kf, "Session", "dock", "command", "string");
697            read_key_value(kf, "Session", "dock", "session", "string");
698        }
699
700        /* File Manager */
701        if (read_keyfile_string_value(kf, "Session", "file_manager", "command", null) != null)
702        {
703            read_key_value(kf, "Session", "file_manager", "command", "string");
704            read_key_value(kf, "Session", "file_manager", "session", "string");
705            read_key_value(kf, "Session", "file_manager", "extras", "string");
706        }
707
708        /* Desktop handler */
709        if (read_keyfile_string_value(kf, "Session", "desktop_manager", "command", null) != null)
710        {
711            read_key_value(kf, "Session", "desktop_manager", "command", "string");
712            read_key_value(kf, "Session", "desktop_manager", "wallpaper", "string");
713        }
714
715        /* Launcher manager */
716        if (read_keyfile_string_value(kf, "Session", "launcher_manager", "command", null) != null)
717        {
718            read_key_value(kf, "Session", "launcher_manager", "command", "string");
719            read_key_value(kf, "Session", "launcher_manager", "autostart", "string");
720        }
721
722        /* Composite manager */
723        if (read_keyfile_string_value(kf, "Session", "composite_manager", "command", null) != null)
724        {
725            read_key_value(kf, "Session", "composite_manager", "command", "string");
726            read_key_value(kf, "Session", "composite_manager", "autostart", "string");
727        }
728
729        /* IM */
730        if (read_keyfile_string_value(kf, "Session", "im1", "command", null) != null)
731        {
732            read_key_value(kf, "Session", "im1", "command", "string");
733            read_key_value(kf, "Session", "im1", "autostart", "string");
734        }
735
736        if (read_keyfile_string_value(kf, "Session", "im2", "command", null) != null)
737        {
738            read_key_value(kf, "Session", "im2", "command", "string");
739            read_key_value(kf, "Session", "im2", "autostart", "string");
740        }
741
742        /* Widget */
743        if (read_keyfile_string_value(kf, "Session", "widget1", "command", null) != null)
744        {
745            read_key_value(kf, "Session", "widget1", "command", "string");
746            read_key_value(kf, "Session", "widget1", "autostart", "string");
747        }
748
749        /* Notification */
750        if (read_keyfile_string_value(kf, "Session", "notification", "command", null) != null)
751        {
752            read_key_value(kf, "Session", "notification", "command", "string");
753            read_key_value(kf, "Session", "notification", "autostart", "string");
754        }
755
756        /* Key bindings */
757        if (read_keyfile_string_value(kf, "Session", "keybindings", "command", null) != null)
758        {
759            read_key_value(kf, "Session", "keybindings", "command", "string");
760            read_key_value(kf, "Session", "keybindings", "autostart", "string");
761        }
762
763        /* IM manager */
764        if (read_keyfile_string_value(kf, "Session", "im_manager", "command", null) != null)
765        {
766            read_key_value(kf, "Session", "im_manager", "command", "string");
767            read_key_value(kf, "Session", "im_manager", "autostart", "string");
768        }
769
770        /* Other session applications */
771        read_key_value(kf, "Session", "screensaver", "command", "string");
772        read_key_value(kf, "Session", "power_manager", "command", "string");
773        read_key_value(kf, "Session", "polkit", "command", "string");
774        read_key_value(kf, "Session", "audio_manager", "command", "string");
775        read_key_value(kf, "Session", "quit_manager", "command", "string");
776
777        if (read_keyfile_string_value(kf, "Session", "quit_manager", "command", null) != null)
778        {
779            read_key_value(kf, "Session", "quit_manager", "command", "string");
780            read_key_value(kf, "Session", "quit_manager", "image", "string");
781            read_key_value(kf, "Session", "quit_manager", "layout", "string");
782        }
783
784        read_key_value(kf, "Session", "workspace_manager", "command", "string");
785        read_key_value(kf, "Session", "terminal_manager", "command", "string");
786        read_key_value(kf, "Session", "screenshot_manager", "command", "string");
787        read_key_value(kf, "Session", "lock_manager", "command", "string");
788        read_key_value(kf, "Session", "message_manager", "command", "string");
789        read_key_value(kf, "Session", "upgrade_manager", "command", "string");
790        read_key_value(kf, "Session", "updates_manager", "command", "string");
791        read_key_value(kf, "Session", "updates_manager", "timeout", "string");
792        read_key_value(kf, "Session", "crash_manager", "command", "string");
793        read_key_value(kf, "Session", "crash_manager", "dev_mode", "string");
794        read_key_value(kf, "Session", "crash_manager", "timeout", "string");
795        read_key_value(kf, "Session", "clipboard", "command", "string");
796        read_key_value(kf, "Session", "disable_autostart", null, "string");
797        read_key_value(kf, "Session", "upstart_user_session", null, "string");
798        read_key_value(kf, "Session", "xsettings_manager", "command", "string");
799        read_key_value(kf, "Session", "proxy_manager", "command", "string");
800        read_key_value(kf, "Session", "proxy_manager", "http", "string");
801        read_key_value(kf, "Session", "a11y", "command", "string");
802        read_key_value(kf, "Session", "keyring", "command", "string");
803        read_key_value(kf, "Session", "xrandr", "command", "string");
804        read_key_value(kf, "Session", "network_gui", "command", "string");
805
806        /* Mime applications */
807        read_key_value(kf, "Session", "webbrowser", "command", "string");
808        read_key_value(kf, "Session", "email", "command", "string");
809        read_key_value(kf, "Session", "pdf_reader", "command", "string");
810        read_key_value(kf, "Session", "video_player", "command", "string");
811        read_key_value(kf, "Session", "audio_player", "command", "string");
812        read_key_value(kf, "Session", "images_display", "command", "string");
813        read_key_value(kf, "Session", "text_editor", "command", "string");
814        read_key_value(kf, "Session", "archive", "command", "string");
815        read_key_value(kf, "Session", "charmap", "command", "string");
816        read_key_value(kf, "Session", "calculator", "command", "string");
817        read_key_value(kf, "Session", "spreadsheet", "command", "string");
818        read_key_value(kf, "Session", "bittorent", "command", "string");
819        read_key_value(kf, "Session", "document", "command", "string");
820        read_key_value(kf, "Session", "webcam", "command", "string");
821        read_key_value(kf, "Session", "burn", "command", "string");
822        read_key_value(kf, "Session", "notes", "command", "string");
823        read_key_value(kf, "Session", "disk_utility", "command", "string");
824        read_key_value(kf, "Session", "tasks", "command", "string");
825
826        /* Keymap */
827        if (read_keyfile_string_value(kf, "Keymap", "mode", null, null) != null)
828        {
829            read_key_value(kf, "Keymap", "mode", null, "string");
830            read_key_value(kf, "Keymap", "model", null, "string");
831            read_key_value(kf, "Keymap", "layout", null, "string");
832            read_key_value(kf, "Keymap", "variant", null, "string");
833            read_key_value(kf, "Keymap", "options", null, "string");
834        }
835
836        /* Other */
837        read_key_value(kf, "State", "laptop_mode", null, "string");
838        read_key_value(kf, "State", "guess_default", null, "string");
839        read_key_value(kf, "Dbus", "lxde", null, "string");
840        read_key_value(kf, "Dbus", "gnome", null, "string");
841        read_key_value(kf, "Environment", "type", null, "string");
842        read_key_value(kf, "Environment", "menu_prefix", null, "string");
843        read_key_value(kf, "Environment", "ubuntu_menuproxy", null, "string");
844        read_key_value(kf, "Environment", "toolkit_integration", null, "string");
845        read_key_value(kf, "Environment", "gtk", "overlay_scrollbar_disable", "string");
846        read_key_value(kf, "Environment", "qt", "force_theme", "string");
847        read_key_value(kf, "Environment", "qt", "platform", "string");
848
849        read_key_value(kf, "GTK", "sNet", "ThemeName", "string");
850        read_key_value(kf, "GTK", "sNet", "IconThemeName", "string");
851        read_key_value(kf, "GTK", "sGtk", "FontName", "string");
852        read_key_value(kf, "GTK", "iGtk", "ToolbarStyle", "string");
853        read_key_value(kf, "GTK", "iGtk", "ButtonImages", "string");
854        read_key_value(kf, "GTK", "iGtk", "MenuImages", "string");
855        read_key_value(kf, "GTK", "iGtk", "CursorThemeSize", "string");
856        read_key_value(kf, "GTK", "iXft", "Antialias", "string");
857        read_key_value(kf, "GTK", "iXft", "Hinting", "string");
858        read_key_value(kf, "GTK", "sXft", "HintStyle", "string");
859        read_key_value(kf, "GTK", "sXft", "RGBA", "string");
860        read_key_value(kf, "GTK", "sGtk", "ColorScheme", "string");
861        read_key_value(kf, "GTK", "sGtk", "CursorThemeName", "string");
862        read_key_value(kf, "GTK", "iGtk", "ToolbarIconSize", "string");
863        read_key_value(kf, "GTK", "iNet", "EnableEventSounds", "string");
864        read_key_value(kf, "GTK", "iNet", "EnableInputFeedbackSounds", "string");
865        read_key_value(kf, "Mouse", "AccFactor", null, "string");
866        read_key_value(kf, "Mouse", "AccThreshold", null, "string");
867        read_key_value(kf, "Mouse", "LeftHanded", null, "string");
868        read_key_value(kf, "Keyboard", "Delay", null, "string");
869        read_key_value(kf, "Keyboard", "Interval", null, "string");
870        read_key_value(kf, "Keyboard", "Beep", null, "string");
871
872        read_secondary_keyfile();
873
874    }
875
876    public virtual void read_secondary_keyfile()
877    {
878
879    }
880
881    public void sync_setting_files ()
882    {
883        string desktop_file = desktop_config_home_path;
884        if (FileUtils.test (desktop_file, FileTest.EXISTS))
885        {
886            string autostart_file = get_config_home_path("autostart");
887            if (FileUtils.test (autostart_file, FileTest.EXISTS))
888            {
889                /* File in sync, nothing to do */
890            }
891            else
892            {
893                var file = File.new_for_path (get_config_path("autostart"));
894                var destination = File.new_for_path (get_config_home_path("autostart"));
895                try
896                {
897                    file.copy (destination, FileCopyFlags.NONE);
898                }
899                catch (GLib.Error err)
900                {
901		            message (err.message);
902                }
903            }
904        }
905    }
906
907    public void save_keyfile ()
908    {
909        // message ("Saving desktop file");
910        var str = kf.to_data (null);
911        try
912        {
913            FileUtils.set_contents (desktop_config_path, str, str.length);
914        }
915        catch (FileError err)
916        {
917            warning (err.message);
918            try
919            {
920                /* Try to save on user config directory */
921                string user_config_dir = desktop_config_home_path;
922
923                File file_parent;
924                var file = File.new_for_path(user_config_dir);
925                file_parent = file.get_parent();
926                if (!file_parent.query_exists())
927                {
928                    try
929                    {
930                        file_parent.make_directory_with_parents();
931                    }
932                    catch (GLib.Error err)
933                    {
934                        warning (err.message);
935                    }
936                }
937
938                FileUtils.set_contents (user_config_dir, str, str.length);
939                desktop_config_path = user_config_dir;
940                setup_monitor_desktop_file();
941                sync_setting_files ();
942            }
943            catch (FileError err)
944            {
945                warning (err.message);
946            }
947        }
948        save_secondary_keyfile();
949    }
950
951    public virtual void save_secondary_keyfile()
952    {
953
954    }
955
956    public override void on_update_string_set (string dbus_arg, string kf_categorie, string kf_key1, string? kf_key2)
957    {
958        switch (kf_key2)
959            {
960                case null:
961                    // message("Changing %s - %s to %s" , kf_categorie, kf_key1, dbus_arg);
962                    kf.set_value (kf_categorie, kf_key1, dbus_arg);
963                    break;
964                case "":
965                    // message("Changing %s - %s to %s" , kf_categorie, kf_key1, dbus_arg);
966                    kf.set_value (kf_categorie, kf_key1, dbus_arg);
967                    break;
968                case " ":
969                    // message("Changing %s - %s to %s" , kf_categorie, kf_key1, dbus_arg);
970                    kf.set_value (kf_categorie, kf_key1, dbus_arg);
971                    break;
972                default:
973                    // message("Changing %s - %s - %s to %s" , kf_categorie, kf_key1, kf_key2, dbus_arg);
974                    kf.set_value (kf_categorie, kf_key1 + "/" + kf_key2, dbus_arg);
975                    break;
976            }
977        save_keyfile();
978    }
979
980    public override void on_update_string_list_set (string[] dbus_arg, string kf_categorie, string kf_key1, string? kf_key2)
981    {
982        switch (kf_key2)
983            {
984                case null:
985                    // message("Changing %s - %s" , kf_categorie, kf_key1);
986                    kf.set_string_list (kf_categorie, kf_key1, dbus_arg);
987                    break;
988                case "":
989                    // message("Changing %s - %s" , kf_categorie, kf_key1);
990                    kf.set_string_list (kf_categorie, kf_key1, dbus_arg);
991                    break;
992                case " ":
993                    // message("Changing %s - %s" , kf_categorie, kf_key1);
994                    kf.set_string_list (kf_categorie, kf_key1, dbus_arg);
995                    break;
996                default:
997                    // message("Changing %s - %s - %s" , kf_categorie, kf_key1, kf_key2);
998                    kf.set_string_list (kf_categorie, kf_key1 + "/" + kf_key2, dbus_arg);
999                    break;
1000            }
1001        save_keyfile();
1002    }
1003
1004    public override void on_update_int_set (int dbus_arg, string kf_categorie, string kf_key1, string? kf_key2)
1005    {
1006        switch (kf_key2)
1007            {
1008                case null:
1009                    message("Changing %s - %s to %i" , kf_categorie, kf_key1, dbus_arg);
1010                    kf.set_integer (kf_categorie, kf_key1, dbus_arg);
1011                    break;
1012                case "":
1013                    message("Changing %s - %s to %i" , kf_categorie, kf_key1, dbus_arg);
1014                    kf.set_integer (kf_categorie, kf_key1, dbus_arg);
1015                    break;
1016                case " ":
1017                    message("Changing %s - %s to %i" , kf_categorie, kf_key1, dbus_arg);
1018                    kf.set_integer (kf_categorie, kf_key1, dbus_arg);
1019                    break;
1020                default:
1021                    message("Changing %s - %s - %s to %i" , kf_categorie, kf_key1, kf_key2, dbus_arg);
1022                    kf.set_integer (kf_categorie, kf_key1 + "/" + kf_key2, dbus_arg);
1023                    break;
1024            }
1025        save_keyfile();
1026    }
1027
1028    public void on_reload_settings_daemon ()
1029    {
1030        message("Reloading XSettings daemon");
1031        reload_xsettings();
1032    }
1033
1034}
1035
1036public class RazorQtConfigKeyFile: LxsessionConfigKeyFile
1037{
1038    public KeyFile kf_session = new KeyFile();
1039
1040    public string session_razor_config_path;
1041    public string session_razor_config_home_path;
1042    public GLib.File session_razor_file ;
1043    public GLib.File home_session_razor_file ;
1044
1045    public KeyFile kf_conf = new KeyFile();
1046
1047    public string conf_razor_config_path;
1048    public string conf_razor_config_home_path;
1049    public GLib.File confrazor_file ;
1050    public GLib.File home_conf_razor_file ;
1051
1052    public RazorQtConfigKeyFile(string session_arg, string desktop_env_name_arg)
1053    {
1054        base (session_arg, desktop_env_name_arg);
1055
1056            init_desktop_files();
1057            init_desktop_razor_files();
1058
1059            this.session_name = session_arg;
1060            this.desktop_env_name = desktop_env_name_arg;
1061
1062            read_keyfile();
1063
1064            init_signal ();
1065
1066            /* Monitor desktop file */
1067            setup_monitor_desktop_file();
1068
1069            /* Guess default */
1070            if (get_item_string("State", "guess_default", null) != "false")
1071            {
1072                guess_default();
1073            }
1074    }
1075
1076    public void init_desktop_razor_files()
1077    {
1078        session_razor_config_home_path = Path.build_filename(Environment.get_user_config_dir (), "razor", "session.conf");
1079        var home_session_razor_file = File.new_for_path(session_razor_config_home_path);
1080
1081        session_razor_config_path = Path.build_filename("etc", "xdg", "razor", "session.conf");
1082        var session_razor_file = File.new_for_path(session_razor_config_path);
1083
1084        if (home_session_razor_file.query_exists ())
1085        {
1086            session_razor_config_path = session_razor_config_home_path;
1087        }
1088        else if (session_razor_file.query_exists ())
1089        {
1090            /* Do nothing, keep session_razor_config_path value */
1091        }
1092        else
1093        {
1094            session_razor_config_path = session_razor_config_home_path;
1095            try
1096            {
1097                home_session_razor_file.create(FileCreateFlags.NONE);
1098            }
1099            catch (GLib.Error err)
1100            {
1101		        message (err.message);
1102            }
1103        }
1104
1105        conf_razor_config_home_path = Path.build_filename(Environment.get_user_config_dir (), "razor", "razor.conf");
1106        var home_conf_razor_file = File.new_for_path(conf_razor_config_home_path);
1107
1108        conf_razor_config_path = Path.build_filename("etc", "xdg", "razor", "razor.conf");
1109        var conf_razor_file = File.new_for_path(conf_razor_config_path);
1110
1111        if (home_conf_razor_file.query_exists ())
1112        {
1113            conf_razor_config_path = conf_razor_config_home_path;
1114        }
1115        else if (conf_razor_file.query_exists ())
1116        {
1117            /* Do nothing, keep conf_razor_config_path value */
1118        }
1119        else
1120        {
1121            conf_razor_config_path = conf_razor_config_home_path;
1122            try
1123            {
1124                home_conf_razor_file.create(FileCreateFlags.NONE);
1125            }
1126            catch (GLib.Error err)
1127            {
1128		        message (err.message);
1129            }
1130        }
1131
1132    }
1133
1134    public void read_razor_key_value (KeyFile kf, string categorie, string key1, string? key2, string type, string categorie_razor, string key1_razor, string? key2_razor)
1135    {
1136        string default_variable = null;
1137        string final_variable = null;
1138        string type_output = null;
1139
1140        string item_key = categorie + ";" + key1 + ";" + key2 +";";
1141
1142        switch (type)
1143        {
1144            case "string":
1145                final_variable = read_razor_keyfile_bool_value(kf, categorie_razor, key1_razor, key2_razor, default_variable);
1146                break;
1147        }
1148
1149        if (config_item_db.contains(item_key))
1150        {
1151            message ("Create new config key: %s", item_key);
1152            create_config_item(categorie, key1, key2, type, null);
1153        }
1154        else
1155        {
1156            get_item(categorie, key1, key2, out default_variable, out type_output);
1157            set_config_item_value(categorie, key1, key2, type, final_variable);
1158        }
1159
1160    }
1161
1162    public override void read_secondary_keyfile()
1163    {
1164
1165        /* override razor menu prefix */
1166        set_generic_default("Environment", "menu_prefix", null, "string", "razor-");
1167
1168        kf_session = load_keyfile (session_razor_config_path);
1169
1170        /* Windows manager */
1171        read_razor_key_value(kf, "Session", "windows_manager", "command", "string", "General", "windowmanager", null);
1172
1173        /* Panel */
1174        read_razor_key_value(kf, "Session", "panel", "command", "string", "modules", "razor-panel", null);
1175        read_razor_key_value(kf, "Session", "desktop", "command", "string", "modules", "razor-desktop", null);
1176        read_razor_key_value(kf, "Session", "launcher_manager", "command", "string", "modules", "razor-runner", null);
1177
1178        if (get_item_string("Session", "launcher_manager", "command") == "razor-runner")
1179        {
1180            set_config_item_value("Session", "launcher_manager", "autostart", "string", "true");
1181        }
1182
1183        read_razor_key_value(kf, "Session", "polkit", "command", "string", "modules", "razor-policykit-agent", null);
1184
1185        /* TODO Convert this config on file to lxsession config
1186        razor-appswitcher=false
1187        */
1188
1189        kf_conf = load_keyfile (session_razor_config_path);
1190
1191        read_razor_key_value(kf_conf, "GTK", "sNet", "ThemeName", "string", "Theme", "theme", null);
1192        read_razor_key_value(kf_conf, "GTK", "sNet", "IconThemeName", "string", "Theme", "icon_theme", null);
1193
1194    }
1195
1196    public string read_razor_keyfile_bool_value (KeyFile keyfile, string kf_categorie, string kf_key1, string? kf_key2, string? default_value)
1197    {
1198        string copy_value = null;
1199        string return_value = null;
1200        try
1201        {
1202            if (kf_key2 == null)
1203            {
1204                copy_value = keyfile.get_value (kf_categorie, kf_key1);
1205            }
1206            else
1207            {
1208                copy_value = keyfile.get_value (kf_categorie, kf_key1 + "/" + kf_key2);
1209            }
1210	    }
1211        catch (KeyFileError err)
1212        {
1213		    message (err.message);
1214        }
1215
1216
1217        if (copy_value == null)
1218        {
1219            return_value = default_value;
1220        }
1221        else
1222        {
1223            if (copy_value == "true")
1224            {
1225                return_value = kf_key1;
1226            }
1227            else
1228            {
1229                return_value = default_value;
1230            }
1231        }
1232
1233        return return_value;
1234    }
1235
1236
1237    public override void save_secondary_keyfile()
1238    {
1239        session_razor_config_path = session_razor_config_home_path;
1240        conf_razor_config_path = conf_razor_config_home_path;
1241
1242        kf_session.set_value ("General", "windowmanager", get_item_string("Session", "windows_manager", "command"));
1243
1244        if (get_item_string("Session", "panel", "command") == "razor-panel")
1245        {
1246            kf_session.set_value ("modules", "razor-panel", "true");
1247        }
1248        else
1249        {
1250            kf_session.set_value ("modules", "razor-panel", "false");
1251        }
1252
1253        if (get_item_string("Session", "desktop", "command") == "razor-desktop")
1254        {
1255            kf_session.set_value ("modules", "razor-desktop", "true");
1256        }
1257        else
1258        {
1259            kf_session.set_value ("modules", "razor-desktop", "false");
1260        }
1261
1262        if (get_item_string("Session", "launcher_manager", "command") == "razor-runner")
1263        {
1264            kf_session.set_value ("modules", "razor-runner", "true");
1265        }
1266        else
1267        {
1268            kf_session.set_value ("modules", "razor-runner", "false");
1269        }
1270
1271        if (get_item_string("Session", "polkit", "command") == "razor-policykit-agent")
1272        {
1273            kf_session.set_value ("modules", "razor-policykit-agent", "true");
1274        }
1275        else
1276        {
1277            kf_session.set_value ("modules", "razor-policykit-agent", "false");
1278        }
1279
1280        /* TODO Convert this config on file to lxsession config
1281        razor-appswitcher=false
1282        */
1283
1284        message ("Saving razor session file");
1285        var str_session = kf_session.to_data (null);
1286        try
1287        {
1288            FileUtils.set_contents (session_razor_config_path, str_session, str_session.length);
1289        }
1290        catch (FileError err)
1291        {
1292            warning (err.message);
1293        }
1294
1295        message ("Saving razor conf file");
1296        var str_conf = kf_conf.to_data (null);
1297        try
1298        {
1299            FileUtils.set_contents (conf_razor_config_path, str_conf, str_conf.length);
1300        }
1301        catch (FileError err)
1302        {
1303            warning (err.message);
1304        }
1305    }
1306}
1307
1308}
1309