From 0bc5775baa3b0c0e70f66507e64d08e16b5e94e3 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Mon, 27 Feb 2012 10:22:43 +0000 Subject: added stream browser plugin git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@2638 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/streambrowser/streamwindow.cpp | 224 +++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 src/plugins/General/streambrowser/streamwindow.cpp (limited to 'src/plugins/General/streambrowser/streamwindow.cpp') diff --git a/src/plugins/General/streambrowser/streamwindow.cpp b/src/plugins/General/streambrowser/streamwindow.cpp new file mode 100644 index 000000000..4dbf86579 --- /dev/null +++ b/src/plugins/General/streambrowser/streamwindow.cpp @@ -0,0 +1,224 @@ +/*************************************************************************** + * Copyright (C) 2012 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "streamwindow.h" + +StreamWindow::StreamWindow(QWidget *parent) : QWidget(parent) +{ + ui.setupUi(this); + setWindowFlags(Qt::Window); + setAttribute(Qt::WA_DeleteOnClose); + setAttribute(Qt::WA_QuitOnClose, false); + m_requestReply = 0; + + ui.addPushButton->setIcon(QIcon::fromTheme("list-add")); + ui.updatePushButton->setIcon(QIcon::fromTheme("view-refresh")); + + + m_icecastModel = new QStandardItemModel(this); + m_icecastModel->setHorizontalHeaderLabels(QStringList() << tr("Name") + << tr("Genre") + << tr("Bitrate") + << tr("Format")); + ui.icecastTableView->setModel(m_icecastModel); + ui.icecastTableView->verticalHeader()->setDefaultSectionSize(fontMetrics().height()); + ui.icecastTableView->verticalHeader()->setResizeMode(QHeaderView::Fixed); + ui.icecastTableView->setEditTriggers(QAbstractItemView::NoEditTriggers); + + ui.statusLabel->hide(); + + + m_http = new QNetworkAccessManager(this); + //load global proxy settings + QmmpSettings *gs = QmmpSettings::instance(); + if (gs->isProxyEnabled()) + { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); + if(gs->useProxyAuth()) + { + proxy.setUser(gs->proxy().userName()); + proxy.setPassword(gs->proxy().password()); + } + m_http->setProxy(proxy); + } + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(showText(QNetworkReply *))); + //read settings + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + settings.beginGroup("StreamBrowser"); + restoreGeometry(settings.value("geometry").toByteArray()); + ui.icecastTableView->horizontalHeader()->restoreState(settings.value("icecast_headers").toByteArray()); + settings.endGroup(); + //create cache dir + QString path = QFileInfo(Qmmp::configFile()).absoluteDir().path(); + QDir dir(path); + if(!dir.exists("streambrowser")) + dir.mkdir("streambrowser"); + //read cache + QFile file(path + "/streambrowser/icecast.xml"); + if(file.open(QIODevice::ReadOnly)) + readIceCast(&file); + else + on_updatePushButton_clicked(); +} + + +StreamWindow::~StreamWindow() +{ +} + +void StreamWindow::showText(QNetworkReply *reply) +{ + ui.statusLabel->setText(tr("Done")); + if (reply->error() != QNetworkReply::NoError) + { + ui.statusLabel->setText(tr("Error")); + QMessageBox::warning (this, tr("Error"), reply->errorString()); + m_requestReply = 0; + reply->deleteLater(); + return; + } + if(m_requestReply == reply) + { + m_requestReply = 0; + readIceCast(reply); + } + reply->deleteLater(); +} + +void StreamWindow::on_updatePushButton_clicked() +{ + QNetworkRequest request; + request.setUrl(QUrl("http://dir.xiph.org/yp.xml")); + request.setRawHeader("User-Agent", QString("qmmp/%1").arg(Qmmp::strVersion()).toAscii()); + m_requestReply = m_http->get(request); + ui.statusLabel->setText(tr("Receiving")); + ui.statusLabel->show(); +} + +void StreamWindow::on_addPushButton_clicked() +{ + QModelIndexList indexes = ui.icecastTableView->selectionModel()->selectedRows(0); + QStringList urls; + foreach(QModelIndex index, indexes) + { + urls.append(m_icecastModel->item(index.row(),0)->data().toString()); + } + urls.removeDuplicates(); + PlayListManager::instance()->add(urls); +} + +void StreamWindow::closeEvent(QCloseEvent *) +{ + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + settings.beginGroup("StreamBrowser"); + settings.setValue("geometry", saveGeometry()); + settings.setValue("icecast_headers", ui.icecastTableView->horizontalHeader()->saveState()); + settings.endGroup(); + + QString path = QFileInfo(Qmmp::configFile()).absoluteDir().path(); + QFile file(path + "/streambrowser/icecast.xml"); + file.open(QIODevice::WriteOnly); + + QXmlStreamWriter writer(&file); + writer.setCodec("UTF-8"); + writer.setAutoFormatting(true); + writer.writeStartDocument(); + writer.writeStartElement("directory"); + for(int i = 0; i < m_icecastModel->rowCount(); ++i) + { + writer.writeStartElement("entry"); + writer.writeTextElement("server_name", m_icecastModel->item(i,0)->text()); + writer.writeTextElement("listen_url", m_icecastModel->item(i,0)->data().toString()); + writer.writeTextElement("genre", m_icecastModel->item(i,1)->text()); + writer.writeTextElement("bitrate", m_icecastModel->item(i,2)->text()); + writer.writeTextElement("server_type", m_icecastModel->item(i,3)->text()); + writer.writeEndElement(); + } + writer.writeEndElement(); + writer.writeEndDocument(); +} + +void StreamWindow::readIceCast(QIODevice *input) +{ + m_icecastModel->removeRows(0, m_icecastModel->rowCount()); + QXmlStreamReader xml(input); + QString currentTag, server_name, listen_url, genre, bitrate, server_type; + while (!xml.atEnd()) + { + xml.readNext(); + if (xml.isStartElement()) + { + currentTag = xml.name().toString(); + } + else if (xml.isEndElement()) + { + if (xml.name() == "entry") + { + m_icecastModel->appendRow(QList () + << new QStandardItem(server_name) + << new QStandardItem(genre) + << new QStandardItem(bitrate) + << new QStandardItem(server_type)); + + QStandardItem *item = m_icecastModel->item(m_icecastModel->rowCount()-1, 0); + item->setToolTip(server_name + "\n" + listen_url); + item->setData(listen_url); + + server_name.clear(); + listen_url.clear(); + genre.clear(); + bitrate.clear(); + server_type.clear(); + } + + } + else if (xml.isCharacters() && !xml.isWhitespace()) + { + if (currentTag == "server_name") + server_name += xml.text().toString(); + else if (currentTag == "listen_url") + listen_url += xml.text().toString(); + else if (currentTag == "genre") + genre += xml.text().toString(); + else if (currentTag == "bitrate") + bitrate += xml.text().toString(); + else if(currentTag == "server_type") + server_type += xml.text().toString(); + } + } + if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) + { + qWarning("StreamWindow: xml error: %lld: %s", xml.lineNumber(), qPrintable(xml.errorString())); + } +} -- cgit v1.2.3-13-gbd6f