OSDN Git Service

Change idx sub dir's time stamp when saving idx.
[fukui-no-namari/dialektos.git] / src / application_window.cxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
7  * Dialektos is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Dialektos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Dialektos.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "application_window.hxx"
22
23 #include <gtkmm/main.h>
24 #include <boost/lambda/lambda.hpp>
25 #include <boost/foreach.hpp>
26 #include <boost/archive/xml_iarchive.hpp>
27 #include <boost/archive/xml_oarchive.hpp>
28 #include <boost/serialization/nvp.hpp>
29 #include <boost/serialization/vector.hpp>
30 #include <boost/filesystem.hpp>
31 #include <fstream>
32 #include <iostream>
33 #include <cstdlib>
34 #include "uri_opener.hxx"
35 #include "misc.hxx"
36
37
38 namespace dialektos {
39
40 /* static data members */
41 boost::ptr_vector<ApplicationWindow> ApplicationWindow::windows;
42
43 /* static member function */
44 bool ApplicationWindow::is_opened(const bbs_detail::Base& bbs) {
45   BOOST_FOREACH(const ApplicationWindow& window, windows) {
46     if (window.is_same(bbs)) return true;
47   }
48   return false;
49 }
50
51 void ApplicationWindow::load() {
52   std::string homedir = std::getenv("HOME");
53   boost::filesystem::path dir(homedir);
54   boost::filesystem::path session = dir / ".dialektos" / "session.xml";
55
56   std::vector<std::string> uris;
57
58   if (boost::filesystem::exists(session) &&
59       boost::filesystem::is_regular(session)) {
60     std::ifstream ifs(session.file_string().c_str());
61     try {
62       boost::archive::xml_iarchive ia(ifs);
63       ia >> boost::serialization::make_nvp("Session", uris);
64     } catch (const boost::archive::archive_exception& e) {
65       std::cerr << "ApplicationWindow::load(): " << e.what() << std::endl;
66     }
67   }
68
69   BOOST_FOREACH(const std::string& uri, uris) {
70     uri_opener::open(uri);
71   }
72
73   if (uris.empty()) {
74     uri_opener::open("http://dubai.2ch.net/morningcoffee/");
75   }
76 }
77
78 void ApplicationWindow::save() {
79   std::vector<std::string> uris;
80   BOOST_FOREACH(const ApplicationWindow& window, windows) {
81     uris.push_back(window.get_uri());
82   }
83
84   std::string homedir = std::getenv("HOME");
85   boost::filesystem::path dir(homedir);
86   boost::filesystem::path session = dir / ".dialektos" / "session.xml";
87
88   if (!misc::create_directories(session.parent_path())) return;
89   std::ofstream ofs(session.file_string().c_str());
90   try {
91     boost::archive::xml_oarchive oa(ofs);
92     oa << boost::serialization::make_nvp("Session", uris);
93   } catch (const boost::archive::archive_exception& e) {
94     std::cerr << "save(): " << e.what() << std::endl;
95   }
96 }
97
98 //bool ApplicationWindow::is_same(const bbs_detail::Base& bbs) const {
99 //  return false;
100 //}
101
102 ApplicationWindow::ApplicationWindow(): Gtk::Window() {}
103
104 ApplicationWindow::~ApplicationWindow() {}
105
106 void ApplicationWindow::regist(ApplicationWindow* window) {
107   windows.push_back(window);
108   save();
109 }
110
111 bool ApplicationWindow::on_delete_event(GdkEventAny* /*event*/) {
112   using namespace boost::lambda;
113 //  using boost::lambda::_1;
114   save_state();
115   windows.erase_if(&_1 == this);
116   if (!windows.empty()) save();
117   if (windows.empty()) Gtk::Main::quit();
118   return false;
119 }
120
121
122 } // namespace dialektos