devhelp r1094 - in trunk: . src



Author: rhult
Date: Thu Oct  2 20:02:44 2008
New Revision: 1094
URL: http://svn.gnome.org/viewvc/devhelp?rev=1094&view=rev

Log:
2008-10-02  Richard Hult  <richard imendio com>

	* src/dh-base.c:
	* src/dh-base.h: Add private instance data and fixup
	whitepace/style.


Modified:
   trunk/ChangeLog
   trunk/src/dh-base.c
   trunk/src/dh-base.h

Modified: trunk/src/dh-base.c
==============================================================================
--- trunk/src/dh-base.c	(original)
+++ trunk/src/dh-base.c	Thu Oct  2 20:02:44 2008
@@ -39,386 +39,355 @@
 #include "dh-preferences.h"
 #include "dh-base.h"
 
-#define d(x) 
+typedef struct {
+        GSList      *windows;
+        GNode       *book_tree;
+        GList       *keywords;
+        GHashTable  *books;
+        GConfClient *gconf_client;
+} DhBasePriv;
+
+G_DEFINE_TYPE (DhBase, dh_base, G_TYPE_OBJECT);
+
+#define GET_PRIVATE(instance) G_TYPE_INSTANCE_GET_PRIVATE  \
+  (instance, DH_TYPE_BASE, DhBasePriv);
+
+static void dh_base_init       (DhBase      *base);
+static void dh_base_class_init (DhBaseClass *klass);
+static void base_init_books    (DhBase      *base);
+static void base_add_books     (DhBase      *base,
+                                const gchar *directory);
 
-struct _DhBasePriv {
-	GSList      *windows;
-	GNode       *book_tree;
-	GList       *keywords;
-	GHashTable  *books;
-	GConfClient *gconf_client;
-};
-
-static void base_init                (DhBase      *base);
-static void base_class_init          (DhBaseClass *klass);
-static void base_window_finalized_cb (DhBase      *base,
-				      DhWindow    *window);
-static void base_init_books          (DhBase      *base);
-static void base_add_books           (DhBase      *base,
-				      const gchar *directory);
-
-
-static GObjectClass *parent_class;
-static DhBase       *base_instance;
-
-GType
-dh_base_get_type (void)
-{
-	static GType type = 0;
-
-	if (!type) {
-		static const GTypeInfo info = {
-			sizeof (DhBaseClass),
-			NULL,
-			NULL,
-			(GClassInitFunc) base_class_init,
-			NULL,
-			NULL,
-			sizeof (DhBase),
-			0,
-			(GInstanceInitFunc) base_init,
-		};
-
-		type = g_type_register_static (G_TYPE_OBJECT, "DhBase",
-					       &info, 0);
-	}
-
-	return type;
-}
+static DhBase *base_instance;
 
 static void
-base_init (DhBase *base)
+base_finalize (GObject *object)
 {
-        DhBasePriv *priv;
-
-        priv = g_new0 (DhBasePriv, 1);
-        base->priv = priv;
-
-	priv->windows   = NULL;
-	priv->book_tree = g_node_new (NULL);
-	priv->keywords  = NULL;
-	priv->books     = g_hash_table_new_full (g_str_hash, g_str_equal,
-						 g_free, g_free);
+        DhBasePriv *priv = GET_PRIVATE (object);
 
-#ifdef HAVE_PLATFORM_X11
-	{
-		gint n_screens, i;
-
-		/* For some reason, libwnck doesn't seem to update its list of
-		 * workspaces etc if we don't do this.
-		 */
-		n_screens = gdk_display_get_n_screens (gdk_display_get_default ());
-		for (i = 0; i < n_screens; i++) {
-			WnckScreen *screen;
-			
-			screen = wnck_screen_get (i);
-		}
-	}
-#endif
+        g_object_unref (priv->gconf_client);
 
-	priv->gconf_client = gconf_client_get_default ();
-	gconf_client_add_dir (priv->gconf_client,
-			      GCONF_PATH,
-			      GCONF_CLIENT_PRELOAD_ONELEVEL,
-			      NULL);
+        G_OBJECT_CLASS (dh_base_parent_class)->finalize (object);
 }
 
 static void
-dh_base_finalize (GObject *object)
+dh_base_class_init (DhBaseClass *klass)
 {
-        DhBasePriv *priv;
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-	priv = DH_BASE (object)->priv;
+        object_class->finalize = base_finalize;
 
-	g_object_unref (priv->gconf_client);
-	
-	parent_class->finalize (object);
+	g_type_class_add_private (klass, sizeof (DhBasePriv));
 }
 
 static void
-base_class_init (DhBaseClass *klass)
+dh_base_init (DhBase *base)
 {
-	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        DhBasePriv *priv = GET_PRIVATE (base);
 
-	parent_class = g_type_class_peek_parent (klass);
+        priv->windows = NULL;
+        priv->book_tree = g_node_new (NULL);
+        priv->keywords = NULL;
+        priv->books = g_hash_table_new_full (g_str_hash, g_str_equal,
+                                             g_free, g_free);
 
-	object_class->finalize = dh_base_finalize;
+#ifdef HAVE_PLATFORM_X11
+        {
+                gint n_screens, i;
+
+                /* For some reason, libwnck doesn't seem to update its list of
+                 * workspaces etc if we don't do this.
+                 */
+                n_screens = gdk_display_get_n_screens (gdk_display_get_default ());
+                for (i = 0; i < n_screens; i++) {
+                        WnckScreen *screen;
+
+                        screen = wnck_screen_get (i);
+                }
+        }
+#endif
+
+        priv->gconf_client = gconf_client_get_default ();
+        gconf_client_add_dir (priv->gconf_client,
+                              GCONF_PATH,
+                              GCONF_CLIENT_PRELOAD_ONELEVEL,
+                              NULL);
 }
 
 static void
-base_window_finalized_cb (DhBase *base, DhWindow *window)
+base_window_finalized_cb (DhBase   *base,
+                          DhWindow *window)
 {
-	DhBasePriv *priv;
+        DhBasePriv *priv = GET_PRIVATE (base);
 
-	priv = base->priv;
+        priv->windows = g_slist_remove (priv->windows, window);
 
-	priv->windows = g_slist_remove (priv->windows, window);
-
-	if (g_slist_length (priv->windows) == 0) {
-		gtk_main_quit ();
-	}
+        if (g_slist_length (priv->windows) == 0) {
+                gtk_main_quit ();
+        }
 }
 
 static gint
 book_sort_func (gconstpointer a,
-		gconstpointer b)
+                gconstpointer b)
 {
-	DhLink      *link_a, *link_b;
-	const gchar *name_a, *name_b;
+        DhLink      *link_a, *link_b;
+        const gchar *name_a, *name_b;
 
-	link_a = ((GNode *) a)->data;
-	link_b = ((GNode *) b)->data;
+        link_a = ((GNode *) a)->data;
+        link_b = ((GNode *) b)->data;
 
-	name_a = link_a->name;
-	if (!name_a) {
-		name_a = "";
-	}
-
-	name_b = link_b->name;
-	if (!name_b) {
-		name_b = "";
-	}
-
-	if (g_ascii_strncasecmp (name_a, "the ", 4) == 0) {
-		name_a += 4;
-	}
-	if (g_ascii_strncasecmp (name_b, "the ", 4) == 0) {
-		name_b += 4;
-	}
+        name_a = link_a->name;
+        if (!name_a) {
+                name_a = "";
+        }
+
+        name_b = link_b->name;
+        if (!name_b) {
+                name_b = "";
+        }
 
-	return g_utf8_collate (name_a, name_b);
+        if (g_ascii_strncasecmp (name_a, "the ", 4) == 0) {
+                name_a += 4;
+        }
+        if (g_ascii_strncasecmp (name_b, "the ", 4) == 0) {
+                name_b += 4;
+        }
+
+        return g_utf8_collate (name_a, name_b);
 }
 
 static void
 base_sort_books (DhBase *base)
 {
-	DhBasePriv *priv;
-	GNode      *n;
-	DhLink     *link;
-	GList      *list = NULL, *l;
-
-	priv = base->priv;
-
-	if (base->priv->book_tree) {
-		n = base->priv->book_tree->children;
-
-		while (n) {
-			list = g_list_prepend (list, n);
-			n = n->next;
-		}
+        DhBasePriv *priv = GET_PRIVATE (base);
+        GNode      *n;
+        DhLink     *link;
+        GList      *list = NULL, *l;
+
+        if (priv->book_tree) {
+                n = priv->book_tree->children;
+
+                while (n) {
+                        list = g_list_prepend (list, n);
+                        n = n->next;
+                }
 
-		list = g_list_sort (list, book_sort_func);
-	}
+                list = g_list_sort (list, book_sort_func);
+        }
 
-	for (l = list; l; l = l->next) {
-		n = l->data;
-		link = n->data;
-		g_node_unlink (n);
-	}
+        for (l = list; l; l = l->next) {
+                n = l->data;
+                link = n->data;
+                g_node_unlink (n);
+        }
 
-	for (l = list; l; l = l->next) {
-		n = l->data;
+        for (l = list; l; l = l->next) {
+                n = l->data;
 
-		g_node_append (base->priv->book_tree, n);
-	}
+                g_node_append (priv->book_tree, n);
+        }
 
-	g_list_free (list);
+        g_list_free (list);
 }
 
 static void
 base_add_books_in_data_dir (DhBase *base, const gchar *data_dir)
 {
-	gchar *dir;
-	
-	dir = g_build_filename (data_dir, "gtk-doc", "html", NULL);
-	base_add_books (base, dir);
-	g_free (dir);
-
-	dir = g_build_filename (data_dir, "devhelp", "books", NULL);
-	base_add_books (base, dir);
-	g_free (dir);
+        gchar *dir;
+
+        dir = g_build_filename (data_dir, "gtk-doc", "html", NULL);
+        base_add_books (base, dir);
+        g_free (dir);
+
+        dir = g_build_filename (data_dir, "devhelp", "books", NULL);
+        base_add_books (base, dir);
+        g_free (dir);
 }
 
 static void
 base_init_books (DhBase *base)
 {
-	const gchar * const * system_dirs;
+        const gchar * const * system_dirs;
 
-	base_add_books_in_data_dir (base, g_get_user_data_dir ());
+        base_add_books_in_data_dir (base, g_get_user_data_dir ());
 
-	system_dirs = g_get_system_data_dirs ();
-	while (*system_dirs) {
-		base_add_books_in_data_dir (base, *system_dirs);
-		system_dirs++;
-	}
+        system_dirs = g_get_system_data_dirs ();
+        while (*system_dirs) {
+                base_add_books_in_data_dir (base, *system_dirs);
+                system_dirs++;
+        }
 
-	base_sort_books (base);
+        base_sort_books (base);
 }
 
 static gchar *
 base_get_book_path (DhBase      *base,
-		    const gchar *base_path,
-		    const gchar *name,
-		    const gchar *suffix)
-{
-	gchar *tmp;
-	gchar *book_path;
-	
-	tmp = g_build_filename (base_path, name, name, NULL);
-	book_path = g_strconcat (tmp, ".", suffix, NULL);
-	g_free (tmp);
-
-	if (!g_file_test (book_path, G_FILE_TEST_EXISTS)) {
-		g_free (book_path);
-		return NULL;
-	}
+                    const gchar *base_path,
+                    const gchar *name,
+                    const gchar *suffix)
+{
+        gchar *tmp;
+        gchar *book_path;
+
+        tmp = g_build_filename (base_path, name, name, NULL);
+        book_path = g_strconcat (tmp, ".", suffix, NULL);
+        g_free (tmp);
+
+        if (!g_file_test (book_path, G_FILE_TEST_EXISTS)) {
+                g_free (book_path);
+                return NULL;
+        }
 
-	return book_path;
-}	
+        return book_path;
+}
 
 static void
 base_add_books (DhBase *base, const gchar *path)
 {
-	DhBasePriv  *priv;
-	GDir        *dir;
-	const gchar *name;
-
-	priv = base->priv;
-
-	d(g_print ("Adding books from %s\n", path));
-
-	dir = g_dir_open (path, 0, NULL);
-	if (!dir) {
-		return;
-	}
-
-	while ((name = g_dir_read_name (dir)) != NULL) {
-		gchar  *book_path;
-		GError *error = NULL;
-
-		if (g_hash_table_lookup (priv->books, name)) {
-			continue;
-		}
-
-		book_path = base_get_book_path (base, path, name, "devhelp2");
-		if (!book_path) {
-			book_path = base_get_book_path (base, path, name, "devhelp2.gz");
-		}
-		if (!book_path) {
-			book_path = base_get_book_path (base, path, name, "devhelp");
-		}
-		if (!book_path) {
-			book_path = base_get_book_path (base, path, name, "devhelp.gz");
-		}
-		
-		if (!book_path) {
-			continue;
-		}
-
-		if (!dh_parser_read_file  (book_path,
-					   priv->book_tree,
-					   &priv->keywords,
-					   &error)) {
-			g_warning ("Failed to read '%s': %s",
-				   book_path, error->message);
-			g_clear_error (&error);
-		} else {
-			g_hash_table_insert (priv->books,
-					     g_strdup (name),
-					     book_path);
-			d(g_print ("Found book: '%s'\n", book_path));
-		}
+        DhBasePriv  *priv = GET_PRIVATE (base);
+        GDir        *dir;
+        const gchar *name;
+
+        dir = g_dir_open (path, 0, NULL);
+        if (!dir) {
+                return;
+        }
 
-		g_free (book_path);
-	}
+        while ((name = g_dir_read_name (dir)) != NULL) {
+                gchar  *book_path;
+                GError *error = NULL;
 
-	g_dir_close (dir);
+                if (g_hash_table_lookup (priv->books, name)) {
+                        continue;
+                }
+
+                book_path = base_get_book_path (base, path, name, "devhelp2");
+                if (!book_path) {
+                        book_path = base_get_book_path (base, path, name, "devhelp2.gz");
+                }
+                if (!book_path) {
+                        book_path = base_get_book_path (base, path, name, "devhelp");
+                }
+                if (!book_path) {
+                        book_path = base_get_book_path (base, path, name, "devhelp.gz");
+                }
+
+                if (!book_path) {
+                        continue;
+                }
+
+                if (!dh_parser_read_file  (book_path,
+                                           priv->book_tree,
+                                           &priv->keywords,
+                                           &error)) {
+                        g_warning ("Failed to read '%s': %s",
+                                   book_path, error->message);
+                        g_clear_error (&error);
+                } else {
+                        g_hash_table_insert (priv->books,
+                                             g_strdup (name),
+                                             book_path);
+                }
+
+                g_free (book_path);
+        }
+
+        g_dir_close (dir);
 }
 
 DhBase *
 dh_base_get (void)
 {
-	if (!base_instance) {
-		base_instance = g_object_new (DH_TYPE_BASE, NULL);		
+        if (!base_instance) {
+                base_instance = g_object_new (DH_TYPE_BASE, NULL);
 
-		base_init_books (base_instance);
-		dh_preferences_init ();
-	}
-	
-	return base_instance;
+                base_init_books (base_instance);
+                dh_preferences_init ();
+        }
+
+        return base_instance;
 }
 
 DhBase *
 dh_base_new (void)
 {
-	if (base_instance) {
-		g_error ("You can only have one DhBase instance.");
-	}
+        if (base_instance) {
+                g_error ("You can only have one DhBase instance.");
+        }
 
-	return dh_base_get ();
+        return dh_base_get ();
 }
 
 GtkWidget *
 dh_base_new_window (DhBase *base)
 {
-	DhBasePriv *priv;
-	GtkWidget  *window;
+        DhBasePriv *priv;
+        GtkWidget  *window;
 
         g_return_val_if_fail (DH_IS_BASE (base), NULL);
 
-	priv = base->priv;
+        priv = GET_PRIVATE (base);
 
         window = dh_window_new (base);
 
-	priv->windows = g_slist_prepend (priv->windows, window);
+        priv->windows = g_slist_prepend (priv->windows, window);
 
-	g_object_weak_ref (G_OBJECT (window),
-			   (GWeakNotify) base_window_finalized_cb,
-			   base);
+        g_object_weak_ref (G_OBJECT (window),
+                           (GWeakNotify) base_window_finalized_cb,
+                           base);
 
-	return window;
+        return window;
 }
 
 GNode *
 dh_base_get_book_tree (DhBase *base)
 {
-	g_return_val_if_fail (DH_IS_BASE (base), NULL);
+        DhBasePriv *priv;
 
-	return base->priv->book_tree;
+        g_return_val_if_fail (DH_IS_BASE (base), NULL);
+
+        priv = GET_PRIVATE (base);
+
+        return priv->book_tree;
 }
 
 GList *
 dh_base_get_keywords (DhBase *base)
 {
-	g_return_val_if_fail (DH_IS_BASE (base), NULL);
+        DhBasePriv *priv;
+
+        g_return_val_if_fail (DH_IS_BASE (base), NULL);
 
-	return base->priv->keywords;
+        priv = GET_PRIVATE (base);
+
+        return priv->keywords;
 }
 
 GSList *
 dh_base_get_windows (DhBase *base)
 {
-	DhBasePriv *priv;
+        DhBasePriv *priv;
 
-	g_return_val_if_fail (DH_IS_BASE (base), NULL);
+        g_return_val_if_fail (DH_IS_BASE (base), NULL);
 
-	priv = base->priv;
+        priv = GET_PRIVATE (base);
 
-	return priv->windows;
+        return priv->windows;
 }
 
 GtkWidget *
 dh_base_get_window_on_current_workspace (DhBase *base)
 {
-	DhBasePriv    *priv;
+        DhBasePriv *priv;
 
-	g_return_val_if_fail (DH_IS_BASE (base), NULL);
+        g_return_val_if_fail (DH_IS_BASE (base), NULL);
 
-	priv = base->priv;
+        priv = GET_PRIVATE (base);
 
-	if (!priv->windows) {
-		return NULL;
-	}
+        if (!priv->windows) {
+                return NULL;
+        }
 
 #ifdef HAVE_PLATFORM_X11
         {
@@ -478,13 +447,13 @@
 GConfClient *
 dh_base_get_gconf_client (DhBase *base)
 {
-	DhBasePriv *priv;
+        DhBasePriv *priv;
+
+        g_return_val_if_fail (DH_IS_BASE (base), NULL);
 
-	g_return_val_if_fail (DH_IS_BASE (base), NULL);
-	
-	priv = base->priv;
-	
-	return priv->gconf_client;
+        priv = GET_PRIVATE (base);
+
+        return priv->gconf_client;
 }
 
 GtkWidget *
@@ -492,13 +461,13 @@
 {
         GtkWidget *window;
 
-	g_return_val_if_fail (DH_IS_BASE (base), NULL);
-	
-	window = dh_base_get_window_on_current_workspace (base);
-	if (!window) {
-		window = dh_base_new_window (base);
-		gtk_window_present (GTK_WINDOW (window));
-	}
+        g_return_val_if_fail (DH_IS_BASE (base), NULL);
+
+        window = dh_base_get_window_on_current_workspace (base);
+        if (!window) {
+                window = dh_base_new_window (base);
+                gtk_window_present (GTK_WINDOW (window));
+        }
 
         return window;
 }

Modified: trunk/src/dh-base.h
==============================================================================
--- trunk/src/dh-base.h	(original)
+++ trunk/src/dh-base.h	Thu Oct  2 20:02:44 2008
@@ -28,7 +28,6 @@
 
 typedef struct _DhBase      DhBase;
 typedef struct _DhBaseClass DhBaseClass;
-typedef struct _DhBasePriv  DhBasePriv;
 
 #define DH_TYPE_BASE         (dh_base_get_type ())
 #define DH_BASE(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), DH_TYPE_BASE, DhBase))
@@ -37,11 +36,8 @@
 #define DH_IS_BASE_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), DH_TYPE_BASE))
 #define DH_BASE_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), DH_TYPE_BASE, DhBaseClass))
 
-
 struct _DhBase {
-        GObject     parent;
-        
-        DhBasePriv *priv;
+        GObject parent_instance;
 };
 
 struct _DhBaseClass {
@@ -52,11 +48,11 @@
 DhBase *     dh_base_get                             (void);
 DhBase *     dh_base_new                             (void);
 GtkWidget *  dh_base_new_window                      (DhBase *base);
+GtkWidget *  dh_base_get_window                      (DhBase *base);
 GtkWidget *  dh_base_get_window_on_current_workspace (DhBase *base);
 GNode *      dh_base_get_book_tree                   (DhBase *base);
 GList *      dh_base_get_keywords                    (DhBase *base);
 GSList *     dh_base_get_windows                     (DhBase *base);
 GConfClient *dh_base_get_gconf_client                (DhBase *base);
-GtkWidget *  dh_base_get_window                      (DhBase *base);
 
 #endif /* __DH_BASE_H__ */



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