dcop Library API Documentation

testdcop.cpp

00001 /*****************************************************************
00002 
00003 Copyright (c) 1999 Preston Brown <pbrown@kde.org>
00004 Copyright (c) 1999 Matthias Ettrich <ettrich@kde.org>
00005  
00006 Permission is hereby granted, free of charge, to any person obtaining a copy
00007 of this software and associated documentation files (the "Software"), to deal
00008 in the Software without restriction, including without limitation the rights
00009 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010 copies of the Software, and to permit persons to whom the Software is
00011 furnished to do so, subject to the following conditions:
00012  
00013 The above copyright notice and this permission notice shall be included in
00014 all copies or substantial portions of the Software.
00015  
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
00019 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00020 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00021 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00022  
00023 ******************************************************************
00024 */
00025 
00026 #include <testdcop.h>
00027 
00028 bool MyDCOPObject::process(const QCString &fun, const QByteArray &data,
00029                QCString& replyType, QByteArray &replyData)
00030 {
00031   qDebug("in MyDCOPObject::process, fun = %s", fun.data());
00032   
00033   // note "fun" is normlized here (i.e. whitespace clean)
00034   if (fun == "aFunction(QString,int)") {
00035     QDataStream args(data, IO_ReadOnly);
00036     QString arg1;
00037     int arg2;
00038     args >> arg1 >> arg2;
00039     function(arg1, arg2);
00040     replyType = "void";
00041     return true;
00042   }
00043   if (fun == "canLaunchRockets(QRect)") {
00044     QDataStream args(data, IO_ReadOnly);
00045     QRect arg1;
00046     args >> arg1;
00047 
00048     printf("Rect x = %d, y = %d, w = %d, h = %d\n", arg1.x(), arg1.y(), arg1.width(), arg1.height());
00049 
00050     replyType = "QRect";
00051     QDataStream reply( replyData, IO_WriteOnly );
00052     QRect r(10,20,100,200);
00053     reply << r;
00054     return true;
00055   }
00056   if (fun == "isAliveSlot(int)") {
00057     
00058     qDebug("isAliveSlot(int)");
00059     bool connectResult = kapp->dcopClient()->disconnectDCOPSignal("", objId(), "", objId(), "" );
00060     qDebug("disconnectDCOPSignal returns %s", connectResult ? "true" : "false");
00061     return true;
00062   }
00063 
00064   return DCOPObject::process(fun, data, replyType, replyData);
00065 }
00066 
00067 QCStringList MyDCOPObject::functions()
00068 {
00069    QCStringList result = DCOPObject::functions();
00070    result << "QRect canLaunchRockets(QRect)";
00071    return result;
00072 }
00073 
00074 int main(int argc, char **argv)
00075 {
00076   KApplication app(argc, argv, "testdcop");
00077 
00078   QCString replyType;
00079   QByteArray data, reply;
00080   DCOPClient *client; client = app.dcopClient();
00081 
00082 //  client->attach(); // attach to the server, now we can use DCOP service
00083 
00084   client->registerAs( app.name(), false ); // register at the server, now others can call us.
00085   qDebug("I registered as '%s'", client->appId().data() );
00086 
00087   if ( client->isApplicationRegistered( app.name() ) )
00088       qDebug("indeed, we are registered!");
00089 
00090   QDataStream dataStream( data, IO_WriteOnly );
00091   dataStream << (int) 43;
00092   client->emitDCOPSignal("alive(int,QCString)", data);
00093 
00094   MyDCOPObject *obj1 = new MyDCOPObject("object1");
00095 
00096   bool connectResult = client->connectDCOPSignal("", "alive(int , QCString)", "object1", "isAliveSlot(int)", false);
00097   qDebug("connectDCOPSignal returns %s", connectResult ? "true" : "false");
00098 
00099   QDataStream ds(data, IO_WriteOnly);
00100   ds << QString("fourty-two") << 42;
00101   if (!client->call(app.name(), "object1", "aFunction(QString,int)", data, replyType, reply))
00102     qDebug("I couldn't call myself");
00103   else
00104       qDebug("return type was '%s'", replyType.data() ); 
00105 
00106   client->send(app.name(), "object1", "aFunction(QString,int)", data );
00107 
00108   int n = client->registeredApplications().count();
00109   qDebug("number of attached applications = %d", n );
00110 
00111   QObject::connect( client, SIGNAL( applicationRegistered( const QCString&)),
00112                     obj1, SLOT( registered( const QCString& )));
00113 
00114   QObject::connect( client, SIGNAL( applicationRemoved( const QCString&)),
00115                     obj1, SLOT( unregistered( const QCString& )));
00116 
00117   // Enable the above signals
00118   client->setNotifications( true );
00119 
00120   QCString foundApp;
00121   QCString foundObj;
00122 
00123   // Find a object called "object1" in any application that
00124   // meets the criteria "canLaunchRockets()"
00125 //  bool boolResult = client->findObject( "", "object1", "canLaunchRockets()", data, foundApp, foundObj);
00126 //  qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00127 //  foundApp.data(), foundObj.data());
00128 
00129   // Find an application that matches with "konqueror*"
00130   bool boolResult = client->findObject( "konqueror*", "", "", data, foundApp, foundObj);
00131   qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00132     foundApp.data(), foundObj.data());
00133 
00134   // Find an object called "object1" in any application.
00135   boolResult = client->findObject( "", "ksycoca", "", data, foundApp, foundObj);
00136   qDebug("findObject: result = %s, %s, %s\n", boolResult ? "true" : "false",
00137     foundApp.data(), foundObj.data());
00138 
00139   DCOPClient *client2 = new DCOPClient();
00140   client2->registerAs(app.name(), false);
00141   qDebug("I2 registered as '%s'", client2->appId().data() );
00142 
00143 qDebug("Sending to object1");
00144   client2->send(app.name(), "object1", "aFunction(QString,int)", data );
00145 
00146 qDebug("Calling object1");
00147   if (!client2->call(app.name(), "object1", "aFunction(QString,int)", data, replyType, reply))
00148     qDebug("I couldn't call myself");
00149   else
00150       qDebug("return type was '%s'", replyType.data() ); 
00151 
00152   return app.exec();
00153 
00154   client->detach();
00155 }
00156 
00157 #include "testdcop.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:26 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001