| 1 |
/* |
| 2 |
* Copyright (C) 2023 Ubports Foundation |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; version 3. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
*/ |
| 16 |
|
| 17 |
#include "ringtonemodel.h" |
| 18 |
|
| 19 |
#include <QDir> |
| 20 |
#include <QFileInfo> |
| 21 |
#include <QStandardPaths> |
| 22 |
#include <QContactRingtone> |
| 23 |
#include <QDebug> |
| 24 |
|
| 25 |
#define DEFAULT_SOUND_DIR "sounds/lomiri/ringtones/" |
| 26 |
#define DEFAULT_NOTIFICATION_DIR "sounds/lomiri/notifications/" |
| 27 |
#define CONTACT_SOUND_DIR "sounds/ringtones" |
| 28 |
|
| 29 |
bool sortRingtones(const Ringtone &s1, const Ringtone &s2) |
| 30 |
{ |
| 31 |
return s1.name() < s2.name(); |
| 32 |
} |
| 33 |
|
| 34 |
QString fileName(const QString &path) |
| 35 |
{ |
| 36 |
QFileInfo fileInfo(path); |
| 37 |
QString name = fileInfo.baseName().replace(QRegExp("[._-]"), " "); |
| 38 |
return name.at(0).toUpper() + name.mid(1); |
| 39 |
} |
| 40 |
|
| 41 |
QList<Ringtone> ringtonesFromDir(const QString &dirString) |
| 42 |
{ |
| 43 |
QList<Ringtone> ringtones; |
| 44 |
QDir soundsDir(dirString); |
| 45 |
bool readOnly = false; |
| 46 |
if (soundsDir.isRelative()) { |
| 47 |
readOnly = true; |
| 48 |
QString path = QStandardPaths::locate( |
| 49 |
QStandardPaths::GenericDataLocation, dirString, |
| 50 |
QStandardPaths::LocateDirectory |
| 51 |
); |
| 52 |
if (path.isEmpty()) { |
| 53 |
return ringtones; |
| 54 |
} |
| 55 |
soundsDir = QDir(path); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
if (soundsDir.exists()) |
| 60 |
{ |
| 61 |
soundsDir.setFilter(QDir::Files | QDir::NoSymLinks); |
| 62 |
|
| 63 |
for (uint i=0; i < soundsDir.count(); i++) { |
| 64 |
ringtones << Ringtone(fileName(soundsDir[i]),soundsDir.absoluteFilePath(soundsDir[i]), readOnly ); |
| 65 |
} |
| 66 |
} |
| 67 |
return ringtones; |
| 68 |
} |
| 69 |
|
| 70 |
Ringtone::Ringtone(const QString &name, const QString &path, bool readonly) |
| 71 |
: mName(name), mPath(path), mReadonly(readonly) |
| 72 |
{ |
| 73 |
} |
| 74 |
|
| 75 |
QString Ringtone::name() const |
| 76 |
{ |
| 77 |
return mName; |
| 78 |
} |
| 79 |
|
| 80 |
QString Ringtone::path() const |
| 81 |
{ |
| 82 |
return mPath; |
| 83 |
} |
| 84 |
|
| 85 |
bool Ringtone::readonly() const |
| 86 |
{ |
| 87 |
return mReadonly; |
| 88 |
} |
| 89 |
|
| 90 |
RingtoneModel::RingtoneModel(QObject *parent) : QAbstractListModel(parent) |
| 91 |
{ |
| 92 |
|
| 93 |
QString genericDataLocation = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); |
| 94 |
|
| 95 |
mAudioRingtonePath = QString(genericDataLocation + "/" + CONTACT_SOUND_DIR + "/audio"); |
| 96 |
QDir dir(mAudioRingtonePath); |
| 97 |
if (!dir.exists()) |
| 98 |
dir.mkpath("."); |
| 99 |
|
| 100 |
mVideoRingtonePath = QString(genericDataLocation + "/" + CONTACT_SOUND_DIR + "/video"); |
| 101 |
QDir dirv(mVideoRingtonePath); |
| 102 |
if (!dirv.exists()) |
| 103 |
dirv.mkpath("."); |
| 104 |
|
| 105 |
mVibrationRingtonePath = QString(genericDataLocation + "/" + CONTACT_SOUND_DIR + "/notification"); |
| 106 |
QDir dirm(mVibrationRingtonePath); |
| 107 |
if (!dirm.exists()) |
| 108 |
dirm.mkpath("."); |
| 109 |
|
| 110 |
connect(this, SIGNAL(ringtoneTypeChanged()), SLOT(populatesData())); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
void RingtoneModel::populatesData() |
| 115 |
{ |
| 116 |
QtContacts::QContactRingtone::RingtoneField type = (QtContacts::QContactRingtone::RingtoneField) mRingToneType; |
| 117 |
QStringList sounds; |
| 118 |
QStringList soundDirs; |
| 119 |
if (QtContacts::QContactRingtone::RingtoneField::FieldAudioRingtoneUrl == type) { |
| 120 |
soundDirs << DEFAULT_SOUND_DIR << mAudioRingtonePath; |
| 121 |
} else if (QtContacts::QContactRingtone::RingtoneField::FieldVideoRingtoneUrl == type) { |
| 122 |
soundDirs << DEFAULT_SOUND_DIR << mVideoRingtonePath; |
| 123 |
} else if (QtContacts::QContactRingtone::RingtoneField::FieldVibrationRingtoneUrl == type) { |
| 124 |
soundDirs << DEFAULT_NOTIFICATION_DIR << mVibrationRingtonePath; |
| 125 |
} |
| 126 |
|
| 127 |
qDebug() << "Ringtone paths:" << soundDirs; |
| 128 |
|
| 129 |
beginResetModel(); |
| 130 |
mRingtones.clear(); |
| 131 |
endResetModel(); |
| 132 |
|
| 133 |
QList<Ringtone> ringtones; |
| 134 |
for (int i = 0; i < soundDirs.size(); ++i) { |
| 135 |
QList<Ringtone> tmpRingtones = ringtonesFromDir(soundDirs[i]); |
| 136 |
qSort(tmpRingtones.begin(), tmpRingtones.end(), sortRingtones); |
| 137 |
ringtones << tmpRingtones; |
| 138 |
} |
| 139 |
|
| 140 |
beginInsertRows(QModelIndex(), 0, ringtones.count() - 1); |
| 141 |
mRingtones.append(ringtones); |
| 142 |
endInsertRows(); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
void RingtoneModel::setRingToneType(int ringToneType) |
| 147 |
{ |
| 148 |
mRingToneType = ringToneType; |
| 149 |
Q_EMIT ringtoneTypeChanged(); |
| 150 |
} |
| 151 |
|
| 152 |
int RingtoneModel::getIndex(const QString &path) |
| 153 |
{ |
| 154 |
for (int i = 0; i < mRingtones.count(); i++) { |
| 155 |
if (mRingtones[i].path() == path) { |
| 156 |
return i; |
| 157 |
} |
| 158 |
} |
| 159 |
return -1; |
| 160 |
} |
| 161 |
|
| 162 |
QString RingtoneModel::soundPath(int ringtoneField) |
| 163 |
{ |
| 164 |
QtContacts::QContactRingtone::RingtoneField ringToneType = (QtContacts::QContactRingtone::RingtoneField) ringtoneField; |
| 165 |
|
| 166 |
if (QtContacts::QContactRingtone::RingtoneField::FieldAudioRingtoneUrl == ringToneType) { |
| 167 |
return mAudioRingtonePath; |
| 168 |
} else if (QtContacts::QContactRingtone::RingtoneField::FieldVideoRingtoneUrl == ringToneType) { |
| 169 |
return mVideoRingtonePath; |
| 170 |
} else if (QtContacts::QContactRingtone::RingtoneField::FieldVibrationRingtoneUrl == ringToneType) { |
| 171 |
return mVibrationRingtonePath; |
| 172 |
} else { |
| 173 |
return ""; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void RingtoneModel::addFile(const QString &file) |
| 178 |
{ |
| 179 |
beginInsertRows(QModelIndex(), 0, 0); |
| 180 |
mRingtones << Ringtone( fileName(file), file, false); |
| 181 |
qSort(mRingtones.begin(), mRingtones.end(), sortRingtones); |
| 182 |
endInsertRows(); |
| 183 |
} |
| 184 |
|
| 185 |
void RingtoneModel::removeFile(const QString &path) |
| 186 |
{ |
| 187 |
int index = getIndex(path); |
| 188 |
if (index > -1) { |
| 189 |
QFile file (path); |
| 190 |
beginRemoveRows(QModelIndex(), index, index); |
| 191 |
if (file.remove()) { |
| 192 |
mRingtones.removeAt(index); |
| 193 |
} else { |
| 194 |
qWarning() << "error when trying to remove" << path; |
| 195 |
} |
| 196 |
|
| 197 |
endRemoveRows(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
QHash<int, QByteArray> RingtoneModel::roleNames() const |
| 202 |
{ |
| 203 |
static QHash<int, QByteArray> roles; |
| 204 |
if (roles.isEmpty()) { |
| 205 |
roles[Name] = "name"; |
| 206 |
roles[Path] = "path"; |
| 207 |
roles[ReadOnly] = "readOnly"; |
| 208 |
} |
| 209 |
return roles; |
| 210 |
} |
| 211 |
|
| 212 |
int RingtoneModel::rowCount(const QModelIndex &parent) const |
| 213 |
{ |
| 214 |
Q_UNUSED(parent); |
| 215 |
return mRingtones.count(); |
| 216 |
} |
| 217 |
|
| 218 |
QVariant RingtoneModel::data(const QModelIndex & index, int role) const |
| 219 |
{ |
| 220 |
if (index.row() < 0 || index.row() >= mRingtones.count()) |
| 221 |
return QVariant(); |
| 222 |
|
| 223 |
const Ringtone &ringtone = mRingtones[index.row()]; |
| 224 |
if (role == Name) |
| 225 |
return QVariant::fromValue(ringtone.name()); |
| 226 |
else if (role == Path) |
| 227 |
return QVariant::fromValue(ringtone.path()); |
| 228 |
else if (role == ReadOnly) { |
| 229 |
return QVariant::fromValue(ringtone.readonly()); |
| 230 |
} else { |
| 231 |
return QVariant(); |
| 232 |
} |
| 233 |
} |