OSDN Git Service

29cda8833c281854e3412cca35ccf9cfac0e88c4
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / qt-maemo / 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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33 #include "maemoglobal.h"
34
35 #include "maemoconstants.h"
36 #include "maemodeviceconfigurations.h"
37
38 #include <projectexplorer/projectexplorerconstants.h>
39
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>
45
46 #include <QtGui/QDesktopServices>
47 #include <QtCore/QDir>
48 #include <QtCore/QProcess>
49 #include <QtCore/QString>
50
51 namespace Qt4ProjectManager {
52 namespace Internal {
53 namespace {
54 static const QLatin1String binQmake("/bin/qmake" EXEC_SUFFIX);
55 }
56
57 bool MaemoGlobal::isMaemoTargetId(const QString &id)
58 {
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);
62 }
63
64 bool MaemoGlobal::isValidMaemo5QtVersion(const QtVersion *version)
65 {
66     return isValidMaemoQtVersion(version, Maemo5);
67 }
68
69 bool MaemoGlobal::isValidHarmattanQtVersion(const QtVersion *version)
70 {
71     return isValidMaemoQtVersion(version, Maemo6);
72 }
73
74 bool MaemoGlobal::isValidMeegoQtVersion(const Qt4ProjectManager::QtVersion *version)
75 {
76     return isValidMaemoQtVersion(version, Meego);
77 }
78
79 bool MaemoGlobal::isValidMaemoQtVersion(const QtVersion *qtVersion,
80     MaemoVersion maemoVersion)
81 {
82     if (version(qtVersion) != maemoVersion)
83         return false;
84     QProcess madAdminProc;
85     const QStringList arguments(QLatin1String("list"));
86     if (!callMadAdmin(madAdminProc, arguments, qtVersion, false))
87         return false;
88     if (!madAdminProc.waitForStarted() || !madAdminProc.waitForFinished())
89         return false;
90
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)")))
97             return true;
98     }
99     return false;
100 }
101
102
103 QString MaemoGlobal::homeDirOnDevice(const QString &uname)
104 {
105     return uname == QLatin1String("root")
106         ? QString::fromLatin1("/root")
107         : QLatin1String("/home/") + uname;
108 }
109
110 QString MaemoGlobal::remoteSudo()
111 {
112     return QLatin1String("/usr/lib/mad-developer/devrootsh");
113 }
114
115 QString MaemoGlobal::remoteCommandPrefix(MaemoVersion maemoVersion,
116     const QString &commandFilePath)
117 {
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 ");
122     return prefix;
123 }
124
125 QString MaemoGlobal::remoteSourceProfilesCommand()
126 {
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);
133 }
134
135 QString MaemoGlobal::remoteEnvironment(const QList<Utils::EnvironmentItem> &list)
136 {
137     QString env;
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);
142 }
143
144 QString MaemoGlobal::failedToConnectToServerMessage(const Utils::SshConnection::Ptr &connection,
145     const MaemoDeviceConfig::ConstPtr &deviceConfig)
146 {
147     QString errorMsg = tr("Could not connect to host: %1")
148         .arg(connection->errorString());
149
150     if (deviceConfig->type() == MaemoDeviceConfig::Emulator) {
151         if (connection->errorState() == Utils::SshTimeoutError
152                 || connection->errorState() == Utils::SshSocketError) {
153             errorMsg += tr("\nDid you start Qemu?");
154         }
155     } else if (connection->errorState() == Utils::SshTimeoutError) {
156         errorMsg += tr("\nIs the device connected and set up for network access?");
157     }
158     return errorMsg;
159 }
160
161 QString MaemoGlobal::deviceConfigurationName(const MaemoDeviceConfig::ConstPtr &devConf)
162 {
163     return devConf ? devConf->name() : tr("(No device)");
164 }
165
166 QString MaemoGlobal::maddeRoot(const QtVersion *qtVersion)
167 {
168     QDir dir(targetRoot(qtVersion));
169     dir.cdUp(); dir.cdUp();
170     return dir.absolutePath();
171 }
172
173 QString MaemoGlobal::targetRoot(const QtVersion *qtVersion)
174 {
175     return QDir::cleanPath(qtVersion->qmakeCommand()).remove(binQmake);
176 }
177
178 QString MaemoGlobal::targetName(const QtVersion *qtVersion)
179 {
180     return QDir(targetRoot(qtVersion)).dirName();
181 }
182
183 QString MaemoGlobal::madAdminCommand(const QtVersion *qtVersion)
184 {
185     return maddeRoot(qtVersion) + QLatin1String("/bin/mad-admin");
186 }
187
188 QString MaemoGlobal::madCommand(const QtVersion *qtVersion)
189 {
190     return maddeRoot(qtVersion) + QLatin1String("/bin/mad");
191 }
192
193 QString MaemoGlobal::madDeveloperUiName(MaemoVersion maemoVersion)
194 {
195     return maemoVersion == Maemo6
196         ? tr("SDK Connectivity") : tr("Mad Developer");
197 }
198
199 MaemoGlobal::MaemoVersion MaemoGlobal::version(const QtVersion *qtVersion)
200 {
201     const QString &name = targetName(qtVersion);
202     if (name.startsWith(QLatin1String("fremantle")))
203         return Maemo5;
204     if (name.startsWith(QLatin1String("harmattan")))
205         return Maemo6;
206     if (name.startsWith(QLatin1String("meego")))
207         return Meego;
208     return static_cast<MaemoVersion>(-1);
209 }
210
211 QString MaemoGlobal::architecture(const QtVersion *qtVersion)
212 {
213     QProcess proc;
214     const QStringList args = QStringList() << QLatin1String("uname")
215         << QLatin1String("-m");
216     if (!callMad(proc, args, qtVersion, true))
217         return QString();
218     if (!proc.waitForFinished())
219         return QString();
220     QString arch = QString::fromUtf8(proc.readAllStandardOutput());
221     arch.chop(1); // Newline
222     return arch;
223 }
224
225 bool MaemoGlobal::removeRecursively(const QString &filePath, QString &error)
226 {
227     error.clear();
228     QFileInfo fileInfo(filePath);
229     if (!fileInfo.exists())
230         return true;
231     QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser);
232     if (fileInfo.isDir()) {
233         QDir dir(filePath);
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))
238                 return false;
239         }
240         dir.cdUp();
241         if (!dir.rmdir(fileInfo.fileName())) {
242             error = tr("Failed to remove directory '%1'.")
243                 .arg(QDir::toNativeSeparators(filePath));
244             return false;
245         }
246     } else {
247         if (!QFile::remove(filePath)) {
248             error = tr("Failed to remove file '%1'.")
249                 .arg(QDir::toNativeSeparators(filePath));
250             return false;
251         }
252     }
253     return true;
254 }
255
256 bool MaemoGlobal::copyRecursively(const QString &srcFilePath,
257     const QString &tgtFilePath, QString *error)
258 {
259     QFileInfo srcFileInfo(srcFilePath);
260     if (srcFileInfo.isDir()) {
261         QDir targetDir(tgtFilePath);
262         targetDir.cdUp();
263         if (!targetDir.mkdir(QFileInfo(tgtFilePath).fileName())) {
264             if (error) {
265                 *error = tr("Failed to create directory '%1'.")
266                     .arg(QDir::toNativeSeparators(tgtFilePath));
267                 return false;
268             }
269         }
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))
278                 return false;
279         }
280     } else {
281         if (!QFile::copy(srcFilePath, tgtFilePath)) {
282             if (error) {
283                 *error = tr("Could not copy file '%1' to '%2'.")
284                     .arg(QDir::toNativeSeparators(srcFilePath),
285                          QDir::toNativeSeparators(tgtFilePath));
286             }
287             return false;
288         }
289     }
290     return true;
291 }
292
293 bool MaemoGlobal::callMad(QProcess &proc, const QStringList &args,
294     const QtVersion *qtVersion, bool useTarget)
295 {
296     return callMaddeShellScript(proc, qtVersion, madCommand(qtVersion), args,
297         useTarget);
298 }
299
300 bool MaemoGlobal::callMadAdmin(QProcess &proc, const QStringList &args,
301     const QtVersion *qtVersion, bool useTarget)
302 {
303     return callMaddeShellScript(proc, qtVersion, madAdminCommand(qtVersion),
304         args, useTarget);
305 }
306
307 bool MaemoGlobal::callMaddeShellScript(QProcess &proc,
308     const QtVersion *qtVersion, const QString &command, const QStringList &args,
309     bool useTarget)
310 {
311     if (!QFileInfo(command).exists())
312         return false;
313     QString actualCommand = command;
314     QStringList actualArgs = targetArgs(qtVersion, useTarget) + args;
315 #ifdef Q_OS_WIN
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");
324 #endif
325     proc.start(actualCommand, actualArgs);
326     return true;
327 }
328
329 QStringList MaemoGlobal::targetArgs(const QtVersion *qtVersion, bool useTarget)
330 {
331     QStringList args;
332     if (useTarget) {
333         args << QLatin1String("-t") << targetName(qtVersion);
334     }
335     return args;
336 }
337
338 QString MaemoGlobal::maemoVersionToString(MaemoVersion version)
339 {
340     switch (version) {
341     case Maemo5: return QLatin1String("Maemo5/Fremantle");
342     case Maemo6: return QLatin1String("Harmattan");
343     case Meego: return QLatin1String("Meego");
344     }
345     qDebug("%s: Unknown OS Version %d.", Q_FUNC_INFO, version);
346     return QString();
347 }
348
349 MaemoGlobal::PackagingSystem MaemoGlobal::packagingSystem(MaemoVersion maemoVersion)
350 {
351     return maemoVersion == Meego ? Rpm : Dpkg;
352 }
353
354 MaemoGlobal::FileUpdate::FileUpdate(const QString &fileName)
355     : m_fileName(fileName)
356 {
357     Core::FileManager::instance()->expectFileChange(fileName);
358 }
359
360 MaemoGlobal::FileUpdate::~FileUpdate()
361 {
362     Core::FileManager::instance()->unexpectFileChange(m_fileName);
363 }
364
365 } // namespace Internal
366 } // namespace Qt4ProjectManager