4#include "soundspickermodel.h"
9#include <QStandardPaths>
11class SoundsPickerModel::Private
14 QStringList defaultAudio;
15 std::vector<QString> soundsVec;
16 bool notification =
false;
17 QString theme = QStringLiteral(
"plasma-mobile");
20SoundsPickerModel::SoundsPickerModel(
QObject *parent)
22 , d(std::make_unique<Private>())
27void SoundsPickerModel::loadFiles()
31 const QString
path = QStringLiteral(
"/sounds/") + d->theme + QStringLiteral(
"/stereo/");
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");
43 while (it.hasNext()) {
44 d->soundsVec.push_back(it.next());
50SoundsPickerModel::~SoundsPickerModel() =
default;
55 {Roles::NameRole, QByteArrayLiteral(
"ringtoneName")},
56 {Roles::UrlRole, QByteArrayLiteral(
"sourceUrl")}
60bool SoundsPickerModel::notification()
const
62 return d->notification;
65void SoundsPickerModel::setNotification(
bool notification)
67 if (d->notification == notification) {
70 d->notification = notification;
72 bool needReset =
false;
74 if (!d->notification && QDir(path + QStringLiteral(
"ringtone")).exists()) {
76 }
else if (d->notification && QDir(path + QStringLiteral(
"notification")).exists()) {
84 rearrangeRingtoneOrder();
88QString SoundsPickerModel::initialSourceUrl(
int index)
90 if (
index >= 0 &&
index < (
int)d->soundsVec.size()) {
91 return d->soundsVec.at(
index);
98 if (!
index.isValid() ||
index.row() < 0 ||
index.row() >= (
int)d->soundsVec.size()) {
102 if (role == NameRole) {
103 auto ret = d->soundsVec.at(
index.row());
104 int suffixPos = ret.lastIndexOf(QLatin1Char(
'.'));
106 ret.truncate(suffixPos);
108 int pathPos = ret.lastIndexOf(QLatin1Char(
'/'));
110 ret.remove(0, pathPos + 1);
114 return d->soundsVec.at(
index.row());
116int SoundsPickerModel::rowCount(
const QModelIndex& parent)
const {
118 return d->soundsVec.size();
121const QStringList &SoundsPickerModel::defaultAudio()
const
123 return d->defaultAudio;
126void SoundsPickerModel::setDefaultAudio(
const QStringList &audio)
128 d->defaultAudio = audio;
129 rearrangeRingtoneOrder();
132const QString &SoundsPickerModel::theme()
const
137void SoundsPickerModel::setTheme(
const QString &theme)
139 if (d->theme == theme) {
147 rearrangeRingtoneOrder();
150void SoundsPickerModel::rearrangeRingtoneOrder()
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]);
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=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QStringList standardLocations(StandardLocation type)
QString writableLocation(StandardLocation type)