084f8eeb241674bb384588e02abf901c0d0eea52
[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 STARDICTPLUGIN_H
26 #define STARDICTPLUGIN_H
27
28
29 #include <QObject>
30 #include <QDialog>
31 #include <QRegExp>
32 #include <QTime>
33 #include <QFile>
34 //#include <QXmlStreamReader>
35 #include <QtPlugin>
36 #include <QHash>
37 #include <QIcon>
38 #include <QtEndian>
39
40 #include "../../include/CommonDictInterface.h"
41 #include "../../include/settings.h"
42 #include "StarDictDialog.h"
43 #include "TranslationStarDict.h"
44
45 class TranslationXdxf;
46
47 class StarDictPlugin : public CommonDictInterface
48 {
49     Q_OBJECT
50     Q_INTERFACES(CommonDictInterface)
51 public:
52     StarDictPlugin(QObject *parent=0);
53
54     ~StarDictPlugin();
55
56     //! \returns source language code iso 639-2
57     QString langFrom() const;
58
59     //! \returns destination language code iso 639-2
60     QString langTo() const;
61
62     //! \returns dictionary name (like "old English" or so)
63     QString name() const;
64
65     //! \returns dictionary type (xdxf, google translate, etc)
66     QString type() const;
67
68     //! returns information about dictionary in xml (name, authors, etc)
69     QString infoNote() const;
70
71     /*! \returns DictDialog object that creates dialogs
72         for adding a new dictionary and changing plugin settings
73       */
74     DictDialog* dictDialog();
75
76     //! \returns new, clean copy of plugin with settings set as in Settings*
77     CommonDictInterface* getNew(const Settings*) const;
78
79     //! \returns whether plugin can start searching
80     bool isAvailable() const;
81
82     //! \returns a description of a word given by a QString
83     QString search(QString key) {
84         return search(key, 0, 0);
85     }
86
87     /*! \return a description of a word given by a QString
88         \param offset offset of translation to be cut out
89         \param len lenght of translation to be cut out
90         */
91     QString search(QString key, qint64 offset, qint32 len);
92
93     //! \returns current plugin settings
94     Settings* settings();
95
96     //! \returns words count in a dictionary
97     long wordsCount();
98
99     //! Sets new settings
100     bool setSettings(const Settings*);
101
102     //! \returns plugin icon
103     QIcon* icon();
104
105     /*! plugin should delete any files (eg. cache) that have been created and are ready
106         to be deleted
107         */
108     void clean();
109
110
111
112 public Q_SLOTS:
113     /*! performs search in a dictionary
114       \param  word word to search for in a dictionary
115       \param limit limit on number of results
116
117       After finishing search it has to emit
118       \see CommonDictInterface:finalTranslation  finalTranslation
119     */
120     QList<Translation*> searchWordList(QString word, int limit=0);
121
122     //! stops current operation
123     void stop();
124
125     //! loads translations for each plugin only once
126     void retranslate();
127
128
129 private:
130
131     /*! \returns true or false depending on whether the dictionary is cached
132         or not
133      */
134     bool isCached();
135     /*! searches for a list of words similar to a word in a database file
136     \param word key compared with keys in a database
137     \param limit limits the number of translations in returned list,
138            0 means unlimited
139     \returns list of translations
140     */
141     QList<Translation*> searchWordListCache(QString word, int limit=0);
142
143     /*! searches for a list of words similar to a word in a xdxf file
144     \param word key compared with keys in a xdxf file
145     \param limit limits the number of translations in returned list,
146            0 means unlimited
147     \returns list of translations
148     */
149     QList<Translation*> searchWordListFile(QString word, int limit=0);
150
151     /*! searches for a translation of a word which is exactly like a key
152         in a xdxf file */
153     QString searchFile(QString key);
154
155     //! scans dictionary file to get information about it
156     bool getDictionaryInfo();
157
158     //! counts the keys in a xdxf file
159     int countWords();
160
161
162     //! language from which we translate
163     QString _langFrom;
164     //! language to which we translate
165     QString _langTo;
166     //! name of a dictionary
167     QString _name;
168     //! information about dictionary
169     QString _infoNote;
170
171     QString _dictionaryInfo;
172
173     //! icon displayed during translations and when a dictionary is chosen
174     QIcon _icon;
175
176     //! number of words in a dictionary
177     long _wordsCount;
178     //! indicates if search is stopped
179     volatile bool stopped;
180     Settings *_settings;
181     StarDictDialog* _dictDialog;
182     Settings* _ifoFileSettings;
183
184     /*! Reads and process (converting to qstring) data from StarDict dictionary
185         file (*.dict[.dz])
186         \return converted translation
187         \param QByteArray raw data to process
188         \param mode StarDict parametr "sametypesequence" */
189     QString format(QByteArray, QString mode,QString key);
190
191     /*! Reads bytes bytes of data or reads until \0
192         \param it iterator to given data
193         \param end end of data
194         \param bytes to read
195         \return readed data chunk*/
196     QByteArray read(QByteArray::iterator it, QByteArray::iterator end,
197            int bytes = 0);
198
199     /*! Interpret data basis on mode (StarDict dict data type)
200         \param it iterator on given data set
201         \param end iterator pointing to the data end
202         \param mode stardict dict data type
203         \param last used to interpret sametypesequence field last letter (see
204                 StarDict format description)
205         \return QSting containing interpreted data chunk */
206     QString interpret(QByteArray::iterator it, QByteArray::iterator end,
207             QChar mode,QString key, bool last = false);
208
209     friend class StarDictTests;
210 };
211
212 #endif // XDXFPLUGIN_H
213
214