|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the tools applications of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
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 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "CeTcpSyncConnection.h" |
|
43 #include <qdir.h> |
|
44 #include <qfile.h> |
|
45 #include <qfileinfo> |
|
46 |
|
47 static const QString ceTcpSyncProgram = "cetcpsync"; |
|
48 extern void debugOutput(const QString& text, int level); |
|
49 |
|
50 CeTcpSyncConnection::CeTcpSyncConnection() |
|
51 : AbstractRemoteConnection() |
|
52 , connected(false) |
|
53 { |
|
54 } |
|
55 |
|
56 CeTcpSyncConnection::~CeTcpSyncConnection() |
|
57 { |
|
58 if (isConnected()) |
|
59 disconnect(); |
|
60 } |
|
61 |
|
62 bool CeTcpSyncConnection::connect(QVariantList&) |
|
63 { |
|
64 // We connect with each command, so this is always true |
|
65 // The command itself will fail then |
|
66 const QString cmd = ceTcpSyncProgram + " noop"; |
|
67 if (system(qPrintable(cmd)) != 0) |
|
68 return false; |
|
69 connected = true; |
|
70 return true; |
|
71 } |
|
72 |
|
73 void CeTcpSyncConnection::disconnect() |
|
74 { |
|
75 connected = false; |
|
76 } |
|
77 |
|
78 bool CeTcpSyncConnection::isConnected() const |
|
79 { |
|
80 return connected; |
|
81 } |
|
82 |
|
83 inline QString boolToString(bool b) |
|
84 { |
|
85 return b ? "true" : "false"; |
|
86 } |
|
87 |
|
88 static bool fileTimeFromString(FILETIME& ft, const QString& str) |
|
89 { |
|
90 int idx = str.indexOf("*"); |
|
91 if (idx <= 0) |
|
92 return false; |
|
93 bool ok; |
|
94 ft.dwLowDateTime = str.left(idx).toULong(&ok); |
|
95 if (!ok) |
|
96 return false; |
|
97 ft.dwHighDateTime = str.mid(idx+1).toULong(&ok); |
|
98 return ok; |
|
99 } |
|
100 |
|
101 static QString fileTimeToString(FILETIME& ft) |
|
102 { |
|
103 return QString::number(ft.dwLowDateTime) + "*" + QString::number(ft.dwHighDateTime); |
|
104 } |
|
105 |
|
106 bool CeTcpSyncConnection::copyFileToDevice(const QString &localSource, const QString &deviceDest, bool failIfExists) |
|
107 { |
|
108 QString cmd = ceTcpSyncProgram + " copyFileToDevice \"" + localSource + "\" \"" + deviceDest + "\" " + boolToString(failIfExists); |
|
109 return system(qPrintable(cmd)) == 0; |
|
110 } |
|
111 |
|
112 bool CeTcpSyncConnection::copyDirectoryToDevice(const QString &localSource, const QString &deviceDest, bool recursive) |
|
113 { |
|
114 QString cmd = ceTcpSyncProgram + " copyDirectoryToDevice \"" + localSource + "\" \"" + deviceDest + "\" " + boolToString(recursive); |
|
115 return system(qPrintable(cmd)) == 0; |
|
116 } |
|
117 |
|
118 bool CeTcpSyncConnection::copyFileFromDevice(const QString &deviceSource, const QString &localDest, bool failIfExists) |
|
119 { |
|
120 QString cmd = ceTcpSyncProgram + " copyFileFromDevice \"" + deviceSource + "\" \"" + localDest + "\" " + boolToString(failIfExists); |
|
121 return system(qPrintable(cmd)) == 0; |
|
122 } |
|
123 |
|
124 bool CeTcpSyncConnection::copyDirectoryFromDevice(const QString &deviceSource, const QString &localDest, bool recursive) |
|
125 { |
|
126 QString cmd = ceTcpSyncProgram + " copyDirectoryFromDevice \"" + deviceSource + "\" \"" + localDest + "\" " + boolToString(recursive); |
|
127 return system(qPrintable(cmd)) == 0; |
|
128 } |
|
129 |
|
130 bool CeTcpSyncConnection::copyFile(const QString &srcFile, const QString &destFile, bool failIfExists) |
|
131 { |
|
132 QString cmd = ceTcpSyncProgram + " copyFile \"" + srcFile + "\" \"" + destFile + "\" " + boolToString(failIfExists); |
|
133 return system(qPrintable(cmd)) == 0; |
|
134 } |
|
135 |
|
136 bool CeTcpSyncConnection::copyDirectory(const QString &srcDirectory, const QString &destDirectory, |
|
137 bool recursive) |
|
138 { |
|
139 QString cmd = ceTcpSyncProgram + " copyDirectory \"" + srcDirectory + "\" \"" + destDirectory + "\" " + boolToString(recursive); |
|
140 return system(qPrintable(cmd)) == 0; |
|
141 } |
|
142 |
|
143 bool CeTcpSyncConnection::deleteFile(const QString &fileName) |
|
144 { |
|
145 QString cmd = ceTcpSyncProgram + " deleteFile \"" + fileName + "\""; |
|
146 return system(qPrintable(cmd)) == 0; |
|
147 } |
|
148 |
|
149 bool CeTcpSyncConnection::deleteDirectory(const QString &directory, bool recursive, bool failIfContentExists) |
|
150 { |
|
151 QString cmd = ceTcpSyncProgram + " deleteDirectory \"" + directory + "\" " + boolToString(recursive) + " " + boolToString(failIfContentExists); |
|
152 return system(qPrintable(cmd)) == 0; |
|
153 } |
|
154 |
|
155 bool CeTcpSyncConnection::execute(QString program, QString arguments, int timeout, int *returnValue) |
|
156 { |
|
157 QString cmd = ceTcpSyncProgram + " execute \"" + program + "\" \"" + arguments + "\" " + QString::number(timeout); |
|
158 int exitCode = system(qPrintable(cmd)); |
|
159 if (returnValue) |
|
160 *returnValue = exitCode; |
|
161 return true; |
|
162 } |
|
163 |
|
164 bool CeTcpSyncConnection::createDirectory(const QString &path, bool deleteBefore) |
|
165 { |
|
166 QString cmd = ceTcpSyncProgram + " createDirectory \"" + path + "\" " + boolToString(deleteBefore); |
|
167 return system(qPrintable(cmd)) == 0; |
|
168 } |
|
169 |
|
170 bool CeTcpSyncConnection::timeStampForLocalFileTime(FILETIME* fTime) const |
|
171 { |
|
172 QString cmd = ceTcpSyncProgram + " timeStampForLocalFileTime " + fileTimeToString(*fTime) + " >qt_cetcpsyncdata.txt"; |
|
173 if (system(qPrintable(cmd)) != 0) |
|
174 return false; |
|
175 |
|
176 QFile file("qt_cetcpsyncdata.txt"); |
|
177 if (!file.open(QIODevice::ReadOnly)) |
|
178 return false; |
|
179 |
|
180 bool result = fileTimeFromString(*fTime, file.readLine()); |
|
181 file.close(); |
|
182 file.remove(); |
|
183 return result; |
|
184 } |
|
185 |
|
186 bool CeTcpSyncConnection::fileCreationTime(const QString &fileName, FILETIME* deviceCreationTime) const |
|
187 { |
|
188 QString cmd = ceTcpSyncProgram + " fileCreationTime \"" + fileName + "\" >qt_cetcpsyncdata.txt"; |
|
189 if (system(qPrintable(cmd)) != 0) |
|
190 return false; |
|
191 |
|
192 QFile file("qt_cetcpsyncdata.txt"); |
|
193 if (!file.open(QIODevice::ReadOnly)) |
|
194 return false; |
|
195 |
|
196 bool result = fileTimeFromString(*deviceCreationTime, file.readLine()); |
|
197 file.close(); |
|
198 file.remove(); |
|
199 return result; |
|
200 } |