OSDN Git Service

Add open all menu and open all and close others menu in History.
[fukui-no-namari/dialektos.git] / src / application_window.cxx
index a579108..3600ab2 100644 (file)
 #include <boost/serialization/nvp.hpp>
 #include <boost/serialization/vector.hpp>
 #include <boost/filesystem.hpp>
+#include <boost/bind.hpp>
+#include <boost/ref.hpp>
 #include <fstream>
 #include <iostream>
+#include <algorithm>
 #include <cstdlib>
 #include "uri_opener.hxx"
+#include "misc.hxx"
 
 
 namespace dialektos {
 
 /* static data members */
 boost::ptr_vector<ApplicationWindow> ApplicationWindow::windows;
+history::Data ApplicationWindow::histories;
 
 /* static member function */
-bool ApplicationWindow::is_opened(const bbs_detail::Base& bbs) {
-  BOOST_FOREACH(const ApplicationWindow& window, windows) {
-    if (window.is_same(bbs)) return true;
-  }
-  return false;
+ApplicationWindow* ApplicationWindow::find(const bbs_detail::Base& bbs) {
+  boost::ptr_vector<ApplicationWindow>::iterator it =
+      std::find_if(windows.begin(), windows.end(),
+          boost::bind(&ApplicationWindow::is_same, _1, boost::ref(bbs)));
+  return it != windows.end() ? &*it : 0;
 }
 
 void ApplicationWindow::load() {
@@ -52,6 +57,7 @@ void ApplicationWindow::load() {
   boost::filesystem::path dir(homedir);
   boost::filesystem::path session = dir / ".dialektos" / "session.xml";
 
+  histories.from_xml(dir / ".dialektos" / "history.xml");
   std::vector<std::string> uris;
 
   if (boost::filesystem::exists(session) &&
@@ -72,6 +78,8 @@ void ApplicationWindow::load() {
   if (uris.empty()) {
     uri_opener::open("http://dubai.2ch.net/morningcoffee/");
   }
+
+  histories.set_writable();
 }
 
 void ApplicationWindow::save() {
@@ -84,6 +92,7 @@ void ApplicationWindow::save() {
   boost::filesystem::path dir(homedir);
   boost::filesystem::path session = dir / ".dialektos" / "session.xml";
 
+  if (!misc::create_directories(session.parent_path())) return;
   std::ofstream ofs(session.file_string().c_str());
   try {
     boost::archive::xml_oarchive oa(ofs);
@@ -91,6 +100,21 @@ void ApplicationWindow::save() {
   } catch (const boost::archive::archive_exception& e) {
     std::cerr << "save(): " << e.what() << std::endl;
   }
+
+  histories.to_xml(dir / ".dialektos" / "history.xml");
+}
+
+void ApplicationWindow::close_windows(
+    const std::vector<ApplicationWindow*>& wins) {
+  BOOST_FOREACH(ApplicationWindow* window, wins) {
+    using namespace boost::lambda;
+    using boost::lambda::_1;
+    window->save_state();
+    windows.erase_if(&_1 == window);
+  }
+
+  if (!windows.empty()) save();
+  if (windows.empty()) Gtk::Main::quit();
 }
 
 //bool ApplicationWindow::is_same(const bbs_detail::Base& bbs) const {
@@ -108,7 +132,8 @@ void ApplicationWindow::regist(ApplicationWindow* window) {
 
 bool ApplicationWindow::on_delete_event(GdkEventAny* /*event*/) {
   using namespace boost::lambda;
-//  using boost::lambda::_1;
+  using boost::lambda::_1;
+  save_state();
   windows.erase_if(&_1 == this);
   if (!windows.empty()) save();
   if (windows.empty()) Gtk::Main::quit();