forked from ufbycd/kdoubanfm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trayicon.cpp
766 lines (648 loc) · 24.2 KB
/
trayicon.cpp
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/*
* This file is part of KDoubanFM, Douban FM Client
* Copyright (C) 2012 Ni Hui <shuizhuyuanluo@126.com>
*
* 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) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "trayicon.h"
#include <QDialog>
#include <QTimer>
#include <QUrl>
#include <QFile>
#include <QMessageBox>
#include <KAboutApplicationDialog>
#include <KAction>
#include <KComponentData>
#include <KConfig>
#include <KConfigGroup>
#include <KDebug>
#include <KGlobal>
#include <KIcon>
#include <KIO/Job>
#include <KIO/StoredTransferJob>
#include <KLocale>
#include <KMenu>
#include <KShortcut>
#include <KNotification>
#include <KWallet/Wallet>
#include "mpris2.h"
#include "ui_account.h"
TrayIcon::TrayIcon() : KStatusNotifierItem("kdoubanfm")
{
likeAction = 0;
m_channelMenu = 0;
m_channelAct = 0;
m_channelId = 0;
m_playIndex = -1;
m_playIndexNext = 0;
m_albumPictureJob = 0;
m_media = 0;
m_output = 0;
setAssociatedWidget(contextMenu());
setIconByName("kdoubanfm");
// setTitle(i18n("KDoubanFM"));
setToolTipIconByName("kdoubanfm");
setToolTipTitle(i18n("KDoubanFM"));
setToolTipSubTitle(i18n("Douban FM"));
setCategory(KStatusNotifierItem::ApplicationStatus);
setStatus(KStatusNotifierItem::Active);
// lazy initialization
QTimer::singleShot(0, this, SLOT(init()));
}
TrayIcon::~TrayIcon()
{
if ( m_notify )
m_notify.data()->close();
}
void TrayIcon::login()
{
QUrl apiUrl("http://www.douban.com/j/app/login");
// login info
QUrl postUrl;
postUrl.addEncodedQueryItem("email", m_accountEmail);
postUrl.addEncodedQueryItem("password", m_accountPassword);
postUrl.addEncodedQueryItem("app_name", "radio_desktop_win");
postUrl.addEncodedQueryItem("version", "100");
KIO::TransferJob* job = KIO::storedHttpPost(postUrl.encodedQuery(), apiUrl, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_login(KJob*)));
job->start();
}
void TrayIcon::channels()
{
QUrl apiUrl("http://www.douban.com/j/app/radio/channels");
KIO::TransferJob* job = KIO::storedGet(apiUrl, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_channels(KJob*)));
job->start();
}
void TrayIcon::playlist(const QList<SongInfo>& history)
{
QUrl apiUrl("http://www.douban.com/j/app/radio/people");
// auth info
apiUrl.addEncodedQueryItem("user_id", m_userid);
apiUrl.addEncodedQueryItem("expire", m_expire);
apiUrl.addEncodedQueryItem("token", m_token);
apiUrl.addEncodedQueryItem("app_name", "radio_desktop_win");
apiUrl.addEncodedQueryItem("version", "100");
// playlist info
apiUrl.addEncodedQueryItem("type", history.isEmpty() ? "n" : "p");// new playlist
apiUrl.addEncodedQueryItem("channel", QByteArray::number(m_channelId));
if (!history.isEmpty()) {
const SongInfo& esi = history.last();
apiUrl.addEncodedQueryItem("sid", QByteArray::number(esi.sid));// last sid
// create history parameter
QStringList hes;
foreach (const SongInfo& si, history) {
hes << QString::number(si.sid) + ':' + (si.fullplayed ? 'p' : 's');
}
apiUrl.addEncodedQueryItem("h", QUrl::toPercentEncoding(hes.join("|")));// history
}
else {
apiUrl.addEncodedQueryItem("h", "");// empty history
}
apiUrl.addEncodedQueryItem("r", "weq24d903jj");// random 10 bytes here
KIO::TransferJob* job = KIO::storedGet(apiUrl, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_playlist(KJob*)));
job->start();
}
void TrayIcon::like(int sid)
{
QUrl apiUrl("http://www.douban.com/j/app/radio/people");
// auth info
apiUrl.addEncodedQueryItem("user_id", m_userid);
apiUrl.addEncodedQueryItem("expire", m_expire);
apiUrl.addEncodedQueryItem("token", m_token);
apiUrl.addEncodedQueryItem("app_name", "radio_desktop_win");
apiUrl.addEncodedQueryItem("version", "100");
// playlist info
apiUrl.addEncodedQueryItem("type", "r");// rate
apiUrl.addEncodedQueryItem("channel", QByteArray::number(m_channelId));
apiUrl.addEncodedQueryItem("sid", QByteArray::number(sid));// song id
apiUrl.addEncodedQueryItem("r", "zz343903oo");// random 10 bytes here
KIO::TransferJob* job = KIO::storedGet(apiUrl, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_playlist(KJob*)));
job->start();
}
void TrayIcon::unlike(int sid)
{
QUrl apiUrl("http://www.douban.com/j/app/radio/people");
// auth info
apiUrl.addEncodedQueryItem("user_id", m_userid);
apiUrl.addEncodedQueryItem("expire", m_expire);
apiUrl.addEncodedQueryItem("token", m_token);
apiUrl.addEncodedQueryItem("app_name", "radio_desktop_win");
apiUrl.addEncodedQueryItem("version", "100");
// playlist info
apiUrl.addEncodedQueryItem("type", "u");// unrate
apiUrl.addEncodedQueryItem("channel", QByteArray::number(m_channelId));
apiUrl.addEncodedQueryItem("sid", QByteArray::number(sid));// song id
apiUrl.addEncodedQueryItem("r", "wrggx34f55");// random 10 bytes here
KIO::TransferJob* job = KIO::storedGet(apiUrl, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_playlist(KJob*)));
job->start();
}
void TrayIcon::ban(int sid)
{
QUrl apiUrl("http://www.douban.com/j/app/radio/people");
// auth info
apiUrl.addEncodedQueryItem("user_id", m_userid);
apiUrl.addEncodedQueryItem("expire", m_expire);
apiUrl.addEncodedQueryItem("token", m_token);
apiUrl.addEncodedQueryItem("app_name", "radio_desktop_win");
apiUrl.addEncodedQueryItem("version", "100");
// playlist info
apiUrl.addEncodedQueryItem("type", "b");// ban
apiUrl.addEncodedQueryItem("channel", QByteArray::number(m_channelId));
apiUrl.addEncodedQueryItem("sid", QByteArray::number(sid));// song id
apiUrl.addEncodedQueryItem("r", "3094fjidse");// random 10 bytes here
KIO::TransferJob* job = KIO::storedGet(apiUrl, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_playlist(KJob*)));
job->start();
}
void TrayIcon::end(int sid)
{
QUrl apiUrl("http://www.douban.com/j/app/radio/people");
// auth info
apiUrl.addEncodedQueryItem("user_id", m_userid);
apiUrl.addEncodedQueryItem("expire", m_expire);
apiUrl.addEncodedQueryItem("token", m_token);
apiUrl.addEncodedQueryItem("app_name", "radio_desktop_win");
apiUrl.addEncodedQueryItem("version", "100");
// playlist info
apiUrl.addEncodedQueryItem("type", "e");// end
apiUrl.addEncodedQueryItem("channel", QByteArray::number(m_channelId));
apiUrl.addEncodedQueryItem("sid", QByteArray::number(sid));// song id
apiUrl.addEncodedQueryItem("r", "ie93ddlaaw");// random 10 bytes here
KIO::TransferJob* job = KIO::storedGet(apiUrl, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slot_end(KJob*)));
job->start();
}
void TrayIcon::init()
{
// setup menu actions
nowplayingLabel = new KAction(KIcon("emblem-music-symbolic"), i18n("nowpalying ..."), this);
nowplayingLabel->setObjectName("kdoubanfm-nowplaying");
contextMenu()->addAction(nowplayingLabel);
pauseAction = new KAction(KIcon("media-playback-start"), i18n("&Play"), this);
pauseAction->setObjectName("kdoubanfm-pause");
pauseAction->setGlobalShortcut(KShortcut(Qt::Key_MediaPlay));
connect(pauseAction, SIGNAL(triggered()), this, SLOT(slotPauseAction()));
contextMenu()->addAction(pauseAction);
skipAction = new KAction(KIcon("media-skip-forward"), i18n("&Skip"), this);
skipAction->setObjectName("kdoubanfm-skip");
skipAction->setGlobalShortcut(KShortcut(Qt::Key_MediaNext));
connect(skipAction, SIGNAL(triggered()), this, SLOT(slotSkipAction()));
contextMenu()->addAction(skipAction);
likeAction = new KAction(KIcon("non-starred-symbolic"), i18n("&Like"), this);
likeAction->setEnabled(false);
// likeAction->setCheckable(true);
m_iscurLike = false;
connect(likeAction, SIGNAL(triggered()), this, SLOT(slotLikeAction()));
contextMenu()->addAction(likeAction);
banAction = new KAction(KIcon("user-trash-symbolic"), i18n("&Ban"), this);
banAction->setEnabled(false);
connect(banAction, SIGNAL(triggered()), this, SLOT(slotBanAction()));
contextMenu()->addAction(banAction);
m_channelMenu = contextMenu()->addMenu(KIcon("view-statistics"), i18n("Channel"));
KAction* accountAction = new KAction(KIcon("im-user"), i18n("A&ccount..."), this);
connect(accountAction, SIGNAL(triggered()), this, SLOT(slotAccountAction()));
contextMenu()->addAction(accountAction);
KAction* aboutAction = new KAction(KIcon("kdoubanfm"), i18n("&About..."), this);
connect(aboutAction, SIGNAL(triggered()), this, SLOT(slotAboutAction()));
contextMenu()->addAction(aboutAction);
m_channelAct = new QActionGroup(this);
m_channelAct->setExclusive(true);
connect(m_channelAct, SIGNAL(triggered(QAction*)), this, SLOT(slotChannelAction(QAction*)));
//Prepare for KWallet by metalbrick
m_wallet = KWallet::Wallet::openWallet(KWallet::Wallet::NetworkWallet(),
contextMenu()->winId(),
KWallet::Wallet::Asynchronous);
connect(m_wallet, SIGNAL(walletOpened(bool)), SLOT(walletOpened(bool)));
// predefined red heart channel
m_redHeartMHz = m_channelMenu->addAction(i18n("Red Heart MHz"));
m_redHeartMHz->setCheckable(true);
m_redHeartMHz->setData(-3);
m_redHeartMHz->setEnabled(false);
m_channelAct->addAction(m_redHeartMHz);
// setup media player
m_media = new Phonon::MediaObject(this);
m_output = new Phonon::AudioOutput(Phonon::MusicCategory, this);
Phonon::createPath(m_media, m_output);
connect(m_media, SIGNAL(aboutToFinish()), this, SLOT(slotMediaAboutToFinish()));
connect(m_media, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
this, SLOT(slotMediaCurrentSourceChanged()));
connect(m_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
this, SLOT(slotMeidaStateChanged(Phonon::State)));
// mpris object
new Mpris2(this);
// get channel sub menu entries
channels();
}
void TrayIcon::slot_login(KJob* job)
{
if (job->error()) {
kWarning() << "Job Error: " << job->errorString();
return;
}
KIO::StoredTransferJob* j = static_cast<KIO::StoredTransferJob*>(job);
// kWarning() << QString::fromUtf8(j->data());
bool ok;
QVariantMap map = m_parser.parse(j->data(), &ok).toMap();
if (!ok) {
kWarning() << "parse error";
return;
}
int ret = map["r"].toInt();
if (ret != 0) {
/// login error here
kWarning() << "login error";
return;
}
m_userid = map["user_id"].toByteArray();
m_token = map["token"].toByteArray();
m_expire = map["expire"].toByteArray();
m_username = map["user_name"].toByteArray();
m_email = map["email"].toByteArray();
// request initial playlist
playlist();
likeAction->setEnabled(true);
nowplayingLabel->setEnabled(true);
banAction->setEnabled(true);
m_redHeartMHz->setEnabled(true);
}
void TrayIcon::slot_channels(KJob* job)
{
if (job->error()) {
kWarning() << "Job Error: " << job->errorString();
return;
}
KIO::StoredTransferJob* j = static_cast<KIO::StoredTransferJob*>(job);
// kWarning() << QString::fromUtf8(j->data());
bool ok;
QVariantMap map = m_parser.parse(j->data(), &ok).toMap();
if (!ok) {
kWarning() << "parse error";
return;
}
QVariantList channellist = map["channels"].toList();
foreach (const QVariant& channel, channellist) {
QVariantMap channelmap = channel.toMap();
QString name = channelmap["name"].toString();
// int seq_id = channelmap["seq_id"].toInt();
int channel_id = channelmap["channel_id"].toInt();
// kWarning() << name << seq_id << channel_id;
QAction* channelAction = m_channelMenu->addAction(name);
channelAction->setCheckable(true);
channelAction->setData(channel_id);
if (m_channelId == channel_id) {
channelAction->setChecked(true);
}
m_channelAct->addAction(channelAction);
}
}
void TrayIcon::slot_playlist(KJob* job)
{
if (job->error()) {
kWarning() << "Job Error: " << job->errorString();
return;
}
KIO::StoredTransferJob* j = static_cast<KIO::StoredTransferJob*>(job);
// kWarning() << QString::fromUtf8(j->data());
bool ok;
QVariantMap map = m_parser.parse(j->data(), &ok).toMap();
if (!ok) {
kWarning() << "parse error";
return;
}
int ret = map["r"].toInt();
if (ret != 0) {
/// playlist error here
kWarning() << "playlist error";
return;
}
// save to an empty playlist
QList<SongInfo>& playlist = m_playlist.isEmpty() ? m_playlist : m_playlistNext;
playlist.clear();
QVariantList songlist = map["song"].toList();
foreach (const QVariant& song, songlist) {
QVariantMap songmap = song.toMap();
SongInfo si;
QString subtype = songmap["subtype"].toString();
if (!subtype.isEmpty()) {
// skip ad
continue;
}
si.picture = songmap["picture"].toString();
si.albumtitle = songmap["albumtitle"].toString();
// QString company = songmap["company"].toString();
// double rating_avg = songmap["rating_avg"].toDouble();
// QString public_time = songmap["public_time"].toString();
// QString ssid = songmap["ssid"].toString();
// QString album = songmap["album"].toString();
si.like = songmap["like"].toInt();
si.artist = songmap["artist"].toString();
si.url = songmap["url"].toString();
si.title = songmap["title"].toString();
si.length = songmap["length"].toInt();
si.sid = songmap["sid"].toInt();
si.aid = songmap["aid"].toInt();
si.fullplayed = false;
kWarning() << si.title << si.artist << si.albumtitle << si.like;
playlist << si;
}
// enqueue next song
m_playIndexNext = 0;
const SongInfo& si = playlist.first();
m_media->clearQueue();
m_media->enqueue(QList<QUrl>() << si.url);
if (m_media->state() != Phonon::PlayingState
&& m_media->state() != Phonon::PausedState) {
m_media->play();
}
}
void TrayIcon::slot_end(KJob* job)
{
if (job->error()) {
kWarning() << "Job Error: " << job->errorString();
return;
}
// KIO::StoredTransferJob* j = static_cast<KIO::StoredTransferJob*>(job);
// kWarning() << QString::fromUtf8(j->data());
}
void TrayIcon::slotPauseAction()
{
if (m_media->state() == Phonon::PlayingState) {
m_media->pause();
}
else {
m_media->play();
}
}
void TrayIcon::slotStopAction()
{
// reset media player and playlist
m_media->stop();
m_media->clear();
m_playlist.clear();
m_playlistNext.clear();
m_playIndex = -1;
m_playIndexNext = 0;
}
void TrayIcon::slotSkipAction()
{
m_media->pause();
if (!m_media->queue().isEmpty()) {
// play next media
m_media->setCurrentSource(m_media->queue().first());
m_media->play();
}
else {
// get playlist in progress, clear the current one
m_media->stop();
m_media->clear();
}
}
void TrayIcon::slotLikeAction()
{
const SongInfo& si = m_playlist.at(m_playIndex);
if ( !m_iscurLike ) {
likeAction->setIcon(KIcon("starred-symbolic"));
likeAction->setText(i18n("&Unlike"));
like(si.sid);
}
else {
likeAction->setIcon(KIcon("non-starred-symbolic"));
likeAction->setText(i18n("&Like"));
unlike(si.sid);
}
m_iscurLike = !m_iscurLike;
}
void TrayIcon::slotBanAction()
{
if ( QMessageBox::information( NULL, "KDoubanFM", "Are you sure Ban this song ?", "Sure", "Cancel") == 0 ) {
const SongInfo& si = m_playlist.at(m_playIndex);
ban(si.sid);
slotSkipAction();
};
}
void TrayIcon::slotAccountAction()
{
QDialog dlg;
Ui::AccountDialog ui;
ui.setupUi(&dlg);
ui.emailEdit->setText(QString::fromAscii(m_accountEmail));
ui.passwordEdit->setText(QString::fromAscii(m_accountPassword));
if (dlg.exec() == QDialog::Accepted) {
// save account info
m_accountEmail = ui.emailEdit->text().toAscii();
m_accountPassword = ui.passwordEdit->text().toAscii();
KConfigGroup cg(KGlobal::config(), "Account");
cg.writeEntry("Email", m_accountEmail);
cg.sync();
// Integrated with KWallet by metalbrick
if (m_wallet->writePassword(m_accountEmail, m_accountPassword)) {
kDebug() << "Writing Password to KWallet Failed.";
}
else {
kDebug() << "Write Password to KWallet Succesfully.";
}
if (!m_accountEmail.isEmpty() && !m_accountPassword.isEmpty()) {
// let us login now
login();
}
}
}
void TrayIcon::walletOpened(bool ok)
{
// Integrated with KWallet by metalbrick
if ( ok &&
(m_wallet->hasFolder("KDoubanFM") ||
m_wallet->createFolder("KDoubanFM")) &&
m_wallet->setFolder("KDoubanFM")) {
// load account info
KConfigGroup cg(KGlobal::config(), "Account");
m_accountEmail = cg.readEntry("Email", QByteArray());
// m_accountPassword = cg.readEntry("Password", QByteArray());
// Reda Account & Password from KWallet
QString tmp_pswd;
m_wallet->readPassword(QString(m_accountEmail), tmp_pswd);
m_accountPassword = tmp_pswd.toUtf8();
if (!m_accountEmail.isEmpty() && !m_accountPassword.isEmpty()) {
// let us login now
login();
}
}
else {
KNotification notify( "message" );
notify.setTitle("KDoubanFM");
notify.setText("Login Failed, Checkup your KWallet.");
notify.sendEvent();
}
}
void TrayIcon::slotAboutAction()
{
KAboutApplicationDialog dlg(KGlobal::mainComponent().aboutData());
dlg.exec();
}
void TrayIcon::slotChannelAction(QAction* action)
{
slotStopAction();
m_channelId = action->data().toInt();
playlist();
}
void TrayIcon::slotMediaAboutToFinish()
{
const SongInfo& si = m_playlist.at(m_playIndex);
si.fullplayed = true;
}
void TrayIcon::slotMediaCurrentSourceChanged()
{
kWarning() << m_playIndex << m_playIndexNext;
// report full played song
if (m_playIndex != -1) {
const SongInfo& si = m_playlist.at(m_playIndex);
if (si.fullplayed) {
end(si.sid);
}
}
m_playIndex = m_playIndexNext;
++m_playIndexNext;
if (!m_playlistNext.isEmpty()) {
// switch next playlist
m_playlist.clear();
m_playlist = m_playlistNext;
m_playIndex = 0;
m_playlistNext.clear();
}
// change current song info
const SongInfo& si = m_playlist.at(m_playIndex);
// setTitle(si.title);
setToolTipIconByName("kdoubanfm");
setToolTipTitle(si.title);
setToolTipSubTitle(si.artist + " - " + si.albumtitle);
// change like state
// likeAction->setChecked(si.like == 1);
m_iscurLike = ( si.like == 1);
if ( m_iscurLike ) {
likeAction->setIcon(KIcon("starred-symbolic"));
likeAction->setText(i18n("&Unlike"));
}
else {
likeAction->setIcon(KIcon("non-starred-symbolic"));
likeAction->setText(i18n("&Like"));
}
// download album picture
if (m_albumPictureJob) {
// kill previous one
m_albumPictureJob->kill();
}
KIO::TransferJob* job = KIO::storedGet(si.picture, KIO::Reload, KIO::HideProgressInfo);
job->addMetaData("content-type", "Content-Type: application/x-www-form-urlencoded");
connect(job, SIGNAL(result(KJob*)), this, SLOT(slotAlbumPicture(KJob*)));
m_albumPictureJob = job;
job->start();
if (m_playIndexNext < m_playlist.count()) {
// enqueue next song in playlist
const SongInfo& nsi = m_playlist.at(m_playIndexNext);
m_media->enqueue(QList<QUrl>() << nsi.url);
}
else {
// no more song in playlist, get new one with history
m_playIndexNext = 0;
playlist(m_playlist);
}
// KNotification Test by metalbrick
KNotification *notify = m_notify.data();
if( !notify )
notify = new KNotification( "trackChange" );
m_curTitle = i18n( si.title.toUtf8() );
m_curText = i18n( (si.artist + " - " + si.albumtitle).toUtf8() );
notify->setTitle( m_curTitle );
notify->setText( m_curText );
notify->setPixmap( m_curCoverart );
// Change Nowplaying Lable on Traymenu
nowplayingLabel->setText((m_curTitle + " / " + m_curText).left(32) + " ..." );
this->setToolTipTitle(m_curTitle + " / " + m_curText);
if( m_notify ) // existing notification already shown
notify->update();
notify->sendEvent(); // (re)start timeout in both cases
m_notify = notify;
}
void TrayIcon::slotMeidaStateChanged(Phonon::State newstate)
{
QString title;
QString text;
QPixmap pixmap;
switch (newstate) {
case Phonon::StoppedState:
title = i18n( "Douban FM" );
text = i18n( "Stopped" );
break;
case Phonon::PausedState:
pauseAction->setIcon(KIcon("media-playback-start"));
pauseAction->setText(i18n("&Play"));
title = i18n( "Douban FM" );
text = i18n( "Paused" );
break;
case Phonon::PlayingState:
pauseAction->setIcon(KIcon("media-playback-pause"));
pauseAction->setText(i18n("&Pause"));
title = m_curTitle;
text = m_curText;
pixmap = m_curCoverart;
break;
case Phonon::ErrorState:
case Phonon::BufferingState:
case Phonon::LoadingState:
break;
}
KNotification *notify = m_notify.data();
if( !notify )
notify = new KNotification( "trackChange" );
notify->setTitle( title );
notify->setText( text );
notify->setPixmap( pixmap );
notify->sendEvent();
if( m_notify ) // existing notification already shown
notify->update();
notify->sendEvent(); // (re)start timeout in both cases
m_notify = notify;
}
void TrayIcon::slotAlbumPicture(KJob* job)
{
if (job->error()) {
kWarning() << "Job Error: " << job->errorString();
return;
}
KIO::StoredTransferJob* j = static_cast<KIO::StoredTransferJob*>(job);
m_albumPictureJob = 0;
QPixmap pixmap;
pixmap.loadFromData(j->data());
m_curCoverart = pixmap;
m_notify.data()->setPixmap(pixmap);
// QFile outFile("/home/transbric/logFile");
// outFile.open(QIODevice::WriteOnly | QIODevice::Append);
// QTextStream out(&outFile);
// out << j->data() << "\n";
setToolTipIconByPixmap(pixmap);
}