[bijiben] Add --new-note



commit 2c038ef7c74ac66f2319f49ed8486eaa3655a1b4
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Sun Jun 22 00:27:05 2014 +0200

    Add --new-note
    
    Add a new commandline option, to create a new note at startup.
    See https://bugzilla.gnome.org/show_bug.cgi?id=724000

 src/bjb-bijiben.c    |   67 ++++++++++++++++++++++++++++++++-----------------
 src/bjb-controller.c |   17 +++++++++++-
 2 files changed, 59 insertions(+), 25 deletions(-)
---
diff --git a/src/bjb-bijiben.c b/src/bjb-bijiben.c
index 8209bc2..b562264 100644
--- a/src/bjb-bijiben.c
+++ b/src/bjb-bijiben.c
@@ -38,15 +38,15 @@ struct _BijibenPriv
   BijiManager *manager;
   BjbSettings *settings;
 
-
-
   /* Controls. to_open is for startup */
 
   gboolean     first_run;
   gboolean     is_loaded;
-  GQueue       *to_open;
+  gboolean     new_note;
+  GQueue       *files_to_open; // paths
 };
 
+
 G_DEFINE_TYPE (Bijiben, bijiben, GTK_TYPE_APPLICATION);
 
 static void
@@ -55,6 +55,7 @@ bijiben_new_window_internal (Bijiben *app,
                              BijiItem *item,
                              GError *error);
 
+
 static void
 on_window_activated_cb   (BjbWindowBase *window,
                           gboolean win_is_available,
@@ -70,14 +71,12 @@ on_window_activated_cb   (BjbWindowBase *window,
   priv->is_loaded = TRUE;
   notfound = NULL;
 
-  while ((path = g_queue_pop_head (priv->to_open)))
+  while ((path = g_queue_pop_head (priv->files_to_open)))
   {
-
     item = biji_manager_get_item_at_path (priv->manager, path);
 
     if (item != NULL)
     {
-
       /* If that's a note, detach it */
       if (BIJI_IS_NOTE_OBJ (item))
       {
@@ -93,17 +92,13 @@ on_window_activated_cb   (BjbWindowBase *window,
         else
            bijiben_new_window_internal (self, NULL, item, NULL);
       }
-
       g_free (path);
     }
 
-
     else
     {
       notfound = g_list_prepend (notfound, path);
     }
-
-
   }
 
   /* We just wait for next provider to be loaded.
@@ -111,17 +106,32 @@ on_window_activated_cb   (BjbWindowBase *window,
    * in order to trigger file reading. */
   for (l = notfound; l != NULL; l=l->next)
   {
-    g_queue_push_head (priv->to_open, l->data);
+    g_queue_push_head (priv->files_to_open, l->data);
   }
 
+
+  /* All requested notes are loaded, but we have a new one to create...
+   * This implementation is not really safe,
+   * we might have loaded SOME provider(s)
+   * but not the default one - more work is needed here */
+  if (notfound == NULL &&
+      priv->new_note == TRUE)
+  {
+    priv->new_note = FALSE;
+    item = BIJI_ITEM (biji_manager_note_new (
+                        priv->manager,
+                        NULL,
+                        bjb_settings_get_default_location (self->priv->settings)));
+    bijiben_new_window_internal (self, NULL, item, NULL);
+  }
 }
 
 
 static void
-bijiben_new_window_internal (Bijiben *self,
-                             GFile *file,
-                             BijiItem *item,
-                             GError *error)
+bijiben_new_window_internal (Bijiben     *self,
+                             GFile       *file,
+                             BijiItem    *item,
+                             GError      *error)
 {
   BjbWindowBase *window;
   BijiNoteObj   *note;
@@ -200,14 +210,13 @@ bijiben_open (GApplication  *application,
 
   self = BIJIBEN_APPLICATION (application);
 
-
   for (i = 0; i < n_files; i++)
   {
     if (self->priv->is_loaded == TRUE)
-      bijiben_new_window_internal (BIJIBEN_APPLICATION (application), files[i], NULL, NULL);
+      bijiben_new_window_internal (self, files[i], NULL, NULL);
 
     else
-      g_queue_push_head (self->priv->to_open, g_file_get_parse_name (files[i]));
+      g_queue_push_head (self->priv->files_to_open, g_file_get_parse_name (files[i]));
   }
 }
 
@@ -221,7 +230,8 @@ bijiben_init (Bijiben *self)
     G_TYPE_INSTANCE_GET_PRIVATE (self, BIJIBEN_TYPE_APPLICATION, BijibenPriv);
 
   priv->settings = bjb_settings_new ();
-  priv->to_open = g_queue_new ();
+  priv->files_to_open = g_queue_new ();
+  priv->new_note = FALSE;
   priv->is_loaded = FALSE;
 }
 
@@ -438,6 +448,8 @@ bijiben_application_local_command_line (GApplication *application,
   const GOptionEntry options[] = {
     { "version", 0, 0, G_OPTION_ARG_NONE, &version,
       N_("Show the application's version"), NULL},
+    { "new-note", 0, 0, G_OPTION_ARG_NONE, &BIJIBEN_APPLICATION(application)->priv->new_note,
+      N_("Create a new note"), NULL},
     { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &remaining,
       NULL,  N_("[FILE...]") },
     { NULL }
@@ -453,7 +465,8 @@ bijiben_application_local_command_line (GApplication *application,
   argv = *arguments;
   argc = g_strv_length (argv);
 
-  if (!g_option_context_parse (context, &argc, &argv, &error)) {
+  if (!g_option_context_parse (context, &argc, &argv, &error))
+  {
     /* Translators: this is a fatal error quit message
      * printed on the command line */
     g_printerr ("%s: %s\n", _("Could not parse arguments"), error->message);
@@ -463,14 +476,16 @@ bijiben_application_local_command_line (GApplication *application,
     goto out;
   }
 
-  if (version) {
+  if (version)
+  {
     g_print ("%s %s\n", _("GNOME Notes"), VERSION);
     goto out;
   }
 
   g_application_register (application, NULL, &error);
 
-  if (error != NULL) {
+  if (error != NULL)
+  {
     /* Translators: this is a fatal error quit message
      * printed on the command line */
     g_printerr ("%s: %s\n",
@@ -482,6 +497,12 @@ bijiben_application_local_command_line (GApplication *application,
     goto out;
   }
 
+  if (BIJIBEN_APPLICATION (application)->priv->new_note)
+  {
+    g_application_open (application, NULL, 0, "");
+    goto out;
+  }
+
   len = 0;
   files = NULL;
 
@@ -538,7 +559,7 @@ bijiben_finalize (GObject *object)
 
   g_clear_object (&self->priv->manager);
   g_clear_object (&self->priv->settings);
-  g_queue_free (self->priv->to_open);
+  g_queue_free (self->priv->files_to_open);
 
   G_OBJECT_CLASS (bijiben_parent_class)->finalize (object);
 }
diff --git a/src/bjb-controller.c b/src/bjb-controller.c
index ed05314..ac818d1 100644
--- a/src/bjb-controller.c
+++ b/src/bjb-controller.c
@@ -579,6 +579,15 @@ on_needle_changed (BjbController *self)
 }
 
 
+/* Return FALSE to end timeout, see below on_manager_changed */
+static gboolean
+bjb_controller_set_window_active (BjbController *self)
+{
+  bjb_window_base_set_active (self->priv->window, TRUE);
+  return FALSE;
+}
+
+
 /* Depending on the change at data level,
  * the view has to be totaly refreshed or just amended */
 static void
@@ -595,7 +604,6 @@ on_manager_changed (BijiManager            *manager,
 
 
 
-
   if (group != self->priv->group)
   {
     g_debug ("Controller received signal for group %i while %i",
@@ -654,10 +662,15 @@ on_manager_changed (BijiManager            *manager,
 
       break;
 
+    /* Apply the needle to display the relevant items.
+     * The window will ping to tell it's now active
+     * Use another thread for this, because controller is now up to date,
+     * and we need to unlock mutex,
+     * since activating window can call this function! */
     default:
       bjb_controller_apply_needle (self);
       if (flag == BIJI_MANAGER_MASS_CHANGE)
-        bjb_window_base_set_active (self->priv->window, TRUE);
+        g_timeout_add (1, (GSourceFunc) bjb_controller_set_window_active, self);
   }
 
   g_mutex_unlock (&priv->mutex);


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