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