Autopausing when headphone unplugged
authorNikolay Tischenko <niktischenko@gmail.com>
Sun, 28 Nov 2010 12:27:29 +0000 (18:27 +0600)
committerNikolay Tischenko <niktischenko@gmail.com>
Sun, 28 Nov 2010 12:27:29 +0000 (18:27 +0600)
src/dbusadaptor.cpp
src/dbusadaptor.h
src/player/player.cpp
src/player/player.h

index 945ced5..3ca01fa 100644 (file)
@@ -108,8 +108,27 @@ void DBusAdaptop::processBTSignal(QString event, QString state) {
                                prev();
                        } else if (state == "play-cd" || state == "pause-cd") {
                                toggle();
+                       } else if (state == "connection") {
+                               bool present = QDBusInterface ("org.freedesktop.Hal",
+                                                              "/org/freedesktop/Hal/devices/platform_headphone",
+                                                              "org.freedesktop.Hal.Device",
+                                                              QDBusConnection::systemBus()).call ("GetProperty", "button.state.value").arguments().at(0).toBool();
+                               if (!present) {
+                                       pause();
+                               } else {
+                                       QTimer::singleShot(1000, this, SLOT(playIfPaused()));
+                               }
+
                        }
                }
        }
        _time = t;
 }
+
+void DBusAdaptop::pause() {
+       QMetaObject::invokeMethod(parent(), "pause");
+}
+
+void DBusAdaptop::playIfPaused() {
+       QMetaObject::invokeMethod(parent(), "playIfPaused");
+}
index 9c82b67..a0fb6cc 100644 (file)
@@ -78,6 +78,8 @@ public Q_SLOTS: // METHODS
     Q_NOREPLY void stop();
     QString title();
     Q_NOREPLY void toggle();
+    Q_NOREPLY void pause();
+    Q_NOREPLY void playIfPaused();
 
     void processBTSignal(QString, QString);
 Q_SIGNALS: // SIGNALS
index 758b09e..e0d3632 100644 (file)
@@ -334,3 +334,17 @@ Track Player::current() {
                return Track();
        }
 }
+
+void Player::pause() {
+       if (_state == PLAYER_PLAYING) {
+               _player->pause();
+               _state = PLAYER_PAUSED;
+               emit stateChanged(_state);
+       }
+}
+
+void Player::playIfPaused() {
+       if (_state == PLAYER_PAUSED) {
+               play();
+       }
+}
index 2ecc032..71d0b27 100644 (file)
@@ -82,6 +82,8 @@ namespace SomePlayer {
                        void enqueue(int id);
                        void toggle();
                        void play();
+                       void pause();
+                       void playIfPaused();
                        void stop();
                        void next();
                        void prev();