File indexing completed on 2024-04-28 15:26:51
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 #if KIOGUI_ENABLE_DEPRECATED_SINCE(5, 103) 0094 /** 0095 * Sets the icon for the startup notification. 0096 * @param iconName name of the icon, to be loaded from the current icon theme 0097 * 0098 * Alternatively, use setDesktopName(). 0099 * 0100 * @deprecated since 5.103, use setDesktopName() instead. 0101 */ 0102 KIOGUI_DEPRECATED_VERSION(5, 103, "Use setDesktopName() instead.") 0103 void setIcon(const QString &iconName); 0104 #endif 0105 0106 /** 0107 * Set the name of the desktop file (e.g.\ "org.kde.dolphin", without the ".desktop" filename extension). 0108 * 0109 * This is necessary for startup notification to work. 0110 */ 0111 void setDesktopName(const QString &desktopName); 0112 0113 /** 0114 * Sets the platform-specific startup id of the command launch. 0115 * @param startupId startup id, if any (otherwise ""). 0116 * For X11, this would be the id for the Startup Notification protocol. 0117 * For Wayland, this would be the token for the XDG Activation protocol. 0118 */ 0119 void setStartupId(const QByteArray &startupId); 0120 0121 /** 0122 * Sets the working directory from which to run the command. 0123 * @param workingDirectory path of a local directory 0124 */ 0125 void setWorkingDirectory(const QString &workingDirectory); 0126 0127 /** 0128 * Returns the working directory, which was previously set with @c setWorkingDirectory(). 0129 * @since 5.83 0130 */ 0131 QString workingDirectory() const; 0132 0133 /** 0134 * Can be used to pass environment variables to the child process. 0135 * @param environment set of environment variables to pass to the child process 0136 * @see QProcessEnvironment 0137 * @since 5.82 0138 */ 0139 void setProcessEnvironment(const QProcessEnvironment &environment); 0140 0141 /** 0142 * Starts the job. 0143 * You must call this, after having called all the necessary setters. 0144 */ 0145 void start() override; 0146 0147 /** 0148 * @return the PID of the application that was started 0149 * 0150 * Available after the job emits result(). 0151 */ 0152 qint64 pid() const; 0153 0154 private: 0155 friend class ::KRunPrivate; // KF6 REMOVE 0156 friend class ::CommandLauncherJobTest; // KF6 REMOVE 0157 /** 0158 * Blocks until the process has started. Only exists for KRun, will disappear in KF6. 0159 */ 0160 bool waitForStarted(); 0161 0162 friend class CommandLauncherJobPrivate; 0163 QScopedPointer<CommandLauncherJobPrivate> d; 0164 }; 0165 0166 } // namespace KIO 0167 0168 #endif