1 /*****************************************************************************
2 * settingsdialog.cpp - QStarDict, a StarDict clone written 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 *****************************************************************************/
20 #include "settingsdialog.h"
25 #include <QListWidgetItem>
27 #include "ui_adddictionarydialog.h"
31 QStringList supportedCharsets()
33 QList<QByteArray> list = QTextCodec::availableCodecs();
35 for (QList<QByteArray>::const_iterator i = list.begin(); i != list.end(); ++i)
41 SettingsDialog::SettingsDialog(Web *plugin, QWidget *parent)
47 QStringList filenames = QDir(plugin->workPath()).entryList(QStringList("*.webdict"), QDir::Files, QDir::Name);
48 for (QStringList::iterator i = filenames.begin(); i != filenames.end(); ++i)
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());
56 dictsList->setProperty("FingerScrollable", true);
61 void SettingsDialog::on_editDictButton_clicked()
63 if (dictsList->currentRow() == -1)
65 QString dict = dictsList->currentItem()->text();
66 Ui::AddDictionaryDialog ui;
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)
78 if (ui.nameEdit->text() != dict)
81 dict = ui.nameEdit->text();
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();
90 void SettingsDialog::on_addDictButton_clicked()
92 Ui::AddDictionaryDialog ui;
95 ui.charsetEdit->addItems(supportedCharsets());
96 ui.charsetEdit->setCurrentIndex(ui.charsetEdit->findText("UTF-8"));
97 if (dialog.exec() != QDialog::Accepted)
99 m_dicts[ui.nameEdit->text()] =
100 Dict(ui.authorEdit->text(), ui.descEdit->toPlainText(), ui.queryEdit->text());
104 void SettingsDialog::on_removeDictButton_clicked()
106 if(dictsList->count()) {
107 QListWidgetItem *item = dictsList->takeItem(dictsList->currentRow());
109 m_dicts.remove(item->text());
114 void SettingsDialog::refresh()
117 dictsList->insertItems(0, m_dicts.keys());
120 void SettingsDialog::accept()
122 for (QHash<QString, Dict>::const_iterator i = m_dicts.begin(); i != m_dicts.end(); ++i)
124 QSettings dict(m_plugin->workPath() + "/" + i.key() + ".webdict", QSettings::IniFormat);
125 dict.setValue("author", i->author);
126 dict.setValue("description", i->description);
127 dict.setValue("query", i->query);
128 dict.setValue("charset", i->charset);
129 m_oldDicts.remove(i.key());
131 for (QHash<QString, Dict>::const_iterator i = m_oldDicts.begin(); i != m_oldDicts.end(); ++i)
132 QFile::remove(m_plugin->workPath() + "/" + i.key() + ".webdict");
137 void SettingsDialog::closeEvent(QCloseEvent *event)
139 SettingsDialog::accept();
142 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc