| # dcop Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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 <qbuffer.h>
    00025 #include <qvariant.h>
    00026 #include <qcolor.h>
    00027 #include <qimage.h>
    00028 #include "../kdatastream.h"
    00029 #include "../dcopclient.h"
    00030 #include "../dcopref.h"
    00031 #include <stdlib.h>
    00032 #include <stdio.h>
    00033 #include <ctype.h>
    00034
    00035 #include "marshall.cpp"
    00036
    00037 static DCOPClient* dcop = 0;
    00038 static bool bAppIdOnly = 0;
    00039 static bool bLaunchApp = 0;
    00040
    00041 bool findObject( const char* app, const char* obj, const char* func, QCStringList args )
    00042 {
    00043     QString f = func; // Qt is better with unicode strings, so use one.
    00044     int left = f.find( '(' );
    00045     int right = f.find( ')' );
    00046
    00047     if ( right <  left )
    00048     {
    00049     qWarning( "parentheses do not match" );
    00050         exit(1);
    00051     }
    00052
    00053     if ( !f.isEmpty() && (left < 0) )
    00054     f += "()";
    00055
    00056     // This may seem expensive but is done only once per invocation
    00057     // of dcop, so it should be OK.
    00058     //
    00059     //
    00060     QStringList intTypes;
    00061     intTypes << "int" << "unsigned" << "long" << "bool" ;
    00062
    00063     QStringList types;
    00064     if ( left >0 && left + 1 < right - 1) {
    00065     types = QStringList::split( ',', f.mid( left + 1, right - left - 1) );
    00066     for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
    00067         QString lt = (*it).simplifyWhiteSpace();
    00068
    00069         int s = lt.find(' ');
    00070
    00071         // If there are spaces in the name, there may be two
    00072         // reasons: the parameter name is still there, ie.
    00073         // "QString URL" or it's a complicated int type, ie.
    00074         // "unsigned long long int bool".
    00075         //
    00076         //
    00077         if ( s > 0 )
    00078         {
    00079         QStringList partl = QStringList::split(' ' , lt);
    00080
    00081         // The zero'th part is -- at the very least -- a
    00082         // type part. Any trailing parts *might* be extra
    00083         // int-type keywords, or at most one may be the
    00084         // parameter name.
    00085         //
    00086         //
    00087             s=1;
    00088
    00089         while (s < (int)partl.count() && intTypes.contains(partl[s]))
    00090         {
    00091             s++;
    00092         }
    00093
    00094         if (s<(int)partl.count()-1)
    00095         {
    00096             qWarning("The argument `%s' seems syntactically wrong.",
    00097                 lt.latin1());
    00098         }
    00099         if (s==(int)partl.count()-1)
    00100         {
    00101             partl.remove(partl.at(s));
    00102         }
    00103
    00104             lt = partl.join(" ");
    00105         lt = lt.simplifyWhiteSpace();
    00106         }
    00107
    00108         (*it) = lt;
    00109     }
    00110     QString fc = f.left( left );
    00111     fc += '(';
    00112     bool first = true;
    00113     for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
    00114         if ( !first )
    00115         fc +=",";
    00116         first = false;
    00117         fc += *it;
    00118     }
    00119     fc += ')';
    00120     f = fc;
    00121     }
    00122
    00123     if ( types.count() != args.count() ) {
    00124     qWarning( "arguments do not match" );
    00125     exit(1);
    00126     }
    00127
    00128     QByteArray data;
    00129     QDataStream arg(data, IO_WriteOnly);
    00130
    00131     uint i = 0;
    00132     for ( QStringList::Iterator it = types.begin(); it != types.end(); ++it ) {
    00133         marshall(arg, args, i, *it);
    00134     }
    00135     if ( (uint) i != args.count() ) {
    00136     qWarning( "arguments do not match" );
    00137     exit(1);
    00138     }
    00139
    00140     QCString foundApp;
    00141     QCString foundObj;
    00142     if ( dcop->findObject( app, obj, f.latin1(),  data, foundApp, foundObj) )
    00143     {
    00144        if (bAppIdOnly)
    00145           puts(foundApp.data());
    00146        else
    00147           printf("DCOPRef(%s,%s)\n", qStringToC(foundApp), qStringToC(foundObj));
    00148        return true;
    00149     }
    00150     return false;
    00151 }
    00152
    00153 bool launchApp(QString app)
    00154 {
    00155     int l = app.length();
    00156     if (l && (app[l-1] == '*'))
    00157        l--;
    00158     if (l && (app[l-1] == '-'))
    00159        l--;
    00160     if (!l) return false;
    00161     app.truncate(l);
    00162
    00163     QStringList URLs;
    00164     QByteArray data, replyData;
    00165     QCString replyType;
    00166     QDataStream arg(data, IO_WriteOnly);
    00167     arg << app << URLs;
    00168
    00169     if ( !dcop->call( "klauncher", "klauncher", "start_service_by_desktop_name(QString,QStringList)",
    00170                       data, replyType, replyData) ) {
    00171     qWarning( "call to klauncher failed.");
    00172         return false;
    00173     }
    00174     QDataStream reply(replyData, IO_ReadOnly);
    00175
    00176     if ( replyType != "serviceResult" )
    00177     {
    00178         qWarning( "unexpected result '%s' from klauncher.", replyType.data());
    00179         return false;
    00180     }
    00181     int result;
    00182     QCString dcopName;
    00183     QString error;
    00184     reply >> result >> dcopName >> error;
    00185     if (result != 0)
    00186     {
    00187         qWarning("Error starting '%s': %s", app.local8Bit().data(), error.local8Bit().data());
    00188         return false;
    00189     }
    00190     return true;
    00191 }
    00192
    00193 void usage()
    00194 {
    00195    fprintf( stderr, "Usage: dcopfind [-l] [-a] application [object [function [arg1] [arg2] [arg3] ... ] ] ] \n" );
    00196    exit(0);
    00197 }
    00198
    00199
    00200 int main( int argc, char** argv )
    00201 {
    00202     int argi = 1;
    00203
    00204     while ((argi < argc) && (argv[argi][0] == '-'))
    00205     {
    00206        switch ( argv[argi][1] ) {
    00207        case 'l':
    00208             bLaunchApp = true;
    00209             break;
    00210        case 'a':
    00211             bAppIdOnly = true;
    00212             break;
    00213        default:
    00214             usage();
    00215        }
    00216        argi++;
    00217     }
    00218
    00219     if (argc <= argi)
    00220        usage();
    00221
    00222     DCOPClient client;
    00223     client.attach();
    00224     dcop = &client;
    00225
    00226     QCString app;
    00227     QCString objid;
    00228     QCString function;
    00229     char **args = 0;
    00230     if ((argc > argi) && (strncmp(argv[argi], "DCOPRef(", 8)) == 0)
    00231     {
    00232        char *delim = strchr(argv[argi], ',');
    00233        if (!delim)
    00234        {
    00235           fprintf(stderr, "Error: '%s' is not a valid DCOP reference.\n", argv[argi]);
    00236           return 1;
    00237        }
    00238        *delim = 0;
    00239        app = argv[argi++] + 8;
    00240        delim++;
    00241        delim[strlen(delim)-1] = 0;
    00242        objid = delim;
    00243     }
    00244     else
    00245     {
    00246        if (argc > argi)
    00247           app = argv[argi++];
    00248        if (argc > argi)
    00249           objid = argv[argi++];
    00250     }
    00251     if (argc > argi)
    00252        function = argv[argi++];
    00253
    00254     if (argc > argi)
    00255     {
    00256        args = &argv[argi];
    00257        argc = argc-argi;
    00258     }
    00259     else
    00260     {
    00261        argc = 0;
    00262     }
    00263
    00264     QCStringList params;
    00265     for( int i = 0; i < argc; i++ )
    00266         params.append( args[ i ] );
    00267     bool ok = findObject( app, objid, function, params );
    00268     if (ok)
    00269        return 0;
    00270     if (bLaunchApp)
    00271     {
    00272        ok = launchApp(app);
    00273        if (!ok)
    00274           return 2;
    00275        ok = findObject( app, objid, function, params );
    00276     }
    00277
    00278     return 1;
    00279 }

This file is part of the documentation for dcop Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:16:55 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003