1/* ============================================================
2 *
3 * Date : 2008-04-17
4 * Description : Sane plugin interface for KDE
5 *
6 * Copyright (C) 2008 by Kare Sars <kare dot sars at iki dot fi>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) version 3, or any
12 * later version accepted by the membership of KDE e.V. (or its
13 * successor approved by the membership of KDE e.V.), which shall
14 * act as a proxy defined in Section 6 of version 3 of the license.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * ============================================================ */
25
26#include "sanedialog.h"
27
28#include <KLocale>
29#include <KDebug>
30#include <KPluginLoader>
31#include <KMessageBox>
32#include <KSharedConfig>
33
34K_PLUGIN_FACTORY(SaneDialogFactory, registerPlugin<SaneDialog>();)
35K_EXPORT_PLUGIN(SaneDialogFactory("ksaneplugin"))
36
37
38///////////////////////////////////////////////////////////////////
39
40
41SaneDialog::SaneDialog(QWidget *parent, const QVariantList &)
42 : KScanDialog(Plain, Close, parent)
43{
44 ksanew = new KSaneIface::KSaneWidget(this);
45 addPage(ksanew, QString());
46
47 connect(ksanew, SIGNAL(imageReady(QByteArray &, int, int, int, int)),
48 this, SLOT(imageReady(QByteArray &, int, int, int, int)));
49
50 openDev = QString();
51}
52
53
54bool SaneDialog::setup()
55{
56 if(!ksanew) {
57 // new failed
58 return false;
59 }
60 if (!openDev.isEmpty()) {
61 return true;
62 }
63 // need to select a scanner
64 openDev = ksanew->selectDevice(0);
65 if (openDev.isEmpty()) {
66 // either no scanner was found or then cancel was pressed.
67 return false;
68 }
69 if (ksanew->openDevice(openDev) == false) {
70 // could not open the scanner
71 KMessageBox::sorry(0, i18n("Opening the selected scanner failed."));
72 openDev = QString();
73 return false;
74 }
75
76 // restore scan dialog size and all options for the selected device if available
77 KSharedConfigPtr configPtr = KSharedConfig::openConfig("scannersettings");
78 restoreDialogSize(KConfigGroup(configPtr, "ScanDialog"));
79 QString groupName = openDev;
80 if (configPtr->hasGroup(groupName)) {
81 KConfigGroup group(configPtr, groupName);
82 QStringList keys = group.keyList();
83 for (int i = 0; i < keys.count(); i++)
84 ksanew->setOptVal(keys[i], group.readEntry(keys[i]));
85 }
86
87 return true;
88}
89
90SaneDialog::~SaneDialog()
91{
92 if (ksanew && !openDev.isEmpty()) {
93 // save scan dialog size and all options for the selected device if available
94 KSharedConfigPtr configPtr = KSharedConfig::openConfig("scannersettings");
95 KConfigGroup group(configPtr, "ScanDialog");
96 saveDialogSize(group, KConfigGroup::Persistent);
97 group = configPtr->group(openDev);
98 QMap<QString, QString> opts;
99 ksanew->getOptVals(opts);
100 QMap<QString, QString>::const_iterator i = opts.constBegin();
101 for (; i != opts.constEnd(); ++i)
102 group.writeEntry(i.key(), i.value(), KConfigGroup::Persistent);
103 }
104}
105
106void SaneDialog::imageReady(QByteArray &data, int w, int h, int bpl, int f)
107{
108 /* copy the image data into img */
109 QImage img = ksanew->toQImage(data, w, h, bpl, (KSaneIface::KSaneWidget::ImageFormat)f);
110 emit finalImage(img, nextId());
111}
112
113
114#include "sanedialog.moc"
115