[devhelp: 3/36] book: notify signal if updated or deleted
- From: Aleksander Morgado <aleksm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp: 3/36] book: notify signal if updated or deleted
- Date: Mon, 20 Dec 2010 14:31:40 +0000 (UTC)
commit be07b08cd3ea3075e7da87706d6a55b563166c08
Author: Aleksander Morgado <aleksander lanedo com>
Date: Thu Dec 2 16:05:40 2010 +0100
book: notify signal if updated or deleted
src/dh-book-manager.c | 29 ++++++++++++++++++++++++++++-
src/dh-book.c | 44 ++++++++++++++++++++++++++++++++++++++------
2 files changed, 66 insertions(+), 7 deletions(-)
---
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index f0af2b0..cc375da 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -390,11 +390,32 @@ book_manager_add_from_xcode_docset (DhBookManager *book_manager,
#endif
static void
+book_manager_book_deleted_cb (DhBook *book,
+ gpointer user_data)
+{
+ DhBookManager *book_manager = user_data;
+ DhBookManagerPriv *priv;
+ GList *li;
+
+ priv = GET_PRIVATE (book_manager);
+
+ /* Look for the item we want to remove */
+ li = g_list_find (priv->books, book);
+ if (li) {
+ g_debug ("Deleting book '%s' from the book manager list",
+ dh_book_get_title (book));
+ /* Remove the item and unref our reference */
+ priv->books = g_list_delete_link (priv->books, li);
+ g_object_unref (book);
+ }
+}
+
+static void
book_manager_add_from_filepath (DhBookManager *book_manager,
const gchar *book_path)
{
DhBookManagerPriv *priv;
- DhBook *book;
+ DhBook *book;
g_return_if_fail (book_manager);
g_return_if_fail (book_path);
@@ -425,6 +446,12 @@ book_manager_add_from_filepath (DhBookManager *book_manager,
priv->books = g_list_insert_sorted (priv->books,
book,
(GCompareFunc)dh_book_cmp_by_title);
+
+ /* Get notifications of book being REMOVED */
+ g_signal_connect (book,
+ "book-deleted",
+ G_CALLBACK (book_manager_book_deleted_cb),
+ book_manager);
}
GList *
diff --git a/src/dh-book.c b/src/dh-book.c
index 5c54366..4d44916 100644
--- a/src/dh-book.c
+++ b/src/dh-book.c
@@ -27,11 +27,19 @@
#include "dh-link.h"
#include "dh-parser.h"
#include "dh-book.h"
+#include "dh-marshal.h"
/* Timeout to wait for new events in the book so that
* they are merged and we don't spam unneeded signals */
#define EVENT_MERGE_TIMEOUT_SECS 2
+/* Signals managed by the DhBook */
+enum {
+ BOOK_UPDATED,
+ BOOK_DELETED,
+ BOOK_LAST_SIGNAL
+};
+
/* Structure defining basic contents to store about every book */
typedef struct {
/* File path of the book */
@@ -54,7 +62,6 @@ typedef struct {
gboolean is_updated;
/* ID of the event source */
guint monitor_event_timeout_id;
-
} DhBookPriv;
G_DEFINE_TYPE (DhBook, dh_book, G_TYPE_OBJECT);
@@ -69,10 +76,11 @@ static void book_monitor_event_cb (GFileMonitor *file_monitor,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data);
-
static void unref_node_link (GNode *node,
gpointer data);
+static guint signals[BOOK_LAST_SIGNAL] = { 0 };
+
static void
book_finalize (GObject *object)
{
@@ -113,6 +121,26 @@ dh_book_class_init (DhBookClass *klass)
object_class->finalize = book_finalize;
+ signals[BOOK_UPDATED] =
+ g_signal_new ("book-updated",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ _dh_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+
+ signals[BOOK_DELETED] =
+ g_signal_new ("book-deleted",
+ G_TYPE_FROM_CLASS (klass),
+ G_SIGNAL_RUN_LAST,
+ 0,
+ NULL, NULL,
+ _dh_marshal_VOID__VOID,
+ G_TYPE_NONE,
+ 0);
+
g_type_class_add_private (klass, sizeof (DhBookPriv));
}
@@ -209,11 +237,15 @@ book_monitor_event_timeout_cb (gpointer data)
/* We'll get either is_deleted OR is_updated,
* not possible to have both or none */
if (priv->is_deleted) {
- g_debug ("Book '%s' was deleted",
- dh_book_get_title (book));
+ g_debug ("Book '%s' was deleted", dh_book_get_title (book));
+ /* Emit the signal, but make sure we hold a reference
+ * while doing it */
+ g_object_ref (book);
+ g_signal_emit (book, signals[BOOK_DELETED], 0);
+ g_object_unref (book);
} else if (priv->is_updated) {
- g_debug ("Book '%s' was updated",
- dh_book_get_title (book));
+ g_debug ("Book '%s' was updated", dh_book_get_title (book));
+ g_signal_emit (book, signals[BOOK_UPDATED], 0);
} else {
g_warn_if_reached ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]