[devhelp: 34/36] language: new object in separate source files
- From: Aleksander Morgado <aleksm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp: 34/36] language: new object in separate source files
- Date: Mon, 20 Dec 2010 14:34:16 +0000 (UTC)
commit e977a755cfbd1c97d7d9877fc5489caae63e0aff
Author: Aleksander Morgado <aleksander lanedo com>
Date: Sun Dec 19 18:20:09 2010 +0100
language: new object in separate source files
src/Makefile.am | 2 +
src/dh-book-manager.c | 128 ++++++++++++------------------------------------
src/dh-book-manager.h | 10 +----
src/dh-language.c | 105 ++++++++++++++++++++++++++++++++++++++++
src/dh-language.h | 40 +++++++++++++++
src/dh-search.c | 3 +-
6 files changed, 182 insertions(+), 106 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 0659ba5..a153824 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -52,6 +52,7 @@ INST_H_FILES = \
dh-assistant-view.h \
dh-base.h \
dh-book-manager.h \
+ dh-language.h \
dh-book.h \
dh-book-tree.h \
dh-error.h \
@@ -72,6 +73,7 @@ libdevhelp_3_la_SOURCES = \
dh-assistant-view.c \
dh-base.c \
dh-book-manager.c \
+ dh-language.c \
dh-book.c \
dh-book-tree.c \
dh-error.c \
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index c333cd2..1d92d1b 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -27,6 +27,7 @@
#include "dh-link.h"
#include "dh-util.h"
#include "dh-book.h"
+#include "dh-language.h"
#include "dh-book-manager.h"
#include "dh-marshal.h"
@@ -37,11 +38,6 @@ typedef struct {
GFile *file;
} NewPossibleBookData;
-struct _DhBookManagerLanguage {
- gchar *name;
- gint n_books_enabled;
-};
-
typedef struct {
/* The list of all DhBooks found in the system */
GList *books;
@@ -86,9 +82,9 @@ static void book_manager_add_from_filepath (DhBookManager *book_manager,
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);
+ const gchar *language_name);
static void book_manager_dec_language (DhBookManager *book_manager,
- const gchar *language);
+ const gchar *language_name);
static void book_manager_get_property (GObject *object,
guint prop_id,
GValue *value,
@@ -119,12 +115,9 @@ book_manager_finalize (GObject *object)
g_list_free (priv->books);
/* Free all languages */
- for (l = priv->languages; l; l = g_list_next (l)) {
- DhBookManagerLanguage *lang = l->data;
-
- g_free (lang->name);
- g_free (lang);
- }
+ g_list_foreach (priv->languages,
+ (GFunc)dh_language_free,
+ NULL);
g_list_free (priv->languages);
/* Destroy the monitors HT */
@@ -774,36 +767,6 @@ dh_book_manager_get_books (DhBookManager *book_manager)
return GET_PRIVATE (book_manager)->books;
}
-DhBook *
-dh_book_manager_get_book_by_name (DhBookManager *book_manager,
- const gchar *name)
-{
- GList *l;
-
- g_return_val_if_fail (book_manager, NULL);
-
- l = g_list_find_custom (GET_PRIVATE (book_manager)->books,
- name,
- (GCompareFunc)dh_book_cmp_by_name_str);
-
- return l ? l->data : NULL;
-}
-
-DhBook *
-dh_book_manager_get_book_by_path (DhBookManager *book_manager,
- const gchar *path)
-{
- GList *l;
-
- g_return_val_if_fail (book_manager, NULL);
-
- l = g_list_find_custom (GET_PRIVATE (book_manager)->books,
- path,
- (GCompareFunc)dh_book_cmp_by_path_str);
-
- return l ? l->data : NULL;
-}
-
gboolean
dh_book_manager_get_group_by_language (DhBookManager *book_manager)
{
@@ -831,70 +794,57 @@ dh_book_manager_set_group_by_language (DhBookManager *book_manager,
static void
book_manager_inc_language (DhBookManager *book_manager,
- const gchar *language)
+ const gchar *language_name)
{
GList *li;
- DhBookManagerLanguage *lang_data;
+ DhLanguage *language;
DhBookManagerPriv *priv = GET_PRIVATE (book_manager);
- for (li = priv->languages; li; li = g_list_next (li)) {
- lang_data = li->data;
+ li = g_list_find_custom (priv->languages,
+ language_name,
+ (GCompareFunc)dh_language_compare_by_name);
- if (strcmp (language, lang_data->name) == 0) {
- /* Already in list. */
- lang_data->n_books_enabled++;
- break;
- }
+ /* 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 */
- if (!li) {
- lang_data = g_new (DhBookManagerLanguage, 1);
- lang_data->name = g_strdup (language);
- lang_data->n_books_enabled = 1;
- priv->languages = g_list_prepend (priv->languages,
- lang_data);
- /* Emit signal to notify others */
- g_signal_emit (book_manager,
- signals[LANGUAGE_ENABLED],
- 0,
- language);
- }
+ language = dh_language_new (language_name);
+ 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)
+ const gchar *language_name)
{
GList *li;
- DhBookManagerLanguage *lang_data;
DhBookManagerPriv *priv = GET_PRIVATE (book_manager);
- for (li = priv->languages; li; li = g_list_next (li)) {
- lang_data = li->data;
-
- if (strcmp (language, lang_data->name) == 0) {
- /* Already in list. */
- lang_data->n_books_enabled--;
- break;
- }
- }
-
- /* Language */
+ /* Language must exist in list */
+ li = g_list_find_custom (priv->languages,
+ language_name,
+ (GCompareFunc)dh_language_compare_by_name);
g_assert (li != NULL);
- g_assert (lang_data->n_books_enabled >= 0);
/* If language count reaches zero, remove from list */
- if (lang_data->n_books_enabled == 0) {
- g_free (lang_data->name);
- g_free (lang_data);
+ if (dh_language_dec_n_books_enabled (li->data))
+ {
+ dh_language_free (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);
+ language_name);
}
}
@@ -906,20 +856,6 @@ dh_book_manager_get_languages (DhBookManager *book_manager)
return GET_PRIVATE (book_manager)->languages;
}
-const gchar *
-dh_book_manager_language_get_name (DhBookManagerLanguage *language)
-{
- g_return_val_if_fail (language != NULL, NULL);
- return language->name;
-}
-
-gint
-dh_book_manager_language_get_n_books_enabled (DhBookManagerLanguage *language)
-{
- g_return_val_if_fail (language != NULL, 0);
- return language->n_books_enabled;
-}
-
DhBookManager *
dh_book_manager_new (void)
{
diff --git a/src/dh-book-manager.h b/src/dh-book-manager.h
index 7218737..3f7b086 100644
--- a/src/dh-book-manager.h
+++ b/src/dh-book-manager.h
@@ -29,7 +29,6 @@ G_BEGIN_DECLS
typedef struct _DhBookManager DhBookManager;
typedef struct _DhBookManagerClass DhBookManagerClass;
-typedef struct _DhBookManagerLanguage DhBookManagerLanguage;
#define DH_TYPE_BOOK_MANAGER (dh_book_manager_get_type ())
#define DH_BOOK_MANAGER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), DH_TYPE_BOOK_MANAGER, DhBookManager))
@@ -50,19 +49,12 @@ GType dh_book_manager_get_type (void) G_GNUC_CONST;
DhBookManager *dh_book_manager_new (void);
void dh_book_manager_populate (DhBookManager *book_manager);
GList *dh_book_manager_get_books (DhBookManager *book_manager);
-DhBook *dh_book_manager_get_book_by_name (DhBookManager *book_manager,
- const gchar *name);
-DhBook *dh_book_manager_get_book_by_path (DhBookManager *book_manager,
- const gchar *path);
+GList *dh_book_manager_get_languages (DhBookManager *book_manager);
gboolean dh_book_manager_get_group_by_language (DhBookManager *book_manager);
void dh_book_manager_set_group_by_language (DhBookManager *book_manager,
gboolean group_by_language);
-GList *dh_book_manager_get_languages (DhBookManager *book_manager);
-const gchar *dh_book_manager_language_get_name (DhBookManagerLanguage *language);
-gint dh_book_manager_language_get_n_books_enabled (DhBookManagerLanguage *language);
-
G_END_DECLS
#endif /* __DH_BOOK_MANAGER_H__ */
diff --git a/src/dh-language.c b/src/dh-language.c
new file mode 100644
index 0000000..a50698a
--- /dev/null
+++ b/src/dh-language.c
@@ -0,0 +1,105 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Lanedo GmbH
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include <string.h>
+
+#include "dh-language.h"
+
+struct _DhLanguage {
+ /* Name of the language */
+ gchar *name;
+ /* Number of books enabled in the language */
+ gint n_books_enabled;
+};
+
+void
+dh_language_free (DhLanguage *language)
+{
+ g_free (language->name);
+ g_slice_free (DhLanguage, language);
+}
+
+DhLanguage *
+dh_language_new (const gchar *name)
+{
+ DhLanguage *language;
+
+ g_return_val_if_fail (name != NULL, NULL);
+
+ language = g_slice_new0 (DhLanguage);
+ language->name = g_strdup (name);
+
+ return language;
+}
+
+gint
+dh_language_compare (const DhLanguage *language_a,
+ const DhLanguage *language_b)
+{
+ g_return_val_if_fail (language_a != NULL, NULL);
+ g_return_val_if_fail (language_b != NULL, NULL);
+
+ return strcmp (language_a->name, language_b->name);
+}
+
+gint
+dh_language_compare_by_name (const DhLanguage *language_a,
+ const gchar *language_name_b)
+{
+ g_return_val_if_fail (language_a != NULL, NULL);
+ g_return_val_if_fail (language_name_b != NULL, NULL);
+
+ return strcmp (language_a->name, language_name_b);
+}
+
+const gchar *
+dh_language_get_name (DhLanguage *language)
+{
+ g_return_val_if_fail (language != NULL, NULL);
+
+ return language->name;
+}
+
+
+gint
+dh_language_get_n_books_enabled (DhLanguage *language)
+{
+ g_return_val_if_fail (language != NULL, NULL);
+
+ return language->n_books_enabled;
+}
+
+void
+dh_language_inc_n_books_enabled (DhLanguage *language)
+{ g_return_if_fail (language != NULL);
+
+ language->n_books_enabled++;
+}
+
+gboolean
+dh_language_dec_n_books_enabled (DhLanguage *language)
+{
+ g_return_val_if_fail (language != NULL, FALSE);
+
+ language->n_books_enabled--;
+
+ return (language->n_books_enabled <= 0) ? TRUE : FALSE;
+}
diff --git a/src/dh-language.h b/src/dh-language.h
new file mode 100644
index 0000000..ebd30d6
--- /dev/null
+++ b/src/dh-language.h
@@ -0,0 +1,40 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2010 Lanedo GmbH
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __DH_LANGUAGE_H__
+#define __DH_LANGUAGE_H__
+
+#include <glib-object.h>
+
+typedef struct _DhLanguage DhLanguage;
+
+DhLanguage *dh_language_new (const gchar *name);
+void dh_language_free (DhLanguage *language);
+const gchar *dh_language_get_name (DhLanguage *language);
+gint dh_language_compare (const DhLanguage *language_a,
+ const DhLanguage *language_b);
+gint dh_language_compare_by_name (const DhLanguage *language_a,
+ const gchar *language_name_b);
+gint dh_language_get_n_books_enabled (DhLanguage *language);
+void dh_language_inc_n_books_enabled (DhLanguage *language);
+gboolean dh_language_dec_n_books_enabled (DhLanguage *language);
+
+
+#endif /* __DH_LANGUAGE_H__ */
diff --git a/src/dh-search.c b/src/dh-search.c
index b8fa391..fce8352 100644
--- a/src/dh-search.c
+++ b/src/dh-search.c
@@ -34,6 +34,7 @@
#include "dh-util.h"
#include "dh-book-manager.h"
#include "dh-book.h"
+#include "dh-language.h"
typedef struct {
DhKeywordModel *model;
@@ -516,7 +517,7 @@ search_combo_populate (DhSearch *search)
l;
l = g_list_next (l)) {
search_combo_add_language (search,
- dh_book_manager_language_get_name (l->data));
+ dh_language_get_name (l->data));
}
/* Add book items */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]