dictplugin.h 9.1 KB

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