settingsdialog.cpp 4.7 KB

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