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 QMutexLocker<QMutex> locker(s_mutexsane);
46
47 if (s_instancesane == nullptr) {
48 s_instancesane = new FindSaneDevicesThread();
49 }
50
51 return s_instancesane;
52}
53
54FindSaneDevicesThread::FindSaneDevicesThread() : QThread(nullptr)
55{
56}
57
58FindSaneDevicesThread::~FindSaneDevicesThread()
59{
60 QMutexLocker<QMutex> locker(s_mutexsane);
61 qDeleteAll(m_deviceList);
62 wait();
63}
64
65void FindSaneDevicesThread::run()
66{
67 SANE_Device const **devList;
68 SANE_Status status;
69
70 // This is unfortunately not very reliable as many back-ends do not refresh
71 // the device list after the sane_init() call...
72 status = sane_get_devices(&devList, SANE_FALSE);
73
74 qDeleteAll(m_deviceList);
75 m_deviceList.clear();
76 if (status == SANE_STATUS_GOOD) {
77 int i = 0;
78
79 while (devList[i] != nullptr) {
80 /* Do not list cameras as scanner devices when requested.
81 * Strings taken from SANE API documentation. */
82 const QString type = QString::fromUtf8(devList[i]->type);
83 if (m_deviceType == Interface::AllDevices
84 || (m_deviceType == Interface::NoCameraAndVirtualDevices && type != QLatin1String("still camera") && type != QLatin1String("video camera")
85 && type != QLatin1String("virtual device"))) {
86 InternalDeviceInformation *device = new InternalDeviceInformation(QString::fromUtf8(devList[i]->name),
87 QString::fromUtf8(devList[i]->vendor),
88 QString::fromUtf8(devList[i]->model),
89 type);
90 m_deviceList.append(std::move(device));
91 qCDebug(KSANECORE_LOG) << "Adding device " << device->vendor() << device->name() << device->model() << device->type() << " to device list";
92 } else {
93 qCDebug(KSANECORE_LOG) << "Ignoring device type" << type;
94 }
95 i++;
96 }
97 }
98}
99
100QList<DeviceInformation *> FindSaneDevicesThread::devicesList() const
101{
102 return m_deviceList;
103}
104
105void FindSaneDevicesThread::setDeviceType(const Interface::DeviceType type)
106{
107 m_deviceType = type;
108}
109
110} // namespace KSaneCore
111
112#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()
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:57:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.