Parent: [7a05a7] (diff)

Child: [fd0f5f] (diff)

Download this file

ohradio_qo.h    187 lines (159 with data), 5.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright (C) 2014 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _OHRADIO_QO_INCLUDED
#define _OHRADIO_QO_INCLUDED
#include <string>
#include <QObject>
#include <QThread>
#include <QDebug>
#include <QTimer>
#include "libupnpp/control/ohradio.hxx"
#include "ohpool.h"
class OHRadioQO : public QObject, public UPnPClient::VarEventReporter {
Q_OBJECT
public:
OHRadioQO(UPnPClient::OHRDH ohp, QObject *parent = 0)
: QObject(parent), m_curid(-1), m_srv(ohp), m_timer(0), m_errcnt(0) {
m_timer = new QTimer(this);
connect(m_timer, SIGNAL(timeout()), this, SLOT(testconn()));
m_timer->start(2000);
m_srv->installReporter(this);
}
virtual ~OHRadioQO() {
m_srv->installReporter(0);
}
// Events: we are interested in:
// Id (current playing), TransportState, Shuffle, Repeat.
// IdArray (the current playlist. We use readList() to
// translate this into actual track information)
virtual void changed(const char *nm, int value) {
//qDebug() << "OHRadioQO: changed: " << nm << " (int): " << value;
if (!strcmp(nm, "Id")) {
emit currentTrackId(value);
} else if (!strcmp(nm, "TransportState")) {
emit tpStateChanged(value);
}
}
virtual void changed(const char *nm, std::vector<int> ids) {
Q_UNUSED(nm);
//qDebug() << "OHRadioQO: changed: " << nm << " (vector<int>)";
emit __idArrayChanged(ids);
}
virtual void changed(const char * nm, const char *value) {
Q_UNUSED(nm);
Q_UNUSED(value);
//qDebug() << "OHRadioQO: changed: " << nm << " (char*): " << value;
}
public slots:
// Ping renderer to check it's still there.
virtual void testconn() {
int val;
if (m_srv->id(&val) != 0) {
if (m_errcnt++ > 2) {
emit connectionLost();
}
return;
}
m_errcnt = 0;
}
// Read state from the remote. Used when starting up, to avoid
// having to wait for events.
virtual void fetchState() {
//qDebug() << "OHRadioQO::fetchState()";
std::vector<int> ids;
int tok;
if (idArray(&ids, &tok))
onIdArrayChanged(ids);
if (m_srv->id(&tok) == 0) {
m_curid = tok;
emit currentTrackId(m_curid);
}
UPnPClient::OHPlaylist::TPState tpst;
if (m_srv->transportState(&tpst) == 0)
emit tpStateChanged(tpst);
}
virtual bool play() {
//qDebug() << "OHRadioQO::play()";
return m_srv->play() == 0;
}
virtual bool stop() {
//qDebug() << "OHRadioQO::stop()";
return m_srv->stop() == 0;
}
virtual bool pause() {
//qDebug() << "OHRadioQO::pause()";
m_srv->pause();
// Pause generally silently fails to accomplish
// anything. Fetch and signal the actual device state so that
// the playctl buttons reflect it
UPnPClient::OHPlaylist::TPState tpst;
if (m_srv->transportState(&tpst) == 0) {
//qDebug() << "OHPL::pause(): tpstate: " << tpst;
emit tpStateChanged(tpst);
}
return true;
}
virtual bool setIdx(int idx) {
//qDebug() << "OHRadioQO::setIdx: idx: " << idx;
if (idx < 0 || idx >= int(m_idsv.size()))
return false;
int id = m_idsv[idx];
//qDebug() << "OHRadioQO::setIdx: id: " << id;
STD_UNORDERED_MAP<int, UPnPClient::UPnPDirObject>::iterator it;
if ((it = m_metapool.find(id)) == m_metapool.end()) {
qDebug() << "OHRadioQO::setId: id not found";
return false;
}
const std::string& uri = it->second.m_resources[0].m_uri;
int ret = m_srv->setId(id, uri);
if (ret != 0) {
qDebug() << "OHRadioQO: setId failed: " << ret;
return false;
}
m_curid = id;
return true;
}
signals:
void currentTrackId(int);
void trackArrayChanged();
void tpStateChanged(int);
void connectionLost();
// This is an internal signal. Use trackArrayChanged()
void __idArrayChanged(std::vector<int>);
private slots:
void onIdArrayChanged(std::vector<int> nids) {
m_idsv = nids;
if (!ohupdmetapool(nids, m_curid, m_metapool, m_srv))
return;
//qDebug() << "OHRad::onIdArrayChanged: emit trackArrayChanged(). " <<
// "idsv size" << m_idsv.size() << " pool size " << m_metapool.size();
emit trackArrayChanged();
emit currentTrackId(m_curid);
}
protected:
std::vector<int> m_idsv;
STD_UNORDERED_MAP<int, UPnPClient::UPnPDirObject> m_metapool;
int m_curid;
private:
virtual bool idArray(std::vector<int> *ids, int *tokp) {
return m_srv->idArray(ids, tokp) == 0;
}
UPnPClient::OHRDH m_srv;
QTimer *m_timer;
int m_errcnt;
};
#endif // _OHRADIO_QO_INCLUDED