1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
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.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at info@qt.nokia.com.
31 **************************************************************************/
32 #include "maemoglobal.h"
34 #include "maemoconstants.h"
35 #include "maemoqemumanager.h"
37 #include <projectexplorer/projectexplorerconstants.h>
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>
47 #include <QtCore/QDateTime>
48 #include <QtCore/QDir>
49 #include <QtCore/QFileInfo>
50 #include <QtCore/QProcess>
51 #include <QtCore/QString>
52 #include <QtGui/QDesktopServices>
54 using namespace Qt4ProjectManager;
55 using namespace Qt4ProjectManager::Constants;
57 namespace RemoteLinux {
60 static const QLatin1String binQmake("/bin/qmake" EXEC_SUFFIX);
63 bool MaemoGlobal::isMaemoTargetId(const QString &id)
65 return isFremantleTargetId(id) || isHarmattanTargetId(id)
66 || isMeegoTargetId(id);
69 bool MaemoGlobal::isFremantleTargetId(const QString &id)
71 return id == QLatin1String(MAEMO5_DEVICE_TARGET_ID);
74 bool MaemoGlobal::isHarmattanTargetId(const QString &id)
76 return id == QLatin1String(HARMATTAN_DEVICE_TARGET_ID);
79 bool MaemoGlobal::isMeegoTargetId(const QString &id)
81 return id == QLatin1String(MEEGO_DEVICE_TARGET_ID);
84 bool MaemoGlobal::isValidMaemo5QtVersion(const QString &qmakePath)
86 return isValidMaemoQtVersion(qmakePath, LinuxDeviceConfiguration::Maemo5OsType);
89 bool MaemoGlobal::isValidHarmattanQtVersion(const QString &qmakePath)
91 return isValidMaemoQtVersion(qmakePath, LinuxDeviceConfiguration::HarmattanOsType);
94 bool MaemoGlobal::isValidMeegoQtVersion(const QString &qmakePath)
96 return isValidMaemoQtVersion(qmakePath, LinuxDeviceConfiguration::MeeGoOsType);
99 bool MaemoGlobal::isLinuxQt(const QtSupport::BaseQtVersion *qtVersion)
103 const QList<ProjectExplorer::Abi> &abis = qtVersion->qtAbis();
104 foreach (const ProjectExplorer::Abi &abi, abis) {
105 if (abi.os() == ProjectExplorer::Abi::LinuxOS)
111 bool MaemoGlobal::hasLinuxQt(const ProjectExplorer::Target *target)
113 const Qt4BaseTarget * const qtTarget
114 = qobject_cast<const Qt4BaseTarget *>(target);
117 const Qt4BuildConfiguration * const bc
118 = qtTarget->activeBuildConfiguration();
119 return bc && isLinuxQt(bc->qtVersion());
122 bool MaemoGlobal::isValidMaemoQtVersion(const QString &qmakePath, const QString &osType)
124 if (MaemoGlobal::osType(qmakePath) != osType)
126 QProcess madAdminProc;
127 const QStringList arguments(QLatin1String("list"));
128 if (!callMadAdmin(madAdminProc, arguments, qmakePath, false))
130 if (!madAdminProc.waitForStarted() || !madAdminProc.waitForFinished())
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)")))
145 QString MaemoGlobal::homeDirOnDevice(const QString &uname)
147 return uname == QLatin1String("root")
148 ? QString::fromLatin1("/root")
149 : QLatin1String("/home/") + uname;
152 QString MaemoGlobal::devrootshPath()
154 return QLatin1String("/usr/lib/mad-developer/devrootsh");
157 int MaemoGlobal::applicationIconSize(const QString &osType)
159 return osType == LinuxDeviceConfiguration::HarmattanOsType ? 80 : 64;
162 QString MaemoGlobal::remoteSudo(const QString &osType, const QString &uname)
164 if (uname == QLatin1String("root"))
166 if (osType == LinuxDeviceConfiguration::Maemo5OsType
167 || osType == LinuxDeviceConfiguration::HarmattanOsType
168 || osType == LinuxDeviceConfiguration::MeeGoOsType) {
169 return devrootshPath();
171 return QString(); // Using sudo would open a can of worms.
174 QString MaemoGlobal::remoteCommandPrefix(const QString &osType)
176 QString prefix = QString::fromLocal8Bit("%1; ").arg(remoteSourceProfilesCommand());
177 if (osType != LinuxDeviceConfiguration::Maemo5OsType
178 && osType != LinuxDeviceConfiguration::HarmattanOsType) {
179 prefix += QLatin1String("DISPLAY=:0.0 ");
184 QString MaemoGlobal::remoteSourceProfilesCommand()
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);
194 QString MaemoGlobal::failedToConnectToServerMessage(const Utils::SshConnection::Ptr &connection,
195 const LinuxDeviceConfiguration::ConstPtr &deviceConfig)
197 QString errorMsg = tr("Could not connect to host: %1")
198 .arg(connection->errorString());
200 if (deviceConfig->type() == LinuxDeviceConfiguration::Emulator) {
201 if (connection->errorState() == Utils::SshTimeoutError
202 || connection->errorState() == Utils::SshSocketError) {
203 errorMsg += tr("\nDid you start Qemu?");
205 } else if (connection->errorState() == Utils::SshTimeoutError) {
206 errorMsg += tr("\nIs the device connected and set up for network access?");
211 QString MaemoGlobal::deviceConfigurationName(const LinuxDeviceConfiguration::ConstPtr &devConf)
213 return devConf ? devConf->name() : tr("(No device)");
216 PortList MaemoGlobal::freePorts(const LinuxDeviceConfiguration::ConstPtr &devConf,
217 const QtSupport::BaseQtVersion *qtVersion)
219 if (!devConf || !qtVersion)
221 if (devConf->type() == LinuxDeviceConfiguration::Emulator) {
223 const int id = qtVersion->uniqueId();
224 if (MaemoQemuManager::instance().runtimeForQtVersion(id, &rt))
225 return rt.m_freePorts;
227 return devConf->freePorts();
230 QString MaemoGlobal::maddeRoot(const QString &qmakePath)
232 QDir dir(targetRoot(qmakePath));
233 dir.cdUp(); dir.cdUp();
234 return dir.absolutePath();
237 QString MaemoGlobal::targetRoot(const QString &qmakePath)
239 return QDir::cleanPath(qmakePath).remove(binQmake);
242 QString MaemoGlobal::targetName(const QString &qmakePath)
244 return QDir(targetRoot(qmakePath)).dirName();
247 QString MaemoGlobal::madAdminCommand(const QString &qmakePath)
249 return maddeRoot(qmakePath) + QLatin1String("/bin/mad-admin");
252 QString MaemoGlobal::madCommand(const QString &qmakePath)
254 return maddeRoot(qmakePath) + QLatin1String("/bin/mad");
257 QString MaemoGlobal::madDeveloperUiName(const QString &osType)
259 return osType == LinuxDeviceConfiguration::HarmattanOsType
260 ? tr("SDK Connectivity") : tr("Mad Developer");
263 QString MaemoGlobal::osType(const QString &qmakePath)
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;
275 QString MaemoGlobal::architecture(const QString &qmakePath)
278 const QStringList args = QStringList() << QLatin1String("uname")
279 << QLatin1String("-m");
280 if (!callMad(proc, args, qmakePath, true))
282 if (!proc.waitForFinished())
284 QString arch = QString::fromUtf8(proc.readAllStandardOutput());
285 arch.chop(1); // Newline
289 void MaemoGlobal::addMaddeEnvironment(Utils::Environment &env, const QString &qmakePath)
291 Utils::Environment maddeEnv;
293 const QString root = maddeRoot(qmakePath);
294 env.prependOrSetPath(root + QLatin1String("/bin"));
295 env.prependOrSet(QLatin1String("HOME"),
296 QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
300 for (Utils::Environment::const_iterator it = maddeEnv.constBegin(); it != maddeEnv.constEnd(); ++it)
301 env.prependOrSet(it.key(), it.value());
304 void MaemoGlobal::transformMaddeCall(QString &command, QStringList &args, const QString &qmakePath)
307 const QString root = maddeRoot(qmakePath);
308 args.prepend(command);
309 command = root + QLatin1String("/bin/sh.exe");
317 bool MaemoGlobal::callMad(QProcess &proc, const QStringList &args,
318 const QString &qmakePath, bool useTarget)
320 return callMaddeShellScript(proc, qmakePath, madCommand(qmakePath), args,
324 bool MaemoGlobal::callMadAdmin(QProcess &proc, const QStringList &args,
325 const QString &qmakePath, bool useTarget)
327 return callMaddeShellScript(proc, qmakePath, madAdminCommand(qmakePath),
331 bool MaemoGlobal::callMaddeShellScript(QProcess &proc,
332 const QString &qmakePath, const QString &command, const QStringList &args,
335 if (!QFileInfo(command).exists())
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);
347 QStringList MaemoGlobal::targetArgs(const QString &qmakePath, bool useTarget)
351 args << QLatin1String("-t") << targetName(qmakePath);
356 QString MaemoGlobal::osTypeToString(const QString &osType)
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);
364 return tr("Unknown OS");
367 MaemoGlobal::PackagingSystem MaemoGlobal::packagingSystem(const QString &osType)
369 if (osType == LinuxDeviceConfiguration::Maemo5OsType
370 || osType == LinuxDeviceConfiguration::HarmattanOsType) {
373 if (osType == LinuxDeviceConfiguration::MeeGoOsType)
378 } // namespace Internal
379 } // namespace RemoteLinux