init
[qstardict] / plugins / dictplugin.h
1 /*****************************************************************************
2  * dictplugin.h - QStarDict, a StarDict clone written using Qt               *
3  * Copyright (C) 2008 Alexander Rodin                                        *
4  *                                                                           *
5  * This program is free software; you can redistribute it and/or modify      *
6  * it under the terms of the GNU General Public License as published by      *
7  * the Free Software Foundation; either version 2 of the License, or         *
8  * (at your option) any later version.                                       *
9  *                                                                           *
10  * This program is distributed in the hope that it will be useful,           *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
13  * GNU General Public License for more details.                              *
14  *                                                                           *
15  * You should have received a copy of the GNU General Public License along   *
16  * with this program; if not, write to the Free Software Foundation, Inc.,   *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
18  *****************************************************************************/
19
20 #ifndef DICTPLUGIN_H
21 #define DICTPLUGIN_H
22
23 #include <QtPlugin>
24 #include <QStringList>
25 #include <QDir>
26 #include <QCoreApplication>
27 #include <QVariant>
28
29 namespace QStarDict
30 {
31
32 /**
33  * This is a base class for all dictionary plugins classes.
34  */
35 class DictPlugin
36 {
37     public:
38         /**
39          * This enum describes a features of dictionary plugin.
40          */
41         enum Feature
42         {
43             /**
44              * No features.
45              */
46             None          = 0x00,
47             /**
48              * Dictionary plugin can search for similar words using
49              * fuzzy algoritms.
50              */
51             SearchSimilar = 0x01,
52             /**
53              * Dictionary plugin has a settings dialog.
54              */
55             SettingsDialog = 0x02,
56         };
57         Q_DECLARE_FLAGS(Features, Feature)
58
59         /**
60          * This class represents information about dictionary.
61          */
62         class DictInfo
63         {
64             public:
65                 /**
66                  * Construct empty DictInfo object.
67                  */
68                 DictInfo()
69                     : m_wordsCount(-1L)
70                 { }
71                 /**
72                  * Construct DictInfo object from data.
73                  * @param plugin A plugin name
74                  * @param name A dictionary name
75                  * @param author A dictionary author
76                  * @param desription A dictionary description
77                  * @param wordsCount A count of words that available in dictionary
78                  */
79                 DictInfo(const QString &plugin,
80                          const QString &name,
81                          const QString &author = QString(),
82                          const QString &description = QString(),
83                          long wordsCount = -1L)
84                     : m_plugin(plugin),
85                       m_name(name),
86                       m_author(author),
87                       m_description(description),
88                       m_wordsCount(wordsCount)
89                 { }
90
91                 const QString &plugin() const
92                 { return m_plugin; }
93                 const QString &name() const
94                 { return m_name; }
95                 const QString &author() const
96                 { return m_author; }
97                 const QString &description() const
98                 { return m_description; }
99                 long wordsCount() const
100                 { return m_wordsCount; }
101
102                 void setPlugin(const QString &plugin)
103                 { m_plugin = plugin; }
104                 void setName(const QString &name)
105                 { m_name = name; }
106                 void setAuthor(const QString &author)
107                 { m_author = author; }
108                 void setDescription(const QString &description)
109                 { m_description = description; }
110                 void setWordsCount(long wordsCount)
111                 { m_wordsCount = wordsCount; }
112
113             private:
114                 QString m_plugin;
115                 QString m_name;
116                 QString m_author;
117                 QString m_description;
118                 long m_wordsCount;
119         };
120
121         /**
122          * This class represent a translation.
123          */
124         class Translation
125         {
126             public:
127                 /**
128                  * Construct an empty translation.
129                  */
130                 Translation()
131                 { }
132
133                 /**
134                  * Construct a translation from data.
135                  * @param title A translation title
136                  * @param dictName A full dictionary name
137                  * @param translation A translation
138                  */
139                 Translation(const QString &title,
140                         const QString &dictName,
141                         const QString &translation)
142                     : m_title(title),
143                       m_dictName(dictName),
144                       m_translation(translation)
145                 { }
146
147                 /**
148                  * Return the translation title.
149                  */
150                 const QString &title() const
151                 { return m_title; }
152
153                 /**
154                  * Return the dictionary name.
155                  */
156                 const QString &dictName() const
157                 { return m_dictName; }
158
159                 /**
160                  * Return the translation.
161                  */
162                 const QString &translation() const
163                 { return m_translation; }
164
165                 /**
166                  * Set a translation title.
167                  */
168                 void setTitle(const QString &title)
169                 { m_title = title; }
170
171                 /**
172                  * Set a dictionary name.
173                  */
174                 void setDictName(const QString &dictName)
175                 { m_dictName = dictName; }
176
177                 /**
178                  * Set a translation.
179                  */
180                 void setTranslation(const QString &translation)
181                 { m_translation = translation; }
182
183             private:
184                 QString m_title;
185                 QString m_dictName;
186                 QString m_translation;
187         };
188
189         /**
190          * Destructor.
191          */
192         virtual ~DictPlugin() { }
193
194         /**
195          * Return the plugin name.
196          */
197         virtual QString name() const = 0;
198
199         /**
200          * Return the plugin version.
201          */
202         virtual QString version() const = 0;
203
204         /**
205          * Return the plugin description.
206          */
207         virtual QString description() const = 0;
208
209         /**
210          * Return the plugin authors.
211          */
212         virtual QStringList authors() const = 0;
213
214         /**
215          * Return a features supported by dictionary plugin.
216          */
217         virtual Features features() const
218         { return Features(None); }
219
220         /**
221          * Return a list of available dictionaries.
222          */
223         virtual QStringList availableDicts() const = 0;
224
225         /**
226          * Return a list of loaded dictionaries.
227          */
228         virtual QStringList loadedDicts() const = 0;
229
230         /**
231          * Set a list of loaded dictionaries.
232          */
233         virtual void setLoadedDicts(const QStringList &loadedDicts) = 0;
234
235         /**
236          * Return true if translation exists in dictionary,
237          * otherwise returns false.
238          */
239         virtual bool isTranslatable(const QString &dict, const QString &word) = 0;
240         /**
241          * Return translation for word from dictionary. If word not found
242          * returns empty string.
243          */
244         virtual Translation translate(const QString &dict, const QString &word) = 0;
245         /**
246          * Return a list of similar to "word" words from all loaded dictionaries.
247          * Works only if SearchSimilar feature is enabled.
248          */
249         virtual QStringList findSimilarWords(const QString &dict, const QString &word)
250         { Q_UNUSED(dict); return QStringList(word); }
251         
252         /**
253          * Return a required resource. Scheme of URLs:
254          *   plugin://plugin_name/...
255          */
256         virtual QVariant resource(int type, const QUrl &name)
257         { Q_UNUSED(type) Q_UNUSED(name) return QVariant(); }
258
259         /**
260          * Return an information about dictionary. The dictionary may be not loaded
261          * but available.
262          */
263         virtual DictInfo dictInfo(const QString &dict) = 0;
264
265         /**
266          * Run a settings dialog and return QDialog::DialogCode.
267          */
268         virtual int execSettingsDialog(QWidget *parent = 0)
269         { Q_UNUSED(parent); return 0; }
270
271     protected:
272         /**
273          * Return a directory that contains plugin's data.
274          */
275         QString workPath() const
276         {
277             QString path = QDir::homePath() + "/.config/qstardict/pluginsdata/" + name();
278
279             if (! QDir::root().exists(path))
280                 QDir::root().mkpath(path);
281             return path;
282         }
283 };
284
285 Q_DECLARE_OPERATORS_FOR_FLAGS(DictPlugin::Features)
286
287 }
288
289 Q_DECLARE_INTERFACE(QStarDict::DictPlugin, "org.qstardict.DictPlugin/1.0")
290
291 #endif // DICTPLUGIN_H
292
293 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent
294