SVN_SILENT: oops, forgot to commit the xml tokenizer bit. Thanks Sebastian.
[kdelibs.git] / kinit / klauncher_main.cpp
blobcb10b3ddce8df550336add2e7d94610492791601
1 /*
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.
20 #include <config.h>
22 #include <unistd.h>
23 #include <fcntl.h>
25 #include "klauncher.h"
26 #include <kcomponentdata.h>
27 #include "kcrash.h"
28 #include "kdebug.h"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <signal.h>
32 #include <klocale.h>
33 #include <kde_file.h>
35 #include "klauncher_cmds.h"
36 #include <QtCore/QCoreApplication>
38 #ifndef Q_WS_WIN
39 static int sigpipe[ 2 ];
40 static void sig_handler(int sig_num)
42 // No recursion
43 KDE_signal( SIGHUP, SIG_IGN);
44 KDE_signal( SIGTERM, SIG_IGN);
45 fprintf(stderr, "klauncher: Exiting on signal %d\n", sig_num);
46 char tmp = 'x';
47 write( sigpipe[ 1 ], &tmp, 1 );
49 #endif
51 extern "C" KDE_EXPORT int kdemain( int argc, char**argv )
53 #ifndef Q_WS_WIN
54 // Started via kdeinit.
55 if (fcntl(LAUNCHER_FD, F_GETFD) == -1)
57 fprintf(stderr, "%s", i18n("klauncher: This program is not supposed to be started manually.\n"
58 "klauncher: It is started automatically by kdeinit4.\n").toLocal8Bit().data());
59 return 1;
61 #endif
62 KComponentData componentData("klauncher");
64 // WABA: Make sure not to enable session management.
65 putenv(strdup("SESSION_MANAGER="));
67 // Allow the locale to initialize properly
68 KLocale::setMainCatalog("kdelibs4");
70 // We need a QCoreApplication to get a DBus event loop
71 QCoreApplication app(argc, argv);
72 app.setApplicationName( componentData.componentName() );
74 int maxTry = 3;
75 while(true)
77 QString service(QLatin1String("org.kde.klauncher")); // same as ktoolinvocation.cpp
78 if (!QDBusConnection::sessionBus().isConnected()) {
79 kWarning() << "No DBUS session-bus found. Check if you have started the DBUS server.";
80 return 1;
82 QDBusReply<QDBusConnectionInterface::RegisterServiceReply> reply =
83 QDBusConnection::sessionBus().interface()->registerService(service);
84 if (!reply.isValid())
86 kWarning() << "DBUS communication problem!";
87 return 1;
89 if (reply == QDBusConnectionInterface::ServiceRegistered)
90 break;
92 if (--maxTry == 0)
94 kWarning() << "Another instance of klauncher is already running!";
95 return 1;
98 // Wait a bit...
99 kWarning() << "Waiting for already running klauncher to exit.";
100 sleep(1);
102 // Try again...
105 KLauncher *launcher = new KLauncher(LAUNCHER_FD);
106 QDBusConnection::sessionBus().registerObject("/", launcher);
108 #ifndef Q_WS_WIN
109 pipe( sigpipe );
110 QSocketNotifier* signotif = new QSocketNotifier( sigpipe[ 0 ], QSocketNotifier::Read, launcher );
111 QObject::connect( signotif, SIGNAL( activated( int )), launcher, SLOT( destruct()));
112 KCrash::setEmergencySaveFunction(sig_handler);
113 KDE_signal( SIGHUP, sig_handler);
114 KDE_signal( SIGPIPE, SIG_IGN);
115 KDE_signal( SIGTERM, sig_handler);
116 #endif
118 return app.exec();