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