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

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1997-1999 Matthias Kalle Dalheimer <kalle@kde.org>
0004     SPDX-FileCopyrightText: 1997-2000 Matthias Ettrich <ettrich@troll.no>
0005     SPDX-FileCopyrightText: 1998-2005 Stephan Kulow <coolo@kde.org>
0006     SPDX-FileCopyrightText: 1999-2004 Waldo Bastian <bastian@kde.org>
0007     SPDX-FileCopyrightText: 2001-2005 Lubos Lunak <l.lunak@kde.org>
0008     SPDX-FileCopyrightText: 2008 Aaron Seigo <aseigo@kde.org>
0009 
0010     SPDX-License-Identifier: LGPL-2.0-or-later
0011 */
0012 
0013 #ifndef _KTOOLINVOCATION_H
0014 #define _KTOOLINVOCATION_H
0015 
0016 #include <kservice_export.h>
0017 
0018 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 89)
0019 #include <QByteArray>
0020 #include <QExplicitlySharedDataPointer>
0021 #include <QObject>
0022 #include <QStringList>
0023 
0024 class QUrl;
0025 class KToolInvocationPrivate;
0026 class KService;
0027 typedef QExplicitlySharedDataPointer<KService> KServicePtr;
0028 
0029 /**
0030  * @class KToolInvocation ktoolinvocation.h <KToolInvocation>
0031  *
0032  * KToolInvocation: for starting other programs
0033  *
0034  * @section desktopfiles Desktop files for startServiceBy
0035  *
0036  * The way a service gets started depends on the 'X-DBUS-StartupType'
0037  * entry in the desktop file of the service:
0038  *
0039  * There are three possibilities:
0040  * @li X-DBUS-StartupType=None (default)
0041  *    Always start a new service,
0042  *    don't wait till the service registers with D-Bus.
0043  * @li X-DBUS-StartupType=Multi
0044  *    Always start a new service,
0045  *    wait until the service has registered with D-Bus.
0046  * @li X-DBUS-StartupType=Unique
0047  *    Only start the service if it isn't already running,
0048  *    wait until the service has registered with D-Bus.
0049  * The .desktop file can specify the name that the application will use when registering
0050  * using X-DBUS-ServiceName=org.domain.mykapp. Otherwise org.kde.binaryname is assumed.
0051  *
0052  * @section thread Multi-threading
0053  *
0054  * The static members (apart from self()), have to be called from the QApplication main thread.
0055  * Calls to members are only allowed if there is a Q(Core)Application object created
0056  * If you call the members with signal/slot connections across threads, you can't use the return values
0057  * If a function is called from the wrong thread and it has a return value -1 is returned
0058  * Investigate if this is really needed or if D-Bus is threadsafe anyway
0059  *
0060  * For more details see <a
0061  * href="http://techbase.kde.org/Development/Architecture/KDE4/Starting_Other_Programs#KToolInvocation::startServiceByDesktopPath">techbase</a>.
0062  *
0063  */
0064 class KSERVICE_EXPORT KToolInvocation : public QObject
0065 {
0066     Q_OBJECT
0067 private:
0068     KToolInvocation();
0069 
0070 public:
0071     // @internal
0072     ~KToolInvocation() override;
0073     static KToolInvocation *self();
0074 
0075 public Q_SLOTS:
0076 
0077 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 0)
0078     /**
0079      * Convenience method; invokes the standard email application.
0080      *
0081      * @param address The destination address
0082      * @param subject Subject string. Can be QString().
0083      * @param startup_id for app startup notification, "0" for none,
0084      *           "" ( empty string ) is the default
0085      *
0086      * @deprecated since 5.0, use QDesktopServices::openUrl(mailtoURL),
0087      * using QUrl::setPath(address) and a query item of "subject" for the subject.
0088      */
0089     KSERVICE_DEPRECATED_VERSION(5, 0, "Use QDesktopServices::openUrl(mailtoURL), using QUrl::setPath(address) and a query item of \"subject\" for the subject")
0090     static void invokeMailer(const QString &address, const QString &subject, const QByteArray &startup_id = QByteArray());
0091 #endif
0092 
0093 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 0)
0094     /**
0095      * Invokes the standard email application.
0096      *
0097      * @param mailtoURL A mailto URL.
0098      * @param startup_id for app startup notification, "0" for none,
0099      *           "" ( empty string ) is the default
0100      * @param allowAttachments whether attachments specified in mailtoURL should be honoured.
0101      *           The default is false; do not honor requests for attachments.
0102      * @deprecated since 5.0, use QDesktopServices::openUrl(mailtoURL)
0103      */
0104     KSERVICE_DEPRECATED_VERSION(5, 0, "Use QDesktopServices::openUrl(const QUrl&)")
0105     static void invokeMailer(const QUrl &mailtoURL, const QByteArray &startup_id = QByteArray(), bool allowAttachments = false);
0106 #endif
0107 
0108     /**
0109      * Convenience method; invokes the standard email application.
0110      *
0111      * All parameters are optional.
0112      *
0113      * @param to          The destination address.
0114      * @param cc          The Cc field
0115      * @param bcc         The Bcc field
0116      * @param subject     Subject string
0117      * @param body        A string containing the body of the mail (exclusive with messageFile)
0118      * @param messageFile A file (URL) containing the body of the mail (exclusive with body) - currently unsupported
0119      * @param attachURLs  List of URLs to be attached to the mail.
0120      * @param startup_id for app startup notification, "0" for none,
0121      *           "" ( empty string ) is the default
0122      * @deprecated Since 5.89, use KEMailClientLauncherJob instead
0123      */
0124     KSERVICE_DEPRECATED_VERSION(5, 89, "use KEMailClientLauncherJob instead")
0125     static void invokeMailer(const QString &to,
0126                              const QString &cc,
0127                              const QString &bcc,
0128                              const QString &subject,
0129                              const QString &body,
0130                              const QString &messageFile = QString(),
0131                              const QStringList &attachURLs = QStringList(),
0132                              const QByteArray &startup_id = QByteArray());
0133 
0134 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 0)
0135     /**
0136      * Invokes the user's preferred browser.
0137      * Note that you should only do this when you know for sure that the browser can
0138      * handle the URL (i.e. its MIME type). In doubt, if the URL can point to an image
0139      * or anything else than HTML, prefer to use new KRun( url ).
0140      *
0141      * See also <a
0142      * href="http://techbase.kde.org/Development/Architecture/KDE4/Starting_Other_Programs#KToolInvocation::invokeBrowser>techbase</a>
0143      * for a discussion of invokeBrowser vs KRun.
0144      *
0145      * @param url The destination address
0146      * @param startup_id for app startup notification, "0" for none,
0147      *           "" ( empty string ) is the default
0148      * @deprecated since 5.0, use QDesktopServices::openUrl(url)
0149      */
0150     KSERVICE_DEPRECATED_VERSION(5, 0, "Use QDesktopServices::openUrl(const QUrl&)")
0151     static void invokeBrowser(const QString &url, const QByteArray &startup_id = QByteArray());
0152 #endif
0153 
0154     /**
0155      * Invokes the standard terminal application.
0156      *
0157      * @param command the command to execute, can be empty.
0158      * @param envs ENV variables for the invoked terminal command
0159      * @param workdir the initial working directory, can be empty.
0160      * @param startup_id for app startup notification, "0" for none,
0161      *           "" ( empty string ) is the default
0162      * @since 5.73
0163      * @deprecated Since 5.89, use KTerminalLauncherJob instead
0164      */
0165     KSERVICE_DEPRECATED_VERSION(5, 89, "use KTerminalLauncherJob instead")
0166     static void invokeTerminal(const QString &command, const QStringList &envs, const QString &workdir = QString(), const QByteArray &startup_id = "");
0167 
0168 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 79)
0169     /**
0170      *
0171      * Invokes the standard terminal application.
0172      *
0173      * @param command the command to execute, can be empty.
0174      * @param workdir the initial working directory, can be empty.
0175      * @param startup_id for app startup notification, "0" for none,
0176      *           "" ( empty string ) is the default
0177      *
0178      * @since 4.1
0179      * @deprecated since 5,79, use invokeTerminal(const QString &command, const QStringList &envs, const QString &workdir, const QByteArray &startup_id) instead
0180      */
0181     KSERVICE_DEPRECATED_VERSION(
0182         5,
0183         79,
0184         "Use invokeTerminal(const QString &command, const QStringList &envs, const QString &workdir, const QByteArray &startup_id) instead")
0185     static void invokeTerminal(const QString &command, const QString &workdir = QString(), const QByteArray &startup_id = "");
0186 #endif
0187 
0188     /**
0189      * Returns the configured default terminal application. This is compatible with
0190      * the old config format from the KCM in Plasma < 5.21.
0191      * @param command Command that should be executed in the terminal app
0192      * @param workingDir Working directory for the terminal app.
0193      * @return KServicePtr of terminal application.
0194      * @since 5.78
0195      * @deprecated Since 5.89, use KTerminalLauncherJob instead to launch the executable
0196      */
0197     KSERVICE_DEPRECATED_VERSION(5, 89, "use KTerminalLauncherJob instead to launch the executable")
0198     static KServicePtr terminalApplication(const QString &command = QString(), const QString &workingDir = QString());
0199 
0200 public:
0201 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 0)
0202     /**
0203      * Starts a service based on the (translated) name of the service.
0204      * E.g. "Web Browser"
0205      *
0206      * @param _name the name of the service
0207      * @param URL if not empty this URL is passed to the service
0208      * @param error On failure, error contains a description of the error
0209      *         that occurred. If the pointer is 0, the argument will be
0210      *         ignored
0211      * @param serviceName On success, serviceName contains the D-Bus name
0212      *         under which this service is available. If empty, the service does
0213      *         not provide D-Bus services. If the pointer is 0 the argument
0214      *         will be ignored
0215      * @param pid On success, the process id of the new service will be written
0216      *        here. If the pointer is 0, the argument will be ignored.
0217      * @param startup_id for app startup notification, "0" for none,
0218      *           "" ( empty string ) is the default
0219      * @param noWait if set, the function does not wait till the service is running.
0220      * @return an error code indicating success (== 0) or failure (> 0).
0221      * @deprecated Since 5.0, use startServiceByDesktopName or startServiceByDesktopPath
0222      */
0223     KSERVICE_DEPRECATED_VERSION(5, 0, "Use KToolInvocation::startServiceByDesktopName(...) or KToolInvocation::startServiceByDesktopPath(...)")
0224     static int startServiceByName(const QString &_name,
0225                                   const QString &URL,
0226                                   QString *error = nullptr,
0227                                   QString *serviceName = nullptr,
0228                                   int *pid = nullptr,
0229                                   const QByteArray &startup_id = QByteArray(),
0230                                   bool noWait = false);
0231 #endif
0232 
0233 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 0)
0234     /**
0235      * Starts a service based on the (translated) name of the service.
0236      * E.g. "Web Browser"
0237      *
0238      * @param _name the name of the service
0239      * @param URLs if not empty these URLs will be passed to the service
0240      * @param error On failure, @p error contains a description of the error
0241      *         that occurred. If the pointer is 0, the argument will be
0242      *         ignored
0243      * @param serviceName On success, @p serviceName contains the D-Bus name
0244      *         under which this service is available. If empty, the service does
0245      *         not provide D-Bus services. If the pointer is 0 the argument
0246      *         will be ignored
0247      * @param pid On success, the process id of the new service will be written
0248      *        here. If the pointer is 0, the argument will be ignored.
0249      * @param startup_id for app startup notification, "0" for none,
0250      *           "" ( empty string ) is the default
0251      * @param noWait if set, the function does not wait till the service is running.
0252      * @return an error code indicating success (== 0) or failure (> 0).
0253      * @deprecated Since 5.0, use startServiceByDesktopName or startServiceByDesktopPath
0254      */
0255     KSERVICE_DEPRECATED_VERSION(5, 0, "Use KToolInvocation::startServiceByDesktopName(...) or KToolInvocation::startServiceByDesktopPath(...)")
0256     static int startServiceByName(const QString &_name,
0257                                   const QStringList &URLs = QStringList(),
0258                                   QString *error = nullptr,
0259                                   QString *serviceName = nullptr,
0260                                   int *pid = nullptr,
0261                                   const QByteArray &startup_id = QByteArray(),
0262                                   bool noWait = false);
0263 #endif
0264 
0265 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 82)
0266     /**
0267      * Starts a service based on the desktop path of the service.
0268      * E.g. "Applications/konqueror.desktop" or "/home/user/bla/myfile.desktop"
0269      *
0270      * @param _name the path of the desktop file
0271      * @param URL if not empty this URL is passed to the service
0272      * @param error On failure, @p error contains a description of the error
0273      *         that occurred. If the pointer is 0, the argument will be
0274      *         ignored
0275      * @param serviceName On success, @p serviceName contains the D-Bus name
0276      *         under which this service is available. If empty, the service does
0277      *         not provide D-Bus services. If the pointer is 0 the argument
0278      *         will be ignored
0279      * @param pid On success, the process id of the new service will be written
0280      *        here. If the pointer is 0, the argument will be ignored.
0281      * @param startup_id for app startup notification, "0" for none,
0282      *           "" ( empty string ) is the default
0283      * @param noWait if set, the function does not wait till the service is running.
0284      * @return an error code indicating success (== 0) or failure (> 0).
0285      *
0286      * @deprecated since 5.0 use QDBusConnectionInterface::startService("org.kde.serviceName"),
0287      *   to start a unique application in order to make D-Bus calls to it (after ensuring that
0288      *   it installs a D-Bus org.kde.serviceName.service file). Otherwise just use QProcess,
0289      *   KIO::CommandLauncherJob or KIO::ApplicationLauncherJob.
0290      */
0291     KSERVICE_DEPRECATED_VERSION_BELATED(5, 82, 5, 0, "See API documentation")
0292     static int startServiceByDesktopPath(const QString &_name,
0293                                          const QString &URL,
0294                                          QString *error = nullptr,
0295                                          QString *serviceName = nullptr,
0296                                          int *pid = nullptr,
0297                                          const QByteArray &startup_id = QByteArray(),
0298                                          bool noWait = false);
0299 #endif
0300 
0301 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 82)
0302     /**
0303      * Starts a service based on the desktop path of the service.
0304      * E.g. "Applications/konqueror.desktop" or "/home/user/bla/myfile.desktop"
0305      *
0306      * @param _name the path of the desktop file
0307      * @param URLs if not empty these URLs will be passed to the service
0308      * @param error On failure, @p error contains a description of the error
0309      *         that occurred. If the pointer is 0, the argument will be
0310      *         ignored   * @param serviceName On success, @p serviceName contains the D-Bus name
0311      *         under which this service is available. If empty, the service does
0312      *         not provide D-Bus services. If the pointer is 0 the argument
0313      *         will be ignored
0314      * @param pid On success, the process id of the new service will be written
0315      *        here. If the pointer is 0, the argument will be ignored.
0316      * @param startup_id for app startup notification, "0" for none,
0317      *           "" ( empty string ) is the default
0318      * @param noWait if set, the function does not wait till the service is running.
0319      * @return an error code indicating success (== 0) or failure (> 0).
0320      * @deprecated since 5.0 use QDBusConnectionInterface::startService("org.kde.serviceName"),
0321      *   to start a unique application in order to make D-Bus calls to it (after ensuring that
0322      *   it installs a D-Bus org.kde.serviceName.service file). Otherwise just use QProcess,
0323      *   KIO::CommandLauncherJob or KIO::ApplicationLauncherJob.
0324      */
0325     KSERVICE_DEPRECATED_VERSION_BELATED(5, 82, 5, 0, "See API documentation")
0326     static int startServiceByDesktopPath(const QString &_name,
0327                                          const QStringList &URLs = QStringList(),
0328                                          QString *error = nullptr,
0329                                          QString *serviceName = nullptr,
0330                                          int *pid = nullptr,
0331                                          const QByteArray &startup_id = QByteArray(),
0332                                          bool noWait = false);
0333 #endif
0334 
0335 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 82)
0336     /**
0337      * Starts a service based on the desktop name of the service.
0338      * E.g. "konqueror"
0339      *
0340      * @param _name the desktop name of the service
0341      * @param URL if not empty this URL is passed to the service
0342      * @param error On failure, @p error contains a description of the error
0343      *         that occurred. If the pointer is 0, the argument will be
0344      *         ignored
0345      * @param serviceName On success, @p serviceName contains the D-Bus service name
0346      *         under which this service is available. If empty, the service does
0347      *         not provide D-Bus services. If the pointer is 0 the argument
0348      *         will be ignored
0349      * @param pid On success, the process id of the new service will be written
0350      *        here. If the pointer is 0, the argument will be ignored.
0351      * @param startup_id for app startup notification, "0" for none,
0352      *           "" ( empty string ) is the default
0353      * @param noWait if set, the function does not wait till the service is running.
0354      * @return an error code indicating success (== 0) or failure (> 0).
0355      * @deprecated since 5.0 use QDBusConnectionInterface::startService("org.kde.serviceName"),
0356      *   to start a unique application in order to make D-Bus calls to it (after ensuring that
0357      *   it installs a D-Bus org.kde.serviceName.service file). Otherwise just use QProcess,
0358      *   KIO::CommandLauncherJob or KIO::ApplicationLauncherJob.
0359      */
0360     KSERVICE_DEPRECATED_VERSION_BELATED(5, 82, 5, 0, "See API documentation")
0361     static int startServiceByDesktopName(const QString &_name,
0362                                          const QString &URL,
0363                                          QString *error = nullptr,
0364                                          QString *serviceName = nullptr,
0365                                          int *pid = nullptr,
0366                                          const QByteArray &startup_id = QByteArray(),
0367                                          bool noWait = false);
0368 #endif
0369 
0370 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 82)
0371     /**
0372      * Starts a service based on the desktop name of the service.
0373      * E.g. "konqueror"
0374      *
0375      * @param _name the desktop name of the service
0376      * @param URLs if not empty these URLs will be passed to the service
0377      * @param error On failure, @p error contains a description of the error
0378      *         that occurred. If the pointer is 0, the argument will be
0379      *         ignored
0380      * @param serviceName On success, @p serviceName contains the D-Bus service name
0381      *         under which this service is available. If empty, the service does
0382      *         not provide D-Bus services. If the pointer is 0 the argument
0383      *         will be ignored
0384      * @param pid On success, the process id of the new service will be written
0385      *        here. If the pointer is 0, the argument will be ignored.
0386      * @param startup_id for app startup notification, "0" for none,
0387      *           "" ( empty string ) is the default
0388      * @param noWait if set, the function does not wait till the service is running.
0389      * @return an error code indicating success (== 0) or failure (> 0).
0390      * @deprecated since 5.0 use QDBusConnectionInterface::startService("org.kde.serviceName"),
0391      *   to start a unique application in order to make D-Bus calls to it (after ensuring that
0392      *   it installs a D-Bus org.kde.serviceName.service file). Otherwise just use QProcess,
0393      *   KIO::CommandLauncherJob or KIO::ApplicationLauncherJob.
0394      */
0395     KSERVICE_DEPRECATED_VERSION_BELATED(5, 82, 5, 0, "See API documentation")
0396     static int startServiceByDesktopName(const QString &_name,
0397                                          const QStringList &URLs = QStringList(),
0398                                          QString *error = nullptr,
0399                                          QString *serviceName = nullptr,
0400                                          int *pid = nullptr,
0401                                          const QByteArray &startup_id = QByteArray(),
0402                                          bool noWait = false);
0403 #endif
0404 
0405 #if KSERVICE_ENABLE_DEPRECATED_SINCE(5, 83)
0406     /**
0407      * Starts a program via kdeinit.
0408      *
0409      * program name and arguments are converted to according to the
0410      * local encoding and passed as is to kdeinit.
0411      *
0412      * @param name Name of the program to start
0413      * @param args Arguments to pass to the program
0414      * @param error On failure, @p error contains a description of the error
0415      *         that occurred. If the pointer is 0, the argument will be
0416      *         ignored
0417      * @param pid On success, the process id of the new service will be written
0418      *        here. If the pointer is 0, the argument will be ignored.
0419      * @param startup_id for app startup notification, "0" for none,
0420      *           "" ( empty string ) is the default
0421      * @return an error code indicating success (== 0) or failure (> 0).
0422      * @deprecated since 5.83, use CommandLauncherJob instead
0423      */
0424     KSERVICE_DEPRECATED_VERSION(5, 83, "use CommandLauncherJob instead")
0425     static int kdeinitExec(const QString &name,
0426                            const QStringList &args = QStringList(),
0427                            QString *error = nullptr,
0428                            int *pid = nullptr,
0429                            const QByteArray &startup_id = QByteArray());
0430 #endif
0431 
0432     /**
0433      * Starts a program via kdeinit and wait for it to finish.
0434      *
0435      * Like kdeinitExec(), but it waits till the program is finished.
0436      * As such it behaves similar to the system(...) function.
0437      *
0438      * @param name Name of the program to start
0439      * @param args Arguments to pass to the program
0440      * @param error On failure, @p error contains a description of the error
0441      *         that occurred. If the pointer is 0, the argument will be
0442      *         ignored
0443      * @param pid On success, the process id of the new service will be written
0444      *        here. If the pointer is 0, the argument will be ignored.
0445      * @param startup_id for app startup notification, "0" for none,
0446      *           "" ( empty string ) is the default
0447      * @return an error code indicating success (== 0) or failure (> 0).
0448      * @deprecated since 5.88, kdeinit is deprecated, launch the executable manually
0449      */
0450     KSERVICE_DEPRECATED_VERSION(5, 89, "kdeinit is deprecated, launch the executable manually")
0451     static int kdeinitExecWait(const QString &name,
0452                                const QStringList &args = QStringList(),
0453                                QString *error = nullptr,
0454                                int *pid = nullptr,
0455                                const QByteArray &startup_id = QByteArray());
0456 
0457     /**
0458      * Ensures that kdeinit5 and klauncher are running.
0459      * @deprecated Since 5.88, kdeinit is deprecated
0460      */
0461     KSERVICE_DEPRECATED_VERSION(5, 89, "kdeinit is deprecated")
0462     static void ensureKdeinitRunning();
0463 
0464 Q_SIGNALS:
0465     /**
0466      * Hook for KApplication in kdeui
0467      * @internal
0468      */
0469     void kapplication_hook(QStringList &env, QByteArray &startup_id);
0470 
0471 private:
0472     int startServiceInternal(const char *_function,
0473                              const QString &_name,
0474                              const QStringList &URLs,
0475                              QString *error,
0476                              QString *serviceName,
0477                              int *pid,
0478                              const QByteArray &startup_id,
0479                              bool noWait,
0480                              const QString &workdir = QString(),
0481                              const QStringList &envs = QStringList());
0482     static bool isMainThreadActive(QString *error = nullptr);
0483 
0484     KToolInvocationPrivate *const d;
0485     friend class KToolInvocationSingleton;
0486 };
0487 
0488 #endif
0489 #endif