OSDN Git Service

Set window title.
[fukui-no-namari/dialektos.git] / src / board_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 "board_window.hxx"
22
23 #include <glibmm/ustring.h>
24 #include <glibmm/refptr.h>
25 #include <sigc++/functors/mem_fun.h>
26 #include <sigc++/signal.h>
27
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>
35
36 #include <string>
37 #include <fstream>
38 #include <iostream>
39 #include <sstream>
40
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"
50 #include "misc.hxx"
51
52
53 namespace dialektos {
54
55
56 void BoardWindow::create(std::auto_ptr<bbs_detail::Base> bbs) {
57   regist(new BoardWindow(bbs));
58 }
59
60 BoardWindow::BoardWindow(std::auto_ptr<bbs_detail::Base> bbs) :
61   ApplicationFrameWork(), tree_view_(), bbs_(bbs), scrolled_(),
62   tree_model_(ThreadListModel::create()),
63   http_getter_()
64   {
65
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));
69
70   Glib::ustring ui =
71     "<ui>"
72     "  <menubar name='MenuBar'>"
73     "    <menu action='MenuFile'>"
74     "      <menuitem action='FileOpen'/>"
75     "    </menu>"
76     "  </menubar>"
77     "  <popup name='MenuPopup'>"
78     "    <menuitem action='FileOpen'/>"
79     "  </popup>"
80     "</ui>";
81
82   ui_manager_->add_ui_from_string(ui);
83
84
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));
89
90   set_default_size(400,300);
91
92   scrolled_.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
93   add(scrolled_);
94   scrolled_.add(tree_view_);
95
96   tree_view_.set_model(tree_model_);
97   tree_view_.set_fixed_height_mode(true);
98   tree_view_.set_rules_hint(true);
99
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));
106
107   using namespace boost::posix_time;
108   ptime start = microsec_clock::local_time();
109
110   load();
111
112   std::cout <<
113   to_simple_string(microsec_clock::local_time() - start) << std::endl;
114
115   set_title(get_uri());
116
117   show_all();
118 }
119
120 BoardWindow::~BoardWindow() {
121 }
122
123 void BoardWindow::on_row_activated(const Gtk::TreeModel::Path& path,
124     Gtk::TreeViewColumn* /*col*/) {
125   Gtk::TreeIter iter = tree_model_->get_iter(path);
126   std::string id;
127   iter->get_value(boost::mpl::find<model_column::List,
128       model_column::ID>::type::pos::value, id);
129
130   const std::string uri = bbs_->get_another_thread_uri(id);
131   uri_opener::open(uri);
132 }
133
134 void BoardWindow::on_action_view_stop() {
135   if (http_getter_) http_getter_->cancel();
136 }
137
138 bool BoardWindow::is_same(const bbs_detail::Base& bbs) const {
139   return *bbs_ == bbs;
140 }
141
142 std::string BoardWindow::get_uri() const {
143   return bbs_->get_board_uri();
144 }
145
146
147 void BoardWindow::load() {
148   boost::unordered_map<model_column::ID::type, ModelColumns> buffer;
149
150   std::vector<ThreadIdxCache> caches;
151
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);
155
156   BOOST_FOREACH(const ThreadIdxCache& cache, caches) {
157     ModelColumns cols;
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;
162   }
163
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);
168
169     idx_ = SubjectIdx::from_xml(
170         boost::filesystem::path(bbs_->get_board_subject_idx_path()));
171
172     merge_subject_txt(subjects, buffer);
173   }
174
175   tree_model_->set_buffer(buffer);
176 }
177
178 void BoardWindow::merge_subject_txt(
179     const std::vector<SubjectItem>& subjects,
180     boost::unordered_map<std::string, ModelColumns>& buffer) {
181
182   boost::posix_time::ptime last_modified;
183   try {
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();
188   }
189
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_;
196
197     try {
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;
207     }
208   }
209 }
210
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;
224     }
225   }
226   BOOST_FOREACH(const std::string& id, remove_list)
227     buffer.erase(id);
228 }
229
230 void BoardWindow::on_action_file_open() {
231   std::cout << "file open activated" << std::endl;
232 }
233
234 void BoardWindow::on_action_view_refresh() {
235   if (http_getter_) return;
236
237   statusbar_.push("HTTP/1.0 GET...");
238
239   const std::string uri = bbs_->get_board_subject_uri();
240   http::Header request_header = bbs_->get_board_subject_request_header();
241
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_);
245
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));
249   http_getter_->run();
250 }
251
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();
258   ofs.close();
259
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()));
263 }
264
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);
271   if (err) {
272     statusbar_.push(err.message());
273     return;
274   }
275   if (!success) {
276     statusbar_.push("Canceled.");
277     return;
278   }
279
280   on_refresh_end(response.get_status_line(), response.get_header());
281
282   std::vector<SubjectItem> subjects;
283   bbs_->load_subject_from_string(response.get_content(), subjects);
284
285   if (subjects.empty()) return;
286
287   save_content(response);
288
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);
294 }
295
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()) {
301     message += " ";
302     message += last_modified;
303   }
304   statusbar_.push(message);
305 }
306
307 } // namespace dialektos