OSDN Git Service

namesapce communicationを追加
[gikomona/libcore.git] / src / model.cpp
1 #include <boost/filesystem/path.hpp>
2 #include <boost/algorithm/string/split.hpp>
3
4 #include "model.hpp"
5
6 namespace monazilla { namespace GikoMona { namespace core {
7
8 struct model::model_pimpl {
9     // session/tab-window
10     /* 構造
11      * bbs-name(TEXT)|board-name(TEXT)|thread-id(TEXT)|thread-name(TEXT)|is_fixed(INTEGAR)
12      */
13     database tab_db;
14     
15     // session/history
16     /* 構造
17      * date(TEXT)|bbs-name(TEXT)|board-name(TEXT)|thread-id(TEXT)|thread-name(TEXT)
18      */
19     database history_db;
20     
21     // application
22     config app_config;
23     
24     struct {
25         const mona_string tab_db = "session.tab-window.db";
26         const mona_string history_db = "session.history.db";
27         const mona_string linkref_db = "session.linkrefs.db";
28         const mona_string app_config = "application-config.xml";
29     } file_name;
30 };
31
32 model::model() noexcept {
33     instance = this;
34     pimpl = std::make_shared<model_pimpl>();
35     
36     auto config_path = pimpl->app_config;
37 }
38
39 bool model::load_file(const boost::filesystem::path& file_path) {
40     if(!boost::filesystem::exists(file_path)) {
41         return false;
42     }
43     
44     auto ext = file_path.extension();
45     auto file_name = file_path.filename();
46     
47     /* 依存型が欲しいなぁって */
48     if(ext == "db") {
49         // sqlite -> class `database'
50         if(file_name == pimpl->file_name.tab_db) {
51             pimpl->tab_db.get_connection().open(file_path.c_str());
52         } else if(file_name == pimpl->file_name.history_db) {
53             pimpl->history_db.get_connection().open(file_path.c_str());
54         }
55     } else if(ext == "xml") {
56         // xml -> class `config'
57         pimpl->app_config;
58     }
59     
60     return true;
61 }
62
63 bool model::save_to_file(const boost::filesystem::path& path) {
64     if(!boost::filesystem::exists(path)) { return false; }
65     
66     auto ext = path.extension();
67     
68     if(ext == "db") {
69         // caprice::sqlitexx::connection は open の時点でファイルの作成を完了している。
70         // さらに、connection に対する動作は全て db に書き込まれているので、単に true を返すに留める。
71         return true;
72     }
73     
74     pimpl->app_config;
75     return true;
76 }
77
78 void model::execute_accumulated_query()  {
79     inserted_value_triv_copyable_type q;
80     
81     if(insertion_query_queue.empty()) { return; }
82     
83     insertion_query_queue.pop(q);
84
85     mona_string into;
86     boost::any value;
87     std::tie(*q, into, value);
88     
89     std::vector<mona_string> path_derimed = analyze_query(into);
90     
91     if(path_derimed[0] != "application") {
92         // application/*
93     } else if (path_derimed[0] == "session") {
94         // session/*
95         if(path_derimed[1] == "history") {
96             ;
97         } else if(path_derimed[1] == "tab-window") {
98             ;
99         }
100     } else if(path_derimed[0] == "extension") {
101         // extension/*
102     } else {
103         // ?
104     }
105 }
106
107 boost::any model::select(const mona_string& from, bool tag) const noexcept {
108     std::vector<mona_string> path_derimed = analyze_query(from);
109     
110     if(path_derimed[0] != "application") {
111         // application/*
112     } else if (path_derimed[0] == "session") {
113         // session/*
114         if(path_derimed[1] == "history") {
115             ;
116         } else if(path_derimed[1] == "tab-window") {
117             ;
118         }
119     } else if(path_derimed[0] == "extension") {
120         // extension/*
121     } else {
122         // ?
123     }
124 }
125
126 std::vector<mona_string> model::analyze_query(const mona_string& src) const {
127     std::vector<mona_string> result;
128     boost::algorithm::split(result,
129                             src,
130                             [](char c) -> bool {
131                                 if(c == '/') return true;
132                                 return false;
133                             });
134     
135     return std::move(result); // RVO を期待した方が良いか…?
136 }
137
138 } } }