File: backendselection.cpp

package info (click to toggle)
phonon 4%3A4.12.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,380 kB
  • sloc: cpp: 15,009; makefile: 35; sh: 30; awk: 22
file content (132 lines) | stat: -rw-r--r-- 4,195 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
    Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org>
    Copyright (C) 2011-2019 Harald Sitter <sitter@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) version 3.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "backendselection.h"

#include <QList>
#include <QStringList>
#include <QListWidget>

#include <QDebug>
#include <QDesktopServices>
#include <QDir>
#include <QJsonArray>
#include <QJsonObject>
#include <QPluginLoader>
#include <QSettings>
#include <QUrl>

BackendSelection::BackendSelection(QWidget *parent)
    : QWidget(parent)
{
    setupUi(this);

    m_down->setIcon(QIcon::fromTheme("go-down"));
    m_up->setIcon(QIcon::fromTheme("go-up"));
    m_comment->setWordWrap(true);

    m_emptyPage = stackedWidget->addWidget(new QWidget());

    connect(m_select, &QListWidget::itemSelectionChanged,
            this, &BackendSelection::selectionChanged);
    connect(m_up, &QToolButton::clicked,
            this, &BackendSelection::up);
    connect(m_down, &QToolButton::clicked,
            this, &BackendSelection::down);

    connect(m_website, &QLabel::linkActivated,
            this, [=](const QString &url) { QDesktopServices::openUrl(QUrl(url)); });
}

void BackendSelection::load()
{
    // NOTE: for phonon5 this should move into the library in some form.
    m_backends.clear();

    const auto backends = Phonon::Factory::findBackends();

    m_select->clear();

    for (const auto &bd : backends) {
        m_select->addItem(bd.name);
        m_backends.insert(bd.name, bd);
    }
    m_select->item(0)->setSelected(true);
}

void BackendSelection::save()
{
    QSettings settings("kde.org", "libphonon");
    settings.beginWriteArray("Backends", m_select->count());
    for (int i = 0; i < m_select->count(); ++i) {
        settings.setArrayIndex(i);
        const QListWidgetItem *item = m_select->item(i);
        const auto backend = m_backends.value(item->text());
        settings.setValue("iid", backend.iid);
    }
    settings.endArray();
    settings.sync();
}

void BackendSelection::selectionChanged()
{
    if (m_select->selectedItems().isEmpty()) {
        m_up->setEnabled(false);
        m_down->setEnabled(false);
        return;
    }

    const QListWidgetItem *const item = m_select->selectedItems().first();
    m_up->setEnabled(m_select->row(item) > 0);
    m_down->setEnabled(m_select->row(item) < m_select->count() - 1);
    const auto backend = m_backends[item->text()];
    m_icon->setPixmap(QIcon::fromTheme(backend.icon).pixmap(128, 128));
    m_name->setText(backend.name);
    m_website->setText(QString("<a href=\"%1\">%1</a>").arg(backend.website));
    m_version->setText(backend.version);
}

void BackendSelection::up()
{
    const QList<QListWidgetItem *> selectedList = m_select->selectedItems();
    for (const QListWidgetItem *selected : selectedList) {
        const int row = m_select->row(selected);
        if (row <= 0) {
            continue;
        }
        QListWidgetItem *taken = m_select->takeItem(row - 1);
        m_select->insertItem(row, taken);
        selectionChanged();
    }
}

void BackendSelection::down()
{
    const QList<QListWidgetItem *> selectedList = m_select->selectedItems();
    for (const QListWidgetItem *selected : selectedList) {
        const int row = m_select->row(selected);
        if (row + 1 >= m_select->count()) {
            continue;
        }
        QListWidgetItem *taken = m_select->takeItem(row + 1);
        m_select->insertItem(row, taken);
        selectionChanged();
    }
}