dcop Library API Documentation

dcopfind.cpp

00001 /***************************************************************** 00002 Copyright (c) 2000 Matthias Ettrich <ettrich@kde.org> 00003 Copyright (c) 2001 Waldo Bastian <bastian@kde.org> 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 00019 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 00020 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00021 00022 ******************************************************************/ 00023 00024 #include <qvariant.h> 00025 #include <qcolor.h> 00026 #include "../kdatastream.h" 00027 #include "../dcopclient.h" 00028 #include "../dcopref.h" 00029 #include <stdlib.h> 00030 #include <stdio.h> 00031 #include <ctype.h> 00032 00033 #include "marshall.cpp" 00034 00035 static DCOPClient* dcop = 0; 00036 static bool bAppIdOnly = 0; 00037 static bool bLaunchApp = 0; 00038 00039 bool findObject( const char* app, const char* obj, const char* func, QCStringList args ) 00040 { 00041 QString f = func; // Qt is better with unicode strings, so use one. 00042 int left = f.find( '(' ); 00043 int right = f.find( ')' ); 00044 00045 if ( right < left ) 00046 { 00047 qWarning( "parentheses do not match" ); 00048 exit(1); 00049 } 00050 00051 if ( !f.isEmpty() && (left < 0) ) 00052 f += "()"; 00053 00054 // This may seem expensive but is done only once per invocation 00055 // of dcop, so it should be OK. 00056 // 00057 // 00058 QStringList intTypes; 00059 intTypes << "int" << "unsigned" << "long" << "bool" ; 00060 00061 QStringList types; 00062 if ( left >0 && left + 1 < right - 1) { 00063 types = QStringList::split( ',', f.mid( left + 1, right - left - 1) ); 00064 for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) { 00065 QString lt = (*it).simplifyWhiteSpace(); 00066 00067 int s = lt.find(' '); 00068 00069 // If there are spaces in the name, there may be two 00070 // reasons: the parameter name is still there, ie. 00071 // "QString URL" or it's a complicated int type, ie. 00072 // "unsigned long long int bool". 00073 // 00074 // 00075 if ( s > 0 ) 00076 { 00077 QStringList partl = QStringList::split(' ' , lt); 00078 00079 // The zero'th part is -- at the very least -- a 00080 // type part. Any trailing parts *might* be extra 00081 // int-type keywords, or at most one may be the 00082 // parameter name. 00083 // 00084 // 00085 s=1; 00086 00087 while (s < (int)partl.count() && intTypes.contains(partl[s])) 00088 { 00089 s++; 00090 } 00091 00092 if (s<(int)partl.count()-1) 00093 { 00094 qWarning("The argument `%s' seems syntactically wrong.", 00095 lt.latin1()); 00096 } 00097 if (s==(int)partl.count()-1) 00098 { 00099 partl.remove(partl.at(s)); 00100 } 00101 00102 lt = partl.join(" "); 00103 lt = lt.simplifyWhiteSpace(); 00104 } 00105 00106 (*it) = lt; 00107 } 00108 QString fc = f.left( left ); 00109 fc += '('; 00110 bool first = true; 00111 for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) { 00112 if ( !first ) 00113 fc +=","; 00114 first = false; 00115 fc += *it; 00116 } 00117 fc += ')'; 00118 f = fc; 00119 } 00120 00121 if ( types.count() != args.count() ) { 00122 qWarning( "arguments do not match" ); 00123 exit(1); 00124 } 00125 00126 QByteArray data; 00127 QDataStream arg(data, IO_WriteOnly); 00128 00129 uint i = 0; 00130 for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) { 00131 marshall(arg, args, i, *it); 00132 } 00133 if ( (uint) i != args.count() ) { 00134 qWarning( "arguments do not match" ); 00135 exit(1); 00136 } 00137 00138 QCString foundApp; 00139 QCString foundObj; 00140 if ( dcop->findObject( app, obj, f.latin1(), data, foundApp, foundObj) ) 00141 { 00142 if (bAppIdOnly) 00143 puts(foundApp.data()); 00144 else 00145 printf("DCOPRef(%s,%s)\n", qStringToC(foundApp), qStringToC(foundObj)); 00146 return true; 00147 } 00148 return false; 00149 } 00150 00151 bool launchApp(QString app) 00152 { 00153 int l = app.length(); 00154 if (l && (app[l-1] == '*')) 00155 l--; 00156 if (l && (app[l-1] == '-')) 00157 l--; 00158 if (!l) return false; 00159 app.truncate(l); 00160 00161 QStringList URLs; 00162 QByteArray data, replyData; 00163 QCString replyType; 00164 QDataStream arg(data, IO_WriteOnly); 00165 arg << app << URLs; 00166 00167 if ( !dcop->call( "klauncher", "klauncher", "start_service_by_desktop_name(QString,QStringList)", 00168 data, replyType, replyData) ) { 00169 qWarning( "call to klauncher failed."); 00170 return false; 00171 } 00172 QDataStream reply(replyData, IO_ReadOnly); 00173 00174 if ( replyType != "serviceResult" ) 00175 { 00176 qWarning( "unexpected result '%s' from klauncher.", replyType.data()); 00177 return false; 00178 } 00179 int result; 00180 QCString dcopName; 00181 QString error; 00182 reply >> result >> dcopName >> error; 00183 if (result != 0) 00184 { 00185 qWarning("Error starting '%s': %s", app.local8Bit().data(), error.local8Bit().data()); 00186 return false; 00187 } 00188 return true; 00189 } 00190 00191 void usage() 00192 { 00193 fprintf( stderr, "Usage: dcopfind [-l] [-a] application [object [function [arg1] [arg2] [arg3] ... ] ] ] \n" ); 00194 exit(0); 00195 } 00196 00197 00198 int main( int argc, char** argv ) 00199 { 00200 int argi = 1; 00201 00202 while ((argi < argc) && (argv[argi][0] == '-')) 00203 { 00204 switch ( argv[argi][1] ) { 00205 case 'l': 00206 bLaunchApp = true; 00207 break; 00208 case 'a': 00209 bAppIdOnly = true; 00210 break; 00211 default: 00212 usage(); 00213 } 00214 argi++; 00215 } 00216 00217 if (argc <= argi) 00218 usage(); 00219 00220 DCOPClient client; 00221 client.attach(); 00222 dcop = &client; 00223 00224 QCString app; 00225 QCString objid; 00226 QCString function; 00227 char **args = 0; 00228 if ((argc > argi) && (strncmp(argv[argi], "DCOPRef(", 8)) == 0) 00229 { 00230 char *delim = strchr(argv[argi], ','); 00231 if (!delim) 00232 { 00233 fprintf(stderr, "Error: '%s' is not a valid DCOP reference.\n", argv[argi]); 00234 return 1; 00235 } 00236 *delim = 0; 00237 app = argv[argi++] + 8; 00238 delim++; 00239 delim[strlen(delim)-1] = 0; 00240 objid = delim; 00241 } 00242 else 00243 { 00244 if (argc > argi) 00245 app = argv[argi++]; 00246 if (argc > argi) 00247 objid = argv[argi++]; 00248 } 00249 if (argc > argi) 00250 function = argv[argi++]; 00251 00252 if (argc > argi) 00253 { 00254 args = &argv[argi]; 00255 argc = argc-argi; 00256 } 00257 else 00258 { 00259 argc = 0; 00260 } 00261 00262 QCStringList params; 00263 for( int i = 0; i < argc; i++ ) 00264 params.append( args[ i ] ); 00265 bool ok = findObject( app, objid, function, params ); 00266 if (ok) 00267 return 0; 00268 if (bLaunchApp) 00269 { 00270 ok = launchApp(app); 00271 if (!ok) 00272 return 2; 00273 ok = findObject( app, objid, function, params ); 00274 } 00275 00276 return 1; 00277 }
KDE Logo
This file is part of the documentation for dcop Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 16 17:21:26 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003