init
[qstardict] / plugins / web / settingsdialog.cpp
1 /*****************************************************************************
2  * settingsdialog.cpp - QStarDict, a StarDict clone written 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 "settingsdialog.h"
21
22 #include <QFile>
23 #include <QDir>
24 #include <QSettings>
25 #include <QListWidgetItem>
26 #include <QTextCodec>
27 #include "ui_adddictionarydialog.h"
28
29 namespace
30 {
31 QStringList supportedCharsets()
32 {
33     QList<QByteArray> list = QTextCodec::availableCodecs();
34     QStringList newList;
35     for (QList<QByteArray>::const_iterator i = list.begin(); i != list.end(); ++i)
36         newList << *i;
37     return newList;
38 }
39 }
40
41 SettingsDialog::SettingsDialog(Web *plugin, QWidget *parent)
42     : QDialog(parent),
43       m_plugin(plugin)
44 {
45     setupUi(this);
46     
47     QStringList filenames = QDir(plugin->workPath()).entryList(QStringList("*.webdict"), QDir::Files, QDir::Name);
48     for (QStringList::iterator i = filenames.begin(); i != filenames.end(); ++i)
49     {
50         QSettings dict(plugin->workPath() + "/" + *i, QSettings::IniFormat);
51         m_oldDicts[i->remove(".webdict")] = 
52             Dict(dict.value("author").toString(), dict.value("description").toString(),
53             dict.value("query").toString(), dict.value("charset").toByteArray());
54     }
55     m_dicts = m_oldDicts;
56     dictsList->setProperty("FingerScrollable", true);
57
58     refresh();
59 }
60
61 void SettingsDialog::on_editDictButton_clicked()
62 {
63     if (dictsList->currentRow() == -1)
64         return;
65     QString dict = dictsList->currentItem()->text();
66     Ui::AddDictionaryDialog ui;
67     QDialog dialog(this);
68     ui.setupUi(&dialog);
69     dialog.setWindowTitle(tr("Edit dictionary"));
70     ui.nameEdit->setText(dict);
71     ui.authorEdit->setText(m_dicts[dict].author);
72     ui.descEdit->setText(m_dicts[dict].description);
73     ui.queryEdit->setText(m_dicts[dict].query);
74     ui.charsetEdit->addItems(supportedCharsets());
75     ui.charsetEdit->setCurrentIndex(ui.charsetEdit->findText(m_dicts[dict].charset));
76     if (dialog.exec() != QDialog::Accepted)
77         return;
78     if (ui.nameEdit->text() != dict)
79     {
80         m_dicts.remove(dict);
81         dict = ui.nameEdit->text();
82     }
83     m_dicts[dict].author = ui.authorEdit->text();
84     m_dicts[dict].description = ui.descEdit->toPlainText();
85     m_dicts[dict].query = ui.queryEdit->text();
86     m_dicts[dict].charset = ui.charsetEdit->currentText().toAscii();
87     refresh();
88 }
89
90 void SettingsDialog::on_addDictButton_clicked()
91 {
92     Ui::AddDictionaryDialog ui;
93     QDialog dialog(this);
94     ui.setupUi(&dialog);
95     ui.charsetEdit->addItems(supportedCharsets());
96     ui.charsetEdit->setCurrentIndex(ui.charsetEdit->findText("UTF-8"));
97     if (dialog.exec() != QDialog::Accepted)
98         return;
99     m_dicts[ui.nameEdit->text()] =
100         Dict(ui.authorEdit->text(), ui.descEdit->toPlainText(), ui.queryEdit->text());
101     refresh();
102 }
103
104 void SettingsDialog::on_removeDictButton_clicked()
105 {
106     QListWidgetItem *item = dictsList->takeItem(dictsList->currentRow());
107     m_dicts.remove(item->text());
108     delete item;
109 }
110
111 void SettingsDialog::refresh()
112 {
113     dictsList->clear();
114     dictsList->insertItems(0, m_dicts.keys());
115 }
116
117 void SettingsDialog::accept()
118 {
119     for (QHash<QString, Dict>::const_iterator i = m_dicts.begin(); i != m_dicts.end(); ++i)
120     {
121         QSettings dict(m_plugin->workPath() + "/" + i.key() + ".webdict", QSettings::IniFormat);
122         dict.setValue("author", i->author);
123         dict.setValue("description", i->description);
124         dict.setValue("query", i->query);
125         dict.setValue("charset", i->charset);
126         m_oldDicts.remove(i.key());
127     }
128     for (QHash<QString, Dict>::const_iterator i = m_oldDicts.begin(); i != m_oldDicts.end(); ++i)
129         QFile::remove(m_plugin->workPath() + "/" + i.key() + ".webdict");
130
131     QDialog::accept();
132 }
133
134 void SettingsDialog::closeEvent(QCloseEvent *event)
135 {
136     SettingsDialog::accept();
137 }
138
139 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
140