1 /*****************************************************************************
2 * web.cpp - QStarDict, a StarDict clone written with using Qt *
3 * Copyright (C) 2008 Alexander Rodin *
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. *
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. *
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 *****************************************************************************/
30 #include "settingsdialog.h"
32 Web::Web(QObject *parent)
37 QStringList Web::availableDicts() const
39 QStringList result = QDir(workPath()).entryList(QStringList("*.webdict"), QDir::Files, QDir::Name);
40 result.replaceInStrings(".webdict", "");
44 void Web::setLoadedDicts(const QStringList &dicts)
46 for (QStringList::const_iterator i = dicts.begin(); i != dicts.end(); ++i)
48 QString filename = workPath() + "/" + *i + ".webdict";
49 if (! QFile::exists(filename))
51 QSettings dict(filename, QSettings::IniFormat);
52 QString query = dict.value("query").toString();
53 if (! query.isEmpty())
55 m_loadedDicts[*i].query = query;
56 m_loadedDicts[*i].codec = dict.value("charset").toByteArray();
61 Web::DictInfo Web::dictInfo(const QString &dict)
63 QString filename = workPath() + "/" + dict + ".webdict";
64 if (! QFile::exists(filename))
66 QSettings dictFile(filename, QSettings::IniFormat);
67 DictInfo info(name(), dict,
68 dictFile.value("author").toString(),
69 dictFile.value("description").toString());
73 bool Web::isTranslatable(const QString &dict, const QString &word)
75 if (! m_loadedDicts.contains(dict))
82 Web::Translation Web::translate(const QString &dict, const QString &word)
84 if (! m_loadedDicts.contains(dict))
86 QUrl url(m_loadedDicts[dict].query.replace("%s", word));
88 QHttp http(url.host(), url.port(80), &loop);
89 connect(&http, SIGNAL(done(bool)), &loop, SLOT(quit()));
90 http.get(url.path() + "?" + url.encodedQuery());
92 QTextCodec *codec = QTextCodec::codecForName(m_loadedDicts[dict].codec);
95 translation = codec->toUnicode(http.readAll());
97 translation = QString::fromUtf8(http.readAll());
98 return Translation(dict, word, translation);
101 int Web::execSettingsDialog(QWidget *parent)
103 ::SettingsDialog dialog(this, parent);
104 return dialog.exec();
107 Q_EXPORT_PLUGIN2(web, Web)
109 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc