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
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 <boost/bind.hpp>
32 #include <boost/ref.hpp>
33 #include <fstream>
34 #include <iostream>
35 #include <algorithm>
36 #include <cstdlib>
37 #include "uri_opener.hxx"
38 #include "misc.hxx"
39
40
41 namespace dialektos {
42
43 /* static data members */
44 boost::ptr_vector<ApplicationWindow> ApplicationWindow::windows;
45 history::Data ApplicationWindow::histories;
46
47 /* static member function */
48 ApplicationWindow* ApplicationWindow::find(const bbs_detail::Base& bbs) {
49   boost::ptr_vector<ApplicationWindow>::iterator it =
50       std::find_if(windows.begin(), windows.end(),
51           boost::bind(&ApplicationWindow::is_same, _1, boost::ref(bbs)));
52   return it != windows.end() ? &*it : 0;
53 }
54
55 void ApplicationWindow::load() {
56   std::string homedir = std::getenv("HOME");
57   boost::filesystem::path dir(homedir);
58   boost::filesystem::path session = dir / ".dialektos" / "session.xml";
59
60   histories.from_xml(dir / ".dialektos" / "history.xml");
61   std::vector<std::string> uris;
62
63   if (boost::filesystem::exists(session) &&
64       boost::filesystem::is_regular(session)) {
65     std::ifstream ifs(session.file_string().c_str());
66     try {
67       boost::archive::xml_iarchive ia(ifs);
68       ia >> boost::serialization::make_nvp("Session", uris);
69     } catch (const boost::archive::archive_exception& e) {
70       std::cerr << "ApplicationWindow::load(): " << e.what() << std::endl;
71     }
72   }
73
74   BOOST_FOREACH(const std::string& uri, uris) {
75     uri_opener::open(uri);
76   }
77
78   if (uris.empty()) {
79     uri_opener::open("http://dubai.2ch.net/morningcoffee/");
80   }
81
82   histories.set_writable();
83 }
84
85 void ApplicationWindow::save() {
86   std::vector<std::string> uris;
87   BOOST_FOREACH(const ApplicationWindow& window, windows) {
88     uris.push_back(window.get_uri());
89   }
90
91   std::string homedir = std::getenv("HOME");
92   boost::filesystem::path dir(homedir);
93   boost::filesystem::path session = dir / ".dialektos" / "session.xml";
94
95   if (!misc::create_directories(session.parent_path())) return;
96   std::ofstream ofs(session.file_string().c_str());
97   try {
98     boost::archive::xml_oarchive oa(ofs);
99     oa << boost::serialization::make_nvp("Session", uris);
100   } catch (const boost::archive::archive_exception& e) {
101     std::cerr << "save(): " << e.what() << std::endl;
102   }
103
104   histories.to_xml(dir / ".dialektos" / "history.xml");
105 }
106
107 void ApplicationWindow::close_windows(
108     const std::vector<ApplicationWindow*>& wins) {
109   BOOST_FOREACH(ApplicationWindow* window, wins) {
110     using namespace boost::lambda;
111     using boost::lambda::_1;
112     window->save_state();
113     windows.erase_if(&_1 == window);
114   }
115
116   if (!windows.empty()) save();
117   if (windows.empty()) Gtk::Main::quit();
118 }
119
120 //bool ApplicationWindow::is_same(const bbs_detail::Base& bbs) const {
121 //  return false;
122 //}
123
124 ApplicationWindow::ApplicationWindow(): Gtk::Window() {}
125
126 ApplicationWindow::~ApplicationWindow() {}
127
128 void ApplicationWindow::regist(ApplicationWindow* window) {
129   windows.push_back(window);
130   save();
131 }
132
133 bool ApplicationWindow::on_delete_event(GdkEventAny* /*event*/) {
134   using namespace boost::lambda;
135   using boost::lambda::_1;
136   save_state();
137   windows.erase_if(&_1 == this);
138   if (!windows.empty()) save();
139   if (windows.empty()) Gtk::Main::quit();
140   return false;
141 }
142
143
144 } // namespace dialektos