web.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*****************************************************************************
  2. * web.cpp - QStarDict, a StarDict clone written with 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. #include "web.h"
  20. #include <QBuffer>
  21. #include <QDir>
  22. #include <QEventLoop>
  23. #include <QFile>
  24. #include <QNetworkAccessManager>
  25. #include <QNetworkReply>
  26. #include <QSettings>
  27. #include <QUrl>
  28. #include <QTextCodec>
  29. #include "settingsdialog.h"
  30. Web::Web(QObject *parent)
  31. : QObject(parent)
  32. {
  33. }
  34. QStringList Web::availableDicts() const
  35. {
  36. QStringList result = QDir(workPath()).entryList(QStringList("*.webdict"), QDir::Files, QDir::Name);
  37. result.replaceInStrings(".webdict", "");
  38. return result;
  39. }
  40. void Web::setLoadedDicts(const QStringList &dicts)
  41. {
  42. for (QStringList::const_iterator i = dicts.begin(); i != dicts.end(); ++i)
  43. {
  44. QString filename = workPath() + "/" + *i + ".webdict";
  45. if (! QFile::exists(filename))
  46. continue;
  47. QSettings dict(filename, QSettings::IniFormat);
  48. QString query = dict.value("query").toString();
  49. if (! query.isEmpty())
  50. {
  51. m_loadedDicts[*i].query = query;
  52. m_loadedDicts[*i].codec = dict.value("charset").toByteArray();
  53. }
  54. }
  55. }
  56. Web::DictInfo Web::dictInfo(const QString &dict)
  57. {
  58. QString filename = workPath() + "/" + dict + ".webdict";
  59. if (! QFile::exists(filename))
  60. return DictInfo();
  61. QSettings dictFile(filename, QSettings::IniFormat);
  62. DictInfo info(name(), dict,
  63. dictFile.value("author").toString(),
  64. dictFile.value("description").toString());
  65. return info;
  66. }
  67. bool Web::isTranslatable(const QString &dict, const QString &word)
  68. {
  69. if (! m_loadedDicts.contains(dict))
  70. return false;
  71. // TODO
  72. Q_UNUSED(word);
  73. return true;
  74. }
  75. Web::Translation Web::translate(const QString &dict, const QString &word)
  76. {
  77. if (! m_loadedDicts.contains(dict))
  78. return Translation();
  79. QUrl url(m_loadedDicts[dict].query.replace("%s", word));
  80. QEventLoop loop;
  81. QNetworkAccessManager qnam;
  82. QNetworkReply *reply = qnam.get(QNetworkRequest(url));
  83. connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
  84. loop.exec();
  85. QTextCodec *codec = QTextCodec::codecForName(m_loadedDicts[dict].codec);
  86. QString translation;
  87. if (codec)
  88. translation = codec->toUnicode(reply->readAll());
  89. else
  90. translation = QString::fromUtf8(reply->readAll());
  91. return Translation(dict, word, translation);
  92. }
  93. int Web::execSettingsDialog(QWidget *parent)
  94. {
  95. ::SettingsDialog dialog(this, parent);
  96. return dialog.exec();
  97. }
  98. #if QT_VERSION < 0x050000
  99. Q_EXPORT_PLUGIN2(web, Web)
  100. #endif
  101. // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc