File indexing completed on 2025-01-19 03:41:33
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0006 */ 0007 0008 #ifndef KIO_COMMANDLAUNCHERJOB_H 0009 #define KIO_COMMANDLAUNCHERJOB_H 0010 0011 #include "kiogui_export.h" 0012 #include <KJob> 0013 0014 class KRunPrivate; // KF6 REMOVE 0015 class CommandLauncherJobTest; // KF6 REMOVE 0016 0017 class QProcessEnvironment; 0018 namespace KIO 0019 { 0020 class CommandLauncherJobPrivate; 0021 0022 /** 0023 * @class CommandLauncherJob commandlauncherjob.h <KIO/CommandLauncherJob> 0024 * 0025 * @brief CommandLauncherJob runs a command and watches it while running. 0026 * 0027 * It creates a startup notification and finishes it on success or on error (for the taskbar). 0028 * It also emits an error message if necessary (e.g. "program not found"). 0029 * 0030 * The job finishes when the application is successfully started. At that point you can 0031 * query the PID. 0032 * 0033 * For error handling, either connect to the result() signal, or for a simple messagebox on error, 0034 * you can do 0035 * @code 0036 * job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, this)); 0037 * @endcode 0038 * 0039 * @since 5.69 0040 */ 0041 class KIOGUI_EXPORT CommandLauncherJob : public KJob 0042 { 0043 public: 0044 /** 0045 * Creates a CommandLauncherJob. 0046 * @param command the shell command to run 0047 * The command is given "as is" to the shell, it must already be quoted if necessary. 0048 * If @p command is instead a filename, consider using the other constructor, even if no args are present. 0049 * @param parent the parent QObject 0050 * 0051 * Please consider also calling setDesktopName() for better startup notification. 0052 */ 0053 explicit CommandLauncherJob(const QString &command, QObject *parent = nullptr); 0054 0055 /** 0056 * Creates a CommandLauncherJob. 0057 * @param executable the name of the executable 0058 * @param args the commandline arguments to pass to the executable 0059 * @param parent the parent QObject 0060 * 0061 * Please consider also calling setDesktopName() for better startup notification. 0062 */ 0063 explicit CommandLauncherJob(const QString &executable, const QStringList &args, QObject *parent = nullptr); 0064 0065 /** 0066 * Destructor 0067 * 0068 * Note that jobs auto-delete themselves after emitting result 0069 */ 0070 ~CommandLauncherJob() override; 0071 0072 /** 0073 * Sets the command to execute, this will change the command that was set by any of the constructors. 0074 * @since 5.83 0075 */ 0076 void setCommand(const QString &command); 0077 0078 /** 0079 * Returns the command executed by this job. 0080 * @since 5.83 0081 */ 0082 QString command() const; 0083 0084 /** 0085 * Sets the name of the executable, used in the startup notification 0086 * (see KStartupInfoData::setBin()). 0087 * @param executable executable name, with or without a path 0088 * 0089 * Alternatively, use setDesktopName(). 0090 */ 0091 void setExecutable(const QString &executable); 0092 0093 /** 0094 * Set the name of the desktop file (e.g.\ "org.kde.dolphin", without the ".desktop" filename extension). 0095 * 0096 * This is necessary for startup notification to work. 0097 */ 0098 void setDesktopName(const QString &desktopName); 0099 0100 /** 0101 * Sets the platform-specific startup id of the command launch. 0102 * @param startupId startup id, if any (otherwise ""). 0103 * For X11, this would be the id for the Startup Notification protocol. 0104 * For Wayland, this would be the token for the XDG Activation protocol. 0105 */ 0106 void setStartupId(const QByteArray &startupId); 0107 0108 /** 0109 * Sets the working directory from which to run the command. 0110 * @param workingDirectory path of a local directory 0111 */ 0112 void setWorkingDirectory(const QString &workingDirectory); 0113 0114 /** 0115 * Returns the working directory, which was previously set with @c setWorkingDirectory(). 0116 * @since 5.83 0117 */ 0118 QString workingDirectory() const; 0119 0120 /** 0121 * Can be used to pass environment variables to the child process. 0122 * @param environment set of environment variables to pass to the child process 0123 * @see QProcessEnvironment 0124 * @since 5.82 0125 */ 0126 void setProcessEnvironment(const QProcessEnvironment &environment); 0127 0128 /** 0129 * Starts the job. 0130 * You must call this, after having called all the necessary setters. 0131 */ 0132 void start() override; 0133 0134 /** 0135 * @return the PID of the application that was started 0136 * 0137 * Available after the job emits result(). 0138 */ 0139 qint64 pid() const; 0140 0141 private: 0142 friend class ::KRunPrivate; // KF6 REMOVE 0143 friend class ::CommandLauncherJobTest; // KF6 REMOVE 0144 /** 0145 * Blocks until the process has started. Only exists for KRun, will disappear in KF6. 0146 */ 0147 bool waitForStarted(); 0148 0149 friend class CommandLauncherJobPrivate; 0150 QScopedPointer<CommandLauncherJobPrivate> d; 0151 }; 0152 0153 } // namespace KIO 0154 0155 #endif