OSDN Git Service

RemoteLinux: Move generic Dir/File functions into Utils.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / remotelinux / maemoglobal.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
30 **
31 **************************************************************************/
32 #include "maemoglobal.h"
33
34 #include "maemoconstants.h"
35 #include "maemoqemumanager.h"
36
37 #include <projectexplorer/projectexplorerconstants.h>
38
39 #include <coreplugin/filemanager.h>
40 #include <extensionsystem/pluginmanager.h>
41 #include <utils/ssh/sshconnection.h>
42 #include <qt4projectmanager/qt4projectmanagerconstants.h>
43 #include <qtsupport/qtversionmanager.h>
44 #include <qt4projectmanager/qt4target.h>
45 #include <utils/environment.h>
46
47 #include <QtCore/QDateTime>
48 #include <QtCore/QDir>
49 #include <QtCore/QFileInfo>
50 #include <QtCore/QProcess>
51 #include <QtCore/QString>
52 #include <QtGui/QDesktopServices>
53
54 using namespace Qt4ProjectManager;
55 using namespace Qt4ProjectManager::Constants;
56
57 namespace RemoteLinux {
58 namespace Internal {
59 namespace {
60 static const QLatin1String binQmake("/bin/qmake" EXEC_SUFFIX);
61 }
62
63 bool MaemoGlobal::isMaemoTargetId(const QString &id)
64 {
65     return isFremantleTargetId(id) || isHarmattanTargetId(id)
66         || isMeegoTargetId(id);
67 }
68
69 bool MaemoGlobal::isFremantleTargetId(const QString &id)
70 {
71     return id == QLatin1String(MAEMO5_DEVICE_TARGET_ID);
72 }
73
74 bool MaemoGlobal::isHarmattanTargetId(const QString &id)
75 {
76     return id == QLatin1String(HARMATTAN_DEVICE_TARGET_ID);
77 }
78
79 bool MaemoGlobal::isMeegoTargetId(const QString &id)
80 {
81     return id == QLatin1String(MEEGO_DEVICE_TARGET_ID);
82 }
83
84 bool MaemoGlobal::isValidMaemo5QtVersion(const QString &qmakePath)
85 {
86     return isValidMaemoQtVersion(qmakePath, LinuxDeviceConfiguration::Maemo5OsType);
87 }
88
89 bool MaemoGlobal::isValidHarmattanQtVersion(const QString &qmakePath)
90 {
91     return isValidMaemoQtVersion(qmakePath, LinuxDeviceConfiguration::HarmattanOsType);
92 }
93
94 bool MaemoGlobal::isValidMeegoQtVersion(const QString &qmakePath)
95 {
96     return isValidMaemoQtVersion(qmakePath, LinuxDeviceConfiguration::MeeGoOsType);
97 }
98
99 bool MaemoGlobal::isLinuxQt(const QtSupport::BaseQtVersion *qtVersion)
100 {
101     if (!qtVersion)
102         return false;
103     const QList<ProjectExplorer::Abi> &abis = qtVersion->qtAbis();
104     foreach (const ProjectExplorer::Abi &abi, abis) {
105         if (abi.os() == ProjectExplorer::Abi::LinuxOS)
106             return true;
107     }
108     return false;
109 }
110
111 bool MaemoGlobal::hasLinuxQt(const ProjectExplorer::Target *target)
112 {
113     const Qt4BaseTarget * const qtTarget
114         = qobject_cast<const Qt4BaseTarget *>(target);
115     if (!qtTarget)
116         return false;
117     const Qt4BuildConfiguration * const bc
118         = qtTarget->activeBuildConfiguration();
119     return bc && isLinuxQt(bc->qtVersion());
120 }
121
122 bool MaemoGlobal::isValidMaemoQtVersion(const QString &qmakePath, const QString &osType)
123 {
124     if (MaemoGlobal::osType(qmakePath) != osType)
125         return false;
126     QProcess madAdminProc;
127     const QStringList arguments(QLatin1String("list"));
128     if (!callMadAdmin(madAdminProc, arguments, qmakePath, false))
129         return false;
130     if (!madAdminProc.waitForStarted() || !madAdminProc.waitForFinished())
131         return false;
132
133     madAdminProc.setReadChannel(QProcess::StandardOutput);
134     const QByteArray tgtName = targetName(qmakePath).toAscii();
135     while (madAdminProc.canReadLine()) {
136         const QByteArray &line = madAdminProc.readLine();
137         if (line.contains(tgtName)
138             && (line.contains("(installed)") || line.contains("(default)")))
139             return true;
140     }
141     return false;
142 }
143
144
145 QString MaemoGlobal::homeDirOnDevice(const QString &uname)
146 {
147     return uname == QLatin1String("root")
148         ? QString::fromLatin1("/root")
149         : QLatin1String("/home/") + uname;
150 }
151
152 QString MaemoGlobal::devrootshPath()
153 {
154     return QLatin1String("/usr/lib/mad-developer/devrootsh");
155 }
156
157 int MaemoGlobal::applicationIconSize(const QString &osType)
158 {
159     return osType == LinuxDeviceConfiguration::HarmattanOsType ? 80 : 64;
160 }
161
162 QString MaemoGlobal::remoteSudo(const QString &osType, const QString &uname)
163 {
164     if (uname == QLatin1String("root"))
165         return QString();
166     if (osType == LinuxDeviceConfiguration::Maemo5OsType
167             || osType == LinuxDeviceConfiguration::HarmattanOsType
168             || osType == LinuxDeviceConfiguration::MeeGoOsType) {
169         return devrootshPath();
170     }
171     return QString(); // Using sudo would open a can of worms.
172 }
173
174 QString MaemoGlobal::remoteCommandPrefix(const QString &osType)
175 {
176     QString prefix = QString::fromLocal8Bit("%1; ").arg(remoteSourceProfilesCommand());
177     if (osType != LinuxDeviceConfiguration::Maemo5OsType
178             && osType != LinuxDeviceConfiguration::HarmattanOsType) {
179         prefix += QLatin1String("DISPLAY=:0.0 ");
180     }
181     return prefix;
182 }
183
184 QString MaemoGlobal::remoteSourceProfilesCommand()
185 {
186     const QList<QByteArray> profiles = QList<QByteArray>() << "/etc/profile"
187         << "/home/user/.profile" << "~/.profile";
188     QByteArray remoteCall(":");
189     foreach (const QByteArray &profile, profiles)
190         remoteCall += "; test -f " + profile + " && source " + profile;
191     return QString::fromAscii(remoteCall);
192 }
193
194 QString MaemoGlobal::failedToConnectToServerMessage(const Utils::SshConnection::Ptr &connection,
195     const LinuxDeviceConfiguration::ConstPtr &deviceConfig)
196 {
197     QString errorMsg = tr("Could not connect to host: %1")
198         .arg(connection->errorString());
199
200     if (deviceConfig->type() == LinuxDeviceConfiguration::Emulator) {
201         if (connection->errorState() == Utils::SshTimeoutError
202                 || connection->errorState() == Utils::SshSocketError) {
203             errorMsg += tr("\nDid you start Qemu?");
204         }
205    } else if (connection->errorState() == Utils::SshTimeoutError) {
206         errorMsg += tr("\nIs the device connected and set up for network access?");
207     }
208     return errorMsg;
209 }
210
211 QString MaemoGlobal::deviceConfigurationName(const LinuxDeviceConfiguration::ConstPtr &devConf)
212 {
213     return devConf ? devConf->name() : tr("(No device)");
214 }
215
216 PortList MaemoGlobal::freePorts(const LinuxDeviceConfiguration::ConstPtr &devConf,
217     const QtSupport::BaseQtVersion *qtVersion)
218 {
219     if (!devConf || !qtVersion)
220         return PortList();
221     if (devConf->type() == LinuxDeviceConfiguration::Emulator) {
222         MaemoQemuRuntime rt;
223         const int id = qtVersion->uniqueId();
224         if (MaemoQemuManager::instance().runtimeForQtVersion(id, &rt))
225             return rt.m_freePorts;
226     }
227     return devConf->freePorts();
228 }
229
230 QString MaemoGlobal::maddeRoot(const QString &qmakePath)
231 {
232     QDir dir(targetRoot(qmakePath));
233     dir.cdUp(); dir.cdUp();
234     return dir.absolutePath();
235 }
236
237 QString MaemoGlobal::targetRoot(const QString &qmakePath)
238 {
239     return QDir::cleanPath(qmakePath).remove(binQmake);
240 }
241
242 QString MaemoGlobal::targetName(const QString &qmakePath)
243 {
244     return QDir(targetRoot(qmakePath)).dirName();
245 }
246
247 QString MaemoGlobal::madAdminCommand(const QString &qmakePath)
248 {
249     return maddeRoot(qmakePath) + QLatin1String("/bin/mad-admin");
250 }
251
252 QString MaemoGlobal::madCommand(const QString &qmakePath)
253 {
254     return maddeRoot(qmakePath) + QLatin1String("/bin/mad");
255 }
256
257 QString MaemoGlobal::madDeveloperUiName(const QString &osType)
258 {
259     return osType == LinuxDeviceConfiguration::HarmattanOsType
260         ? tr("SDK Connectivity") : tr("Mad Developer");
261 }
262
263 QString MaemoGlobal::osType(const QString &qmakePath)
264 {
265     const QString &name = targetName(qmakePath);
266     if (name.startsWith(QLatin1String("fremantle")))
267         return LinuxDeviceConfiguration::Maemo5OsType;
268     if (name.startsWith(QLatin1String("harmattan")))
269         return LinuxDeviceConfiguration::HarmattanOsType;
270     if (name.startsWith(QLatin1String("meego")))
271         return LinuxDeviceConfiguration::MeeGoOsType;
272     return LinuxDeviceConfiguration::GenericLinuxOsType;
273 }
274
275 QString MaemoGlobal::architecture(const QString &qmakePath)
276 {
277     QProcess proc;
278     const QStringList args = QStringList() << QLatin1String("uname")
279         << QLatin1String("-m");
280     if (!callMad(proc, args, qmakePath, true))
281         return QString();
282     if (!proc.waitForFinished())
283         return QString();
284     QString arch = QString::fromUtf8(proc.readAllStandardOutput());
285     arch.chop(1); // Newline
286     return arch;
287 }
288
289 void MaemoGlobal::addMaddeEnvironment(Utils::Environment &env, const QString &qmakePath)
290 {
291     Utils::Environment maddeEnv;
292 #ifdef Q_OS_WIN
293     const QString root = maddeRoot(qmakePath);
294     env.prependOrSetPath(root + QLatin1String("/bin"));
295     env.prependOrSet(QLatin1String("HOME"),
296         QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
297 #else
298     Q_UNUSED(qmakePath);
299 #endif
300     for (Utils::Environment::const_iterator it = maddeEnv.constBegin(); it != maddeEnv.constEnd(); ++it)
301         env.prependOrSet(it.key(), it.value());
302 }
303
304 void MaemoGlobal::transformMaddeCall(QString &command, QStringList &args, const QString &qmakePath)
305 {
306 #ifdef Q_OS_WIN
307     const QString root = maddeRoot(qmakePath);
308     args.prepend(command);
309     command = root + QLatin1String("/bin/sh.exe");
310 #else
311     Q_UNUSED(command);
312     Q_UNUSED(args);
313     Q_UNUSED(qmakePath);
314 #endif
315 }
316
317 bool MaemoGlobal::callMad(QProcess &proc, const QStringList &args,
318     const QString &qmakePath, bool useTarget)
319 {
320     return callMaddeShellScript(proc, qmakePath, madCommand(qmakePath), args,
321         useTarget);
322 }
323
324 bool MaemoGlobal::callMadAdmin(QProcess &proc, const QStringList &args,
325     const QString &qmakePath, bool useTarget)
326 {
327     return callMaddeShellScript(proc, qmakePath, madAdminCommand(qmakePath),
328         args, useTarget);
329 }
330
331 bool MaemoGlobal::callMaddeShellScript(QProcess &proc,
332     const QString &qmakePath, const QString &command, const QStringList &args,
333     bool useTarget)
334 {
335     if (!QFileInfo(command).exists())
336         return false;
337     QString actualCommand = command;
338     QStringList actualArgs = targetArgs(qmakePath, useTarget) + args;
339     Utils::Environment env(proc.systemEnvironment());
340     addMaddeEnvironment(env, qmakePath);
341     proc.setEnvironment(env.toStringList());
342     transformMaddeCall(actualCommand, actualArgs, qmakePath);
343     proc.start(actualCommand, actualArgs);
344     return true;
345 }
346
347 QStringList MaemoGlobal::targetArgs(const QString &qmakePath, bool useTarget)
348 {
349     QStringList args;
350     if (useTarget) {
351         args << QLatin1String("-t") << targetName(qmakePath);
352     }
353     return args;
354 }
355
356 QString MaemoGlobal::osTypeToString(const QString &osType)
357 {
358     const QList<ILinuxDeviceConfigurationFactory *> &factories
359         = ExtensionSystem::PluginManager::instance()->getObjects<ILinuxDeviceConfigurationFactory>();
360     foreach (const ILinuxDeviceConfigurationFactory * const factory, factories) {
361         if (factory->supportsOsType(osType))
362             return factory->displayNameForOsType(osType);
363     }
364     return tr("Unknown OS");
365 }
366
367 MaemoGlobal::PackagingSystem MaemoGlobal::packagingSystem(const QString &osType)
368 {
369     if (osType == LinuxDeviceConfiguration::Maemo5OsType
370            || osType == LinuxDeviceConfiguration::HarmattanOsType) {
371         return Dpkg;
372     }
373     if (osType == LinuxDeviceConfiguration::MeeGoOsType)
374         return Rpm;
375     return Tar;
376 }
377
378 } // namespace Internal
379 } // namespace RemoteLinux