-
Notifications
You must be signed in to change notification settings - Fork 570
/
memory.cc
136 lines (122 loc) · 3.76 KB
/
memory.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// Copyleft 2013 RIME Developers
// License: GPLv3
//
// 2013-01-02 GONG Chen <chen.sst@gmail.com>
//
#include <boost/bind.hpp>
#include <boost/foreach.hpp>
#include <rime/candidate.h>
#include <rime/context.h>
#include <rime/composition.h>
#include <rime/engine.h>
#include <rime/key_event.h>
#include <rime/schema.h>
#include <rime/ticket.h>
#include <rime/dict/dictionary.h>
#include <rime/dict/user_dictionary.h>
#include <rime/gear/memory.h>
#include <rime/gear/translator_commons.h>
namespace rime {
void CommitEntry::Clear() {
text.clear();
code.clear();
elements.clear();
}
void CommitEntry::AppendPhrase(const shared_ptr<Phrase>& phrase) {
text += phrase->text();
code.insert(code.end(), phrase->code().begin(), phrase->code().end());
shared_ptr<Sentence> sentence = As<Sentence>(phrase);
if (sentence) {
BOOST_FOREACH(const DictEntry& e, sentence->components()) {
elements.push_back(&e);
}
}
else {
elements.push_back(&phrase->entry());
}
}
bool CommitEntry::Save() const {
if (memory && !empty()) {
DLOG(INFO) << "memorize commit entry: " << text;
return memory->Memorize(*this);
}
return false;
}
Memory::Memory(const Ticket& ticket) {
if (!ticket.engine) return;
Dictionary::Component *dictionary = Dictionary::Require("dictionary");
if (dictionary) {
dict_.reset(dictionary->Create(ticket));
if (dict_)
dict_->Load();
}
UserDictionary::Component *user_dictionary =
UserDictionary::Require("user_dictionary");
if (user_dictionary) {
user_dict_.reset(user_dictionary->Create(ticket));
if (user_dict_) {
user_dict_->Load();
if (dict_)
user_dict_->Attach(dict_->table(), dict_->prism());
}
}
Context* ctx = ticket.engine->context();
commit_connection_ = ctx->commit_notifier().connect(
boost::bind(&Memory::OnCommit, this, _1));
delete_connection_ = ctx->delete_notifier().connect(
boost::bind(&Memory::OnDeleteEntry, this, _1));
unhandled_key_connection_ = ctx->unhandled_key_notifier().connect(
boost::bind(&Memory::OnUnhandledKey, this, _1, _2));
}
Memory::~Memory() {
commit_connection_.disconnect();
delete_connection_.disconnect();
unhandled_key_connection_.disconnect();
}
void Memory::OnCommit(Context* ctx) {
if (!user_dict_|| user_dict_->readonly())
return;
user_dict_->NewTransaction();
CommitEntry commit_entry(this);
BOOST_FOREACH(Composition::value_type &seg, *ctx->composition()) {
shared_ptr<Phrase> phrase =
As<Phrase>(Candidate::GetGenuineCandidate(seg.GetSelectedCandidate()));
bool recognized = phrase && phrase->language() == language();
if (recognized) {
commit_entry.AppendPhrase(phrase);
}
if (!recognized || seg.status >= Segment::kConfirmed) {
commit_entry.Save();
commit_entry.Clear();
}
}
}
void Memory::OnDeleteEntry(Context* ctx) {
if (!user_dict_ ||
user_dict_->readonly() ||
!ctx ||
ctx->composition()->empty())
return;
Segment &seg(ctx->composition()->back());
shared_ptr<Phrase> phrase =
As<Phrase>(Candidate::GetGenuineCandidate(seg.GetSelectedCandidate()));
bool recognized = phrase && phrase->language() == language();
if (recognized) {
const DictEntry& entry(phrase->entry());
LOG(INFO) << "deleting entry: '" << entry.text << "'.";
user_dict_->UpdateEntry(entry, -1); // mark as deleted in user dict
ctx->RefreshNonConfirmedComposition();
}
}
void Memory::OnUnhandledKey(Context* ctx, const KeyEvent& key) {
if (!user_dict_ || user_dict_->readonly()) return;
if ((key.modifier() & ~kShiftMask) == 0) {
if (key.keycode() == XK_BackSpace &&
user_dict_->RevertRecentTransaction()) {
return; // forget about last commit
}
user_dict_->CommitPendingTransaction();
}
}
} // namespace rime