TTKMusicPlayer  4.3.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
musicsongdlnatransferwidget.cpp
Go to the documentation of this file.
2 #include "ui_musicsongdlnatransferwidget.h"
4 #include "musictoastlabel.h"
5 
6 #include "qdlna/qdlnafinder.h"
7 #include "qdlna/qdlnaclient.h"
9 
11  : MusicAbstractMoveWidget(parent),
13  m_state(TTK::PlayState::Stopped),
14  m_currentPlayIndex(0)
15 {
16  m_ui->setupUi(this);
17  setFixedSize(size());
18  setAttribute(Qt::WA_DeleteOnClose);
19  setBackgroundLabel(m_ui->background);
20 
21  m_ui->topTitleCloseButton->setIcon(QIcon(":/functions/btn_close_hover"));
22  m_ui->topTitleCloseButton->setStyleSheet(TTK::UI::ToolButtonStyle04);
23  m_ui->topTitleCloseButton->setCursor(QCursor(Qt::PointingHandCursor));
24  m_ui->topTitleCloseButton->setToolTip(tr("Close"));
25  connect(m_ui->topTitleCloseButton, SIGNAL(clicked()), SLOT(close()));
26 
27  m_ui->iconLabel->setPixmap(QPixmap(":/contextMenu/btn_mobile"));
28  m_ui->playButton->setIcon(QIcon(":/functions/btn_play_hover"));
29  m_ui->previousButton->setIcon(QIcon(":/functions/btn_previous_hover"));
30  m_ui->nextButton->setIcon(QIcon(":/functions/btn_next_hover"));
31  m_ui->refreshButton->setIcon(QIcon(":/functions/btn_fresh_fore_hover"));
32 
33  m_ui->playButton->setStyleSheet(TTK::UI::BackgroundStyle01);
34  m_ui->previousButton->setStyleSheet(TTK::UI::BackgroundStyle01);
35  m_ui->nextButton->setStyleSheet(TTK::UI::BackgroundStyle01);
36  m_ui->refreshButton->setStyleSheet(TTK::UI::BackgroundStyle01);
37 
38 #ifdef Q_OS_UNIX
39  m_ui->playButton->setFocusPolicy(Qt::NoFocus);
40  m_ui->previousButton->setFocusPolicy(Qt::NoFocus);
41  m_ui->nextButton->setFocusPolicy(Qt::NoFocus);
42  m_ui->refreshButton->setFocusPolicy(Qt::NoFocus);
43 #endif
44 
45  m_ui->playButton->setIconSize(QSize(31, 31));
46  m_ui->previousButton->setIconSize(QSize(31, 31));
47  m_ui->nextButton->setIconSize(QSize(31, 31));
48  m_ui->refreshButton->setIconSize(QSize(20, 20));
49 
50  m_ui->playButton->setCursor(QCursor(Qt::PointingHandCursor));
51  m_ui->previousButton->setCursor(QCursor(Qt::PointingHandCursor));
52  m_ui->nextButton->setCursor(QCursor(Qt::PointingHandCursor));
53  m_ui->refreshButton->setCursor(QCursor(Qt::PointingHandCursor));
54 
55  m_ui->deviceComboBox->setStyleSheet(TTK::UI::ComboBoxStyle02);
56 
57  m_ui->deviceComboBox->addItem(tr("No connections"));
58  m_ui->deviceComboBox->setEnabled(false);
59 
60  m_timer = new QTimer(this);
61  m_dlnaFinder = new QDlnaFinder(this);
63 
64  startToScan();
65 
67  m_timer->setInterval(TTK_DN_S2MS);
68 
69  connect(m_ui->playButton, SIGNAL(clicked()), SLOT(playSongAction()));
70  connect(m_ui->previousButton, SIGNAL(clicked()), SLOT(playPrevious()));
71  connect(m_ui->nextButton, SIGNAL(clicked()), SLOT(playNext()));
72  connect(m_ui->refreshButton, SIGNAL(clicked()), SLOT(startToScan()));
73  connect(m_dlnaFinder, SIGNAL(finished()), SLOT(scanFinished()));
74  connect(m_timer, SIGNAL(timeout()), SLOT(timeout()));
75 }
76 
78 {
80 
81  QDlnaClient *client = getClient();
82  if(client)
83  {
84  client->stop();
85  client->remove();
86  TTK_INFO_STREAM("Stop DLNA module");
87  }
88 
89  if(m_timer->isActive())
90  {
91  m_timer->stop();
92  }
93  delete m_timer;
94  delete m_dlnaFinder;
95  delete m_dlnaFileServer;
96  delete m_ui;
97 }
98 
100 {
101  m_dlnaFinder->find();
102 }
103 
105 {
106  const QStringList &clients = m_dlnaFinder->clientNames();
107  if(!clients.isEmpty())
108  {
109  m_ui->deviceComboBox->clear();
110  m_ui->deviceComboBox->setEnabled(true);
111  }
112 
113  for(const QString &name : qAsConst(clients))
114  {
115  m_ui->deviceComboBox->addItem(name);
116  }
117 }
118 
120 {
121  if(m_ui->deviceComboBox->currentText() == tr("No connections"))
122  {
123  MusicToastLabel::popup(tr("Can not find any clients"));
124  return;
125  }
126 
127  QDlnaClient *client = getClient();
128  if(!client)
129  {
130  return;
131  }
132 
134  {
135  if(!client->pause())
136  {
137  MusicToastLabel::popup(tr("Do DLNA operation failed"));
138  return;
139  }
140 
142  m_ui->playButton->setIcon(QIcon(":/functions/btn_play_hover"));
143  }
144  else
145  {
147  {
148  if(!client->play())
149  {
150  MusicToastLabel::popup(tr("Do DLNA operation failed"));
151  return;
152  }
153 
155  m_ui->playButton->setIcon(QIcon(":/functions/btn_pause_hover"));
156  return;
157  }
158 
159  m_timer->stop();
161  if(path.isEmpty())
162  {
163  // fallback set index to default one
164  m_currentPlayIndex = 0;
166  if(path.isEmpty())
167  {
168  return;
169  }
170  }
171 
172  QFileInfo fin(path);
173  m_dlnaFileServer->setPrefixPath(fin.path());
174  if(!client->openUri(m_dlnaFileServer->localAddress(client->server()), fin.fileName()))
175  {
176  MusicToastLabel::popup(tr("Do DLNA operation failed"));
177  return;
178  }
179 
180  if(client->play())
181  {
182  m_timer->start();
184  m_ui->playButton->setIcon(QIcon(":/functions/btn_pause_hover"));
185 
186  {
187  QDlna::PositionInfo info;
188  if(client->positionInfo(info))
189  {
190  TTK_INFO_STREAM(info.position << " " << info.duration);
191  }
192  }
193 
194  {
195  QDlna::MediaInfo info;
196  if(client->mediaInfo(info))
197  {
198  TTK_INFO_STREAM(info.duration << " " << info.nextURI << " " << info.medium);
199  }
200  }
201  }
202  else
203  {
204  MusicToastLabel::popup(tr("Do DLNA operation failed"));
206  m_ui->playButton->setIcon(QIcon(":/functions/btn_play_hover"));
207  }
208  }
209 }
210 
212 {
215  playSongAction();
216 }
217 
219 {
222  playSongAction();
223 }
224 
226 {
227  QDlnaClient *client = getClient();
228  if(!client)
229  {
230  return;
231  }
232 
234  if(client->transportInfo(info))
235  {
236  TTK_INFO_STREAM(info.state << " " << info.status << " " << info.speed);
237  }
238 }
239 
241 {
242  const int index = m_ui->deviceComboBox->currentIndex();
243  return index < 0 ? nullptr : m_dlnaFinder->client(index);
244 }
QString localAddress(const QString &prefix) const
QString status
Definition: qdlna.h:38
PlayState
Definition: musicobject.h:183
bool remove(int instance=0) const
bool pause(int instance=0) const
voidpf void uLong size
Definition: ioapi.h:136
The class of the dlna client.
Definition: qdlnaclient.h:35
void setBackgroundLabel(QLabel *label) noexcept
The class of the dlna finder.
Definition: qdlnafinder.h:31
static MusicPlayedListPopWidget * instance()
Ui::MusicSongDlnaTransferWidget * m_ui
QStringList clientNames() const
qint64 duration
Definition: qdlna.h:32
static void popup(const QString &text)
QDlnaClient * client(int index) const
Definition: qdlnafinder.cpp:85
void setPrefixPath(const QString &path)
#define qAsConst
Definition: ttkqtglobal.h:57
bool openUri(const QString &url, const QString &name, int instance=0) const
const char * name
Definition: http_parser.c:458
QString duration
Definition: qdlna.h:44
bool play(int instance=0) const
#define TTK_INFO_STREAM(msg)
Definition: ttklogger.h:74
The namespace of the application object.
Definition: ttkcompat.h:24
The class of the song dlna transfer widget.
bool transportInfo(QDlna::TransportInfo &info, int instance=0) const
bool mediaInfo(QDlna::MediaInfo &info, int instance=0) const
QString state
Definition: qdlna.h:37
qint64 position
Definition: qdlna.h:31
static const QString BackgroundStyle01
Background.
Definition: musicuiobject.h:77
The class of the dlna file server.
#define TTK_REMOVE_SINGLE_WIDGET(object)
QString currentMediaPath(int index) const
#define TTK_DN_S2MS
Definition: ttkglobal.h:355
static const QString ToolButtonStyle04
QString server() const
bool stop(int instance=0) const
MusicSongDlnaTransferWidget(QWidget *parent=nullptr)
bool positionInfo(QDlna::PositionInfo &info, int instance=0) const
QString medium
Definition: qdlna.h:46
QString nextURI
Definition: qdlna.h:45
The class of the moving widget base.
static const QString ComboBoxStyle02