File indexing completed on 2023-05-30 10:42:09

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool...
0003     SPDX-FileCopyrightText: 2005 Paul Temple <paul.temple@gmx.net>
0004     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0005     SPDX-FileCopyrightText: 2006 Eric Kjeldergaard <kjelderg@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #include "configdictionaryselector.h"
0011 
0012 #include <KConfigSkeleton>
0013 
0014 #include <QFileDialog>
0015 #include <QStringList>
0016 
0017 ConfigDictionarySelector::ConfigDictionarySelector(const QString &dictionaryName, QWidget *parent, KConfigSkeleton *config, Qt::WindowFlags f)
0018     : QWidget(parent, f)
0019 {
0020     setupUi(this);
0021     _dictName = dictionaryName;
0022     _config = config;
0023 
0024     connect(addButton, &QPushButton::clicked, this, &ConfigDictionarySelector::addDictSlot);
0025     connect(delButton, &QPushButton::clicked, this, &ConfigDictionarySelector::deleteDictSlot);
0026     __useGlobal->setObjectName(QString("kcfg_" + _dictName + "__useGlobal"));
0027 }
0028 
0029 // Read from preferences to the active list
0030 void ConfigDictionarySelector::updateWidgets()
0031 {
0032     QString groupName = "dicts_" + _dictName;
0033     KConfigGroup group = _config->config()->group(groupName);
0034     QStringList names = group.readEntry("__NAMES", QStringList());
0035 
0036     fileList->clear();
0037 
0038     foreach (const QString &it, names) {
0039         QStringList newRow(it);
0040         newRow << group.readEntry(it, QString());
0041         (void)new QTreeWidgetItem(fileList, newRow);
0042     }
0043 }
0044 
0045 void ConfigDictionarySelector::updateSettings()
0046 {
0047     QStringList names;
0048 
0049     KConfigGroup group = _config->config()->group("dicts_" + _dictName.toLower());
0050 
0051     for (int i = 0; i < fileList->topLevelItemCount(); i++) {
0052         QTreeWidgetItem *it = fileList->topLevelItem(i);
0053         QString dictionaryName = it->text(0);
0054         QString dictionaryPath = it->text(1);
0055         names.append(dictionaryName);
0056 
0057         group.writeEntry(dictionaryName, dictionaryPath);
0058     }
0059 
0060     // This feels distinctly hackish to me... :(
0061     _config->findItem(_dictName + "__NAMES")->setProperty(names);
0062     _config->save();
0063 }
0064 
0065 void ConfigDictionarySelector::updateWidgetsDefault()
0066 {
0067     // no default for custom edict list or
0068     // should we really delete all items in the list?
0069 }
0070 
0071 bool ConfigDictionarySelector::isDefault()
0072 {
0073     // no default for custom edict list or
0074     // should we really delete all items in the list?
0075     return true;
0076 }
0077 
0078 bool ConfigDictionarySelector::hasChanged()
0079 {
0080     return false;
0081 }
0082 
0083 void ConfigDictionarySelector::addDictSlot()
0084 {
0085     QTreeWidgetItem *item = fileList->topLevelItem(0);
0086 
0087     QString filename = QFileDialog::getOpenFileName(nullptr, QString(), item ? QFileInfo(item->text(1)).absolutePath().append("/") : QString());
0088     QString name = QFileInfo(filename).fileName();
0089     if (filename.isNull())
0090         return;
0091 
0092     QStringList newRow(name);
0093     newRow << filename;
0094     (void)new QTreeWidgetItem(fileList, newRow);
0095 
0096     updateSettings();
0097     Q_EMIT widgetChanged();
0098 }
0099 
0100 void ConfigDictionarySelector::deleteDictSlot()
0101 {
0102     foreach (QTreeWidgetItem *file, fileList->selectedItems()) {
0103         if (!file) {
0104             return;
0105         }
0106 
0107         delete file;
0108 
0109         updateSettings();
0110         Q_EMIT widgetChanged();
0111     }
0112 }