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