34 #include <QMutexLocker>
35 #include <QCoreApplication>
47 KToolInvocation::KToolInvocation() :
QObject(0), d(0)
56 (QString::fromLatin1(
"org.kde.klauncher"), QString::fromLatin1(
"/KLauncher"), QDBusConnection::sessionBus()))
60 if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(QString::fromLatin1(
"org.kde.klauncher"))) {
61 kDebug(180) <<
"klauncher not running... launching kdeinit";
62 KToolInvocation::startKdeinit();
64 return ::klauncherIface();
75 bool KToolInvocation::isMainThreadActive(
QString* error)
77 if (QCoreApplication::instance() && QCoreApplication::instance()->thread() != QThread::currentThread())
79 printError(
i18n(
"Function must be called from the main thread."), error);
86 int KToolInvocation::startServiceInternal(
const char *_function,
89 const QByteArray& startup_id,
bool noWait,
92 QString function = QLatin1String(_function);
94 QDBusMessage msg = QDBusMessage::createMethodCall(launcher->service(),
96 launcher->interface(),
99 if (
function == QLatin1String(
"kdeinit_exec_with_workdir"))
104 QByteArray s = startup_id;
107 msg << QString::fromLatin1(s);
112 if( !
function.startsWith( QLatin1String(
"kdeinit_exec") ) )
115 QDBusMessage reply = QDBusConnection::sessionBus().call(msg, QDBus::Block, INT_MAX);
116 if ( reply.type() != QDBusMessage::ReplyMessage )
118 QDBusReply<QString> replyObj(reply);
119 if (replyObj.error().type() == QDBusError::NoReply) {
120 printError(
i18n(
"Error launching %1. Either KLauncher is not running anymore, or it failed to start the application.", _name), error);
122 const QString rpl = reply.arguments().count() > 0 ? reply.arguments().at(0).toString() : reply.errorMessage();
123 printError(
i18n(
"KLauncher could not be reached via D-Bus. Error when calling %1:\n%2\n",
function, rpl), error);
132 Q_ASSERT(reply.arguments().count() == 4);
134 *serviceName = reply.arguments().at(1).toString();
136 *error = reply.arguments().at(2).toString();
138 *pid = reply.arguments().at(3).toInt();
139 return reply.arguments().at(0).toInt();
142 #ifndef KDE_NO_DEPRECATED
146 const QByteArray& startup_id,
bool noWait )
148 if (!isMainThreadActive(error))
154 return self()->startServiceInternal(
"start_service_by_name",
155 _name, URLs, error, serviceName, pid, startup_id, noWait);
159 #ifndef KDE_NO_DEPRECATED
163 const QByteArray& startup_id,
bool noWait )
165 if (!isMainThreadActive(error))
168 return self()->startServiceInternal(
"start_service_by_name",
169 _name, URLs, error, serviceName, pid, startup_id, noWait);
176 int *pid,
const QByteArray& startup_id,
bool noWait )
178 if (!isMainThreadActive(error))
184 return self()->startServiceInternal(
"start_service_by_desktop_path",
185 _name, URLs, error, serviceName, pid, startup_id, noWait);
191 const QByteArray& startup_id,
bool noWait )
193 if (!isMainThreadActive(error))
196 return self()->startServiceInternal(
"start_service_by_desktop_path",
197 _name, URLs, error, serviceName, pid, startup_id, noWait);
203 const QByteArray& startup_id,
bool noWait )
205 if (!isMainThreadActive(error))
211 return self()->startServiceInternal(
"start_service_by_desktop_name",
212 _name, URLs, error, serviceName, pid, startup_id, noWait);
218 const QByteArray& startup_id,
bool noWait )
220 if (!isMainThreadActive(error))
223 return self()->startServiceInternal(
"start_service_by_desktop_name",
224 _name, URLs, error, serviceName, pid, startup_id, noWait);
229 QString *error,
int *pid,
const QByteArray& startup_id )
231 if (!isMainThreadActive(error))
234 return self()->startServiceInternal(
"kdeinit_exec",
235 name, args, error, 0, pid, startup_id,
false);
241 QString *error,
int *pid,
const QByteArray& startup_id )
243 if (!isMainThreadActive(error))
246 return self()->startServiceInternal(
"kdeinit_exec_wait",
247 name, args, error, 0, pid, startup_id,
false);
252 const QByteArray& startup_id )
254 if (!isMainThreadActive())
260 if (_appname.isEmpty()) {
261 appname = QCoreApplication::instance()->applicationName();
270 if (!docPath.isEmpty()) {
271 url =
KUrl(
KUrl(
"help:/"), docPath);
273 url = QString::fromLatin1(
"help:/%1/index.html").arg(appname);
276 if (!anchor.isEmpty()) {
277 url.
addQueryItem(QString::fromLatin1(
"anchor"), anchor);
282 if (!(url.
protocol() == QLatin1String(
"help") || url.
protocol() == QLatin1String(
"man") || url.
protocol() == QLatin1String(
"info"))) {
287 QDBusInterface *iface =
new QDBusInterface(QLatin1String(
"org.kde.khelpcenter"),
288 QLatin1String(
"/KHelpCenter"),
289 QLatin1String(
"org.kde.khelpcenter.khelpcenter"),
290 QDBusConnection::sessionBus());
291 if ( !iface->isValid() )
302 i18n(
"Could not launch the KDE Help Center:\n\n%1", error),
303 i18n(
"Could not Launch Help Center"));
309 iface =
new QDBusInterface(QLatin1String(
"org.kde.khelpcenter"),
310 QLatin1String(
"/KHelpCenter"),
311 QLatin1String(
"org.kde.khelpcenter.khelpcenter"),
312 QDBusConnection::sessionBus());
315 iface->call(QString::fromLatin1(
"openUrl"), url.
url(), startup_id );
321 if (!isMainThreadActive())
330 if (!isMainThreadActive())
339 const QStringList queries = mailtoURL.
query().mid(1).split(QLatin1Char(
'&'));
340 const QChar comma = QChar::fromLatin1(
',');
342 for (QStringList::ConstIterator it = queries.begin(); it != queries.end(); ++it)
345 if (q.startsWith(QLatin1String(
"subject=")))
346 subject = KUrl::fromPercentEncoding((*it).mid(8).toLatin1());
348 if (q.startsWith(QLatin1String(
"cc=")))
349 cc = cc.isEmpty()? KUrl::fromPercentEncoding((*it).mid(3).toLatin1()): cc + comma + KUrl::fromPercentEncoding((*it).mid(3).toLatin1());
351 if (q.startsWith(QLatin1String(
"bcc=")))
352 bcc = bcc.isEmpty()? KUrl::fromPercentEncoding((*it).mid(4).toLatin1()): bcc + comma + KUrl::fromPercentEncoding((*it).mid(4).toLatin1());
354 if (q.startsWith(QLatin1String(
"body=")))
355 body = KUrl::fromPercentEncoding((*it).mid(5).toLatin1());
357 if (allowAttachments && q.startsWith(QLatin1String(
"attach=")))
358 attachURLs.push_back(KUrl::fromPercentEncoding((*it).mid(7).toLatin1()));
360 if (allowAttachments && q.startsWith(QLatin1String(
"attachment=")))
361 attachURLs.push_back(KUrl::fromPercentEncoding((*it).mid(11).toLatin1()));
363 if (q.startsWith(QLatin1String(
"to=")))
364 address = address.isEmpty()? KUrl::fromPercentEncoding((*it).mid(3).toLatin1()): address + comma + KUrl::fromPercentEncoding((*it).mid(3).toLatin1());
370 void KToolInvocation::startKdeinit()
376 if( QDBusConnection::sessionBus().interface()->isServiceRegistered(QString::fromLatin1(
"org.kde.klauncher")))
389 args += QString::fromLatin1(
"--suicide");
391 QProcess::execute(srv, args);
396 #include "ktoolinvocation.moc"