~ubuntu-branches/ubuntu/breezy/kdemultimedia/breezy

« back to all changes in this revision

Viewing changes to noatun/library/player.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-03-24 04:48:58 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050324044858-8ff88o9jxej6ii3d
Tags: 4:3.4.0-0ubuntu3
Add kubuntu_02_hide_arts_menu_entries.diff to hide artsbuilder and artscontrol k-menu entries

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "player.h"
 
2
 
 
3
#include <noatun/playlist.h>
 
4
#include "engine.h"
 
5
#include "app.h"
 
6
#include "titleproxy.h"
 
7
 
 
8
#include <klibloader.h>
 
9
#include <knotifyclient.h>
 
10
#include <klocale.h>
 
11
#include <qfile.h>
 
12
 
 
13
enum ArtsPOS { posIdle=0, posPlaying, posPaused };
 
14
 
 
15
 
 
16
 
 
17
Player::Player(QObject *parent) : QObject(parent, "Player"),
 
18
        position(-1), mLoopStyle(None), firstTimeout(true)
 
19
{
 
20
        mEngine=new Engine;
 
21
        connect(&filePos, SIGNAL(timeout()), SLOT(posTimeout()));
 
22
        connect(mEngine, SIGNAL(aboutToPlay()), this, SLOT(aboutToPlay()));
 
23
        connect(mEngine,
 
24
                SIGNAL(receivedStreamMeta(const QString &, const QString &,
 
25
                        const QString &, const QString &,
 
26
                        const QString &, const QString &)),
 
27
                this, SLOT(
 
28
                        slotUpdateStreamMeta(const QString &, const QString &,
 
29
                                const QString &, const QString &,
 
30
                                const QString &, const QString &))
 
31
        );
 
32
        connect(mEngine, SIGNAL(playingFailed()), this, SLOT(forward()));
 
33
 
 
34
        handleButtons();
 
35
}
 
36
 
 
37
Player::~Player()
 
38
{
 
39
        delete mEngine;
 
40
}
 
41
 
 
42
bool Player::isPlaying()
 
43
{
 
44
        return mEngine->state()==posPlaying;
 
45
}
 
46
 
 
47
bool Player::isPaused()
 
48
{
 
49
        return mEngine->state()==posPaused;
 
50
}
 
51
 
 
52
bool Player::isStopped()
 
53
{
 
54
        return mEngine->state()==posIdle;
 
55
}
 
56
 
 
57
void Player::toggleListView()
 
58
{
 
59
        napp->playlist()->toggleList();
 
60
}
 
61
 
 
62
void Player::handleButtons()
 
63
{
 
64
        switch (mEngine->state())
 
65
        {
 
66
        case (posPlaying):
 
67
                emit playing();
 
68
                break;
 
69
        case (posPaused):
 
70
                emit paused();
 
71
                break;
 
72
        case (posIdle):
 
73
                emit stopped();
 
74
        }
 
75
}
 
76
 
 
77
void Player::back()
 
78
{
 
79
        if (napp->playlist()->previous())
 
80
        {
 
81
                stop();
 
82
                play();
 
83
        }
 
84
}
 
85
 
 
86
void Player::stop()
 
87
{
 
88
        filePos.stop();
 
89
        position=0;
 
90
        mEngine->stop();
 
91
        emit stopped();
 
92
        mCurrent=0;
 
93
}
 
94
 
 
95
void Player::play()
 
96
{
 
97
        napp->processEvents();
 
98
        bool work=false;
 
99
        firstTimeout=true;
 
100
 
 
101
        if (mEngine->state()==posPlaying) // do nothing if already playing
 
102
                return;
 
103
 
 
104
        if (mEngine->state()==posPaused)
 
105
        {
 
106
                work=mEngine->play();
 
107
        }
 
108
        else
 
109
        {
 
110
                stop();
 
111
                mCurrent = napp->playlist()->current();
 
112
                if (!mCurrent)
 
113
                {
 
114
                        work=false;
 
115
                }
 
116
                else
 
117
                {
 
118
 
 
119
                        work=mEngine->open(mCurrent);
 
120
                }
 
121
        }
 
122
 
 
123
        if (!work)
 
124
        {
 
125
                forward(false);
 
126
        }
 
127
        else
 
128
        {
 
129
                filePos.start(500);
 
130
                emit changed();
 
131
                mEngine->play();
 
132
        }
 
133
 
 
134
        handleButtons();
 
135
}
 
136
 
 
137
void Player::play(const PlaylistItem &item)
 
138
{
 
139
        napp->playlist()->setCurrent(item);
 
140
}
 
141
 
 
142
void Player::playpause()
 
143
{
 
144
        if (mEngine->state()==posPlaying)
 
145
        {
 
146
                filePos.stop();
 
147
                mEngine->pause();
 
148
//              emit paused(); NOT necessary because emitted in handleButtons()  (mETz)
 
149
                handleButtons();
 
150
        }
 
151
        else
 
152
                play();
 
153
}
 
154
 
 
155
void Player::forward(bool allowLoop)
 
156
{
 
157
        stop();
 
158
        if (napp->playlist()->next())
 
159
                play();
 
160
        else if (allowLoop && napp->loopList())
 
161
                if (napp->playlist()->reset(), napp->playlist()->current())
 
162
                        play();
 
163
}
 
164
 
 
165
void Player::skipTo(int msec) // skip to a certain time in the track
 
166
{
 
167
        if( (current()) && (msec>=0) )
 
168
        {
 
169
                mEngine->seek(msec);
 
170
                position = mEngine->position(); // make sure position is recent
 
171
                emit timeout(); // update the UI
 
172
                emit skipped(msec);
 
173
                emit skipped();
 
174
        }
 
175
}
 
176
 
 
177
void Player::playCurrent()
 
178
{
 
179
        if (!mEngine->initialized()) return;
 
180
        stop();
 
181
        mCurrent=0;
 
182
        if (napp->playlist()->current())
 
183
                play();
 
184
}
 
185
 
 
186
void Player::newCurrent()
 
187
{
 
188
        // the second half of the following
 
189
        if (!napp->playlist() || !mEngine->initialized())
 
190
                return; // no playlist, or squelch playing as an optimization
 
191
        if ((mEngine->state()!=posPlaying) && napp->autoPlay())
 
192
                playCurrent();
 
193
}
 
194
 
 
195
void Player::posTimeout()
 
196
{
 
197
        if (mEngine->state()==posIdle)
 
198
        {
 
199
                stop();
 
200
                handleButtons();
 
201
                // If you're supposed to loop the song, don't go next
 
202
                // otherwise, do go next
 
203
                if (loopStyle()==Song || napp->playlist()->next())
 
204
                        play();
 
205
                else if (loopStyle()==Playlist)
 
206
                {
 
207
                        napp->playlist()->reset();
 
208
                        play();
 
209
                }
 
210
                else if (napp->loopList())
 
211
                        napp->playlist()->reset();
 
212
 
 
213
                return;
 
214
        }
 
215
        position = mEngine->position();
 
216
 
 
217
        if (current())
 
218
        {
 
219
                current().setLength(mEngine->length());
 
220
                if (current().length() && firstTimeout)
 
221
                {
 
222
                        int minutes = (int) ( current().length() / 60 );
 
223
                        int seconds = current().length() - minutes * 60;
 
224
                        emit newSongLen ( minutes, seconds );
 
225
                        firstTimeout = false;
 
226
                        emit newSong();
 
227
                }
 
228
        }
 
229
 
 
230
        emit timeout();
 
231
}
 
232
 
 
233
QString Player::lengthString(int _position)
 
234
{
 
235
        if (!current())
 
236
                return QString("--:--/--:--");
 
237
 
 
238
        QString posString;
 
239
        QString lenString;
 
240
        int secs, seconds, minutes;
 
241
 
 
242
        if (_position < 0)
 
243
                _position = position;
 
244
 
 
245
        if (_position<0)
 
246
        {
 
247
                posString="--:--/";
 
248
        }
 
249
        else
 
250
        { // get the position
 
251
                bool remain = napp->displayRemaining() && current();
 
252
                if (remain && current().length()<0)
 
253
                {
 
254
                        remain = false;
 
255
                }
 
256
 
 
257
                if (remain)
 
258
                {
 
259
                        _position = current().length() - _position;
 
260
                }
 
261
 
 
262
                secs = _position / 1000; // convert milliseconds -> seconds
 
263
                seconds = secs % 60;
 
264
                minutes = (secs - seconds) / 60;
 
265
 
 
266
                // the string has to look like '00:00/00:00'
 
267
                posString.sprintf("%.2d:%.2d/", minutes, seconds);
 
268
                if (remain) posString.prepend('-');
 
269
        }
 
270
 
 
271
        if (!current() || current().length()<0)
 
272
        {
 
273
                posString+="--:--";
 
274
        }
 
275
        else
 
276
        { // get the length
 
277
                secs = current().length() / 1000; // convert milliseconds -> seconds
 
278
                seconds = secs % 60;
 
279
                minutes = (secs - seconds) / 60;
 
280
 
 
281
                // the string has to look like '00:00/00:00'
 
282
                lenString.sprintf("%.2d:%.2d", minutes, seconds);
 
283
                posString += lenString;
 
284
        }
 
285
 
 
286
        return posString;
 
287
}
 
288
 
 
289
int Player::getLength()
 
290
{
 
291
        if (!current())
 
292
                return -1;
 
293
        return current().length(); // return track-length in milliseconds
 
294
}
 
295
 
 
296
void Player::openFile(const KURL &f, bool purge, bool autoplay)
 
297
{
 
298
        if (purge)
 
299
                napp->playlist()->clear();
 
300
        napp->playlist()->addFile(f, autoplay);
 
301
}
 
302
 
 
303
void Player::openFile(const KURL::List &f, bool purge, bool autoplay)
 
304
{
 
305
        if (purge)
 
306
                napp->playlist()->clear();
 
307
        for (KURL::List::ConstIterator i(f.begin()); i != f.end(); ++i)
 
308
        {
 
309
                napp->playlist()->addFile(*i, autoplay);
 
310
                autoplay=false;
 
311
        }
 
312
}
 
313
 
 
314
 
 
315
void Player::loop()
 
316
{
 
317
        mLoopStyle++;
 
318
        if (mLoopStyle>Random)
 
319
                mLoopStyle=None;
 
320
        emit loopTypeChange(mLoopStyle);
 
321
}
 
322
void Player::loop(int i)
 
323
{
 
324
        mLoopStyle=i;
 
325
        emit loopTypeChange(mLoopStyle);
 
326
}
 
327
 
 
328
void Player::removeCurrent()
 
329
{
 
330
        if (napp->playlist()->current())
 
331
                napp->playlist()->current().remove();
 
332
}
 
333
 
 
334
void Player::setVolume(int v)
 
335
{
 
336
        if (v<0) v=0;
 
337
        if (v>100) v=100;
 
338
        mEngine->setVolume(v);
 
339
        emit timeout();
 
340
        emit volumeChanged(v);
 
341
}
 
342
 
 
343
int Player::volume() const
 
344
{
 
345
        return mEngine->volume();
 
346
}
 
347
 
 
348
void Player::aboutToPlay()
 
349
{
 
350
        emit aboutToOpen( mCurrent );
 
351
}
 
352
 
 
353
void Player::slotUpdateStreamMeta(
 
354
        const QString &streamName, const QString &streamGenre,
 
355
        const QString &streamUrl, const QString &streamBitrate,
 
356
        const QString &trackTitle, const QString &trackUrl)
 
357
{
 
358
        PlaylistItem currentItem = napp->playlist()->current();
 
359
        if(currentItem)
 
360
        {
 
361
                currentItem.setProperty("title", trackTitle);
 
362
                currentItem.setProperty("bitrate", streamBitrate);
 
363
 
 
364
                if(!streamName.isEmpty())
 
365
                        currentItem.setProperty("author", streamName);
 
366
                if(!streamGenre.isEmpty())
 
367
                        currentItem.setProperty("genre", streamGenre);
 
368
                if(!trackUrl.isEmpty())
 
369
                        currentItem.setProperty("comment", trackUrl);
 
370
                else if(!streamUrl.isEmpty())
 
371
                        currentItem.setProperty("comment", streamUrl);
 
372
                else
 
373
                        currentItem.clearProperty("comment");
 
374
                emit changed();
 
375
        }
 
376
}
 
377
 
 
378
#include "player.moc"