source: trunk/tools/qtestlib/wince/cetcpsyncserver/commands.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 21.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the tools applications of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41#include "commands.h"
42#include <QtCore/QDebug>
43#include <QtCore/QFileInfo>
44#include <QtCore/QDir>
45#include <QtCore/QProcess>
46
47#ifdef Q_OS_WINCE
48#include <windows.h>
49#endif
50
51/////////////////////////////////////////////////////
52// Abstract Command Implementation //
53/////////////////////////////////////////////////////
54AbstractCommand::AbstractCommand()
55: m_socket(0)
56{
57}
58
59AbstractCommand::~AbstractCommand()
60{
61}
62
63void AbstractCommand::reportSuccess()
64{
65 m_socket->write(COMMAND_SUCCESS, strlen(COMMAND_SUCCESS));
66 m_socket->waitForBytesWritten();
67}
68
69void AbstractCommand::reportError()
70{
71 m_socket->write(COMMAND_ERROR, strlen(COMMAND_ERROR));
72 m_socket->waitForBytesWritten();
73}
74
75void AbstractCommand::dataReceived(QByteArray&)
76{
77 debugOutput(1, "AbstractCommand::dataReceived NOT SUPPOSED TO BE HERE");
78}
79
80void AbstractCommand::commandFinished()
81{
82 debugOutput(1, "AbstractCommand::commandFinished()NOT SUPPOSED TO BE HERE");
83}
84
85void AbstractCommand::setSocket(QTcpSocket* socket)
86{
87 debugOutput(0, "AbstractCommand::setSocket()");
88 Q_ASSERT(socket);
89 m_socket = socket;
90 connect(m_socket, SIGNAL(readyRead()), this, SLOT(_readData()));
91 reportSuccess();
92}
93
94QTcpSocket* AbstractCommand::socket()
95{
96 return m_socket;
97}
98
99void AbstractCommand::_readData()
100{
101 QByteArray arr = m_socket->readAll();
102 dataReceived(arr);
103}
104
105void AbstractCommand::_disconnect()
106{
107}
108
109/////////////////////////////////////////////////////
110// Create File Command Implementation //
111/////////////////////////////////////////////////////
112CreateFileCommand::CreateFileCommand()
113: m_dataCount(0)
114{
115 debugOutput(0, "CreateFileCommand::CreateFileCommand");
116 m_options.fileSize= -1;
117}
118
119CreateFileCommand::~CreateFileCommand()
120{
121 debugOutput(0, "CreateFileCommand::~CreateFileCommand");
122 if (m_file.isOpen()) {
123 fprintf(stderr, "****************FILE IS STILL OPENED AND HAVENT FINISHED WRITING**********************\n");
124 fprintf(stderr, "Current: %d Expected: %d\n", m_dataCount , m_options.fileSize);
125 m_file.close();
126 }
127}
128
129void CreateFileCommand::dataReceived(QByteArray &data)
130{
131 bool successful = true;
132 // If we haven't received the options yet
133 if (m_options.fileSize == -1) {
134 CreateFileOptions* opt = (CreateFileOptions*) data.data();
135 memcpy(&m_options , opt , sizeof(CreateFileOptions));
136
137 if (QFileInfo(QString::fromLatin1(m_options.fileName)).exists()) {
138 if (m_options.overwriteExisting) {
139#ifdef Q_OS_WINCE
140 SetFileAttributes(QFileInfo(m_options.fileName).absoluteFilePath().utf16(), FILE_ATTRIBUTE_NORMAL);
141#endif
142 QFile::remove(m_options.fileName);
143 } else
144 successful = false;
145 }
146 m_file.setFileName(QString::fromLatin1(m_options.fileName));
147 if (!m_file.open(QIODevice::WriteOnly))
148 successful = false;
149 else
150 debugOutput(3, QString::fromLatin1("Creating file: %1").arg(m_options.fileName));
151 } else { // write buffer on disc
152 if (!m_file.isOpen())
153 return;
154 m_file.write(data);
155 m_dataCount += data.size();
156 if (m_dataCount >= m_options.fileSize) {
157 // We do not care about more data than announced
158 m_file.close();
159 }
160 }
161
162 if (successful)
163 reportSuccess();
164 else
165 reportError();
166}
167
168void CreateFileCommand::commandFinished()
169{
170 debugOutput(0, "CreateFileCommand::commandFinished");
171#ifdef Q_OS_WIN
172 // We need to set the file attributes for intelligent time comparisons
173 QString tmpFile = QString::fromLatin1(m_options.fileName);
174 HANDLE handle = CreateFile(tmpFile.utf16(), GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
175 if (handle != INVALID_HANDLE_VALUE) {
176 SetFileTime(handle, &(m_options.fileTime), NULL, NULL);
177 CloseHandle(handle);
178 }
179 SetFileAttributes(tmpFile.utf16(), m_options.fileAttributes);
180#endif