[gnome-control-center/extensible-shell] shell: add CcShellModel to store the list of available panels



commit 2a49ad7f5d7e83bdca53aa364304ca85098050b8
Author: Thomas Wood <thomas wood intel com>
Date:   Tue Apr 13 17:16:48 2010 +0100

    shell: add CcShellModel to store the list of available panels
    
    CcShellModel is a GtkListStore subclass that implements storing the list of
    available control center panels.

 shell/Makefile.am              |    2 +
 shell/cc-shell-category-view.c |    1 +
 shell/cc-shell-item-view.c     |    2 +-
 shell/cc-shell-model.c         |  101 ++++++++++++++++++++++++++++++++++++++++
 shell/cc-shell-model.h         |   88 ++++++++++++++++++++++++++++++++++
 shell/control-center.c         |   60 +++--------------------
 shell/control-center.h         |   10 ----
 7 files changed, 201 insertions(+), 63 deletions(-)
---
diff --git a/shell/Makefile.am b/shell/Makefile.am
index 2ee976a..dabf12b 100644
--- a/shell/Makefile.am
+++ b/shell/Makefile.am
@@ -27,6 +27,8 @@ gnome_control_center_SOURCES =			\
 	cc-shell-category-view.h		\
 	cc-shell-item-view.c			\
 	cc-shell-item-view.h			\
+	cc-shell-model.c			\
+	cc-shell-model.h			\
 	$(MARSHAL_FILES)			\
 	$(NULL)
 
diff --git a/shell/cc-shell-category-view.c b/shell/cc-shell-category-view.c
index 9bcc3c7..9593f38 100644
--- a/shell/cc-shell-category-view.c
+++ b/shell/cc-shell-category-view.c
@@ -22,6 +22,7 @@
 #include "cc-shell-item-view.h"
 #include "cc-shell.h"
 #include "control-center.h"
+#include "cc-shell-model.h"
 
 G_DEFINE_TYPE (CcShellCategoryView, cc_shell_category_view, GTK_TYPE_FRAME)
 
diff --git a/shell/cc-shell-item-view.c b/shell/cc-shell-item-view.c
index 3f17aec..a1cd9d7 100644
--- a/shell/cc-shell-item-view.c
+++ b/shell/cc-shell-item-view.c
@@ -19,7 +19,7 @@
  */
 
 #include "cc-shell-item-view.h"
-#include "control-center.h"
+#include "cc-shell-model.h"
 #include "cc-shell-marshal.h"
 
 G_DEFINE_TYPE (CcShellItemView, cc_shell_item_view, GTK_TYPE_ICON_VIEW)
diff --git a/shell/cc-shell-model.c b/shell/cc-shell-model.c
new file mode 100644
index 0000000..3bd4d31
--- /dev/null
+++ b/shell/cc-shell-model.c
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2009, 2010 Intel, Inc.
+ * Copyright (c) 2010 Red Hat, Inc.
+ *
+ * The Control Center 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 2 of the License, or (at your
+ * option) any later version.
+ *
+ * The Control Center 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 the Control Center; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Thomas Wood <thos gnome org>
+ */
+
+#include "cc-shell-model.h"
+#include <string.h>
+
+G_DEFINE_TYPE (CcShellModel, cc_shell_model, GTK_TYPE_LIST_STORE)
+
+static void
+cc_shell_model_class_init (CcShellModelClass *klass)
+{
+
+}
+
+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};
+
+  gtk_list_store_set_column_types (GTK_LIST_STORE (self),
+                                   N_COLS, types);
+}
+
+CcShellModel *
+cc_shell_model_new (void)
+{
+  return g_object_new (CC_TYPE_SHELL_MODEL, NULL);
+}
+
+void
+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);
+  const gchar *id = gmenu_tree_entry_get_desktop_file_id (item);
+  const gchar *desktop = gmenu_tree_entry_get_desktop_file_path (item);
+  const gchar *comment = gmenu_tree_entry_get_comment (item);
+  GdkPixbuf *pixbuf = NULL;
+  gchar *icon2 = NULL;
+  GError *err = NULL;
+  gchar *search_target;
+
+  if (icon != NULL && *icon == '/')
+    {
+      pixbuf = gdk_pixbuf_new_from_file_at_scale (icon, 32, 32, TRUE, &err);
+    }
+  else
+    {
+      if (icon2 == NULL && icon != NULL && g_str_has_suffix (icon, ".png"))
+        icon2 = g_strndup (icon, strlen (icon) - strlen (".png"));
+
+      pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+                                         icon2 ? icon2 : icon, 32,
+                                         GTK_ICON_LOOKUP_FORCE_SIZE,
+                                         &err);
+    }
+
+  if (err)
+    {
+      g_warning ("Could not load icon '%s': %s", icon2 ? icon2 : icon,
+                 err->message);
+      g_error_free (err);
+    }
+
+  g_free (icon2);
+
+  search_target = g_strconcat (name, " - ", comment, NULL);
+
+  gtk_list_store_insert_with_values (GTK_LIST_STORE (model), NULL, 0,
+                                     COL_NAME, name,
+                                     COL_DESKTOP_FILE, desktop,
+                                     COL_ID, id,
+                                     COL_PIXBUF, pixbuf,
+                                     COL_CATEGORY, category_name,
+                                     COL_SEARCH_TARGET, search_target,
+                                     -1);
+
+  g_free (search_target);
+
+}
diff --git a/shell/cc-shell-model.h b/shell/cc-shell-model.h
new file mode 100644
index 0000000..851b7d2
--- /dev/null
+++ b/shell/cc-shell-model.h
@@ -0,0 +1,88 @@
+/*
+ * Copyright (c) 2010 Intel, Inc.
+ *
+ * The Control Center 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 2 of the License, or (at your
+ * option) any later version.
+ *
+ * The Control Center 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 the Control Center; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Thomas Wood <thos gnome org>
+ */
+
+
+#ifndef _CC_SHELL_MODEL_H
+#define _CC_SHELL_MODEL_H
+
+#include <gtk/gtk.h>
+#define GMENU_I_KNOW_THIS_IS_UNSTABLE
+#include <gmenu-tree.h>
+
+G_BEGIN_DECLS
+
+#define CC_TYPE_SHELL_MODEL cc_shell_model_get_type()
+
+#define CC_SHELL_MODEL(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+  CC_TYPE_SHELL_MODEL, CcShellModel))
+
+#define CC_SHELL_MODEL_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+  CC_TYPE_SHELL_MODEL, CcShellModelClass))
+
+#define CC_IS_SHELL_MODEL(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+  CC_TYPE_SHELL_MODEL))
+
+#define CC_IS_SHELL_MODEL_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+  CC_TYPE_SHELL_MODEL))
+
+#define CC_SHELL_MODEL_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+  CC_TYPE_SHELL_MODEL, CcShellModelClass))
+
+typedef struct _CcShellModel CcShellModel;
+typedef struct _CcShellModelClass CcShellModelClass;
+
+enum
+{
+  COL_NAME,
+  COL_DESKTOP_FILE,
+  COL_ID,
+  COL_PIXBUF,
+  COL_CATEGORY,
+  COL_SEARCH_TARGET,
+
+  N_COLS
+};
+
+struct _CcShellModel
+{
+  GtkListStore parent;
+};
+
+struct _CcShellModelClass
+{
+  GtkListStoreClass parent_class;
+};
+
+GType cc_shell_model_get_type (void) G_GNUC_CONST;
+
+CcShellModel *cc_shell_model_new (void);
+
+void cc_shell_model_add_item (CcShellModel   *model,
+                              const gchar    *category_name,
+                              GMenuTreeEntry *item);
+
+G_END_DECLS
+
+#endif /* _CC_SHELL_MODEL_H */
diff --git a/shell/control-center.c b/shell/control-center.c
index 3675b1c..2010d31 100644
--- a/shell/control-center.c
+++ b/shell/control-center.c
@@ -36,6 +36,7 @@
 #include "cc-shell.h"
 #include "shell-search-renderer.h"
 #include "cc-shell-category-view.h"
+#include "cc-shell-model.h"
 
 #include <unique/unique.h>
 
@@ -256,9 +257,7 @@ fill_model (ShellData *data)
 
   list = gmenu_tree_directory_get_contents (d);
 
-  data->store = gtk_list_store_new (N_COLS, G_TYPE_STRING, G_TYPE_STRING,
-                                    G_TYPE_STRING, GDK_TYPE_PIXBUF,
-                                    G_TYPE_STRING, G_TYPE_STRING);
+  data->store = (GtkListStore *) cc_shell_model_new ();
 
 
 
@@ -278,7 +277,8 @@ fill_model (ShellData *data)
           dir_name = gmenu_tree_directory_get_name (l->data);
 
           /* create new category view for this category */
-          filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (data->store), NULL);
+          filter = gtk_tree_model_filter_new (GTK_TREE_MODEL (data->store),
+                                              NULL);
           gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (filter),
                                                   (GtkTreeModelFilterVisibleFunc) category_filter_func,
                                                   g_strdup (dir_name), g_free);
@@ -293,55 +293,11 @@ fill_model (ShellData *data)
           /* add the items from this category to the model */
           for (f = contents; f; f = f->next)
             {
-              if (gmenu_tree_item_get_type (f->data)
-                  == GMENU_TREE_ITEM_ENTRY)
+              if (gmenu_tree_item_get_type (f->data) == GMENU_TREE_ITEM_ENTRY)
                 {
-                  GError *err = NULL;
-                  gchar *search_target;
-                  const gchar *icon = gmenu_tree_entry_get_icon (f->data);
-                  const gchar *name = gmenu_tree_entry_get_name (f->data);
-                  const gchar *id = gmenu_tree_entry_get_desktop_file_id (f->data);
-                  const gchar *desktop = gmenu_tree_entry_get_desktop_file_path (f->data);
-                  const gchar *comment = gmenu_tree_entry_get_comment (f->data);
-                  GdkPixbuf *pixbuf = NULL;
-                  char *icon2 = NULL;
-
-                  if (icon != NULL && *icon == '/')
-                    {
-                      pixbuf = gdk_pixbuf_new_from_file_at_scale (icon, 32, 32, TRUE, &err);
-                    }
-                  else
-                    {
-                      if (icon2 == NULL && icon != NULL && g_str_has_suffix (icon, ".png"))
-                        icon2 = g_strndup (icon, strlen (icon) - strlen (".png"));
-
-                      pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
-                                                         icon2 ? icon2 : icon, 32,
-                                                         GTK_ICON_LOOKUP_FORCE_SIZE,
-                                                         &err);
-                    }
-
-                  if (err)
-                    {
-                      g_warning ("Could not load icon '%s': %s", icon2 ? icon2 : icon,
-                                       err->message);
-                      g_error_free (err);
-                    }
-
-                  g_free (icon2);
-
-                  search_target = g_strconcat (name, " - ", comment, NULL);
-
-                  gtk_list_store_insert_with_values (data->store, NULL, 0,
-                                                     COL_NAME, name,
-                                                     COL_DESKTOP_FILE, desktop,
-                                                     COL_ID, id,
-                                                     COL_PIXBUF, pixbuf,
-                                                     COL_CATEGORY, dir_name,
-                                                     COL_SEARCH_TARGET, search_target,
-                                                     -1);
-
-                  g_free (search_target);
+                  cc_shell_model_add_item (CC_SHELL_MODEL (data->store),
+                                           dir_name,
+                                           f->data);
                 }
             }
         }
diff --git a/shell/control-center.h b/shell/control-center.h
index 88c1a27..943616b 100644
--- a/shell/control-center.h
+++ b/shell/control-center.h
@@ -19,14 +19,4 @@
  */
 
 
-enum
-{
-  COL_NAME,
-  COL_DESKTOP_FILE,
-  COL_ID,
-  COL_PIXBUF,
-  COL_CATEGORY,
-  COL_SEARCH_TARGET,
 
-  N_COLS
-};



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]