334a5060680640d51f1589837ddcecd1898eaff3
[mdictionary] / src / plugins / stardict / StarDictPlugin.h
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary 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 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary 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
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22
23 /*! \file xdxfplugin.h
24 */
25 #ifndef XDXFPLUGIN_H
26 #define XDXFPLUGIN_H
27
28
29 #include <QObject>
30 #include <QDialog>
31 #include <QRegExp>
32 #include <QTime>
33 #include <QSqlQuery>
34 #include <QSqlDatabase>
35 #include <QSqlError>
36 #include <QFile>
37 #include <QXmlStreamReader>
38 #include <QtPlugin>
39 #include <QHash>
40
41 #include "../../include/CommonDictInterface.h"
42 #include "../../include/settings.h"
43 #include "XdxfDictDialog.h"
44 #include "XdxfCachingDialog.h"
45 #include "TranslationXdxf.h"
46
47 class TranslationXdxf;
48
49 class XdxfPlugin : public CommonDictInterface
50 {
51     Q_OBJECT
52     Q_INTERFACES(CommonDictInterface)
53 public:
54     XdxfPlugin(QObject *parent=0);
55
56     ~XdxfPlugin();
57
58     //! \returns source language code iso 639-2
59     QString langFrom() const;
60
61     //! \returns destination language code iso 639-2
62     QString langTo() const;
63
64     //! \returns dictionary name (like "old English" or so)
65     QString name() const;
66
67     //! \returns dictionary type (xdxf, google translate, etc)
68     QString type() const;
69
70     //! returns information about dictionary in xml (name, authors, etc)
71     QString infoNote() const;
72
73     /*! \returns DictDialog object that creates dialogs
74         for adding a new dictionary and changing plugin settings
75       */
76     DictDialog* dictDialog();
77
78     //! \returns new, clean copy of plugin with settings set as in Settings*
79     CommonDictInterface* getNew(const Settings*) const;
80
81     //! \returns whether plugin can start searching
82     bool isAvailable() const;
83
84     //! \returns a description of a word given by a QString
85     QString search(QString key);
86
87     //! \returns current plugin settings
88     Settings* settings();
89
90     //! \returns words count in a dictionary
91     long wordsCount();
92
93     //! Sets new settings
94     bool setSettings(const Settings*);
95
96     //! \returns plugin icon
97     QIcon* icon();
98
99     /*! plugin should delete any files (eg. cache) that have been created and are ready
100         to be deleted
101         */
102     void clean();
103
104
105
106 public Q_SLOTS:
107     /*! performs search in a dictionary
108       \param  word word to search for in a dictionary
109       \param limit limit on number of results
110
111       After finishing search it has to emit
112       \see CommonDictInterface:finalTranslation  finalTranslation
113     */
114     QList<Translation*> searchWordList(QString word, int limit=0);
115
116     //! stops current operation
117     void stop();
118
119     //! loads translations for each plugin only once
120     void retranslate();
121
122 Q_SIGNALS:
123     //! emitted with percent count of caching progress, and time elapsed from
124     //! last signal emit
125     void updateCachingProgress(int, int);
126
127
128 private:
129
130     /*! \returns true or false depending on whether the dictionary is cached
131         or not
132      */
133     bool isCached();
134     /*! searches for a list of words similar to a word in a database file
135     \param word key compared with keys in a database
136     \param limit limits the number of translations in returned list,
137            0 means unlimited
138     \returns list of translations
139     */
140     QList<Translation*> searchWordListCache(QString word, int limit=0);
141
142     /*! searches for a list of words similar to a word in a xdxf file
143     \param word key compared with keys in a xdxf file
144     \param limit limits the number of translations in returned list,
145            0 means unlimited
146     \returns list of translations
147     */
148     QList<Translation*> searchWordListFile(QString word, int limit=0);
149
150     /*! searches for a translation of a word which is exactly like a key
151         in a xdxf file */
152     QString searchFile(QString key);
153
154     /*! searches for a translation of a word which is exactly like a key
155         in a database file */
156     QString searchCache(QString key);
157
158     //! scans dictionary file to get information about it
159     bool getDictionaryInfo();
160
161     //! counts the keys in a xdxf file
162     int countWords();
163
164     /*! transforms xdxf files to database files (caching operation)
165         \returns true on success, false on failure */
166     bool makeCache(QString dir);
167
168     //! language from which we translate
169     QString _langFrom;
170     //! language to which we translate
171     QString _langTo;
172     //! name of a dictionary
173     QString _name;
174     //! information about dictionary
175     QString _infoNote;
176
177     QString _dictionaryInfo;
178
179     //! icon displayed during translations and when a dictionary is chosen
180     QIcon _icon;
181     QSqlDatabase db;
182     QString db_name;
183     //! number of words in a dictionary
184     long _wordsCount;
185     //! indicates if search is stopped
186     volatile bool stopped;
187     Settings *_settings;
188     XdxfDictDialog* _dictDialog;
189     XdxfCachingDialog* cachingDialog;
190 };
191
192 #endif // XDXFPLUGIN_H
193
194