-rw-r--r-- | core/apps/qcop/qcopimpl.cpp | 246 | ||||
-rw-r--r-- | core/apps/qcop/qcopimpl.h | 82 |
2 files changed, 328 insertions, 0 deletions
diff --git a/core/apps/qcop/qcopimpl.cpp b/core/apps/qcop/qcopimpl.cpp new file mode 100644 index 0000000..c018aea --- a/dev/null +++ b/core/apps/qcop/qcopimpl.cpp | |||
@@ -0,0 +1,246 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000-2005 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of the Qtopia Environment. | ||
5 | ** | ||
6 | ** This program is free software; you can redistribute it and/or modify it | ||
7 | ** under the terms of the GNU General Public License as published by the | ||
8 | ** Free Software Foundation; either version 2 of the License, or (at your | ||
9 | ** option) any later version. | ||
10 | ** | ||
11 | ** A copy of the GNU GPL license version 2 is included in this package as | ||
12 | ** LICENSE.GPL. | ||
13 | ** | ||
14 | ** This program is distributed in the hope that it will be useful, but | ||
15 | ** WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | ** See the GNU General Public License for more details. | ||
18 | ** | ||
19 | ** In addition, as a special exception Trolltech gives permission to link | ||
20 | ** the code of this program with Qtopia applications copyrighted, developed | ||
21 | ** and distributed by Trolltech under the terms of the Qtopia Personal Use | ||
22 | ** License Agreement. You must comply with the GNU General Public License | ||
23 | ** in all respects for all of the code used other than the applications | ||
24 | ** licensed under the Qtopia Personal Use License Agreement. If you modify | ||
25 | ** this file, you may extend this exception to your version of the file, | ||
26 | ** but you are not obligated to do so. If you do not wish to do so, delete | ||
27 | ** this exception statement from your version. | ||
28 | ** | ||
29 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
30 | ** | ||
31 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
32 | ** not clear to you. | ||
33 | ** | ||
34 | **********************************************************************/ | ||
35 | |||
36 | #include "qcopimpl.h" | ||
37 | #include <qtopia/timeconversion.h> | ||
38 | |||
39 | void doqcopusage() | ||
40 | { | ||
41 | fprintf( stderr, "Usage: qcop [-l username] channel command [parameters] [-w channel command [timeout]]\n" ); | ||
42 | } | ||
43 | |||
44 | void doqcopsyntax( const QString &where, const QString &what ) | ||
45 | { | ||
46 | fprintf( stderr, "Syntax error in %s: %s\n", where.latin1(), what.latin1() ); | ||
47 | exit(1); | ||
48 | } | ||
49 | |||
50 | void disableqdebug( QtMsgType type, const char *msg ) | ||
51 | { | ||
52 | // Ignore messages that are sent via qDebug. | ||
53 | Q_UNUSED( type ); | ||
54 | Q_UNUSED( msg ); | ||
55 | } | ||
56 | |||
57 | int doqcopimpl (int argc, char *argv[]) | ||
58 | { | ||
59 | qInstallMsgHandler( disableqdebug ); | ||
60 | |||
61 | if ( argc > 1 ) { | ||
62 | QString opt = argv[1]; | ||
63 | if ( opt == "-l" ) { | ||
64 | if ( argc < 5 ) { | ||
65 | doqcopusage(); | ||
66 | exit(1); | ||
67 | } | ||
68 | #ifndef Q_OS_WIN32 | ||
69 | const char *username = argv[2]; | ||
70 | struct passwd *pwd = getpwnam( username ); | ||
71 | if ( !pwd ) { | ||
72 | fprintf( stderr, "Unknown user %s\n", username ); | ||
73 | exit(1); | ||
74 | } | ||
75 | int uid = pwd->pw_uid; | ||
76 | int gid = pwd->pw_gid; | ||
77 | if ( initgroups( username, gid ) != 0 ) { | ||
78 | fprintf( stderr, "Could not chg group for user:%s\n", username ); | ||
79 | exit(1); | ||
80 | } | ||
81 | |||
82 | if ( setuid( uid ) != 0 ) { | ||
83 | fprintf( stderr, "Could not run as user %s\n", username ); | ||
84 | exit(1); | ||
85 | } | ||
86 | setenv( "LOGNAME", username, 1 ); | ||
87 | #else | ||
88 | setenv("LOGNAME", argv[2], 1); | ||
89 | #endif | ||
90 | |||
91 | argc -= 2; | ||
92 | for ( int i = 1; i < argc; i++ ) { | ||
93 | argv[i] = argv[i+2]; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | } | ||
98 | |||
99 | if ( argc < 3 ) { | ||
100 | doqcopusage(); | ||
101 | exit(1); | ||
102 | } | ||
103 | |||
104 | QApplication app( argc, argv ); | ||
105 | |||
106 | QString channel = argv[1]; | ||
107 | QString command = argv[2]; | ||
108 | command.stripWhiteSpace(); | ||
109 | |||
110 | int paren = command.find( "(" ); | ||
111 | if ( paren <= 0 ) | ||
112 | doqcopsyntax( "command", command ); | ||
113 | |||
114 | QString params = command.mid( paren + 1 ); | ||
115 | if ( params[(int)params.length()-1] != ')' ) | ||
116 | doqcopsyntax( "command", command ); | ||
117 | |||
118 | params.truncate( params.length()-1 ); | ||
119 | #ifndef QT_NO_COP | ||
120 | int argIdx = 3; | ||
121 | { | ||
122 | QCopEnvelope env(channel.latin1(), command.latin1()); | ||
123 | |||
124 | QStringList paramList = QStringList::split( ",", params ); | ||
125 | QStringList::Iterator it; | ||
126 | for ( it = paramList.begin(); it != paramList.end(); ++it ) { | ||
127 | QString arg = argv[argIdx]; | ||
128 | if ( *it == "QString" ) { | ||
129 | env << arg; | ||
130 | } else if ( *it == "int" ) { | ||
131 | env << arg.toInt(); | ||
132 | } else if ( *it == "QDateTime") { | ||
133 | env << TimeConversion::fromISO8601(QCString(arg)); | ||
134 | } else if ( *it == "bool") { | ||
135 | if (arg.lower() == "false") | ||
136 | env << (int)false; | ||
137 | else if (arg.lower() == "true") | ||
138 | env << (int)true; | ||
139 | else | ||
140 | doqcopsyntax( "parameter value for bool should be either 'true' or 'false'", arg ); | ||
141 | } else { | ||
142 | doqcopsyntax( "parameter type", *it ); | ||
143 | } | ||
144 | argIdx++; | ||
145 | } | ||
146 | // send env | ||
147 | } | ||
148 | |||
149 | // Check for a "-w" option, which indicates that we should | ||
150 | // wait for a QCop command before exiting. | ||
151 | if ( argIdx < argc && QString(argv[argIdx]) == "-w" ) { | ||
152 | if ( ( argIdx + 3 ) > argc ) { | ||
153 | doqcopusage(); | ||
154 | exit(1); | ||
155 | } | ||
156 | |||
157 | channel = argv[argIdx + 1]; | ||
158 | command = argv[argIdx + 2]; | ||
159 | |||
160 | QCopWatcher *watcher = new QCopWatcher( &app, command ); | ||
161 | QCopChannel *chan = new QCopChannel( QCString(channel), &app ); | ||
162 | |||
163 | QObject::connect | ||
164 | ( chan, SIGNAL(received(const QCString&,const QByteArray&)), | ||
165 | watcher, SLOT(received(const QCString&,const QByteArray&)) ); | ||
166 | QObject::connect | ||
167 | ( watcher, SIGNAL(done()), &app, SLOT(quit()) ); | ||
168 | |||
169 | if ( ( argIdx + 3 ) < argc ) { | ||
170 | QTimer::singleShot( QString(argv[argIdx + 3]).toInt(), | ||
171 | watcher, SLOT(timeout()) ); | ||
172 | } | ||
173 | |||
174 | return app.exec(); | ||
175 | } | ||
176 | |||
177 | #endif | ||
178 | |||
179 | QTimer::singleShot( 0, &app, SLOT(quit()) ); | ||
180 | return app.exec(); | ||
181 | } | ||
182 | |||
183 | QCopWatcher::QCopWatcher( QObject *parent, const QString& msg ) | ||
184 | : QObject( parent ) | ||
185 | { | ||
186 | this->msg = msg; | ||
187 | } | ||
188 | |||
189 | QCopWatcher::~QCopWatcher() | ||
190 | { | ||
191 | } | ||
192 | |||
193 | void QCopWatcher::received( const QCString& msg, const QByteArray& data ) | ||
194 | { | ||
195 | if ( msg != this->msg ) | ||
196 | return; | ||
197 | |||
198 | QString command = msg; | ||
199 | QDataStream stream( data, IO_ReadOnly ); | ||
200 | |||
201 | int paren = command.find( "(" ); | ||
202 | if ( paren <= 0 ) | ||
203 | doqcopsyntax( "wait-command", command ); | ||
204 | |||
205 | QString params = command.mid( paren + 1 ); | ||
206 | if ( params[(int)params.length()-1] != ')' ) | ||
207 | doqcopsyntax( "wait-command", command ); | ||
208 | |||
209 | params.truncate( params.length()-1 ); | ||
210 | QStringList paramList = QStringList::split( ",", params ); | ||
211 | QStringList::Iterator it; | ||
212 | for ( it = paramList.begin(); it != paramList.end(); ++it ) { | ||
213 | if ( *it == "QString" ) { | ||
214 | QString value; | ||
215 | stream >> value; | ||
216 | puts( value.latin1() ); | ||
217 | } else if ( *it == "int" ) { | ||
218 | int value; | ||
219 | stream >> value; | ||
220 | printf( "%d\n", value ); | ||
221 | } else if ( *it == "QDateTime") { | ||
222 | QDateTime value; | ||
223 | QString converted; | ||
224 | stream >> value; | ||
225 | converted = TimeConversion::toISO8601(value); | ||
226 | puts( converted.latin1() ); | ||
227 | } else if ( *it == "bool") { | ||
228 | int value; | ||
229 | stream >> value; | ||
230 | if ( value ) | ||
231 | puts( "true" ); | ||
232 | else | ||
233 | puts( "false" ); | ||
234 | } else { | ||
235 | doqcopsyntax( "parameter type", *it ); | ||
236 | } | ||
237 | } | ||
238 | |||
239 | emit done(); | ||
240 | } | ||
241 | |||
242 | void QCopWatcher::timeout() | ||
243 | { | ||
244 | QApplication::exit(1); | ||
245 | } | ||
246 | |||
diff --git a/core/apps/qcop/qcopimpl.h b/core/apps/qcop/qcopimpl.h new file mode 100644 index 0000000..dfda063 --- a/dev/null +++ b/core/apps/qcop/qcopimpl.h | |||
@@ -0,0 +1,82 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2000-2005 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of the Qtopia Environment. | ||
5 | ** | ||
6 | ** This program is free software; you can redistribute it and/or modify it | ||
7 | ** under the terms of the GNU General Public License as published by the | ||
8 | ** Free Software Foundation; either version 2 of the License, or (at your | ||
9 | ** option) any later version. | ||
10 | ** | ||
11 | ** A copy of the GNU GPL license version 2 is included in this package as | ||
12 | ** LICENSE.GPL. | ||
13 | ** | ||
14 | ** This program is distributed in the hope that it will be useful, but | ||
15 | ** WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | ** See the GNU General Public License for more details. | ||
18 | ** | ||
19 | ** In addition, as a special exception Trolltech gives permission to link | ||
20 | ** the code of this program with Qtopia applications copyrighted, developed | ||
21 | ** and distributed by Trolltech under the terms of the Qtopia Personal Use | ||
22 | ** License Agreement. You must comply with the GNU General Public License | ||
23 | ** in all respects for all of the code used other than the applications | ||
24 | ** licensed under the Qtopia Personal Use License Agreement. If you modify | ||
25 | ** this file, you may extend this exception to your version of the file, | ||
26 | ** but you are not obligated to do so. If you do not wish to do so, delete | ||
27 | ** this exception statement from your version. | ||
28 | ** | ||
29 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
30 | ** | ||
31 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
32 | ** not clear to you. | ||
33 | ** | ||
34 | **********************************************************************/ | ||
35 | |||
36 | #ifndef QCOPIMPL_H | ||
37 | #define QCOPIMPL_H | ||
38 | |||
39 | #ifdef QWS | ||
40 | #include <qtopia/qcopenvelope_qws.h> | ||
41 | #endif | ||
42 | |||
43 | #include <qtopia/qpeapplication.h> | ||
44 | #include <qstringlist.h> | ||
45 | #include <qdatastream.h> | ||
46 | #include <qtimer.h> | ||
47 | |||
48 | #include <stdlib.h> | ||
49 | #include <stdio.h> | ||
50 | #include <sys/types.h> | ||
51 | |||
52 | #ifndef Q_OS_WIN32 | ||
53 | #include <pwd.h> | ||
54 | #include <unistd.h> | ||
55 | #include <grp.h> | ||
56 | #endif | ||
57 | |||
58 | // No tr() anywhere in this file | ||
59 | |||
60 | void doqcopusage(); | ||
61 | void doqcopsyntax( const QString &where, const QString &what ); | ||
62 | int doqcopimpl (int argc, char *argv[]); | ||
63 | |||
64 | class QCopWatcher : public QObject | ||
65 | { | ||
66 | Q_OBJECT | ||
67 | public: | ||
68 | QCopWatcher( QObject *parent, const QString& msg ); | ||
69 | ~QCopWatcher(); | ||
70 | |||
71 | signals: | ||
72 | void done(); | ||
73 | |||
74 | public slots: | ||
75 | void received( const QCString& msg, const QByteArray& data ); | ||
76 | void timeout(); | ||
77 | |||
78 | private: | ||
79 | QCString msg; | ||
80 | }; | ||
81 | |||
82 | #endif | ||