[devhelp: 4/36] book-manager: manage created books
- From: Aleksander Morgado <aleksm src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [devhelp: 4/36] book-manager: manage created books
- Date: Mon, 20 Dec 2010 14:31:45 +0000 (UTC)
commit 02020313babc18bd7c5ca52af6a1d5b62fe97943
Author: Aleksander Morgado <aleksander lanedo com>
Date: Thu Dec 2 16:34:05 2010 +0100
book-manager: manage created books
src/dh-book-manager.c | 94 +++++++++++++++++++++++++++++++++++++++++++++----
src/dh-book.c | 6 +--
2 files changed, 89 insertions(+), 11 deletions(-)
---
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index cc375da..c12aae9 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -216,14 +216,66 @@ book_manager_booklist_monitor_event_cb (GFileMonitor *file_monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
- gpointer user_data)
+ gpointer user_data)
{
- gchar *file_uri;
+ DhBookManager *book_manager = user_data;
+ GError *error = NULL;
+ GFileInfo *file_info;
+ gchar *file_path;
+ gchar *file_basename;
+ gchar *book_path;
+
+ /* In the book manager we only handle events for new directories
+ * created. Books removed or updated are handled by the book objects
+ * themselves */
+ if (event_type != G_FILE_MONITOR_EVENT_CREATED) {
+ return;
+ }
+
+ /* Get file info */
+ file_info = g_file_query_info (file,
+ G_FILE_ATTRIBUTE_STANDARD_TYPE,
+ G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
+ NULL,
+ &error);
+ if (!file_info) {
+ gchar *file_uri;
+
+ file_uri = g_file_get_uri (file);
+ g_warning ("Couldn't query file type of '%s': %s",
+ file_uri,
+ error ? error->message : "unknown error");
+ g_free (file_uri);
+ g_clear_error (&error);
+ return;
+ }
- file_uri = g_file_get_uri (file);
+ /* The created file must be a directory! */
+ if (g_file_info_get_file_type (file_info) != G_FILE_TYPE_DIRECTORY) {
+ g_object_unref (file_info);
+ return;
+ }
+ g_object_unref (file_info);
+
+ file_path = g_file_get_path (file);
+ file_basename = g_file_get_basename (file);
+
+ /* Compute book path */
+ book_path = book_manager_get_book_path (file_path, file_basename);
+ if (book_path) {
+ /* Add book from filepath */
+ book_manager_add_from_filepath (book_manager,
+ book_path);
+ g_free (book_path);
+
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[DISABLED_BOOK_LIST_UPDATED],
+ 0);
+ }
- g_debug ("CHANGED BOOKLIST DIR '%s'", file_uri);
- g_free (file_uri);
+ g_free (file_path);
+ g_free (file_basename);
}
static void
@@ -404,13 +456,34 @@ book_manager_book_deleted_cb (DhBook *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);
+
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[DISABLED_BOOK_LIST_UPDATED],
+ 0);
}
}
static void
+book_manager_book_updated_cb (DhBook *book,
+ gpointer user_data)
+{
+ DhBookManager *book_manager = user_data;
+
+ g_debug ("Updating book '%s' in the book manager list",
+ dh_book_get_title (book));
+
+ /* Emit signal to notify others */
+ g_signal_emit (book_manager,
+ signals[DISABLED_BOOK_LIST_UPDATED],
+ 0);
+}
+
+static void
book_manager_add_from_filepath (DhBookManager *book_manager,
const gchar *book_path)
{
@@ -447,11 +520,18 @@ book_manager_add_from_filepath (DhBookManager *book_manager,
book,
(GCompareFunc)dh_book_cmp_by_title);
- /* Get notifications of book being REMOVED */
+ g_debug ("Adding book '%s' to the book manager list",
+ dh_book_get_title (book));
+
+ /* Get notifications of book being deleted or updated */
g_signal_connect (book,
- "book-deleted",
+ "deleted",
G_CALLBACK (book_manager_book_deleted_cb),
book_manager);
+ g_signal_connect (book,
+ "updated",
+ G_CALLBACK (book_manager_book_updated_cb),
+ book_manager);
}
GList *
diff --git a/src/dh-book.c b/src/dh-book.c
index 4d44916..4f4c33e 100644
--- a/src/dh-book.c
+++ b/src/dh-book.c
@@ -122,7 +122,7 @@ dh_book_class_init (DhBookClass *klass)
object_class->finalize = book_finalize;
signals[BOOK_UPDATED] =
- g_signal_new ("book-updated",
+ g_signal_new ("updated",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
@@ -132,7 +132,7 @@ dh_book_class_init (DhBookClass *klass)
0);
signals[BOOK_DELETED] =
- g_signal_new ("book-deleted",
+ g_signal_new ("deleted",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
0,
@@ -237,14 +237,12 @@ 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));
/* 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_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]