26 #include <QtCore/QBool>
27 #include <QtGui/QPixmapCache>
43 struct AppPresenceCurrent
54 class ContactPresenceListCurrent :
public QList<AppPresenceCurrent>
58 bool update(
const AppPresenceCurrent );
59 AppPresenceCurrent best();
63 class KIMProxy::Private
74 bool ContactPresenceListCurrent::update( AppPresenceCurrent ap )
82 bool bestChanged =
false;
83 AppPresenceCurrent best;
85 ContactPresenceListCurrent::iterator it =
begin();
86 const ContactPresenceListCurrent::iterator itEnd =
end();
87 ContactPresenceListCurrent::iterator existing = itEnd;
91 if ( (*it).presence > best.presence )
93 if ( (*it).appId == ap.appId )
98 if ( ap.presence > best.presence ||
99 best.appId == ap.appId )
102 if ( existing != itEnd )
110 AppPresenceCurrent ContactPresenceListCurrent::best()
112 AppPresenceCurrent best;
114 ContactPresenceListCurrent::iterator it =
begin();
115 const ContactPresenceListCurrent::iterator itEnd =
end();
116 while ( it != itEnd )
118 if ( (*it).presence > best.presence )
123 if ( best.presence == -1 )
172 return new OrgKdeKIMInterface( app,
"/KIMIface", QDBusConnection::sessionBus() );
181 KIMProxy::KIMProxy() :
QObject(), d( new Private )
184 m_initialized =
false;
185 connect( QDBusConnection::sessionBus().interface(),
189 d->presence_strings.append(
"Unknown" );
190 d->presence_strings.append(
"Offline" );
191 d->presence_strings.append(
"Connecting" );
192 d->presence_strings.append(
"Away" );
193 d->presence_strings.append(
"Online" );
195 d->presence_icons.append(
"presence_unknown" );
196 d->presence_icons.append(
"presence_offline" );
197 d->presence_icons.append(
"presence_connecting" );
198 d->presence_icons.append(
"presence_away" );
199 d->presence_icons.append(
"presence_online" );
206 QDBusConnection::sessionBus().connect(
QString(),
"/KIMIface",
"org.kde.KIM",
"contactPresenceChanged",
212 qDeleteAll(m_im_client_stubs);
217 if ( !m_initialized )
219 m_initialized =
true;
225 KService::List::const_iterator offer;
226 QStringList registeredApps = QDBusConnection::sessionBus().interface()->registeredServiceNames();
227 foreach (
const QString &app, registeredApps)
231 for ( offer = offers.begin(); offer != offers.end(); ++offer )
233 QString dbusService = (*offer)->property(
"X-DBUS-ServiceName").toString();
234 if ( !dbusService.isEmpty() )
238 if ( app.startsWith( dbusService ) )
240 m_apps_available =
true;
242 if ( !m_im_client_stubs.contains( dbusService ) )
244 kDebug(
debugArea() ) <<
"App " << app <<
", found, using it for presence info.";
254 return !m_im_client_stubs.isEmpty();
260 if ( m_im_client_stubs.contains( appId ) )
262 kDebug(
debugArea() ) << appId <<
" quit, removing its presence info.";
264 PresenceStringMap::Iterator it = d->presence_map.begin();
265 const PresenceStringMap::Iterator
end = d->presence_map.end();
266 for ( ; it != end; ++it )
268 ContactPresenceListCurrent
list = it.value();
269 ContactPresenceListCurrent::iterator cpIt = list.begin();
270 while( cpIt != list.end() )
272 ContactPresenceListCurrent::iterator gone = cpIt++;
273 if ( (*gone).appId == appId )
279 delete m_im_client_stubs.take( appId );
284 if ( !newOwner.isEmpty() ) {
290 KService::List::const_iterator it;
291 for ( it = offers.begin(); it != offers.end(); ++it )
293 QString dbusService = (*it)->property(
"X-DBUS-ServiceName").toString();
294 if ( appId.startsWith( dbusService ) )
297 if ( !m_im_client_stubs.contains( appId ) )
300 kDebug(
debugArea() ) <<
"App: " << appId <<
", dbusService: " << dbusService <<
" started, using it for presence info.";
316 ContactPresenceListCurrent current;
317 current = d->presence_map[ uid ];
319 AppPresenceCurrent newPresence;
320 newPresence.appId = appId;
321 newPresence.presence = presence;
323 if ( current.update( newPresence ) )
325 d->presence_map.insert( uid, current );
332 AppPresenceCurrent ap;
336 ContactPresenceListCurrent presence = d->presence_map[ uid ];
337 ap = presence.best();
344 AppPresenceCurrent ap;
348 ContactPresenceListCurrent presence = d->presence_map[ uid ];
349 ap = presence.best();
351 if ( ap.appId.isEmpty() )
354 return d->presence_strings[ ap.presence ];
359 AppPresenceCurrent ap;
363 ContactPresenceListCurrent presence = d->presence_map[ uid ];
364 ap = presence.best();
366 if ( ap.appId.isEmpty() )
374 return SmallIcon( d->presence_icons[ ap.presence ]);
390 QHashIterator<QString, OrgKdeKIMInterface*> it( m_im_client_stubs );
394 value += it.value()->reachableContacts( );
403 PresenceStringMap::iterator it = d->presence_map.begin();
404 const PresenceStringMap::iterator end= d->presence_map.end();
405 for ( ; it != end; ++it )
406 if ( it.value().best().presence > 2 )
407 value.append( it.key() );
418 QHashIterator<QString, OrgKdeKIMInterface*> it( m_im_client_stubs );
422 value += it.value()->fileTransferContacts( );
430 return ( !d->presence_map[ uid ].isEmpty() );
438 if ( OrgKdeKIMInterface* s =
stubForUid( uid ) )
439 name = s->displayName( uid );
449 if ( OrgKdeKIMInterface* s =
stubForUid( uid ) )
450 return s->canReceiveFiles( uid );
459 if ( OrgKdeKIMInterface* s =
stubForUid( uid ) )
460 return s->canRespond( uid );
469 if ( OrgKdeKIMInterface* s =
stubForUid( uid ) )
470 return s->context( uid );
479 if ( OrgKdeKIMInterface* s =
stubForUid( uid ) )
481 kapp->updateRemoteUserTimestamp( s->service() );
482 s->chatWithContact( uid );
492 if ( OrgKdeKIMInterface* s =
stubForUid( uid ) )
494 kapp->updateRemoteUserTimestamp( s->service() );
495 s->messageContact( uid, message );
505 QHashIterator<QString,OrgKdeKIMInterface*> it( m_im_client_stubs );
506 while ( it.hasNext() )
509 if ( it.value()->canReceiveFiles( uid ) )
511 kapp->updateRemoteUserTimestamp( it.value()->service() );
512 it.value()->sendFile( uid, sourceURL, altFileName, fileSize );
525 return s->addContact( contactId, protocol );
535 return s->locate( contactId, protocol );
542 return ( !m_im_client_stubs.isEmpty() );
548 # warning "unused variable: preferences"
560 kDebug(
debugArea() ) <<
"error was: " << error <<
", dbusService: " << dbusService;
562 return ( result == 0 );
588 OrgKdeKIMInterface * appStub = m_im_client_stubs.value( appId );
589 QStringList contacts = m_im_client_stubs.value( appId )->allContacts();
590 QStringList::iterator it = contacts.begin();
591 QStringList::iterator end = contacts.end();
592 for ( ; it != end; ++it )
594 ContactPresenceListCurrent current = d->presence_map[ *it ];
595 AppPresenceCurrent ap;
598 # warning "KIMProxy::pollApp( const QString & appId ).presenceStatus() function doesn't exist Need to fix it"
601 current.append( ap );
603 d->presence_map.insert( *it, current );
604 if ( current.update( ap ) )
613 AppPresenceCurrent ap = d->presence_map[ uid ].best();
615 return m_im_client_stubs.value( ap.appId );
622 # warning "KIMProxy::stubForProtocol( const QString &protocol) code disabled: protocols() function doesn't exist. Need to fix it"
625 OrgKdeKIMInterface * app;
628 if ( ( app = m_im_client_stubs.value( preferred ) ) )
630 if ( app->protocols().value().filter( protocol ).count() > 0 )
634 QHashIterator<QString, OrgKdeKIMInterface*> it( m_im_client_stubs );
635 while ( it.hasNext() )
638 if ( it.value()->protocols().value().filter( protocol ).count() > 0 )
654 #include "kimproxy.moc"