[gnote] Centralize note opening



commit 76e8d852b40b3f093165e48e967eafe0c47f5042
Author: Aurimas Černius <aurisc4 gmail com>
Date:   Mon Jul 29 00:06:09 2013 +0300

    Centralize note opening
    
    Open notes through centralized place in MainWindow, so that general
    logic works the same everywhere.
    Fixes Bug 703151.

 src/addins/backlinks/backlinkmenuitem.cpp |    5 +--
 src/dbus/remotecontrol.cpp                |    3 +-
 src/gnote.cpp                             |    4 +-
 src/mainwindow.cpp                        |   58 +++++++++++++++++++++++++++++
 src/mainwindow.hpp                        |    7 +++-
 src/notebooks/notebooknewnotemenuitem.cpp |    7 +--
 src/noterenamedialog.cpp                  |   18 ++------
 src/recentchanges.cpp                     |    9 ++--
 src/recentchanges.hpp                     |    2 +-
 src/synchronization/syncdialog.cpp        |    4 +-
 src/tray.cpp                              |    5 +--
 src/watchers.cpp                          |   16 +------
 12 files changed, 84 insertions(+), 54 deletions(-)
---
diff --git a/src/addins/backlinks/backlinkmenuitem.cpp b/src/addins/backlinks/backlinkmenuitem.cpp
index d085090..de10985 100644
--- a/src/addins/backlinks/backlinkmenuitem.cpp
+++ b/src/addins/backlinks/backlinkmenuitem.cpp
@@ -96,10 +96,7 @@ void BacklinkMenuItem::on_activate()
     return;
   }
 
-  gnote::MainWindow &window = gnote::IGnote::obj().new_main_window();
-  window.present_note(m_note);
-  window.present();
-  window.close_on_escape(true);
+  gnote::MainWindow::present_in_new_window(m_note, true);
 }
 
 
diff --git a/src/dbus/remotecontrol.cpp b/src/dbus/remotecontrol.cpp
index cd38d39..7bf43d6 100644
--- a/src/dbus/remotecontrol.cpp
+++ b/src/dbus/remotecontrol.cpp
@@ -408,8 +408,7 @@ void RemoteControl::on_note_saved(const Note::Ptr & note)
 MainWindow & RemoteControl::present_note(const Note::Ptr & note)
 {
   MainWindow & window = IGnote::obj().get_window_for_note();
-  window.present_note(note);
-  window.present();
+  MainWindow::present_in(window, note);
   return window;
 }
 
diff --git a/src/gnote.cpp b/src/gnote.cpp
index a852a48..758f7d7 100644
--- a/src/gnote.cpp
+++ b/src/gnote.cpp
@@ -543,9 +543,7 @@ namespace gnote {
 
   void Gnote::open_note(const Note::Ptr & note)
   {
-    MainWindow & main_window = get_window_for_note();
-    main_window.present_note(note);
-    main_window.present();
+    MainWindow::present_in(get_window_for_note(), note);
   }
 
 
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 7fd78d0..4d7a188 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -18,7 +18,9 @@
  */
 
 
+#include "ignote.hpp"
 #include "mainwindow.hpp"
+#include "notewindow.hpp"
 
 namespace gnote {
 
@@ -38,6 +40,62 @@ MainWindow *MainWindow::get_owning(Gtk::Widget & widget)
   return dynamic_cast<MainWindow*>(container);
 }
 
+void MainWindow::present_in(MainWindow & win, const Note::Ptr & note)
+{
+  win.present_note(note);
+  win.present();
+}
+
+MainWindow *MainWindow::present_active(const Note::Ptr & note)
+{
+  if(note && note->get_window()->host()
+     && note->get_window()->host()->is_foreground(*note->get_window())) {
+    MainWindow *win = dynamic_cast<MainWindow*>(note->get_window()->host());
+    win->present();
+    return win;
+  }
+
+  return NULL;
+}
+
+MainWindow *MainWindow::present_in_new_window(const Note::Ptr & note, bool close_on_esc)
+{
+  if(!note) {
+    return NULL;
+  }
+  if(!MainWindow::present_active(note)) {
+    MainWindow & window = IGnote::obj().new_main_window();
+    window.present_note(note);
+    window.present();
+    window.close_on_escape(close_on_esc);
+    return &window;
+  }
+
+  return NULL;
+}
+
+MainWindow *MainWindow::present_default(const Note::Ptr & note)
+{
+  if(!note) {
+    return NULL;
+  }
+  MainWindow *win = MainWindow::present_active(note);
+  if(win) {
+    return win;
+  }
+  if(false == Preferences::obj().get_schema_settings(Preferences::SCHEMA_GNOTE)->get_boolean(
+                Preferences::OPEN_NOTES_IN_NEW_WINDOW)) {
+    win = dynamic_cast<MainWindow*>(note->get_window()->host());
+  }
+  if(!win) {
+    win = &IGnote::obj().new_main_window();
+    win->close_on_escape(true);
+  }
+  win->present_note(note);
+  win->present();
+  return win;
+}
+
 
 MainWindow::MainWindow(const std::string & title)
   : utils::ForcedPresentWindow(title)
diff --git a/src/mainwindow.hpp b/src/mainwindow.hpp
index 45dbd01..491df2b 100644
--- a/src/mainwindow.hpp
+++ b/src/mainwindow.hpp
@@ -34,11 +34,14 @@ class MainWindow
 {
 public:
   static MainWindow *get_owning(Gtk::Widget & widget);
+  static void present_in(MainWindow & win, const Note::Ptr & note);
+  static MainWindow *present_active(const Note::Ptr & note);
+  static MainWindow *present_in_new_window(const Note::Ptr & note, bool close_on_esacpe);
+  static MainWindow *present_default(const Note::Ptr & note);
 
   explicit MainWindow(const std::string & title);
 
   virtual void set_search_text(const std::string & value) = 0;
-  virtual void present_note(const Note::Ptr & note) = 0;
   virtual void show_search_bar() = 0;
   virtual void present_search() = 0;
   virtual void new_note() = 0;
@@ -52,6 +55,8 @@ public:
     {
       return m_close_on_esc;
     }
+protected:
+  virtual void present_note(const Note::Ptr & note) = 0;
 private:
   bool m_close_on_esc;
 };
diff --git a/src/notebooks/notebooknewnotemenuitem.cpp b/src/notebooks/notebooknewnotemenuitem.cpp
index bdf5661..2ef488a 100644
--- a/src/notebooks/notebooknewnotemenuitem.cpp
+++ b/src/notebooks/notebooknewnotemenuitem.cpp
@@ -25,8 +25,8 @@
 #include <gtkmm/image.h>
 
 #include "sharp/string.hpp"
-#include "ignote.hpp"
 #include "iconmanager.hpp"
+#include "mainwindow.hpp"
 #include "note.hpp"
 #include "notemanager.hpp"
 #include "notewindow.hpp"
@@ -54,10 +54,7 @@ namespace gnote {
       
       // Look for the template note and create a new note
       Note::Ptr note = m_notebook->create_notebook_note ();
-      MainWindow &win = IGnote::obj().new_main_window();
-      win.present_note(note);
-      win.present();
-      win.close_on_escape(true);
+      MainWindow::present_in_new_window(note, true);
     }
 
 
diff --git a/src/noterenamedialog.cpp b/src/noterenamedialog.cpp
index e460d0e..30a24eb 100644
--- a/src/noterenamedialog.cpp
+++ b/src/noterenamedialog.cpp
@@ -29,7 +29,7 @@
 #include <glibmm/i18n.h>
 #include <gtkmm/expander.h>
 
-#include "ignote.hpp"
+#include "mainwindow.hpp"
 #include "notewindow.hpp"
 #include "noterenamedialog.hpp"
 
@@ -361,19 +361,11 @@ void NoteRenameDialog::on_notes_view_row_activated(
   if (!note)
     return;
 
-  Gtk::Widget *parent = get_parent();
-  MainWindow *window = NULL;
-  if(parent) {
-    window = MainWindow::get_owning(*parent);
+  MainWindow *window = MainWindow::present_default(note);
+  if(window) {
+    window->set_search_text(Glib::ustring::compose("\"%1\"", old_title));
+    window->show_search_bar();
   }
-  if(!window) {
-    window = &IGnote::obj().new_main_window();
-  }
-
-  window->present_note(note);
-  window->present();
-  window->set_search_text(Glib::ustring::compose("\"%1\"", old_title));
-  window->show_search_bar();
 }
 
 void NoteRenameDialog::on_select_all_button_clicked(bool select)
diff --git a/src/recentchanges.cpp b/src/recentchanges.cpp
index 5414ed5..24a3223 100644
--- a/src/recentchanges.cpp
+++ b/src/recentchanges.cpp
@@ -319,16 +319,15 @@ namespace gnote {
       on_open_note_new_window(note);
     }
     else {
-      present_note(note);
+      if(!present_active(note)) {
+        present_note(note);
+      }
     }
   }
 
   void NoteRecentChanges::on_open_note_new_window(const Note::Ptr & note)
   {
-    MainWindow & window = IGnote::obj().new_main_window();
-    window.present();
-    window.present_note(note);
-    window.close_on_escape(true);
+    present_in_new_window(note, true);
   }
 
   void NoteRecentChanges::on_delete_note()
diff --git a/src/recentchanges.hpp b/src/recentchanges.hpp
index 07438aa..6f23d1e 100644
--- a/src/recentchanges.hpp
+++ b/src/recentchanges.hpp
@@ -46,7 +46,6 @@ public:
   virtual ~NoteRecentChanges();
   virtual void show_search_bar() override;
   virtual void set_search_text(const std::string & value) override;
-  virtual void present_note(const Note::Ptr & note) override;
   virtual void new_note() override;
   virtual void present_search() override;
   virtual void close_window() override;
@@ -62,6 +61,7 @@ public:
   virtual bool contains(EmbeddableWidget &) override;
   virtual bool is_foreground(EmbeddableWidget &) override;
 protected:
+  virtual void present_note(const Note::Ptr & note) override;
   virtual void on_show() override;
   virtual bool on_map_event(GdkEventAny *evt) override;
 private:
diff --git a/src/synchronization/syncdialog.cpp b/src/synchronization/syncdialog.cpp
index 66381ad..e8b3a33 100644
--- a/src/synchronization/syncdialog.cpp
+++ b/src/synchronization/syncdialog.cpp
@@ -743,9 +743,7 @@ void SyncDialog::rename_note(const Note::Ptr & note, const std::string & newTitl
 
 void SyncDialog::present_note(const Note::Ptr & note)
 {
-  MainWindow & window = IGnote::obj().get_window_for_note();
-  window.present_note(note);
-  window.present();
+  MainWindow::present_in(IGnote::obj().get_window_for_note(), note);
 }
 
 }
diff --git a/src/tray.cpp b/src/tray.cpp
index cf34a3d..c32e07d 100644
--- a/src/tray.cpp
+++ b/src/tray.cpp
@@ -95,10 +95,7 @@ namespace gnote {
   {
     if(!m_inhibit_activate) {
       if(m_note) {
-        MainWindow & window = IGnote::obj().new_main_window();
-        window.present_note(m_note);
-        window.present();
-        window.close_on_escape(true);
+        MainWindow::present_in_new_window(m_note, true);
       }
     }
   }
diff --git a/src/watchers.cpp b/src/watchers.cpp
index d8f2ddc..c3a31a8 100644
--- a/src/watchers.cpp
+++ b/src/watchers.cpp
@@ -33,7 +33,7 @@
 
 #include "sharp/string.hpp"
 #include "debug.hpp"
-#include "ignote.hpp"
+#include "mainwindow.hpp"
 #include "noteeditor.hpp"
 #include "notemanager.hpp"
 #include "notewindow.hpp"
@@ -878,7 +878,7 @@ namespace gnote {
   }
 
 
-  bool NoteLinkWatcher::open_or_create_link(const NoteEditor & editor,
+  bool NoteLinkWatcher::open_or_create_link(const NoteEditor &,
                                             const Gtk::TextIter & start,
                                             const Gtk::TextIter & end)
   {
@@ -910,17 +910,7 @@ namespace gnote {
     // also works around the bug.
     if (link) {
       DBG_OUT ("Opening note '%s' on click...", link_name.c_str());
-      MainWindow *window = NULL;
-      if(false == Preferences::obj().get_schema_settings(Preferences::SCHEMA_GNOTE)->get_boolean(
-                    Preferences::OPEN_NOTES_IN_NEW_WINDOW)) {
-        window = MainWindow::get_owning(const_cast<NoteEditor&>(editor));
-      }
-      if(!window) {
-        window = &IGnote::obj().new_main_window();
-        window->close_on_escape(true);
-      }
-      window->present_note(link);
-      window->present();
+      MainWindow::present_default(link);
       return true;
     }
 


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