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 **************************************************************************/
34 #include "maemoglobal.h"
36 #include "maemoconstants.h"
37 #include "maemodeviceconfigurations.h"
39 #include <coreplugin/filemanager.h>
40 #include <coreplugin/ssh/sshconnection.h>
41 #include <qt4projectmanager/qtversionmanager.h>
42 #include <utils/environment.h>
44 #include <QtCore/QCoreApplication>
45 #include <QtGui/QDesktopServices>
46 #include <QtCore/QDir>
47 #include <QtCore/QProcess>
48 #include <QtCore/QString>
50 #define TR(text) QCoreApplication::translate("Qt4ProjectManager::Internal::MaemoGlobal", text)
52 namespace Qt4ProjectManager {
55 static const QLatin1String binQmake("/bin/qmake" EXEC_SUFFIX);
58 QString MaemoGlobal::homeDirOnDevice(const QString &uname)
60 return uname == QLatin1String("root")
61 ? QString::fromLatin1("/root")
62 : QLatin1String("/home/") + uname;
65 QString MaemoGlobal::remoteSudo()
67 return QLatin1String("/usr/lib/mad-developer/devrootsh");
70 QString MaemoGlobal::remoteCommandPrefix(const QString &commandFilePath)
72 return QString::fromLocal8Bit("%1 chmod a+x %2; %3; ")
73 .arg(remoteSudo(), commandFilePath, remoteSourceProfilesCommand());
76 QString MaemoGlobal::remoteSourceProfilesCommand()
78 const QList<QByteArray> profiles = QList<QByteArray>() << "/etc/profile"
79 << "/home/user/.profile" << "~/.profile";
80 QByteArray remoteCall(":");
81 foreach (const QByteArray &profile, profiles)
82 remoteCall += "; test -f " + profile + " && source " + profile;
83 return QString::fromAscii(remoteCall);
86 QString MaemoGlobal::remoteEnvironment(const QList<Utils::EnvironmentItem> &list)
89 QString placeHolder = QLatin1String("%1=%2 ");
90 foreach (const Utils::EnvironmentItem &item, list)
91 env.append(placeHolder.arg(item.name).arg(item.value));
92 return env.mid(0, env.size() - 1);
95 QString MaemoGlobal::failedToConnectToServerMessage(const Core::SshConnection::Ptr &connection,
96 const MaemoDeviceConfig &deviceConfig)
98 QString errorMsg = TR("Could not connect to host: %1")
99 .arg(connection->errorString());
101 if (deviceConfig.type == MaemoDeviceConfig::Simulator) {
102 if (connection->errorState() == Core::SshTimeoutError
103 || connection->errorState() == Core::SshSocketError) {
104 errorMsg += TR("\nDid you start Qemu?");
106 } else if (connection->errorState() == Core::SshTimeoutError) {
107 errorMsg += TR("\nIs the device connected and set up for network access?");
112 QString MaemoGlobal::maddeRoot(const QtVersion *qtVersion)
114 QDir dir(targetRoot(qtVersion));
115 dir.cdUp(); dir.cdUp();
116 return dir.absolutePath();
119 QString MaemoGlobal::targetRoot(const QtVersion *qtVersion)
121 return QDir::cleanPath(qtVersion->qmakeCommand()).remove(binQmake);
124 QString MaemoGlobal::targetName(const QtVersion *qtVersion)
126 return QDir(targetRoot(qtVersion)).dirName();
129 QString MaemoGlobal::madAdminCommand(const QtVersion *qtVersion)
131 return maddeRoot(qtVersion) + QLatin1String("/bin/mad-admin");
134 QString MaemoGlobal::madCommand(const QtVersion *qtVersion)
136 return maddeRoot(qtVersion) + QLatin1String("/bin/mad");
139 MaemoGlobal::MaemoVersion MaemoGlobal::version(const QtVersion *qtVersion)
141 const QString &name = targetName(qtVersion);
142 if (name.startsWith(QLatin1String("fremantle")))
144 if (name.startsWith(QLatin1String("harmattan")))
146 qWarning("Unknown Maemo version!");
147 return static_cast<MaemoVersion>(-1);
150 bool MaemoGlobal::removeRecursively(const QString &filePath, QString &error)
153 QFileInfo fileInfo(filePath);
154 if (!fileInfo.exists())
156 QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser);
157 if (fileInfo.isDir()) {
159 QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden
160 | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
161 foreach (const QString &fileName, fileNames) {
162 if (!removeRecursively(filePath + QLatin1Char('/') + fileName, error))
166 if (!dir.rmdir(fileInfo.fileName())) {
167 error = TR("Failed to remove directory '%1'.")
168 .arg(QDir::toNativeSeparators(filePath));
172 if (!QFile::remove(filePath)) {
173 error = TR("Failed to remove file '%1'.")
174 .arg(QDir::toNativeSeparators(filePath));
181 bool MaemoGlobal::callMad(QProcess &proc, const QStringList &args,
182 const QtVersion *qtVersion)
184 return callMaddeShellScript(proc, maddeRoot(qtVersion),
185 madCommand(qtVersion), args);
188 bool MaemoGlobal::callMadAdmin(QProcess &proc, const QStringList &args,
189 const QtVersion *qtVersion)
191 return callMaddeShellScript(proc, maddeRoot(qtVersion),
192 madAdminCommand(qtVersion), args);
195 bool MaemoGlobal::callMaddeShellScript(QProcess &proc, const QString &maddeRoot,
196 const QString &command, const QStringList &args)
198 if (!QFileInfo(command).exists())
200 QString actualCommand = command;
201 QStringList actualArgs = args;
203 Utils::Environment env(proc.systemEnvironment());
204 env.prependOrSetPath(maddeRoot + QLatin1String("/bin"));
205 env.prependOrSet(QLatin1String("HOME"),
206 QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
207 proc.setEnvironment(env.toStringList());
208 actualArgs.prepend(command);
209 actualCommand = maddeRoot + QLatin1String("/bin/sh.exe");
213 proc.start(actualCommand, actualArgs);
217 MaemoGlobal::FileUpdate::FileUpdate(const QString &fileName)
218 : m_fileName(fileName)
220 Core::FileManager::instance()->expectFileChange(fileName);
223 MaemoGlobal::FileUpdate::~FileUpdate()
225 Core::FileManager::instance()->unexpectFileChange(m_fileName);
228 } // namespace Internal
229 } // namespace Qt4ProjectManager