OSDN Git Service

* {init_ / done_} core() に新たにapplicationの名前を表す引数を追加
[gikomona/libcore.git] / src / GikoMona.cpp
1 #include <exception>
2 #include <memory>
3 #include <sstream>
4
5 #include <boost/filesystem.hpp>
6 #include <boost/log/trivial.hpp>
7
8 #include <wx/xrc/xmlres.h>
9 #include <wx/msgdlg.h>
10
11 #include "GikoMona.hpp"
12 #include "database.hpp"
13 #include "model.hpp"
14 #include "post-office.hpp"
15
16 #include "extension.hpp"
17
18 namespace monazilla { namespace GikoMona { namespace core {
19
20 namespace {
21
22 std::shared_ptr<model> app_model;
23 std::shared_ptr<extension> app_extension;
24 std::shared_ptr<communication::post_office> app_postoffice;
25
26 }
27
28 bool init_core(const mona_string& app_name) {
29     wxXmlResource::Get()->InitAllHandlers();
30     wxXmlResource::Get()->LoadAllFiles(wxT("resouce"));
31     
32     boost::system::error_code reason;
33     std::stringstream path_builder;
34     path_builder << ".tmp/" << app_name;
35     
36     // 一時解凍ファイルなどを溜め込む .tmp フォルダを作成
37     if(!boost::filesystem::exists(path_builder.str(), reason) &&
38        !boost::filesystem::create_directories(path_builder.str(), reason)) {
39         // log
40         std::terminate();
41     }
42     
43     app_model = std::make_shared<model>();
44     app_extension = std::make_shared<extension>();
45     app_postoffice = std::make_shared<communication::post_office>(app_name);
46     
47     return true;
48 }
49
50 void done_core(const mona_string& app_name) noexcept {
51     boost::system::error_code reason;
52     boost::filesystem::remove_all(".tmp", reason);
53     
54     optimize_database("./history.db");
55 }
56
57 void optimize_database(const boost::filesystem::path& db_path) {
58     boost::system::error_code ec;
59     
60     database db(db_path, ec);
61     sqlite::connection con = db.get_connection();
62
63     mona_string sql(u8"SELECT counts-of-deleting-value FROM file-information;");
64     auto count = sqlite::execute_statement<int>(db, sql, ec);
65
66     if(!count) { /* log */ }
67     
68     if(count->at<0>(0) >= 1000) {
69         db.optimize(ec);
70     }
71 }
72
73 } } }