1/*
2 Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org>
3 Copyright (C) 2011-2019 Harald Sitter <sitter@kde.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) version 3.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "backendselection.h"
22
23#include <QList>
24#include <QStringList>
25#include <QListWidget>
26
27#include <QDebug>
28#include <QDesktopServices>
29#include <QDir>
30#include <QJsonArray>
31#include <QJsonObject>
32#include <QPluginLoader>
33#include <QSettings>
34#include <QUrl>
35
36BackendSelection::BackendSelection(QWidget *parent)
37 : QWidget(parent)
38{
39 setupUi(this);
40
41 m_down->setIcon(QIcon::fromTheme(name: "go-down"));
42 m_up->setIcon(QIcon::fromTheme(name: "go-up"));
43 m_comment->setWordWrap(true);
44
45 m_emptyPage = stackedWidget->addWidget(w: new QWidget());
46
47 connect(sender: m_select, signal: &QListWidget::itemSelectionChanged,
48 context: this, slot: &BackendSelection::selectionChanged);
49 connect(sender: m_up, signal: &QToolButton::clicked,
50 context: this, slot: &BackendSelection::up);
51 connect(sender: m_down, signal: &QToolButton::clicked,
52 context: this, slot: &BackendSelection::down);
53
54 connect(sender: m_website, signal: &QLabel::linkActivated,
55 context: this, slot: [=](const QString &url) { QDesktopServices::openUrl(url: QUrl(url)); });
56}
57
58void BackendSelection::load()
59{
60 // NOTE: for phonon5 this should move into the library in some form.
61 m_backends.clear();
62
63 const auto backends = Phonon::Factory::findBackends();
64
65 m_select->clear();
66
67 for (const auto &bd : backends) {
68 m_select->addItem(label: bd.name);
69 m_backends.insert(key: bd.name, value: bd);
70 }
71 m_select->item(row: 0)->setSelected(true);
72}
73
74void BackendSelection::save()
75{
76 QSettings settings("kde.org", "libphonon");
77 settings.beginWriteArray(prefix: "Backends", size: m_select->count());
78 for (int i = 0; i < m_select->count(); ++i) {
79 settings.setArrayIndex(i);
80 const QListWidgetItem *item = m_select->item(row: i);
81 const auto backend = m_backends.value(key: item->text());
82 settings.setValue(key: "iid", value: backend.iid);
83 }
84 settings.endArray();
85 settings.sync();
86}
87
88void BackendSelection::selectionChanged()
89{
90 if (m_select->selectedItems().isEmpty()) {
91 m_up->setEnabled(false);
92 m_down->setEnabled(false);
93 return;
94 }
95
96 const QListWidgetItem *const item = m_select->selectedItems().first();
97 m_up->setEnabled(m_select->row(item) > 0);
98 m_down->setEnabled(m_select->row(item) < m_select->count() - 1);
99 const auto backend = m_backends[item->text()];
100 m_icon->setPixmap(QIcon::fromTheme(name: backend.icon).pixmap(w: 128, h: 128));
101 m_name->setText(backend.name);
102 m_website->setText(QString("<a href=\"%1\">%1</a>").arg(a: backend.website));
103 m_version->setText(backend.version);
104}
105
106void BackendSelection::up()
107{
108 const QList<QListWidgetItem *> selectedList = m_select->selectedItems();
109 for (const QListWidgetItem *selected : selectedList) {
110 const int row = m_select->row(item: selected);
111 if (row <= 0) {
112 continue;
113 }
114 QListWidgetItem *taken = m_select->takeItem(row: row - 1);
115 m_select->insertItem(row, item: taken);
116 selectionChanged();
117 }
118}
119
120void BackendSelection::down()
121{
122 const QList<QListWidgetItem *> selectedList = m_select->selectedItems();
123 for (const QListWidgetItem *selected : selectedList) {
124 const int row = m_select->row(item: selected);
125 if (row + 1 >= m_select->count()) {
126 continue;
127 }
128 QListWidgetItem *taken = m_select->takeItem(row: row + 1);
129 m_select->insertItem(row, item: taken);
130 selectionChanged();
131 }
132}
133

source code of phonon/settings/backendselection.cpp