summaryrefslogtreecommitdiffstats
path: root/kicker-applets/mediacontrol/amarokInterface.cpp
blob: f52ae4b07b1cd2e2fee07cd6e858bb953717c9e9 (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
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
/***************************************************************************
                          Interface to access Amarok
                             -------------------
    begin                : Tue Dec  2 23:54:53 CET 2003
    copyright            : (c) 2003 by Thomas Capricelli
    adapted from juk* (C) 2001-2002 by Stefan Gehn (metz {AT} gehn {DOT} net)
    email                : orzel@freehackers.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 "amarokInterface.h"
#include "amarokInterface.moc"

#include <kdebug.h>
#include <tqstringlist.h>
#include <tqstrlist.h>
#include <kurldrag.h>

#define TIMER_FAST  250

AmarokInterface::AmarokInterface() : PlayerInterface()
{
   mTimerValue = TIMER_FAST;
   mAmarokTimer = new TQTimer ( this, "mAmaroKTimer" );

   connect(mAmarokTimer, TQT_SIGNAL(timeout()), TQT_SLOT(updateSlider()) );
   kapp->dcopClient()->setNotifications ( true );

   connect(kapp->dcopClient(), TQT_SIGNAL(applicationRegistered(const TQCString&)),
      TQT_SLOT(appRegistered(const TQCString&)) );

   connect(kapp->dcopClient(), TQT_SIGNAL(applicationRemoved(const TQCString&)),
      TQT_SLOT(appRemoved(const TQCString&)));

   TQTimer::singleShot(0, this, TQT_SLOT(myInit()));
}

AmarokInterface::~AmarokInterface()
{
   kapp->dcopClient()->setNotifications(false);
   delete mAmarokTimer;
}

void AmarokInterface::myInit()
{
   // Start the timer if amarok is already running
   // Needed if user adds applet while running amarok
   if ( findRunningAmarok() )
   {
      emit playerStarted();
      mAmarokTimer->start(mTimerValue);
   }
   else
   {
      emit playerStopped();
      emit newSliderPosition(0,0);
   }
}

void AmarokInterface::appRegistered ( const TQCString &appId )
{
   if(appId.contains("amarok",false) )
   {
      mAppId = appId;
      emit playerStarted();
      mAmarokTimer->start(mTimerValue);
   }
}

void AmarokInterface::appRemoved ( const TQCString &appId )
{
   if ( appId.contains("amarok",false) )
   {
       // is there still another amarok alive?
      if ( findRunningAmarok() )
         return;
      mAmarokTimer->stop();
      emit playerStopped();
      emit newSliderPosition(0,0);
   }
}

void AmarokInterface::updateSlider ( )
{
   // length/time in msecs, -1 means "no playobject in amarok"
   int len, time;
   TQByteArray data, replyData;
   TQCString replyType;

   if (!kapp->dcopClient()->call(mAppId, "player", "trackTotalTime()",data, replyType, replyData))
   {
      //kdDebug(90200) << "mediacontrol: DCOP communication Error" << endl;
      // -2 is an internal errornumber, might be used later
      len = -2;
   }
   else
   {
      TQDataStream reply(replyData, IO_ReadOnly);
      if (replyType == "int")
      {
         reply >> len;
      }
      else
      {
         //kdDebug(90200) << "mediacontrol: unexpected type of DCOP-reply" << endl;
         // -3 is an internal errornumber, might be used later
         len = -3;
      }
   }

   data = 0;
   replyData = 0;
   replyType = 0;

   if (!kapp->dcopClient()->call(mAppId, "player", "trackCurrentTime()",data, replyType, replyData))
   {
      //kdDebug(90200) << "mediacontrol: DCOP communication error" << endl;
      time = -2;
   }
   else
   {
      TQDataStream reply(replyData, IO_ReadOnly);
      if (replyType == "int")
      {
         reply >> time;
      }
      else
      {
         //kdDebug(90200) << "mediacontrol: unexpected type of DCOP-reply" << endl;
         time = -3;
      }
   }

   if ( (time < 0) || (len < 0)) // Amarok isn't playing and thus returns -1
   {
      len = 0;
      time = 0;
   }
   emit newSliderPosition(len,time);
   emit playingStatusChanged(playingStatus());
}

// Drag-n-Drop stuff =================================================================

void AmarokInterface::dragEnterEvent(TQDragEnterEvent* event)
{
//   kdDebug(90200) << "AmarokInterface::dragEnterEvent()" << endl;
   event->accept( KURLDrag::canDecode(event) );
}

void AmarokInterface::dropEvent(TQDropEvent* event)
{
   kdDebug(90200) << "AmarokInterface::dropEvent()" << endl;
   KURL::List list;
   if (KURLDrag::decode(event, list))
   {
      TQByteArray data;
      TQDataStream arg(data, IO_WriteOnly);
      arg << list;
      if (!kapp->dcopClient()->send(mAppId, "player", "addMediaList(KURL::List)",data))
         kdDebug(90200) << "Couldn't send drop to amarok" << endl;
   }
}

// ====================================================================================

void AmarokInterface::sliderStartDrag()
{
   mAmarokTimer->stop();
}

void AmarokInterface::sliderStopDrag()
{
   mAmarokTimer->start(mTimerValue);
}

void AmarokInterface::jumpToTime( int sec )
{
   TQByteArray data;
   TQDataStream arg(data, IO_WriteOnly);
   arg << sec;
   kapp->dcopClient()->send(mAppId, "player", "seek(int)", data);
}

void AmarokInterface::playpause()
{
   if (!findRunningAmarok())
       startPlayer("amarok");
   TQByteArray data;
   kapp->dcopClient()->send(mAppId, "player", "playPause()", data);
}

void AmarokInterface::stop()
{
   TQByteArray data;
   kapp->dcopClient()->send(mAppId, "player", "stop()", data);
}

void AmarokInterface::next()
{
   TQByteArray data;
   kapp->dcopClient()->send(mAppId, "player", "next()", data);
}

void AmarokInterface::prev()
{
   TQByteArray data;
   kapp->dcopClient()->send(mAppId, "player", "prev()", data);
}

void AmarokInterface::volumeUp()
{
   TQByteArray data;
   kapp->dcopClient()->send(mAppId, "player", "volumeUp()", data);
}

void AmarokInterface::volumeDown()
{
   TQByteArray data;
   kapp->dcopClient()->send(mAppId, "player", "volumeDown()", data);
}

const TQString AmarokInterface::getTrackTitle() const
{
   TQString title;
   TQByteArray data, replyData;
   TQCString replyType;
   if (!kapp->dcopClient()->call(mAppId, "player", "nowPlaying()",data, replyType, replyData))
   {
      //kdDebug(90200) << "mediacontrol: DCOP communication Error" << endl;
      return TQString("");
   }
   else
   {
      TQDataStream reply(replyData, IO_ReadOnly);
      if (replyType == "TQString")
      {
         reply >> title;
         return title;
      }
      else
      {
         //kdDebug(90200) << "mediacontrol: unexpected type of DCOP-reply" << endl;
         return TQString("");
      }
   }
}

bool AmarokInterface::findRunningAmarok()
{
   QCStringList allApps = kapp->dcopClient()->registeredApplications();
   TQValueList<TQCString>::const_iterator iterator;
   TQByteArray data, replyData;
   TQCString replyType;

   for (iterator = allApps.constBegin(); iterator != allApps.constEnd(); ++iterator)
   {
      if ((*iterator).contains("amarok",false))
      {
         if (kapp->dcopClient()->call((*iterator), "player", "interfaces()", data, replyType, replyData) )
    {
            if ( replyType == "QCStringList" )
            {
               TQDataStream reply(replyData, IO_ReadOnly);
               QCStringList list;
               reply >> list;

               if ( list.contains("AmarokPlayerInterface") )
               {
                  kdDebug(90200) << "mediacontrol: amarok found" << endl;
                  mAppId = *iterator;
                  return true;
               }
            }
         }
      }
   }
   kdDebug(90200) << "mediacontrol: amarok not found" << endl;
   return false;
}


int AmarokInterface::playingStatus()
{
   TQByteArray data, replyData;
   TQCString replyType;

   if (kapp->dcopClient()->call(mAppId, "player", "status()", data, replyType,
      replyData))
   {
      int status = 0;
      TQDataStream reply(replyData, IO_ReadOnly);
      if (replyType == "int")
         reply >> status;
      if (status == 2)
         return Playing;
      else if (status == 1)
         return Paused;
   }
   return Stopped;
}