2 This file is part of the KDE libraries
3 Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
23 #include "autostart.h"
25 #include <sys/types.h>
34 #include <QtCore/QString>
35 #include <QtCore/QSocketNotifier>
36 #include <QtCore/QTimer>
37 #include <QtCore/QList>
38 #include <QtCore/QObject>
39 #include <QtDBus/QtDBus>
44 #include <kio/connection.h>
46 class IdleSlave
: public QObject
50 explicit IdleSlave(QObject
*parent
);
51 bool match( const QString
&protocol
, const QString
&host
, bool connected
);
52 void connect( const QString
&app_socket
);
53 pid_t
pid() const { return mPid
;}
55 void reparseConfiguration();
56 bool onHold(const KUrl
&url
);
57 QString
protocol() const {return mProtocol
;}
60 void statusUpdate(IdleSlave
*);
66 KIO::Connection mConn
;
77 class SlaveWaitRequest
81 QDBusMessage transaction
;
90 QString tolerant_dbus_name
;
91 enum status_t
{ Init
= 0, Launching
, Running
, Error
, Done
};
94 QDBusMessage transaction
;
95 KService::DBusStartupType dbus_startup_type
;
99 QByteArray startup_id
; // "" is the default, "0" for none
100 QByteArray startup_dpy
; // Display to send startup notification to.
102 QStringList envs
; // env. variables to be app's environment
107 friend class KLauncher
;
113 int result
; // 0 means success. > 0 means error (-1 means pending)
114 QString dbusName
; // Contains DBUS name on success
115 QString error
; // Contains error description on failure.
119 class KLauncher
: public QObject
124 KLauncher(int kdeinitSocket
);
131 void destruct(); // exit!
134 void processDied(pid_t pid
, long exitStatus
);
136 void requestStart(KLaunchRequest
*request
);
137 void requestDone(KLaunchRequest
*request
);
139 bool start_service(KService::Ptr service
, const QStringList
&urls
,
140 const QStringList
&envs
, const QByteArray
&startup_id
,
141 bool blind
, bool autoStart
, const QDBusMessage
&msg
);
143 void createArgs( KLaunchRequest
*request
, const KService::Ptr service
,
144 const QStringList
&url
);
146 void queueRequest(KLaunchRequest
*);
148 void send_service_startup_info( KLaunchRequest
*request
, KService::Ptr service
, const QByteArray
&startup_id
,
149 const QStringList
&envs
);
150 void cancel_service_startup_info( KLaunchRequest
*request
, const QByteArray
& startup_id
,
151 const QStringList
&envs
);
154 void autoStart0Done();
155 void autoStart1Done();
156 void autoStart2Done();
158 public: // remote methods, called by KLauncherAdaptor
159 void autoStart(int phase
= 1);
163 * 'envs' are environment variables that will be added
164 * to this program's environment before starting it
165 * 'startup_id' is for application startup notification,
166 * "" is the default, "0" for none
168 void exec_blind(const QString
&name
, const QStringList
&arg_list
, const QStringList
&envs
, const QString
&startup_id
);
169 inline void exec_blind(const QString
&name
, const QStringList
&arg_list
)
170 { exec_blind(name
, arg_list
, QStringList(), QLatin1String("0")); }
172 bool kdeinit_exec(const QString
&app
, const QStringList
&args
,
173 const QString
& workdir
, const QStringList
&envs
,
174 const QString
&startup_id
, bool wait
, const QDBusMessage
&msg
);
176 void reparseConfiguration();
177 void setLaunchEnv(const QString
&name
, const QString
&value
);
180 * Start a service by desktop name.
182 * 'serviceName' refers to a desktop file describing the service.
183 * The service is looked up anywhere in $KDEDIR/applnk and/or
185 * E.g. it should have the form "korganizer".
187 * 'url', if not empty, will be passed to the service as
190 * 'envs' are environment variables that will be added
191 * to this program's environment before starting it
193 * 'startup_id' is for application startup notification,
194 * "" is the default, "0" for none
196 bool start_service_by_desktop_name(const QString
&serviceName
, const QStringList
&urls
, const QStringList
&envs
, const QString
&startup_id
, bool blind
, const QDBusMessage
&msg
);
199 * Start a service by desktop path.
201 * 'serviceName' refers to a desktop file describing the service.
202 * This may be an absolute path or a path relative to $KDEDIRS/applnk
203 * and/or $KDEDIRS/services
204 * E.g. it should have the form "Applications/korganizer.desktop" or
205 * "/opt/kde/share/applnk/Applications/korganizer.desktop".
206 * Note that for absolute paths the restrictions of
207 * KDesktopFile::isAuthorizedDesktopFile() are obeyed for security.
209 * 'url', if not empty, will be passed to the service as
212 * 'envs' are environment variables that will be added
213 * to this program's environment before starting it
215 * 'startup_id' is for application startup notification,
216 * "" is the default, "0" for none
218 bool start_service_by_desktop_path(const QString
&serviceName
, const QStringList
&urls
, const QStringList
&envs
, const QString
&startup_id
, bool blind
, const QDBusMessage
&msg
);
221 * Start a service by (translated) name - deprecated
223 * 'serviceName' refers to the service name as given by·
224 * the Name field in the desktop file describing the service.
226 * 'url', if not empty, will be passed to the service as
229 * 'envs' are environment variables that will be added
230 * to this program's environment before starting it
232 * 'startup_id' is for application startup notification,
233 * "" is the default, "0" for none
235 * @deprecated use start_service_by_desktop_path
237 bool start_service_by_name(const QString
&serviceName
, const QStringList
&urls
, const QStringList
&envs
, const QString
&startup_id
, bool blind
, const QDBusMessage
&msg
); // KDE5: remove
239 pid_t
requestHoldSlave(const KUrl
&url
, const QString
&app_socket
);
241 pid_t
requestSlave(const QString
&protocol
, const QString
&host
,
242 const QString
&app_socket
, QString
&error
);
243 void waitForSlave(int pid
, const QDBusMessage
&msg
);
246 void slotAutoStart();
249 void slotKDEInitData(int);
251 void slotNameOwnerChanged(const QString
&name
, const QString
&oldOnwer
, const QString
&newOwner
);
252 void slotSlaveStatus(IdleSlave
*);
254 void slotSlaveGone();
258 serviceResult requestResult
; // accessed by the adaptor
260 QList
<KLaunchRequest
*> requestList
; // Requests being handled
261 QList
<KLaunchRequest
*> requestQueue
; // Requests waiting to being handled
262 KLaunchRequest
*lastRequest
;
263 QList
<SlaveWaitRequest
*> mSlaveWaitRequest
;
266 QSocketNotifier
*kdeinitNotifier
;
268 KIO::ConnectionServer mConnectionServer
;
269 QList
<IdleSlave
*> mSlaveList
;
272 bool bProcessingQueue
;
273 AutoStart mAutoStart
;
275 QString mSlaveValgrind
;
276 QString mSlaveValgrindSkin
;
277 bool dontBlockReading
;
279 Display
*mCached_dpy
;
281 void processRequestReturn(int status
, const QByteArray
&requestData
);
285 void slotGotOutput();
286 void slotFinished(int exitCode
, QProcess::ExitStatus exitStatus
);