File indexing completed on 2025-01-19 12:59:19
0001 /************************************************************************ 0002 * * 0003 * This file is part of Kooka, a scanning/OCR application using * 0004 * Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>. * 0005 * * 0006 * Copyright (C) 2000-2016 Klaas Freitag <freitag@suse.de> * 0007 * Jonathan Marten <jjm@keelhaul.me.uk> * 0008 * * 0009 * Kooka is free software; you can redistribute it and/or modify it * 0010 * under the terms of the GNU Library General Public License as * 0011 * published by the Free Software Foundation and appearing in the * 0012 * file COPYING included in the packaging of this file; either * 0013 * version 2 of the License, or (at your option) any later version. * 0014 * * 0015 * As a special exception, permission is given to link this program * 0016 * with any version of the KADMOS OCR/ICR engine (a product of * 0017 * reRecognition GmbH, Kreuzlingen), and distribute the resulting * 0018 * executable without including the source code for KADMOS in the * 0019 * source distribution. * 0020 * * 0021 * This program is distributed in the hope that it will be useful, * 0022 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0024 * GNU General Public License for more details. * 0025 * * 0026 * You should have received a copy of the GNU General Public * 0027 * License along with this program; see the file COPYING. If * 0028 * not, see <http://www.gnu.org/licenses/>. * 0029 * * 0030 ************************************************************************/ 0031 0032 #include "kscanoptset.h" 0033 0034 #include <qstring.h> 0035 0036 #include <kconfig.h> 0037 #include <kconfiggroup.h> 0038 #include <klocalizedstring.h> 0039 0040 #include "kscanoption.h" 0041 #include "kscandevice.h" 0042 #include "scansettings.h" 0043 #include "libkookascan_logging.h" 0044 0045 0046 // Debugging options 0047 #undef DEBUG_BACKUP 0048 0049 0050 // Mappings between a save set name and a configuration file group name 0051 static QString groupForSet(const QString &setName) 0052 { 0053 return (ScanSettings::self()->saveSetDescItem()->group()+" "+setName); 0054 } 0055 0056 0057 static QString setNameFromGroup(const QString &grpName) 0058 { 0059 QString prefix = ScanSettings::self()->saveSetDescItem()->group(); 0060 if (!grpName.startsWith(prefix)) return (QString()); 0061 return (grpName.mid(prefix.length()+1)); 0062 } 0063 0064 KScanOptSet::KScanOptSet(const QString &setName) 0065 { 0066 mSetName = setName; 0067 mSetDescription = ""; 0068 0069 if (mSetName.isEmpty()) mSetName = "default"; 0070 qCDebug(LIBKOOKASCAN_LOG) << mSetName; 0071 } 0072 0073 KScanOptSet::~KScanOptSet() 0074 { 0075 //qCDebug(LIBKOOKASCAN_LOG) << mSetName << "with" << count() << "options"; 0076 } 0077 0078 QByteArray KScanOptSet::getValue(const QByteArray &optName) const 0079 { 0080 return (value(optName)); 0081 } 0082 0083 bool KScanOptSet::backupOption(const KScanOption *opt) 0084 { 0085 if (opt == nullptr || !opt->isValid()) { 0086 return (false); 0087 } 0088 0089 const QByteArray optName = opt->getName(); 0090 if (optName.isNull()) { 0091 qCDebug(LIBKOOKASCAN_LOG) << "option has no name"; 0092 return (false); 0093 } 0094 0095 if (!opt->isReadable()) { 0096 qCDebug(LIBKOOKASCAN_LOG) << "option is not readable" << optName; 0097 return (false); 0098 } 0099 0100 const QByteArray val = opt->get(); 0101 #ifdef DEBUG_BACKUP 0102 if (contains(optName)) qCDebug(LIBKOOKASCAN_LOG) << "replace" << optName << "with value" << val; 0103 else qCDebug(LIBKOOKASCAN_LOG) << "add" << optName << "with value" << val; 0104 #endif // DEBUG_BACKUP 0105 insert(optName, val); 0106 return (true); 0107 } 0108 0109 void KScanOptSet::setSetName(const QString &newName) 0110 { 0111 qCDebug(LIBKOOKASCAN_LOG) << "renaming" << mSetName << "->" << newName; 0112 mSetName = newName; 0113 } 0114 0115 void KScanOptSet::setDescription(const QString &desc) 0116 { 0117 mSetDescription = desc; 0118 } 0119 0120 void KScanOptSet::saveConfig(const QByteArray &scannerName, 0121 const QString &desc) const 0122 { 0123 qCDebug(LIBKOOKASCAN_LOG) << "Saving set" << mSetName << "for scanner" << scannerName 0124 << "with" << count() << "options"; 0125 0126 QString grpName = groupForSet(mSetName); 0127 KConfigGroup grp = KScanDevice::configGroup(grpName); 0128 grp.writeEntry(ScanSettings::self()->saveSetDescItem()->key(), desc); 0129 grp.writeEntry(ScanSettings::self()->saveSetScannerItem()->key(), scannerName); 0130 0131 for (KScanOptSet::const_iterator it = constBegin(); 0132 it != constEnd(); ++it) { 0133 //qCDebug(LIBKOOKASCAN_LOG) << " " << it.key() << "=" << it.value(); 0134 grp.writeEntry(QString(it.key()), it.value()); 0135 } 0136 0137 grp.sync(); 0138 qCDebug(LIBKOOKASCAN_LOG) << "done"; 0139 } 0140 0141 bool KScanOptSet::loadConfig(const QByteArray &scannerName) 0142 { 0143 QString grpName = groupForSet(mSetName); 0144 const KConfigGroup grp = KScanDevice::configGroup(grpName); 0145 if (!grp.exists()) { 0146 qCDebug(LIBKOOKASCAN_LOG) << "Group" << grpName << "does not exist in configuration!"; 0147 return (false); 0148 } 0149 0150 qCDebug(LIBKOOKASCAN_LOG) << "Loading set" << mSetName << "for scanner" << scannerName; 0151 0152 const QMap<QString, QString> emap = grp.entryMap(); 0153 for (QMap<QString, QString>::const_iterator it = emap.constBegin(); 0154 it != emap.constEnd(); ++it) { 0155 QString optName = it.key(); 0156 if (optName==ScanSettings::self()->saveSetDescItem()->key()) continue; 0157 // ignore this as saved 0158 if (optName==ScanSettings::self()->saveSetScannerItem()->key()) 0159 { // check this but ignore 0160 if (!scannerName.isEmpty() && scannerName!=it.value()) 0161 { 0162 qCDebug(LIBKOOKASCAN_LOG) << "was saved for scanner" << it.value(); 0163 } 0164 continue; 0165 } 0166 0167 //qCDebug(LIBKOOKASCAN_LOG) << " " << it.key() << "=" << it.value(); 0168 insert(it.key().toLatin1(), it.value().toLatin1()); 0169 } 0170 0171 qCDebug(LIBKOOKASCAN_LOG) << "done with" << count() << "options"; 0172 return (true); 0173 } 0174 0175 KScanOptSet::StringMap KScanOptSet::readList() 0176 { 0177 StringMap ret; 0178 0179 ScanSettings::self()->load(); // ensure refreshed 0180 KConfig *conf = ScanSettings::self()->config(); 0181 const QStringList groups = conf->groupList(); 0182 for (const QString &grp : groups) 0183 { 0184 QString set = setNameFromGroup(grp); 0185 if (!set.isEmpty()) 0186 { 0187 if (set==startupSetName()) continue; // don't return this one 0188 qCDebug(LIBKOOKASCAN_LOG) << "found group" << grp << "-> set" << set; 0189 const KConfigGroup g = KScanDevice::configGroup(grp); 0190 ret[set] = g.readEntry(ScanSettings::self()->saveSetDescItem()->key(), i18n("No description")); 0191 } 0192 } 0193 0194 return (ret); 0195 } 0196 0197 void KScanOptSet::deleteSet(const QString &setName) 0198 { 0199 const QString grpName = groupForSet(setName); 0200 qCDebug(LIBKOOKASCAN_LOG) << grpName; 0201 KConfig *conf = ScanSettings::self()->config(); 0202 conf->deleteGroup(grpName); 0203 conf->sync(); 0204 }