[general] "poor programming style" fix
[libxmms2client-qt.git] / playback.cpp
blobc28b174e3de745cc23fa8cb01d5b53ad80c23aea
1 //
2 // C++ Implementation: playback
3 //
4 // Author: Oliver Groß <z.o.gross@gmx.de>, (C) 2008
5 //
6 // Copyright: See COPYING file that comes with this distribution
7 //
8 #include "common.h"
9 #include "playback.h"
10 #include "client.h"
11 #include <QDebug>
13 namespace XmmsQt {
14 QPlayback::QPlayback(QClient * parent) : QObject(parent), m_Connection(0) {
15 connect(parent, SIGNAL(connectionChanged(xmmsc_connection_t *)),
16 this, SLOT(setConnection(xmmsc_connection_t *)));
17 if (parent->isConnectedToServer())
18 setConnection(parent->connection());
19 else
20 m_Connection = NULL;
23 QPlayback::~QPlayback() {
26 void QPlayback::setVolumeChannel(const void * key, xmmsc_result_value_type_t, const void * value, void * instance) {
27 if (instance) {
28 QPlayback * playback = static_cast<QPlayback *>(instance);
29 QString hashKey((const char *)key);
30 quint32 volume = XPOINTER_TO_INT(value);
31 if (playback->m_Volume.contains(hashKey))
32 playback->m_Volume[hashKey] = volume;
33 else
34 playback->m_Volume.insert(hashKey, volume);
38 //callback for the "volume changed"-broadcast
39 void QPlayback::volumeCallback(xmmsc_result_t * result, void * instance) {
40 if (xmmsc_result_iserror(result)) {
41 qDebug() << "Error: (QPlayback::volumeCallback)" << xmmsc_result_get_error(result);
42 return;
45 if (instance) {
46 QPlayback * playback = static_cast<QPlayback *>(instance);
47 xmmsc_result_dict_foreach(result, setVolumeChannel, instance);
48 emit playback->volumeChanged();
50 //xmmsc_result_unref(result);
53 //callback for the "status changed"-broadcast
54 void QPlayback::statusCallback(xmmsc_result_t * result, void * instance) {
55 if (xmmsc_result_iserror(result)) {
56 qDebug() << "Error: (QPlayback::statusCallback)" << xmmsc_result_get_error(result);
57 return;
60 if (instance) {
61 QPlayback * playback = static_cast<QPlayback *>(instance);
62 quint32 buffer;
63 xmmsc_result_get_uint(result, &buffer);
64 playback->m_Status = (xmms_playback_status_t)buffer;
65 emit playback->statusChanged(playback->m_Status);
67 //xmmsc_result_unref(result);
70 //callback for the "id changed"-broadcast
71 void QPlayback::idCallback(xmmsc_result_t * result, void * instance) {
72 if (xmmsc_result_iserror(result)) {
73 qDebug() << "Error: (QPlayback::idCallback)" << xmmsc_result_get_error(result);
74 return;
77 if (instance) {
78 QPlayback * playback = static_cast<QPlayback *>(instance);
79 xmmsc_result_get_uint(result, &playback->m_CurrentID);
80 emit playback->idChanged(playback->m_CurrentID);
82 //xmmsc_result_unref(result);
85 //callback for the "play time changed"-signal
86 void QPlayback::playTimeCallback(xmmsc_result_t * result, void * instance) {
87 if (xmmsc_result_iserror(result)) {
88 qDebug() << "Error: (QPlayback::playTimeCallback)" << xmmsc_result_get_error(result);
89 return;
92 if (instance) {
93 QPlayback * playback = static_cast<QPlayback *>(instance);
94 quint32 time;
95 xmmsc_result_get_uint(result, &time);
96 xmmsc_result_unref(xmmsc_result_restart(result));
97 emit playback->playTimeChanged(time);
99 xmmsc_result_unref(result);
102 void QPlayback::reset() {
103 if (m_Connection) {
104 connectNotifierUnref(xmmsc_playback_volume_get(m_Connection), volumeCallback, this);
105 connectNotifierUnref(xmmsc_playback_current_id(m_Connection), idCallback, this);
106 connectNotifierUnref(xmmsc_playback_status(m_Connection), statusCallback, this);
110 void QPlayback::setConnection(xmmsc_connection_t * connection) {
111 if (m_Connection == connection)
112 return;
114 m_Volume.clear();
116 //disconnect
117 if (m_Connection) {
118 disconnectNotifierUnref(xmmsc_broadcast_playback_volume_changed(m_Connection));
119 disconnectNotifierUnref(xmmsc_broadcast_playback_current_id(m_Connection));
120 disconnectNotifierUnref(xmmsc_broadcast_playback_status(m_Connection));
121 disconnectNotifierUnref(xmmsc_signal_playback_playtime(m_Connection));
124 m_Connection = connection;
125 m_CurrentID = 0;
126 m_Status = XMMS_PLAYBACK_STATUS_STOP;
128 //connect
129 if (m_Connection) {
130 connectNotifierUnref(xmmsc_broadcast_playback_volume_changed(m_Connection), volumeCallback, this);
131 connectNotifierUnref(xmmsc_broadcast_playback_current_id(m_Connection), idCallback, this);
132 connectNotifierUnref(xmmsc_broadcast_playback_status(m_Connection), statusCallback, this);
133 connectNotifierUnref(xmmsc_signal_playback_playtime(m_Connection), playTimeCallback, this);
135 connectNotifierUnref(xmmsc_playback_volume_get(m_Connection), volumeCallback, this);
136 connectNotifierUnref(xmmsc_playback_current_id(m_Connection), idCallback, this);
137 connectNotifierUnref(xmmsc_playback_status(m_Connection), statusCallback, this);
139 else {
140 emit idChanged(0);
141 emit volumeChanged();
142 emit statusChanged(XMMS_PLAYBACK_STATUS_STOP);
146 void QPlayback::start() {
147 xmmsc_result_unref(xmmsc_playback_start(m_Connection));
150 void QPlayback::pause() {
151 xmmsc_result_unref(xmmsc_playback_pause(m_Connection));
154 void QPlayback::stop() {
155 xmmsc_result_unref(xmmsc_playback_stop(m_Connection));
158 void QPlayback::tickle() {
159 xmmsc_result_unref(xmmsc_playback_tickle(m_Connection));
162 void QPlayback::setVolume(int volume, QString channel) {
163 if (channel.isEmpty()) {
164 foreach(QString i, m_Volume.keys())
165 xmmsc_result_unref(xmmsc_playback_volume_set(m_Connection, i.toAscii().data(), volume));
167 else
168 xmmsc_result_unref(xmmsc_playback_volume_set(m_Connection, channel.toAscii().data(), volume));
171 void QPlayback::setNext(quint32 position) {
172 xmmsc_result_unref(xmmsc_playlist_set_next(m_Connection, position));
175 void QPlayback::setPlayTime(int time) {
176 xmmsc_result_unref(xmmsc_playback_seek_ms(m_Connection, time));