[gnote] Semi-functionnal option handling over DBus, blocked by Glibmm bugs.



commit adafecbc9fae382cad72ad237e617e3903030920
Author: Hubert Figuiere <hub figuiere net>
Date:   Sat Jul 18 20:24:24 2009 -0400

    Semi-functionnal option handling over DBus, blocked by Glibmm bugs.

 src/Makefile.am            |    1 +
 src/gnote.cpp              |  182 +++++++++++++++++++++++++++++++++++++++++++-
 src/gnote.hpp              |    7 ++
 src/sharp/streamreader.cpp |   79 +++++++++++++++++++
 src/sharp/streamreader.hpp |   63 +++++++++++++++
 5 files changed, 330 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 97d7023..016fd63 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -100,6 +100,7 @@ libgnote_a_SOURCES = \
 	sharp/modulefactory.hpp \
 	sharp/modulemanager.hpp sharp/modulemanager.cpp \
 	sharp/propertyeditor.hpp sharp/propertyeditor.cpp \
+	sharp/streamreader.hpp sharp/streamreader.cpp \
 	sharp/streamwriter.hpp sharp/streamwriter.cpp \
 	sharp/string.hpp sharp/string.cpp \
 	sharp/uri.hpp sharp/uri.cpp \
diff --git a/src/gnote.cpp b/src/gnote.cpp
index 363cf25..fde26d8 100644
--- a/src/gnote.cpp
+++ b/src/gnote.cpp
@@ -49,6 +49,8 @@
 #include "utils.hpp"
 #include "xkeybinder.hpp"
 #include "sharp/string.hpp"
+#include "sharp/streamreader.hpp"
+#include "sharp/files.hpp"
 
 #if HAVE_PANELAPPLETMM
 #include "applet.hpp"
@@ -90,12 +92,20 @@ namespace gnote {
     try {
       context.parse(argc, argv);
     }
-    catch(...)
+    catch(const Glib::Error & e)
     {
+      ERR_OUT("error parsing: %s", e.what().c_str());
     }
 
     m_is_panel_applet = cmd_line.use_panel_applet();
 
+#ifdef ENABLE_DBUS
+    if(cmd_line.needs_execute()) {
+      cmd_line.execute();
+      return 0;
+    }
+#endif
+
     m_icon_theme = Gtk::IconTheme::get_default();
     m_icon_theme->append_search_path(DATADIR"/icons");
     m_icon_theme->append_search_path(DATADIR"/gnote/icons");
@@ -119,9 +129,11 @@ namespace gnote {
       (*iter)->initialize();
     }
 
+#ifndef ENABLE_DBUS
     if(cmd_line.needs_execute()) {
       cmd_line.execute();
     }
+#endif
 
     if(m_is_panel_applet) {
       DBG_OUT("starting applet");
@@ -404,33 +416,171 @@ namespace gnote {
     , m_open_start_here(false)
     , m_use_panel(false)
     , m_show_version(false)
+    , m_open_search(false)
   {
     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);
+
+#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
   }
 
   int GnoteCommandLine::execute()
 
   {
     bool quit = false;
-#ifndef ENABLE_DBUS
+    DBG_OUT("running args");
+#ifdef ENABLE_DBUS
+    RemoteControlClient * remote = RemoteControlProxy::get_instance();
+    if(!remote) {
+      ERR_OUT("couldn't get remote client");
+      return 1;
+    }
+    if (m_new_note) {
+      std::string new_uri;
+
+      if (!m_new_note_name.empty()) {
+        new_uri = remote->FindNote (m_new_note_name);
+
+        if (new_uri.empty()) {
+          new_uri = remote->CreateNamedNote(m_new_note_name);
+        }
+      } 
+      else {
+        new_uri = remote->CreateNote ();
+      }
+
+      if (!new_uri.empty()) {
+        remote->DisplayNote (new_uri);
+      }
+    }
+
+    if (m_open_start_here) {
+      m_open_note_uri = remote->FindStartHereNote ();
+    }
+    if (!m_open_note_name.empty()) {
+      m_open_note_uri = remote->FindNote (m_open_note_name);
+    }
+    if (!m_open_note_uri.empty()) {
+      if (!m_highlight_search.empty()) {
+        remote->DisplayNoteWithSearch (m_open_note_uri,
+                                       m_highlight_search);
+      }
+      else {
+        remote->DisplayNote (m_open_note_uri);
+      }
+    }
+
+    if (!m_open_external_note_path.empty()) {
+      std::string note_id = sharp::file_basename(m_open_external_note_path);
+      if (!note_id.empty()) {
+        // Attempt to load the note, assuming it might already
+        // be part of our notes list.
+        if (remote->DisplayNote (
+              str(boost::format("note://gnote/%1%") % note_id)) == false) {
+
+          sharp::StreamReader sr;
+          sr.init(m_open_external_note_path);
+          if (sr.file()) {
+            std::string noteTitle;
+            std::string noteXml;
+            sr.read_to_end (noteXml);
+
+            // Make sure noteXml is parseable
+            xmlDocPtr doc = xmlParseDoc((const xmlChar*)noteXml.c_str());
+            if(doc) {
+              xmlFreeDoc(doc);
+            }
+            else {
+              noteXml = "";
+            }
+
+						if (!noteXml.empty()) {
+              noteTitle = NoteArchiver::obj().get_title_from_note_xml (noteXml);
+              if (!noteTitle.empty()) {
+                // Check for conflicting titles
+                std::string baseTitle = noteTitle;
+                for (int i = 1; !remote->FindNote (noteTitle).empty(); i++) {
+                  noteTitle = str(boost::format("%1% (%2%)") % baseTitle % i);
+                }
+
+                std::string note_uri = remote->CreateNamedNote (noteTitle);
+
+                // Update title in the note XML
+                noteXml = NoteArchiver::obj().get_renamed_note_xml (noteXml, baseTitle, noteTitle);
+
+                if (!note_uri.empty()) {
+                  // Load in the XML contents of the note file
+                  if (remote->SetNoteCompleteXml (note_uri, noteXml)) {
+                    remote->DisplayNote (note_uri);
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+
+    if (m_open_search) {
+      if (!m_search.empty()) {
+        remote->DisplaySearchWithText(m_search);
+      }
+      else {
+        remote->DisplaySearch();
+      }
+    }
+#else
       // as long as we don't have the DBus support.
     if(!m_search.empty()) {
       NoteRecentChanges * recent_changes
@@ -453,6 +603,32 @@ 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()
   {
     Glib::ustring version = str(boost::format(_("Version %1%"))
@@ -463,7 +639,9 @@ 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() ||
diff --git a/src/gnote.hpp b/src/gnote.hpp
index 209a7f0..4205a3e 100644
--- a/src/gnote.hpp
+++ b/src/gnote.hpp
@@ -117,6 +117,8 @@ public:
       return m_use_panel;
     }
 
+  virtual bool on_post_parse(Glib::OptionContext& context, OptionGroup&	group);
+
 private:
   void        print_version();
 
@@ -124,11 +126,16 @@ private:
   bool        m_open_start_here;
   bool        m_use_panel;
   bool        m_show_version;
+  bool        m_open_search;
+  Glib::ustring m_new_note_name;
+  Glib::ustring m_open_note;
+  // 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/sharp/streamreader.cpp b/src/sharp/streamreader.cpp
new file mode 100644
index 0000000..86d2077
--- /dev/null
+++ b/src/sharp/streamreader.cpp
@@ -0,0 +1,79 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+
+
+
+
+#include "sharp/streamreader.hpp"
+
+#include "debug.hpp"
+
+namespace sharp {
+
+#define READ_BUFFER_COUNT 1024
+
+StreamReader::StreamReader()
+  : m_file(NULL)
+{
+}
+
+StreamReader::~StreamReader()
+{
+  if(m_file) {
+    fclose(m_file);
+  }
+}
+
+void StreamReader::init(const std::string & filename)
+{
+  m_file = fopen(filename.c_str(), "rb");
+}
+
+
+void StreamReader::read_to_end(std::string & text)
+{
+  DBG_ASSERT(m_file, "file is NULL");
+
+  text.clear();
+
+  char buffer[READ_BUFFER_COUNT + 1]; // +1 for the NUL terminate.
+  size_t byte_read = 0;
+  do {
+    byte_read = fread(buffer, 1, READ_BUFFER_COUNT, m_file);
+    buffer[byte_read+1] = 0; // NUL terminate.
+    text += buffer;
+  }
+  while(byte_read == READ_BUFFER_COUNT);
+}
+
+
+void StreamReader::close()
+{
+  fclose(m_file);
+  m_file = NULL;
+}
+
+
+}
diff --git a/src/sharp/streamreader.hpp b/src/sharp/streamreader.hpp
new file mode 100644
index 0000000..eee1b97
--- /dev/null
+++ b/src/sharp/streamreader.hpp
@@ -0,0 +1,63 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2009 Hubert Figuiere
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+
+
+#ifndef __SHARP_STREAMREADER_HPP_
+#define __SHARP_STREAMREADER_HPP_
+
+#include <stdio.h>
+
+#include <string>
+
+#include <libxml/tree.h>
+
+namespace sharp {
+
+
+class StreamReader
+//  : public TextReader
+{
+public:
+  StreamReader();
+  ~StreamReader();
+
+  void init(const std::string &);
+
+  FILE * file()
+    {
+      return m_file;
+    }
+
+  void read_to_end(std::string &);
+
+  void close();
+private:
+  FILE * m_file;
+};
+
+}
+
+
+#endif



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