1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
33 #include "maemoglobal.h"
35 #include "maemoconstants.h"
36 #include "maemodeviceconfigurations.h"
38 #include <projectexplorer/projectexplorerconstants.h>
40 #include <coreplugin/filemanager.h>
41 #include <utils/ssh/sshconnection.h>
42 #include <qt4projectmanager/qt4projectmanagerconstants.h>
43 #include <qt4projectmanager/qtversionmanager.h>
44 #include <utils/environment.h>
46 #include <QtGui/QDesktopServices>
47 #include <QtCore/QDir>
48 #include <QtCore/QProcess>
49 #include <QtCore/QString>
51 namespace Qt4ProjectManager {
54 static const QLatin1String binQmake("/bin/qmake" EXEC_SUFFIX);
57 bool MaemoGlobal::isMaemoTargetId(const QString &id)
59 return id == QLatin1String(Constants::MAEMO5_DEVICE_TARGET_ID)
60 || id == QLatin1String(Constants::HARMATTAN_DEVICE_TARGET_ID)
61 || id == QLatin1String(Constants::MEEGO_DEVICE_TARGET_ID);
64 bool MaemoGlobal::isValidMaemo5QtVersion(const QtVersion *version)
66 return isValidMaemoQtVersion(version, Maemo5);
69 bool MaemoGlobal::isValidHarmattanQtVersion(const QtVersion *version)
71 return isValidMaemoQtVersion(version, Maemo6);
74 bool MaemoGlobal::isValidMeegoQtVersion(const Qt4ProjectManager::QtVersion *version)
76 return isValidMaemoQtVersion(version, Meego);
79 bool MaemoGlobal::isValidMaemoQtVersion(const QtVersion *qtVersion,
80 MaemoVersion maemoVersion)
82 if (version(qtVersion) != maemoVersion)
84 QProcess madAdminProc;
85 const QStringList arguments(QLatin1String("list"));
86 if (!callMadAdmin(madAdminProc, arguments, qtVersion, false))
88 if (!madAdminProc.waitForStarted() || !madAdminProc.waitForFinished())
91 madAdminProc.setReadChannel(QProcess::StandardOutput);
92 const QByteArray tgtName = targetName(qtVersion).toAscii();
93 while (madAdminProc.canReadLine()) {
94 const QByteArray &line = madAdminProc.readLine();
95 if (line.contains(tgtName)
96 && (line.contains("(installed)") || line.contains("(default)")))
103 QString MaemoGlobal::homeDirOnDevice(const QString &uname)
105 return uname == QLatin1String("root")
106 ? QString::fromLatin1("/root")
107 : QLatin1String("/home/") + uname;
110 QString MaemoGlobal::remoteSudo()
112 return QLatin1String("/usr/lib/mad-developer/devrootsh");
115 QString MaemoGlobal::remoteCommandPrefix(MaemoVersion maemoVersion,
116 const QString &commandFilePath)
118 QString prefix = QString::fromLocal8Bit("%1 chmod a+x %2; %3; ")
119 .arg(remoteSudo(), commandFilePath, remoteSourceProfilesCommand());
120 if (maemoVersion != Maemo5 && maemoVersion != Maemo6)
121 prefix += QLatin1String("DISPLAY=:0.0 ");
125 QString MaemoGlobal::remoteSourceProfilesCommand()
127 const QList<QByteArray> profiles = QList<QByteArray>() << "/etc/profile"
128 << "/home/user/.profile" << "~/.profile";
129 QByteArray remoteCall(":");
130 foreach (const QByteArray &profile, profiles)
131 remoteCall += "; test -f " + profile + " && source " + profile;
132 return QString::fromAscii(remoteCall);
135 QString MaemoGlobal::remoteEnvironment(const QList<Utils::EnvironmentItem> &list)
138 QString placeHolder = QLatin1String("%1=%2 ");
139 foreach (const Utils::EnvironmentItem &item, list)
140 env.append(placeHolder.arg(item.name).arg(item.value));
141 return env.mid(0, env.size() - 1);
144 QString MaemoGlobal::failedToConnectToServerMessage(const Utils::SshConnection::Ptr &connection,
145 const MaemoDeviceConfig::ConstPtr &deviceConfig)
147 QString errorMsg = tr("Could not connect to host: %1")
148 .arg(connection->errorString());
150 if (deviceConfig->type() == MaemoDeviceConfig::Emulator) {
151 if (connection->errorState() == Utils::SshTimeoutError
152 || connection->errorState() == Utils::SshSocketError) {
153 errorMsg += tr("\nDid you start Qemu?");
155 } else if (connection->errorState() == Utils::SshTimeoutError) {
156 errorMsg += tr("\nIs the device connected and set up for network access?");
161 QString MaemoGlobal::deviceConfigurationName(const MaemoDeviceConfig::ConstPtr &devConf)
163 return devConf ? devConf->name() : tr("(No device)");
166 QString MaemoGlobal::maddeRoot(const QtVersion *qtVersion)
168 QDir dir(targetRoot(qtVersion));
169 dir.cdUp(); dir.cdUp();
170 return dir.absolutePath();
173 QString MaemoGlobal::targetRoot(const QtVersion *qtVersion)
175 return QDir::cleanPath(qtVersion->qmakeCommand()).remove(binQmake);
178 QString MaemoGlobal::targetName(const QtVersion *qtVersion)
180 return QDir(targetRoot(qtVersion)).dirName();
183 QString MaemoGlobal::madAdminCommand(const QtVersion *qtVersion)
185 return maddeRoot(qtVersion) + QLatin1String("/bin/mad-admin");
188 QString MaemoGlobal::madCommand(const QtVersion *qtVersion)
190 return maddeRoot(qtVersion) + QLatin1String("/bin/mad");
193 QString MaemoGlobal::madDeveloperUiName(MaemoVersion maemoVersion)
195 return maemoVersion == Maemo6
196 ? tr("SDK Connectivity") : tr("Mad Developer");
199 MaemoGlobal::MaemoVersion MaemoGlobal::version(const QtVersion *qtVersion)
201 const QString &name = targetName(qtVersion);
202 if (name.startsWith(QLatin1String("fremantle")))
204 if (name.startsWith(QLatin1String("harmattan")))
206 if (name.startsWith(QLatin1String("meego")))
208 return static_cast<MaemoVersion>(-1);
211 QString MaemoGlobal::architecture(const QtVersion *qtVersion)
214 const QStringList args = QStringList() << QLatin1String("uname")
215 << QLatin1String("-m");
216 if (!callMad(proc, args, qtVersion, true))
218 if (!proc.waitForFinished())
220 QString arch = QString::fromUtf8(proc.readAllStandardOutput());
221 arch.chop(1); // Newline
225 bool MaemoGlobal::removeRecursively(const QString &filePath, QString &error)
228 QFileInfo fileInfo(filePath);
229 if (!fileInfo.exists())
231 QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser);
232 if (fileInfo.isDir()) {
234 QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden
235 | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
236 foreach (const QString &fileName, fileNames) {
237 if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error))
241 if (!dir.rmdir(fileInfo.fileName())) {
242 error = tr("Failed to remove directory '%1'.")
243 .arg(QDir::toNativeSeparators(filePath));
247 if (!QFile::remove(filePath)) {
248 error = tr("Failed to remove file '%1'.")
249 .arg(QDir::toNativeSeparators(filePath));
256 bool MaemoGlobal::copyRecursively(const QString &srcFilePath,
257 const QString &tgtFilePath, QString *error)
259 QFileInfo srcFileInfo(srcFilePath);
260 if (srcFileInfo.isDir()) {
261 QDir targetDir(tgtFilePath);
263 if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) {
265 *error = tr("Failed to create directory '%1'.")
266 .arg(QDir::toNativeSeparators(tgtFilePath));
270 QDir sourceDir(srcFilePath);
271 QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
272 foreach (const QString &fileName, fileNames) {
273 const QString newSrcFilePath
274 = srcFilePath + QLatin1Char('/') + fileName;
275 const QString newTgtFilePath
276 = tgtFilePath + QLatin1Char('/') + fileName;
277 if (!copyRecursively(newSrcFilePath, newTgtFilePath))
281 if (!QFile::copy(srcFilePath, tgtFilePath)) {
283 *error = tr("Could not copy file '%1' to '%2'.")
284 .arg(QDir::toNativeSeparators(srcFilePath),
285 QDir::toNativeSeparators(tgtFilePath));
293 bool MaemoGlobal::callMad(QProcess &proc, const QStringList &args,
294 const QtVersion *qtVersion, bool useTarget)
296 return callMaddeShellScript(proc, qtVersion, madCommand(qtVersion), args,
300 bool MaemoGlobal::callMadAdmin(QProcess &proc, const QStringList &args,
301 const QtVersion *qtVersion, bool useTarget)
303 return callMaddeShellScript(proc, qtVersion, madAdminCommand(qtVersion),
307 bool MaemoGlobal::callMaddeShellScript(QProcess &proc,
308 const QtVersion *qtVersion, const QString &command, const QStringList &args,
311 if (!QFileInfo(command).exists())
313 QString actualCommand = command;
314 QStringList actualArgs = targetArgs(qtVersion, useTarget) + args;
316 Utils::Environment env(proc.systemEnvironment());
317 const QString root = maddeRoot(qtVersion);
318 env.prependOrSetPath(root + QLatin1String("/bin"));
319 env.prependOrSet(QLatin1String("HOME"),
320 QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
321 proc.setEnvironment(env.toStringList());
322 actualArgs.prepend(command);
323 actualCommand = root + QLatin1String("/bin/sh.exe");
325 proc.start(actualCommand, actualArgs);
329 QStringList MaemoGlobal::targetArgs(const QtVersion *qtVersion, bool useTarget)
333 args << QLatin1String("-t") << targetName(qtVersion);
338 QString MaemoGlobal::maemoVersionToString(MaemoVersion version)
341 case Maemo5: return QLatin1String("Maemo5/Fremantle");
342 case Maemo6: return QLatin1String("Harmattan");
343 case Meego: return QLatin1String("Meego");
345 qDebug("%s: Unknown OS Version %d.", Q_FUNC_INFO, version);
349 MaemoGlobal::PackagingSystem MaemoGlobal::packagingSystem(MaemoVersion maemoVersion)
351 return maemoVersion == Meego ? Rpm : Dpkg;
354 MaemoGlobal::FileUpdate::FileUpdate(const QString &fileName)
355 : m_fileName(fileName)
357 Core::FileManager::instance()->expectFileChange(fileName);
360 MaemoGlobal::FileUpdate::~FileUpdate()
362 Core::FileManager::instance()->unexpectFileChange(m_fileName);
365 } // namespace Internal
366 } // namespace Qt4ProjectManager