KSaneCore

finddevicesthread.cpp
1/*
2 * SPDX-FileCopyrightText: 2009 Grzegorz Kurtyka <grzegorz dot kurtyka at gmail dot com>
3 * SPDX-FileCopyrightText: 2010 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#include "finddevicesthread.h"
10
11// Sane includes
12extern "C"
13{
14#include <sane/saneopts.h>
15#include <sane/sane.h>
16}
17
18#include <QMutex>
19#include <QMutexLocker>
20
21#include <ksanecore_debug.h>
22
23#include "deviceinformation_p.h"
24
25namespace KSaneCore
26{
27static FindSaneDevicesThread *s_instancesane = nullptr;
28Q_GLOBAL_STATIC(QMutex, s_mutexsane)
29
30class InternalDeviceInformation : public DeviceInformation
31{
32public:
33 InternalDeviceInformation(const QString &name, const QString &vendor,
34 const QString &model, const QString &type)
35 {
36 d->name = name;
37 d->model = model;
38 d->vendor = vendor;
39 d->type = type;
40 }
41};
42
43FindSaneDevicesThread *FindSaneDevicesThread::getInstance()
44{
45#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
46 QMutexLocker<QMutex> locker(s_mutexsane);
47#else
48 QMutexLocker locker(s_mutexsane);
49#endif
50
51 if (s_instancesane == nullptr) {
52 s_instancesane = new FindSaneDevicesThread();
53 }
54
55 return s_instancesane;
56}
57
58FindSaneDevicesThread::FindSaneDevicesThread() : QThread(nullptr)
59{
60}
61
62FindSaneDevicesThread::~FindSaneDevicesThread()
63{
64#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
65 QMutexLocker<QMutex> locker(s_mutexsane);
66#else
67 QMutexLocker locker(s_mutexsane);
68#endif
69 qDeleteAll(m_deviceList);
70 wait();
71}
72
73void FindSaneDevicesThread::run()
74{
75 SANE_Device const **devList;
76 SANE_Status status;
77
78 // This is unfortunately not very reliable as many back-ends do not refresh
79 // the device list after the sane_init() call...
80 status = sane_get_devices(&devList, SANE_FALSE);
81
82 qDeleteAll(m_deviceList);
83 m_deviceList.clear();
84 if (status == SANE_STATUS_GOOD) {
85 int i = 0;
86
87 while (devList[i] != nullptr) {
88 /* Do not list cameras as scanner devices when requested.
89 * Strings taken from SANE API documentation. */
90 const QString type = QString::fromUtf8(devList[i]->type);
91 if (m_deviceType == Interface::AllDevices
92 || (m_deviceType == Interface::NoCameraAndVirtualDevices && type != QLatin1String("still camera") && type != QLatin1String("video camera")
93 && type != QLatin1String("virtual device"))) {
94 InternalDeviceInformation *device = new InternalDeviceInformation(QString::fromUtf8(devList[i]->name),
95 QString::fromUtf8(devList[i]->vendor),
96 QString::fromUtf8(devList[i]->model),
97 type);
98 m_deviceList.append(std::move(device));
99 qCDebug(KSANECORE_LOG) << "Adding device " << device->vendor() << device->name() << device->model() << device->type() << " to device list";
100 } else {
101 qCDebug(KSANECORE_LOG) << "Ignoring device type" << type;
102 }
103 i++;
104 }
105 }
106}
107
108QList<DeviceInformation *> FindSaneDevicesThread::devicesList() const
109{
110 return m_deviceList;
111}
112
113void FindSaneDevicesThread::setDeviceType(const Interface::DeviceType type)
114{
115 m_deviceType = type;
116}
117
118} // namespace KSaneCore
119
120#include "moc_finddevicesthread.cpp"
QString type() const
This function returns the device type (e.g., "flatbed scanner")
QString name() const
This function returns a unique device name for the scanner device.
QString model() const
This function returns the device vendor string of the scanner device.
QString vendor() const
This function returns the device vendor string of the scanner device.
Q_SCRIPTABLE CaptureState status()
Type type(const QSqlDatabase &db)
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.