a6264f0be594caebbb4e646192bf4ae8d964935e
[someplayer] / src / dbusadaptor.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "dbusadaptor.h"
21 #include <QtCore/QMetaObject>
22 #include <QtCore/QByteArray>
23 #include <QtCore/QList>
24 #include <QtCore/QMap>
25 #include <QtCore/QString>
26 #include <QtCore/QStringList>
27 #include <QtCore/QVariant>
28 #include <QDebug>
29 #include "config.h"
30
31 /*
32  * Implementation of adaptor class DBusAdaptop
33  */
34
35 DBusAdaptop::DBusAdaptop(QObject *parent)
36         : QDBusAbstractAdaptor(parent)
37 {
38         // constructor
39         setAutoRelaySignals(true);
40         // handling signals from bluetooth headset
41         _time = QTime::currentTime();
42         if (!QDBusConnection::systemBus().connect(QString(), QString(),
43                                                   "org.freedesktop.Hal.Device", "Condition", this, SLOT(processBTSignal(QString, QString)))) {
44                 qWarning() << "Can not connect to HAL";
45         }
46         if (!QDBusConnection::systemBus().connect(QString(), QString(), "org.bluez.AudioSink", "PropertyChanged", this, SLOT(processBTConnect(QString, QDBusVariant)))) {
47                 qWarning() << "Can not connect to HAL 2";
48         }
49         setAutoRelaySignals(true);
50 }
51
52 DBusAdaptop::~DBusAdaptop()
53 {
54         // destructor
55 }
56
57 QString DBusAdaptop::album() {
58         // handle method call ru.somebody.someplayer.album
59         QString out0;
60         QMetaObject::invokeMethod(parent(), "album", Q_RETURN_ARG(QString, out0));
61         return out0;
62 }
63
64 QString DBusAdaptop::artist() {
65         // handle method call ru.somebody.someplayer.artist
66         QString out0;
67         QMetaObject::invokeMethod(parent(), "artist", Q_RETURN_ARG(QString, out0));
68         return out0;
69 }
70
71 QString DBusAdaptop::title_artist_album() {
72         // handle method call ru.somebody.someplayer.title_artist_album
73         QString album, title, artist;
74         QMetaObject::invokeMethod(parent(), "artist", Q_RETURN_ARG(QString, artist));
75         QMetaObject::invokeMethod(parent(), "title", Q_RETURN_ARG(QString, title));
76         QMetaObject::invokeMethod(parent(), "album", Q_RETURN_ARG(QString, album));
77         return QString("%1\n%2\n%3").arg(title).arg(artist).arg(album);
78 }
79
80 void DBusAdaptop::next() {
81         // handle method call ru.somebody.someplayer.next
82         QMetaObject::invokeMethod(parent(), "next");
83 }
84
85 void DBusAdaptop::prev() {
86         // handle method call ru.somebody.someplayer.prev
87         QMetaObject::invokeMethod(parent(), "prev");
88 }
89
90 void DBusAdaptop::stop() {
91         // handle method call ru.somebody.someplayer.stop
92         QMetaObject::invokeMethod(parent(), "stop");
93 }
94
95 QString DBusAdaptop::title() {
96         // handle method call ru.somebody.someplayer.title
97         QString out0;
98         QMetaObject::invokeMethod(parent(), "title", Q_RETURN_ARG(QString, out0));
99         return out0;
100 }
101
102 void DBusAdaptop::toggle() {
103         // handle method call ru.somebody.someplayer.toggle
104         QMetaObject::invokeMethod(parent(), "toggle");
105 }
106
107 QString DBusAdaptop::state() {
108         // handle method call ru.somebody.someplayer.state
109         QString out0;
110         QMetaObject::invokeMethod(parent(), "stateText", Q_RETURN_ARG(QString, out0));
111         return out0;
112 }
113
114 QString DBusAdaptop::albumart() {
115         // handle method call ru.somebody.someplayer.albumart
116         QString out0;
117         QMetaObject::invokeMethod(parent(), "albumart", Q_RETURN_ARG(QString, out0));
118         return out0;
119 }
120
121 void DBusAdaptop::processBTSignal(QString event, QString state) {
122         QTime t = QTime::currentTime();
123         long msec = _time.msecsTo(t);
124         if (msec > _DBUS_ACTION_TIMEOUT_) {
125                 if (event == "ButtonPressed") {
126                         if (state == "next-song") {
127                                 next();
128                         } else if (state == "previous-song") {
129                                 prev();
130                         } else if (state == "play-cd" || state == "pause-cd") {
131                                 toggle();
132                         } else if (state == "connection") {
133                                 SomePlayer::Storage::Config config;
134                                 if (config.getValue("hw/hpautopause").toString() != "yes") {
135                                         return;
136                                 }
137                                 bool present = QDBusInterface ("org.freedesktop.Hal",
138                                                                "/org/freedesktop/Hal/devices/platform_headphone",
139                                                                "org.freedesktop.Hal.Device",
140                                                                QDBusConnection::systemBus()).call ("GetProperty", "button.state.value").arguments().at(0).toBool();
141                                 if (!present) {
142                                         pause();
143                                 } else {
144                                         QTimer::singleShot(1000, this, SLOT(playIfPaused()));
145                                 }
146
147                         }
148                 }
149         }
150         _time = t;
151 }
152
153 void DBusAdaptop::pause() {
154         QMetaObject::invokeMethod(parent(), "pause");
155 }
156
157 void DBusAdaptop::playIfPaused() {
158         QMetaObject::invokeMethod(parent(), "playIfPaused");
159 }
160
161 void DBusAdaptop::processBTConnect(QString stateName, QDBusVariant state) {
162         SomePlayer::Storage::Config config;
163         if (config.getValue("hw/hpautopause").toString() != "yes") {
164                 return;
165         }
166         if (stateName == "State") {
167                 if (state.variant().toString() == "disconnected") {
168                         pause();
169                 } else if (state.variant().toString() == "connected") {
170                         QTimer::singleShot(1000, this, SLOT(playIfPaused()));
171                 }
172         }
173 }