Kirigami-addons

soundspickermodel.cpp
1// SPDX-FileCopyrightText: 2021 Han Young <hanyoung@protonmail.com>
2// SPDX-License-Identifier: LGPL-2.0-or-later
3
4#include "soundspickermodel.h"
5
6#include <vector>
7
8#include <QDirIterator>
9#include <QStandardPaths>
10
11class SoundsPickerModel::Private
12{
13public:
14 QStringList defaultAudio;
15 std::vector<QString> soundsVec;
16 bool notification = false;
17 QString theme = QStringLiteral("plasma-mobile");
18};
19
20SoundsPickerModel::SoundsPickerModel(QObject *parent)
21 : QAbstractListModel(parent)
22 , d(std::make_unique<Private>())
23{
24 loadFiles();
25}
26
27void SoundsPickerModel::loadFiles()
28{
29 d->soundsVec.clear();
31 const QString path = QStringLiteral("/sounds/") + d->theme + QStringLiteral("/stereo/");
32
33 for (const auto &directory : locations) {
34 if (QDir(directory + path).exists()) {
35 QString subPath = directory + path;
36 if (!d->notification && QDir(subPath + QStringLiteral("ringtone")).exists()) {
37 subPath += QStringLiteral("ringtone");
38 } else if (d->notification && QDir(subPath + QStringLiteral("notification")).exists()) {
39 subPath += QStringLiteral("notification");
40 }
41
43 while (it.hasNext()) {
44 d->soundsVec.push_back(it.next());
45 }
46 }
47 }
48}
49
50SoundsPickerModel::~SoundsPickerModel() = default;
51
52QHash<int, QByteArray> SoundsPickerModel::roleNames() const
53{
54 return {
55 {Roles::NameRole, QByteArrayLiteral("ringtoneName")},
56 {Roles::UrlRole, QByteArrayLiteral("sourceUrl")}
57 };
58}
59
60bool SoundsPickerModel::notification() const
61{
62 return d->notification;
63}
64
65void SoundsPickerModel::setNotification(bool notification)
66{
67 if (d->notification == notification) {
68 return;
69 }
70 d->notification = notification;
71
72 bool needReset = false;
73 QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/") + d->theme + QStringLiteral("/stereo/");
74 if (!d->notification && QDir(path + QStringLiteral("ringtone")).exists()) {
75 needReset = true;
76 } else if (d->notification && QDir(path + QStringLiteral("notification")).exists()) {
77 needReset = true;
78 }
79
80 if (needReset) {
82 loadFiles();
84 rearrangeRingtoneOrder();
85 }
86}
87
88QString SoundsPickerModel::initialSourceUrl(int index)
89{
90 if (index >= 0 && index < (int)d->soundsVec.size()) {
91 return d->soundsVec.at(index);
92 }
93 return {};
94}
95
96QVariant SoundsPickerModel::data(const QModelIndex &index, int role) const
97{
98 if (!index.isValid() || index.row() < 0 || index.row() >= (int)d->soundsVec.size()) {
99 return {};
100 }
101
102 if (role == NameRole) {
103 auto ret = d->soundsVec.at(index.row());
104 int suffixPos = ret.lastIndexOf(QLatin1Char('.'));
105 if (suffixPos > 0) {
106 ret.truncate(suffixPos);
107 }
108 int pathPos = ret.lastIndexOf(QLatin1Char('/'));
109 if (pathPos >= 0) {
110 ret.remove(0, pathPos + 1);
111 }
112 return ret;
113 }
114 return d->soundsVec.at(index.row());
115}
116int SoundsPickerModel::rowCount(const QModelIndex& parent) const {
117 Q_UNUSED(parent)
118 return d->soundsVec.size();
119}
120
121const QStringList &SoundsPickerModel::defaultAudio() const
122{
123 return d->defaultAudio;
124}
125
126void SoundsPickerModel::setDefaultAudio(const QStringList &audio)
127{
128 d->defaultAudio = audio;
129 rearrangeRingtoneOrder();
130}
131
132const QString &SoundsPickerModel::theme() const
133{
134 return d->theme;
135}
136
137void SoundsPickerModel::setTheme(const QString &theme)
138{
139 if (d->theme == theme) {
140 return;
141 }
142 d->theme = theme;
143
145 loadFiles();
147 rearrangeRingtoneOrder();
148}
149
150void SoundsPickerModel::rearrangeRingtoneOrder()
151{
152 auto i {0};
153 for (int j = 0; j < (int)d->soundsVec.size(); j++) {
154 if (d->defaultAudio.contains(data(index(j), NameRole).toString())) {
155 std::swap(d->soundsVec[j], d->soundsVec[i]);
158 i++;
159 }
160 }
161}
162
163#include "moc_soundspickermodel.cpp"
char * toString(const EngineQuery &query)
QString path(const QString &relativePath)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
QStringList standardLocations(StandardLocation type)
QString writableLocation(StandardLocation type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.