KukuRuzo

qomp mpris 2

Jun 15th, 2022
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.98 KB | None | 0 0
  1. diff --git a/plugins/mprisplugin/mprisadapter.cpp b/plugins/mprisplugin/mprisadapter.cpp
  2. index 771d4be..759f76e 100644
  3. --- a/plugins/mprisplugin/mprisadapter.cpp
  4. +++ b/plugins/mprisplugin/mprisadapter.cpp
  5. @@ -1,5 +1,5 @@
  6.  /*
  7. - * Copyright (C) 2013-2019  Khryukin Evgeny, Vitaly Tonkacheyev
  8. + * Copyright (C) 2013-2022  Khryukin Evgeny, Vitaly Tonkacheyev
  9.   *
  10.   * This program is free software; you can redistribute it and/or
  11.   * modify it under the terms of the GNU General Public License
  12. @@ -18,13 +18,13 @@
  13.   */
  14.  
  15.  #include "mprisadapter.h"
  16. -#include "tune.h"
  17.  #include "mpriscontroller.h"
  18. +#include "tune.h"
  19.  
  20. +#include <QStringList>
  21.  #include <QtDBus/QDBusConnection>
  22.  #include <QtDBus/QDBusMessage>
  23.  #include <QtDBus/QDBusObjectPath>
  24. -#include <QStringList>
  25.  #ifdef DEBUG_OUTPUT
  26.  #include <QDebug>
  27.  #endif
  28. @@ -33,18 +33,15 @@
  29.  #endif
  30.  
  31.  MprisAdapter::MprisAdapter(MprisController *p) :
  32. -   QDBusAbstractAdaptor(p),
  33. -   controller_(p),
  34. -   playerStatus_("Stopped"),
  35. -   statusChanged_(false),
  36. -   metadataChanged_(false)
  37. +    QDBusAbstractAdaptor(p), controller_(p), playerStatus_("Stopped"), statusChanged_(false), metadataChanged_(false),
  38. +    shuffle_(false), loopAll_(QStringLiteral("None"))
  39.  {
  40.  }
  41.  
  42.  void MprisAdapter::setStatus(const QString &status)
  43.  {
  44.     if (!status.isEmpty() && (status != playerStatus_)) {
  45. -       playerStatus_ = status;
  46. +       playerStatus_  = status;
  47.         statusChanged_ = true;
  48.     }
  49.     else {
  50. @@ -65,124 +62,123 @@ void MprisAdapter::setMetadata(const QompMetaData &tune)
  51.         metaDataMap_["xesam:album"] = tune.album;
  52.     }
  53.     if (!tune.artist.isEmpty()) {
  54. -       metaDataMap_["xesam:artist"] = QStringList({tune.artist});
  55. +       metaDataMap_["xesam:artist"] = QStringList({ tune.artist });
  56.     }
  57. -   metaDataMap_["xesam:url"] = tune.url;
  58. +   metaDataMap_["xesam:url"]     = tune.url;
  59.     metaDataMap_["xesam:trackNumber"] = tune.trackNumber;
  60. -   metaDataMap_["mpris:length"] = tune.trackLength;
  61. +   metaDataMap_["mpris:length"]      = tune.trackLength;
  62. +   quint32 argument          = 0;
  63.  #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
  64. -   trackId_ = QDBusObjectPath(QString("/org/qomp/MediaPlayer2/Track/%1").arg(qrand()));
  65. +   argument = qrand();
  66.  #else
  67. -   trackId_ = QDBusObjectPath(QString("/org/qomp/MediaPlayer2/Track/%1").arg(QRandomGenerator::global()->generate()));
  68. +   argument = QRandomGenerator::global()->generate();
  69.  #endif
  70. +   trackId_              = QDBusObjectPath(QString("/org/qomp/MediaPlayer2/Track/%1").arg(argument));
  71.     metaDataMap_["mpris:trackid"] = QVariant::fromValue<QDBusObjectPath>(trackId_);
  72. -   if(!tune.cover.isEmpty()) {
  73. +   if (!tune.cover.isEmpty())
  74.         metaDataMap_["mpris:artUrl"] = tune.cover.startsWith("http") ? tune.cover : "file://" + tune.cover;
  75. -   }
  76.     metadataChanged_ = true;
  77.  }
  78.  
  79. -QVariantMap MprisAdapter::metadata() const
  80. -{
  81. -   return metaDataMap_;
  82. -}
  83. +QVariantMap MprisAdapter::metadata() const { return metaDataMap_; }
  84.  
  85. -QString MprisAdapter::playbackStatus() const
  86. -{
  87. -   return playerStatus_;
  88. -}
  89. +QString MprisAdapter::playbackStatus() const { return playerStatus_; }
  90.  
  91.  void MprisAdapter::updateProperties()
  92.  {
  93. -   QVariantMap map({{"CanGoNext", canGoNext()},
  94. -            {"CanGoPrevious", canGoPrevious()},
  95. -            {"CanPlay", canPlay()},
  96. -            {"CanPause", canPause()},
  97. -            {"CanSeek", canSeek()},
  98. -            {"Volume", getVolume()}});
  99. -   if (!playerStatus_.isEmpty() && statusChanged_) {
  100. +   QVariantMap map({ { "CanGoNext", canGoNext() },
  101. +             { "CanGoPrevious", canGoPrevious() },
  102. +             { "CanPlay", canPlay() },
  103. +             { "CanPause", canPause() },
  104. +             { "CanSeek", canSeek() },
  105. +             { "Volume", getVolume() },
  106. +             { "Shuffle", shuffle() },
  107. +             { "LoopStatus", loopAll() } });
  108. +   if (!playerStatus_.isEmpty() && statusChanged_)
  109.         map.insert("PlaybackStatus", playbackStatus());
  110. -   }
  111. +
  112.     if (!metadata().isEmpty() && metadataChanged_) {
  113.         map.insert("Metadata", metadata());
  114.         metadataChanged_ = false;
  115.     }
  116.  
  117. -   QDBusMessage msg = QDBusMessage::createSignal("/org/mpris/MediaPlayer2",
  118. -                             "org.freedesktop.DBus.Properties",
  119. +   QDBusMessage msg = QDBusMessage::createSignal("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties",
  120.                               "PropertiesChanged");
  121.     msg << "org.mpris.MediaPlayer2.Player" << map << QStringList();
  122.     QDBusConnection::sessionBus().send(msg);
  123.  }
  124.  
  125. -
  126.  void MprisAdapter::Play()
  127.  {
  128. -   if(canPlay()) {
  129. +   if (canPlay())
  130.         controller_->emitSignal(PLAY);
  131. -   }
  132.  }
  133.  
  134.  void MprisAdapter::Pause()
  135.  {
  136. -   if(canPause()) {
  137. +   if (canPause())
  138.         controller_->emitSignal(PAUSE);
  139. -   }
  140.  }
  141.  
  142.  void MprisAdapter::Next()
  143.  {
  144. -   if(canGoNext()) {
  145. +   if (canGoNext())
  146.         controller_->emitSignal(NEXT);
  147. -   }
  148.  }
  149.  
  150.  void MprisAdapter::Previous()
  151.  {
  152. -   if(canGoPrevious()) {
  153. +   if (canGoPrevious())
  154.         controller_->emitSignal(PREVIOUS);
  155. -   }
  156.  }
  157.  
  158.  void MprisAdapter::PlayPause()
  159.  {
  160. -   if(canPlay() && canPause()) {
  161. -       if(playerStatus_ == "Playing") {
  162. +   if (canPlay() && canPause()) {
  163. +       if (playerStatus_ == "Playing")
  164.             controller_->emitSignal(PAUSE);
  165. -       }
  166. -       else {
  167. +       else
  168.             controller_->emitSignal(PLAY);
  169. -       }
  170.     }
  171.  }
  172.  
  173.  void MprisAdapter::Stop()
  174.  {
  175. -   if(canControl()) {
  176. +   if (canControl())
  177.         controller_->emitSignal(STOP);
  178. -   }
  179.  }
  180.  
  181.  void MprisAdapter::setVolume(const qreal &volume)
  182.  {
  183. -   if( volume >= 0.0 ) {
  184. +   if (volume >= 0.0)
  185.         controller_->emitSignal(VOLUME, volume);
  186. -   }
  187.  }
  188.  
  189.  void MprisAdapter::SetPosition(const QDBusObjectPath &TrackId, qlonglong Position)
  190.  {
  191. -   if (trackId_ == TrackId && Position > 0.0) {
  192. +   if (trackId_ == TrackId && Position > 0.0)
  193.         controller_->emitSignal(POSITION, Position);
  194. -   }
  195.  }
  196.  
  197. -qreal MprisAdapter::getVolume()
  198. +void MprisAdapter::setLoopAndShuffle(bool loop, bool shuffle)
  199. +{
  200. +   shuffle_ = shuffle;
  201. +   loopAll_ = (loop) ? QStringLiteral("Playlist") : QStringLiteral("None");
  202. +   updateProperties();
  203. +}
  204. +
  205. +void MprisAdapter::sendShuffle(bool shuffle)
  206.  {
  207. -   return controller_->getVolume();
  208. +   shuffle_ = shuffle;
  209. +   controller_->emitSignal(SHUFFLE);
  210.  }
  211.  
  212. -qreal MprisAdapter::getPosition()
  213. +void MprisAdapter::sendLoopStatus(const QString &status)
  214.  {
  215. -   return controller_->getPosition();
  216. +   loopAll_ = status;
  217. +   controller_->emitSignal(LOOPALL);
  218.  }
  219. +
  220. +qreal MprisAdapter::getVolume() { return controller_->getVolume(); }
  221. +
  222. +qreal MprisAdapter::getPosition() { return controller_->getPosition(); }
  223. diff --git a/plugins/mprisplugin/mprisadapter.h b/plugins/mprisplugin/mprisadapter.h
  224. index d1a6ad4..174c835 100644
  225. --- a/plugins/mprisplugin/mprisadapter.h
  226. +++ b/plugins/mprisplugin/mprisadapter.h
  227. @@ -1,5 +1,5 @@
  228.  /*
  229. - * Copyright (C) 2013  Khryukin Evgeny, Vitaly Tonkacheyev
  230. + * Copyright (C) 2013-2022  Khryukin Evgeny, Vitaly Tonkacheyev
  231.   *
  232.   * This program is free software; you can redistribute it and/or
  233.   * modify it under the terms of the GNU General Public License
  234. @@ -50,6 +50,8 @@ class MprisAdapter : public QDBusAbstractAdaptor
  235.     Q_PROPERTY(bool CanControl READ canControl)
  236.     Q_PROPERTY(qreal Volume READ getVolume WRITE setVolume)
  237.     Q_PROPERTY(qlonglong Position READ getPosition)
  238. +   Q_PROPERTY(bool Shuffle READ shuffle WRITE sendShuffle)
  239. +   Q_PROPERTY(QString LoopStatus READ loopAll WRITE sendLoopStatus)
  240.  
  241.  public:
  242.     explicit MprisAdapter(MprisController *p);
  243. @@ -67,6 +69,7 @@ public:
  244.     void setStatus(const QString &status);
  245.     void setMetadata(const QompMetaData &tune);
  246.     void updateProperties();
  247. +   void setLoopAndShuffle(bool loop, bool shuffle);
  248.  
  249.  private:
  250.     QVariantMap metadata() const;
  251. @@ -80,6 +83,10 @@ private:
  252.     void setVolume(const qreal &volume);
  253.     qreal getVolume();
  254.     qreal getPosition();
  255. +   bool shuffle() const {return shuffle_;};
  256. +   QString loopAll() const {return loopAll_;};
  257. +   void sendShuffle(bool shuffle);
  258. +   void sendLoopStatus(const QString &status);
  259.  
  260.  private:
  261.     MprisController *controller_;
  262. @@ -88,6 +95,8 @@ private:
  263.     bool statusChanged_;
  264.     bool metadataChanged_;
  265.     QDBusObjectPath trackId_;
  266. +   bool shuffle_;
  267. +   QString loopAll_;
  268.  };
  269.  
  270.  #endif // MPRISADAPTER_H
  271. diff --git a/plugins/mprisplugin/mpriscontroller.cpp b/plugins/mprisplugin/mpriscontroller.cpp
  272. index 2986e7e..7e33ada 100644
  273. --- a/plugins/mprisplugin/mpriscontroller.cpp
  274. +++ b/plugins/mprisplugin/mpriscontroller.cpp
  275. @@ -22,12 +22,9 @@
  276.  
  277.  #include <QtDBus/QDBusConnection>
  278.  
  279. -MprisController::MprisController(QObject *parent)
  280. -: QObject(parent),
  281. -  rootAdapter_(new RootAdapter(this)),
  282. -  mprisAdapter_(new MprisAdapter(this)),
  283. -  volume_(0.0),
  284. -  position_(0.0)
  285. +MprisController::MprisController(QObject *parent) :
  286. +    QObject(parent), rootAdapter_(new RootAdapter(this)), mprisAdapter_(new MprisAdapter(this)), volume_(0.0),
  287. +    position_(0.0)
  288.  {
  289.     QDBusConnection qompConnection = QDBusConnection::sessionBus();
  290.     qompConnection.registerObject("/org/mpris/MediaPlayer2", this);
  291. @@ -35,10 +32,7 @@ MprisController::MprisController(QObject *parent)
  292.     rootAdapter_->setData();
  293.  }
  294.  
  295. -MprisController::~MprisController()
  296. -{
  297. -   QDBusConnection::sessionBus().unregisterService("org.mpris.MediaPlayer2.qomp");
  298. -}
  299. +MprisController::~MprisController() { QDBusConnection::sessionBus().unregisterService("org.mpris.MediaPlayer2.qomp"); }
  300.  
  301.  void MprisController::sendData(const QString &status, const QompMetaData &tune)
  302.  {
  303. @@ -50,7 +44,7 @@ void MprisController::sendData(const QString &status, const QompMetaData &tune)
  304.  
  305.  void MprisController::emitSignal(SignalType type, const qreal &userValue)
  306.  {
  307. -   switch(type) {
  308. +   switch (type) {
  309.     case PLAY:
  310.         emit play();
  311.         break;
  312. @@ -76,7 +70,13 @@ void MprisController::emitSignal(SignalType type, const qreal &userValue)
  313.         emit volumeChanged(userValue);
  314.         break;
  315.     case POSITION:
  316. -       emit positionChanged(userValue/1000.0);
  317. +       emit positionChanged(userValue / 1000.0);
  318. +       break;
  319. +   case SHUFFLE:
  320. +       emit shuffleUpdated();
  321. +       break;
  322. +   case LOOPALL:
  323. +       emit loopStatusUpdated();
  324.         break;
  325.     }
  326.  }
  327. @@ -93,12 +93,6 @@ qreal MprisController::getPosition()
  328.     return position_;
  329.  }
  330.  
  331. -void MprisController::setVolume(const qreal &volume)
  332. -{
  333. -   volume_ = volume;
  334. -}
  335. +void MprisController::setVolume(const qreal &volume) { volume_ = volume; }
  336.  
  337. -void MprisController::setPosition(const qreal &pos)
  338. -{
  339. -   position_ = pos;
  340. -}
  341. +void MprisController::setPosition(const qreal &pos) { position_ = pos; }
  342. diff --git a/plugins/mprisplugin/mpriscontroller.h b/plugins/mprisplugin/mpriscontroller.h
  343. index 2be0430..c544ff5 100644
  344. --- a/plugins/mprisplugin/mpriscontroller.h
  345. +++ b/plugins/mprisplugin/mpriscontroller.h
  346. @@ -34,7 +34,9 @@ enum SignalType {
  347.     VOLUME = 5,
  348.     QUIT = 6,
  349.     RAISE = 7,
  350. -   POSITION = 8
  351. +   POSITION = 8,
  352. +   SHUFFLE = 9,
  353. +   LOOPALL = 10
  354.  };
  355.  
  356.  class MprisController : public QObject
  357. @@ -50,6 +52,7 @@ public:
  358.     qreal getPosition();
  359.     void setVolume(const qreal &volume);
  360.     void setPosition(const qreal &pos);
  361. +   void setLoopAndShuffle(bool loop, bool shuffle) { mprisAdapter_->setLoopAndShuffle(loop, shuffle); };
  362.  
  363.  signals:
  364.     void play();
  365. @@ -63,6 +66,8 @@ signals:
  366.     void updateVolume();
  367.     void updatePosition();
  368.     void positionChanged(const qreal &position);
  369. +   void shuffleUpdated();
  370. +   void loopStatusUpdated();
  371.  
  372.  private:
  373.     RootAdapter *rootAdapter_;
  374. diff --git a/plugins/mprisplugin/mprisplugin.cpp b/plugins/mprisplugin/mprisplugin.cpp
  375. index 9fe8952..54724b5 100644
  376. --- a/plugins/mprisplugin/mprisplugin.cpp
  377. +++ b/plugins/mprisplugin/mprisplugin.cpp
  378. @@ -1,5 +1,5 @@
  379.  /*
  380. - * Copyright (C) 2013  Khryukin Evgeny
  381. + * Copyright (C) 2013-2022  Khryukin Evgeny
  382.   *
  383.   * This program is free software; you can redistribute it and/or
  384.   * modify it under the terms of the GNU General Public License
  385. @@ -18,17 +18,19 @@
  386.   */
  387.  
  388.  #include "mprisplugin.h"
  389. +#include "common.h"
  390. +#include "defines.h"
  391. +#include "options.h"
  392.  #include "qompplayer.h"
  393.  #include "tune.h"
  394. -#include "common.h"
  395.  
  396. -#include <QTimer>
  397. -#include <QtPlugin>
  398.  #include <QApplication>
  399.  #include <QMainWindow>
  400. -#include <QToolButton>
  401.  #include <QStandardPaths>
  402.  #include <QTemporaryFile>
  403. +#include <QTimer>
  404. +#include <QToolButton>
  405. +#include <QtPlugin>
  406.  #ifdef DEBUG_OUTPUT
  407.  #include <QDebug>
  408.  #endif
  409. @@ -41,49 +43,47 @@
  410.  #define PLAYING "Playing"
  411.  #define nextButtonName "tb_next"
  412.  #define prevButtonName "tb_prev"
  413. +#define shuffleButtonName "tb_shuffle"
  414. +#define loopAllButtonName "tb_repeatAll"
  415.  
  416. -static const QSize maxArtSize(510,510);
  417. +static const QSize maxArtSize(510, 510);
  418.  
  419.  MprisPlugin::MprisPlugin() :
  420. -   player_(nullptr),
  421. -   enabled_(true),
  422. -   mpris_(nullptr),
  423. -   tune_(nullptr),
  424. -   lastTune_(nullptr),
  425. -   artFile_(nullptr)
  426. +    player_(nullptr), enabled_(true), mpris_(nullptr), tune_(nullptr), lastTune_(nullptr), artFile_(nullptr)
  427.  {
  428.  }
  429.  
  430.  void MprisPlugin::qompPlayerChanged(QompPlayer *player)
  431.  {
  432. -   if(player_ != player) {
  433. -       if(player_) {
  434. +   if (player_ != player) {
  435. +       if (player_) {
  436.             disconnect(player_, &QompPlayer::stateChanged, this, &MprisPlugin::playerStatusChanged);
  437.             disconnect(player_, &QompPlayer::tuneDataUpdated, this, &MprisPlugin::tuneUpdated);
  438.         }
  439.  
  440.         player_ = player;
  441. -       if(player_) {
  442. +       if (player_) {
  443.             connect(player_, &QompPlayer::stateChanged, this, &MprisPlugin::playerStatusChanged);
  444. -           //needed to update albumArt for online files
  445. +           // needed to update albumArt for online files
  446.             connect(player_, &QompPlayer::tuneDataUpdated, this, &MprisPlugin::tuneUpdated);
  447.         }
  448.     }
  449.  }
  450.  
  451. -void MprisPlugin::playerControlChanged(QompPlayerControl *control)
  452. -{
  453. -   Q_UNUSED(control)
  454. -}
  455. +void MprisPlugin::playerControlChanged(QompPlayerControl *control) { Q_UNUSED(control) }
  456.  
  457.  void MprisPlugin::setEnabled(bool enabled)
  458.  {
  459.     enabled_ = enabled;
  460. -   if(enabled_) {
  461. -       mpris_ = new MprisController(this);
  462. -       tune_ = new QompMetaData();
  463. +   if (enabled_) {
  464. +       mpris_   = new MprisController(this);
  465. +       tune_    = new QompMetaData();
  466.         artFile_ = new QTemporaryFile(this);
  467.         artFile_->setAutoRemove(true);
  468. +       auto options = Options::instance();
  469. +       bool shuffle = options->getOption(OPTION_SHUFFLE).toBool();
  470. +       bool loopAll = options->getOption(OPTION_REPEAT_ALL).toBool();
  471. +       mpris_->setLoopAndShuffle(loopAll, shuffle);
  472.         connect(mpris_, &MprisController::play, this, &MprisPlugin::play);
  473.         connect(mpris_, &MprisController::pause, this, &MprisPlugin::pause);
  474.         connect(mpris_, &MprisController::stop, this, &MprisPlugin::stop);
  475. @@ -95,6 +95,9 @@ void MprisPlugin::setEnabled(bool enabled)
  476.         connect(mpris_, &MprisController::updateVolume, this, &MprisPlugin::updateVolume);
  477.         connect(mpris_, &MprisController::updatePosition, this, &MprisPlugin::updatePosition);
  478.         connect(mpris_, &MprisController::positionChanged, this, &MprisPlugin::setPosition);
  479. +       connect(options, &Options::updateOptions, this, &MprisPlugin::optionsUpdated);
  480. +       connect(mpris_, &MprisController::shuffleUpdated, this, &MprisPlugin::updateShuffle);
  481. +       connect(mpris_, &MprisController::loopStatusUpdated, this, &MprisPlugin::updateLoopAll);
  482.     }
  483.     else {
  484.         disconnect(mpris_);
  485. @@ -106,159 +109,144 @@ void MprisPlugin::setEnabled(bool enabled)
  486.  
  487.  void MprisPlugin::play()
  488.  {
  489. -   if(player_) {
  490. +   if (player_) {
  491.         player_->play();
  492.     }
  493.  }
  494.  
  495.  void MprisPlugin::pause()
  496.  {
  497. -   if(player_) {
  498. +   if (player_)
  499.         player_->pause();
  500. -   }
  501.  }
  502.  
  503.  void MprisPlugin::stop()
  504.  {
  505. -   if(player_) {
  506. +   if (player_)
  507.         player_->stop();
  508. -   }
  509.  }
  510.  
  511.  void MprisPlugin::next()
  512.  {
  513. -   if(player_) {
  514. +   if (player_) {
  515.         QMainWindow *mainWin = Qomp::getMainWindow();
  516.         if (mainWin) {
  517.             QToolButton *nextBtn = mainWin->findChild<QToolButton *>(nextButtonName);
  518. -           if(nextBtn) {
  519. +           if (nextBtn)
  520.                 emit nextBtn->clicked();
  521. -           }
  522.         }
  523.     }
  524.  }
  525.  
  526.  void MprisPlugin::previous()
  527.  {
  528. -   if(player_) {
  529. +   if (player_) {
  530.         QMainWindow *mainWin = Qomp::getMainWindow();
  531.         if (mainWin) {
  532.             QToolButton *prevBtn = mainWin->findChild<QToolButton *>(prevButtonName);
  533. -           if(prevBtn) {
  534. +           if (prevBtn)
  535.                 emit prevBtn->clicked();
  536. -           }
  537.         }
  538.     }
  539.  }
  540.  
  541.  void MprisPlugin::setVolume(const qreal &volume)
  542.  {
  543. -   if(player_) {
  544. +   if (player_) {
  545.         player_->setVolume(volume);
  546. -       emit player_->volumeChanged(volume); //Temporary hack, FIXME
  547. +       emit player_->volumeChanged(volume); // Temporary hack, FIXME
  548.     }
  549.  }
  550.  
  551. -void MprisPlugin::doQuit()
  552. -{
  553. -   qApp->quit();
  554. -}
  555. +void MprisPlugin::doQuit() { qApp->quit(); }
  556.  
  557.  void MprisPlugin::doRaise()
  558.  {
  559.     QMainWindow *mainWin = Qomp::getMainWindow();
  560. -   if(mainWin) {
  561. -       if(mainWin->isHidden()) {
  562. +   if (mainWin)
  563. +       if (mainWin->isHidden())
  564.             mainWin->show();
  565. -       }
  566. -   }
  567.  }
  568.  
  569.  void MprisPlugin::updateVolume()
  570.  {
  571. -   if(player_) {
  572. +   if (player_)
  573.         mpris_->setVolume(player_->volume());
  574. -   }
  575.  }
  576.  
  577.  void MprisPlugin::updatePosition()
  578.  {
  579. -   if(player_) {
  580. -       mpris_->setPosition(player_->position()*1000); //microseconds
  581. -   }
  582. +   if (player_)
  583. +       mpris_->setPosition(player_->position() * 1000); // microseconds
  584.  }
  585.  
  586.  void MprisPlugin::setPosition(const qreal &position)
  587.  {
  588. -   if(player_) {
  589. -       player_->setPosition(position); //miliseconds
  590. -   }
  591. +   if (player_)
  592. +       player_->setPosition(position); // miliseconds
  593.  }
  594.  
  595. -void MprisPlugin::unload()
  596. -{
  597. -   disableMpris();
  598. -}
  599. +void MprisPlugin::unload() { disableMpris(); }
  600.  
  601.  void MprisPlugin::playerStatusChanged(Qomp::State state)
  602.  {
  603. -   if(!enabled_ || !mpris_ || !player_)
  604. +   if (!enabled_ || !mpris_ || !player_)
  605.         return;
  606.  
  607. -   switch(state) {
  608. -       case Qomp::StatePlaying:
  609. -           getMetaData(player_->currentTune());
  610. -           sendMetadata(PLAYING);
  611. -           break;
  612. -       case Qomp::StateStopped:
  613. -           sendMetadata(STOPPED);
  614. -           break;
  615. -       case Qomp::StatePaused:
  616. -           sendMetadata(PAUSED);
  617. -           break;
  618. -       default:
  619. -           break;
  620. +   switch (state) {
  621. +   case Qomp::StatePlaying:
  622. +       getMetaData(player_->currentTune());
  623. +       sendMetadata(PLAYING);
  624. +       break;
  625. +   case Qomp::StateStopped:
  626. +       sendMetadata(STOPPED);
  627. +       break;
  628. +   case Qomp::StatePaused:
  629. +       sendMetadata(PAUSED);
  630. +       break;
  631. +   default:
  632. +       break;
  633.     }
  634.  }
  635.  
  636.  void MprisPlugin::getMetaData(Tune *tune)
  637.  {
  638.     if (tune && lastTune_ != tune) {
  639. -       lastTune_ = tune;
  640. -       const int num = tune->trackNumber.isEmpty() ? 0 : tune->trackNumber.toInt();
  641. -       tune_->artist = tune->artist;
  642. -       tune_->title= tune->title;
  643. -       tune_->album = tune->album;
  644. +       lastTune_      = tune;
  645. +       const int num      = tune->trackNumber.isEmpty() ? 0 : tune->trackNumber.toInt();
  646. +       tune_->artist      = tune->artist;
  647. +       tune_->title       = tune->title;
  648. +       tune_->album       = tune->album;
  649.         tune_->trackNumber = num;
  650. -       tune_->trackLength = Qomp::durationStringToSeconds(tune->duration)*1e6; //in microseconds
  651. -       if (!tune->file.isEmpty()) {
  652. +       tune_->trackLength = Qomp::durationStringToSeconds(tune->duration) * 1e6; // in microseconds
  653. +       if (!tune->file.isEmpty())
  654.             tune_->url = (tune->file.startsWith("file://")) ? tune->file : "file://" + tune->file;
  655. -       }
  656. -       else {
  657. +       else
  658.             tune_->url = QString();
  659. -       }
  660.         tune_->cover = getAlbumArtFile(tune->cover());
  661.     }
  662.  }
  663.  
  664.  QString MprisPlugin::getAlbumArtFile(const QImage &art)
  665.  {
  666. -   if(!art.isNull()) {
  667. -       if(artFile_->exists()) {
  668. +   if (!art.isNull()) {
  669. +       if (artFile_->exists())
  670.             artFile_->remove();
  671. -       }
  672. +
  673.         const QString tmpPath = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
  674. -       if(!tmpPath.isEmpty()) {
  675. +       if (!tmpPath.isEmpty()) {
  676.             QImage scaledArt = art;
  677. -           if((scaledArt.size().width() > maxArtSize.width())
  678. -              || (scaledArt.size().height() > maxArtSize.height())) {
  679. +           if ((scaledArt.size().width() > maxArtSize.width())
  680. +               || (scaledArt.size().height() > maxArtSize.height()))
  681.                 scaledArt = scaledArt.scaled(maxArtSize, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  682. -           }
  683. +
  684. +           qint32 index = 0;
  685.  #if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
  686. -           artFile_->setFileName(tmpPath + "/qomp_" + QString::number(qrand()) + "_cover.png");
  687. +           index = qrand();
  688.  #else
  689. -           artFile_->setFileName(tmpPath + "/qomp_" + QString::number(QRandomGenerator::global()->generate()) + "_cover.png");
  690. +           index = QRandomGenerator::global()->generate();
  691.  #endif
  692. +           artFile_->setFileName(tmpPath + "/qomp_" + QString::number(index) + "_cover.png");
  693.             if (artFile_->open()) {
  694.                 if (!scaledArt.save(artFile_, "PNG")) {
  695.                     artFile_->close();
  696. @@ -274,7 +262,7 @@ QString MprisPlugin::getAlbumArtFile(const QImage &art)
  697.  
  698.  void MprisPlugin::tuneUpdated(Tune *tune)
  699.  {
  700. -   if(player_->state() == Qomp::StatePlaying) {
  701. +   if (player_->state() == Qomp::StatePlaying) {
  702.         getMetaData(tune);
  703.         sendMetadata(PLAYING);
  704.     }
  705. @@ -282,11 +270,41 @@ void MprisPlugin::tuneUpdated(Tune *tune)
  706.  
  707.  void MprisPlugin::sendMetadata(const QString &status)
  708.  {
  709. -   if (status == STOPPED || status == PAUSED) {
  710. +   if (status == STOPPED || status == PAUSED)
  711.         mpris_->sendData(status, QompMetaData());
  712. -   }
  713. -   else if (status == PLAYING){
  714. +   else if (status == PLAYING)
  715.         mpris_->sendData(status, *tune_);
  716. +}
  717. +
  718. +void MprisPlugin::optionsUpdated()
  719. +{
  720. +   auto options = Options::instance();
  721. +   bool shuffle = options->getOption(OPTION_SHUFFLE).toBool();
  722. +   bool loopAll = options->getOption(OPTION_REPEAT_ALL).toBool();
  723. +   mpris_->setLoopAndShuffle(loopAll, shuffle);
  724. +}
  725. +
  726. +void MprisPlugin::updateShuffle()
  727. +{
  728. +   if (player_) {
  729. +       QMainWindow *mainWin = Qomp::getMainWindow();
  730. +       if (mainWin) {
  731. +           QToolButton *shuffleBtn = mainWin->findChild<QToolButton *>(shuffleButtonName);
  732. +           if (shuffleBtn)
  733. +               shuffleBtn->click();
  734. +       }
  735. +   }
  736. +}
  737. +
  738. +void MprisPlugin::updateLoopAll()
  739. +{
  740. +   if (player_) {
  741. +       QMainWindow *mainWin = Qomp::getMainWindow();
  742. +       if (mainWin) {
  743. +           QToolButton *loopBtn = mainWin->findChild<QToolButton *>(loopAllButtonName);
  744. +           if (loopBtn)
  745. +               loopBtn->click();
  746. +       }
  747.     }
  748.  }
  749.  
  750. @@ -295,6 +313,6 @@ void MprisPlugin::disableMpris()
  751.     delete mpris_;
  752.     mpris_ = 0;
  753.     delete tune_;
  754. -   tune_ = 0;
  755. +   tune_     = 0;
  756.     lastTune_ = 0;
  757.  }
  758. diff --git a/plugins/mprisplugin/mprisplugin.h b/plugins/mprisplugin/mprisplugin.h
  759. index de78312..825a843 100644
  760. --- a/plugins/mprisplugin/mprisplugin.h
  761. +++ b/plugins/mprisplugin/mprisplugin.h
  762. @@ -1,5 +1,5 @@
  763.  /*
  764. - * Copyright (C) 2013  Khryukin Evgeny
  765. + * Copyright (C) 2013-2022  Khryukin Evgeny
  766.   *
  767.   * This program is free software; you can redistribute it and/or
  768.   * modify it under the terms of the GNU General Public License
  769. @@ -58,6 +58,9 @@ private slots:
  770.     void updatePosition();
  771.     void setPosition(const qreal &position);
  772.     void tuneUpdated(Tune *tune);
  773. +   void optionsUpdated();
  774. +   void updateShuffle();
  775. +   void updateLoopAll();
  776.  
  777.  private:
  778.     void disableMpris();
  779. diff --git a/plugins/mprisplugin/rootadapter.cpp b/plugins/mprisplugin/rootadapter.cpp
  780. index f221839..525557e 100644
  781. --- a/plugins/mprisplugin/rootadapter.cpp
  782. +++ b/plugins/mprisplugin/rootadapter.cpp
  783. @@ -1,5 +1,5 @@
  784.  /*
  785. - * Copyright (C) 2016-2019  Khryukin Evgeny, Vitaly Tonkacheyev
  786. + * Copyright (C) 2016-2022  Khryukin Evgeny, Vitaly Tonkacheyev
  787.   *
  788.   * This program is free software; you can redistribute it and/or
  789.   * modify it under the terms of the GNU General Public License
  790. @@ -23,23 +23,18 @@
  791.  #include <QDBusConnection>
  792.  #include <QDBusMessage>
  793.  
  794. -RootAdapter::RootAdapter(MprisController *p)
  795. - :QDBusAbstractAdaptor(p),
  796. -  controller_(p)
  797. -{
  798. -}
  799. +RootAdapter::RootAdapter(MprisController *p) : QDBusAbstractAdaptor(p), controller_(p) { }
  800.  
  801.  void RootAdapter::setData()
  802.  {
  803. -   QVariantMap map({{"SupportedMimeTypes", getMimeTypes()},
  804. -            {"Identity", getIdentity()},
  805. -            {"CanQuit", canQuit()},
  806. -            {"CanRaise", canRaise()},
  807. -            {"CanSetFullscreen", canSetFullscreen()},
  808. -            {"SupportedUriSchemes", QStringList()},
  809. -            {"HasTrackList", hasTrackList()}});
  810. -   QDBusMessage msg = QDBusMessage::createSignal("/org/mpris/MediaPlayer2",
  811. -                             "org.freedesktop.DBus.Properties",
  812. +   QVariantMap  map({ { "SupportedMimeTypes", getMimeTypes() },
  813. +              { "Identity", getIdentity() },
  814. +              { "CanQuit", canQuit() },
  815. +              { "CanRaise", canRaise() },
  816. +              { "CanSetFullscreen", canSetFullscreen() },
  817. +              { "SupportedUriSchemes", QStringList() },
  818. +              { "HasTrackList", hasTrackList() } });
  819. +   QDBusMessage msg = QDBusMessage::createSignal("/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties",
  820.                               "PropertiesChanged");
  821.     msg << "org.mpris.MediaPlayer2" << map << QStringList();
  822.     QDBusConnection::sessionBus().send(msg);
  823. @@ -47,25 +42,19 @@ void RootAdapter::setData()
  824.  
  825.  void RootAdapter::Quit()
  826.  {
  827. -   if(canQuit()) {
  828. +   if (canQuit())
  829.         controller_->emitSignal(QUIT);
  830. -   }
  831.  }
  832.  
  833.  void RootAdapter::Raise()
  834.  {
  835. -   if(canRaise()) {
  836. +   if (canRaise())
  837.         controller_->emitSignal(RAISE);
  838. -   }
  839.  }
  840.  
  841.  QStringList RootAdapter::getMimeTypes() const
  842.  {
  843. -   return QStringList({"audio/aac", "audio/x-flac",
  844. -                "audio/flac", "audio/mp3",
  845. -                "audio/mpeg", "application/ogg",
  846. -                "audio/x-vorbis+ogg", "audio/x-ms-wma",
  847. -                "audio/mp4", "audio/MP4A-LATM",
  848. -                "audio/mpeg4-generic", "audio/m4a",
  849. -                "audio/ac3"});
  850. +   return QStringList({ "audio/aac", "audio/x-flac", "audio/flac", "audio/mp3", "audio/mpeg", "application/ogg",
  851. +                "audio/x-vorbis+ogg", "audio/x-ms-wma", "audio/mp4", "audio/MP4A-LATM",
  852. +                "audio/mpeg4-generic", "audio/m4a", "audio/ac3" });
  853.  }
  854. diff --git a/plugins/mprisplugin/rootadapter.h b/plugins/mprisplugin/rootadapter.h
  855. index f995066..fdbf9e4 100644
  856. --- a/plugins/mprisplugin/rootadapter.h
  857. +++ b/plugins/mprisplugin/rootadapter.h
  858. @@ -1,5 +1,5 @@
  859.  /*
  860. - * Copyright (C) 2016  Khryukin Evgeny, Vitaly Tonkacheyev
  861. + * Copyright (C) 2016-2022  Khryukin Evgeny, Vitaly Tonkacheyev
  862.   *
  863.   * This program is free software; you can redistribute it and/or
  864.   * modify it under the terms of the GNU General Public License
  865. @@ -43,7 +43,7 @@ public:
  866.  
  867.  private:
  868.     QStringList getMimeTypes() const;
  869. -    QStringList getUriSchemes() const {return QStringList();}
  870. +   QStringList getUriSchemes() const {return QStringList();}
  871.     QString getIdentity() const {return "Qomp";}
  872.     QString getDesktopEntry() const {return "qomp";}
  873.     bool canQuit() const {return true;}
  874. diff --git a/src/qompmainwin.cpp b/src/qompmainwin.cpp
  875. index b8fb878..4ae6ef0 100644
  876. --- a/src/qompmainwin.cpp
  877. +++ b/src/qompmainwin.cpp
  878. @@ -508,6 +508,7 @@ void QompMainWin::Private::updateOptions()
  879.  {
  880.     Options::instance()->setOption(OPTION_REPEAT_ALL, ui->tb_repeatAll->isChecked());
  881.     Options::instance()->setOption(OPTION_SHUFFLE, ui->tb_shuffle->isChecked());
  882. +   emit Options::instance()->updateOptions();
  883.  }
  884.  
  885.  void QompMainWin::Private::removeSelectedIndexes()
  886.  
Add Comment
Please, Sign In to add comment