1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/*
nowlisteningplugin.cpp
Kopete Now Listening To plugin
Copyright (c) 2002,2003,2004 by Will Stephenson <will@stevello.free-online.co.uk>
Copyright (c) 2005-2006 by Michaƫl Larouche <michael.larouche@kdemail.net>
Kopete (c) 2002-2006 by the Kopete developers <kopete-devel@kde.org>
*************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
*************************************************************************
*/
#include <tqtimer.h>
#include <tqstringlist.h>
#include <tqstylesheet.h>
#include <tqregexp.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <tdeapplication.h>
#include <dcopclient.h>
#include <tdeaction.h>
#include "config.h"
#include "kopetechatsessionmanager.h"
#include "kopetemetacontact.h"
#include "kopetecontact.h"
#include "kopetecommandhandler.h"
#include "kopeteaccount.h"
#include "kopeteprotocol.h"
#include "kopeteaccountmanager.h"
#include "nowlisteningconfig.h"
#include "nowlisteningplugin.h"
#include "nlmediaplayer.h"
#include "nlkscd.h"
#include "nlnoatun.h"
#include "nljuk.h"
#include "nlamarok.h"
#include "nlkaffeine.h"
#include "nowlisteningguiclient.h"
#if defined TQ_WS_X11 && !defined K_WS_TQTONLY && defined HAVE_XMMS
#include "nlxmms.h"
#endif
class NowListeningPlugin::Private
{
public:
Private() : m_currentMediaPlayer(0L), m_client(0L), m_currentChatSession(0L), m_currentMetaContact(0L),
advertTimer(0L)
{}
// abstracted media player interfaces
TQPtrList<NLMediaPlayer> m_mediaPlayerList;
NLMediaPlayer *m_currentMediaPlayer;
// Needed for DCOP interprocess communication
DCOPClient *m_client;
Kopete::ChatSession *m_currentChatSession;
Kopete::MetaContact *m_currentMetaContact;
// Used when using automatic advertising to know who has already gotten
// the music information
TQStringList m_musicSentTo;
// Used when advertising to status message.
TQTimer *advertTimer;
};
typedef KGenericFactory<NowListeningPlugin> NowListeningPluginFactory;
K_EXPORT_COMPONENT_FACTORY( kopete_nowlistening, NowListeningPluginFactory( "kopete_nowlistening" ) )
NowListeningPlugin::NowListeningPlugin( TQObject *parent, const char* name, const TQStringList& /*args*/ )
: Kopete::Plugin( NowListeningPluginFactory::instance(), parent, name )
{
if ( pluginStatic_ )
kdDebug( 14307 )<<"####"<<"Now Listening already initialized"<<endl;
else
pluginStatic_ = this;
d = new Private;
kdDebug(14307) << k_funcinfo << endl;
// Connection for the "/media" command (always needed)
connect( Kopete::ChatSessionManager::self(), TQT_SIGNAL(
chatSessionCreated( Kopete::ChatSession * )) , TQT_SLOT( slotNewKMM(
Kopete::ChatSession * ) ) );
// If autoadvertising is on...
connect(Kopete::ChatSessionManager::self(),
TQT_SIGNAL(aboutToSend(Kopete::Message&)),
this,
TQT_SLOT(slotOutgoingMessage(Kopete::Message&)));
TQValueList<Kopete::ChatSession*> sessions = Kopete::ChatSessionManager::self()->sessions();
for (TQValueListIterator<Kopete::ChatSession*> it= sessions.begin(); it!=sessions.end() ; ++it)
slotNewKMM( *it );
// get a pointer to the dcop client
d->m_client = kapp->dcopClient(); //new DCOPClient();
// set up known media players
d->m_mediaPlayerList.setAutoDelete( true );
d->m_mediaPlayerList.append( new NLKscd( d->m_client ) );
d->m_mediaPlayerList.append( new NLNoatun( d->m_client ) );
d->m_mediaPlayerList.append( new NLJuk( d->m_client ) );
d->m_mediaPlayerList.append( new NLamaroK( d->m_client ) );
d->m_mediaPlayerList.append( new NLKaffeine( d->m_client ) );
#if defined TQ_WS_X11 && !defined K_WS_TQTONLY && HAVE_XMMS
d->m_mediaPlayerList.append( new NLXmms() );
#endif
// User has selected a specific mediaPlayer so update the currentMediaPlayer pointer.
if( NowListeningConfig::self()->useSpecifiedMediaPlayer() )
{
updateCurrentMediaPlayer();
}
// watch for '/media' getting typed
Kopete::CommandHandler::commandHandler()->registerCommand(
this,
"media",
TQT_SLOT( slotMediaCommand( const TQString &, Kopete::ChatSession * ) ),
i18n("USAGE: /media - Displays information on current song"),
0
);
connect ( this , TQT_SIGNAL( settingsChanged() ) , this , TQT_SLOT( slotSettingsChanged() ) );
// Advert the accounts with the current listened track.
d->advertTimer = new TQTimer(this);
connect(d->advertTimer, TQT_SIGNAL( timeout() ), this, TQT_SLOT( slotAdvertCurrentMusic() ) );
d->advertTimer->start(5000); // Update every 5 seconds
}
NowListeningPlugin::~NowListeningPlugin()
{
//kdDebug( 14307 ) << k_funcinfo << endl;
delete d;
pluginStatic_ = 0L;
}
void NowListeningPlugin::slotNewKMM(Kopete::ChatSession *KMM)
{
new NowListeningGUIClient( KMM, this );
}
NowListeningPlugin* NowListeningPlugin::plugin()
{
return pluginStatic_ ;
}
void NowListeningPlugin::slotMediaCommand( const TQString &args, Kopete::ChatSession *theChat )
{
TQString advert = mediaPlayerAdvert();
if ( advert.isEmpty() )
{
// Catch no players/no track playing message case:
// Since we can't stop a message send in a plugin, add some message text to
// prevent us sending an empty message
advert = i18n("Message from Kopete user to another user; used when sending media information even though there are no songs playing or no media players running", "Now Listening for Kopete - it would tell you what I am listening to, if I was listening to something on a supported media player.");
}
Kopete::Message msg( theChat->myself(),
theChat->members(),
advert + " " + args,
Kopete::Message::Outbound,
Kopete::Message::PlainText
);
theChat->sendMessage( msg );
}
void NowListeningPlugin::slotOutgoingMessage(Kopete::Message& msg)
{
// Only do stuff if autoadvertising is on
if(!NowListeningConfig::self()->chatAdvertising())
return;
TQString originalBody = msg.escapedBody();
// If it is a /media message, don't process it
if(originalBody.startsWith(NowListeningConfig::self()->header()))
return;
// What will be sent
TQString newBody;
// Getting the list of contacts the message will be sent to to determine if at least
// one of them has never gotten the current music information.
Kopete::ContactPtrList dest = msg.to();
bool mustSendAnyway = false;
for( Kopete::Contact *c = dest.first() ; c ; c = dest.next() )
{
const TQString& cId = c->contactId();
if( 0 == d->m_musicSentTo.contains( cId ) )
{
mustSendAnyway = true;
// The contact will get the music information so we put it in the list.
d->m_musicSentTo.push_back( cId );
}
}
bool newTrack = newTrackPlaying();
// We must send the music information if someone has never gotten it or the track(s)
// has changed since it was last sent.
if ( mustSendAnyway || newTrack )
{
TQString advert = mediaPlayerAdvert(false); // false since newTrackPlaying() did the update
if( !advert.isEmpty() )
newBody = originalBody + "<br>" + TQStyleSheet::escape(advert);
// If we send because the information has changed since it was last sent, we must
// rebuild the list of contacts the latest information was sent to.
if( newTrack )
{
d->m_musicSentTo.clear();
for( Kopete::Contact *c = dest.first() ; c ; c = dest.next() )
{
d->m_musicSentTo.push_back( c->contactId() );
}
}
}
// If the body has been modified, change the message
if( !newBody.isEmpty() )
{
msg.setBody( newBody, Kopete::Message::RichText );
}
}
void NowListeningPlugin::slotAdvertCurrentMusic()
{
// Do anything when statusAdvertising is off.
if( !NowListeningConfig::self()->statusAdvertising() && !NowListeningConfig::self()->appendStatusAdvertising() )
return;
// This slot is called every 5 seconds, so we check if we have a new track playing.
if( newTrackPlaying() )
{
TQString advert;
TQPtrList<Kopete::Account> accountsList = Kopete::AccountManager::self()->accounts();
for( Kopete::Account* a = accountsList.first(); a; a = accountsList.next() )
{
/*
NOTE:
MSN status message(personal message) use a special tag to advert the current music playing.
So, we don't send the all formatted string, send a special string seperated by ";".
Also, do not use MSN hack in appending mode.
*/
if( a->protocol()->pluginId() == "MSNProtocol" && !NowListeningConfig::self()->appendStatusAdvertising() )
{
TQString track, artist, album, mediaList;
bool isPlaying=false;
if( NowListeningConfig::self()->useSpecifiedMediaPlayer() && d->m_currentMediaPlayer )
{
if( d->m_currentMediaPlayer->playing() )
{
track = d->m_currentMediaPlayer->track();
artist = d->m_currentMediaPlayer->artist();
album = d->m_currentMediaPlayer->album();
mediaList = track + ";" + artist + ";" + album;
isPlaying = true;
}
}
else
{
for ( NLMediaPlayer* i = d->m_mediaPlayerList.first(); i; i = d->m_mediaPlayerList.next() )
{
if( i->playing() )
{
track = i->track();
artist = i->artist();
album = i->album();
mediaList = track + ";" + artist + ";" + album;
isPlaying = true;
}
}
}
// KDE4 TODO: Use the new status message framework, and remove this "hack".
if( isPlaying )
{
advert = TQString("[Music]%1").arg(mediaList);
}
}
else
{
if( NowListeningConfig::self()->appendStatusAdvertising() )
{
// Check for the now listening message in parenthesis,
// include the header to not override other messages in parenthesis.
TQRegExp statusSong( TQString(" \\(%1.*\\)$").arg( NowListeningConfig::header()) );
// HACK: Don't keep appending the now listened song. Replace it in the status message.
advert = a->myself()->property( Kopete::Global::Properties::self()->awayMessage() ).value().toString();
// Remove the braces when they are no listened song.
TQString mediaAdvert = mediaPlayerAdvert(false);
if(!mediaAdvert.isEmpty())
{
if(statusSong.search(advert) != -1)
{
advert = advert.replace(statusSong, TQString(" (%1)").arg(mediaPlayerAdvert(false)) );
}
else
{
advert += TQString(" (%1)").arg( mediaPlayerAdvert(false) );
}
}
else
{
advert = advert.replace(statusSong, "");
}
}
else
{
advert = mediaPlayerAdvert(false); // newTrackPlaying has done the update.
}
}
a->setOnlineStatus(a->myself()->onlineStatus(), advert);
}
}
}
TQString NowListeningPlugin::mediaPlayerAdvert(bool update)
{
// generate message for all players
TQString message;
if( NowListeningConfig::self()->useSpecifiedMediaPlayer() && d->m_currentMediaPlayer != 0L )
{
buildTrackMessage(message, d->m_currentMediaPlayer, update);
}
else
{
for ( NLMediaPlayer* i = d->m_mediaPlayerList.first(); i; i = d->m_mediaPlayerList.next() )
{
buildTrackMessage(message, i, update);
}
}
kdDebug( 14307 ) << k_funcinfo << message << endl;
return message;
}
void NowListeningPlugin::buildTrackMessage(TQString &message, NLMediaPlayer *player, bool update)
{
TQString perTrack = NowListeningConfig::self()->perTrack();
if(update)
player->update();
if ( player->playing() )
{
kdDebug( 14307 ) << k_funcinfo << player->name() << " is playing" << endl;
if ( message.isEmpty() )
message = NowListeningConfig::self()->header();
if ( message != NowListeningConfig::self()->header() ) // > 1 track playing!
message = message + NowListeningConfig::self()->conjunction();
message = message + substDepthFirst( player, perTrack, false );
}
}
bool NowListeningPlugin::newTrackPlaying(void) const
{
if( NowListeningConfig::self()->useSpecifiedMediaPlayer() && d->m_currentMediaPlayer != 0L )
{
d->m_currentMediaPlayer->update();
if( d->m_currentMediaPlayer->newTrack() )
return true;
}
else
{
for ( NLMediaPlayer* i = d->m_mediaPlayerList.first(); i; i = d->m_mediaPlayerList.next() )
{
i->update();
if( i->newTrack() )
return true;
}
}
return false;
}
TQString NowListeningPlugin::substDepthFirst( NLMediaPlayer *player,
TQString in, bool inBrackets ) const
{
TQString track = player->track();
TQString artist = player->artist();
TQString album = player->album();
TQString playerName = player->name();
for ( unsigned int i = 0; i < in.length(); i++ )
{
TQChar c = in.at( i );
//kdDebug(14307) << "Now working on:" << in << " char is: " << c << endl;
if ( c == '(' )
{
// find matching bracket
int depth = 0;
//kdDebug(14307) << "Looking for ')'" << endl;
for ( unsigned int j = i + 1; j < in.length(); j++ )
{
TQChar d = in.at( j );
//kdDebug(14307) << "Got " << d << endl;
if ( d == '(' )
depth++;
if ( d == ')' )
{
// have we found the match?
if ( depth == 0 )
{
// recursively replace contents of matching ()
TQString substitution = substDepthFirst( player,
in.mid( i + 1, j - i - 1), true ) ;
in.replace ( i, j - i + 1, substitution );
// perform substitution and return the result
i = i + substitution.length() - 1;
break;
}
else
depth--;
}
}
}
}
// no () found, perform substitution!
// get each string (to) to substitute for (from)
bool done = false;
if ( in.contains ( "%track" ) )
{
if ( track.isEmpty() )
track = i18n("Unknown track");
in.replace( "%track", track );
done = true;
}
if ( in.contains ( "%artist" ) && !artist.isEmpty() )
{
if ( artist.isEmpty() )
artist = i18n("Unknown artist");
in.replace( "%artist", artist );
done = true;
}
if ( in.contains ( "%album" ) && !album.isEmpty() )
{
if ( album.isEmpty() )
album = i18n("Unknown album");
in.replace( "%album", album );
done = true;
}
if ( in.contains ( "%player" ) && !playerName.isEmpty() )
{
if ( playerName.isEmpty() )
playerName = i18n("Unknown player");
in.replace( "%player", playerName );
done = true;
}
// make whether we return anything dependent on whether we
// were in brackets and if we were, if a substitution was made.
if ( inBrackets && !done )
return "";
return in;
}
void NowListeningPlugin::advertiseToChat( Kopete::ChatSession *theChat, TQString message )
{
Kopete::ContactPtrList pl = theChat->members();
// get on with it
kdDebug(14307) << k_funcinfo <<
( pl.isEmpty() ? "has no " : "has " ) << "interested recipients: " << endl;
/* for ( pl.first(); pl.current(); pl.next() )
kdDebug(14307) << "NowListeningPlugin::advertiseNewTracks() " << pl.current()->displayName() << endl; */
// if no-one in this KMM wants to be advertised to, don't send
// any message
if ( pl.isEmpty() )
return;
Kopete::Message msg( theChat->myself(),
pl,
message,
Kopete::Message::Outbound,
Kopete::Message::PlainText );
theChat->sendMessage( msg );
}
void NowListeningPlugin::updateCurrentMediaPlayer()
{
kdDebug(14307) << k_funcinfo << "Update current media player (single mode)" << endl;
d->m_currentMediaPlayer = d->m_mediaPlayerList.at( NowListeningConfig::self()->selectedMediaPlayer() );
}
void NowListeningPlugin::slotSettingsChanged()
{
// Force reading config
NowListeningConfig::self()->readConfig();
// Update the currentMediaPlayer, because config has changed.
if( NowListeningConfig::useSpecifiedMediaPlayer() )
updateCurrentMediaPlayer();
disconnect(Kopete::ChatSessionManager::self(),
TQT_SIGNAL(aboutToSend(Kopete::Message&)),
this,
TQT_SLOT(slotOutgoingMessage(Kopete::Message&)));
d->advertTimer->stop();
disconnect(d->advertTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotAdvertCurrentMusic()));
if( NowListeningConfig::self()->chatAdvertising() )
{
kdDebug(14307) << k_funcinfo << "Now using chat window advertising." << endl;
connect(Kopete::ChatSessionManager::self(),
TQT_SIGNAL(aboutToSend(Kopete::Message&)),
this,
TQT_SLOT(slotOutgoingMessage(Kopete::Message&)));
}
else if( NowListeningConfig::self()->statusAdvertising() || NowListeningConfig::self()->appendStatusAdvertising() )
{
kdDebug(14307) << k_funcinfo << "Now using status message advertising." << endl;
connect(d->advertTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotAdvertCurrentMusic()));
d->advertTimer->start(5000);
}
}
NowListeningPlugin* NowListeningPlugin::pluginStatic_ = 0L;
#include "nowlisteningplugin.moc"
|