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

« back to all changes in this revision

Viewing changes to shell/cc-shell-model.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
/*
 
2
 * Copyright (c) 2009, 2010 Intel, Inc.
 
3
 * Copyright (c) 2010 Red Hat, Inc.
 
4
 *
 
5
 * The Control Center is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by the
 
7
 * Free Software Foundation; either version 2 of the License, or (at your
 
8
 * option) any later version.
 
9
 *
 
10
 * The Control Center is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
12
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
13
 * for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License along
 
16
 * with the Control Center; if not, write to the Free Software Foundation,
 
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 *
 
19
 * Author: Thomas Wood <thos@gnome.org>
 
20
 */
 
21
 
 
22
#include "cc-shell-model.h"
 
23
#include <string.h>
 
24
 
 
25
#define GNOME_SETTINGS_PANEL_ID_KEY "X-GNOME-Settings-Panel"
 
26
#define GNOME_SETTINGS_PANEL_CATEGORY GNOME_SETTINGS_PANEL_ID_KEY
 
27
#define GNOME_SETTINGS_PANEL_ID_KEYWORDS "X-GNOME-Keywords"
 
28
 
 
29
 
 
30
G_DEFINE_TYPE (CcShellModel, cc_shell_model, GTK_TYPE_LIST_STORE)
 
31
 
 
32
static GdkPixbuf *
 
33
load_pixbuf_for_string (const char *icon)
 
34
{
 
35
  GtkIconTheme *theme;
 
36
  GdkPixbuf *pixbuf;
 
37
  GError *err = NULL;
 
38
  char *icon2 = NULL;
 
39
 
 
40
  if (icon == NULL)
 
41
    return NULL;
 
42
 
 
43
  theme = gtk_icon_theme_get_default ();
 
44
 
 
45
  /* find the icon */
 
46
  if (*icon == '/')
 
47
    {
 
48
      pixbuf = gdk_pixbuf_new_from_file_at_scale (icon, 32, 32, TRUE, &err);
 
49
    }
 
50
  else
 
51
    {
 
52
      if (g_str_has_suffix (icon, ".png"))
 
53
        icon2 = g_strndup (icon, strlen (icon) - strlen (".png"));
 
54
 
 
55
      pixbuf = gtk_icon_theme_load_icon (theme,
 
56
                                         icon2 ? icon2 : icon, 32,
 
57
                                         GTK_ICON_LOOKUP_FORCE_SIZE,
 
58
                                         &err);
 
59
    }
 
60
 
 
61
  if (err)
 
62
    {
 
63
      g_warning ("Could not load icon '%s': %s", icon2 ? icon2 : icon,
 
64
                 err->message);
 
65
      g_error_free (err);
 
66
    }
 
67
 
 
68
  g_free (icon2);
 
69
 
 
70
  return pixbuf;
 
71
}
 
72
 
 
73
static void
 
74
icon_theme_changed (GtkIconTheme *theme,
 
75
                    CcShellModel *self)
 
76
{
 
77
  GtkTreeIter iter;
 
78
  GtkTreeModel *model;
 
79
  gboolean cont;
 
80
 
 
81
  model = GTK_TREE_MODEL (self);
 
82
  cont = gtk_tree_model_get_iter_first (model, &iter);
 
83
  while (cont)
 
84
    {
 
85
      GdkPixbuf *pixbuf;
 
86
      char *icon;
 
87
 
 
88
      gtk_tree_model_get (model, &iter,
 
89
                          COL_ICON_NAME, &icon,
 
90
                          -1);
 
91
      pixbuf = load_pixbuf_for_string (icon);
 
92
      g_free (icon);
 
93
      gtk_list_store_set (GTK_LIST_STORE (model), &iter,
 
94
                          COL_PIXBUF, pixbuf,
 
95
                          -1);
 
96
 
 
97
      cont = gtk_tree_model_iter_next (model, &iter);
 
98
    }
 
99
}
 
100
 
 
101
static void
 
102
cc_shell_model_class_init (CcShellModelClass *klass)
 
103
{
 
104
}
 
105
 
 
106
static void
 
107
cc_shell_model_init (CcShellModel *self)
 
108
{
 
109
  GType types[] = {G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
 
110
      GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRV};
 
111
 
 
112
  gtk_list_store_set_column_types (GTK_LIST_STORE (self),
 
113
                                   N_COLS, types);
 
114
 
 
115
  gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self), COL_NAME,
 
116
                                        GTK_SORT_ASCENDING);
 
117
 
 
118
  g_signal_connect (G_OBJECT (gtk_icon_theme_get_default ()), "changed",
 
119
                    G_CALLBACK (icon_theme_changed), self);
 
120
}
 
121
 
 
122
CcShellModel *
 
123
cc_shell_model_new (void)
 
124
{
 
125
  return g_object_new (CC_TYPE_SHELL_MODEL, NULL);
 
126
}
 
127
 
 
128
static gboolean
 
129
desktop_entry_has_panel_category (GKeyFile *key_file)
 
130
{
 
131
  char   **strv;
 
132
  gsize    len;
 
133
  int      i;
 
134
 
 
135
  strv = g_key_file_get_string_list (key_file,
 
136
                                     "Desktop Entry",
 
137
                                     "Categories",
 
138
                                     &len,
 
139
                                     NULL);
 
140
  if (!strv)
 
141
    return FALSE;
 
142
 
 
143
  for (i = 0; strv[i]; i++)
 
144
    {
 
145
      if (g_str_equal (strv[i], GNOME_SETTINGS_PANEL_CATEGORY))
 
146
        {
 
147
          g_strfreev (strv);
 
148
          return TRUE;
 
149
        }
 
150
    }
 
151
 
 
152
  g_strfreev (strv);
 
153
 
 
154
  return FALSE;
 
155
 
 
156
}
 
157
 
 
158
void
 
159
cc_shell_model_add_item (CcShellModel   *model,
 
160
                         const gchar    *category_name,
 
161
                         GMenuTreeEntry *item)
 
162
{
 
163
  const gchar *icon = gmenu_tree_entry_get_icon (item);
 
164
  const gchar *name = gmenu_tree_entry_get_name (item);
 
165
  const gchar *desktop = gmenu_tree_entry_get_desktop_file_path (item);
 
166
  const gchar *comment = gmenu_tree_entry_get_comment (item);
 
167
  gchar *id;
 
168
  GdkPixbuf *pixbuf = NULL;
 
169
  gchar *search_target;
 
170
  GKeyFile *key_file;
 
171
  gchar **keywords;
 
172
 
 
173
  /* load the .desktop file since gnome-menus doesn't have a way to read
 
174
   * custom properties from desktop files */
 
175
 
 
176
  key_file = g_key_file_new ();
 
177
  g_key_file_load_from_file (key_file, desktop, 0, NULL);
 
178
 
 
179
  id = g_key_file_get_string (key_file, "Desktop Entry",
 
180
                              GNOME_SETTINGS_PANEL_ID_KEY, NULL);
 
181
 
 
182
  if (!id)
 
183
    {
 
184
      /* Refuse to load desktop files without a panel ID, but
 
185
       * with the X-GNOME-Settings-Panel category */
 
186
      if (desktop_entry_has_panel_category (key_file))
 
187
        {
 
188
          g_warning ("Not loading desktop file '%s' because it uses the "
 
189
                     GNOME_SETTINGS_PANEL_CATEGORY
 
190
                     " category but isn't a panel.",
 
191
                     desktop);
 
192
         g_key_file_free (key_file);
 
193
         return;
 
194
        }
 
195
      id = g_strdup (gmenu_tree_entry_get_desktop_file_id (item));
 
196
    }
 
197
 
 
198
  keywords = g_key_file_get_locale_string_list (key_file, "Desktop Entry",
 
199
                                                GNOME_SETTINGS_PANEL_ID_KEYWORDS,
 
200
                                                NULL, NULL, NULL);
 
201
 
 
202
  g_key_file_free (key_file);
 
203
  key_file = NULL;
 
204
 
 
205
  pixbuf = load_pixbuf_for_string (icon);
 
206
 
 
207
  search_target = g_strconcat (name, " - ", comment, NULL);
 
208
 
 
209
  gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, 0,
 
210
                                     COL_NAME, name,
 
211
                                     COL_DESKTOP_FILE, desktop,
 
212
                                     COL_ID, id,
 
213
                                     COL_PIXBUF, pixbuf,
 
214
                                     COL_CATEGORY, category_name,
 
215
                                     COL_SEARCH_TARGET, search_target,
 
216
                                     COL_ICON_NAME, icon,
 
217
                                     COL_KEYWORDS, keywords,
 
218
                                     -1);
 
219
 
 
220
  g_free (id);
 
221
  g_free (search_target);
 
222
  g_strfreev (keywords);
 
223
}