summaryrefslogtreecommitdiff
path: root/core/apps/qcop/qcopimpl.cpp
blob: c018aea9239faaa05240642497ce0eb58a128fb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/**********************************************************************
** Copyright (C) 2000-2005 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
** 
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2 of the License, or (at your
** option) any later version.
** 
** A copy of the GNU GPL license version 2 is included in this package as 
** LICENSE.GPL.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
** See the GNU General Public License for more details.
**
** In addition, as a special exception Trolltech gives permission to link
** the code of this program with Qtopia applications copyrighted, developed
** and distributed by Trolltech under the terms of the Qtopia Personal Use
** License Agreement. You must comply with the GNU General Public License
** in all respects for all of the code used other than the applications
** licensed under the Qtopia Personal Use License Agreement. If you modify
** this file, you may extend this exception to your version of the file,
** but you are not obligated to do so. If you do not wish to do so, delete
** this exception statement from your version.
** 
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#include "qcopimpl.h"
#include <qtopia/timeconversion.h>

void doqcopusage()
{
    fprintf( stderr, "Usage: qcop [-l username] channel command [parameters] [-w channel command [timeout]]\n" );
}

void doqcopsyntax( const QString &where, const QString &what )
{
    fprintf( stderr, "Syntax error in %s: %s\n", where.latin1(), what.latin1() );
    exit(1);
}

void disableqdebug( QtMsgType type, const char *msg )
{
    // Ignore messages that are sent via qDebug.
   Q_UNUSED( type );
   Q_UNUSED( msg );
}

int doqcopimpl (int argc, char *argv[])
{
    qInstallMsgHandler( disableqdebug );

    if ( argc > 1 ) {
	QString opt = argv[1];
	if ( opt == "-l" ) {
	    if ( argc < 5 ) {
		doqcopusage();
		exit(1);
	    }
#ifndef Q_OS_WIN32
	    const char *username = argv[2];
	    struct passwd *pwd = getpwnam( username );
	    if ( !pwd ) {
		fprintf( stderr, "Unknown user %s\n", username );
		exit(1);
	    }
	    int uid =  pwd->pw_uid;
	    int gid =  pwd->pw_gid;
	    if ( initgroups( username, gid ) != 0 ) {
		fprintf( stderr, "Could not chg group for user:%s\n", username );
		exit(1);
	    }

	    if ( setuid( uid ) != 0 ) {
		fprintf( stderr, "Could not run as user %s\n", username );
		exit(1);
	    }
	    setenv( "LOGNAME", username, 1 );
#else
	    setenv("LOGNAME", argv[2], 1);
#endif
	    
	    argc -= 2;
	    for ( int i = 1; i < argc; i++ ) {
		argv[i] = argv[i+2];
	    }
	}
		      
    }

    if ( argc < 3 ) {
	doqcopusage();
	exit(1);
    }

    QApplication app( argc, argv );
    
    QString channel = argv[1];
    QString command = argv[2];
    command.stripWhiteSpace();

    int paren = command.find( "(" );
    if ( paren <= 0 )
	doqcopsyntax( "command", command );

    QString params = command.mid( paren + 1 );
    if ( params[(int)params.length()-1] != ')' )
	doqcopsyntax( "command", command );

    params.truncate( params.length()-1 );
#ifndef QT_NO_COP
    int argIdx = 3;
    {
	QCopEnvelope env(channel.latin1(), command.latin1());

	QStringList paramList = QStringList::split( ",", params );
	QStringList::Iterator it;
	for ( it = paramList.begin(); it != paramList.end(); ++it ) {
	    QString arg = argv[argIdx];
	    if ( *it == "QString" ) {
		env << arg;
	    } else if ( *it == "int" ) {
		env << arg.toInt();
	    } else if ( *it == "QDateTime") {
		env << TimeConversion::fromISO8601(QCString(arg));
	    } else if ( *it == "bool") {
		if (arg.lower() == "false")
		    env << (int)false;
		else if (arg.lower() == "true")
		    env << (int)true;
		else
		    doqcopsyntax( "parameter value for bool should be either 'true' or 'false'", arg );
	    } else {
		doqcopsyntax( "parameter type", *it );
	    }
	    argIdx++;
	}
	// send env
    }

    // Check for a "-w" option, which indicates that we should
    // wait for a QCop command before exiting.
    if ( argIdx < argc && QString(argv[argIdx]) == "-w" ) {
	if ( ( argIdx + 3 ) > argc ) {
	    doqcopusage();
	    exit(1);
	}

	channel = argv[argIdx + 1];
	command = argv[argIdx + 2];

	QCopWatcher *watcher = new QCopWatcher( &app, command );
	QCopChannel *chan = new QCopChannel( QCString(channel), &app );

        QObject::connect
	    ( chan, SIGNAL(received(const QCString&,const QByteArray&)),
	      watcher, SLOT(received(const QCString&,const QByteArray&)) );
        QObject::connect
	    ( watcher, SIGNAL(done()), &app, SLOT(quit()) );

	if ( ( argIdx + 3 ) < argc ) {
	    QTimer::singleShot( QString(argv[argIdx + 3]).toInt(),
				watcher, SLOT(timeout()) );
	}

	return app.exec();
    }

#endif

    QTimer::singleShot( 0, &app, SLOT(quit()) );
    return app.exec();
}

QCopWatcher::QCopWatcher( QObject *parent, const QString& msg )
    : QObject( parent )
{
    this->msg = msg;
}

QCopWatcher::~QCopWatcher()
{
}

void QCopWatcher::received( const QCString& msg, const QByteArray& data )
{
    if ( msg != this->msg )
	return;

    QString command = msg;
    QDataStream stream( data, IO_ReadOnly );

    int paren = command.find( "(" );
    if ( paren <= 0 )
	doqcopsyntax( "wait-command", command );

    QString params = command.mid( paren + 1 );
    if ( params[(int)params.length()-1] != ')' )
	doqcopsyntax( "wait-command", command );

    params.truncate( params.length()-1 );
    QStringList paramList = QStringList::split( ",", params );
    QStringList::Iterator it;
    for ( it = paramList.begin(); it != paramList.end(); ++it ) {
	if ( *it == "QString" ) {
	    QString value;
	    stream >> value;
	    puts( value.latin1() );
	} else if ( *it == "int" ) {
	    int value;
	    stream >> value;
	    printf( "%d\n", value );
	} else if ( *it == "QDateTime") {
	    QDateTime value;
	    QString converted;
	    stream >> value;
	    converted = TimeConversion::toISO8601(value);
	    puts( converted.latin1() );
	} else if ( *it == "bool") {
	    int value;
	    stream >> value;
	    if ( value )
		puts( "true" );
	    else
		puts( "false" );
	} else {
	    doqcopsyntax( "parameter type", *it );
	}
    }

    emit done();
}

void QCopWatcher::timeout()
{
    QApplication::exit(1);
}