Russian translation. Gui fix.
[qstardict] / qstardict / dictcore.h
1 /*****************************************************************************
2  * dictcore.h - QStarDict, a StarDict clone written with using Qt            *
3  * Copyright (C) 2007 Alexander Rodin                                        *
4  *                                                                           *
5  * This program 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 2 of the License, or         *
8  * (at your option) any later version.                                       *
9  *                                                                           *
10  * This program 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 along   *
16  * with this program; if not, write to the Free Software Foundation, Inc.,   *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.               *
18  *****************************************************************************/
19
20 #ifndef DICTCORE_H
21 #define DICTCORE_H
22
23 #include <QObject>
24
25 #include <QStringList>
26 #include <QPair>
27 #include <QHash>
28 #include <QPluginLoader>
29
30 #include "../plugins/dictplugin.h"
31
32
33 namespace QStarDict
34 {
35
36 /**
37  * The DictCore is a base dictionary class.
38  */
39 class DictCore: public QObject
40 {
41     Q_OBJECT
42
43     public:
44         /**
45          * This class represents a dictionary.
46          */
47         class Dictionary
48         {
49             public:
50                 Dictionary(const QString &plugin, const QString &name)
51                     : m_plugin(plugin),
52                       m_name(name)
53                 {  }
54                 Dictionary()
55                 { }
56                 
57                 const QString &plugin() const
58                 { return m_plugin; }
59                 const QString &name() const
60                 { return m_name; }
61                 void setPlugin(const QString &plugin)
62                 { m_plugin = plugin; }
63                 void setName(const QString &name)
64                 { m_name = name; }
65                 bool operator == (const Dictionary &dict)
66                 { return m_name == dict.m_name && m_plugin == dict.m_plugin; }
67
68             private:
69                 QString m_plugin;
70                 QString m_name;
71         };
72
73         /**
74          * Construct dictionary.
75          */
76         DictCore(QObject *parent = 0);
77         /**
78          * Destructor.
79          */
80         ~DictCore();
81
82         /**
83          * Returns true if word is exists in dictionaries,
84          * otherwise false.
85          */
86         bool isTranslatable(const QString &word);
87         /**
88          * Returns translation for word. If word not found, returns
89          * "Not found!"
90          */
91         QString translate(const QString &word);
92         /**
93          * Returns a list of similar words contained in dictionaries.
94          */
95         QStringList findSimilarWords(const QString &word);
96
97         /**
98          * Returns a list of available dictionary plugins.
99          */
100         QStringList availablePlugins() const;
101
102         /**
103          * Returns a list of loaded dictionary plugins.
104          */
105         QStringList loadedPlugins() const
106         { return QStringList(m_plugins.keys()); }
107
108         /**
109          * Sets a loaded plugins.
110          * If plugin cannot be loaded it will not be added to
111          * loadedPlugins list.
112          */
113         void setLoadedPlugins(const QStringList &loadedPlugins);
114
115         /**
116          * Returns a list of available dictionaries.
117          * The first item in pair is a plugin name, the second item
118          * in pair is a dictionary name.
119          */
120         QList<Dictionary> availableDicts() const;
121
122         /**
123          * Returns a list of loaded dictionaries. 
124          * The first item in pair is a plugin name, the second item
125          * in pair is a dictionary name.
126          */
127         const QList<Dictionary> &loadedDicts() const
128         { return m_loadedDicts; }
129
130         /**
131          * Sets a loaded dictionaries.
132          * The first item in pair is a plugin name, the second item
133          * in pair is a dictionary name.
134          * If dictionary cannot be loaded it will not be added to 
135          * availableDicts list.
136          */
137         void setLoadedDicts(const QList<Dictionary> &loadedDicts);
138
139         /**
140          * Reload loaded dicts.
141          */
142         void reloadDicts();
143
144         /**
145          * Returns pointer to plugin instance or 0 if not loaded.
146          */
147         DictPlugin *plugin(const QString &plugin)
148         { return m_plugins.contains(plugin) ? qobject_cast<DictPlugin*>(m_plugins[plugin]->instance()) : 0; }
149
150         /**
151          * Save settings.
152          */
153         void saveSettings();
154
155     private:
156
157         /**
158          * Load settings.
159          */
160         void loadSettings();
161
162         QHash<QString, QPluginLoader*> m_plugins;
163         QList<Dictionary> m_loadedDicts;
164 };
165
166 }
167
168 #endif // DICTCORE_H
169
170 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
171