File indexing completed on 2024-04-28 05:01:57

0001 /*
0002     Manage custom settings
0003 
0004     SPDX-FileCopyrightText: 2011-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef SMB4KCUSTOMOPTIONSMANAGER_H
0009 #define SMB4KCUSTOMOPTIONSMANAGER_H
0010 
0011 // application specific includes
0012 #include "smb4kglobal.h"
0013 
0014 // Qt includes
0015 #include <QObject>
0016 #include <QScopedPointer>
0017 
0018 // forward declarations
0019 class Smb4KCustomSettingsManagerPrivate;
0020 
0021 /**
0022  * This classes manages the custom settings that were defined
0023  * for a certain share or host.
0024  *
0025  * @author Alexander Reinholdt <alexander.reinholdt@kdemail.net>
0026  * @since 1.0.0
0027  */
0028 
0029 class Q_DECL_EXPORT Smb4KCustomSettingsManager : public QObject
0030 {
0031     Q_OBJECT
0032 
0033     friend class Smb4KCustomSettingsManagerPrivate;
0034 
0035 public:
0036     /**
0037      * Constructor
0038      */
0039     explicit Smb4KCustomSettingsManager(QObject *parent = nullptr);
0040 
0041     /**
0042      * Destructor
0043      */
0044     ~Smb4KCustomSettingsManager();
0045 
0046     /**
0047      * Returns a static pointer to this class
0048      *
0049      * @returns a static pointer to this class
0050      */
0051     static Smb4KCustomSettingsManager *self();
0052 
0053     /**
0054      * Add the share to the list of shares that are to be remounted
0055      * either only on next program start or always Smb4K is restarted.
0056      *
0057      * @param share     The share object
0058      *
0059      * @param always    If set to TRUE the share is always mounted
0060      *                  when Smb4K is restarted.
0061      */
0062     void addRemount(const SharePtr &share, bool always = false);
0063 
0064     /**
0065      * Remove the share @p share from the list of shares that are to be
0066      * remounted. If @p force is set to TRUE, the share is removed even
0067      * if it should always be removed (option is set to
0068      * Smb4KCustomSettings::AlwaysRemount). Apart from that, the share is only
0069      * removed when the option is set to Smb4KCustomSettings::DoRemount.
0070      *
0071      * @param share     The share object
0072      *
0073      * @param force     If set to TRUE, the share is removed regardless of the
0074      *                  remount setting.
0075      */
0076     void removeRemount(const SharePtr &share, bool force = false);
0077 
0078     /**
0079      * Removes all remounts from the list of custom settings. If @p force
0080      * is set to TRUE, even those are removed that should always be remounted.
0081      *
0082      * @param force     If set to TRUE, even those shares are removed that should
0083      *                  always be remounted.
0084      */
0085     void clearRemounts(bool force);
0086 
0087     /**
0088      * Returns the list of shares that are to be remounted.
0089      *
0090      * @returns the list of shares that are to be remounted
0091      */
0092     QList<CustomSettingsPtr> sharesToRemount();
0093 
0094     /**
0095      * Find custom settings for the network item @p networkItem.
0096      *
0097      * If the network item represents a share and custom settings for it are not
0098      * defined, but for the host that provides the share, the custom settings of
0099      * the host are returned. If neither is in the list, NULL is returned.
0100      *
0101      * If you set @p exactMatch to TRUE, NULL will be returned if the URL is not found.
0102      * Except in some special cases, you should not set @p exactMatch to true,
0103      * because settings that are defined for all shares provided by a certain host and
0104      * stored in a host-type custom settings object are ignored then.
0105      *
0106      * @param networkItem         The network item
0107      * @param exactMatch          If TRUE, only exact matches are returned
0108      *
0109      * @returns the custom settings for the network item
0110      */
0111     CustomSettingsPtr findCustomSettings(const NetworkItemPtr &networkItem, bool exactMatch = false);
0112 
0113     /**
0114      * Find custom settings for the provided @p url.
0115      *
0116      * This function searches the list of custom settings and compares the host entry
0117      * and, if applicable, the path (i.e. the share name). If an exact match was found,
0118      * the corresponding custom settings are returned.
0119      *
0120      * @param url                 The network item's URL
0121      *
0122      * @returns the custom settings
0123      */
0124     CustomSettingsPtr findCustomSettings(const QUrl &url);
0125 
0126     /**
0127      * Get the list of custom settings. If @p withoutRemountOnce is defined, only those
0128      * entries are returned that have custom settings apart from the remount settings
0129      * defined.
0130      *
0131      * @param withoutRemountOnce  Returns the list of custom settings without those that
0132      *                            have only the one time remount option set.
0133      *
0134      * @returns the list of custom settings objects.
0135      */
0136     QList<CustomSettingsPtr> customSettings(bool withoutRemountOnce = false) const;
0137 
0138     /**
0139      * This function adds custom settings for a single network item to the list
0140      * of custom settings. If there already are custom settings defined for the
0141      * network item, they are updated.
0142      *
0143      * Please note that this function will store a copy of @p settings and not
0144      * the original object.
0145      *
0146      * @param settings            The custom settings
0147      */
0148     void addCustomSettings(const CustomSettingsPtr &settings);
0149 
0150     /**
0151      * This function removes custom settings for a single network item from the
0152      * list of options.
0153      *
0154      * @param settings            The custom settings
0155      */
0156     void removeCustomSettings(const CustomSettingsPtr &settings);
0157 
0158     /**
0159      * This function returns a list of custom settings objects that have
0160      * Wake-On-LAN features defined.
0161      *
0162      * @returns a list of custom settings objects with WOL features defined.
0163      */
0164     QList<CustomSettingsPtr> wakeOnLanEntries() const;
0165 
0166     /**
0167      * Save custom settings to the file.
0168      *
0169      * @param settingsList        The list of custom settings
0170      */
0171     void saveCustomSettings(const QList<CustomSettingsPtr> &settingsList);
0172 
0173 Q_SIGNALS:
0174     /**
0175      * Emitted when the list of custom settings was updated
0176      */
0177     void updated();
0178 
0179 protected Q_SLOTS:
0180     /**
0181      * Called when a profile was removed
0182      *
0183      * @param name          The name of the profile
0184      */
0185     void slotProfileRemoved(const QString &name);
0186 
0187     /**
0188      * Called when a profile was migrated
0189      *
0190      * @param oldName       The old profile name
0191      * @param newName       The new profile name
0192      */
0193     void slotProfileMigrated(const QString &oldName, const QString &newName);
0194 
0195 private:
0196     /**
0197      * Add custom settings
0198      */
0199     void add(const CustomSettingsPtr &settings);
0200 
0201     /**
0202      * Remove custom settings
0203      */
0204     void remove(const CustomSettingsPtr &settings);
0205 
0206     /**
0207      * Read custom settings
0208      */
0209     void read();
0210 
0211     /**
0212      * Write custom settings
0213      */
0214     void write();
0215 
0216     /**
0217      * Pointer to Smb4KCustomSettingsManagerPrivate class
0218      */
0219     const QScopedPointer<Smb4KCustomSettingsManagerPrivate> d;
0220 };
0221 
0222 #endif