OSDN Git Service

It's 2011 now.
[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
34 #include "maemoglobal.h"
35
36 #include "maemoconstants.h"
37 #include "maemodeviceconfigurations.h"
38
39 #include <coreplugin/filemanager.h>
40 #include <coreplugin/ssh/sshconnection.h>
41 #include <qt4projectmanager/qtversionmanager.h>
42 #include <utils/environment.h>
43
44 #include <QtCore/QCoreApplication>
45 #include <QtGui/QDesktopServices>
46 #include <QtCore/QDir>
47 #include <QtCore/QProcess>
48 #include <QtCore/QString>
49
50 #define TR(text) QCoreApplication::translate("Qt4ProjectManager::Internal::MaemoGlobal", text)
51
52 namespace Qt4ProjectManager {
53 namespace Internal {
54 namespace {
55 static const QLatin1String binQmake("/bin/qmake" EXEC_SUFFIX);
56 }
57
58 QString MaemoGlobal::homeDirOnDevice(const QString &uname)
59 {
60     return uname == QLatin1String("root")
61         ? QString::fromLatin1("/root")
62         : QLatin1String("/home/") + uname;
63 }
64
65 QString MaemoGlobal::remoteSudo()
66 {
67     return QLatin1String("/usr/lib/mad-developer/devrootsh");
68 }
69
70 QString MaemoGlobal::remoteCommandPrefix(const QString &commandFilePath)
71 {
72     return QString::fromLocal8Bit("%1 chmod a+x %2; %3; ")
73         .arg(remoteSudo(), commandFilePath, remoteSourceProfilesCommand());
74 }
75
76 QString MaemoGlobal::remoteSourceProfilesCommand()
77 {
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);
84 }
85
86 QString MaemoGlobal::remoteEnvironment(const QList<Utils::EnvironmentItem> &list)
87 {
88     QString env;
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);
93 }
94
95 QString MaemoGlobal::failedToConnectToServerMessage(const Core::SshConnection::Ptr &connection,
96     const MaemoDeviceConfig &deviceConfig)
97 {
98     QString errorMsg = TR("Could not connect to host: %1")
99         .arg(connection->errorString());
100
101     if (deviceConfig.type == MaemoDeviceConfig::Simulator) {
102         if (connection->errorState() == Core::SshTimeoutError
103                 || connection->errorState() == Core::SshSocketError) {
104             errorMsg += TR("\nDid you start Qemu?");
105         }
106     } else if (connection->errorState() == Core::SshTimeoutError) {
107         errorMsg += TR("\nIs the device connected and set up for network access?");
108     }
109     return errorMsg;
110 }
111
112 QString MaemoGlobal::maddeRoot(const QtVersion *qtVersion)
113 {
114     QDir dir(targetRoot(qtVersion));
115     dir.cdUp(); dir.cdUp();
116     return dir.absolutePath();
117 }
118
119 QString MaemoGlobal::targetRoot(const QtVersion *qtVersion)
120 {
121     return QDir::cleanPath(qtVersion->qmakeCommand()).remove(binQmake);
122 }
123
124 QString MaemoGlobal::targetName(const QtVersion *qtVersion)
125 {
126     return QDir(targetRoot(qtVersion)).dirName();
127 }
128
129 QString MaemoGlobal::madAdminCommand(const QtVersion *qtVersion)
130 {
131     return maddeRoot(qtVersion) + QLatin1String("/bin/mad-admin");
132 }
133
134 QString MaemoGlobal::madCommand(const QtVersion *qtVersion)
135 {
136     return maddeRoot(qtVersion) + QLatin1String("/bin/mad");
137 }
138
139 MaemoGlobal::MaemoVersion MaemoGlobal::version(const QtVersion *qtVersion)
140 {
141     const QString &name = targetName(qtVersion);
142     if (name.startsWith(QLatin1String("fremantle")))
143         return Maemo5;
144     if (name.startsWith(QLatin1String("harmattan")))
145         return Maemo6;
146     qWarning("Unknown Maemo version!");
147     return static_cast<MaemoVersion>(-1);
148 }
149
150 bool MaemoGlobal::removeRecursively(const QString &filePath, QString &error)
151 {
152     error.clear();
153     QFileInfo fileInfo(filePath);
154     if (!fileInfo.exists())
155         return true;
156     QFile::setPermissions(filePath, fileInfo.permissions() | QFile::WriteUser);
157     if (fileInfo.isDir()) {
158         QDir dir(filePath);
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))
163                 return false;
164         }
165         dir.cdUp();
166         if (!dir.rmdir(fileInfo.fileName())) {
167             error = TR("Failed to remove directory '%1'.")
168                 .arg(QDir::toNativeSeparators(filePath));
169             return false;
170         }
171     } else {
172         if (!QFile::remove(filePath)) {
173             error = TR("Failed to remove file '%1'.")
174                 .arg(QDir::toNativeSeparators(filePath));
175             return false;
176         }
177     }
178     return true;
179 }
180
181 bool MaemoGlobal::callMad(QProcess &proc, const QStringList &args,
182     const QtVersion *qtVersion)
183 {
184     return callMaddeShellScript(proc, maddeRoot(qtVersion),
185         madCommand(qtVersion), args);
186 }
187
188 bool MaemoGlobal::callMadAdmin(QProcess &proc, const QStringList &args,
189     const QtVersion *qtVersion)
190 {
191     return callMaddeShellScript(proc, maddeRoot(qtVersion),
192         madAdminCommand(qtVersion), args);
193 }
194
195 bool MaemoGlobal::callMaddeShellScript(QProcess &proc, const QString &maddeRoot,
196     const QString &command, const QStringList &args)
197 {
198     if (!QFileInfo(command).exists())
199         return false;
200     QString actualCommand = command;
201     QStringList actualArgs = args;
202 #ifdef Q_OS_WIN
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");
210 #else
211     Q_UNUSED(maddeRoot);
212 #endif
213     proc.start(actualCommand, actualArgs);
214     return true;
215 }
216
217 MaemoGlobal::FileUpdate::FileUpdate(const QString &fileName)
218     : m_fileName(fileName)
219 {
220     Core::FileManager::instance()->expectFileChange(fileName);
221 }
222
223 MaemoGlobal::FileUpdate::~FileUpdate()
224 {
225     Core::FileManager::instance()->unexpectFileChange(m_fileName);
226 }
227
228 } // namespace Internal
229 } // namespace Qt4ProjectManager