1 |
t-suwa |
3 |
/* |
2 |
|
|
|
3 |
|
|
MacOS X implementation of the SKK input method. |
4 |
|
|
|
5 |
|
|
Copyright (C) 2008 Tomotaka SUWA <t.suwa@mac.com> |
6 |
|
|
|
7 |
|
|
This program 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 2 of the License, or |
10 |
|
|
any later version. |
11 |
|
|
|
12 |
|
|
This program 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 this program; if not, write to the Free Software |
19 |
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
|
21 |
|
|
*/ |
22 |
|
|
|
23 |
|
|
#include "SKKCommonDictionary.h" |
24 |
|
|
#include <sys/stat.h> |
25 |
|
|
|
26 |
|
|
// 非同期辞書ロードクラス |
27 |
|
|
class SKKCommonDictionaryLoader : public SKKDictionaryLoader { |
28 |
|
|
std::string path_; |
29 |
|
|
std::time_t lastupdate_; |
30 |
t-suwa |
26 |
bool first_; |
31 |
t-suwa |
3 |
|
32 |
|
|
virtual bool run() { |
33 |
|
|
SKKDictionaryFile tmp; |
34 |
|
|
|
35 |
|
|
if(updated() && tmp.Load(path_)) { |
36 |
|
|
tmp.Sort(); |
37 |
|
|
NotifyObserver(tmp); |
38 |
t-suwa |
26 |
} else { |
39 |
|
|
if(first_) { |
40 |
|
|
first_ = false; |
41 |
|
|
NotifyObserver(tmp); |
42 |
|
|
} |
43 |
|
|
} |
44 |
t-suwa |
3 |
|
45 |
|
|
return true; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
bool updated() { |
49 |
|
|
struct stat st; |
50 |
|
|
|
51 |
|
|
if(stat(path_.c_str(), &st) == 0 && lastupdate_ < st.st_mtime) { |
52 |
|
|
lastupdate_ = st.st_mtime; |
53 |
|
|
return true; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
return false; |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
public: |
60 |
t-suwa |
26 |
SKKCommonDictionaryLoader(const std::string& location) : path_(location), lastupdate_(0), first_(true) {} |
61 |
t-suwa |
3 |
}; |
62 |
|
|
|
63 |
|
|
// ====================================================================== |
64 |
|
|
|
65 |
|
|
SKKCommonDictionary::SKKCommonDictionary() : loader_(0) {} |
66 |
|
|
|
67 |
|
|
void SKKCommonDictionary::Initialize(const std::string& path) { |
68 |
|
|
if(loader_.get()) return; |
69 |
|
|
|
70 |
|
|
loader_ = std::auto_ptr<SKKCommonDictionaryLoader>(new SKKCommonDictionaryLoader(path)); |
71 |
|
|
keeper_.Initialize(loader_.get(), 60, 1); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
std::string SKKCommonDictionary::FindOkuriAri(const std::string& query) { |
75 |
|
|
return keeper_.FindOkuriAri(query); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
std::string SKKCommonDictionary::FindOkuriNasi(const std::string& query) { |
79 |
|
|
return keeper_.FindOkuriNasi(query); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
bool SKKCommonDictionary::FindCompletions(const std::string& entry, std::vector<std::string>& result) { |
83 |
|
|
return keeper_.FindCompletions(entry, result); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
// ファクトリメソッドの登録 |
87 |
|
|
#include "SKKDictionaryFactory.h" |
88 |
|
|
static bool initialize = SKKRegisterFactoryMethod<SKKCommonDictionary>(SKKCommonDictionaryType); |