File: autostart.vala

package info (click to toggle)
lxsession 0.5.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,188 kB
  • sloc: ansic: 4,087; makefile: 523; sh: 238; xml: 114
file content (317 lines) | stat: -rw-r--r-- 10,750 bytes parent folder | download | duplicates (2)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* 
    Copyright 2013 Julien Lavergne <gilir@ubuntu.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using Gtk;

namespace LDefaultApps
{
    public string read_autostart_conf ()
    {
        string config_path;
        config_path = get_config_home_path("autostart");
        var config_file = File.new_for_path (config_path);
        if (!config_file.query_exists ())
        {
            string config_system_path;
            config_system_path = get_config_path("autostart");

            if (config_system_path == null)
            {
                /* No system file and no home file, create a blank file in home */
                var config_parent = config_file.get_parent();

                if (!config_parent.query_exists ())
                {
                    try
                    {
                        config_parent.make_directory_with_parents ();
                    }
                    catch (GLib.Error e)
                    {
                        message (e.message);
                    }
                }

                try
                {
                    message("Create blank file");
                    File blank_file = File.new_for_path (config_path);
		            blank_file.create (FileCreateFlags.PRIVATE);
                }
                catch (GLib.Error e)
                {
                    message (e.message);
                }
            }
            else
            {
                File file = File.new_for_path (config_system_path);
                var config_parent = config_file.get_parent();

                if (!config_parent.query_exists ())
                {
                    try
                    {
                        config_parent.make_directory_with_parents ();
                    }
                    catch (GLib.Error e)
                    {
                        message (e.message);
                    }
                }

               try
                {
                    file.copy (config_file, FileCopyFlags.NONE);
                }
                catch (GLib.Error e)
                {
                    message (e.message);
                }
            }
        }
        message("Conf file for autostart: %s", config_path);
        return config_path;
    }

    public void manual_autostart_init (Builder builder)
    {
        var autostart_conf = read_autostart_conf();

        if (autostart_conf == null)
        {
            message("Can't find an autostart file, abort");
        }
        else
        {
            FileStream stream = FileStream.open (autostart_conf, "r");
	        assert (stream != null);

            message ("Autostart conf file : %s", autostart_conf);

            var auto_align = builder.get_object("autostart_alignment") as Gtk.Alignment;
            var auto_vbox = builder.get_object("manual_autostart_vbox") as Gtk.VBox;

            foreach (var widget in auto_vbox.get_children())
            {
                auto_vbox.remove(widget);
            }

	        string? line = null;
	        while ((line = stream.read_line ()) != null)
            {
                message("Autostart line : %s", line);
                var hbox = new HBox(false, 0);
                var check = new Gtk.CheckButton.with_label (line);
                if (line[0:1] == "#")
                {
                    check.set_active(false);
                }
                else
                {
                    check.set_active(true);
                }

                check.toggled.connect (() => {
                    message("Label to update : %s", check.get_label());
                    if (check.get_active())
                    {
                        if (check.get_label()[0:1] == "#")
                        {
                                update_autostart_conf(check.get_label(), "activate", builder);
                                message("Activate : %s", check.get_label());
                        }
                    }
                    else
                    {
                        if (check.get_label()[0:1] != "#")
                        {
                                update_autostart_conf(check.get_label(), "desactivate", builder);
                                message("Deactivate : %s", check.get_label());
                        }
                    }
                });

		        hbox.pack_start(check, false, false, 0);

                var button = new Button.from_stock("gtk-remove");
                button.clicked.connect (() => {
                    update_autostart_conf(check.get_label(), "remove", builder);
                    message ("try to remove : %s", check.get_label());
                });

		        hbox.pack_start(button, false, false, 0);

                auto_vbox.pack_start(hbox, false, false, 0);
	        }

            var add_hbox = new HBox(false, 0);
            var add_button = new Button.from_stock("gtk-add");
            var add_entry = new Entry();
            add_hbox.pack_start(add_button, false, false, 0);
            add_hbox.pack_start(add_entry, false, false, 0);
            auto_vbox.pack_start(add_hbox, false, false, 0);
            auto_align.add(auto_vbox);
            add_button.clicked.connect (() => {
                update_autostart_conf(add_entry.get_text(), "add", builder);
                add_entry.set_text("");
            });

            auto_vbox.show_all();
        }
    }

    public void update_autostart_conf (string line, string action, Builder builder)
    {
        var new_line = new StringBuilder ();
        switch (action)
        {
            case ("activate"):
                new_line.append(line);
                new_line.erase(0,1);
                new_line.append("\n");
                break;
            case ("desactivate"):
                new_line.append("#");
                new_line.append(line);
                new_line.append("\n");
                break;
            case ("add"):
                new_line.append(line);
                new_line.append("\n");
                break;
            case ("remove"):
                break;
        }
        try
        {
            string tmp_path = Path.build_filename(Environment.get_user_cache_dir(), "lxsession-default-apps", "autostart.tmp");
            var tmp_file = File.new_for_path (tmp_path);
            var dest_file = File.new_for_path (read_autostart_conf());

            FileStream stream = FileStream.open (read_autostart_conf(), "r");
            var tmp_stream = new DataOutputStream (tmp_file.create (FileCreateFlags.REPLACE_DESTINATION));

	        assert (stream != null);
	        string? read = null;
	        while ((read = stream.read_line ()) != null)
            {
                message ("read : %s", read);
                message ("line : %s", line); 
                if (read == line)
                {
                    tmp_stream.put_string(new_line.str);
                }
                else
                {
                    tmp_stream.put_string(read);
                    tmp_stream.put_string("\n");
                }
            }

            if (action == "add")
            {
                tmp_stream.put_string(new_line.str);
            }

            tmp_file.copy(dest_file, FileCopyFlags.OVERWRITE);
            tmp_file.delete();
            
            manual_autostart_init(builder);
        }
        catch (GLib.Error e)
        {
            message (e.message);
        }
        
    }

    public void autostart_core_applications (Builder builder, DbusBackend dbus_backend)
    {
        /* TODO Finish this
        var vbox = builder.get_object("autostart_core_applications") as Gtk.VBox;
        var item_box = new Gtk.VBox(false, 0);
        vbox.add(item_box);

        First, applications that are always autostarted (don't accept to disable)
        string[] list_not_disable = {"windows_manager", "panel"};

        for (int a = 0 ; a < list_not_disable.length ; a++)
        {
            string default_text = dbus_backend.Get(list_not_disable[a],"command");
            if ( default_text != "")
            {
                var item_label = new Label(default_text);
                item_box.add(item_label);
            }

        }

        vbox.show_all();
        */

    }

    /* TODO make the 2 following function common, or Dbus, or env, or anything to replace this c&p */
    public string get_config_home_path (string conf_file)
    {

        string user_config_dir = Path.build_filename(
                                 Environment.get_user_config_dir (),
                                 "lxsession",
                                 Environment.get_variable("DESKTOP_SESSION"),
                                 conf_file);

        return user_config_dir;

    }


    public string get_config_path (string conf_file) {

        string final_config_file;

        string user_config_dir = get_config_home_path(conf_file);

        if (FileUtils.test (user_config_dir, FileTest.EXISTS))
        {
            message ("User config used : %s", user_config_dir);
            final_config_file = user_config_dir;
        }
        else
        {
            string[] system_config_dirs = Environment.get_system_config_dirs ();
            string config_system_location = null;
            string path_system_config_file = null;

            foreach (string config in (system_config_dirs)) {
                config_system_location = Path.build_filename (config, "lxsession", Environment.get_variable("DESKTOP_SESSION"));
                message ("Config system location : %s", config_system_location);
                if (FileUtils.test (config_system_location, FileTest.EXISTS)) {
                    path_system_config_file = Path.build_filename (config_system_location, conf_file);
                    break;
                }
            }
          message ("System system path location : %s", path_system_config_file);
          final_config_file =  path_system_config_file;

         }
         message ("Final file used : %s", final_config_file);
         return final_config_file;

    }

}