1 /************************************************************************
2 * *
3 * This file is part of Kooka, a scanning/OCR application using *
4 * Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>. *
5 * *
6 * Copyright (C) 2000-2016 Klaas Freitag <freitag@suse.de> *
7 * Jonathan Marten <jjm@keelhaul.me.uk> *
8 * *
9 * Kooka is free software; you can redistribute it and/or modify it *
10 * under the terms of the GNU Library General Public License as *
11 * published by the Free Software Foundation and appearing in the *
12 * file COPYING included in the packaging of this file; either *
13 * version 2 of the License, or (at your option) any later version. *
14 * *
15 * As a special exception, permission is given to link this program *
16 * with any version of the KADMOS OCR/ICR engine (a product of *
17 * reRecognition GmbH, Kreuzlingen), and distribute the resulting *
18 * executable without including the source code for KADMOS in the *
19 * source distribution. *
20 * *
21 * This program is distributed in the hope that it will be useful, *
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
24 * GNU General Public License for more details. *
25 * *
26 * You should have received a copy of the GNU General Public *
27 * License along with this program; see the file COPYING. If *
28 * not, see <http://www.gnu.org/licenses/>. *
29 * *
30 ************************************************************************/
31
32 #include "kscanoptset.h"
33
34 #include <qstring.h>
35 #include <qdebug.h>
36
37 #include <kconfig.h>
38 #include <kconfiggroup.h>
39 #include <klocalizedstring.h>
40
41 #include "kscanoption.h"
42 #include "kscandevice.h"
43 #include "scansettings.h"
44
45
46 // Debugging options
47 #undef DEBUG_BACKUP
48
49
50 // Mappings between a save set name and a configuration file group name
groupForSet(const QString & setName)51 static QString groupForSet(const QString &setName)
52 {
53 return (ScanSettings::self()->saveSetDescItem()->group()+" "+setName);
54 }
55
56
setNameFromGroup(const QString & grpName)57 static QString setNameFromGroup(const QString &grpName)
58 {
59 QString prefix = ScanSettings::self()->saveSetDescItem()->group();
60 if (!grpName.startsWith(prefix)) return (QString());
61 return (grpName.mid(prefix.length()+1));
62 }
63
KScanOptSet(const QString & setName)64 KScanOptSet::KScanOptSet(const QString &setName)
65 {
66 mSetName = setName;
67 mSetDescription = "";
68
69 if (mSetName.isEmpty()) mSetName = "default";
70 //qDebug() << mSetName;
71 }
72
~KScanOptSet()73 KScanOptSet::~KScanOptSet()
74 {
75 //qDebug() << mSetName << "with" << count() << "options";
76 }
77
getValue(const QByteArray & optName) const78 QByteArray KScanOptSet::getValue(const QByteArray &optName) const
79 {
80 return (value(optName));
81 }
82
backupOption(const KScanOption * opt)83 bool KScanOptSet::backupOption(const KScanOption *opt)
84 {
85 if (opt == nullptr || !opt->isValid()) {
86 return (false);
87 }
88
89 const QByteArray optName = opt->getName();
90 if (optName.isNull()) {
91 //qDebug() << "option has no name";
92 return (false);
93 }
94
95 if (!opt->isReadable()) {
96 //qDebug() << "option is not readable" << optName;
97 return (false);
98 }
99
100 const QByteArray val = opt->get();
101 #ifdef DEBUG_BACKUP
102 if (contains(optName)) qDebug() << "replace" << optName << "with value" << QString(val);
103 else qDebug() << "add" << optName << "with value" << QString(val);
104 #endif // DEBUG_BACKUP
105 insert(optName, val);
106 return (true);
107 }
108
setSetName(const QString & newName)109 void KScanOptSet::setSetName(const QString &newName)
110 {
111 //qDebug() << "renaming" << mSetName << "->" << newName;
112 mSetName = newName;
113 }
114
setDescription(const QString & desc)115 void KScanOptSet::setDescription(const QString &desc)
116 {
117 mSetDescription = desc;
118 }
119
saveConfig(const QByteArray & scannerName,const QString & desc) const120 void KScanOptSet::saveConfig(const QByteArray &scannerName,
121 const QString &desc) const
122 {
123 //qDebug() << "Saving set" << mSetName << "for scanner" << scannerName
124 //<< "with" << count() << "options";
125
126 QString grpName = groupForSet(mSetName);
127 KConfigGroup grp = KScanDevice::configGroup(grpName);
128 grp.writeEntry(ScanSettings::self()->saveSetDescItem()->key(), desc);
129 grp.writeEntry(ScanSettings::self()->saveSetScannerItem()->key(), scannerName);
130
131 for (KScanOptSet::const_iterator it = constBegin();
132 it != constEnd(); ++it) {
133 //qDebug() << " " << it.key() << "=" << it.value();
134 grp.writeEntry(QString(it.key()), it.value());
135 }
136
137 grp.sync();
138 //qDebug() << "done";
139 }
140
loadConfig(const QByteArray & scannerName)141 bool KScanOptSet::loadConfig(const QByteArray &scannerName)
142 {
143 QString grpName = groupForSet(mSetName);
144 const KConfigGroup grp = KScanDevice::configGroup(grpName);
145 if (!grp.exists()) {
146 //qDebug() << "Group" << grpName << "does not exist in configuration!";
147 return (false);
148 }
149
150 //qDebug() << "Loading set" << mSetName << "for scanner" << scannerName;
151
152 const QMap<QString, QString> emap = grp.entryMap();
153 for (QMap<QString, QString>::const_iterator it = emap.constBegin();
154 it != emap.constEnd(); ++it) {
155 QString optName = it.key();
156 if (optName==ScanSettings::self()->saveSetDescItem()->key()) continue;
157 // ignore this as saved
158 if (optName==ScanSettings::self()->saveSetScannerItem()->key())
159 { // check this but ignore
160 if (!scannerName.isEmpty() && scannerName!=it.value())
161 {
162 //qDebug() << "was saved for scanner" << it.value();
163 }
164 continue;
165 }
166
167 //qDebug() << " " << it.key() << "=" << it.value();
168 insert(it.key().toLatin1(), it.value().toLatin1());
169 }
170
171 //qDebug() << "done with" << count() << "options";
172 return (true);
173 }
174
readList()175 KScanOptSet::StringMap KScanOptSet::readList()
176 {
177 StringMap ret;
178
179 ScanSettings::self()->load(); // ensure refreshed
180 KConfig *conf = ScanSettings::self()->config();
181 QStringList groups = conf->groupList();
182 //groups.sort(); qDebug() << groups;
183 foreach (const QString &grp, groups)
184 {
185 QString set = setNameFromGroup(grp);
186 if (!set.isEmpty())
187 {
188 if (set==startupSetName()) continue; // don't return this one
189 qDebug() << "found group" << grp << "-> set" << set;
190 const KConfigGroup g = KScanDevice::configGroup(grp);
191 ret[set] = g.readEntry(ScanSettings::self()->saveSetDescItem()->key(), i18n("No description"));
192 }
193 }
194
195 return (ret);
196 }
197
deleteSet(const QString & setName)198 void KScanOptSet::deleteSet(const QString &setName)
199 {
200 const QString grpName = groupForSet(setName);
201 //qDebug() << grpName;
202 KConfig *conf = ScanSettings::self()->config();
203 conf->deleteGroup(grpName);
204 conf->sync();
205 }
206