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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
/*
* Copyright (C) 2008 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* 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/>.
*
* Authored by Neil Jagdish Patel <neil.patel@canonical.com>
*
*/
#include <config.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include <gconf/gconf.h>
#include <gconf/gconf-client.h>
#define LAUNCHER_NAME "netbook-launcher"
#define LAUNCHER_DESKTOP LAUNCHER_NAME".desktop"
#define OLD_LAUNCHER_NAME "ume-launcher"
#define OLD_LAUNCHER_DESKTOP OLD_LAUNCHER_NAME".desktop"
#define CLASSIC_PANEL_CONFIG "/classic-panel-config.xml"
#define NETBOOK_PANEL_CONFIG "/netbook-panel-config.xml"
#define SHARE_CLASSIC_PANEL_CONFIG PKGDATADIR CLASSIC_PANEL_CONFIG
#define SHARE_NETBOOK_PANEL_CONFIG PKGDATADIR NETBOOK_PANEL_CONFIG
#define SAVED_PANEL_CONF "/.config/desktop-switcher/last-panel-configuration.xml"
#define GNOME_SWITCH_USER_DISABLED "/desktop/gnome/lockdown/disable_user_switching"
#define SCREENSAVER_SWITCH_USER_ENABLED "/apps/gnome-screensaver/user_switch_enabled"
#define NAUTILUS_SHOW_DESKTOP "/apps/nautilus/preferences/show_desktop"
#define THEME "/desktop/gnome/interface/gtk_theme"
#define MAXIMUS "/apps/maximus/undecorate"
#define GCONF_STOP "gconftool-2 --shutdown"
#define GCONF_UNSET "gconftool-2 --recursive-unset /apps/panel"
#define GCONF_LOAD "gconftool-2 --load "
#define GCONF_DUMP_CLASSIC_PANEL PREFIX"/bin/gconf-panel-saving.sh "CLASSIC_PANEL_CONFIG
#define GCONF_DUMP_NETBOOK_PANEL PREFIX"/bin/gconf-panel-saving.sh "NETBOOK_PANEL_CONFIG
#define SESSION_COMPONENTS "/desktop/gnome/session/required_components_list"
enum
{
NETBOOK_MODE = 0,
CLASSIC_MODE
};
static gboolean create_dir (const gchar *dir);
static void enable_netbook ();
static void enable_classic ();
static gboolean check_in_classic_mode ();
static gint current_mode = NETBOOK_MODE;
static gint selected_mode = NETBOOK_MODE;
static gchar *switch_to = NULL;
static GOptionEntry entries[] =
{
{
"set-mode", 'm',
0,
G_OPTION_ARG_STRING, &switch_to,
"Switch to mode. Where mode equals \"netbook\" or \"classic\"",
NULL
},
{
NULL
}
};
gint
main (gint argc, gchar *argv[])
{
GtkBuilder *builder;
GError *error = NULL;
GtkWidget *dialog;
GtkWidget *netbook_radio;
GtkWidget *classic_radio;
gchar *autostart;
bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
textdomain (GETTEXT_PACKAGE);
g_thread_init (NULL);
g_type_init ();
g_set_application_name ("Desktop Switcher");
gtk_init_with_args (&argc, &argv,
"Desktop Switcher", entries,
GETTEXT_PACKAGE, NULL);
/* Load the user interface */
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, PKGDATADIR"/ui.xml", &error);
if (error)
{
g_warning ("Unable to load ui: %s\n", error->message);
g_error_free (error);
return EXIT_FAILURE;
}
gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
g_print ("In classic mode: %s\n", check_in_classic_mode () ? "true":"false");
/* Make sure the autostart directory exists */
autostart = g_build_filename (g_get_home_dir (), ".config","autostart", NULL);
create_dir (autostart);
g_free (autostart);
/* User config directory */
autostart = g_build_filename (g_get_home_dir (), ".config",
"desktop-switcher", NULL);
create_dir (autostart);
g_free (autostart);
if (switch_to)
{
gboolean in_classic;
in_classic = check_in_classic_mode ();
if (strcmp (switch_to, "netbook") == 0)
{
if (in_classic)
{
current_mode = CLASSIC_MODE;
selected_mode = NETBOOK_MODE;
enable_netbook ();
}
else
{
g_warning ("You are already in netbook mode");
return EXIT_SUCCESS;
}
}
else if (strcmp (switch_to, "classic") == 0)
{
if (!in_classic)
{
current_mode = NETBOOK_MODE;
selected_mode = CLASSIC_MODE;
enable_classic ();
}
else
{
g_warning ("You are already in classic mode");
return EXIT_SUCCESS;
}
}
else
{
g_warning ("%s mode does not exist", switch_to);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/* Get all the widgets */
dialog = GTK_WIDGET (gtk_builder_get_object (builder, "dialog1"));
netbook_radio = GTK_WIDGET (gtk_builder_get_object (builder, "netbookradio"));
classic_radio = GTK_WIDGET (gtk_builder_get_object (builder, "classicradio"));
/* Check which mode we're in */
if (check_in_classic_mode ())
{
g_object_set (classic_radio, "active", TRUE, NULL);
current_mode = selected_mode = CLASSIC_MODE;
}
else
{
g_object_set (netbook_radio, "active", TRUE, NULL);
}
/* Connect up the signals & show the window */
gtk_builder_connect_signals (builder, NULL);
gtk_widget_show_all (dialog);
gtk_main ();
return EXIT_SUCCESS;
}
static gboolean
check_in_classic_mode (void)
{
gchar *argv[] = { "pgrep", "-lu", NULL, "netbook", NULL };
gchar *output = NULL;
gchar *error = NULL;
gint exit_status = 0;
gboolean retval = TRUE;
argv[2] = (gchar*)g_get_user_name ();
g_spawn_sync (NULL, argv, NULL,
G_SPAWN_SEARCH_PATH, NULL, NULL,
&output, &error, &exit_status, NULL);
if (output)
{
if (g_strstr_len (output, strlen (output), "launch"))
retval = FALSE;
}
/* Check if the old version of the launcher, ume-launcher, is running */
if (retval == TRUE)
{
argv[3] = "ume";
g_spawn_sync (NULL, argv, NULL,
G_SPAWN_SEARCH_PATH, NULL, NULL,
&output, &error, &exit_status, NULL);
if (output)
{
if (g_strstr_len (output, strlen (output), "launch"))
{
retval = FALSE;
}
}
}
return retval;
}
static gboolean
create_dir (const gchar *dir)
{
if (!g_file_test (dir, G_FILE_TEST_EXISTS))
{
g_mkdir_with_parents (dir, 0700);
return TRUE;
}
return FALSE;
}
void
on_netbookradio_toggled (GtkToggleButton *button)
{
if (gtk_toggle_button_get_active (button))
selected_mode = NETBOOK_MODE;
}
void
on_classicradio_toggled (GtkToggleButton *button)
{
if (gtk_toggle_button_get_active (button))
selected_mode = CLASSIC_MODE;
}
static gchar *
launcher_autostart_file (const gchar *autostart_name)
{
return g_build_filename (g_get_home_dir (),
".config", "autostart",
autostart_name,
NULL);
}
static void
delete_launcher_autostart_file (const gchar *autostart_name)
{
gchar *filename;
GFile *file;
GError *error = NULL;
filename = launcher_autostart_file (autostart_name);
file = g_file_new_for_path (filename);
g_file_delete (file, NULL, &error);
if (error)
{
g_warning ("Unable to remove launcher autostart suppression file: %s",
error->message);
g_error_free (error);
}
g_object_unref (file);
g_free (filename);
}
static void
create_launcher_autostart_file (const gchar *autostart_name)
{
GFile *src, *dest;
gchar *filename;
GError *error = NULL;
filename = g_strdup_printf ("%s/%s", PKGDATADIR, autostart_name);
src = g_file_new_for_path (filename);
g_free (filename);
filename = launcher_autostart_file (autostart_name);
dest = g_file_new_for_path (filename);
g_file_copy (src, dest, 0, NULL, NULL, NULL, &error);
if (error)
{
g_debug ("Unable to suppress launcher autostart: %s", error->message);
g_error_free (error);
}
g_object_unref (dest);
g_object_unref (src);
g_free (filename);
}
static void
enable_netbook (void)
{
gchar *filename;
GError *error = NULL;
GConfClient *client = gconf_client_get_default ();
GdkScreen *screen = gdk_screen_get_default ();
g_debug ("Enabling netbook mode");
/* Make sure the launcher autostarts.
We do both because we can't be sure which launcher user is using. We
could check which is running, but that might not be the same as what is
installed, if the user just did an upgrade. No harm in handling both. */
g_debug ("Enabling launcher autostart");
delete_launcher_autostart_file (LAUNCHER_DESKTOP);
delete_launcher_autostart_file (OLD_LAUNCHER_DESKTOP);
/* Tell GNOME to disallow user switching */
error = NULL;
gconf_client_set_bool (client, GNOME_SWITCH_USER_DISABLED, TRUE, &error);
if (error)
{
g_warning ("Can't make GNOME disallow user switching: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Tell screensaver to disallow user switching */
error = NULL;
gconf_client_set_bool (client, SCREENSAVER_SWITCH_USER_ENABLED, FALSE, &error);
if (error)
{
g_warning ("Can't make screensaver disallow user switching: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Tell nautilus not to draw the desktop */
error = NULL;
gconf_client_set_bool (client, NAUTILUS_SHOW_DESKTOP, FALSE, &error);
if (error)
{
g_warning ("Can't stop Nautilus from drawing the desktop: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Set the theme */
error = NULL;
gconf_client_set_string (client, THEME, "Human-Murrine-Netbook", &error);
if (error)
{
g_warning ("Can't change the theme: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Set maximus */
error = NULL;
gconf_client_set_bool (client, MAXIMUS, TRUE, &error);
if (error)
{
g_warning ("Can't change Maximus's undecorate setting: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Launch the launcher */
g_debug ("Launching the, er, Launcher");
filename = g_find_program_in_path (LAUNCHER_NAME);
if (filename)
gdk_spawn_command_line_on_screen (screen, LAUNCHER_NAME, NULL);
else
gdk_spawn_command_line_on_screen (screen, OLD_LAUNCHER_NAME, NULL);
g_free (filename);
sleep (1);
/* Save current panel configuration */
filename = g_build_filename (g_get_home_dir (), ".config/desktop-switcher",
NETBOOK_PANEL_CONFIG, NULL);
g_spawn_command_line_sync (GCONF_DUMP_CLASSIC_PANEL, NULL, NULL, NULL, NULL);
/* Set the panel to the netbook layout */
g_debug ("Restoring panel settings");
gdk_spawn_command_line_on_screen (screen, GCONF_STOP, NULL);
gdk_spawn_command_line_on_screen (screen, GCONF_UNSET, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
{
gchar *exec;
exec = g_strdup_printf ("%s %s", GCONF_LOAD, filename);
g_spawn_command_line_sync (exec, NULL, NULL, NULL, NULL);
g_free (exec);
g_debug ("\nRestored panel configuration from: %s\n", filename);
}
else
{
gdk_spawn_command_line_on_screen (screen,
GCONF_LOAD SHARE_NETBOOK_PANEL_CONFIG,
NULL);
g_debug ("\nRestored panel configuration from defaults\n");
}
gdk_spawn_command_line_on_screen (screen, "killall gnome-panel", NULL);
gdk_spawn_command_line_on_screen (screen, "gnome-panel", NULL);
/* Remove nautilus from the session required components list */
g_debug ("Removing nautilus from gnome session startup");
GSList *comps = NULL;
comps = g_slist_append (comps, g_strdup ("windowmanager"));
comps = g_slist_append (comps, g_strdup ("panel"));
gconf_client_set_list (client, SESSION_COMPONENTS, GCONF_VALUE_STRING,
comps, NULL);
g_slist_foreach (comps, (GFunc)g_free, NULL);
g_slist_free (comps);
g_object_unref (client);
g_free (filename);
}
static void
enable_classic ()
{
GError *error = NULL;
gchar *filename;
GConfClient *client = gconf_client_get_default ();
GdkScreen *screen = gdk_screen_get_default ();
g_debug ("Enabling classic mode");
/* Stop the launcher from autostarting.
We do both because we can't be sure which launcher user is using. We
could check which is running, but that might not be the same as what is
installed, if the user just did an upgrade. No harm in handling both. */
g_debug ("Suppressing Launcher autostart");
create_launcher_autostart_file (LAUNCHER_DESKTOP);
create_launcher_autostart_file (OLD_LAUNCHER_DESKTOP);
/* Tell GNOME to allow user switching */
error = NULL;
gconf_client_set_bool (client, GNOME_SWITCH_USER_DISABLED, FALSE, &error);
if (error)
{
g_warning ("Can't make GNOME allow user switching: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Tell screensaver to allow user switching */
error = NULL;
gconf_client_set_bool (client, SCREENSAVER_SWITCH_USER_ENABLED, TRUE, &error);
if (error)
{
g_warning ("Can't make screensaver allow user switching: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Tell nautilus to draw the desktop */
error = NULL;
gconf_client_set_bool (client, NAUTILUS_SHOW_DESKTOP, TRUE, &error);
if (error)
{
g_warning ("Can't make Nautilus draw the desktop: %s",
error->message);
g_error_free (error);
error = NULL;
}
gdk_spawn_command_line_on_screen (screen, "nautilus -n", NULL);
/* Set the theme */
error = NULL;
gconf_client_set_string (client, THEME, "Human", &error);
if (error)
{
g_warning ("Can't change the theme: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Set Maximus */
error = NULL;
gconf_client_set_bool (client, MAXIMUS, FALSE, &error);
if (error)
{
g_warning ("Can't change Maximus's undecorate setting: %s",
error->message);
g_error_free (error);
error = NULL;
}
/* Kill any version of the launcher. */
g_debug ("Killing the launcher");
gdk_spawn_command_line_on_screen (screen, "killall "LAUNCHER_NAME, NULL);
gdk_spawn_command_line_on_screen (screen, "killall "OLD_LAUNCHER_NAME, NULL);
sleep (1);
/* Save current panel configuration */
filename = g_build_filename (g_get_home_dir (), ".config/desktop-switcher",
CLASSIC_PANEL_CONFIG, NULL);
g_spawn_command_line_sync (GCONF_DUMP_NETBOOK_PANEL, NULL, NULL, NULL, NULL);
/* Set the panel to the netbook layout */
g_debug ("Restoring panel settings");
gdk_spawn_command_line_on_screen (screen, GCONF_STOP, NULL);
gdk_spawn_command_line_on_screen (screen, GCONF_UNSET, NULL);
if (g_file_test (filename, G_FILE_TEST_EXISTS))
{
gchar *exec;
exec = g_strdup_printf ("%s %s", GCONF_LOAD, filename);
g_spawn_command_line_sync (exec, NULL, NULL, NULL, NULL);
g_free (exec);
g_debug ("\nRestored panel configuration from: %s\n", filename);
}
else
{
gdk_spawn_command_line_on_screen (screen,
GCONF_LOAD SHARE_CLASSIC_PANEL_CONFIG,
NULL);
g_debug ("\nRestored panel configuration from defaults\n");
}
gdk_spawn_command_line_on_screen (screen, "killall gnome-panel", NULL);
gdk_spawn_command_line_on_screen (screen, "gnome-panel", NULL);
/* Add nautilus back to the required components in the session */
GSList *comps = NULL;
comps = g_slist_append (comps, g_strdup ("panel"));
comps = g_slist_append (comps, g_strdup ("filemanager"));
comps = g_slist_append (comps, g_strdup ("windowmanager"));
gconf_client_set_list (client, SESSION_COMPONENTS, GCONF_VALUE_STRING,
comps, NULL);
g_slist_foreach (comps, (GFunc)g_free, NULL);
g_slist_free (comps);
gdk_spawn_command_line_on_screen (screen, "metacity --replace", NULL);
gdk_spawn_command_line_on_screen (screen, "nautilus", NULL);
g_free (filename);
g_object_unref (client);
}
void
on_apply_clicked (GtkButton *button)
{
if (current_mode == selected_mode)
gtk_main_quit ();
else if (selected_mode == NETBOOK_MODE)
enable_netbook ();
else
enable_classic ();
gtk_main_quit ();
}
|