[gnote] Functionnal command line parsing.



commit 0db33d6255aef2f6eeda5c53ec8f81e1d72a205b
Author: Hubert Figuiere <hub figuiere net>
Date:   Sat Jul 18 23:02:19 2009 -0400

    Functionnal command line parsing.
    
    Support --new-note. (Closes #585774)
    Support --open-note with a URL. (Close #585775)

 src/dbus/remotecontrol.cpp |    3 +-
 src/gnote.cpp              |  203 +++++++++++++++++++++-----------------------
 src/gnote.hpp              |   33 +++++---
 src/remotecontrolproxy.cpp |   16 ++--
 4 files changed, 124 insertions(+), 131 deletions(-)
---
diff --git a/src/dbus/remotecontrol.cpp b/src/dbus/remotecontrol.cpp
index 3b18843..80f0daa 100644
--- a/src/dbus/remotecontrol.cpp
+++ b/src/dbus/remotecontrol.cpp
@@ -75,7 +75,8 @@ namespace gnote {
       note = m_manager.create (linked_title);
       return note->uri();
     } 
-    catch (...) {
+    catch (const std::exception & e) {
+      ERR_OUT("create throw: %s", e.what());
     }
     return "";
   }
diff --git a/src/gnote.cpp b/src/gnote.cpp
index fde26d8..b12803f 100644
--- a/src/gnote.cpp
+++ b/src/gnote.cpp
@@ -85,17 +85,7 @@ namespace gnote {
   {
     GnoteCommandLine cmd_line;
 
-
-    Glib::OptionContext context;
-    context.set_ignore_unknown_options(true);
-    context.set_main_group(cmd_line);
-    try {
-      context.parse(argc, argv);
-    }
-    catch(const Glib::Error & e)
-    {
-      ERR_OUT("error parsing: %s", e.what().c_str());
-    }
+    cmd_line.parse(argc, argv);
 
     m_is_panel_applet = cmd_line.use_panel_applet();
 
@@ -410,83 +400,111 @@ namespace gnote {
   }
 
 
+
   GnoteCommandLine::GnoteCommandLine()
-    : Glib::OptionGroup("Gnote", _("A note taking application"))
-    , m_new_note(false)
-    , m_open_start_here(false)
+    : m_context(g_option_context_new("Foobar"))
     , m_use_panel(false)
+    , m_note_path(NULL)
+    , m_do_search(false)
     , m_show_version(false)
-    , m_open_search(false)
+    , m_do_new_note(false)
+    , m_open_note(NULL)
+    , m_open_start_here(false)
+    , m_highlight_search(NULL)
   {
-    Glib::OptionEntry entry;
-    entry.set_long_name("panel-applet");
-    entry.set_flags(Glib::OptionEntry::FLAG_HIDDEN);
-    entry.set_description(_("Run Gnote as a GNOME panel applet."));
-    add_entry(entry, m_use_panel);
-
-    Glib::OptionEntry entry2;
-    entry2.set_long_name("note-path");
-    entry2.set_description(_("Specify the path of the directory containing the notes."));
-    // name of the command line argument
-    entry2.set_arg_description(_("path"));
-    add_entry(entry2, m_note_path);
-
-    Glib::OptionEntry entry3;
-    entry3.set_long_name("search");
-    entry3.set_flags(Glib::OptionEntry::FLAG_OPTIONAL_ARG);
-    entry3.set_description(_("Open the search all notes window with the search text."));
-    // name of the command line argument
-    entry3.set_arg_description(_("text"));
-    add_entry(entry3, m_search);
-
-    Glib::OptionEntry entry4;
-    entry4.set_long_name("version");
-    entry4.set_description(_("Print version information."));
-    add_entry(entry4, m_show_version);
-
+    static const GOptionEntry entries[] =
+      {
+        { "panel-applet", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &m_use_panel, _("Run Gnote as a GNOME panel applet."), NULL },
+        { "note-path", 0, 0, G_OPTION_ARG_STRING, &m_note_path, _("Specify the path of the directory containing the notes."), _("path") },
+        { "search", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, (void*)GnoteCommandLine::parse_func, _("Open the search all notes window with the search text."), _("text") },
+        { "version", 0, 0, G_OPTION_ARG_NONE, &m_show_version, _("Print version information."), NULL },
 #ifdef ENABLE_DBUS
-    Glib::OptionEntry entry5;
-    entry5.set_long_name("new-note");
-    entry5.set_flags(Glib::OptionEntry::FLAG_OPTIONAL_ARG);
-    entry5.set_description(_("Create and display a new note, with a optional title."));
-    // name of the command line argument
-    entry5.set_arg_description(_("title"));
-    DBG_OUT("flags are %d", entry5.gobj()->flags);
-    add_entry(entry5, m_new_note_name);
-
-    Glib::OptionEntry entry6;
-    entry6.set_long_name("open-note");
-    entry6.set_description(_("Display the existing note matching title."));
-    // name of the command line argument
-    entry6.set_arg_description(_("title/url"));
-    add_entry(entry6, m_open_note);
-
-    Glib::OptionEntry entry7;
-    entry7.set_long_name("start-here");
-    entry7.set_description(_("Display the 'Start Here' note."));
-    add_entry(entry7, m_open_start_here);
-
-    Glib::OptionEntry entry8;
-    entry8.set_long_name("highlight-search");
-    entry8.set_description(_("Search and highlight text in the opened note."));
-    // name of the command line argument
-    entry8.set_arg_description(_("text"));
-    add_entry(entry8, m_highlight_search);
-#endif
+        { "new-note", 0, G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK, (void*)GnoteCommandLine::parse_func, _("Create and display a new note, with a optional title."), _("title") },
+        { "open-note", 0, 0, G_OPTION_ARG_STRING, &m_open_note, _("Display the existing note matching title."), _("title/url") },
+        { "start-here", 0, 0, G_OPTION_ARG_NONE, &m_open_start_here, _("Display the 'Start Here' note."), _("text") },
+        { "highlight-search", 0, 0, G_OPTION_ARG_STRING, &m_highlight_search, _("Search and highlight text in the opened note."), _("text") },
+#endif        
+        { NULL, 0, 0, (GOptionArg)0, NULL, NULL, NULL }
+      };
+
+    GOptionGroup *group = g_option_group_new("Gnote", _("A note taking application"), _("Gnote options at launch"), this, NULL);
+    g_option_group_add_entries(group, entries);
+    g_option_context_set_main_group (m_context, group);
+  }
+
+  
+  GnoteCommandLine::~GnoteCommandLine()
+  {
+    g_option_context_free(m_context);
+  }
+
+  gboolean GnoteCommandLine::parse_func(const gchar *option_name,
+                                        const gchar *value,
+                                        gpointer data,
+                                        GError ** /*error*/)
+  {
+    GnoteCommandLine * self = (GnoteCommandLine*)data;
+  
+    if(g_str_equal (option_name, "--search")) {
+      self->m_do_search = true;
+      if(value) {
+        self->m_search = value;
+      }
+    }
+    else if(g_str_equal (option_name, "--new-note")) {
+      self->m_do_new_note = true;
+      if(value) {
+        self->m_new_note_name = value;
+      }
+    }
+    return TRUE;
   }
 
+
+  void GnoteCommandLine::parse(int &argc, gchar ** & argv)
+  {
+    GError *error = NULL;
+
+    if(!g_option_context_parse (m_context, &argc, &argv, &error)) {
+      g_print ("option parsing failed: %s\n", error->message);
+      exit (1);
+    }
+
+    if(m_open_note && *m_open_note) {
+      if (sharp::string_starts_with(m_open_note, "note://gnote/")) {
+        m_open_note_uri = m_open_note;
+      }
+      else if (sharp::file_exists(m_open_note)) {
+        // This is potentially a note file
+        m_open_external_note_path = m_open_note;
+      } 
+      else {
+        m_open_note_name = m_open_note;
+      }
+    }
+    
+  }
+
+
   int GnoteCommandLine::execute()
 
   {
     bool quit = false;
     DBG_OUT("running args");
+
+    if(m_show_version) {
+      print_version();
+      quit = true;
+      exit(0);
+    }
+
 #ifdef ENABLE_DBUS
     RemoteControlClient * remote = RemoteControlProxy::get_instance();
     if(!remote) {
       ERR_OUT("couldn't get remote client");
       return 1;
     }
-    if (m_new_note) {
+    if (m_do_new_note) {
       std::string new_uri;
 
       if (!m_new_note_name.empty()) {
@@ -512,7 +530,7 @@ namespace gnote {
       m_open_note_uri = remote->FindNote (m_open_note_name);
     }
     if (!m_open_note_uri.empty()) {
-      if (!m_highlight_search.empty()) {
+      if (m_highlight_search) {
         remote->DisplayNoteWithSearch (m_open_note_uri,
                                        m_highlight_search);
       }
@@ -572,7 +590,7 @@ namespace gnote {
       }
     }
 
-    if (m_open_search) {
+    if (m_do_search) {
       if (!m_search.empty()) {
         remote->DisplaySearchWithText(m_search);
       }
@@ -582,7 +600,7 @@ namespace gnote {
     }
 #else
       // as long as we don't have the DBus support.
-    if(!m_search.empty()) {
+    if(m_do_search) {
       NoteRecentChanges * recent_changes
         = NoteRecentChanges::get_instance(
           Gnote::obj().default_note_manager());
@@ -592,10 +610,6 @@ namespace gnote {
       recent_changes->present ();
     }
 #endif
-    if(m_show_version) {
-      print_version();
-      quit = true;
-    }
 
     if(quit) {
       exit(0);
@@ -603,31 +617,6 @@ namespace gnote {
     return 0;
   }
 
-  bool GnoteCommandLine::on_post_parse(Glib::OptionContext& context,
-                                       Glib::OptionGroup&	group)
-  {
-    DBG_OUT("post parse");
-    if(!OptionGroup::on_post_parse(context, group)) {
-      return false;
-    }
-
-    if(!m_open_note.empty()) {
-      if (sharp::string_starts_with(m_open_note, "note://gnote/")) {
-        DBG_OUT("is URI");
-        m_open_note_uri = m_open_note;
-      }
-      else if (sharp::file_exists(m_open_note)) {
-        // This is potentially a note file
-        DBG_OUT("is file");
-        m_open_external_note_path = m_open_note;
-      } 
-      else {
-        m_open_note_name = m_open_note;
-      }
-    }
-    
-    return true;
-  }
 
   void GnoteCommandLine::print_version()
   {
@@ -640,13 +629,11 @@ namespace gnote {
   bool GnoteCommandLine::needs_execute() const
   {
     DBG_OUT("needs execute?");
-    return m_new_note ||
-//      !m_open_note.empty() ||
-      !m_open_note_name.empty() ||
-      !m_open_note_uri.empty() ||
-      !m_search.empty() ||
+    return m_do_new_note ||
+      m_open_note ||
+      m_do_search ||
       m_open_start_here ||
-      !m_open_external_note_path.empty() ||
+      m_highlight_search ||
       m_show_version;
   }
 
diff --git a/src/gnote.hpp b/src/gnote.hpp
index 4205a3e..5edc902 100644
--- a/src/gnote.hpp
+++ b/src/gnote.hpp
@@ -101,41 +101,48 @@ private:
 
 
 class GnoteCommandLine
-  : public Glib::OptionGroup
 {
 public:
   GnoteCommandLine();
+  ~GnoteCommandLine();
   int execute();
 
-  const Glib::ustring & note_path() const
+  const gchar * note_path() const
     {
-      return m_note_path;
+      return m_note_path ? m_note_path : "";
     }
   bool        needs_execute() const;
   bool        use_panel_applet() const
     {
       return m_use_panel;
     }
+  void parse(int &argc, gchar ** & argv);
 
-  virtual bool on_post_parse(Glib::OptionContext& context, OptionGroup&	group);
-
+  static gboolean parse_func(const gchar *option_name,
+                             const gchar *value,
+                             gpointer data,
+                             GError **error);
 private:
   void        print_version();
 
-  bool        m_new_note;
-  bool        m_open_start_here;
+  GOptionContext *m_context;
+
   bool        m_use_panel;
+  gchar *     m_note_path;
+  bool        m_do_search;
+  std::string m_search;
   bool        m_show_version;
-  bool        m_open_search;
-  Glib::ustring m_new_note_name;
-  Glib::ustring m_open_note;
+  bool        m_do_new_note;
+  std::string m_new_note_name;
+  gchar*      m_open_note;
+  bool        m_open_start_here;
+  gchar*      m_highlight_search;
+
+
   // depend on m_open_note, set in on_post_parse
   std::string m_open_note_name;
   std::string m_open_note_uri;
   std::string m_open_external_note_path;
-  Glib::ustring m_search;
-  Glib::ustring m_note_path;
-  Glib::ustring m_highlight_search;
 };
 
 }
diff --git a/src/remotecontrolproxy.cpp b/src/remotecontrolproxy.cpp
index 1eb7bcc..840de21 100644
--- a/src/remotecontrolproxy.cpp
+++ b/src/remotecontrolproxy.cpp
@@ -30,20 +30,18 @@ namespace gnote {
 const char *RemoteControlProxy::GNOTE_SERVER_NAME = "org.gnome.Gnote";
 const char *RemoteControlProxy::GNOTE_SERVER_PATH = "/org/gnome/Gnote/RemoteControl";
 
-DBus::Glib::BusDispatcher dispatcher;
+DBus::BusDispatcher dispatcher;
+
+DBus::Glib::BusDispatcher glib_dispatcher;
 
 RemoteControlClient *RemoteControlProxy::get_instance()
 {
+  // we likely won't have a Glib main loop at the point.
   if(!DBus::default_dispatcher) {
     DBus::default_dispatcher = &dispatcher;
-    dispatcher.attach(NULL);
   }
   DBus::Connection conn = DBus::Connection::SessionBus();
-	if(conn.has_name(GNOTE_SERVER_NAME)) {
-    return new RemoteControlClient(conn, GNOTE_SERVER_PATH, GNOTE_SERVER_NAME);
-  }
-
-  return NULL;
+  return new RemoteControlClient(conn, GNOTE_SERVER_PATH, GNOTE_SERVER_NAME);
 }
 
 
@@ -51,8 +49,8 @@ RemoteControl *RemoteControlProxy::register_remote(NoteManager & manager)
 {
   RemoteControl *remote_control = NULL;
   if(!DBus::default_dispatcher) {
-    DBus::default_dispatcher = &dispatcher;
-    dispatcher.attach(NULL);
+    DBus::default_dispatcher = &glib_dispatcher;
+    glib_dispatcher.attach(NULL);
   }
 
 	DBus::Connection conn = DBus::Connection::SessionBus();



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