Version bump
[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         _is_bt_conencted = false;
51 }
52
53 DBusAdaptop::~DBusAdaptop()
54 {
55         // destructor
56 }
57
58 QString DBusAdaptop::album() {
59         // handle method call ru.somebody.someplayer.album
60         QString out0;
61         QMetaObject::invokeMethod(parent(), "album", Q_RETURN_ARG(QString, out0));
62         return out0;
63 }
64
65 QString DBusAdaptop::artist() {
66         // handle method call ru.somebody.someplayer.artist
67         QString out0;
68         QMetaObject::invokeMethod(parent(), "artist", Q_RETURN_ARG(QString, out0));
69         return out0;
70 }
71
72 QString DBusAdaptop::title_artist_album() {
73         // handle method call ru.somebody.someplayer.title_artist_album
74         QString album, title, artist;
75         QMetaObject::invokeMethod(parent(), "artist", Q_RETURN_ARG(QString, artist));
76         QMetaObject::invokeMethod(parent(), "title", Q_RETURN_ARG(QString, title));
77         QMetaObject::invokeMethod(parent(), "album", Q_RETURN_ARG(QString, album));
78         return QString("%1\n%2\n%3").arg(title).arg(artist).arg(album);
79 }
80
81 void DBusAdaptop::next() {
82         // handle method call ru.somebody.someplayer.next
83         QMetaObject::invokeMethod(parent(), "next");
84 }
85
86 void DBusAdaptop::prev() {
87         // handle method call ru.somebody.someplayer.prev
88         QMetaObject::invokeMethod(parent(), "prev");
89 }
90
91 void DBusAdaptop::stop() {
92         // handle method call ru.somebody.someplayer.stop
93         QMetaObject::invokeMethod(parent(), "stop");
94 }
95
96 QString DBusAdaptop::title() {
97         // handle method call ru.somebody.someplayer.title
98         QString out0;
99         QMetaObject::invokeMethod(parent(), "title", Q_RETURN_ARG(QString, out0));
100         return out0;
101 }
102
103 void DBusAdaptop::toggle() {
104         // handle method call ru.somebody.someplayer.toggle
105         QMetaObject::invokeMethod(parent(), "toggle");
106 }
107
108 QString DBusAdaptop::state() {
109         // handle method call ru.somebody.someplayer.state
110         QString out0;
111         QMetaObject::invokeMethod(parent(), "stateText", Q_RETURN_ARG(QString, out0));
112         return out0;
113 }
114
115 QString DBusAdaptop::albumart() {
116         // handle method call ru.somebody.someplayer.albumart
117         QString out0;
118         QMetaObject::invokeMethod(parent(), "albumart", Q_RETURN_ARG(QString, out0));
119         return out0;
120 }
121
122 void DBusAdaptop::processBTSignal(QString event, QString state) {
123         QTime t = QTime::currentTime();
124         long msec = _time.msecsTo(t);
125         if (msec > _DBUS_ACTION_TIMEOUT_) {
126                 if (event == "ButtonPressed") {
127                         if (state == "next-song") {
128                                 next();
129                         } else if (state == "previous-song") {
130                                 prev();
131                         } else if (state == "play-cd" || state == "pause-cd") {
132                                 toggle();
133                         } else if (state == "connection") {
134                                 SomePlayer::Storage::Config config;
135                                 if (config.getValue("hw/hpautopause").toString() != "yes") {
136                                         return;
137                                 }
138                                 bool present = QDBusInterface ("org.freedesktop.Hal",
139                                                                "/org/freedesktop/Hal/devices/platform_headphone",
140                                                                "org.freedesktop.Hal.Device",
141                                                                QDBusConnection::systemBus()).call ("GetProperty", "button.state.value").arguments().at(0).toBool();
142                                 if (!present) {
143                                         pause();
144                                 } else {
145                                         QTimer::singleShot(1000, this, SLOT(playIfPaused()));
146                                 }
147
148                         }
149                 }
150         }
151         _time = t;
152 }
153
154 void DBusAdaptop::pause() {
155         QMetaObject::invokeMethod(parent(), "pause");
156 }
157
158 void DBusAdaptop::playIfPaused() {
159         QMetaObject::invokeMethod(parent(), "playIfPaused");
160 }
161
162 void DBusAdaptop::processBTConnect(QString stateName, QDBusVariant state) {
163         SomePlayer::Storage::Config config;
164         if (config.getValue("hw/hpautopause").toString() != "yes") {
165                 return;
166         }
167         if (stateName == "State") {
168                 if (state.variant().toString() == "disconnected") {
169                         if (!_is_bt_conencted) {
170                                 return;
171                         }
172                         _is_bt_conencted = false;
173                         pause();
174                 } else if (state.variant().toString() == "connected") {
175                         if (_is_bt_conencted) {
176                                 return;
177                         }
178                         _is_bt_conencted = true;
179                         QTimer::singleShot(1000, this, SLOT(playIfPaused()));
180                 }
181         }
182 }