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/>.
|
|
|
|
*/
|
|
|
|
|
2010-02-24 23:26:01 +01:00
|
|
|
#include "savedradio.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";
|
|
|
|
|
2010-05-09 17:51:04 +02:00
|
|
|
SavedRadio::SavedRadio(RadioModel* parent)
|
2010-02-24 23:26:01 +01:00
|
|
|
: RadioService(kServiceName, parent),
|
|
|
|
root_(NULL),
|
2010-08-27 17:20:52 +02:00
|
|
|
context_menu_(NULL)
|
2010-02-24 23:26:01 +01:00
|
|
|
{
|
|
|
|
LoadStreams();
|
|
|
|
}
|
|
|
|
|
|
|
|
SavedRadio::~SavedRadio() {
|
|
|
|
delete context_menu_;
|
|
|
|
}
|
|
|
|
|
|
|
|
RadioItem* SavedRadio::CreateRootItem(RadioItem* parent) {
|
2010-03-21 14:07:02 +01:00
|
|
|
root_ = new RadioItem(this, RadioItem::Type_Service, tr("Your radio streams"), parent);
|
2010-05-19 17:45:29 +02:00
|
|
|
root_->icon = IconLoader::Load("document-open-remote");
|
2010-02-24 23:26:01 +01:00
|
|
|
return root_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedRadio::LazyPopulate(RadioItem* item) {
|
|
|
|
switch (item->type) {
|
|
|
|
case RadioItem::Type_Service:
|
2010-05-28 00:53:07 +02:00
|
|
|
foreach (const Stream& stream, streams_)
|
2010-02-24 23:26:01 +01:00
|
|
|
ItemForStream(stream, root_);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->lazy_loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2010-05-09 19:53:27 +02:00
|
|
|
void SavedRadio::ShowContextMenu(RadioItem* item, const QModelIndex&,
|
|
|
|
const QPoint& global_pos) {
|
2010-08-27 17:20:52 +02:00
|
|
|
if (!context_menu_) {
|
|
|
|
context_menu_ = new QMenu;
|
|
|
|
add_action_ = context_menu_->addAction(IconLoader::Load("media-playback-start"), tr("Add to playlist"), this, SLOT(AddToPlaylist()));
|
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
2010-02-24 23:26:01 +01:00
|
|
|
context_item_ = item;
|
|
|
|
|
|
|
|
add_action_->setEnabled(item != root_);
|
|
|
|
remove_action_->setEnabled(item != root_);
|
2010-05-28 00:53:07 +02:00
|
|
|
edit_action_->setEnabled(item != root_);
|
2010-02-24 23:26:01 +01:00
|
|
|
|
|
|
|
context_menu_->popup(global_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SavedRadio::Remove() {
|
2010-05-28 00:53:07 +02:00
|
|
|
streams_.removeAll(Stream(QUrl(context_item_->key)));
|
2010-02-24 23:26:01 +01:00
|
|
|
context_item_->parent->DeleteNotify(context_item_->row);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
edit_dialog_->set_name(context_item_->display_text);
|
|
|
|
edit_dialog_->set_url(context_item_->key);
|
|
|
|
if (edit_dialog_->exec() == QDialog::Rejected)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int i = streams_.indexOf(Stream(QUrl(context_item_->key)));
|
|
|
|
Stream& stream = streams_[i];
|
|
|
|
stream.name_ = edit_dialog_->name();
|
|
|
|
stream.url_ = edit_dialog_->url();
|
|
|
|
|
|
|
|
context_item_->display_text = stream.name_;
|
|
|
|
context_item_->key = stream.url_.toString();
|
|
|
|
context_item_->ChangedNotify();
|
2010-06-18 03:21:32 +02:00
|
|
|
|
|
|
|
SaveStreams();
|
2010-05-28 00:53:07 +02:00
|
|
|
}
|
|
|
|
|
2010-02-24 23:26:01 +01:00
|
|
|
void SavedRadio::AddToPlaylist() {
|
|
|
|
emit AddItemToPlaylist(context_item_);
|
|
|
|
}
|
|
|
|
|
2010-05-28 00:53:07 +02:00
|
|
|
RadioItem* SavedRadio::ItemForStream(const Stream& stream, RadioItem* parent) {
|
|
|
|
RadioItem* s = new RadioItem(this, Type_Stream, stream.url_.toString(), parent);
|
|
|
|
if (!stream.name_.isEmpty())
|
|
|
|
s->display_text = stream.name_;
|
2010-02-24 23:26:01 +01:00
|
|
|
s->lazy_loaded = true;
|
|
|
|
s->icon = QIcon(":last.fm/icon_radio.png");
|
|
|
|
s->playable = true;
|
2010-06-16 18:11:23 +02:00
|
|
|
s->use_song_loader = true;
|
2010-02-24 23:26:01 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
if (root_->lazy_loaded) {
|
2010-05-28 00:53:07 +02:00
|
|
|
RadioItem* s = ItemForStream(stream, NULL);
|
2010-02-24 23:26:01 +01:00
|
|
|
s->InsertNotify(root_);
|
|
|
|
}
|
|
|
|
SaveStreams();
|
|
|
|
}
|