swac.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*****************************************************************************
  2. * This file is a part of QStarDict, a StarDict clone written with using Qt *
  3. * swac.cpp - Plugin for words audio collections SWAC *
  4. * Copyright (C) 2008 Nicolas Vion <nico@picapo.net> *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License along *
  17. * with this program; if not, write to the Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  19. *****************************************************************************/
  20. #include "swac.h"
  21. #include <QMessageBox>
  22. #include <QStringList>
  23. #include <QSqlDatabase>
  24. #include <QSqlQuery>
  25. #include <QString>
  26. #include <QVariant>
  27. Swac::Swac(QObject *parent) : QObject(parent)
  28. {
  29. db = new QSqlDatabase();
  30. *db = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("swac"));
  31. db->setDatabaseName(QDir::homePath() + "/.swac/swac.db");
  32. db->open();
  33. }
  34. Swac::~Swac()
  35. {
  36. db->close();
  37. delete db;
  38. QSqlDatabase::removeDatabase("swac");
  39. }
  40. QStringList Swac::availableDicts() const
  41. {
  42. QStringList result;
  43. QSqlQuery query = db->exec("SELECT packid FROM packages;");
  44. while (query.next())
  45. {
  46. result << query.value(0).toString();
  47. }
  48. return result;
  49. }
  50. void Swac::setLoadedDicts(const QStringList &dicts)
  51. {
  52. QStringList available = Swac::availableDicts();
  53. m_loadedDicts.clear();
  54. for (QStringList::const_iterator i = dicts.begin(); i != dicts.end(); ++i)
  55. {
  56. if (available.contains(*i))
  57. m_loadedDicts << *i;
  58. }
  59. }
  60. Swac::DictInfo Swac::dictInfo(const QString &dict)
  61. {
  62. QSqlQuery query = db->exec("SELECT name, format, version, organization, readme FROM packages WHERE packid = \'" + dict + "\' LIMIT 1;");
  63. if (query.first())
  64. return DictInfo(query.value(0).toString(), dict, query.value(3).toString(), "<pre>" + query.value(4).toString() + "</pre>");
  65. else
  66. return DictInfo("", dict, "", "");
  67. }
  68. QSqlQuery Swac::search(const QString &dict, const QString &word, const QString &fields, const int limit) {
  69. QSqlQuery query(*db);
  70. query.prepare(
  71. "SELECT " + fields + " "
  72. + "FROM alphaidx" + " "
  73. + "INNER JOIN sounds ON alphaidx.sounds_idx = sounds.idx "
  74. + "INNER JOIN packages ON sounds.packages_idx = packages.idx "
  75. + "WHERE packages.packid = ?1 AND alphaidx.str = ?2 "
  76. + "LIMIT " + QString::number(limit) +";"
  77. );
  78. query.addBindValue(dict);
  79. query.addBindValue(word);
  80. query.exec();
  81. return query;
  82. }
  83. bool Swac::isTranslatable(const QString &dict, const QString &word)
  84. {
  85. QSqlQuery query = search(dict, word, "SWAC_TEXT", 1);
  86. return query.first();
  87. }
  88. Swac::Translation Swac::translate(const QString &dict, const QString &word)
  89. {
  90. QSqlQuery query = search(dict, word, "SWAC_TEXT, packages.path, filename, SWAC_SPEAK_NAME", 128);
  91. QString article("");
  92. int i = 0;
  93. while (query.next())
  94. {
  95. if (i > 0)
  96. article += "<br/>\n";
  97. article += "<img src=':/icons/sound.png'/> &nbsp; <a href=\"" + query.value(1).toString() + query.value(2).toString() + "\">" + query.value(0).toString() + "</a>";
  98. i++;
  99. }
  100. return Translation(word, dict, article);
  101. }
  102. QStringList Swac::findSimilarWords(const QString &dict, const QString &word)
  103. {
  104. return QStringList();
  105. }
  106. int Swac::execSettingsDialog(QWidget *parent)
  107. {
  108. return QMessageBox::information(parent, "SWAC Plugin for QStarDict",
  109. "To install new packages, please, use the <b>swac-get</b> command line program.\n"
  110. "More information about swac-get is available on <a href='http://shtooka.net/'>Shtooka Project Homepage</A>." );
  111. }
  112. #if QT_VERSION < 0x050000
  113. Q_EXPORT_PLUGIN2(swac, Swac)
  114. #endif
  115. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc