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