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