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"
|
2011-01-10 23:26:13 +01:00
|
|
|
#include "core/mimedata.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";
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
SavedRadio::SavedRadio(InternetModel* parent)
|
|
|
|
: InternetService(kServiceName, parent, parent),
|
2011-01-09 19:27:41 +01:00
|
|
|
context_menu_(NULL),
|
|
|
|
root_(NULL)
|
2010-02-24 23:26:01 +01:00
|
|
|
{
|
|
|
|
LoadStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
SavedRadio::~SavedRadio() {
|
|
|
|
delete context_menu_;
|
|
|
|
}
|
|
|
|
|
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:
|
2010-05-28 00:53:07 +02:00
|
|
|
foreach (const Stream& stream, streams_)
|
2011-01-09 19:27:41 +01:00
|
|
|
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");
|
|
|
|
for (int i=0 ; i<count ; ++i) {
|
|
|
|
s.setArrayIndex(i);
|
2010-05-28 00:53:07 +02:00
|
|
|
streams_ << Stream(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);
|
|
|
|
for (int i=0 ; i<count ; ++i) {
|
|
|
|
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-01-09 19:27:41 +01:00
|
|
|
void SavedRadio::ShowContextMenu(const QModelIndex& index,
|
2010-05-09 19:53:27 +02:00
|
|
|
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());
|
2010-08-27 17:20:52 +02: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()));
|
|
|
|
context_menu_->addSeparator();
|
|
|
|
context_menu_->addAction(IconLoader::Load("document-open-remote"), tr("Add another stream..."), this, SIGNAL(ShowAddStreamDialog()));
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
context_item_ = model()->itemFromIndex(index);
|
2011-07-15 15:27:50 +02:00
|
|
|
const bool is_root = 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() {
|
2011-07-15 15:27:50 +02:00
|
|
|
streams_.removeAll(Stream(QUrl(context_item_->data(InternetModel::Role_Url).toUrl())));
|
2011-01-09 19:27:41 +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() {
|
2010-08-27 17:20:52 +02:00
|
|
|
if (!edit_dialog_) {
|
|
|
|
edit_dialog_.reset(new AddStreamDialog);
|
|
|
|
edit_dialog_->set_save_visible(false);
|
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
edit_dialog_->set_name(context_item_->text());
|
2011-07-15 15:27:50 +02:00
|
|
|
edit_dialog_->set_url(context_item_->data(InternetModel::Role_Url).toUrl());
|
2010-05-28 00:53:07 +02:00
|
|
|
if (edit_dialog_->exec() == QDialog::Rejected)
|
|
|
|
return;
|
|
|
|
|
2011-07-15 15:27:50 +02:00
|
|
|
int i = streams_.indexOf(Stream(QUrl(context_item_->data(InternetModel::Role_Url).toUrl())));
|
2010-05-28 00:53:07 +02:00
|
|
|
Stream& stream = streams_[i];
|
|
|
|
stream.name_ = edit_dialog_->name();
|
|
|
|
stream.url_ = edit_dialog_->url();
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
context_item_->setText(stream.name_);
|
2011-07-15 15:27:50 +02:00
|
|
|
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-02-10 23:24:17 +01:00
|
|
|
QModelIndex SavedRadio::GetCurrentIndex() {
|
|
|
|
return context_item_->index();
|
2011-02-09 18:51:59 +01:00
|
|
|
}
|
|
|
|
|
2011-01-09 19:27:41 +01:00
|
|
|
void SavedRadio::AddStreamToList(const Stream& stream, QStandardItem* parent) {
|
|
|
|
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);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
void SavedRadio::Add(const QUrl &url, const QString& name) {
|
|
|
|
if (streams_.contains(Stream(url)))
|
2010-02-24 23:26:01 +01:00
|
|
|
return;
|
|
|
|
|
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();
|
|
|
|
}
|