OSDN Git Service

Session load and save.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 28 Jun 2009 12:54:06 +0000 (21:54 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 28 Jun 2009 12:54:06 +0000 (21:54 +0900)
src/application_framework.cxx
src/application_window.cxx
src/application_window.hxx
src/bbs_detail_base.cxx
src/bbs_detail_base.hxx
src/board_window.cxx
src/board_window.hxx
src/main.cxx
src/thread_window.cxx
src/thread_window.hxx

index 3663830..36dd67d 100644 (file)
@@ -115,6 +115,7 @@ void ApplicationFrameWork::build_menubar() {
 }
 
 void ApplicationFrameWork::on_action_file_quit() {
+  save();
   windows.clear();
   Gtk::Main::quit();
 }
index ab1d95f..a579108 100644 (file)
 #include <gtkmm/main.h>
 #include <boost/lambda/lambda.hpp>
 #include <boost/foreach.hpp>
+#include <boost/archive/xml_iarchive.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/serialization/nvp.hpp>
+#include <boost/serialization/vector.hpp>
+#include <boost/filesystem.hpp>
+#include <fstream>
+#include <iostream>
+#include <cstdlib>
+#include "uri_opener.hxx"
 
 
 namespace dialektos {
@@ -38,6 +47,52 @@ bool ApplicationWindow::is_opened(const bbs_detail::Base& bbs) {
   return false;
 }
 
+void ApplicationWindow::load() {
+  std::string homedir = std::getenv("HOME");
+  boost::filesystem::path dir(homedir);
+  boost::filesystem::path session = dir / ".dialektos" / "session.xml";
+
+  std::vector<std::string> uris;
+
+  if (boost::filesystem::exists(session) &&
+      boost::filesystem::is_regular(session)) {
+    std::ifstream ifs(session.file_string().c_str());
+    try {
+      boost::archive::xml_iarchive ia(ifs);
+      ia >> boost::serialization::make_nvp("Session", uris);
+    } catch (const boost::archive::archive_exception& e) {
+      std::cerr << "ApplicationWindow::load(): " << e.what() << std::endl;
+    }
+  }
+
+  BOOST_FOREACH(const std::string& uri, uris) {
+    uri_opener::open(uri);
+  }
+
+  if (uris.empty()) {
+    uri_opener::open("http://dubai.2ch.net/morningcoffee/");
+  }
+}
+
+void ApplicationWindow::save() {
+  std::vector<std::string> uris;
+  BOOST_FOREACH(const ApplicationWindow& window, windows) {
+    uris.push_back(window.get_uri());
+  }
+
+  std::string homedir = std::getenv("HOME");
+  boost::filesystem::path dir(homedir);
+  boost::filesystem::path session = dir / ".dialektos" / "session.xml";
+
+  std::ofstream ofs(session.file_string().c_str());
+  try {
+    boost::archive::xml_oarchive oa(ofs);
+    oa << boost::serialization::make_nvp("Session", uris);
+  } catch (const boost::archive::archive_exception& e) {
+    std::cerr << "save(): " << e.what() << std::endl;
+  }
+}
+
 //bool ApplicationWindow::is_same(const bbs_detail::Base& bbs) const {
 //  return false;
 //}
@@ -48,12 +103,14 @@ ApplicationWindow::~ApplicationWindow() {}
 
 void ApplicationWindow::regist(ApplicationWindow* window) {
   windows.push_back(window);
+  save();
 }
 
 bool ApplicationWindow::on_delete_event(GdkEventAny* /*event*/) {
   using namespace boost::lambda;
 //  using boost::lambda::_1;
   windows.erase_if(&_1 == this);
+  if (!windows.empty()) save();
   if (windows.empty()) Gtk::Main::quit();
   return false;
 }
index 0debc6f..0564551 100644 (file)
@@ -36,12 +36,15 @@ class ApplicationWindow: public Gtk::Window {
 public:
   virtual ~ApplicationWindow();
   static bool is_opened(const bbs_detail::Base& bbs);
+  static void load();
 protected:
   ApplicationWindow();
   static void regist(ApplicationWindow* window);
+  static void save();
   virtual bool on_delete_event(GdkEventAny*);
 private:
   virtual bool is_same(const bbs_detail::Base& bbs) const = 0;
+  virtual std::string get_uri() const = 0;
 protected:
   static boost::ptr_vector<ApplicationWindow> windows;
 };
index 67c32e8..0eeba68 100644 (file)
@@ -103,6 +103,14 @@ std::string Base::get_thread_dat_uri() const {
   return "http://" + host_ + "/" + board_ + "/dat/" + thread_ + ".dat";
 }
 
+std::string Base::get_thread_uri() const {
+  assert(!thread_.empty());
+  std::stringstream ss;
+  ss << boost::format("http://%1%/test/read.cgi/%2%/%3%/")
+    % host_ % board_ % thread_ << std::flush;
+  return ss.str();
+}
+
 std::string Base::get_another_thread_uri(const std::string& thread) const {
   return "http://" + host_ + "/test/read.cgi/" + board_ + "/" + thread + "/";
 }
@@ -120,6 +128,12 @@ std::string Base::get_board_subject_uri() const {
   return ss.str();
 }
 
+std::string Base::get_board_uri() const {
+  std::stringstream ss;
+  ss << boost::format("http://%1%/%2%/") % host_ % board_ << std::flush;
+  return ss.str();
+}
+
 std::string Base::get_board_subject_idx_path() const {
   boost::filesystem::path dir(get_board_dir_path());
   dir = dir / "subject.xml";
index 8967d96..c77bbbc 100644 (file)
@@ -46,9 +46,11 @@ public:
   virtual std::string get_thread_file_path() const;
   virtual std::string get_thread_idx_path() const;
   virtual std::string get_thread_dat_uri() const;
+  virtual std::string get_thread_uri() const;
   virtual std::string get_another_thread_uri(const std::string& thread) const;
   virtual std::string get_board_subject_path() const;
   virtual std::string get_board_subject_uri() const;
+  virtual std::string get_board_uri() const;
   virtual std::string get_board_subject_idx_path() const;
   virtual std::string get_board_idx_dir_path() const;
   virtual http::Header get_board_subject_request_header() const;
index 5372eeb..f053dcc 100644 (file)
@@ -136,6 +136,11 @@ bool BoardWindow::is_same(const bbs_detail::Base& bbs) const {
   return *bbs_ == bbs;
 }
 
+std::string BoardWindow::get_uri() const {
+  return bbs_->get_board_uri();
+}
+
+
 void BoardWindow::load() {
   boost::unordered_map<model_column::ID::type, ModelColumns> buffer;
 
index f56ea5a..5a1c3f1 100644 (file)
@@ -53,6 +53,7 @@ protected:
   virtual void on_action_view_stop();
 private:
   virtual bool is_same(const bbs_detail::Base& bbs) const;
+  virtual std::string get_uri() const;
 
   void on_row_activated(const Gtk::TreeModel::Path&,
       Gtk::TreeViewColumn*);
index 1568587..73a8337 100644 (file)
@@ -23,6 +23,7 @@
 #include <gtkmm/main.h>
 #include <sigc++/functors/ptr_fun.h>
 #include "uri_opener.hxx"
+#include "application_window.hxx"
 
 
 /*! @brief namespace for dialektos */
@@ -31,9 +32,7 @@ namespace dialektos {}
 
 bool window() {
 
-  dialektos::uri_opener::open(
-      "http://localhost.2ch.net/test/read.cgi/hagehage/1234567890/");
-  dialektos::uri_opener::open("http://localhost.2ch.net/hagehage/");
+  dialektos::ApplicationWindow::load();
 
   return false;
 }
index 52c53b7..11df6a1 100644 (file)
@@ -80,6 +80,11 @@ bool ThreadWindow::is_same(const bbs_detail::Base& bbs) const {
   return lhs == bbs;
 }
 
+std::string ThreadWindow::get_uri() const {
+  return bbs_->get_thread_uri();
+}
+
+
 void ThreadWindow::on_action_view_refresh() {
   if (http_getter_) return;
 
index 3dfd0bf..8056a4a 100644 (file)
@@ -51,6 +51,7 @@ protected:
   virtual void on_action_view_stop();
 private:
   virtual bool is_same(const bbs_detail::Base& bbs) const;
+  virtual std::string get_uri() const;
 
   void on_http_get_end(bool);
   void on_refresh_end(const http::StatusLine&, const http::Header&);