summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/nowlistening/nlkaffeine.cpp
blob: 20ba1a79a296ecdef5ce28270401f4c0cab8bf9d (plain)
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
/*
    nlkaffeine.cpp

    Kopete Now Listening To plugin

    Copyright (c) 2004 by Will Stephenson <will@stevello.free-online.co.uk>
    Kopete
	Copyright (c) 2002,2003,2004 by the Kopete developers  <kopete-devel@kde.org>
	
	Purpose: 
	This class abstracts the interface to Kaffeine by
	implementing NLMediaPlayer

    *************************************************************************
    *                                                                       *
    * 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 <kdebug.h>
#include <tqstring.h>

#include "nlmediaplayer.h"
#include "nlkaffeine.h"

NLKaffeine::NLKaffeine( DCOPClient *client ) : NLMediaPlayer()
{
	m_client = client;
	m_type = Video;
	m_name = "Kaffeine";
}

void NLKaffeine::update()
{
	m_playing = false;
	m_newTrack = false;
	TQString newTrack;
	bool error = true; // Asume we have a error first. 
	TQCString kaffeineIface("KaffeineIface"), kaffeineGetTrack("title()");

	// see if kaffeine is  registered with DCOP
	if ( m_client->isApplicationRegistered( "kaffeine" ) )
	{
		// see if it's playing
		TQByteArray data, replyData;
		TQCString replyType;
		TQString result;
		if ( !m_client->call( "kaffeine", kaffeineIface, "isPlaying()", data,
					replyType, replyData ) )
		{
			kdDebug ( 14307 ) << k_funcinfo << " Trying DCOP interface of Kaffeine < 0.5" << endl;
			// Trying with the old Kaffeine DCOP interface (<0.5)
			kaffeineIface = "Kaffeine";
			kaffeineGetTrack = "getTitle()";
			if( !m_client->call( "kaffeine", kaffeineIface, "isPlaying()", data, replyType, replyData ) )
			{
				kdDebug( 14307 ) << k_funcinfo << " DCOP error on Kaffeine." << endl;
			}
			else
			{
				error = false;
			}
		}
		else
		{
			error = false;
		}
			
		// If we didn't get any DCOP error, check if Kaffeine is playing.
		if(!error)
		{
			TQDataStream reply( replyData, IO_ReadOnly );
			if ( replyType == "bool" ) {
					reply >> m_playing;
					kdDebug( 14307 ) << "checked if Kaffeine is playing!" << endl;
			}
		}

		if ( m_client->call( "kaffeine", kaffeineIface, kaffeineGetTrack, data,
					replyType, replyData ) )
		{
			TQDataStream reply( replyData, IO_ReadOnly );

			if ( replyType == "TQString" ) {
				reply >> newTrack;
			}
		}
		if( newTrack != m_track )
		{
			m_newTrack = true;
			m_track = newTrack;
		}
		if ( kaffeineIface == "Kaffeine" )
		{
			// old Kaffeine DCOP interface (<0.5)
			return;
		}

		if ( m_client->call( "kaffeine", kaffeineIface, "album()", data,
					replyType, replyData ) )
		{
			TQDataStream reply( replyData, IO_ReadOnly );

			if ( replyType == "TQString" ) {
				reply >> m_album;
			}
		}

		if ( m_client->call( "kaffeine", kaffeineIface, "artist()", data,
					replyType, replyData ) )
		{
			TQDataStream reply( replyData, IO_ReadOnly );

			if ( replyType == "TQString" ) {
				reply >> m_artist;
			}
		}
	}
	else
		kdDebug ( 14307 ) << "Kaffeine is not running!\n" << endl;
}