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>
34 #include "uri_opener.hxx"
40 /* static data members */
41 boost::ptr_vector<ApplicationWindow> ApplicationWindow::windows;
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;
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";
56 std::vector<std::string> uris;
58 if (boost::filesystem::exists(session) &&
59 boost::filesystem::is_regular(session)) {
60 std::ifstream ifs(session.file_string().c_str());
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;
69 BOOST_FOREACH(const std::string& uri, uris) {
70 uri_opener::open(uri);
74 uri_opener::open("http://dubai.2ch.net/morningcoffee/");
78 void ApplicationWindow::save() {
79 std::vector<std::string> uris;
80 BOOST_FOREACH(const ApplicationWindow& window, windows) {
81 uris.push_back(window.get_uri());
84 std::string homedir = std::getenv("HOME");
85 boost::filesystem::path dir(homedir);
86 boost::filesystem::path session = dir / ".dialektos" / "session.xml";
88 if (!misc::create_directories(session.parent_path())) return;
89 std::ofstream ofs(session.file_string().c_str());
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;
98 //bool ApplicationWindow::is_same(const bbs_detail::Base& bbs) const {
102 ApplicationWindow::ApplicationWindow(): Gtk::Window() {}
104 ApplicationWindow::~ApplicationWindow() {}
106 void ApplicationWindow::regist(ApplicationWindow* window) {
107 windows.push_back(window);
111 bool ApplicationWindow::on_delete_event(GdkEventAny* /*event*/) {
112 using namespace boost::lambda;
113 // using boost::lambda::_1;
115 windows.erase_if(&_1 == this);
116 if (!windows.empty()) save();
117 if (windows.empty()) Gtk::Main::quit();
122 } // namespace dialektos