[devhelp/wip/book-manager-singleton: 2/3] BookManager: re-order the functions to avoid prototypes
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp/wip/book-manager-singleton: 2/3] BookManager: re-order the functions to avoid prototypes
- Date: Mon, 1 May 2017 12:44:45 +0000 (UTC)
commit 9d1c6010cedd7e50e67a6cc6d2ef6cd44218a633
Author: Sébastien Wilmet <swilmet gnome org>
Date: Mon May 1 14:11:32 2017 +0200
BookManager: re-order the functions to avoid prototypes
And have a more logical order: class_init -> init -> public functions.
Plus static functions in-between.
src/dh-book-manager.c | 611 ++++++++++++++++++++++++-------------------------
1 files changed, 302 insertions(+), 309 deletions(-)
---
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index edd40a6..babff68 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -70,15 +70,8 @@ static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (DhBookManager, dh_book_manager, G_TYPE_OBJECT);
-static void book_manager_add_from_filepath (DhBookManager *book_manager,
- const gchar *book_path);
-static void book_manager_add_from_dir (DhBookManager *book_manager,
- const gchar *dir_path);
-static void book_manager_inc_language (DhBookManager *book_manager,
- const gchar *language_name);
-static void book_manager_dec_language (DhBookManager *book_manager,
- const gchar *language_name);
-static void populate (DhBookManager *book_manager);
+static void book_manager_add_from_filepath (DhBookManager *book_manager,
+ const gchar *book_path);
static void
dh_book_manager_get_property (GObject *object,
@@ -267,6 +260,34 @@ dh_book_manager_class_init (DhBookManagerClass *klass)
G_PARAM_STATIC_STRINGS));
}
+static gchar *
+book_manager_get_book_path (const gchar *base_path,
+ const gchar *name)
+{
+ static const gchar *suffixes[] = {
+ "devhelp2",
+ "devhelp2.gz",
+ "devhelp",
+ "devhelp.gz",
+ NULL
+ };
+ gchar *tmp;
+ gchar *book_path;
+ guint i;
+
+ for (i = 0; suffixes[i]; i++) {
+ tmp = g_build_filename (base_path, name, NULL);
+ book_path = g_strconcat (tmp, ".", suffixes[i], NULL);
+ g_free (tmp);
+
+ if (g_file_test (book_path, G_FILE_TEST_EXISTS)) {
+ return book_path;
+ }
+ g_free (book_path);
+ }
+ return NULL;
+}
+
static void
book_manager_load_books_disabled (DhBookManager *book_manager)
{
@@ -291,23 +312,6 @@ book_manager_load_books_disabled (DhBookManager *book_manager)
}
static void
-dh_book_manager_init (DhBookManager *book_manager)
-{
- DhSettings *settings;
-
- book_manager_load_books_disabled (book_manager);
-
- settings = dh_settings_get_singleton ();
- g_settings_bind (dh_settings_peek_contents_settings (settings),
- "group-books-by-language",
- book_manager,
- "group-by-language",
- G_SETTINGS_BIND_DEFAULT);
-
- populate (book_manager);
-}
-
-static void
book_manager_store_books_disabled (DhBookManager *book_manager)
{
DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
@@ -332,6 +336,63 @@ book_manager_store_books_disabled (DhBookManager *book_manager)
variant);
}
+static void
+book_manager_inc_language (DhBookManager *book_manager,
+ const gchar *language_name)
+{
+ GList *li;
+ DhLanguage *language;
+ DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
+
+ li = g_list_find_custom (priv->languages,
+ language_name,
+ (GCompareFunc)dh_language_compare_by_name);
+
+ /* If already in list, increase count */
+ if (li) {
+ dh_language_inc_n_books_enabled (li->data);
+ return;
+ }
+
+ /* Add new element to list if not found. Language must start with
+ * with n_books_enabled=1. */
+ language = dh_language_new (language_name);
+ dh_language_inc_n_books_enabled (language);
+ priv->languages = g_list_prepend (priv->languages,
+ language);
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[LANGUAGE_ENABLED],
+ 0,
+ language_name);
+}
+
+static void
+book_manager_dec_language (DhBookManager *book_manager,
+ const gchar *language_name)
+{
+ GList *li;
+ DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
+
+ /* Language must exist in list */
+ li = g_list_find_custom (priv->languages,
+ language_name,
+ (GCompareFunc)dh_language_compare_by_name);
+ g_assert (li != NULL);
+
+ /* If language count reaches zero, remove from list */
+ if (dh_language_dec_n_books_enabled (li->data)) {
+ g_object_unref (li->data);
+ priv->languages = g_list_delete_link (priv->languages, li);
+
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[LANGUAGE_DISABLED],
+ 0,
+ language_name);
+ }
+}
+
static gboolean
book_manager_is_book_disabled_in_conf (DhBookManager *book_manager,
DhBook *book)
@@ -353,77 +414,192 @@ book_manager_is_book_disabled_in_conf (DhBookManager *book_manager,
}
static void
-book_manager_add_books_in_data_dir (DhBookManager *book_manager,
- const gchar *data_dir)
+book_manager_book_deleted_cb (DhBook *book,
+ gpointer user_data)
{
- gchar *dir;
+ DhBookManager *book_manager = user_data;
+ DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
+ GList *li;
- dir = g_build_filename (data_dir, "gtk-doc", "html", NULL);
- book_manager_add_from_dir (book_manager, dir);
- g_free (dir);
+ /* Look for the item we want to remove */
+ li = g_list_find (priv->books, book);
+ if (li) {
+ /* Decrement language count */
+ book_manager_dec_language (book_manager,
+ dh_book_get_language (book));
- dir = g_build_filename (data_dir, "devhelp", "books", NULL);
- book_manager_add_from_dir (book_manager, dir);
- g_free (dir);
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[BOOK_DELETED],
+ 0,
+ book);
+
+ /* Remove the item and unref our reference */
+ priv->books = g_list_delete_link (priv->books, li);
+ g_object_unref (book);
+ }
}
static void
-populate (DhBookManager *book_manager)
+book_manager_book_updated_cb (DhBook *book,
+ gpointer user_data)
{
- const gchar * const * system_dirs;
+ DhBookManager *book_manager = user_data;
+ gchar *book_path;
- book_manager_add_books_in_data_dir (book_manager,
- g_get_user_data_dir ());
+ /* When we update a book, we need to delete it and then
+ * create it again. */
+ book_path = g_strdup (dh_book_get_path (book));
+ book_manager_book_deleted_cb (book, book_manager);
+ book_manager_add_from_filepath (book_manager, book_path);
+ g_free (book_path);
+}
- system_dirs = g_get_system_data_dirs ();
- while (*system_dirs) {
- book_manager_add_books_in_data_dir (book_manager,
- *system_dirs);
- system_dirs++;
+static GSList *
+book_manager_find_book_in_disabled_list (GSList *books_disabled,
+ DhBook *book)
+{
+ GSList *li;
+
+ for (li = books_disabled; li; li = g_slist_next (li)) {
+ if (g_strcmp0 (dh_book_get_name (book),
+ (const gchar *)li->data) == 0) {
+ return li;
+ }
}
+ return NULL;
}
-/**
- * dh_book_manager_populate:
- * @book_manager: a #DhBookManager.
- *
- * Populates the #DhBookManager with all books found on the system and user
- * directories.
- *
- * Deprecated: 3.26: The #DhBookManager is now automatically populated when the
- * object is created, there is no need to call this function anymore.
- */
-void
-dh_book_manager_populate (DhBookManager *book_manager)
+static void
+book_manager_book_enabled_cb (DhBook *book,
+ gpointer user_data)
{
+ DhBookManager *book_manager = user_data;
+ DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
+ GSList *li;
+
+ li = book_manager_find_book_in_disabled_list (priv->books_disabled,
+ book);
+ /* When setting as enabled a given book, we should have it in the
+ * disabled books list! */
+ g_assert (li != NULL);
+ priv->books_disabled = g_slist_delete_link (priv->books_disabled, li);
+ book_manager_store_books_disabled (book_manager);
+
+ /* Increment language count */
+ book_manager_inc_language (book_manager,
+ dh_book_get_language (book));
+
+ g_signal_emit (book_manager,
+ signals[BOOK_ENABLED],
+ 0,
+ book);
}
-static gchar *
-book_manager_get_book_path (const gchar *base_path,
- const gchar *name)
+static void
+book_manager_book_disabled_cb (DhBook *book,
+ gpointer user_data)
{
- static const gchar *suffixes[] = {
- "devhelp2",
- "devhelp2.gz",
- "devhelp",
- "devhelp.gz",
- NULL
- };
- gchar *tmp;
- gchar *book_path;
- guint i;
+ DhBookManager *book_manager = user_data;
+ DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
+ GSList *li;
- for (i = 0; suffixes[i]; i++) {
- tmp = g_build_filename (base_path, name, NULL);
- book_path = g_strconcat (tmp, ".", suffixes[i], NULL);
- g_free (tmp);
+ li = book_manager_find_book_in_disabled_list (priv->books_disabled,
+ book);
+ /* When setting as disabled a given book, we shouldn't have it in the
+ * disabled books list! */
+ g_assert (li == NULL);
+ priv->books_disabled = g_slist_append (priv->books_disabled,
+ g_strdup (dh_book_get_name (book)));
+ book_manager_store_books_disabled (book_manager);
- if (g_file_test (book_path, G_FILE_TEST_EXISTS)) {
- return book_path;
- }
- g_free (book_path);
+ /* Decrement language count */
+ book_manager_dec_language (book_manager,
+ dh_book_get_language (book));
+
+ g_signal_emit (book_manager,
+ signals[BOOK_DISABLED],
+ 0,
+ book);
+}
+
+static void
+book_manager_add_from_filepath (DhBookManager *book_manager,
+ const gchar *book_path)
+{
+ DhBookManagerPrivate *priv;
+ DhBook *book;
+ gboolean book_enabled;
+
+ g_return_if_fail (book_manager);
+ g_return_if_fail (book_path);
+
+ priv = dh_book_manager_get_instance_private (book_manager);
+
+ /* Allocate new book struct */
+ book = dh_book_new (book_path);
+ if (book == NULL)
+ return;
+
+ /* Check if book with same path was already loaded in the manager */
+ if (g_list_find_custom (priv->books,
+ book,
+ (GCompareFunc)dh_book_cmp_by_path)) {
+ g_object_unref (book);
+ return;
}
- return NULL;
+
+ /* Check if book with same bookname was already loaded in the manager
+ * (we need to force unique book names) */
+ if (g_list_find_custom (priv->books,
+ book,
+ (GCompareFunc)dh_book_cmp_by_name)) {
+ g_object_unref (book);
+ return;
+ }
+
+ /* Add the book to the book list */
+ priv->books = g_list_insert_sorted (priv->books,
+ book,
+ (GCompareFunc)dh_book_cmp_by_title);
+
+ /* Set the proper enabled/disabled state, depending on conf */
+ book_enabled = !book_manager_is_book_disabled_in_conf (book_manager, book);
+ dh_book_set_enabled (book, book_enabled);
+
+ /* Store language if enabled */
+ if (book_enabled) {
+ book_manager_inc_language (book_manager,
+ dh_book_get_language (book));
+ }
+
+ /* Get notifications of book being deleted or updated */
+ g_signal_connect_object (book,
+ "deleted",
+ G_CALLBACK (book_manager_book_deleted_cb),
+ book_manager,
+ 0);
+ g_signal_connect_object (book,
+ "updated",
+ G_CALLBACK (book_manager_book_updated_cb),
+ book_manager,
+ 0);
+ g_signal_connect_object (book,
+ "enabled",
+ G_CALLBACK (book_manager_book_enabled_cb),
+ book_manager,
+ 0);
+ g_signal_connect_object (book,
+ "disabled",
+ G_CALLBACK (book_manager_book_disabled_cb),
+ book_manager,
+ 0);
+
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[BOOK_CREATED],
+ 0,
+ book);
}
static gboolean
@@ -578,192 +754,77 @@ book_manager_add_from_dir (DhBookManager *book_manager,
}
static void
-book_manager_book_deleted_cb (DhBook *book,
- gpointer user_data)
+book_manager_add_books_in_data_dir (DhBookManager *book_manager,
+ const gchar *data_dir)
{
- DhBookManager *book_manager = user_data;
- DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
- GList *li;
-
- /* Look for the item we want to remove */
- li = g_list_find (priv->books, book);
- if (li) {
- /* Decrement language count */
- book_manager_dec_language (book_manager,
- dh_book_get_language (book));
+ gchar *dir;
- /* Emit signal to notify others */
- g_signal_emit (book_manager,
- signals[BOOK_DELETED],
- 0,
- book);
+ dir = g_build_filename (data_dir, "gtk-doc", "html", NULL);
+ book_manager_add_from_dir (book_manager, dir);
+ g_free (dir);
- /* Remove the item and unref our reference */
- priv->books = g_list_delete_link (priv->books, li);
- g_object_unref (book);
- }
+ dir = g_build_filename (data_dir, "devhelp", "books", NULL);
+ book_manager_add_from_dir (book_manager, dir);
+ g_free (dir);
}
static void
-book_manager_book_updated_cb (DhBook *book,
- gpointer user_data)
+populate (DhBookManager *book_manager)
{
- DhBookManager *book_manager = user_data;
- gchar *book_path;
-
- /* When we update a book, we need to delete it and then
- * create it again. */
- book_path = g_strdup (dh_book_get_path (book));
- book_manager_book_deleted_cb (book, book_manager);
- book_manager_add_from_filepath (book_manager, book_path);
- g_free (book_path);
-}
+ const gchar * const * system_dirs;
-static GSList *
-book_manager_find_book_in_disabled_list (GSList *books_disabled,
- DhBook *book)
-{
- GSList *li;
+ book_manager_add_books_in_data_dir (book_manager,
+ g_get_user_data_dir ());
- for (li = books_disabled; li; li = g_slist_next (li)) {
- if (g_strcmp0 (dh_book_get_name (book),
- (const gchar *)li->data) == 0) {
- return li;
- }
+ system_dirs = g_get_system_data_dirs ();
+ while (*system_dirs) {
+ book_manager_add_books_in_data_dir (book_manager,
+ *system_dirs);
+ system_dirs++;
}
- return NULL;
}
static void
-book_manager_book_enabled_cb (DhBook *book,
- gpointer user_data)
+dh_book_manager_init (DhBookManager *book_manager)
{
- DhBookManager *book_manager = user_data;
- DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
- GSList *li;
+ DhSettings *settings;
- li = book_manager_find_book_in_disabled_list (priv->books_disabled,
- book);
- /* When setting as enabled a given book, we should have it in the
- * disabled books list! */
- g_assert (li != NULL);
- priv->books_disabled = g_slist_delete_link (priv->books_disabled, li);
- book_manager_store_books_disabled (book_manager);
+ book_manager_load_books_disabled (book_manager);
- /* Increment language count */
- book_manager_inc_language (book_manager,
- dh_book_get_language (book));
+ settings = dh_settings_get_singleton ();
+ g_settings_bind (dh_settings_peek_contents_settings (settings),
+ "group-books-by-language",
+ book_manager,
+ "group-by-language",
+ G_SETTINGS_BIND_DEFAULT);
- g_signal_emit (book_manager,
- signals[BOOK_ENABLED],
- 0,
- book);
+ populate (book_manager);
}
-static void
-book_manager_book_disabled_cb (DhBook *book,
- gpointer user_data)
+/**
+ * dh_book_manager_new:
+ *
+ * Returns: a new #DhBookManager object.
+ */
+DhBookManager *
+dh_book_manager_new (void)
{
- DhBookManager *book_manager = user_data;
- DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
- GSList *li;
-
- li = book_manager_find_book_in_disabled_list (priv->books_disabled,
- book);
- /* When setting as disabled a given book, we shouldn't have it in the
- * disabled books list! */
- g_assert (li == NULL);
- priv->books_disabled = g_slist_append (priv->books_disabled,
- g_strdup (dh_book_get_name (book)));
- book_manager_store_books_disabled (book_manager);
-
- /* Decrement language count */
- book_manager_dec_language (book_manager,
- dh_book_get_language (book));
-
- g_signal_emit (book_manager,
- signals[BOOK_DISABLED],
- 0,
- book);
+ return g_object_new (DH_TYPE_BOOK_MANAGER, NULL);
}
-static void
-book_manager_add_from_filepath (DhBookManager *book_manager,
- const gchar *book_path)
+/**
+ * dh_book_manager_populate:
+ * @book_manager: a #DhBookManager.
+ *
+ * Populates the #DhBookManager with all books found on the system and user
+ * directories.
+ *
+ * Deprecated: 3.26: The #DhBookManager is now automatically populated when the
+ * object is created, there is no need to call this function anymore.
+ */
+void
+dh_book_manager_populate (DhBookManager *book_manager)
{
- DhBookManagerPrivate *priv;
- DhBook *book;
- gboolean book_enabled;
-
- g_return_if_fail (book_manager);
- g_return_if_fail (book_path);
-
- priv = dh_book_manager_get_instance_private (book_manager);
-
- /* Allocate new book struct */
- book = dh_book_new (book_path);
- if (book == NULL)
- return;
-
- /* Check if book with same path was already loaded in the manager */
- if (g_list_find_custom (priv->books,
- book,
- (GCompareFunc)dh_book_cmp_by_path)) {
- g_object_unref (book);
- return;
- }
-
- /* Check if book with same bookname was already loaded in the manager
- * (we need to force unique book names) */
- if (g_list_find_custom (priv->books,
- book,
- (GCompareFunc)dh_book_cmp_by_name)) {
- g_object_unref (book);
- return;
- }
-
- /* Add the book to the book list */
- priv->books = g_list_insert_sorted (priv->books,
- book,
- (GCompareFunc)dh_book_cmp_by_title);
-
- /* Set the proper enabled/disabled state, depending on conf */
- book_enabled = !book_manager_is_book_disabled_in_conf (book_manager, book);
- dh_book_set_enabled (book, book_enabled);
-
- /* Store language if enabled */
- if (book_enabled) {
- book_manager_inc_language (book_manager,
- dh_book_get_language (book));
- }
-
- /* Get notifications of book being deleted or updated */
- g_signal_connect_object (book,
- "deleted",
- G_CALLBACK (book_manager_book_deleted_cb),
- book_manager,
- 0);
- g_signal_connect_object (book,
- "updated",
- G_CALLBACK (book_manager_book_updated_cb),
- book_manager,
- 0);
- g_signal_connect_object (book,
- "enabled",
- G_CALLBACK (book_manager_book_enabled_cb),
- book_manager,
- 0);
- g_signal_connect_object (book,
- "disabled",
- G_CALLBACK (book_manager_book_disabled_cb),
- book_manager,
- 0);
-
- /* Emit signal to notify others */
- g_signal_emit (book_manager,
- signals[BOOK_CREATED],
- 0,
- book);
}
/**
@@ -823,71 +884,3 @@ dh_book_manager_set_group_by_language (DhBookManager *book_manager,
priv->group_by_language = group_by_language;
g_object_notify (G_OBJECT (book_manager), "group-by-language");
}
-
-static void
-book_manager_inc_language (DhBookManager *book_manager,
- const gchar *language_name)
-{
- GList *li;
- DhLanguage *language;
- DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
-
- li = g_list_find_custom (priv->languages,
- language_name,
- (GCompareFunc)dh_language_compare_by_name);
-
- /* If already in list, increase count */
- if (li) {
- dh_language_inc_n_books_enabled (li->data);
- return;
- }
-
- /* Add new element to list if not found. Language must start with
- * with n_books_enabled=1. */
- language = dh_language_new (language_name);
- dh_language_inc_n_books_enabled (language);
- priv->languages = g_list_prepend (priv->languages,
- language);
- /* Emit signal to notify others */
- g_signal_emit (book_manager,
- signals[LANGUAGE_ENABLED],
- 0,
- language_name);
-}
-
-static void
-book_manager_dec_language (DhBookManager *book_manager,
- const gchar *language_name)
-{
- GList *li;
- DhBookManagerPrivate *priv = dh_book_manager_get_instance_private (book_manager);
-
- /* Language must exist in list */
- li = g_list_find_custom (priv->languages,
- language_name,
- (GCompareFunc)dh_language_compare_by_name);
- g_assert (li != NULL);
-
- /* If language count reaches zero, remove from list */
- if (dh_language_dec_n_books_enabled (li->data)) {
- g_object_unref (li->data);
- priv->languages = g_list_delete_link (priv->languages, li);
-
- /* Emit signal to notify others */
- g_signal_emit (book_manager,
- signals[LANGUAGE_DISABLED],
- 0,
- language_name);
- }
-}
-
-/**
- * dh_book_manager_new:
- *
- * Returns: a new #DhBookManager object.
- */
-DhBookManager *
-dh_book_manager_new (void)
-{
- return g_object_new (DH_TYPE_BOOK_MANAGER, NULL);
-}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]