~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to libwindow-settings/gnome-wm-manager.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
 
 
3
 
/* gnome-wm-manager.c
4
 
 * Copyright (C) 2002 Seth Nickell
5
 
 * Copyright (C) 1998, 2002 Red Hat, Inc.
6
 
 *
7
 
 * Written by: Seth Nickell <snickell@stanford.edu>,
8
 
 *             Havoc Pennington <hp@redhat.com>
9
 
 *             Owen Taylor <otaylor@redhat.com>,
10
 
 *             Bradford Hovinen <hovinen@helixcode.com>
11
 
 *
12
 
 * This program is free software; you can redistribute it and/or modify
13
 
 * it under the terms of the GNU General Public License as published by
14
 
 * the Free Software Foundation; either version 2, or (at your option)
15
 
 * any later version.
16
 
 *
17
 
 * This program is distributed in the hope that it will be useful,
18
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 
 * GNU General Public License for more details.
21
 
 *
22
 
 * You should have received a copy of the GNU General Public License
23
 
 * along with this program; if not, write to the Free Software
24
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25
 
 * 02111-1307, USA.
26
 
 */
27
 
 
28
 
#include <config.h>
29
 
#include "gnome-wm-manager.h"
30
 
 
31
 
#include <glib.h>
32
 
#include <glib/gi18n.h>
33
 
#include <gdk/gdk.h>
34
 
#include <gdk/gdkx.h>
35
 
 
36
 
#include <sys/types.h>
37
 
#include <dirent.h>
38
 
#include <string.h>
39
 
 
40
 
typedef struct {
41
 
        GnomeDesktopItem *ditem;
42
 
        char *name; /* human readable, localized */
43
 
        char *identify_name; /* name we expect to be set on the screen */
44
 
        char *exec;
45
 
        char *config_exec;
46
 
        char *config_tryexec;
47
 
        char *module;
48
 
        guint session_managed : 1;
49
 
        guint is_user : 1;
50
 
        guint is_present : 1;
51
 
        guint is_config_present : 1;
52
 
        GnomeWindowManager *gnome_wm;
53
 
} AvailableWindowManager;
54
 
 
55
 
static gboolean done_scan = FALSE;
56
 
static GList *available_wms;
57
 
 
58
 
static void
59
 
wm_free (AvailableWindowManager *wm)
60
 
{
61
 
        g_free (wm->name);
62
 
        g_free (wm->exec);
63
 
        g_free (wm->config_exec);
64
 
        g_free (wm->config_tryexec);
65
 
        g_free (wm->module);
66
 
        g_free (wm->identify_name);
67
 
        
68
 
        g_free (wm);
69
 
}
70
 
 
71
 
static GList *
72
 
list_desktop_files_in_dir (gchar *directory)
73
 
{
74
 
        DIR *dir;
75
 
        struct dirent *child;
76
 
        GList *result = NULL;
77
 
        gchar *suffix;
78
 
 
79
 
        dir = opendir (directory);
80
 
        if (dir == NULL)
81
 
                return NULL;
82
 
 
83
 
        while ((child = readdir (dir)) != NULL) {
84
 
                /* Ignore files without .desktop suffix, and ignore
85
 
                 * .desktop files with no prefix
86
 
                 */
87
 
                suffix = child->d_name + strlen (child->d_name) - 8;
88
 
                /* strlen(".desktop") == 8 */
89
 
 
90
 
                if (suffix <= child->d_name || 
91
 
                    strcmp (suffix, ".desktop") != 0)
92
 
                        continue;
93
 
                
94
 
                result = g_list_prepend (result, 
95
 
                                         g_build_filename (directory, child->d_name, NULL));
96
 
        }
97
 
        closedir (dir);
98
 
        
99
 
        return result;
100
 
}
101
 
 
102
 
static gint
103
 
wm_compare (gconstpointer a, gconstpointer b)
104
 
{
105
 
        const AvailableWindowManager *wm_a = (const AvailableWindowManager *)a;
106
 
        const AvailableWindowManager *wm_b = (const AvailableWindowManager *)b;
107
 
 
108
 
        /* mmm, sloooow */
109
 
        
110
 
        return g_utf8_collate (gnome_desktop_item_get_string (wm_a->ditem, GNOME_DESKTOP_ITEM_NAME),
111
 
                               gnome_desktop_item_get_string (wm_b->ditem, GNOME_DESKTOP_ITEM_NAME));
112
 
}
113
 
 
114
 
static AvailableWindowManager*
115
 
wm_load (const char *desktop_file,
116
 
         gboolean    is_user)
117
 
{
118
 
        gchar *path;
119
 
        AvailableWindowManager *wm;
120
 
                
121
 
        wm = g_new0 (AvailableWindowManager, 1);
122
 
        
123
 
        wm->ditem = gnome_desktop_item_new_from_file (desktop_file, 0, NULL);
124
 
 
125
 
        if (wm->ditem == NULL) {
126
 
                g_free (wm);
127
 
                return NULL;
128
 
        }
129
 
 
130
 
        gnome_desktop_item_set_entry_type (wm->ditem, GNOME_DESKTOP_ITEM_TYPE_APPLICATION);
131
 
 
132
 
        wm->exec = g_strdup (gnome_desktop_item_get_string (wm->ditem,
133
 
                                                            GNOME_DESKTOP_ITEM_EXEC));
134
 
        
135
 
        wm->name = g_strdup (gnome_desktop_item_get_string (wm->ditem,
136
 
                                                            GNOME_DESKTOP_ITEM_NAME));
137
 
                                    
138
 
        wm->config_exec = g_strdup (gnome_desktop_item_get_string (wm->ditem,
139
 
                                                                   "ConfigExec"));
140
 
        wm->config_tryexec = g_strdup (gnome_desktop_item_get_string (wm->ditem,
141
 
                                                                      "ConfigTryExec"));
142
 
        wm->session_managed = gnome_desktop_item_get_boolean (wm->ditem,
143
 
                                                              "SessionManaged");
144
 
 
145
 
        wm->module = g_strdup (gnome_desktop_item_get_string (wm->ditem,
146
 
                                                              "X-GNOME-WMSettingsModule"));
147
 
 
148
 
        wm->identify_name = g_strdup (gnome_desktop_item_get_string (wm->ditem,
149
 
                                                                     "X-GNOME-WMName"));
150
 
        
151
 
        wm->is_user = is_user;
152
 
        
153
 
        if (gnome_desktop_item_get_string (wm->ditem, GNOME_DESKTOP_ITEM_EXEC)) {
154
 
                const char *tryexec;
155
 
 
156
 
                tryexec = gnome_desktop_item_get_string (wm->ditem, GNOME_DESKTOP_ITEM_TRY_EXEC);
157
 
                
158
 
                if (tryexec) {
159
 
                        path = g_find_program_in_path (tryexec);
160
 
                        wm->is_present = (path != NULL);
161
 
                        if (path)
162
 
                                g_free (path);
163
 
                } else
164
 
                        wm->is_present = TRUE;
165
 
        } else
166
 
                wm->is_present = FALSE;
167
 
        
168
 
        if (wm->config_exec) {
169
 
                if (wm->config_tryexec) {
170
 
                        path = g_find_program_in_path (wm->config_tryexec);
171
 
                        wm->is_config_present = (path != NULL);
172
 
                        if (path)
173
 
                                g_free (path);
174
 
                } else {
175
 
                        path = g_find_program_in_path (wm->config_exec);
176
 
                        wm->is_config_present = (path != NULL);
177
 
                        if (path)
178
 
                                g_free (path);
179
 
                }
180
 
        } else
181
 
                wm->is_config_present = FALSE;
182
 
 
183
 
        if (wm->name && wm->exec &&
184
 
            (wm->is_user || wm->is_present))
185
 
                return wm;
186
 
        else {
187
 
                wm_free (wm);
188
 
                return NULL;
189
 
        }
190
 
}
191
 
 
192
 
static void
193
 
scan_wm_directory (gchar *directory, gboolean is_user)
194
 
{
195
 
        GList *tmp_list;
196
 
        GList *files;
197
 
 
198
 
        files = list_desktop_files_in_dir (directory);
199
 
 
200
 
        tmp_list = files;
201
 
        while (tmp_list) {
202
 
                AvailableWindowManager *wm;
203
 
 
204
 
                wm = wm_load (tmp_list->data, is_user);
205
 
                
206
 
                if (wm != NULL)
207
 
                        available_wms = g_list_prepend (available_wms, wm);
208
 
                
209
 
                tmp_list = tmp_list->next;
210
 
        }
211
 
 
212
 
        g_list_foreach (files, (GFunc) g_free, NULL);
213
 
        g_list_free (files);
214
 
}
215
 
 
216
 
void
217
 
gnome_wm_manager_init (void)
218
 
{
219
 
        char *tempdir;
220
 
 
221
 
        if (done_scan)
222
 
                return;
223
 
 
224
 
        done_scan = TRUE;
225
 
        
226
 
        tempdir = g_build_filename (GNOME_WM_PROPERTY_PATH, NULL);
227
 
        scan_wm_directory (tempdir, FALSE);
228
 
        g_free (tempdir);
229
 
 
230
 
        tempdir = g_build_filename (g_get_home_dir(), ".gnome2", "wm-properties", NULL);
231
 
        scan_wm_directory (tempdir, TRUE);
232
 
        g_free (tempdir);
233
 
 
234
 
        available_wms = g_list_sort (available_wms,
235
 
                                     wm_compare);
236
 
}
237
 
 
238
 
static AvailableWindowManager*
239
 
get_current_wm (GdkScreen *screen)
240
 
{
241
 
        AvailableWindowManager *current_wm;
242
 
        const char *name;
243
 
        GList *tmp_list;
244
 
        
245
 
        g_return_val_if_fail (GDK_IS_SCREEN (screen), NULL);
246
 
        
247
 
        name = gdk_x11_screen_get_window_manager_name (screen);
248
 
 
249
 
        current_wm = NULL;
250
 
        
251
 
        tmp_list = available_wms;
252
 
        while (tmp_list != NULL) {
253
 
                AvailableWindowManager *wm = tmp_list->data;
254
 
 
255
 
                if (wm->identify_name &&
256
 
                    strcmp (wm->identify_name, name) == 0) {
257
 
                        current_wm = wm;
258
 
                        break;
259
 
                }
260
 
                tmp_list = tmp_list->next;
261
 
        }
262
 
 
263
 
        if (current_wm == NULL) {
264
 
                /* Try with localized name, sort of crackrock
265
 
                 * back compat hack
266
 
                 */
267
 
                
268
 
                tmp_list = available_wms;
269
 
                while (tmp_list != NULL) {
270
 
                        AvailableWindowManager *wm = tmp_list->data;
271
 
                        
272
 
                        if (strcmp (wm->name, name) == 0) {
273
 
                                current_wm = wm;
274
 
                                break;
275
 
                        }
276
 
                        tmp_list = tmp_list->next;
277
 
                }
278
 
        }
279
 
 
280
 
        return current_wm;
281
 
}
282
 
 
283
 
GnomeWindowManager*
284
 
gnome_wm_manager_get_current (GdkScreen *screen)
285
 
{
286
 
        AvailableWindowManager *wm;
287
 
 
288
 
        wm = get_current_wm (screen);
289
 
 
290
 
        if (wm != NULL && wm->module != NULL)
291
 
                /* may still return NULL here */
292
 
                return (GnomeWindowManager*) gnome_window_manager_new (wm->ditem);
293
 
        else
294
 
                return NULL;
295
 
}
296
 
 
297
 
gboolean
298
 
gnome_wm_manager_spawn_config_tool_for_current (GdkScreen  *screen,
299
 
                                                GError    **error)
300
 
{
301
 
        AvailableWindowManager *wm;
302
 
 
303
 
        wm = get_current_wm (screen);
304
 
        
305
 
        if (wm != NULL && wm->config_exec != NULL) {
306
 
                return g_spawn_command_line_async (wm->config_exec,
307
 
                                                   error);
308
 
        } else {
309
 
                const char *name;
310
 
                
311
 
                name = gdk_x11_screen_get_window_manager_name (screen);
312
 
 
313
 
                g_set_error (error, 
314
 
                             G_SPAWN_ERROR,
315
 
                             G_SPAWN_ERROR_FAILED,
316
 
                             _("Window manager \"%s\" has not registered a configuration tool\n"),
317
 
                             name);
318
 
                return FALSE;
319
 
        }
320
 
}