code clean
[mdictionary] / src / plugins / google / GooglePlugin.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 GooglePlugin.h
23     \brief Implementation of google plugin's main class.
24
25     \author Jakub Jaszczynski <j.j.jaszczynski@gmail.com>
26 */
27
28 #ifndef GOOGLEPLUGIN_H
29 #define GOOGLEPLUGIN_H
30
31
32 #include <QObject>
33 #include <QDialog>
34 #include <QtPlugin>
35 #include <QIcon>
36 #include <QtNetwork>
37
38 #include "../../include/CommonDictInterface.h"
39 #include "../../include/settings.h"
40 #include "../../include/DictDialog.h"
41 #include "TranslationGoogle.h"
42 #include "GoogleDictDialog.h"
43
44 class GoogleDictDialog;
45
46 class GooglePlugin : public CommonDictInterface
47 {
48     Q_OBJECT
49     Q_INTERFACES(CommonDictInterface)
50 public:
51     GooglePlugin(QObject *parent=0);
52     ~GooglePlugin();
53
54     //! \returns source language code iso 639-2
55     QString langFrom() const;
56
57     //! \returns destination language code iso 639-2
58     QString langTo() const;
59
60     //! \returns dictionary name (like "old English" or so)
61     QString name() const;
62
63     //! \returns dictionary type (xdxf, google translate, etc)
64     QString type() const;
65
66     //! returns information about dictionary (name, authors, etc)
67     QString infoNote() const;
68
69     //! sets the language to which the translation is done
70     void setLangTo(QString langTo);
71
72     //! sets the language from which the translation is done
73     void setLangFrom(QString langFrom);
74
75     /*!
76         \returns DictDialog object that creates dialogs
77         for adding new dictionaries and changing plugin things
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     //! sets if connection with Internet is possible
88     void setConnectionAccept(QString connectionAcepted);
89
90     //! \returns the value of "connection_accepted" from settings
91     bool isConnectionAccept() const;
92
93     //! \returns a description of a word given by a QString
94     QString search(QString key);
95
96     //! \returns current plugin settings
97     Settings* settings();
98
99     //! Sets new settings
100     void setSettings(const Settings*);
101
102     //! \returns plugin icon
103     QIcon* icon();
104
105     //! \returns plugin icon's resource path
106     QString iconPath();
107
108     //! \returns empty translation object (to be fetched later) for a given key
109     Translation* getTranslationFor(QString key);
110
111     //! initializes the list of available languages in Google translator
112     static void initLanguages();
113
114     static QMap<QString, QString> languages;
115
116 public slots:
117     /*! performs search in a dictionary
118       \param  word word to search for in a dictionary
119       \param limit limit on number of results
120
121       After finishing search it has to emit
122       \see CommonDictInterface:finalTranslation  finalTranslation
123     */
124     QList<Translation*> searchWordList(QString word, int limit=0);
125
126     //! stops current operation
127     void stop();
128
129     //! function called after the request from Google is returned
130     void done();
131
132     //! transforms Google format to String with translation
133     QString jsonParse(QString result);
134
135     //! sets information about dictionary
136     void getDictionaryInfo();
137
138     //! loads translations for each plugin only once
139     void retranslate();
140 protected:
141     static bool noNetworkErrorShowed;
142
143 private:
144     //! name of a dictionary
145     QString _name;
146     //! type of a dictionary
147     QString _type;
148     //! information about dictionary
149     QString _infoNote;
150
151     //! icon displayed during translations and when a dictionary is chosen
152     QIcon _icon;
153     //! Path to icon
154     QString _iconPath;
155     Settings *_settings;
156     //! indicates if search is stopped
157     bool stopped;
158     bool _connectionAccept;
159     //! indicates if response from Google appeared
160     volatile bool wait;
161     QHttp *http;
162     GoogleDictDialog *_dictDialog;
163     QThread *threa;
164 };
165
166 #endif // GOOGLEPLUGIN_H
167
168