diff --git a/data/data.qrc b/data/data.qrc index ba05aab3d..838816aab 100644 --- a/data/data.qrc +++ b/data/data.qrc @@ -425,5 +425,6 @@ providers/vk.png vk/link.png providers/seafile.png + icons/32x32/internet-services.png diff --git a/data/icons/32x32/internet-services.png b/data/icons/32x32/internet-services.png new file mode 100644 index 000000000..464115e4a Binary files /dev/null and b/data/icons/32x32/internet-services.png differ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 25a27e301..9818306bc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -183,6 +183,7 @@ set(SOURCES internet/internetmodel.cpp internet/internetplaylistitem.cpp internet/internetservice.cpp + internet/internetshowsettingspage.cpp internet/internetview.cpp internet/internetviewcontainer.cpp internet/jamendodynamicplaylist.cpp @@ -488,6 +489,7 @@ set(HEADERS internet/internetmimedata.h internet/internetmodel.h internet/internetservice.h + internet/internetshowsettingspage.h internet/internetsongmimedata.h internet/internetview.h internet/internetviewcontainer.h @@ -694,6 +696,7 @@ set(UI internet/digitallyimportedsettingspage.ui internet/groovesharksettingspage.ui internet/icecastfilterwidget.ui + internet/internetshowsettingspage.ui internet/internetviewcontainer.ui internet/magnatunedownloaddialog.ui internet/magnatunesettingspage.ui diff --git a/src/internet/internetmodel.cpp b/src/internet/internetmodel.cpp index 64adb07c2..ef15b42ba 100644 --- a/src/internet/internetmodel.cpp +++ b/src/internet/internetmodel.cpp @@ -63,6 +63,8 @@ using smart_playlists::GeneratorPtr; QMap* InternetModel::sServices = nullptr; +const char* InternetModel::kSettingsGroup = "InternetModel"; + InternetModel::InternetModel(Application* app, QObject* parent) : QStandardItemModel(parent), app_(app), @@ -108,7 +110,7 @@ InternetModel::InternetModel(Application* app, QObject* parent) AddService(new VkService(app, this)); #endif - invisibleRootItem()->sortChildren(0, Qt::AscendingOrder); + UpdateServices(); } void InternetModel::AddService(InternetService* service) { @@ -126,6 +128,12 @@ void InternetModel::AddService(InternetService* service) { qLog(Debug) << "Adding internet service:" << service->name(); sServices->insert(service->name(), service); + ServiceItem service_item; + service_item.item = root; + service_item.shown = true; + + shown_services_.insert(service, service_item); + connect(service, SIGNAL(StreamError(QString)), SIGNAL(StreamError(QString))); connect(service, SIGNAL(StreamMetadataFound(QUrl, Song)), SIGNAL(StreamMetadataFound(QUrl, Song))); @@ -154,6 +162,9 @@ void InternetModel::RemoveService(InternetService* service) { // Remove the service from the list sServices->remove(service->name()); + // Don't forget to delete from shown_services too + shown_services_.remove(service); + // Disconnect the service disconnect(service, 0, this, 0); } @@ -315,3 +326,49 @@ void InternetModel::ReloadSettings() { service->ReloadSettings(); } } + +void InternetModel::UpdateServices() { + QSettings s; + s.beginGroup(kSettingsGroup); + + QStringList keys = s.childKeys(); + + for (QString service_name : keys) { + InternetService* internet_service = ServiceByName(service_name); + bool setting_val = s.value(service_name).toBool(); + + // Only update if values are different + if (setting_val == true && shown_services_[internet_service].shown == false) { + ShowService(internet_service); + } else if (setting_val == false && + shown_services_[internet_service].shown == true) { + HideService(internet_service); + } + } + + s.endGroup(); + + invisibleRootItem()->sortChildren(0, Qt::AscendingOrder); +} + +void InternetModel::ShowService(InternetService* service) { + if(shown_services_[service].shown != true) { + invisibleRootItem()->appendRow(shown_services_[service].item); + shown_services_[service].shown = true; + } +} + +void InternetModel::HideService(InternetService* service) { + // Find and remove the root item that this service created + for (int i = 0; i < invisibleRootItem()->rowCount(); ++i) { + QStandardItem* item = invisibleRootItem()->child(i); + if (!item || + item->data(Role_Service).value() == service) { + // Don't remove the standarditem behind the row + invisibleRootItem()->takeRow(i); + break; + } + } + + shown_services_[service].shown = false; +} diff --git a/src/internet/internetmodel.h b/src/internet/internetmodel.h index 794cbd94a..13fe80348 100644 --- a/src/internet/internetmodel.h +++ b/src/internet/internetmodel.h @@ -114,8 +114,14 @@ class InternetModel : public QStandardItemModel { PlayBehaviour_DoubleClickAction, }; + struct ServiceItem { + QStandardItem *item; + bool shown; + }; + // Needs to be static for InternetPlaylistItem::restore static InternetService* ServiceByName(const QString& name); + static const char* kSettingsGroup; template static T* Service() { @@ -127,6 +133,10 @@ class InternetModel : public QStandardItemModel { // removed from the model. void AddService(InternetService* service); void RemoveService(InternetService* service); + void HideService(InternetService* service); + void ShowService(InternetService* service); + // Add or remove the services according to the setting file + void UpdateServices(); // Returns the service that is a parent of this item. Works by walking up // the tree until it finds an item with Role_Service set. @@ -155,6 +165,9 @@ class InternetModel : public QStandardItemModel { const QModelIndex& current_index() const { return current_index_; } const QModelIndexList& selected_indexes() const { return selected_indexes_; } + const QMap shown_services() const { + return shown_services_; + } signals: void StreamError(const QString& message); @@ -167,6 +180,9 @@ signals: void ServiceDeleted(); private: + + QMap shown_services_; + static QMap* sServices; Application* app_; diff --git a/src/internet/internetshowsettingspage.cpp b/src/internet/internetshowsettingspage.cpp new file mode 100644 index 000000000..baba3887f --- /dev/null +++ b/src/internet/internetshowsettingspage.cpp @@ -0,0 +1,73 @@ +/* This file is part of Clementine. + Copyright 2011, David Sansome + + 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 . +*/ + +#include "internetshowsettingspage.h" + +#include "core/application.h" +#include "ui/settingsdialog.h" +#include "internetservice.h" +#include "internetmodel.h" + +#include + +InternetShowSettingsPage::InternetShowSettingsPage(SettingsDialog* parent) + : SettingsPage(parent), ui_(new Ui::InternetShowSettingsPage) { + ui_->setupUi(this); + + ui_->sources->header()->setResizeMode(0, QHeaderView::Stretch); + ui_->sources->header()->setResizeMode(1, QHeaderView::ResizeToContents); +} + +void InternetShowSettingsPage::Load() { + QMap shown_services = + dialog()->app()->internet_model()->shown_services(); + + ui_->sources->clear(); + + for (QMap::iterator service = shown_services.begin(); + service != shown_services.end(); ++service) { + QTreeWidgetItem* item = new QTreeWidgetItem; + + // Get the same text and the same icon as the service tree + item->setText(0, service.value().item->text()); + item->setIcon(0, service.value().item->icon()); + + Qt::CheckState check_state = service.value().shown == true ? Qt::Checked : Qt::Unchecked; + item->setData(0, Qt::CheckStateRole, check_state); + /* We have to store the constant name of the service */ + item->setData(1, Qt::UserRole, service.key()->name()); + + ui_->sources->invisibleRootItem()->addChild(item); + } + + ui_->sources->invisibleRootItem()->sortChildren(0, Qt::AscendingOrder); +} + +void InternetShowSettingsPage::Save() { + QSettings s; + s.beginGroup(InternetModel::kSettingsGroup); + + for (int i = 0; i < ui_->sources->invisibleRootItem()->childCount(); ++i) { + QTreeWidgetItem* item = ui_->sources->invisibleRootItem()->child(i); + s.setValue(item->data(1, Qt::UserRole).toString(), + (item->data(0, Qt::CheckStateRole).toBool())); + } + + s.endGroup(); + + dialog()->app()->internet_model()->UpdateServices(); +} diff --git a/src/internet/internetshowsettingspage.h b/src/internet/internetshowsettingspage.h new file mode 100644 index 000000000..b9afdc994 --- /dev/null +++ b/src/internet/internetshowsettingspage.h @@ -0,0 +1,43 @@ +/* This file is part of Clementine. + Copyright 2011, David Sansome + + 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 . +*/ + +#ifndef INTERNETSHOWSETTINGSPAGE_H +#define INTERNETSHOWSETTINGSPAGE_H + +#include "ui/settingspage.h" +#include "ui_internetshowsettingspage.h" + +#include + +class QTreeWidgetItem; +class Ui_InternetShowSettingsPage; + +class InternetShowSettingsPage : public SettingsPage { + Q_OBJECT + + public: + InternetShowSettingsPage(SettingsDialog* dialog); + + void Load(); + void Save(); + + private: + std::unique_ptr ui_; + QIcon warning_icon_; +}; + +#endif // INTERNETSHOWSETTINGSPAGE_H diff --git a/src/internet/internetshowsettingspage.ui b/src/internet/internetshowsettingspage.ui new file mode 100644 index 000000000..7b019a029 --- /dev/null +++ b/src/internet/internetshowsettingspage.ui @@ -0,0 +1,82 @@ + + + InternetShowSettingsPage + + + + 0 + 0 + 654 + 506 + + + + Internet services + + + + :/icons/32x32/internet-services.png:/icons/32x32/internet-services.png + + + + + + Sources + + + + + + Choose the internet services you want to show. + + + true + + + + + + + + + Qt::ScrollBarAlwaysOff + + + false + + + 2 + + + false + + + false + + + + 1 + + + + + 2 + + + + + + + + + + + + + sources + + + + + + diff --git a/src/ui/settingsdialog.cpp b/src/ui/settingsdialog.cpp index b23017a35..6cd09ae85 100644 --- a/src/ui/settingsdialog.cpp +++ b/src/ui/settingsdialog.cpp @@ -37,6 +37,7 @@ #include "globalsearch/globalsearchsettingspage.h" #include "internet/digitallyimportedsettingspage.h" #include "internet/groovesharksettingspage.h" +#include "internet/internetshowsettingspage.h" #include "internet/magnatunesettingspage.h" #include "internet/soundcloudsettingspage.h" #include "internet/spotifysettingspage.h" @@ -84,7 +85,6 @@ #endif - #include #include #include @@ -156,6 +156,7 @@ SettingsDialog::SettingsDialog(Application* app, BackgroundStreams* streams, AddPage(Page_Appearance, new AppearanceSettingsPage(this), iface); AddPage(Page_SongInformation, new SongInfoSettingsPage(this), iface); AddPage(Page_Notifications, new NotificationsSettingsPage(this), iface); + AddPage(Page_InternetShow, new InternetShowSettingsPage(this), iface); // Internet providers QTreeWidgetItem* providers = AddCategory(tr("Internet providers")); diff --git a/src/ui/settingsdialog.h b/src/ui/settingsdialog.h index d3d913c71..2c4a29db6 100644 --- a/src/ui/settingsdialog.h +++ b/src/ui/settingsdialog.h @@ -85,7 +85,8 @@ class SettingsDialog : public QDialog { Page_Skydrive, Page_Box, Page_Vk, - Page_Seafile + Page_Seafile, + Page_InternetShow }; enum Role { Role_IsSeparator = Qt::UserRole };