[gnote] Add statistics add-in



commit 467a893cce0c6d1bbc84f6239c60be6967a273d6
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Sun Apr 7 15:18:00 2013 +0300

    Add statistics add-in
    
    An addin to show various statistics about Gnote.

 configure.ac                                       |    1 +
 po/POTFILES.in                                     |    2 +
 src/addins/Makefile.am                             |    1 +
 src/addins/statistics/Makefile.am                  |   12 ++
 .../statistics/statisticsapplicationaddin.cpp      |  110 +++++++++++
 .../statistics/statisticsapplicationaddin.hpp      |   69 +++++++
 src/addins/statistics/statisticswidget.cpp         |  203 ++++++++++++++++++++
 src/addins/statistics/statisticswidget.hpp         |   49 +++++
 8 files changed, 447 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index c31a5f0..6c47960 100644
--- a/configure.ac
+++ b/configure.ac
@@ -167,6 +167,7 @@ src/addins/notedirectorywatcher/Makefile
 src/addins/noteoftheday/Makefile
 src/addins/printnotes/Makefile
 src/addins/replacetitle/Makefile
+src/addins/statistics/Makefile
 src/addins/stickynoteimport/Makefile
 src/addins/tomboyimport/Makefile
 src/addins/underline/Makefile
diff --git a/po/POTFILES.in b/po/POTFILES.in
index ae388fb..281628f 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -17,6 +17,8 @@ src/addins/noteoftheday/noteoftheday.cpp
 src/addins/noteoftheday/noteofthedaypreferences.cpp
 src/addins/printnotes/printnotesnoteaddin.cpp
 src/addins/replacetitle/replacetitlenoteaddin.cpp
+src/addins/statistics/statisticsapplicationaddin.cpp
+src/addins/statistics/statisticswidget.cpp
 src/addins/stickynoteimport/stickynoteimportnoteaddin.cpp
 src/addins/tomboyimport/tomboyimportaddin.cpp
 src/addins/underline/underlinemenuitem.cpp
diff --git a/src/addins/Makefile.am b/src/addins/Makefile.am
index 4dc3f08..6d211d3 100644
--- a/src/addins/Makefile.am
+++ b/src/addins/Makefile.am
@@ -11,6 +11,7 @@ SUBDIRS = backlinks \
        noteoftheday \
        printnotes \
        replacetitle \
+       statistics \
        stickynoteimport \
        tomboyimport \
        underline \
diff --git a/src/addins/statistics/Makefile.am b/src/addins/statistics/Makefile.am
new file mode 100644
index 0000000..62377bf
--- /dev/null
+++ b/src/addins/statistics/Makefile.am
@@ -0,0 +1,12 @@
+
+include $(builddir)/../addins.mk
+
+
+addinsdir = $(ADDINSDIR)
+addins_LTLIBRARIES = statistics.la
+
+statistics_la_SOURCES = \
+       statisticsapplicationaddin.hpp statisticsapplicationaddin.cpp \
+       statisticswidget.hpp statisticswidget.cpp \
+       $(NULL)
+
diff --git a/src/addins/statistics/statisticsapplicationaddin.cpp 
b/src/addins/statistics/statisticsapplicationaddin.cpp
new file mode 100644
index 0000000..229993c
--- /dev/null
+++ b/src/addins/statistics/statisticsapplicationaddin.cpp
@@ -0,0 +1,110 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2013 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <glibmm/i18n.h>
+
+#include "iactionmanager.hpp"
+#include "ignote.hpp"
+#include "statisticsapplicationaddin.hpp"
+
+
+namespace statistics {
+
+StatisticsModule::StatisticsModule()
+{
+  ADD_INTERFACE_IMPL(StatisticsApplicationAddin);
+}
+
+const char * StatisticsModule::id() const
+{
+  return "Statistics";
+}
+
+const char * StatisticsModule::name() const
+{
+  return _("Statistics");
+}
+
+const char * StatisticsModule::description() const
+{
+  return _("Show various statistics about notes");
+}
+
+const char * StatisticsModule::authors() const
+{
+  return _("Aurimas Černius");
+}
+
+int StatisticsModule::category() const
+{
+  return gnote::ADDIN_CATEGORY_TOOLS;
+}
+
+const char * StatisticsModule::version() const
+{
+  return "0.1";
+}
+
+
+StatisticsApplicationAddin::StatisticsApplicationAddin()
+  : m_initialized(false)
+  , m_widget(NULL)
+{
+}
+
+void StatisticsApplicationAddin::initialize()
+{
+  if(!m_initialized) {
+    m_initialized = true;
+    if(m_action == 0) {
+      m_action = Gtk::Action::create();
+      m_action->set_name("ShowStatistics");
+      m_action->set_label(_("Show Statistics"));
+      m_action->signal_activate()
+        .connect(sigc::mem_fun(*this, &StatisticsApplicationAddin::on_show_statistics));
+      gnote::IActionManager::obj().add_main_window_search_action(m_action, 100);
+    }
+  }
+}
+
+void StatisticsApplicationAddin::shutdown()
+{
+  m_initialized = false;
+}
+
+bool StatisticsApplicationAddin::initialized()
+{
+  return m_initialized;
+}
+
+void StatisticsApplicationAddin::on_show_statistics()
+{
+  if(!m_widget) {
+    m_widget = new StatisticsWidget(note_manager());
+  }
+  gnote::MainWindow &main_window = gnote::IGnote::obj().get_main_window();
+  if(m_widget->host()) {
+    m_widget->host()->unembed_widget(*m_widget);
+  }
+  main_window.embed_widget(*m_widget);
+}
+
+}
+
diff --git a/src/addins/statistics/statisticsapplicationaddin.hpp 
b/src/addins/statistics/statisticsapplicationaddin.hpp
new file mode 100644
index 0000000..8ba17d5
--- /dev/null
+++ b/src/addins/statistics/statisticsapplicationaddin.hpp
@@ -0,0 +1,69 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2013 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _STATISTICS_APPLICATION_ADDIN_
+#define _STATISTICS_APPLICATION_ADDIN_
+
+#include <gtkmm/action.h>
+
+#include "applicationaddin.hpp"
+#include "statisticswidget.hpp"
+#include "sharp/dynamicmodule.hpp"
+
+namespace statistics {
+
+class StatisticsModule
+  : public sharp::DynamicModule
+{
+public:
+  StatisticsModule();
+  virtual const char * id() const;
+  virtual const char * name() const;
+  virtual const char * description() const;
+  virtual const char * authors() const;
+  virtual int          category() const;
+  virtual const char * version() const;
+};
+
+DECLARE_MODULE(StatisticsModule);
+
+class StatisticsApplicationAddin
+  : public gnote::ApplicationAddin
+{
+public:
+  static StatisticsApplicationAddin *create()
+    {
+      return new StatisticsApplicationAddin;
+    }
+  virtual void initialize();
+  virtual void shutdown();
+  virtual bool initialized();
+private:
+  StatisticsApplicationAddin();
+  void on_show_statistics();
+
+  bool m_initialized;
+  Glib::RefPtr<Gtk::Action> m_action;
+  StatisticsWidget *m_widget;
+};
+
+}
+
+#endif
+
diff --git a/src/addins/statistics/statisticswidget.cpp b/src/addins/statistics/statisticswidget.cpp
new file mode 100644
index 0000000..004c27c
--- /dev/null
+++ b/src/addins/statistics/statisticswidget.cpp
@@ -0,0 +1,203 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2013 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include <boost/format.hpp>
+#include <boost/lexical_cast.hpp>
+
+#include <glibmm/i18n.h>
+#include <gtkmm/treestore.h>
+
+#include "debug.hpp"
+#include "itagmanager.hpp"
+#include "statisticswidget.hpp"
+#include "notebooks/notebookmanager.hpp"
+
+
+namespace statistics {
+
+class StatisticsModel
+  : public Gtk::TreeStore
+{
+public:
+  typedef Glib::RefPtr<StatisticsModel> Ptr;
+  static Ptr create(gnote::NoteManager & nm)
+    {
+      return Ptr(new StatisticsModel(nm));
+    }
+
+  void update()
+    {
+      if(m_active) {
+        build_stats();
+      }
+    }
+
+  void active(bool is_active)
+    {
+      m_active = is_active;
+    }
+private:
+  class StatisticsRecord
+    : public Gtk::TreeModelColumnRecord
+  {
+  public:
+    StatisticsRecord()
+      {
+        add(m_stat);
+        add(m_value);
+      }
+  private:
+    Gtk::TreeModelColumn<std::string> m_stat;
+    Gtk::TreeModelColumn<std::string> m_value;
+  };
+  StatisticsRecord m_columns;
+
+  StatisticsModel(gnote::NoteManager & nm)
+    : m_note_manager(nm)
+    , m_active(false)
+    {
+      set_column_types(m_columns);
+      build_stats();
+      nm.signal_note_added.connect(sigc::mem_fun(*this, &StatisticsModel::on_note_list_changed));
+      nm.signal_note_deleted.connect(sigc::mem_fun(*this, &StatisticsModel::on_note_list_changed));
+      gnote::notebooks::NotebookManager::obj().signal_note_added_to_notebook()
+        .connect(sigc::mem_fun(*this, &StatisticsModel::on_notebook_note_list_changed));
+      gnote::notebooks::NotebookManager::obj().signal_note_removed_from_notebook()
+        .connect(sigc::mem_fun(*this, &StatisticsModel::on_notebook_note_list_changed));
+    }
+
+  void build_stats()
+    {
+      clear();
+      gnote::Note::List notes = m_note_manager.get_notes();
+
+      Gtk::TreeIter iter = append();
+      std::string stat = _("Total Notes:");
+      iter->set_value(0, stat);
+      iter->set_value(1, boost::lexical_cast<std::string>(notes.size()));
+
+      Glib::RefPtr<Gtk::TreeModel> notebooks = gnote::notebooks::NotebookManager::obj().get_notebooks();
+      iter = append();
+      stat = _("Total Notebooks:");
+      iter->set_value(0, stat);
+      iter->set_value(1, boost::lexical_cast<std::string>(notebooks->children().size()));
+
+      Gtk::TreeIter notebook = notebooks->children().begin();
+      std::map<gnote::notebooks::Notebook::Ptr, int> notebook_notes;
+      while(notebook) {
+        gnote::notebooks::Notebook::Ptr nbook;
+        notebook->get_value(0, nbook);
+        notebook_notes[nbook] = 0;
+        ++notebook;
+      }
+      gnote::Tag::Ptr template_tag = gnote::ITagManager::obj().get_or_create_system_tag(
+        gnote::ITagManager::TEMPLATE_NOTE_SYSTEM_TAG);
+      for(gnote::Note::List::iterator note = notes.begin(); note != notes.end(); ++note) {
+        for(std::map<gnote::notebooks::Notebook::Ptr, int>::iterator nb = notebook_notes.begin();
+            nb != notebook_notes.end(); ++nb) {
+          if((*note)->contains_tag(nb->first->get_tag()) && !(*note)->contains_tag(template_tag)) {
+            ++nb->second;
+          }
+        }
+      }
+      std::map<std::string, int> notebook_stats;
+      for(std::map<gnote::notebooks::Notebook::Ptr, int>::iterator nb = notebook_notes.begin();
+          nb != notebook_notes.end(); ++nb) {
+        notebook_stats[nb->first->get_name()] = nb->second;
+      }
+      for(std::map<std::string, int>::iterator nb = notebook_stats.begin(); nb != notebook_stats.end(); 
++nb) {
+        Gtk::TreeIter nb_stat = append(iter->children());
+        nb_stat->set_value(0, nb->first);
+        char *fmt = ngettext("%1% note", "%1% notes", nb->second);
+        nb_stat->set_value(1, str(boost::format(fmt) % nb->second));
+      }
+
+      DBG_OUT("Statistics updated");
+    }
+
+  void on_note_list_changed(const gnote::Note::Ptr &)
+    {
+      update();
+    }
+
+  void on_notebook_note_list_changed(const gnote::Note &, const gnote::notebooks::Notebook::Ptr &)
+    {
+      update();
+    }
+
+  gnote::NoteManager & m_note_manager;
+  bool m_active;
+};
+
+
+StatisticsWidget::StatisticsWidget(gnote::NoteManager & nm)
+  : Gtk::TreeView(StatisticsModel::create(nm))
+{
+  StatisticsModel::Ptr model = StatisticsModel::Ptr::cast_dynamic(get_model());
+  set_model(model);
+  set_headers_visible(false);
+
+  Gtk::CellRendererText *renderer = manage(new Gtk::CellRendererText);
+  Gtk::TreeViewColumn *column = manage(new Gtk::TreeViewColumn("", *renderer));
+  column->set_cell_data_func(*renderer, sigc::mem_fun(*this, &StatisticsWidget::col1_data_func));
+  append_column(*column);
+
+  renderer = manage(new Gtk::CellRendererText);
+  column = manage(new Gtk::TreeViewColumn("", *renderer));
+  column->set_cell_data_func(*renderer, sigc::mem_fun(*this, &StatisticsWidget::col2_data_func));
+  append_column(*column);
+}
+
+std::string StatisticsWidget::get_name() const
+{
+  return _("Statistics");
+}
+
+void StatisticsWidget::foreground()
+{
+  gnote::utils::EmbeddableWidget::foreground();
+  StatisticsModel::Ptr model = StatisticsModel::Ptr::cast_static(get_model());
+  model->active(true);
+  model->update();
+  expand_all();
+}
+
+void StatisticsWidget::background()
+{
+  gnote::utils::EmbeddableWidget::background();
+  StatisticsModel::Ptr::cast_static(get_model())->active(false);
+}
+
+void StatisticsWidget::col1_data_func(Gtk::CellRenderer *renderer, const Gtk::TreeIter & iter)
+{
+  std::string val;
+  iter->get_value(0, val);
+  static_cast<Gtk::CellRendererText*>(renderer)->property_markup() = "<b>" + val + "</b>";
+}
+
+void StatisticsWidget::col2_data_func(Gtk::CellRenderer *renderer, const Gtk::TreeIter & iter)
+{
+  std::string val;
+  iter->get_value(1, val);
+  static_cast<Gtk::CellRendererText*>(renderer)->property_text() = val;
+}
+
+}
+
diff --git a/src/addins/statistics/statisticswidget.hpp b/src/addins/statistics/statisticswidget.hpp
new file mode 100644
index 0000000..9edbaeb
--- /dev/null
+++ b/src/addins/statistics/statisticswidget.hpp
@@ -0,0 +1,49 @@
+/*
+ * gnote
+ *
+ * Copyright (C) 2013 Aurimas Cernius
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef _STATISTICS_WIDGET_HPP_
+#define _STATISTICS_WIDGET_HPP_
+
+#include <gtkmm/treeview.h>
+
+#include "notemanager.hpp"
+#include "utils.hpp"
+
+
+namespace statistics {
+
+class StatisticsWidget
+  : public Gtk::TreeView
+  , public gnote::utils::EmbeddableWidget
+{
+public:
+  StatisticsWidget(gnote::NoteManager & nm);
+  virtual std::string get_name() const;
+  virtual void foreground();
+  virtual void background();
+private:
+  void col1_data_func(Gtk::CellRenderer *renderer, const Gtk::TreeIter & iter);
+  void col2_data_func(Gtk::CellRenderer *renderer, const Gtk::TreeIter & iter);
+};
+
+}
+
+#endif
+


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