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