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 "board_window.hxx"
23 #include <glibmm/ustring.h>
24 #include <glibmm/refptr.h>
25 #include <sigc++/functors/mem_fun.h>
26 #include <sigc++/signal.h>
28 #include <boost/date_time/posix_time/posix_time.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <boost/filesystem.hpp>
31 #include <boost/format.hpp>
32 #include <boost/bind.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/mpl/find.hpp>
41 #include "bbs_detail_base.hxx"
42 #include "thread_list_model.hxx"
43 #include "board_view_column.hxx"
44 #include "board_subject_item.hxx"
45 #include "thread_idx_cache.hxx"
46 #include "uri_opener.hxx"
47 #include "http_get.hxx"
48 #include "board_subject_idx.hxx"
49 #include "http_date.hxx"
56 void BoardWindow::create(std::auto_ptr<bbs_detail::Base> bbs) {
57 regist(new BoardWindow(bbs));
60 BoardWindow::BoardWindow(std::auto_ptr<bbs_detail::Base> bbs) :
61 ApplicationFrameWork(), tree_view_(), bbs_(bbs), scrolled_(),
62 tree_model_(ThreadListModel::create()),
66 // additional menuitems for board window
67 action_group_->add(Gtk::Action::create("FileOpen", "_Open Thread"),
68 sigc::mem_fun(*this, &BoardWindow::on_action_file_open));
72 " <menubar name='MenuBar'>"
73 " <menu action='MenuFile'>"
74 " <menuitem action='FileOpen'/>"
77 " <popup name='MenuPopup'>"
78 " <menuitem action='FileOpen'/>"
82 ui_manager_->add_ui_from_string(ui);
85 tree_view_.signal_row_activated().connect(
86 sigc::mem_fun(*this, &BoardWindow::on_row_activated));
87 tree_view_.signal_button_press_event().connect_notify(
88 sigc::mem_fun(*this, &BoardWindow::on_child_button_press));
90 set_default_size(400,300);
92 scrolled_.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
94 scrolled_.add(tree_view_);
96 tree_view_.set_model(tree_model_);
97 tree_view_.set_fixed_height_mode(true);
98 tree_view_.set_rules_hint(true);
100 tree_view_.append_column(*Gtk::manage(new view_column::Number));
101 tree_view_.append_column(*Gtk::manage(new view_column::Title));
102 tree_view_.append_column(*Gtk::manage(new view_column::ResNum));
103 tree_view_.append_column(*Gtk::manage(new view_column::LineCount));
104 tree_view_.append_column(*Gtk::manage(new view_column::Average));
105 tree_view_.append_column(*Gtk::manage(new view_column::StartTime));
107 using namespace boost::posix_time;
108 ptime start = microsec_clock::local_time();
113 to_simple_string(microsec_clock::local_time() - start) << std::endl;
115 set_title(get_uri());
120 BoardWindow::~BoardWindow() {
123 void BoardWindow::on_row_activated(const Gtk::TreeModel::Path& path,
124 Gtk::TreeViewColumn* /*col*/) {
125 Gtk::TreeIter iter = tree_model_->get_iter(path);
127 iter->get_value(boost::mpl::find<model_column::List,
128 model_column::ID>::type::pos::value, id);
130 const std::string uri = bbs_->get_another_thread_uri(id);
131 uri_opener::open(uri);
134 void BoardWindow::on_action_view_stop() {
135 if (http_getter_) http_getter_->cancel();
138 bool BoardWindow::is_same(const bbs_detail::Base& bbs) const {
142 std::string BoardWindow::get_uri() const {
143 return bbs_->get_board_uri();
147 void BoardWindow::load() {
148 boost::unordered_map<model_column::ID::type, ModelColumns> buffer;
150 std::vector<ThreadIdxCache> caches;
152 const boost::filesystem::path dir(bbs_->get_board_idx_dir_path());
153 if (boost::filesystem::exists(dir) && boost::filesystem::is_directory(dir))
154 caches = dialektos::ThreadIdxCache::from_directory(dir);
156 BOOST_FOREACH(const ThreadIdxCache& cache, caches) {
158 model_column::field<model_column::Title>(cols) = cache.title_;
159 model_column::field<model_column::ID>(cols) = cache.id_;
160 model_column::field<model_column::LineCount>(cols) = cache.line_count_;
161 buffer[cache.id_] = cols;
164 const boost::filesystem::path sbj(bbs_->get_board_subject_path());
165 if (boost::filesystem::exists(sbj) && boost::filesystem::is_regular(sbj)) {
166 std::vector<SubjectItem> subjects;
167 bbs_->load_subject(subjects);
169 idx_ = SubjectIdx::from_xml(
170 boost::filesystem::path(bbs_->get_board_subject_idx_path()));
172 merge_subject_txt(subjects, buffer);
175 tree_model_->set_buffer(buffer);
178 void BoardWindow::merge_subject_txt(
179 const std::vector<SubjectItem>& subjects,
180 boost::unordered_map<std::string, ModelColumns>& buffer) {
182 boost::posix_time::ptime last_modified;
184 last_modified = rfc1123_to_ptime(idx_.last_modified_);
185 } catch (const HTTPDateError& e) {
186 std::cerr << e.what() << std::endl;
187 last_modified = boost::posix_time::second_clock::universal_time();
190 BOOST_FOREACH(const SubjectItem& item, subjects) {
191 ModelColumns& cols = buffer[item.id_];
192 model_column::field<model_column::Number>(cols) = item.number_;
193 model_column::field<model_column::Title>(cols) = item.title_;
194 model_column::field<model_column::ID>(cols) = item.id_;
195 model_column::field<model_column::ResNum>(cols) = item.res_num_;
198 const std::time_t _start = boost::lexical_cast<std::time_t>(item.id_);
199 const boost::posix_time::ptime start = boost::posix_time::from_time_t(_start);
200 const boost::posix_time::ptime curr = last_modified;
201 const boost::posix_time::time_duration dur = curr - start;
202 const double sec = dur.total_seconds();
203 const double average = item.res_num_ / (sec/60.0/60.0/24.0);
204 model_column::field<model_column::Average>(cols) = average;
205 } catch(const boost::bad_lexical_cast& e) {
206 std::cerr << e.what() << "(" << item.id_ << ")" << std::endl;
211 void BoardWindow::unmerge_subject_txt(
212 boost::unordered_map<std::string, ModelColumns>& buffer) {
213 // remove thread ids which line_count_ is zero.
214 std::list<std::string> remove_list;
215 typedef boost::unordered_map<std::string, ModelColumns>::value_type PairType;
216 BOOST_FOREACH(PairType& pair, buffer) {
217 ModelColumns& cols = pair.second;
218 if (model_column::field<model_column::LineCount>(cols) == 0)
219 remove_list.push_back(pair.first);
220 else if (model_column::field<model_column::Number>(cols) != 0) {
221 model_column::field<model_column::Number>(cols) = 0;
222 model_column::field<model_column::ResNum>(cols) = 0;
223 model_column::field<model_column::Average>(cols) = 0;
226 BOOST_FOREACH(const std::string& id, remove_list)
230 void BoardWindow::on_action_file_open() {
231 std::cout << "file open activated" << std::endl;
234 void BoardWindow::on_action_view_refresh() {
235 if (http_getter_) return;
237 statusbar_.push("HTTP/1.0 GET...");
239 const std::string uri = bbs_->get_board_subject_uri();
240 http::Header request_header = bbs_->get_board_subject_request_header();
242 SubjectIdx idx = SubjectIdx::from_xml(
243 boost::filesystem::path(bbs_->get_board_subject_idx_path()));
244 request_header.set_if_modified_since(idx.last_modified_);
246 http_getter_.reset(new http::GetInThread(uri, request_header));
247 http_getter_->signal_end().connect(
248 sigc::mem_fun(*this, &BoardWindow::on_http_get_end));
252 void BoardWindow::save_content(const http::Response& response) {
253 // save the content to the file subject.txt
254 if (!misc::create_directories(boost::filesystem::path(
255 bbs_->get_board_subject_path()).parent_path())) return;
256 std::ofstream ofs(bbs_->get_board_subject_path().c_str());
257 ofs << response.get_content();
260 // save the metadata to the file subject.xml
261 idx_.last_modified_ = response.get_header().get_last_modified();
262 idx_.to_xml(boost::filesystem::path(bbs_->get_board_subject_idx_path()));
265 void BoardWindow::on_http_get_end(bool success) {
266 // const std::string uri = http_getter_->get_uri();
267 // const http::Header request_header = http_getter_->get_request_header();
268 const http::Response response = http_getter_->get_response();
269 const boost::system::error_code err = http_getter_->get_error();
270 http_getter_.reset(0);
272 statusbar_.push(err.message());
276 statusbar_.push("Canceled.");
280 on_refresh_end(response.get_status_line(), response.get_header());
282 std::vector<SubjectItem> subjects;
283 bbs_->load_subject_from_string(response.get_content(), subjects);
285 if (subjects.empty()) return;
287 save_content(response);
289 boost::unordered_map<std::string, ModelColumns> buffer;
290 tree_model_->get_buffer(buffer);
291 unmerge_subject_txt(buffer);
292 merge_subject_txt(subjects, buffer);
293 tree_model_->set_buffer(buffer);
296 void BoardWindow::on_refresh_end(const http::StatusLine& status,
297 const http::Header& header) {
298 std::string message = status.get_line();
299 const std::string last_modified = header.get_last_modified();
300 if (!last_modified.empty()) {
302 message += last_modified;
304 statusbar_.push(message);
307 } // namespace dialektos