author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the test suite 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 |
||
43 |
#include <qscriptengine.h> |
|
44 |
#include <QFile> |
|
45 |
#include <QTest> |
|
46 |
||
47 |
#include <qlocalsocket.h> |
|
48 |
#include <qlocalserver.h> |
|
49 |
||
50 |
class QScriptLocalSocket : public QObject |
|
51 |
{ |
|
52 |
Q_OBJECT |
|
53 |
Q_PROPERTY(QString serverName WRITE connectToServer READ serverName) |
|
54 |
||
55 |
public: |
|
56 |
QScriptLocalSocket(QObject *parent = 0) : QObject(parent) |
|
57 |
{ |
|
58 |
lc = new QLocalSocket(this); |
|
59 |
} |
|
60 |
||
61 |
public slots: |
|
62 |
QString serverName() |
|
63 |
{ |
|
64 |
return lc->serverName(); |
|
65 |
} |
|
66 |
||
67 |
void connectToServer(const QString &name) { |
|
68 |
lc->connectToServer(name); |
|
69 |
} |
|
70 |
||
71 |
void sleep(int x) const |
|
72 |
{ |
|
73 |
QTest::qSleep(x); |
|
74 |
} |
|
75 |
||
76 |
bool isConnected() { |
|
77 |
return (lc->state() == QLocalSocket::ConnectedState); |
|
78 |
} |
|
79 |
||
80 |
void open() { |
|
81 |
lc->open(QIODevice::ReadWrite); |
|
82 |
} |
|
83 |
||
84 |
bool waitForConnected() { |
|
85 |
return lc->waitForConnected(100000); |
|
86 |
} |
|
87 |
void waitForReadyRead() { |
|
88 |
lc->waitForReadyRead(); |
|
89 |
} |
|
90 |
||
91 |
void write(const QString &string) { |
|
92 |
QTextStream out(lc); |
|
93 |
out << string << endl; |
|
94 |
} |
|
95 |
||
96 |
bool waitForBytesWritten(int t = 3000) { |
|
97 |
return lc->waitForBytesWritten(t); |
|
98 |
} |
|
99 |
||
100 |
QString readLine() { |
|
101 |
QTextStream in(lc); |
|
102 |
return in.readLine(); |
|
103 |
} |
|
104 |
||
105 |
QString errorString() { |
|
106 |
return lc->errorString(); |
|
107 |
} |
|
108 |
||
109 |
void close() { |
|
110 |
lc->close(); |
|
111 |
} |
|
112 |
||
113 |
public: |
|
114 |
QLocalSocket *lc; |
|
115 |
}; |
|
116 |
||
117 |
class QScriptLocalServer : public QLocalServer |
|
118 |
{ |
|
119 |
Q_OBJECT |
|
120 |
Q_PROPERTY(int maxPendingConnections WRITE setMaxPendingConnections READ maxPendingConnections) |
|
121 |
Q_PROPERTY(QString name WRITE listen READ serverName) |
|
122 |
Q_PROPERTY(bool listening READ isListening) |
|
123 |
||
124 |
public: |
|
125 |
QScriptLocalServer(QObject *parent = 0) : QLocalServer(parent) |
|
126 |
{ |
|
127 |
} |
|
128 |
||
129 |
public slots: |
|
130 |
bool listen(const QString &name) { |
|
131 |
if (!QLocalServer::listen(name)) { |
|
132 |
if (serverError() == QAbstractSocket::AddressInUseError) { |
|
133 |
QFile::remove(serverName()); |
|
134 |
return QLocalServer::listen(name); |
|
135 |
} |
|
136 |
return false; |
|
137 |
} |
|
138 |
return true; |
|
139 |
} |
|
140 |
||
141 |
QScriptLocalSocket *nextConnection() { |
|
142 |
QLocalSocket *other = nextPendingConnection(); |
|
143 |
QScriptLocalSocket *s = new QScriptLocalSocket(this); |
|
144 |
delete s->lc; |
|
145 |
s->lc = other; |
|
146 |
return s; |
|
147 |
} |
|
148 |
||
149 |
bool waitForNewConnection() { |
|
150 |
return QLocalServer::waitForNewConnection(30000); |
|
151 |
} |
|
152 |
||
153 |
QString errorString() { |
|
154 |
return QLocalServer::errorString(); |
|
155 |
} |
|
156 |
||
157 |
||
158 |
}; |
|
159 |
||
160 |
template <typename T> |
|
161 |
static QScriptValue _q_ScriptValueFromQObject(QScriptEngine *engine, T* const &in) |
|
162 |
{ |
|
163 |
return engine->newQObject(in); |
|
164 |
} |
|
165 |
template <typename T> |
|
166 |
static void _q_ScriptValueToQObject(const QScriptValue &v, T* &out) |
|
167 |
{ out = qobject_cast<T*>(v.toQObject()); |
|
168 |
} |
|
169 |
template <typename T> |
|
170 |
static int _q_ScriptRegisterQObjectMetaType(QScriptEngine *engine, const QScriptValue &prototype) |
|
171 |
{ |
|
172 |
return qScriptRegisterMetaType<T*>(engine, _q_ScriptValueFromQObject<T>, _q_ScriptValueToQObject<T>, prototype); |
|
173 |
} |
|
174 |
||
175 |
Q_SCRIPT_DECLARE_QMETAOBJECT(QScriptLocalSocket, QObject*); |
|
176 |
Q_SCRIPT_DECLARE_QMETAOBJECT(QScriptLocalServer, QObject*); |
|
177 |
||
178 |
static void interactive(QScriptEngine &eng) |
|
179 |
{ |
|
180 |
QTextStream qin(stdin, QFile::ReadOnly); |
|
181 |
||
182 |
const char *qscript_prompt = "qs> "; |
|
183 |
const char *dot_prompt = ".... "; |
|
184 |
const char *prompt = qscript_prompt; |
|
185 |
||
186 |
QString code; |
|
187 |
||
188 |
forever { |
|
189 |
QString line; |
|
190 |
||
191 |
printf("%s", prompt); |
|
192 |
fflush(stdout); |
|
193 |
||
194 |
line = qin.readLine(); |
|
195 |
if (line.isNull()) |
|
196 |
break; |
|
197 |
||
198 |
code += line; |
|
199 |
code += QLatin1Char('\n'); |
|
200 |
||
201 |
if (line.trimmed().isEmpty()) { |
|
202 |
continue; |
|
203 |
||
204 |
} else if (! eng.canEvaluate(code)) { |
|
205 |
prompt = dot_prompt; |
|
206 |
||
207 |
} else { |
|
208 |
QScriptValue result = eng.evaluate(code); |
|
209 |
code.clear(); |
|
210 |
prompt = qscript_prompt; |
|
211 |
if (!result.isUndefined()) |
|
212 |
fprintf(stderr, "%s\n", qPrintable(result.toString())); |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
Q_DECLARE_METATYPE(QScriptLocalSocket*) |
|
217 |
Q_DECLARE_METATYPE(QScriptLocalServer*) |
|
218 |
int main(int argc, char *argv[]) |
|
219 |
{ |
|
220 |
QCoreApplication app(argc, argv); |
|
221 |
QScriptEngine eng; |
|
222 |
QScriptValue globalObject = eng.globalObject(); |
|
223 |
||
224 |
_q_ScriptRegisterQObjectMetaType<QScriptLocalServer>(&eng, QScriptValue()); |
|
225 |
||
226 |
QScriptValue lss = qScriptValueFromQMetaObject<QScriptLocalServer>(&eng); |
|
227 |
eng.globalObject().setProperty("QScriptLocalServer", lss); |
|
228 |
||
229 |
_q_ScriptRegisterQObjectMetaType<QScriptLocalSocket>(&eng, QScriptValue()); |
|
230 |
||
231 |
QScriptValue lsc = qScriptValueFromQMetaObject<QScriptLocalSocket>(&eng); |
|
232 |
eng.globalObject().setProperty("QScriptLocalSocket", lsc); |
|
233 |
||
234 |
if (! *++argv) { |
|
235 |
interactive(eng); |
|
236 |
return EXIT_SUCCESS; |
|
237 |
} |
|
238 |
||
239 |
QStringList arguments; |
|
240 |
for (int i = 0; i < argc - 1; ++i) |
|
241 |
arguments << QString::fromLocal8Bit(argv[i]); |
|
242 |
||
243 |
while (!arguments.isEmpty()) { |
|
244 |
QString fn = arguments.takeFirst(); |
|
245 |
||
246 |
if (fn == QLatin1String("-i")) { |
|
247 |
interactive(eng); |
|
248 |
break; |
|
249 |
} |
|
250 |
||
251 |
QString contents; |
|
252 |
||
253 |
if (fn == QLatin1String("-")) { |
|
254 |
QTextStream stream(stdin, QFile::ReadOnly); |
|
255 |
contents = stream.readAll(); |
|
256 |
} else { |
|
257 |
QFile file(fn); |
|
258 |
if (!file.exists()) { |
|
259 |
fprintf(stderr, "%s doesn't exists\n", qPrintable(fn)); |
|
260 |
return EXIT_FAILURE; |
|
261 |
} |
|
262 |
if (file.open(QFile::ReadOnly)) { |
|
263 |
QTextStream stream(&file); |
|
264 |
contents = stream.readAll(); |
|
265 |
file.close(); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
if (contents.isEmpty()) |
|
270 |
continue; |
|
271 |
||
272 |
if (contents[0] == '#') { |
|
273 |
contents.prepend("//"); |
|
274 |
QScriptValue args = eng.newArray(); |
|
275 |
args.setProperty("0", QScriptValue(&eng, fn)); |
|
276 |
int i = 1; |
|
277 |
while (!arguments.isEmpty()) |
|
278 |
args.setProperty(i++, QScriptValue(&eng, arguments.takeFirst())); |
|
279 |
eng.currentContext()->activationObject().setProperty("args", args); |
|
280 |
} |
|
281 |
QScriptValue r = eng.evaluate(contents); |
|
282 |
if (eng.hasUncaughtException()) { |
|
283 |
int line = eng.uncaughtExceptionLineNumber(); |
|
284 |
fprintf(stderr, "%d: %s\n\t%s\n\n", line, qPrintable(fn), qPrintable(r.toString())); |
|
285 |
return EXIT_FAILURE; |
|
286 |
} |
|
287 |
if (r.isNumber()) |
|
288 |
return r.toInt32(); |
|
289 |
} |
|
290 |
||
291 |
return EXIT_SUCCESS; |
|
292 |
} |
|
293 |
||
294 |
#include "main.moc" |