[gnome-control-center] shell: Port to new gnome-menus API
- From: Vincent Untz <vuntz src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center] shell: Port to new gnome-menus API
- Date: Mon, 1 Aug 2011 17:41:59 +0000 (UTC)
commit c635d8e875713c697ba9a9b13f5d7a4cc6ced432
Author: Vincent Untz <vuntz gnome org>
Date: Fri Jul 22 13:05:01 2011 +0200
shell: Port to new gnome-menus API
https://bugzilla.gnome.org/show_bug.cgi?id=655110
configure.ac | 2 +-
shell/cc-shell-model.c | 57 +++++++++++-------------
shell/cc-shell-model.h | 2 +-
shell/gnome-control-center.c | 98 ++++++++++++++++++++++++++++-------------
4 files changed, 95 insertions(+), 64 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 5bb94b1..48fd12c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -89,7 +89,7 @@ COMMON_MODULES="gtk+-3.0 >= $GTK_REQUIRED_VERSION
PKG_CHECK_MODULES(LIBGNOME_CONTROL_CENTER, $COMMON_MODULES gconf-2.0)
PKG_CHECK_MODULES(LIBLANGUAGE, $COMMON_MODULES gnome-desktop-3.0)
PKG_CHECK_MODULES(LIBSHORTCUTS, $COMMON_MODULES x11)
-PKG_CHECK_MODULES(SHELL, $COMMON_MODULES libgnome-menu gio-unix-2.0)
+PKG_CHECK_MODULES(SHELL, $COMMON_MODULES libgnome-menu-3.0 gio-unix-2.0)
PKG_CHECK_MODULES(BACKGROUND_PANEL, $COMMON_MODULES libxml-2.0 gnome-desktop-3.0
gdk-pixbuf-2.0 >= $GDKPIXBUF_REQUIRED_VERSION)
PKG_CHECK_MODULES(DATETIME_PANEL, $COMMON_MODULES dbus-glib-1
diff --git a/shell/cc-shell-model.c b/shell/cc-shell-model.c
index 8f63801..c4ae28f 100644
--- a/shell/cc-shell-model.c
+++ b/shell/cc-shell-model.c
@@ -30,43 +30,37 @@
G_DEFINE_TYPE (CcShellModel, cc_shell_model, GTK_TYPE_LIST_STORE)
static GdkPixbuf *
-load_pixbuf_for_string (const char *icon)
+load_pixbuf_for_gicon (GIcon *icon)
{
GtkIconTheme *theme;
+ GtkIconInfo *icon_info;
GdkPixbuf *pixbuf;
GError *err = NULL;
- char *icon2 = NULL;
if (icon == NULL)
return NULL;
theme = gtk_icon_theme_get_default ();
- /* find the icon */
- if (*icon == '/')
+ icon_info = gtk_icon_theme_lookup_by_gicon (theme, icon,
+ 32, GTK_ICON_LOOKUP_FORCE_SIZE);
+ if (icon_info)
{
- pixbuf = gdk_pixbuf_new_from_file_at_scale (icon, 32, 32, TRUE, &err);
- }
- else
- {
- if (g_str_has_suffix (icon, ".png"))
- icon2 = g_strndup (icon, strlen (icon) - strlen (".png"));
+ pixbuf = gtk_icon_info_load_icon (icon_info, &err);
+ if (err)
+ {
+ g_warning ("Could not load icon '%s': %s",
+ gtk_icon_info_get_filename (icon_info), err->message);
+ g_error_free (err);
+ }
- pixbuf = gtk_icon_theme_load_icon (theme,
- icon2 ? icon2 : icon, 32,
- GTK_ICON_LOOKUP_FORCE_SIZE,
- &err);
+ gtk_icon_info_free (icon_info);
}
-
- if (err)
+ else
{
- g_warning ("Could not load icon '%s': %s", icon2 ? icon2 : icon,
- err->message);
- g_error_free (err);
+ g_warning ("Could not find icon");
}
- g_free (icon2);
-
return pixbuf;
}
@@ -83,13 +77,13 @@ icon_theme_changed (GtkIconTheme *theme,
while (cont)
{
GdkPixbuf *pixbuf;
- char *icon;
+ GIcon *icon;
gtk_tree_model_get (model, &iter,
- COL_ICON_NAME, &icon,
+ COL_GICON, &icon,
-1);
- pixbuf = load_pixbuf_for_string (icon);
- g_free (icon);
+ pixbuf = load_pixbuf_for_gicon (icon);
+ g_object_unref (icon);
gtk_list_store_set (GTK_LIST_STORE (model), &iter,
COL_PIXBUF, pixbuf,
-1);
@@ -107,7 +101,7 @@ static void
cc_shell_model_init (CcShellModel *self)
{
GType types[] = {G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING,
- GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRV};
+ GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_ICON, G_TYPE_STRV};
gtk_list_store_set_column_types (GTK_LIST_STORE (self),
N_COLS, types);
@@ -160,10 +154,11 @@ cc_shell_model_add_item (CcShellModel *model,
const gchar *category_name,
GMenuTreeEntry *item)
{
- const gchar *icon = gmenu_tree_entry_get_icon (item);
- const gchar *name = gmenu_tree_entry_get_name (item);
+ GAppInfo *appinfo = G_APP_INFO (gmenu_tree_entry_get_app_info (item));
+ GIcon *icon = g_app_info_get_icon (appinfo);
+ const gchar *name = g_app_info_get_name (appinfo);
const gchar *desktop = gmenu_tree_entry_get_desktop_file_path (item);
- const gchar *comment = gmenu_tree_entry_get_comment (item);
+ const gchar *comment = g_app_info_get_description (appinfo);
gchar *id;
GdkPixbuf *pixbuf = NULL;
gchar *search_target;
@@ -202,7 +197,7 @@ cc_shell_model_add_item (CcShellModel *model,
g_key_file_free (key_file);
key_file = NULL;
- pixbuf = load_pixbuf_for_string (icon);
+ pixbuf = load_pixbuf_for_gicon (icon);
search_target = g_strconcat (name, " - ", comment, NULL);
@@ -213,7 +208,7 @@ cc_shell_model_add_item (CcShellModel *model,
COL_PIXBUF, pixbuf,
COL_CATEGORY, category_name,
COL_SEARCH_TARGET, search_target,
- COL_ICON_NAME, icon,
+ COL_GICON, icon,
COL_KEYWORDS, keywords,
-1);
diff --git a/shell/cc-shell-model.h b/shell/cc-shell-model.h
index 6208e43..97ac160 100644
--- a/shell/cc-shell-model.h
+++ b/shell/cc-shell-model.h
@@ -61,7 +61,7 @@ enum
COL_PIXBUF,
COL_CATEGORY,
COL_SEARCH_TARGET,
- COL_ICON_NAME,
+ COL_GICON,
COL_KEYWORDS,
N_COLS
diff --git a/shell/gnome-control-center.c b/shell/gnome-control-center.c
index a771282..3725a63 100644
--- a/shell/gnome-control-center.c
+++ b/shell/gnome-control-center.c
@@ -84,12 +84,34 @@ struct _GnomeControlCenterPrivate
#define FIXED_WIDTH 675
+static const gchar *
+get_icon_name_from_g_icon (GIcon *gicon)
+{
+ const gchar * const *names;
+ GtkIconTheme *icon_theme;
+ int i;
+
+ if (!G_IS_THEMED_ICON (gicon))
+ return NULL;
+
+ names = g_themed_icon_get_names (G_THEMED_ICON (gicon));
+ icon_theme = gtk_icon_theme_get_default ();
+
+ for (i = 0; names[i] != NULL; i++)
+ {
+ if (gtk_icon_theme_has_icon (icon_theme, names[i]))
+ return names[i];
+ }
+
+ return NULL;
+}
+
static void
activate_panel (GnomeControlCenter *shell,
const gchar *id,
const gchar *desktop_file,
const gchar *name,
- const gchar *icon_name)
+ GIcon *gicon)
{
GnomeControlCenterPrivate *priv = shell->priv;
GType panel_type = G_TYPE_INVALID;
@@ -126,6 +148,7 @@ activate_panel (GnomeControlCenter *shell,
GtkWidget *box;
gint i;
int nat_height;
+ const gchar *icon_name;
/* create the panel plugin */
panel = g_object_new (panel_type, "shell", shell, NULL);
@@ -146,6 +169,7 @@ activate_panel (GnomeControlCenter *shell,
gtk_notebook_set_current_page (GTK_NOTEBOOK (priv->notebook), i);
/* set the title of the window */
+ icon_name = get_icon_name_from_g_icon (gicon);
gtk_window_set_title (GTK_WINDOW (priv->window), name);
gtk_window_set_default_icon_name (icon_name);
gtk_window_set_icon_name (GTK_WINDOW (priv->window), icon_name);
@@ -627,43 +651,57 @@ maybe_add_category_view (GnomeControlCenter *shell,
static void
reload_menu (GnomeControlCenter *shell)
{
- GSList *list, *l;
+ GError *error;
GMenuTreeDirectory *d;
+ GMenuTreeIter *iter;
+ GMenuTreeItemType next_type;
+
+ error = NULL;
+ if (!gmenu_tree_load_sync (shell->priv->menu_tree, &error))
+ {
+ g_warning ("Could not load control center menu: %s", error->message);
+ g_clear_error (&error);
+ return;
+ }
+
d = gmenu_tree_get_root_directory (shell->priv->menu_tree);
- list = gmenu_tree_directory_get_contents (d);
+ iter = gmenu_tree_directory_iter (d);
- for (l = list; l; l = l->next)
+ while ((next_type = gmenu_tree_iter_next (iter)) != GMENU_TREE_ITEM_INVALID)
{
- GMenuTreeItemType type;
- type = gmenu_tree_item_get_type (l->data);
-
- if (type == GMENU_TREE_ITEM_DIRECTORY)
+ if (next_type == GMENU_TREE_ITEM_DIRECTORY)
{
- GSList *contents, *f;
+ GMenuTreeDirectory *subdir;
const gchar *dir_name;
+ GMenuTreeIter *sub_iter;
+ GMenuTreeItemType sub_next_type;
- contents = gmenu_tree_directory_get_contents (l->data);
- dir_name = gmenu_tree_directory_get_name (l->data);
+ subdir = gmenu_tree_iter_get_directory (iter);
+ dir_name = gmenu_tree_directory_get_name (subdir);
maybe_add_category_view (shell, dir_name);
/* add the items from this category to the model */
- for (f = contents; f; f = f->next)
+ sub_iter = gmenu_tree_directory_iter (subdir);
+ while ((sub_next_type = gmenu_tree_iter_next (sub_iter)) != GMENU_TREE_ITEM_INVALID)
{
- if (gmenu_tree_item_get_type (f->data) == GMENU_TREE_ITEM_ENTRY)
+ if (sub_next_type == GMENU_TREE_ITEM_ENTRY)
{
+ GMenuTreeEntry *item = gmenu_tree_iter_get_entry (sub_iter);
cc_shell_model_add_item (CC_SHELL_MODEL (shell->priv->store),
dir_name,
- f->data);
+ item);
+ gmenu_tree_item_unref (item);
}
}
- g_slist_free (contents);
+ gmenu_tree_iter_unref (sub_iter);
+ gmenu_tree_item_unref (subdir);
}
}
- g_slist_free (list);
+ gmenu_tree_iter_unref (iter);
}
static void
@@ -685,17 +723,11 @@ setup_model (GnomeControlCenter *shell)
priv->store = (GtkListStore *) cc_shell_model_new ();
priv->category_views = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
- priv->menu_tree = gmenu_tree_lookup (MENUDIR "/gnomecc.menu", 0);
-
- if (priv->menu_tree == NULL)
- {
- g_warning ("Could not find control center menu");
- return;
- }
+ priv->menu_tree = gmenu_tree_new_for_path (MENUDIR "/gnomecc.menu", 0);
reload_menu (shell);
- gmenu_tree_add_monitor (priv->menu_tree, (GMenuTreeChangedFunc)on_menu_changed, shell);
+ g_signal_connect (priv->menu_tree, "changed", G_CALLBACK (on_menu_changed), shell);
}
static void
@@ -759,7 +791,8 @@ _shell_set_active_panel_from_id (CcShell *shell,
GtkTreeIter iter;
gboolean iter_valid;
gchar *name = NULL;
- gchar *desktop, *icon_name;
+ gchar *desktop;
+ GIcon *gicon;
GnomeControlCenterPrivate *priv = GNOME_CONTROL_CENTER (shell)->priv;
@@ -774,7 +807,7 @@ _shell_set_active_panel_from_id (CcShell *shell,
gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
COL_NAME, &name,
COL_DESKTOP_FILE, &desktop,
- COL_ICON_NAME, &icon_name,
+ COL_GICON, &gicon,
COL_ID, &id,
-1);
@@ -788,7 +821,8 @@ _shell_set_active_panel_from_id (CcShell *shell,
g_free (id);
g_free (name);
g_free (desktop);
- g_free (icon_name);
+ if (gicon)
+ g_object_unref (gicon);
name = NULL;
id = NULL;
@@ -808,11 +842,12 @@ _shell_set_active_panel_from_id (CcShell *shell,
gtk_notebook_remove_page (GTK_NOTEBOOK (priv->notebook), CAPPLET_PAGE);
activate_panel (GNOME_CONTROL_CENTER (shell), start_id, desktop, name,
- icon_name);
+ gicon);
g_free (name);
g_free (desktop);
- g_free (icon_name);
+ if (gicon)
+ g_object_unref (gicon);
return TRUE;
}
@@ -915,8 +950,9 @@ gnome_control_center_finalize (GObject *object)
if (priv->menu_tree)
{
- gmenu_tree_remove_monitor (priv->menu_tree, (GMenuTreeChangedFunc)on_menu_changed, object);
- gmenu_tree_unref (priv->menu_tree);
+ g_signal_handlers_disconnect_by_func (priv->menu_tree,
+ G_CALLBACK (on_menu_changed), object);
+ g_object_unref (priv->menu_tree);
}
if (priv->category_views)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]