1 // OVKPPhraseTools.h: Phrase management tool
2 //
3 // Copyright (c) 2004-2006 The OpenVanilla Project (http://openvanilla.org)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // 1. Redistributions of source code must retain the above copyright
11 //    notice, this list of conditions and the following disclaimer.
12 // 2. Redistributions in binary form must reproduce the above copyright
13 //    notice, this list of conditions and the following disclaimer in the
14 //    documentation and/or other materials provided with the distribution.
15 // 3. Neither the name of OpenVanilla nor the names of its contributors
16 //    may be used to endorse or promote products derived from this software
17 //    without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 
31 #ifndef __OVKPPhraseTool_h
32 #define __OVKPPhraseTool_h
33 
34 // #define OV_DEBUG
35 #include <stdlib.h>
36 #include <string.h>
37 #include <ctype.h>
38 #include <time.h>
39 #include <string>
40 #include <OpenVanilla/OpenVanilla.h>
41 #include <OpenVanilla/OVLibrary.h>
42 #include <OpenVanilla/OVUtility.h>
43 #include "OVSQLite3.h"
44 #include "PTKeySequence.h"
45 #include "PTCandidateList.h"
46 
47 using namespace std;
48 
49 #define PT_DEFKEY       "~"
50 #define PT_CATCHKEY     "["
51 #define PT_DBNAME       "userphrase-0.7.2.0.db"
52 #define PT_DBTBLCREATE  "create table phrase(key, value, context, time, freq);"
53 #define PT_DBIDXCREATE  "create index phrase_index_key on phrase(key);"
54 
55 enum {
56     PTS_WAIT=0,
57     PTS_MODE_ACTIVATED=1,
58     PTS_MODE_COMMAND=2,
59     PTS_MODE_CANDIDATE=3
60 };
61 
62 class OVKPPhraseTools;
63 
64 class OVKPPhraseToolsContext : public OVInputMethodContext {
65 public:
66     OVKPPhraseToolsContext(OVKPPhraseTools *p);
67     virtual void start(OVBuffer*, OVCandidate*, OVService*);
68     virtual void clear();
69     virtual void end();
70     virtual int keyEvent(OVKeyCode*, OVBuffer*, OVCandidate*, OVService*);
71 
72 protected:
73     virtual bool clearBufCandi();   // clear buffer and candidate, always true
74     virtual bool composeAndUpdateBuffer();  // always returns true
75     virtual bool state_activated();
76     virtual bool state_command();
77     virtual bool state_candidate();
78     virtual bool parse_command();
79 
80     virtual bool command_add(const string& k);
81     virtual bool command_search(const string& k, bool wcard=false);
82 
83     // displays end message (mode ended)
84     virtual bool cancelmsg(bool returnvalue);
85     virtual bool cancelmsg(int newstate, bool returnvalue);
86 
87     OVKPPhraseTools *parent;
88 
89     int state;
90     PTKeySequence keyseq;
91 
92     bool wildcard;
93     vector<string>candilist;
94     vector<string>valuelist;
95     PTCandidateList candictrl;
96 
97     OVBuffer *b;
98     OVKeyCode *k;
99     OVCandidate *c;
100     OVService *s;
101 };
102 
103 class OVKPPhraseTools : public OVInputMethod {
104 public:
105     OVKPPhraseTools(bool &cflag, PTKeySequence &wseq, SQLite3 *pdb);
106 
moduleType()107     virtual const char *moduleType() { return "OVKeyPreprocessor"; }
identifier()108     virtual const char *identifier() { return "OVKPPhraseTools"; }
109     virtual const char *localizedName(const char*);
110     virtual int initialize(OVDictionary*, OVService*, const char*);
111     virtual void update(OVDictionary*, OVService*);
112     virtual OVInputMethodContext* newContext();
113 
114 protected:
115     bool &recordingMode;
116     PTKeySequence &recSeq;
117     SQLite3 *db;
118 
119     int actkey;     // key to invoke the tools
120     int catchkey;   // (after phrase tools on) key to start text recorder
121     string selkey;  // selection key (e.g. 1234567890)
122 
123     friend class OVKPPhraseToolsContext;
124 };
125 
126 class OVOFTextRecorder : public OVOutputFilter {
127 public:
128     OVOFTextRecorder(bool &cflag, PTKeySequence& wseq);
129 
identifier()130     virtual const char *identifier() { return "OVOFTextRecorder"; }
131     virtual const char *localizedName(const char*);
132     virtual const char *process(const char*, OVService*);
133 
134 protected:
135     bool &recordingMode;
136     PTKeySequence &recSeq;
137 };
138 
139 #endif
140