File indexing completed on 2024-05-12 08:34:05

0001 /* ============================================================
0002  *
0003  * SPDX-FileCopyrightText: 2017 Alexander Trufanov
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  *
0007  * ============================================================ */
0008 
0009 #ifndef DBusInterface_h
0010 #define DBusInterface_h
0011 
0012 #include <QDBusConnection>
0013 #include <KSaneWidget>
0014 #include <QStringList>
0015 
0016 static const bool defaultSelectionFiltering = true;
0017 static const QLatin1String defaultProfile("1");
0018 
0019 class DBusInterface : public QObject
0020 {
0021     Q_OBJECT
0022     Q_CLASSINFO("D-Bus Interface", "org.kde.skanlite")
0023     QStringList m_msgBuffer;
0024 
0025 public:
0026 
0027     explicit DBusInterface(QObject* parent = nullptr) : QObject(parent) {}
0028 
0029     bool setupDBusInterface();
0030 
0031     const QStringList& reply() { return m_msgBuffer; }
0032     void setReply(const QStringList &reply) { m_msgBuffer = reply; }
0033 
0034 private:
0035     // helper method for QDbusViewer compatibility. The details are in .cpp
0036     const QStringList ensureStringList(const QStringList &list);
0037 
0038 Q_SIGNALS:
0039     // used to communicate with Skanlite class
0040     void requestedScan();
0041     void requestedPreview();
0042     void requestedScanCancel();
0043     void requestedGetScannerOptions();
0044     void requestedSetScannerOptions(const QStringList &options, bool ignoreSelection);
0045     void requestedDefaultScannerOptions();
0046     void requestedDeviceName();
0047     void requestedSaveScannerOptionsToProfile(const QStringList &options, const QString &profile, bool ignoreSelection);
0048     void requestedSwitchToProfile(const QString &profile, bool ignoreSelection);
0049     void requestedGetSelection();
0050     void requestedSetSelection(const QStringList &options);
0051 
0052 public Q_SLOTS:
0053 
0054     // called via D-Bus
0055 
0056     // Perform scan operation if is in idle
0057     Q_SCRIPTABLE void scan() { Q_EMIT requestedScan(); }
0058 
0059     // Perform preview operation if is in idle
0060     Q_SCRIPTABLE void preview() { Q_EMIT requestedPreview(); }
0061 
0062     // Cancel any ongoing operation
0063     Q_SCRIPTABLE void scanCancel() { Q_EMIT requestedScanCancel(); }
0064 
0065     // Return device name, like "Hewlett-Packard:Scanjet 4370"
0066     Q_SCRIPTABLE QString getDeviceName()
0067     {
0068         Q_EMIT requestedDeviceName();
0069         return reply().join(QLatin1String());
0070     }
0071 
0072     // Return current scanner options as returned by KSaneWidget::getOptVals()
0073     Q_SCRIPTABLE QStringList getScannerOptions()
0074     {
0075         Q_EMIT requestedGetScannerOptions();
0076         return reply();
0077     }
0078 
0079     // Replaces current scanner options with argument value. Ignores page selection area info if ignoreSelection is true (by default)
0080     Q_SCRIPTABLE void setScannerOptions(const QStringList &options, bool ignoreSelection = defaultSelectionFiltering)
0081     {
0082         Q_EMIT requestedSetScannerOptions(ensureStringList(options), ignoreSelection);
0083     }
0084 
0085     // Return current scanner options
0086     Q_SCRIPTABLE QStringList getDefaultScannerOptions()
0087     {
0088         Q_EMIT requestedDefaultScannerOptions();
0089         return reply();
0090     }
0091 
0092     // Save options to KConfigGroup named ""Options For %Current_Device% - Profile %profile%""
0093     // Thus it's device specific. Default profile is "1". Could be any string value.
0094     // Ignores page selection area info if ignoreSelection is true (by default)
0095     Q_SCRIPTABLE void saveScannerOptionsToProfile(const QStringList &options, const QString &profile = defaultProfile, bool ignoreSelection = defaultSelectionFiltering)
0096     {
0097         Q_EMIT requestedSaveScannerOptionsToProfile(ensureStringList(options), profile, ignoreSelection);
0098     }
0099 
0100     // Gets current options via KSaneWidget::getOptVals() and saves them into provile
0101     // Acts as a combination of saveScannerOptionsToProfile and requestedSaveScannerOptionsToProfile
0102     // Made for easy bind both to hotkey in KHotkeys
0103     Q_SCRIPTABLE void saveCurrentScannerOptionsToProfile(const QString &profile = defaultProfile, bool ignoreSelection = defaultSelectionFiltering)
0104     {
0105         Q_EMIT requestedGetScannerOptions(); // put result in m_msgBuffer
0106         Q_EMIT requestedSaveScannerOptionsToProfile(reply(), profile, ignoreSelection);
0107     }
0108 
0109     // Loads options from specified profile and applies them. Ignores page selection area info if ignoreSelection is true (by default)
0110     Q_SCRIPTABLE void switchToProfile(const QString &profile = defaultProfile, bool ignoreSelection = defaultSelectionFiltering)
0111     {
0112         Q_EMIT requestedSwitchToProfile(profile, ignoreSelection);
0113     }
0114 
0115     // Returns current selection area in form "{"tl-x=0", "tl-y=0", "br-x=220", "br-y=248"}"
0116     Q_SCRIPTABLE QStringList getSelection()
0117     {
0118         Q_EMIT requestedGetSelection();
0119         return reply();
0120     }
0121 
0122     // Changes current selection area. Argument must be a list of strings in  form "{"tl-x=0", "tl-y=0", "br-x=220", "br-y=248"}"
0123     Q_SCRIPTABLE void setSelection(const QStringList &options)
0124     {
0125         Q_EMIT requestedSetSelection(ensureStringList(options));
0126     }
0127 
0128 Q_SIGNALS:
0129 
0130     Q_SCRIPTABLE void imageSaved(const QString &strFilename);
0131 
0132     // Below are 4 signals which are just forwarded from KSaneWidget.
0133     // You can take a look in KSaneWidget.h for detailed arguments description
0134 
0135     Q_SCRIPTABLE void scanDone(int status, const QString &strStatus);
0136     Q_SCRIPTABLE void userMessage(int type, const QString &strStatus);
0137     Q_SCRIPTABLE void scanProgress(int percent);
0138     Q_SCRIPTABLE void buttonPressed(const QString &optionName, const QString &optionLabel, bool pressed);
0139 };
0140 
0141 #endif