File indexing completed on 2024-12-01 03:29:21

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 using namespace Qt::StringLiterals;
0018 
0019 ConfigDictionarySelector::ConfigDictionarySelector(const QString &dictionaryName, QWidget *parent, KConfigSkeleton *config, Qt::WindowFlags f)
0020     : QWidget(parent, f)
0021 {
0022     setupUi(this);
0023     _dictName = dictionaryName;
0024     _config = config;
0025 
0026     connect(addButton, &QPushButton::clicked, this, &ConfigDictionarySelector::addDictSlot);
0027     connect(delButton, &QPushButton::clicked, this, &ConfigDictionarySelector::deleteDictSlot);
0028     __useGlobal->setObjectName(QString("kcfg_"_L1 + _dictName + "__useGlobal"_L1));
0029 }
0030 
0031 // Read from preferences to the active list
0032 void ConfigDictionarySelector::updateWidgets()
0033 {
0034     QString groupName = "dicts_"_L1 + _dictName;
0035     KConfigGroup group = _config->config()->group(groupName);
0036     QStringList names = group.readEntry("__NAMES"_L1, QStringList());
0037 
0038     fileList->clear();
0039 
0040     for (const QString &it : names) {
0041         QStringList newRow(it);
0042         newRow << group.readEntry(it, QString());
0043         (void)new QTreeWidgetItem(fileList, newRow);
0044     }
0045 }
0046 
0047 void ConfigDictionarySelector::updateSettings()
0048 {
0049     QStringList names;
0050 
0051     KConfigGroup group = _config->config()->group("dicts_"_L1 + _dictName.toLower());
0052 
0053     for (int i = 0; i < fileList->topLevelItemCount(); i++) {
0054         QTreeWidgetItem *it = fileList->topLevelItem(i);
0055         QString dictionaryName = it->text(0);
0056         QString dictionaryPath = it->text(1);
0057         names.append(dictionaryName);
0058 
0059         group.writeEntry(dictionaryName, dictionaryPath);
0060     }
0061 
0062     // This feels distinctly hackish to me... :(
0063     _config->findItem(_dictName + "__NAMES"_L1)->setProperty(names);
0064     _config->save();
0065 }
0066 
0067 void ConfigDictionarySelector::updateWidgetsDefault()
0068 {
0069     // no default for custom edict list or
0070     // should we really delete all items in the list?
0071 }
0072 
0073 bool ConfigDictionarySelector::isDefault()
0074 {
0075     // no default for custom edict list or
0076     // should we really delete all items in the list?
0077     return true;
0078 }
0079 
0080 bool ConfigDictionarySelector::hasChanged()
0081 {
0082     return false;
0083 }
0084 
0085 void ConfigDictionarySelector::addDictSlot()
0086 {
0087     QTreeWidgetItem *item = fileList->topLevelItem(0);
0088 
0089     QString filename = QFileDialog::getOpenFileName(nullptr, QString(), item ? QFileInfo(item->text(1)).absolutePath().append(QStringLiteral("/")) : QString());
0090     QString name = QFileInfo(filename).fileName();
0091     if (filename.isNull())
0092         return;
0093 
0094     QStringList newRow(name);
0095     newRow << filename;
0096     (void)new QTreeWidgetItem(fileList, newRow);
0097 
0098     updateSettings();
0099     Q_EMIT widgetChanged();
0100 }
0101 
0102 void ConfigDictionarySelector::deleteDictSlot()
0103 {
0104     for (QTreeWidgetItem *file : fileList->selectedItems()) {
0105         if (!file) {
0106             return;
0107         }
0108 
0109         delete file;
0110 
0111         updateSettings();
0112         Q_EMIT widgetChanged();
0113     }
0114 }
0115 
0116 #include "moc_configdictionaryselector.cpp"