2 * Copyright (C) 2009 by Aiwota Programmer
3 * aiwotaprog@tetteke.tk
5 * This file is part of Dialektos.
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.
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.
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/>.
21 #include "application_window.hxx"
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>
37 #include "uri_opener.hxx"
43 /* static data members */
44 boost::ptr_vector<ApplicationWindow> ApplicationWindow::windows;
45 history::Data ApplicationWindow::histories;
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;
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";
60 histories.from_xml(dir / ".dialektos" / "history.xml");
61 std::vector<std::string> uris;
63 if (boost::filesystem::exists(session) &&
64 boost::filesystem::is_regular(session)) {
65 std::ifstream ifs(session.file_string().c_str());
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;
74 BOOST_FOREACH(const std::string& uri, uris) {
75 uri_opener::open(uri);
79 uri_opener::open("http://dubai.2ch.net/morningcoffee/");
82 histories.set_writable();
85 void ApplicationWindow::save() {
86 std::vector<std::string> uris;
87 BOOST_FOREACH(const ApplicationWindow& window, windows) {
88 uris.push_back(window.get_uri());
91 std::string homedir = std::getenv("HOME");
92 boost::filesystem::path dir(homedir);
93 boost::filesystem::path session = dir / ".dialektos" / "session.xml";
95 if (!misc::create_directories(session.parent_path())) return;
96 std::ofstream ofs(session.file_string().c_str());
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;
104 histories.to_xml(dir / ".dialektos" / "history.xml");
107 //bool ApplicationWindow::is_same(const bbs_detail::Base& bbs) const {
111 ApplicationWindow::ApplicationWindow(): Gtk::Window() {}
113 ApplicationWindow::~ApplicationWindow() {}
115 void ApplicationWindow::regist(ApplicationWindow* window) {
116 windows.push_back(window);
120 bool ApplicationWindow::on_delete_event(GdkEventAny* /*event*/) {
121 using namespace boost::lambda;
122 using boost::lambda::_1;
124 windows.erase_if(&_1 == this);
125 if (!windows.empty()) save();
126 if (windows.empty()) Gtk::Main::quit();
131 } // namespace dialektos