2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
#include "internetmodel.h"
|
2010-02-24 23:26:01 +01:00
|
|
|
#include "savedradio.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2011-01-10 23:26:13 +01:00
|
|
|
#include "core/mimedata.h"
|
2011-11-06 16:00:22 +01:00
|
|
|
#include "globalsearch/globalsearch.h"
|
|
|
|
#include "globalsearch/savedradiosearchprovider.h"
|
2010-05-28 00:53:07 +02:00
|
|
|
#include "ui/addstreamdialog.h"
|
2010-05-19 17:45:29 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-02-24 23:26:01 +01:00
|
|
|
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QMenu>
|
|
|
|
|
|
|
|
const char* SavedRadio::kServiceName = "SavedRadio";
|
|
|
|
const char* SavedRadio::kSettingsGroup = "SavedRadio";
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
SavedRadio::SavedRadio(Application* app, InternetModel* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: InternetService(kServiceName, app, parent, parent),
|
|
|
|
context_menu_(nullptr),
|
|
|
|
root_(nullptr) {
|
2010-02-24 23:26:01 +01:00
|
|
|
LoadStreams();
|
2011-11-06 16:00:22 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
app_->global_search()->AddProvider(
|
|
|
|
new SavedRadioSearchProvider(this, app_, this));
|
2010-02-24 23:26:01 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
SavedRadio::~SavedRadio() { delete context_menu_; }
|
2010-02-24 23:26:01 +01:00
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
QStandardItem* SavedRadio::CreateRootItem() {
|
|
|
|
root_ = new QStandardItem(IconLoader::Load("document-open-remote"),
|
|
|
|
tr("Your radio streams"));
|
2011-07-15 15:27:50 +02:00
|
|
|
root_->setData(true, InternetModel::Role_CanLazyLoad);
|
2010-02-24 23:26:01 +01:00
|
|
|
return root_;
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
void SavedRadio::LazyPopulate(QStandardItem* item) {
|
2011-07-15 15:27:50 +02:00
|
|
|
switch (item->data(InternetModel::Role_Type).toInt()) {
|
|
|
|
case InternetModel::Type_Service:
|
2014-02-12 22:11:28 +01:00
|
|
|
for (const Stream& stream : streams_) {
|
|
|
|
AddStreamToList(stream, root_);
|
|
|
|
}
|
2010-02-24 23:26:01 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedRadio::LoadStreams() {
|
|
|
|
// Load saved streams
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
int count = s.beginReadArray("streams");
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2010-02-24 23:26:01 +01:00
|
|
|
s.setArrayIndex(i);
|
2014-02-07 16:34:20 +01:00
|
|
|
streams_ << Stream(QUrl(s.value("url").toString()),
|
|
|
|
s.value("name").toString());
|
2010-02-24 23:26:01 +01:00
|
|
|
}
|
|
|
|
s.endArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedRadio::SaveStreams() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
int count = streams_.size();
|
|
|
|
s.beginWriteArray("streams", count);
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < count; ++i) {
|
2010-02-24 23:26:01 +01:00
|
|
|
s.setArrayIndex(i);
|
2010-05-28 00:53:07 +02:00
|
|
|
s.setValue("url", streams_[i].url_);
|
|
|
|
s.setValue("name", streams_[i].name_);
|
2010-02-24 23:26:01 +01:00
|
|
|
}
|
|
|
|
s.endArray();
|
2011-11-06 16:00:22 +01:00
|
|
|
|
|
|
|
emit StreamsChanged();
|
2010-02-24 23:26:01 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:44:43 +01:00
|
|
|
void SavedRadio::ShowContextMenu(const QPoint& global_pos) {
|
2010-08-27 17:20:52 +02:00
|
|
|
if (!context_menu_) {
|
|
|
|
context_menu_ = new QMenu;
|
2011-02-10 23:24:17 +01:00
|
|
|
context_menu_->addActions(GetPlaylistActions());
|
2014-02-07 16:34:20 +01:00
|
|
|
remove_action_ = context_menu_->addAction(
|
|
|
|
IconLoader::Load("list-remove"), tr("Remove"), this, SLOT(Remove()));
|
|
|
|
edit_action_ = context_menu_->addAction(IconLoader::Load("edit-rename"),
|
|
|
|
tr("Edit..."), this, SLOT(Edit()));
|
2010-08-27 17:20:52 +02:00
|
|
|
context_menu_->addSeparator();
|
2014-02-07 16:34:20 +01:00
|
|
|
context_menu_->addAction(IconLoader::Load("document-open-remote"),
|
|
|
|
tr("Add another stream..."), this,
|
|
|
|
SIGNAL(ShowAddStreamDialog()));
|
2010-08-27 17:20:52 +02:00
|
|
|
}
|
|
|
|
|
2012-03-11 15:44:43 +01:00
|
|
|
const bool is_root =
|
|
|
|
model()->current_index().data(InternetModel::Role_Type).toInt() ==
|
|
|
|
InternetModel::Type_Service;
|
2010-02-24 23:26:01 +01:00
|
|
|
|
2011-02-10 23:24:17 +01:00
|
|
|
GetAppendToPlaylistAction()->setEnabled(!is_root);
|
|
|
|
GetReplacePlaylistAction()->setEnabled(!is_root);
|
|
|
|
GetOpenInNewPlaylistAction()->setEnabled(!is_root);
|
2011-01-09 19:27:41 +01:00
|
|
|
remove_action_->setEnabled(!is_root);
|
|
|
|
edit_action_->setEnabled(!is_root);
|
2010-02-24 23:26:01 +01:00
|
|
|
|
|
|
|
context_menu_->popup(global_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedRadio::Remove() {
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItem* context_item =
|
|
|
|
model()->itemFromIndex(model()->current_index());
|
2012-03-11 15:44:43 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
streams_.removeAll(
|
|
|
|
Stream(QUrl(context_item->data(InternetModel::Role_Url).toUrl())));
|
2012-03-11 15:44:43 +01:00
|
|
|
context_item->parent()->removeRow(context_item->row());
|
2010-02-24 23:26:01 +01:00
|
|
|
SaveStreams();
|
|
|
|
}
|
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
void SavedRadio::Edit() {
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItem* context_item =
|
|
|
|
model()->itemFromIndex(model()->current_index());
|
2012-03-11 15:44:43 +01:00
|
|
|
|
2010-08-27 17:20:52 +02:00
|
|
|
if (!edit_dialog_) {
|
|
|
|
edit_dialog_.reset(new AddStreamDialog);
|
|
|
|
edit_dialog_->set_save_visible(false);
|
|
|
|
}
|
|
|
|
|
2012-03-11 15:44:43 +01:00
|
|
|
edit_dialog_->set_name(context_item->text());
|
|
|
|
edit_dialog_->set_url(context_item->data(InternetModel::Role_Url).toUrl());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (edit_dialog_->exec() == QDialog::Rejected) return;
|
2010-05-28 00:53:07 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
int i = streams_.indexOf(
|
|
|
|
Stream(QUrl(context_item->data(InternetModel::Role_Url).toUrl())));
|
2012-08-26 15:22:38 +02:00
|
|
|
Stream* stream = &streams_[i];
|
|
|
|
stream->name_ = edit_dialog_->name();
|
|
|
|
stream->url_ = edit_dialog_->url();
|
2010-05-28 00:53:07 +02:00
|
|
|
|
2012-08-26 15:22:38 +02:00
|
|
|
context_item->setText(stream->name_);
|
|
|
|
context_item->setData(stream->url_, InternetModel::Role_Url);
|
2010-06-18 03:21:32 +02:00
|
|
|
|
|
|
|
SaveStreams();
|
2010-05-28 00:53:07 +02:00
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
void SavedRadio::AddStreamToList(const Stream& stream, QStandardItem* parent) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QStandardItem* s =
|
|
|
|
new QStandardItem(QIcon(":last.fm/icon_radio.png"), stream.name_);
|
2011-07-15 15:27:50 +02:00
|
|
|
s->setData(stream.url_, InternetModel::Role_Url);
|
2014-02-07 16:34:20 +01:00
|
|
|
s->setData(InternetModel::PlayBehaviour_UseSongLoader,
|
|
|
|
InternetModel::Role_PlayBehaviour);
|
2011-01-09 19:27:41 +01:00
|
|
|
parent->appendRow(s);
|
2010-02-24 23:26:01 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void SavedRadio::Add(const QUrl& url, const QString& name) {
|
|
|
|
if (streams_.contains(Stream(url))) return;
|
2010-02-24 23:26:01 +01:00
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
Stream stream(url, name);
|
|
|
|
streams_ << stream;
|
2010-02-24 23:26:01 +01:00
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
if (!root_->data(InternetModel::Role_CanLazyLoad).toBool()) {
|
2011-01-09 19:27:41 +01:00
|
|
|
AddStreamToList(stream, root_);
|
2010-02-24 23:26:01 +01:00
|
|
|
}
|
|
|
|
SaveStreams();
|
|
|
|
}
|