dictplugin.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*****************************************************************************
  2. * dictplugin.h - QStarDict, a StarDict clone written using Qt *
  3. * Copyright (C) 2008 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. #ifndef DICTPLUGIN_H
  20. #define DICTPLUGIN_H
  21. #include <QtPlugin>
  22. #include <QStringList>
  23. #include <QDir>
  24. #include <QCoreApplication>
  25. #include <QVariant>
  26. namespace QStarDict
  27. {
  28. /**
  29. * This is a base class for all dictionary plugins classes.
  30. */
  31. class DictPlugin
  32. {
  33. public:
  34. /**
  35. * This enum describes a features of dictionary plugin.
  36. */
  37. enum Feature
  38. {
  39. /**
  40. * No features.
  41. */
  42. None = 0x00,
  43. /**
  44. * Dictionary plugin can search for similar words using
  45. * fuzzy algoritms.
  46. */
  47. SearchSimilar = 0x01,
  48. /**
  49. * Dictionary plugin has a settings dialog.
  50. */
  51. SettingsDialog = 0x02,
  52. };
  53. Q_DECLARE_FLAGS(Features, Feature)
  54. /**
  55. * This class represents information about dictionary.
  56. */
  57. class DictInfo
  58. {
  59. public:
  60. /**
  61. * Construct empty DictInfo object.
  62. */
  63. DictInfo()
  64. : m_wordsCount(-1L)
  65. { }
  66. /**
  67. * Construct DictInfo object from data.
  68. * @param plugin A plugin name
  69. * @param name A dictionary name
  70. * @param author A dictionary author
  71. * @param desription A dictionary description
  72. * @param wordsCount A count of words that available in dictionary
  73. */
  74. DictInfo(const QString &plugin,
  75. const QString &name,
  76. const QString &author = QString(),
  77. const QString &description = QString(),
  78. long wordsCount = -1L)
  79. : m_plugin(plugin),
  80. m_name(name),
  81. m_author(author),
  82. m_description(description),
  83. m_wordsCount(wordsCount)
  84. { }
  85. const QString &plugin() const
  86. { return m_plugin; }
  87. const QString &name() const
  88. { return m_name; }
  89. const QString &author() const
  90. { return m_author; }
  91. const QString &description() const
  92. { return m_description; }
  93. long wordsCount() const
  94. { return m_wordsCount; }
  95. void setPlugin(const QString &plugin)
  96. { m_plugin = plugin; }
  97. void setName(const QString &name)
  98. { m_name = name; }
  99. void setAuthor(const QString &author)
  100. { m_author = author; }
  101. void setDescription(const QString &description)
  102. { m_description = description; }
  103. void setWordsCount(long wordsCount)
  104. { m_wordsCount = wordsCount; }
  105. private:
  106. QString m_plugin;
  107. QString m_name;
  108. QString m_author;
  109. QString m_description;
  110. long m_wordsCount;
  111. };
  112. /**
  113. * This class represent a translation.
  114. */
  115. class Translation
  116. {
  117. public:
  118. /**
  119. * Construct an empty translation.
  120. */
  121. Translation()
  122. { }
  123. /**
  124. * Construct a translation from data.
  125. * @param title A translation title
  126. * @param dictName A full dictionary name
  127. * @param translation A translation
  128. */
  129. Translation(const QString &title,
  130. const QString &dictName,
  131. const QString &translation)
  132. : m_title(title),
  133. m_dictName(dictName),
  134. m_translation(translation)
  135. { }
  136. /**
  137. * Return the translation title.
  138. */
  139. const QString &title() const
  140. { return m_title; }
  141. /**
  142. * Return the dictionary name.
  143. */
  144. const QString &dictName() const
  145. { return m_dictName; }
  146. /**
  147. * Return the translation.
  148. */
  149. const QString &translation() const
  150. { return m_translation; }
  151. /**
  152. * Set a translation title.
  153. */
  154. void setTitle(const QString &title)
  155. { m_title = title; }
  156. /**
  157. * Set a dictionary name.
  158. */
  159. void setDictName(const QString &dictName)
  160. { m_dictName = dictName; }
  161. /**
  162. * Set a translation.
  163. */
  164. void setTranslation(const QString &translation)
  165. { m_translation = translation; }
  166. private:
  167. QString m_title;
  168. QString m_dictName;
  169. QString m_translation;
  170. };
  171. /**
  172. * Destructor.
  173. */
  174. virtual ~DictPlugin() { }
  175. /**
  176. * Return the plugin name.
  177. */
  178. virtual QString name() const = 0;
  179. /**
  180. * Return the plugin version.
  181. */
  182. virtual QString version() const = 0;
  183. /**
  184. * Return the plugin description.
  185. */
  186. virtual QString description() const = 0;
  187. /**
  188. * Return the plugin authors.
  189. */
  190. virtual QStringList authors() const = 0;
  191. /**
  192. * Return a features supported by dictionary plugin.
  193. */
  194. virtual Features features() const
  195. { return Features(None); }
  196. /**
  197. * Return a list of available dictionaries.
  198. */
  199. virtual QStringList availableDicts() const = 0;
  200. /**
  201. * Return a list of loaded dictionaries.
  202. */
  203. virtual QStringList loadedDicts() const = 0;
  204. /**
  205. * Set a list of loaded dictionaries.
  206. */
  207. virtual void setLoadedDicts(const QStringList &loadedDicts) = 0;
  208. /**
  209. * Return true if translation exists in dictionary,
  210. * otherwise returns false.
  211. */
  212. virtual bool isTranslatable(const QString &dict, const QString &word) = 0;
  213. /**
  214. * Return translation for word from dictionary. If word not found
  215. * returns empty string.
  216. */
  217. virtual Translation translate(const QString &dict, const QString &word) = 0;
  218. /**
  219. * Return a list of similar to "word" words from all loaded dictionaries.
  220. * Works only if SearchSimilar feature is enabled.
  221. */
  222. virtual QStringList findSimilarWords(const QString &dict, const QString &word)
  223. { Q_UNUSED(dict); return QStringList(word); }
  224. /**
  225. * Return a required resource. Scheme of URLs:
  226. * plugin://plugin_name/...
  227. */
  228. virtual QVariant resource(int type, const QUrl &name)
  229. { Q_UNUSED(type) Q_UNUSED(name) return QVariant(); }
  230. /**
  231. * Return an information about dictionary. The dictionary may be not loaded
  232. * but available.
  233. */
  234. virtual DictInfo dictInfo(const QString &dict) = 0;
  235. /**
  236. * Run a settings dialog and return QDialog::DialogCode.
  237. */
  238. virtual int execSettingsDialog(QWidget *parent = 0)
  239. { Q_UNUSED(parent); return 0; }
  240. protected:
  241. /**
  242. * Return a directory that contains plugin's data.
  243. */
  244. QString workPath() const
  245. {
  246. QString path = QDir::homePath() + "/.config/qstardict/pluginsdata/" + name();
  247. if (! QDir::root().exists(path))
  248. QDir::root().mkpath(path);
  249. return path;
  250. }
  251. };
  252. Q_DECLARE_OPERATORS_FOR_FLAGS(DictPlugin::Features)
  253. }
  254. Q_DECLARE_INTERFACE(QStarDict::DictPlugin, "org.qstardict.DictPlugin/1.0")
  255. #endif // DICTPLUGIN_H
  256. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent