File indexing completed on 2024-07-21 10:16:19

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2010-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005     SPDX-FileCopyrightText: 2010-2011 Ramin Gomari <ramin.gomari@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 */
0009 
0010 #include "nowlistening.h"
0011 
0012 #include <QAction>
0013 #include <QDBusConnectionInterface>
0014 #include <QDBusInterface>
0015 #include <QDBusReply>
0016 
0017 #include <KActionCollection>
0018 #include <KLocalizedString>
0019 #include <KPluginFactory>
0020 #include <KMessageBox>
0021 
0022 #include "choqokuiglobal.h"
0023 #include "nowlisteningsettings.h"
0024 #include "quickpost.h"
0025 
0026 #include "mpris.h"
0027 
0028 K_PLUGIN_CLASS_WITH_JSON(NowListening, "choqok_nowlistening.json")
0029 
0030 NowListening::NowListening(QObject *parent, const QList<QVariant> &)
0031     : Choqok::Plugin(QLatin1String("choqok_nowlistening"), parent)
0032 {
0033     QAction *action = new QAction(QIcon::fromTheme(QLatin1String("media-playback-start")), i18n("Now Listening"), this);
0034     actionCollection()->addAction(QLatin1String("nowListening"), action);
0035     connect(action, &QAction::triggered, this, &NowListening::slotPrepareNowListening);
0036     setXMLFile(QLatin1String("nowlisteningui.rc"));
0037 }
0038 
0039 NowListening::~NowListening()
0040 {
0041 
0042 }
0043 
0044 void NowListening::slotPrepareNowListening()
0045 {
0046     QVariantMap trackInfo;
0047     QString player;
0048     bool playerFound = false;
0049     bool isPlaying = false;
0050 
0051     MPRIS amarok(QLatin1String("amarok"));
0052     if (amarok.isValid()) {
0053         if (amarok.isPlaying()) {
0054             trackInfo = amarok.getTrackMetadata();
0055             isPlaying = true;
0056         }
0057         playerFound = true;
0058         player = QLatin1String("Amarok");
0059     }
0060 
0061     MPRIS audacious(QLatin1String("audacious"));
0062     if (!isPlaying && audacious.isValid()) {
0063         if (audacious.isPlaying()) {
0064             trackInfo = audacious.getTrackMetadata();
0065             isPlaying = true;
0066         }
0067         playerFound = true;
0068         player = QLatin1String("Audacious");
0069     }
0070 
0071     // MPRIS id of Dragon Player contain pid of it.
0072     QStringList playersList = MPRIS::getRunningPlayers();
0073     if (!playersList.isEmpty() && playersList.indexOf(QRegExp(QLatin1String("dragonplayer(.*)"))) > -1) {
0074         int i = playersList.indexOf(QRegExp(QLatin1String("dragonplayer(.*)")));
0075         MPRIS dragon(playersList.at(i));
0076         if (!isPlaying && dragon.isValid()) {
0077             if (dragon.isPlaying()) {
0078                 trackInfo = dragon.getTrackMetadata();
0079                 isPlaying = true;
0080             }
0081             playerFound = true;
0082             player = QLatin1String("Dragon Player");
0083         }
0084     }
0085 
0086     //need to enable MPRIS Plugin (Qmmp +0.4)
0087     MPRIS qmmp(QLatin1String("qmmp"));
0088     if (!isPlaying && qmmp.isValid()) {
0089         if (qmmp.isPlaying()) {
0090             trackInfo = qmmp.getTrackMetadata();
0091             isPlaying = true;
0092         }
0093         playerFound = true;
0094         player = QLatin1String("Qmmp");
0095     }
0096 
0097     // only works if enabled D-BUS control interface in VLC (VLC 0.9.0+)
0098     MPRIS vlc(QLatin1String("vlc"));
0099     if (!isPlaying && vlc.isValid()) {
0100         if (vlc.isPlaying()) {
0101             trackInfo = vlc.getTrackMetadata();
0102             isPlaying = true;
0103         }
0104         playerFound = true;
0105         player = QLatin1String("VLC");
0106     }
0107 
0108     //Mpris not complete supported by Kaffeine Version 1.0-svn3
0109     /*
0110     MPRIS kaffeine("kaffeine");
0111     if(!isPlaying && kaffeine.isValid()){
0112       if(kaffeine.isPlaying()){
0113     trackInfo=kaffeine.getTrackMetadata();
0114     isPlaying=true;
0115       }
0116       playerFound=true;
0117       player="Kaffeine";
0118     }
0119     */
0120 
0121     if (!isPlaying && QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.kde.juk")).value()) {
0122         QDBusInterface jukPlayer(QLatin1String("org.kde.juk"),
0123                                  QLatin1String("/Player"),
0124                                  QLatin1String("org.kde.juk.player"));
0125         if (((QDBusReply<bool>)jukPlayer.call(QLatin1String("playing"))).value()) {
0126             QDBusReply< QString> reply = jukPlayer.call(QLatin1String("trackProperty"), QLatin1String("Title"));
0127             trackInfo.insert(QLatin1String("title"), reply.value());
0128 
0129             reply = jukPlayer.call(QLatin1String("trackProperty"), QLatin1String("Track"));
0130             trackInfo.insert(QLatin1String("track"), reply.value());
0131 
0132             reply = jukPlayer.call(QLatin1String("trackProperty"), QLatin1String("Album"));
0133             trackInfo.insert(QLatin1String("album"), reply.value());
0134 
0135             reply = jukPlayer.call(QLatin1String("trackProperty"), QLatin1String("Artist"));
0136             trackInfo.insert(QLatin1String("artist"), reply.value());
0137 
0138             reply = jukPlayer.call(QLatin1String("trackProperty"), QLatin1String("Year"));
0139             trackInfo.insert(QLatin1String("year"), reply.value());
0140 
0141             reply = jukPlayer.call(QLatin1String("trackProperty"), QLatin1String("Genre"));
0142             trackInfo.insert(QLatin1String("genre"), reply.value());
0143             isPlaying = true;
0144         }
0145         playerFound = true;
0146         player = QLatin1String("JuK");
0147     }
0148 
0149     if (!isPlaying && QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.gnome.Rhythmbox")).value())  {
0150         QDBusInterface rhythmboxPlayer(QLatin1String("org.gnome.Rhythmbox") ,
0151                                        QLatin1String("/org/gnome/Rhythmbox/Player"),
0152                                        QLatin1String("org.gnome.Rhythmbox.Player"));
0153         if (((QDBusReply<bool>)rhythmboxPlayer.call(QLatin1String("getPlaying"))).value()) {
0154             QDBusReply<QString> uri = rhythmboxPlayer.call(QLatin1String("getPlayingUri"));
0155 
0156             QDBusInterface rhythmboxShell(QLatin1String("org.gnome.Rhythmbox") ,
0157                                           QLatin1String("/org/gnome/Rhythmbox/Shell"),
0158                                           QLatin1String("org.gnome.Rhythmbox.Shell"));
0159 
0160             QDBusReply< QMap<QString, QVariant> > reply = rhythmboxShell.call(QLatin1String("getSongProperties"), uri.value());
0161             trackInfo = reply.value();
0162             isPlaying = true;
0163         }
0164         playerFound = true;
0165         player = QLatin1String("Rhythmbox");
0166     }
0167 
0168     if (!isPlaying && QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.exaile.Exaile")).value()) {
0169 
0170         QDBusInterface exailePlayer(QLatin1String("org.exaile.Exaile"),
0171                                     QLatin1String("/org/exaile/Exaile"),
0172                                     QLatin1String("org.exaile.Exaile"));
0173         if (((QDBusReply<bool>) exailePlayer.call(QLatin1String("IsPlaying"))).value()) {
0174             QDBusReply<QString> reply = exailePlayer.call(QLatin1String("GetTrackAttr"), QLatin1String("tracknumber"));
0175             trackInfo.insert(QLatin1String("tracknumber"), reply.value());
0176             reply = exailePlayer.call(QLatin1String("GetTrackAttr"), QLatin1String("title"));
0177             trackInfo.insert(QLatin1String("title"), reply.value());
0178             reply = exailePlayer.call(QLatin1String("GetTrackAttr"), QLatin1String("album"));
0179             trackInfo.insert(QLatin1String("album"), reply.value());
0180             reply = exailePlayer.call(QLatin1String("GetTrackAttr"), QLatin1String("artist"));
0181             trackInfo.insert(QLatin1String("artist"), reply.value());
0182             reply = exailePlayer.call(QLatin1String("GetTrackAttr"), QLatin1String("year"));
0183             trackInfo.insert(QLatin1String("year"), reply.value());
0184             reply = exailePlayer.call(QLatin1String("GetTrackAttr"), QLatin1String("genre"));
0185             trackInfo.insert(QLatin1String("genre"), reply.value());
0186             isPlaying = true;
0187         }
0188         playerFound = true;
0189         player = QLatin1String("Exaile");
0190     }
0191 
0192     if (!isPlaying && QDBusConnection::sessionBus().interface()->isServiceRegistered(QLatin1String("org.bansheeproject.Banshee")).value()) {
0193         // provide for new interface in Banshee 1.0+
0194         QDBusInterface bansheePlayer(QLatin1String("org.bansheeproject.Banshee"),
0195                                      QLatin1String("/org/bansheeproject/Banshee/PlayerEngine"),
0196                                      QLatin1String("org.bansheeproject.Banshee.PlayerEngine"));
0197         if (!((QDBusReply<QString>) bansheePlayer.call(QLatin1String("GetCurrentState"))).value().compare(QLatin1String("playing"))) {
0198             QDBusReply< QMap<QString, QVariant> > reply = bansheePlayer.call(QLatin1String("GetCurrentTrack"));
0199             trackInfo = reply.value();
0200             trackInfo.insert(QLatin1String("title"), trackInfo[QLatin1String("name")]);
0201             isPlaying = true;
0202         }
0203         playerFound = true;
0204         player = QLatin1String("Banshee");
0205     }
0206 
0207     //trying to find not supported players that implamented the MPRIS-Dbus interface
0208     if (!isPlaying && !MPRIS::getRunningPlayers().isEmpty()) {
0209 
0210         for (const QString &playerName: MPRIS::getRunningPlayers()) {
0211             playerFound = true;
0212             MPRIS mprisPlayer(playerName);
0213             if (mprisPlayer.isValid() && mprisPlayer.isPlaying()) {
0214                 trackInfo = mprisPlayer.getTrackMetadata();
0215                 isPlaying = true;
0216                 player = mprisPlayer.getPlayerIdentification().left(
0217                              mprisPlayer.getPlayerIdentification().lastIndexOf(QLatin1Char(' '))); //remove the version of player
0218                 break;
0219             }
0220         }
0221     }
0222     if (!isPlaying) {
0223         if (playerFound)
0224             KMessageBox::information(Choqok::UI::Global::mainWindow(),
0225                                      i18nc("Player is running, But it's not playing.",
0226                                            "Play your desired music player."));
0227         else
0228           KMessageBox::error(Choqok::UI::Global::mainWindow(),
0229                              i18n("No supported player found."));
0230         return;
0231     }
0232 
0233     NowListeningSettings::self()->load();
0234     QString text = NowListeningSettings::templateString();
0235     text.replace(QLatin1String("%track%"), trackInfo[QLatin1String("tracknumber")].toString());
0236     text.replace(QLatin1String("%title%"), trackInfo[QLatin1String("title")].toString());
0237     text.replace(QLatin1String("%album%"), trackInfo[QLatin1String("album")].toString());
0238     text.replace(QLatin1String("%artist%"), trackInfo[QLatin1String("artist")].toString());
0239     text.replace(QLatin1String("%year%"), trackInfo[QLatin1String("year")].toString());
0240     text.replace(QLatin1String("%genre%"), trackInfo[QLatin1String("genre")].toString());
0241     text.replace(QLatin1String("%player%"), player);
0242 
0243     if (Choqok::UI::Global::quickPostWidget()) {
0244         Choqok::UI::Global::quickPostWidget()->setText(text);
0245     }
0246 }
0247 
0248 #include "moc_nowlistening.cpp"
0249 #include "nowlistening.moc"