Parent: [56f9b2] (diff)

Child: [2305a0] (diff)

Download this file

application.cpp    287 lines (232 with data), 8.4 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* application.cpp */
/* Copyright (C) 2013 Lucio Carreras
*
* This file is part of sayonara player
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <fstream>
#include <string>
#include <functional>
using namespace std;
#include <QApplication>
#include <QMap>
#include <QSharedMemory>
#include <QMessageBox>
#include "application.h"
#include "GUI/player/GUI_Player.h"
#include "GUI/playlist/GUI_Playlist.h"
#include "GUI/renderchoose/renderchoose.h"
#include "playlist/Playlist.h"
#include "HelperStructs/Helper.h"
#include "HelperStructs/CSettingsStorage.h"
#include "HelperStructs/Style.h"
#include "HelperStructs/globals.h"
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/control/mediarenderer.hxx"
#include "libupnpp/control/renderingcontrol.hxx"
#include "libupnpp/control/discovery.hxx"
using namespace UPnPClient;
UPnPDeviceDirectory *superdir;
static MRDH getRenderer(const string& name, bool isfriendlyname)
{
if (superdir == 0) {
superdir = UPnPDeviceDirectory::getTheDir();
if (superdir == 0) {
cerr << "Can't create UPnP discovery object" << endl;
exit(1);
}
}
UPnPDeviceDesc ddesc;
if (isfriendlyname) {
if (superdir->getDevByFName(name, ddesc)) {
return MRDH(new MediaRenderer(ddesc));
}
cerr << "getDevByFname failed" << endl;
} else {
if (superdir->getDevByUDN(name, ddesc)) {
return MRDH(new MediaRenderer(ddesc));
}
cerr << "getDevByFname failed" << endl;
}
return MRDH();
}
bool Application::is_initialized()
{
return _initialized;
}
bool Application::setupRenderer(const string& uid)
{
delete rdco;
delete avto;
MRDH rdr = getRenderer(uid, false);
if (!rdr) {
cerr << "Renderer " << uid << " not found" << endl;
return false;
}
AVTH avt = rdr->avt();
if (!avt) {
cerr << "Device " << uid <<
" has no AVTransport service" << endl;
return false;
}
RDCH rdc = rdr->rdc();
if (!rdc) {
cerr << "Device " << uid <<
" has no RenderingControl service" << endl;
return false;
}
rdco = new RenderingControlQO(rdc);
avto = new AVTPlayer(avt);
return true;
}
void Application::chooseRenderer()
{
vector<UPnPDeviceDesc> devices;
if (!MediaRenderer::getDeviceDescs(devices) || devices.empty()) {
QMessageBox::warning(0, "Upplay",
tr("No Media Renderers found."));
return;
}
RenderChooseDLG dlg;
for (auto it = devices.begin(); it != devices.end(); it++) {
dlg.rndsLW->addItem(QString::fromUtf8(it->friendlyName.c_str()));
}
if (!dlg.exec())
return;
int row = dlg.rndsLW->currentRow();
if (row < 0 || row >= int(devices.size())) {
cerr << "Internal error: bad row after renderer choose dlg" << endl;
return;
}
qDebug() << "Choosen: " << devices[row].friendlyName.c_str();
if (!setupRenderer(devices[row].UDN)) {
QMessageBox::warning(
0, "Upplay", tr("Can't connect to ") +
QString::fromUtf8(devices[row].friendlyName.c_str()));
return;
}
set->setPlayerUID(QString::fromUtf8(devices[row].UDN.c_str()));
renderer_connections();
}
Application::Application(QApplication* qapp, int,
QTranslator* translator, QObject *parent)
: QObject(parent)
{
app = qapp;
set = CSettingsStorage::getInstance();
QString version = getVersion();
set->setVersion(version);
player = new GUI_Player(translator);
playlist = new Playlist();
ui_playlist = new GUI_Playlist(player->getParentOfPlaylist(), 0);
cdb = new CDBrowser(player->getParentOfLibrary());
rdco = 0;
avto = 0;
string uid = qs2utf8s(set->getPlayerUID());
if (uid.empty()) {
chooseRenderer();
if (rdco == 0) {
exit(1);
}
} else {
if (!setupRenderer(uid)) {
cerr << "Can't connect to media renderer" << endl;
exit(1);
}
}
init_connections();
qDebug() << "setting up player";
player->setWindowTitle("Upplay " + version);
player->setWindowIcon(QIcon(Helper::getIconPath() + "logo.png"));
player->setPlaylist(ui_playlist);
player->setStyle( set->getPlayerStyle() );
player->show();
cdb->show();
ui_playlist->resize(player->getParentOfPlaylist()->size());
player->ui_loaded();
_initialized = true;
}
Application::~Application()
{
}
void Application::renderer_connections()
{
CONNECT(player, pause(), avto, pause());
// the search (actually seek) param is in percent
CONNECT(player, search(int), avto, seekPC(int));
CONNECT(avto, secsInSongChanged(quint32),
player, setCurrentPosition(quint32));
CONNECT(player, sig_volume_changed(int), rdco, setVolume(int));
CONNECT(rdco, volumeChanged(int), player, setVolumeUi(int));
CONNECT(playlist, sig_selected_file_changed_md(const MetaData&, int, bool),
avto, changeTrack(const MetaData&, int, bool));
CONNECT(playlist, sig_no_track_to_play(), avto, stop());
CONNECT(playlist, sig_goon_playing(), avto, play());
}
void Application::init_connections()
{
if (avto && rdco)
renderer_connections();
CONNECT(player, play(), playlist, psl_play());
CONNECT(player, pause(), playlist, psl_pause());
CONNECT(player, stop(), playlist, psl_stop());
CONNECT(player, forward(), playlist, psl_forward());
CONNECT(player, backward(), playlist, psl_backward());
CONNECT(player, show_small_playlist_items(bool),
ui_playlist, psl_show_small_playlist_items(bool));
CONNECT(player, sig_choose_renderer(), this, chooseRenderer());
CONNECT(playlist, sig_selected_file_changed_md(const MetaData&, int, bool),
player, update_track(const MetaData&, int, bool));
CONNECT(playlist, sig_no_track_to_play(), player, stopped());
CONNECT(playlist, sig_selected_file_changed(int),
ui_playlist, track_changed(int));
CONNECT(playlist, sig_playlist_created(MetaDataList&, int, int),
ui_playlist, fillPlaylist(MetaDataList&, int, int));
CONNECT(ui_playlist, selected_row_changed(int),
playlist, psl_change_track(int));
CONNECT(ui_playlist, clear_playlist(), playlist, psl_clear_playlist());
CONNECT(ui_playlist, playlist_mode_changed(const Playlist_Mode&),
playlist, psl_playlist_mode_changed(const Playlist_Mode&));
CONNECT(ui_playlist, dropped_tracks(const MetaDataList&, int),
playlist, psl_insert_tracks(const MetaDataList&, int));
CONNECT(ui_playlist, sig_rows_removed(const QList<int>&, bool),
playlist, psl_remove_rows(const QList<int>&, bool));
CONNECT(cdb, sig_tracks_for_playlist_available(MetaDataList&),
playlist, psl_append_tracks(MetaDataList&));
}
QString Application::getVersion()
{
ifstream istr;
QString version_file = Helper::getSharePath() + "/VERSION";
istr.open(version_file.toUtf8() );
if(!istr || !istr.is_open() ) return "0.3.1";
QMap<QString, int> map;
while(istr.good()){
string type;
int version;
istr >> type >> version;
if(type.size() > 0)
qDebug() << type.c_str() << ": " << version;
map[QString(type.c_str())] = version;
}
istr.close();
QString version_str = QString::number(map["MAJOR"]) + "." +
QString::number(map["MINOR"]) + "." +
QString::number(map["SUBMINOR"]) + " r" + QString::number(map["BUILD"]);
return version_str;
}
QMainWindow* Application::getMainWindow()
{
return this->player;
}