File indexing completed on 2024-04-28 15:29:49

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2006 Aaron Seigo <aseigo@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KDELIBS_KAUTOSTART_H
0009 #define KDELIBS_KAUTOSTART_H
0010 
0011 #include <kservice_export.h>
0012 
0013 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 87)
0014 
0015 #include <QObject>
0016 #include <QStringList>
0017 #include <memory>
0018 
0019 class KAutostartPrivate;
0020 
0021 /**
0022  * @class KAutostart kautostart.h <KAutostart>
0023  *
0024  * KAutostart provides a programmatic means to control the state of
0025  * autostart services on a per-user basis. This is useful for applications
0026  * that wish to offer a configurable means to allow the application to be
0027  * autostarted.
0028  *
0029  * By using this class you future-proof your applications against potential
0030  * future or platform-specific changes to the autostart mechanism(s).
0031  *
0032  * Typical usage might look like:
0033  *
0034  * @code
0035  * KAutostart autostart; // without an entryName arg, gets name from KAboutData
0036  * autostart.setAutostarts(true); // will now start up when the user logs in
0037  *
0038  * // set the value in our configuration settings to reflect whether or not
0039  * // we will actually start up on log in
0040  * config.setAutoStart(autostart.autoStarts());
0041  * @endcode
0042  * @deprecated Since 5.87, the autostart is done inside of Plasma, apps should use the X-KDE-autostart-condition
0043  * to toggle their autostart values.
0044  * If the app should autostart in other desktop environments it's desktop file has to be compied to the autostart location manually.
0045  */
0046 class KSERVICE_EXPORT KAutostart : public QObject
0047 {
0048     Q_OBJECT
0049 
0050 public:
0051     /**
0052      * Creates a new KAutostart object that represents the autostart
0053      * service "entryName". If the service already exists in the system
0054      * then the values associated with that service, such as the executable
0055      * command, will be loaded as well.
0056      *
0057      * Note that unless this service is explicitly set to autostart,
0058      * simply creating a KAutostart object will not result in the
0059      * service being autostarted on next log in.
0060      *
0061      * If no such service is already registered and the command to be
0062      * executed on startup is not the same as entryName, then you will want
0063      * to set the associated command with setExec(const QString&)
0064      * @see setExec
0065      * @param entryName the name used to identify the service. If none is
0066      *        provided then it uses the name registered with KAboutData.
0067      * @param parent QObject
0068      *
0069      * @since 5.61 we are allowed to specify an absolute path to the service
0070      * description and it will still work.
0071      * @deprecated Since 5.87, see class docs
0072      */
0073     KSERVICE_DEPRECATED_VERSION(5, 87, "See class docs")
0074     explicit KAutostart(const QString &entryName = QString(), QObject *parent = nullptr);
0075     ~KAutostart() override;
0076 
0077     /**
0078      * Flags for each of the conditions that may affect whether or not
0079      * a service actually autostarted on login
0080      * @see Conditions
0081      */
0082     enum Condition {
0083         /**
0084          * no conditions set
0085          */
0086         NoConditions = 0x0,
0087         /**
0088          * an executable that is checked for existence by name
0089          */
0090         CheckCommand = 0x1,
0091         /**
0092          * autostart condition will be checked too (KDE-specific)
0093          * @since 4.3
0094          */
0095         CheckCondition = 0x2,
0096         /**
0097          * all necessary conditions will be checked
0098          * @since 4.3
0099          */
0100         CheckAll = 0xff,
0101     };
0102     /**
0103      * Stores a combination of #Condition values.
0104      */
0105     Q_DECLARE_FLAGS(Conditions, Condition)
0106 
0107     /**
0108      * Enumerates the various autostart phases that occur during start-up.
0109      */
0110     enum StartPhase {
0111         /**
0112          * the essential desktop services such as panels and window managers
0113          */
0114         BaseDesktop = 0,
0115         /**
0116          * services that should be available before most interactive
0117          * applications start but that aren't part of the base desktop.
0118          * This would include things such as clipboard managers and
0119          * mouse gesture tools.
0120          */
0121         DesktopServices = 1,
0122         /**
0123          * everything else that doesn't belong in the above two categories,
0124          * including most system tray applications, system monitors and
0125          * interactive applications
0126          */
0127         Applications = 2,
0128     };
0129 
0130     /**
0131      * Sets the given exec to start automatically at login
0132      * @param autostart will register with the autostart facility when true
0133      *        and deregister when false
0134      * @see autostarts()
0135      */
0136     void setAutostarts(bool autostart);
0137 
0138     /**
0139      * Returns whether or not the service represented by entryName in the
0140      * autostart system is set to autostart at login or not
0141      * @param environment if provided the check will be performed as if
0142      *        being loaded in that environment
0143      * @param check autostart conditions to check for (see commandToCheck())
0144      * @see setAutostarts()
0145      */
0146     bool autostarts(const QString &environment = QString(), Conditions check = NoConditions) const;
0147 
0148     /**
0149      * Returns the associated command for this autostart service
0150      * @see setCommand()
0151      */
0152     QString command() const;
0153     /**
0154      * Set the associated command for this autostart service
0155      * @see command()
0156      */
0157     void setCommand(const QString &command);
0158 
0159     /**
0160      * Returns the user-visible name this autostart service is registered as
0161      * @see setVisibleName(), setEntryName()
0162      */
0163     QString visibleName() const;
0164     /**
0165      * Sets the user-visible name for this autostart service.
0166      * @see visibleName()
0167      */
0168     void setVisibleName(const QString &entryName);
0169 
0170     /**
0171      * Checks whether or not a service by the given name @p entryName is registered
0172      * with the autostart system. Does not check whether or not it is
0173      * set to actually autostart or not.
0174      * @param entryName the name of the service to check for
0175      * @deprecated Since 5.87, see class docs
0176      */
0177     KSERVICE_DEPRECATED_VERSION(5, 87, "See class docs")
0178     static bool isServiceRegistered(const QString &entryName);
0179 
0180     /**
0181      * Returns the executable to check for when attempting to autostart
0182      * this service. If the executable is not found in the user's
0183      * environment, it will not autostart.
0184      * @see setCommandToCheck()
0185      */
0186     QString commandToCheck() const;
0187     /**
0188      * Sets the executable to check for the existence of when
0189      * autostarting this service
0190      * @see commandToCheck()
0191      */
0192     void setCommandToCheck(const QString &exec);
0193 
0194     /**
0195      * Returns the autostart phase this service is started in.
0196      *
0197      * Note that this is KDE specific and may not work in other
0198      * environments.
0199      *
0200      * @see StartPhase, setStartPhase()
0201      */
0202     StartPhase startPhase() const;
0203     /**
0204      * Sets the service (by name) this service should be started after.
0205      *
0206      * Note that this is KDE specific and may not work in other
0207      * environments.
0208      *
0209      * @see StartPhase, startPhase()
0210      */
0211     void setStartPhase(StartPhase phase);
0212 
0213     /**
0214      * Returns the list of environments (e.g. "KDE") this service is allowed
0215      * to start in. Use checkAllowedEnvironment() or autostarts() for actual
0216      * checks.
0217      *
0218      * This does not take other autostart conditions
0219      * into account. If any environment is added to the allowed environments
0220      * list, then only those environments will be allowed to
0221      * autoload the service. It is not allowed to specify both allowed and excluded
0222      * environments at the same time.
0223      * @see setAllowedEnvironments()
0224      */
0225     QStringList allowedEnvironments() const;
0226     /**
0227      * Sets the environments this service is allowed to start in
0228      * @see allowedEnvironments(), addToAllowedEnvironments()
0229      */
0230     void setAllowedEnvironments(const QStringList &environments);
0231     /**
0232      * Adds an environment to the list of environments this service may
0233      * start in.
0234      * @see setAllowedEnvironments(), removeFromAllowedEnvironments()
0235      */
0236     void addToAllowedEnvironments(const QString &environment);
0237     /**
0238      * Removes an environment to the list of environments this service may
0239      * start in.
0240      * @see addToAllowedEnvironments()
0241      */
0242     void removeFromAllowedEnvironments(const QString &environment);
0243 
0244     /**
0245      * Returns the list of environments this service is explicitly not
0246      * allowed to start in. Use checkAllowedEnvironment() or autostarts() for actual
0247      * checks.
0248      *
0249      * This does not take other autostart conditions
0250      * such as into account. It is not allowed to specify both allowed and excluded
0251      * environments at the same time.
0252      * @see setExcludedEnvironments()
0253      */
0254     QStringList excludedEnvironments() const;
0255     /**
0256      * Sets the environments this service is not allowed to start in
0257      * @see excludedEnvironments(), addToExcludedEnvironments()
0258      */
0259     void setExcludedEnvironments(const QStringList &environments);
0260     /**
0261      * Adds an environment to the list of environments this service may
0262      * not be autostarted in
0263      * @see removeFromExcludedEnvironments()
0264      */
0265     void addToExcludedEnvironments(const QString &environment);
0266     /**
0267      * Removes an environment to the list of environments this service may
0268      * not be autostarted in
0269      * @see addToExcludedEnvironments()
0270      */
0271     void removeFromExcludedEnvironments(const QString &environment);
0272 
0273     /**
0274      * Returns the name of another service that should be autostarted
0275      * before this one (if that service would be autostarted).
0276      * @internal
0277      * @since 4.3
0278      */
0279     QString startAfter() const;
0280 
0281     /**
0282      * Checks whether autostart is allowed in the given environment,
0283      * depending on allowedEnvironments() and excludedEnvironments().
0284      * @since 4.3
0285      */
0286     bool checkAllowedEnvironment(const QString &environment) const;
0287 
0288     /**
0289      * Checks that a given autostart configuration condition is met.
0290      * @param condition: config in the format "rcfile:group:entry:default"
0291      *
0292      * @since 5.69
0293      * @deprecated Since 5.87, see class docs
0294      */
0295     KSERVICE_DEPRECATED_VERSION(5, 87, "See class docs")
0296     static bool isStartConditionMet(const QString &condition);
0297 
0298 private:
0299     bool checkStartCondition() const;
0300 
0301 private:
0302     std::unique_ptr<KAutostartPrivate> const d;
0303 };
0304 
0305 Q_DECLARE_OPERATORS_FOR_FLAGS(KAutostart::Conditions)
0306 #endif
0307 #endif