KInit
kwrapper_win.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 #include <stdio.h>
00046 #include <assert.h>
00047 #include <stdlib.h>
00048 #include <process.h>
00049 #include <windows.h>
00050
00051 #include <QString>
00052 #include <QProcess>
00053 #include <QtDebug>
00054 #include <QFileInfo>
00055 #include <QCoreApplication>
00056 #include <QList>
00057
00058 bool verbose = 0;
00059
00060 int main(int argc, char **argv)
00061 {
00062 QCoreApplication app(argc,argv);
00063
00064 QStringList envPath;
00065 QStringList searchPath;
00066 QString exeToStart;
00067 QString myAppName = "kwrapper4:";
00068 QStringList exeParams;
00069 int firstParam = 1;
00070
00071 if (QCoreApplication::arguments().size() == 1)
00072 {
00073 qDebug() << myAppName << "no application given";
00074 return 1;
00075 }
00076
00077 if (QCoreApplication::arguments().at(1) == "--verbose")
00078 {
00079 verbose = 1;
00080 firstParam = 2;
00081 }
00082
00083 exeToStart = QCoreApplication::arguments().at(firstParam);
00084
00085 for(int i=firstParam+1; i < QCoreApplication::arguments().size(); i++)
00086 exeParams << QCoreApplication::arguments().at(i);
00087
00088 QString path = QString::fromLocal8Bit(qgetenv("PATH")).toLower().replace('\\','/');
00089
00094 foreach(QString a, path.split(';'))
00095 {
00096 if (!envPath.contains(a))
00097 envPath << a;
00098 if (!a.endsWith("/lib") && !a.endsWith("/lib/") && !searchPath.contains(a))
00099 searchPath << a;
00100 }
00101
00102
00103 path = QCoreApplication::applicationDirPath().toLower().replace('\\','/');
00104 if (!envPath.contains(path))
00105 envPath << path;
00106
00107
00108
00109 QFileInfo fi(path + "/../..");
00110 QString rootPath = fi.canonicalPath();
00111
00112 if (verbose)
00113 qDebug() << "try to find kdedirs.cache in" << rootPath;
00114
00115
00116 path = path.replace("bin","lib");
00117 if (!envPath.contains(path))
00118 envPath << path;
00119
00125 path = QString::fromLocal8Bit(qgetenv("KDEDIRS")).toLower().replace('\\','/');
00126 QStringList kdedirs;
00127
00128 if (path.size() > 0)
00129 kdedirs = path.split(';');
00130
00131 bool changedKDEDIRS = 0;
00132
00133 if (kdedirs.size() == 0)
00134 {
00135 QStringList kdedirsCacheList;
00136 #ifdef Q_CC_MSVC
00137 kdedirsCacheList << rootPath + "/kdedirs.cache.msvc";
00138 #endif
00139 kdedirsCacheList << rootPath + "/kdedirs.cache";
00140
00141 bool found = false;
00142 foreach(QString kdedirsCachePath,kdedirsCacheList)
00143 {
00144 QFile f(kdedirsCachePath);
00145 if (f.exists())
00146 {
00147 f.open(QIODevice::ReadOnly);
00148 QByteArray data = f.readAll();
00149 f.close();
00150 kdedirs = QString(data).split(';');
00151 if (verbose)
00152 qDebug() << "load kdedirs cache from " << kdedirsCachePath << "values=" << kdedirs;
00153 found = true;
00154 break;
00155 }
00156 }
00157 if (!found)
00158 {
00159
00160
00161
00162
00163
00164
00165
00166 }
00167 changedKDEDIRS = 1;
00168 }
00169 if (verbose)
00170 qDebug() << "found KDEDIRS\n\t" << kdedirs.join("\n\t");
00171
00172 foreach(QString a, kdedirs)
00173 {
00174 if (!envPath.contains(a+"/bin"))
00175 envPath << a + "/bin";
00176 if (!envPath.contains(a+"/lib"))
00177 envPath << a + "/lib";
00178 if (!searchPath.contains(a+"/bin"))
00179 searchPath << a + "/bin";
00180 }
00181
00182
00183 WCHAR _appName[MAX_PATH+1];
00184
00185 if (verbose)
00186 qDebug() << "search " << exeToStart << "in";
00187
00188 bool found = false;
00189 foreach(QString a, searchPath)
00190 {
00191 if (verbose)
00192 qDebug() << "\t" << a;
00193 if (SearchPathW((LPCWSTR)a.utf16(),(LPCWSTR)exeToStart.utf16(),
00194 L".exe",MAX_PATH+1,(LPWSTR)_appName,NULL))
00195 {
00196 found = true;
00197 break;
00198 }
00199 }
00200 QString appName = QString::fromUtf16((unsigned short*)_appName);
00201
00202 if (!found)
00203 {
00204 qWarning() << myAppName << "application not found";
00205 return 3;
00206 }
00207
00208 if (verbose)
00209 qDebug() << "run" << exeToStart << "with params" << exeParams << "and PATH environment\n\t" << envPath.join("\n\t");
00210
00211
00212 QStringList env = QProcess::systemEnvironment();
00213 env.replaceInStrings(QRegExp("^PATH=(.*)", Qt::CaseInsensitive), QLatin1String("PATH=") + envPath.join(";"));
00214 if (changedKDEDIRS)
00215 env << QLatin1String("KDEDIRS=") + kdedirs.join(";");
00216
00217 QProcess *process = new QProcess;
00218 process->setEnvironment(env);
00219 process->start(appName,exeParams);
00220 if (process->state() == QProcess::NotRunning)
00221 {
00222 qWarning() << myAppName << "process not running";
00223 return 4;
00224 }
00225 process->waitForStarted();
00226
00227 return 0;
00228 }