1 /* ============================================================
2  *
3  * SPDX-FileCopyrightText: 2009 Kare Sars <kare dot sars at iki dot fi>
4  * SPDX-FileCopyrightText: 2014 Gregor Mitsch : port to KDE5 frameworks
5  *
6  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7  *
8  * ============================================================ */
9 // Local includes
10 #include "ksanebooloption.h"
11 
12 #include <QtCore/QVarLengthArray>
13 
14 #include <ksane_debug.h>
15 
16 namespace KSaneIface
17 {
18 
KSaneBoolOption(const SANE_Handle handle,const int index)19 KSaneBoolOption::KSaneBoolOption(const SANE_Handle handle, const int index)
20     : KSaneBaseOption(handle, index)
21 {
22     m_optionType = KSaneOption::TypeBool;
23 }
24 
setValue(const QVariant & value)25 bool KSaneBoolOption::setValue(const QVariant &value)
26 {
27     if (state() == KSaneOption::StateHidden) {
28         return false;
29     }
30 
31     bool toggled = value.toBool();
32 
33     if (m_checked != toggled) {
34         unsigned char data[4];
35 
36         m_checked = toggled;
37         fromSANE_Word(data, (toggled) ? 1 : 0);
38         writeData(data);
39         Q_EMIT valueChanged(m_checked);
40     }
41     return true;
42 }
43 
readValue()44 void KSaneBoolOption::readValue()
45 {
46     if (state() == KSaneOption::StateHidden) {
47         return;
48     }
49 
50     // read the current value
51     QVarLengthArray<unsigned char> data(m_optDesc->size);
52     SANE_Status status;
53     SANE_Int res;
54     status = sane_control_option(m_handle, m_index, SANE_ACTION_GET_VALUE, data.data(), &res);
55     if (status != SANE_STATUS_GOOD) {
56         return;
57     }
58     bool old = m_checked;
59     m_checked = (toSANE_Word(data.data()) != 0) ? true : false;
60 
61     if ((old != m_checked) && ((m_optDesc->cap & SANE_CAP_SOFT_SELECT) == 0)) {
62         Q_EMIT valueChanged(m_checked);
63     }
64 }
65 
value() const66 QVariant KSaneBoolOption::value() const
67 {
68     if (state() == KSaneOption::StateHidden) {
69         return QVariant();
70     }
71     return m_checked;
72 }
73 
valueAsString() const74 QString KSaneBoolOption::valueAsString() const
75 {
76     if (state() == KSaneOption::StateHidden) {
77         return QString();
78     }
79     if (m_checked) {
80         return QStringLiteral("true");
81     } else {
82         return QStringLiteral("false");
83     }
84 }
85 
86 }  // NameSpace KSaneIface
87