00001 /*****************************************************************
00002
00003 Copyright (c) 1999,2000 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 #include <dcopobject.h>
00026 #include <dcopclient.h>
00027
00028 QMap<QCString, DCOPObject *> *kde_dcopObjMap = 0;
00029
00030 static inline QMap<QCString, DCOPObject *> *objMap()
00031 {
00032 if (!kde_dcopObjMap)
00033 kde_dcopObjMap = new QMap<QCString, DCOPObject *>;
00034 return kde_dcopObjMap;
00035 }
00036
00037 class DCOPObject::DCOPObjectPrivate
00038 {
00039 public:
00040 DCOPObjectPrivate()
00041 { m_signalConnections = 0; m_dcopClient = 0; }
00042
00043 unsigned int m_signalConnections;
00044 DCOPClient *m_dcopClient;
00045 };
00046
00047 DCOPObject::DCOPObject()
00048 {
00049 d = new DCOPObjectPrivate;
00050 ident.sprintf("%p", (void *)this );
00051 objMap()->insert(ident, this );
00052 }
00053
00054 DCOPObject::DCOPObject(QObject *obj)
00055 {
00056 d = new DCOPObjectPrivate;
00057 QObject *currentObj = obj;
00058 while (currentObj != 0L) {
00059 ident.prepend( currentObj->name() );
00060 ident.prepend("/");
00061 currentObj = currentObj->parent();
00062 }
00063 if ( ident[0] == '/' )
00064 ident = ident.mid(1);
00065
00066 objMap()->insert(ident, this);
00067 }
00068
00069 DCOPObject::DCOPObject(const QCString &_objId)
00070 : ident(_objId)
00071 {
00072 d = new DCOPObjectPrivate;
00073 if ( ident.isEmpty() )
00074 ident.sprintf("%p", (void *)this );
00075 objMap()->insert(ident, this);
00076 }
00077
00078 DCOPObject::~DCOPObject()
00079 {
00080 DCOPClient *client = DCOPClient::mainClient();
00081 if ( d->m_signalConnections > 0 && client )
00082 client->disconnectDCOPSignal( 0, 0, 0, objId(), 0 );
00083
00084 objMap()->remove(ident);
00085 delete d;
00086 }
00087
00088 DCOPClient *DCOPObject::callingDcopClient()
00089 {
00090 return d->m_dcopClient;
00091 }
00092
00093 void DCOPObject::setCallingDcopClient(DCOPClient *client)
00094 {
00095 d->m_dcopClient = client;
00096 }
00097
00098 bool DCOPObject::setObjId(const QCString &objId)
00099 {
00100 if (objMap()->find(objId)!=objMap()->end()) return false;
00101
00102 DCOPClient *client = DCOPClient::mainClient();
00103 if ( d->m_signalConnections > 0 && client )
00104 client->disconnectDCOPSignal( 0, 0, 0, ident, 0 );
00105
00106 objMap()->remove(ident);
00107 ident=objId;
00108 objMap()->insert(ident,this);
00109 return true;
00110 }
00111
00112 QCString DCOPObject::objId() const
00113 {
00114 return ident;
00115 }
00116
00117 bool DCOPObject::hasObject(const QCString &_objId)
00118 {
00119 if (objMap()->contains(_objId))
00120 return true;
00121 else
00122 return false;
00123 }
00124
00125 DCOPObject *DCOPObject::find(const QCString &_objId)
00126 {
00127 QMap<QCString, DCOPObject *>::ConstIterator it;
00128 it = objMap()->find(_objId);
00129 if (it != objMap()->end())
00130 return *it;
00131 else
00132 return 0L;
00133 }
00134
00135 QPtrList<DCOPObject> DCOPObject::match(const QCString &partialId)
00136 {
00137 QPtrList<DCOPObject> mlist;
00138 QMap<QCString, DCOPObject *>::ConstIterator it(objMap()->begin());
00139 for (; it != objMap()->end(); ++it)
00140 if (it.key().left(partialId.length()) == partialId) // found it?
00141 mlist.append(it.data());
00142 return mlist;
00143 }
00144
00145
00146 QCString DCOPObject::objectName( QObject* obj )
00147 {
00148 if ( obj == 0 )
00149 return QCString();
00150
00151 QCString identity;
00152
00153 QObject *currentObj = obj;
00154 while (currentObj != 0 )
00155 {
00156 identity.prepend( currentObj->name() );
00157 identity.prepend("/");
00158 currentObj = currentObj->parent();
00159 }
00160 if ( identity[0] == '/' )
00161 identity = identity.mid(1);
00162
00163 return identity;
00164 }
00165
00166 bool DCOPObject::process(const QCString &fun, const QByteArray &data,
00167 QCString& replyType, QByteArray &replyData)
00168 {
00169 if ( fun == "interfaces()" ) {
00170 replyType = "QCStringList";
00171 QDataStream reply( replyData, IO_WriteOnly );
00172 reply << interfaces();
00173 return true;
00174 } else if ( fun == "functions()" ) {
00175 replyType = "QCStringList";
00176 QDataStream reply( replyData, IO_WriteOnly );
00177 reply << functions();
00178 return true;
00179 }
00180 return processDynamic( fun, data, replyType, replyData );
00181 }
00182
00183 bool DCOPObject::processDynamic( const QCString&, const QByteArray&, QCString&, QByteArray& )
00184 {
00185 return false;
00186 }
00187 QCStringList DCOPObject::interfacesDynamic()
00188 {
00189 QCStringList result;
00190 return result;
00191 }
00192
00193 QCStringList DCOPObject::functionsDynamic()
00194 {
00195 QCStringList result;
00196 return result;
00197 }
00198 QCStringList DCOPObject::interfaces()
00199 {
00200 QCStringList result = interfacesDynamic();
00201 result << "DCOPObject";
00202 return result;
00203 }
00204
00205 QCStringList DCOPObject::functions()
00206 {
00207 QCStringList result = functionsDynamic();
00208 result.prepend("QCStringList functions()");
00209 result.prepend("QCStringList interfaces()");
00210 return result;
00211 }
00212
00213 void DCOPObject::emitDCOPSignal( const QCString &signal, const QByteArray &data)
00214 {
00215 DCOPClient *client = DCOPClient::mainClient();
00216 if ( client )
00217 client->emitDCOPSignal( objId(), signal, data );
00218 }
00219
00220 bool DCOPObject::connectDCOPSignal( const QCString &sender, const QCString &senderObj,
00221 const QCString &signal,
00222 const QCString &slot,
00223 bool Volatile)
00224 {
00225 DCOPClient *client = DCOPClient::mainClient();
00226
00227 if ( !client )
00228 return false;
00229
00230 d->m_signalConnections++;
00231 return client->connectDCOPSignal( sender, senderObj, signal, objId(), slot, Volatile );
00232 }
00233
00234 bool DCOPObject::disconnectDCOPSignal( const QCString &sender, const QCString &senderObj,
00235 const QCString &signal,
00236 const QCString &slot)
00237 {
00238 DCOPClient *client = DCOPClient::mainClient();
00239
00240 if ( !client )
00241 return false;
00242
00243 d->m_signalConnections--;
00244 return client->disconnectDCOPSignal( sender, senderObj, signal, objId(), slot );
00245 }
00246
00247
00248 QPtrList<DCOPObjectProxy>* DCOPObjectProxy::proxies = 0;
00249
00250 DCOPObjectProxy::DCOPObjectProxy()
00251 {
00252 if ( !proxies )
00253 proxies = new QPtrList<DCOPObjectProxy>;
00254 proxies->append( this );
00255 }
00256
00257 DCOPObjectProxy::DCOPObjectProxy( DCOPClient*)
00258 {
00259 if ( !proxies )
00260 proxies = new QPtrList<DCOPObjectProxy>;
00261 proxies->append( this );
00262 }
00263
00264 DCOPObjectProxy:: ~DCOPObjectProxy()
00265 {
00266 if ( proxies )
00267 proxies->removeRef( this );
00268 }
00269
00270 bool DCOPObjectProxy::process( const QCString& /*obj*/,
00271 const QCString& /*fun*/,
00272 const QByteArray& /*data*/,
00273 QCString& /*replyType*/,
00274 QByteArray &/*replyData*/ )
00275 {
00276 return false;
00277 }
00278
00279 void DCOPObject::virtual_hook( int, void* )
00280 { /*BASE::virtual_hook( id, data );*/ }
00281
00282 void DCOPObjectProxy::virtual_hook( int, void* )
00283 { /*BASE::virtual_hook( id, data );*/ }