File indexing completed on 2024-04-21 05:01:37

0001 /*
0002     Private classes for the SMB client
0003 
0004     SPDX-FileCopyrightText: 2018-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef SMB4KCLIENT_P_H
0009 #define SMB4KCLIENT_P_H
0010 
0011 // application specific includes
0012 #include "smb4kclient.h"
0013 #include "smb4kfile.h"
0014 #include "smb4kglobal.h"
0015 #include "smb4khost.h"
0016 #include "smb4kshare.h"
0017 #include "smb4kworkgroup.h"
0018 
0019 // Samba includes
0020 #include <libsmbclient.h>
0021 
0022 // Qt includes
0023 #include <QHostAddress>
0024 #include <QTimer>
0025 #include <QUrl>
0026 
0027 // KDE includes
0028 #include <KDNSSD/RemoteService>
0029 #include <KDNSSD/ServiceBrowser>
0030 #include <KJob>
0031 
0032 #ifdef USE_WS_DISCOVERY
0033 #include <WSDiscoveryClient>
0034 #endif
0035 
0036 class Smb4KClientBaseJob : public KJob
0037 {
0038     Q_OBJECT
0039 
0040 public:
0041     /**
0042      * Constructor
0043      */
0044     explicit Smb4KClientBaseJob(QObject *parent = nullptr);
0045 
0046     /**
0047      * Destructor
0048      */
0049     ~Smb4KClientBaseJob();
0050 
0051     /**
0052      * Set the process
0053      */
0054     void setProcess(Smb4KGlobal::Process process);
0055 
0056     /**
0057      * Return the process
0058      */
0059     Smb4KGlobal::Process process() const;
0060 
0061     /**
0062      * Set the basic network item
0063      */
0064     void setNetworkItem(NetworkItemPtr networkItem);
0065 
0066     /**
0067      * Return the basic network item
0068      */
0069     NetworkItemPtr networkItem() const;
0070 
0071     /**
0072      * The list of workgroups that was discovered.
0073      */
0074     QList<WorkgroupPtr> workgroups();
0075 
0076     /**
0077      * The list of hosts that was discovered.
0078      */
0079     QList<HostPtr> hosts();
0080 
0081     /**
0082      * The list shares that was discovered.
0083      */
0084     QList<SharePtr> shares();
0085 
0086     /**
0087      * The ist of files and directories that were discovered.
0088      */
0089     QList<FilePtr> files();
0090 
0091     /**
0092      * Error enumeration
0093      *
0094      * @enum ClientError         The client failed
0095      * @enum AccessDeniedError   Permission denied
0096      * @enum FileAccessError     The file could not be read
0097      * @enum PrintFileError      The file could not be printed
0098      */
0099     enum { ClientError = UserDefinedError, AccessDeniedError, FileAccessError, PrintFileError };
0100 
0101 protected:
0102     Smb4KGlobal::Process *pProcess;
0103     NetworkItemPtr *pNetworkItem;
0104     QList<WorkgroupPtr> *pWorkgroups;
0105     QList<HostPtr> *pHosts;
0106     QList<SharePtr> *pShares;
0107     QList<FilePtr> *pFiles;
0108     QHostAddress lookupIpAddress(const QString &name);
0109 
0110 private:
0111     Smb4KGlobal::Process m_process;
0112     NetworkItemPtr m_networkItem;
0113     QList<WorkgroupPtr> m_workgroups;
0114     QList<HostPtr> m_hosts;
0115     QList<SharePtr> m_shares;
0116     QList<FilePtr> m_files;
0117 };
0118 
0119 class Smb4KClientJob : public Smb4KClientBaseJob
0120 {
0121     Q_OBJECT
0122 
0123 public:
0124     /**
0125      * Constructor
0126      */
0127     explicit Smb4KClientJob(QObject *parent = nullptr);
0128 
0129     /**
0130      * Destructor
0131      */
0132     ~Smb4KClientJob();
0133 
0134     /**
0135      * Starts the job.
0136      */
0137     void start() override;
0138 
0139     /**
0140      * Set the file that is to be printed
0141      */
0142     void setPrintFileItem(const KFileItem &item);
0143 
0144     /**
0145      * Get the file that is to be printed
0146      */
0147     KFileItem printFileItem() const;
0148 
0149     /**
0150      * Set the number of copies that are to be printed
0151      */
0152     void setPrintCopies(int copies);
0153 
0154     /**
0155      * Get the number of copies that are to be printed
0156      */
0157     int printCopies() const;
0158 
0159     /**
0160      * The authentication function for libsmbclient
0161      */
0162     void get_auth_data_fn(const char *server,
0163                           const char *share,
0164                           char *workgroup,
0165                           int maxLenWorkgroup,
0166                           char *username,
0167                           int maxLenUsername,
0168                           char *password,
0169                           int maxLenPassword);
0170 
0171 protected Q_SLOTS:
0172     void slotStartJob();
0173     void slotFinishJob();
0174 
0175 private:
0176     void initClientLibrary();
0177     void doLookups();
0178     void doPrinting();
0179     SMBCCTX *m_context;
0180     KFileItem m_fileItem;
0181     int m_copies;
0182 };
0183 
0184 class Smb4KDnsDiscoveryJob : public Smb4KClientBaseJob
0185 {
0186     Q_OBJECT
0187 
0188 public:
0189     /**
0190      * Constructor
0191      */
0192     explicit Smb4KDnsDiscoveryJob(QObject *parent = nullptr);
0193 
0194     /**
0195      * Destructor
0196      */
0197     ~Smb4KDnsDiscoveryJob();
0198 
0199     /**
0200      * Start the job
0201      */
0202     void start() override;
0203 
0204 protected Q_SLOTS:
0205     void slotStartJob();
0206     void slotServiceAdded(KDNSSD::RemoteService::Ptr service);
0207     void slotFinished();
0208 
0209 private:
0210     KDNSSD::ServiceBrowser *m_serviceBrowser;
0211 };
0212 
0213 #ifdef USE_WS_DISCOVERY
0214 class Smb4KWsDiscoveryJob : public Smb4KClientBaseJob
0215 {
0216     Q_OBJECT
0217 
0218 public:
0219     /**
0220      * Constructor
0221      */
0222     explicit Smb4KWsDiscoveryJob(QObject *parent = nullptr);
0223 
0224     /**
0225      * Destructor
0226      */
0227     ~Smb4KWsDiscoveryJob();
0228 
0229     /**
0230      * Start the job
0231      */
0232     void start() override;
0233 
0234 protected Q_SLOTS:
0235     void slotStartJob();
0236     void slotProbeMatchReceived(const WSDiscoveryTargetService &service);
0237     void slotResolveMatchReceived(const WSDiscoveryTargetService &service);
0238     void slotDiscoveryFinished();
0239 
0240 private:
0241     WSDiscoveryClient *m_discoveryClient;
0242     QTimer *m_timer;
0243 };
0244 #endif
0245 
0246 class Smb4KClientPrivate
0247 {
0248 public:
0249     struct QueueContainer {
0250         Smb4KGlobal::Process process;
0251         NetworkItemPtr networkItem;
0252         KFileItem printFileItem;
0253         int printCopies;
0254     };
0255     QList<WorkgroupPtr> tempWorkgroupList;
0256     QList<HostPtr> tempHostList;
0257     QList<QueueContainer> queue;
0258 };
0259 
0260 class Smb4KClientStatic
0261 {
0262 public:
0263     Smb4KClient instance;
0264 };
0265 
0266 #endif