init
[qstardict] / plugins / swac / swac.cpp
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
21 #include "swac.h"
22
23 #include <QMessageBox>
24 #include <QStringList>
25 #include <QSqlDatabase>
26 #include <QSqlQuery>
27 #include <QString>
28 #include <QVariant>
29
30 Swac::Swac(QObject *parent) : QObject(parent)
31 {
32     db = new QSqlDatabase();
33     *db = QSqlDatabase::addDatabase("QSQLITE", QLatin1String("swac"));
34     db->setDatabaseName(QDir::homePath() + "/.swac/swac.db");
35     db->open();
36 }
37
38 Swac::~Swac()
39 {
40     db->close();
41     delete db;
42     QSqlDatabase::removeDatabase("swac");
43 }
44
45 QStringList Swac::availableDicts() const
46 {
47     QStringList result;
48     QSqlQuery query = db->exec("SELECT packid FROM packages;");
49     while (query.next())
50     {
51         result << query.value(0).toString();
52     }
53     return result;
54 }
55
56 void Swac::setLoadedDicts(const QStringList &dicts)
57 {
58     QStringList available = Swac::availableDicts();
59     m_loadedDicts.clear();
60     for (QStringList::const_iterator i = dicts.begin(); i != dicts.end(); ++i)
61     {
62         if (available.contains(*i))
63             m_loadedDicts << *i;
64     }
65 }
66
67 Swac::DictInfo Swac::dictInfo(const QString &dict)
68 {
69     QSqlQuery query = db->exec("SELECT name, format, version, organization, readme FROM packages WHERE packid = \'" + dict + "\' LIMIT 1;");
70
71     if (query.first())
72         return DictInfo(query.value(0).toString(), dict, query.value(3).toString(), "<pre>" + query.value(4).toString() + "</pre>");
73     else
74         return DictInfo("", dict, "", "");
75 }
76
77
78 QSqlQuery Swac::search(const QString &dict, const QString &word, const QString &fields, const int limit) {
79     QSqlQuery query(*db);
80     query.prepare(
81         "SELECT " + fields + " "
82         + "FROM alphaidx" + " "
83         + "INNER JOIN sounds ON alphaidx.sounds_idx = sounds.idx "
84         + "INNER JOIN packages ON sounds.packages_idx = packages.idx "
85         + "WHERE packages.packid = ?1 AND alphaidx.str = ?2 "
86         + "LIMIT " + QString::number(limit) +";" 
87     );
88     query.addBindValue(dict);
89     query.addBindValue(word);
90     query.exec();
91     return query;
92 }
93
94
95
96 bool Swac::isTranslatable(const QString &dict, const QString &word)
97 {
98     QSqlQuery query = search(dict, word, "SWAC_TEXT", 1);
99     return query.first();
100 }
101
102 Swac::Translation Swac::translate(const QString &dict, const QString &word)
103 {
104     QSqlQuery query = search(dict, word, "SWAC_TEXT, packages.path, filename, SWAC_SPEAK_NAME", 128);
105     QString article("");
106     int i = 0;
107     while (query.next())
108     {
109         if (i > 0)
110             article += "<br/>\n";
111         article += "<img src=':/icons/sound.png'/> &nbsp; <a href=\"" + query.value(1).toString() + query.value(2).toString() + "\">" + query.value(0).toString() + "</a>";
112         i++;
113     }
114
115     return Translation(word, dict, article);
116 }
117
118 QStringList Swac::findSimilarWords(const QString &dict, const QString &word)
119 {
120     return QStringList();
121 }
122
123 int Swac::execSettingsDialog(QWidget *parent)
124 {
125     return QMessageBox::information(parent, "SWAC Plugin for QStarDict",
126                             "To install new packages, please, use the <b>swac-get</b> command line program.\n"
127                             "More information about swac-get is available on <a href='http://shtooka.net/'>Shtooka Project Homepage</A>." );
128 }
129
130 Q_EXPORT_PLUGIN2(swac, Swac)
131
132 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
133