fix tricky regression noticed by Vyacheslav Tokarev on Google Reader.
[kdelibs.git] / kinit / kioslave.cpp
blob1faaa635f84549f996bc38b07d5d4dcf70c3c145
1 /*
2 * This file is part of the KDE libraries
3 * Copyright (c) 1999-2000 Waldo Bastian <bastian@kde.org>
4 * (c) 1999 Mario Weilguni <mweilguni@sime.com>
5 * (c) 2001 Lubos Lunak <l.lunak@kde.org>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License version 2 as published by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
22 #include <kdebug.h>
23 #include <config.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <locale.h>
30 #include <QtCore/QString>
31 #include <QtCore/QLibrary>
32 #include <QtCore/QFile>
33 #ifdef Q_WS_WIN
34 #include <QtCore/QDir>
35 #include <QtCore/QProcess>
36 #include <QtCore/QStringList>
37 #include <windows.h>
38 #include <process.h>
39 #include "kstandarddirs.h"
40 #endif
42 #ifndef Q_WS_WIN
43 /* These are to link libkio even if 'smart' linker is used */
44 #include <kio/authinfo.h>
45 extern "C" KIO::AuthInfo* _kioslave_init_kio() { return new KIO::AuthInfo(); }
46 #endif
48 int main(int argc, char **argv)
50 if (argc < 5)
52 fprintf(stderr, "Usage: kioslave <slave-lib> <protocol> <klauncher-socket> <app-socket>\n\nThis program is part of KDE.\n");
53 exit(1);
55 setlocale(LC_ALL, "");
56 QString libpath = QFile::decodeName(argv[1]);
58 if (libpath.isEmpty())
60 fprintf(stderr, "library path is empty.\n");
61 exit(1);
64 QLibrary lib(libpath);
65 #ifdef Q_WS_WIN
66 qDebug("trying to load '%s'", qPrintable(libpath));
67 #endif
68 if ( !lib.load() || !lib.isLoaded() )
70 #ifdef Q_WS_WIN
71 libpath = KStandardDirs::installPath("module") + QFileInfo(libpath).fileName();
72 lib.setFileName( libpath );
73 if(!lib.load() || !lib.isLoaded())
75 QByteArray kdedirs = qgetenv("KDEDIRS");
76 if (!kdedirs.size()) {
77 qDebug("not able to find '%s' because KDEDIRS environment variable is not set.\n"
78 "Set KDEDIRS to the KDE installation root dir and restart klauncher to fix this problem.",
79 qPrintable(libpath));
80 exit(1);
82 QString paths = QString::fromLocal8Bit(kdedirs);
83 QStringList pathlist = paths.split(';');
84 Q_FOREACH(const QString &path, pathlist) {
85 QString slave_path = path + QLatin1String("/lib/kde4/") + QFileInfo(libpath).fileName();
86 qDebug("trying to load '%s'",slave_path.toAscii().data());
87 lib.setFileName(slave_path);
88 if (lib.load() && lib.isLoaded() )
89 break;
91 if (!lib.isLoaded())
93 qWarning("could not open %s: %s", libpath.data(), qPrintable (lib.errorString()) );
94 exit(1);
97 #else
98 fprintf(stderr, "could not open %s: %s", qPrintable(libpath),
99 qPrintable (lib.errorString()) );
100 exit(1);
101 #endif
104 void* sym = lib.resolve("kdemain");
105 if (!sym )
107 sym = lib.resolve("main");
108 if (!sym )
110 fprintf(stderr, "Could not find main: %s\n", qPrintable(lib.errorString() ));
111 exit(1);
115 #ifdef Q_WS_WIN
116 // enter debugger in case debugging is actived
117 QString slaveDebugWait( QString::fromLocal8Bit( qgetenv("KDE_SLAVE_DEBUG_WAIT") ) );
118 if (slaveDebugWait == QLatin1String("all") || slaveDebugWait == argv[2])
120 # ifdef Q_CC_MSVC
121 // msvc debugger or windbg supports jit debugging, the latter requires setting up windbg jit with windbg -i
122 DebugBreak();
123 # else
124 // gdb does not support win32 jit debug support, so implement it by ourself
125 WCHAR buf[1024];
126 GetModuleFileName(NULL,buf, 1024);
127 QStringList params;
128 params << QString::fromUtf16((const unsigned short*)buf);
129 params << QString::number(GetCurrentProcessId());
130 QProcess::startDetached("gdb",params);
131 Sleep(1000);
132 # endif
134 # ifdef Q_CC_MSVC
135 else {
136 QString slaveDebugPopup( QString::fromLocal8Bit( qgetenv("KDE_SLAVE_DEBUG_POPUP") ) );
137 if (slaveDebugPopup == QLatin1String("all") || slaveDebugPopup == argv[2]) {
138 // A workaround for OSes where DebugBreak() does not work in administrative mode (actually Vista with msvc 2k5)
139 // - display a native message box so developer can attach the debugger to the KIO slave process and click OK.
140 MessageBoxA(NULL,
141 QString("Please attach the debugger to process #%1 (%2)").arg(getpid()).arg(argv[0]).toLatin1(),
142 QString("\"%1\" KIO Slave Debugging").arg(argv[2]).toLatin1(), MB_OK|MB_ICONINFORMATION|MB_TASKMODAL);
145 # endif
146 #endif // Q_WS_WIN
148 int (*func)(int, char *[]) = (int (*)(int, char *[])) sym;
150 exit( func(argc-1, argv+1)); /* Launch! */