Parent: [804659] (diff)

Child: [9c9a53] (diff)

Download this file

songcast.cpp    146 lines (126 with data), 4.6 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
/* Copyright (C) 2015 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.
*/
#include "songcast.h"
#include <iostream>
#include <vector>
#include <QTimer>
#include <QDebug>
#include "libupnpp/control/linnsongcast.hxx"
#include "GUI/songcast/songcastdlg.h"
#include "upadapt/upputils.h"
using namespace std;
using namespace UPnPClient;
using namespace Songcast;
class SongcastTool::Internal {
public:
SongcastDLG *dlg;
vector<ReceiverState> receivers;
vector<SenderState> senders;
};
SongcastTool::SongcastTool(SongcastDLG *dlg, QObject *parent)
: QObject(parent)
{
m = new Internal;
m->dlg = dlg;
connect(m->dlg, SIGNAL(sig_apply(SongcastDLG*)),
this, SLOT(onSongcastApply()));
connect(m->dlg, SIGNAL(accepted()), this, SLOT(onSongcastApply()));
initControls();
}
void SongcastTool::initControls()
{
listSenders(m->senders);
listReceivers(m->receivers);
m->dlg->createControls(m->senders.size(), m->receivers.size());
for (unsigned int i = 0; i < m->senders.size(); i++) {
connect(m->dlg->senderButton(i), SIGNAL(toggled(bool)),
this, SLOT(enableOnButtons()));
}
syncReceivers();
}
SongcastDLG *SongcastTool::dlg()
{
return m->dlg;
}
string SongcastTool::senderNameFromUri(const string& uri)
{
for (unsigned int i = 0; i < m->senders.size(); i++) {
if (uri == m->senders[i].uri)
return m->senders[i].nm;
}
return string();
}
void SongcastTool::onSongcastApply()
{
int senderidx = m->dlg->selectedSenderIdx();
for (unsigned int i = 0; i < m->receivers.size(); i++) {
if (m->dlg->receiverOffRequested(i)) {
stopReceiver(m->receivers[i]);
} else if (senderidx != -1 && m->dlg->receiverOnRequested(i)) {
setReceiverPlaying(m->receivers[i],
m->senders[senderidx].uri,
m->senders[senderidx].meta);
}
}
QTimer::singleShot(500, this, SLOT(syncReceivers()));
}
// This is called when a sender is selected, to enable appropriate on buttons
void SongcastTool::enableOnButtons()
{
int senderidx = m->dlg->selectedSenderIdx();
for (unsigned int i = 0; i < m->receivers.size(); i++) {
ReceiverState::SCState st(m->receivers[i].state);
bool isplaying = st == ReceiverState::SCRS_PLAYING;
m->dlg->receiverOnButton(i)->setEnabled(!isplaying && senderidx >= 0);
}
}
QString SongcastTool::receiverText(int i, bool isplaying)
{
QString rcvdesc("<b>");
rcvdesc += u8s2qs(m->receivers[i].nm + "</b>");
string snm = senderNameFromUri(m->receivers[i].uri);
if (!snm.empty()) {
if (isplaying) {
rcvdesc += tr(" (playing from: ");
} else {
rcvdesc += tr(" (connected to: ");
}
rcvdesc += u8s2qs(snm) + ")";
}
return rcvdesc;
}
// Synchronize ui state with actual receiver state. This will not list
// new devices or delete old ones. Also set the senders names (this is
// used right after creating the grids)
void SongcastTool::syncReceivers()
{
int senderidx = m->dlg->selectedSenderIdx();
for (unsigned int i = 0; i < m->senders.size(); i++) {
m->dlg->senderLabel(i)->setText(u8s2qs(m->senders[i].nm));
}
for (unsigned int i = 0; i < m->receivers.size(); i++) {
string udn = m->receivers[i].UDN;
getReceiverState(udn, m->receivers[i], false);
ReceiverState::SCState st(m->receivers[i].state);
bool isplaying = st == ReceiverState::SCRS_PLAYING;
m->dlg->receiverOffButton(i)->setChecked(false);
m->dlg->receiverOnButton(i)->setChecked(false);
m->dlg->receiverOffButton(i)->setEnabled(isplaying);
m->dlg->receiverOnButton(i)->setEnabled(!isplaying && senderidx >= 0);
m->dlg->receiverLabel(i)->setText(receiverText(i, isplaying));
}
}