-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
310 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "profilehelper.h" | ||
|
||
#include <QUuid> | ||
|
||
ProfileHelper::ProfileHelper(QObject* parent) : | ||
QObject{parent} { | ||
} | ||
|
||
QString ProfileHelper::newProfileUuid() { | ||
return QUuid::createUuid().toString(QUuid::WithoutBraces); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef PROFILEHELPER_H | ||
#define PROFILEHELPER_H | ||
|
||
#include <QObject> | ||
#include <QQmlEngine> | ||
|
||
class ProfileHelper : public QObject { | ||
Q_OBJECT | ||
QML_ELEMENT | ||
public: | ||
explicit ProfileHelper(QObject* parent = nullptr); | ||
|
||
Q_SCRIPTABLE QString newProfileUuid(); | ||
|
||
signals: | ||
}; | ||
|
||
#endif // PROFILEHELPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "profilesmodel.h" | ||
|
||
#include <QDir> | ||
#include <QFileSystemWatcher> | ||
#include <QStandardPaths> | ||
|
||
struct ProfilesModelPrivate { | ||
QStringList profileUuids; | ||
QFileSystemWatcher* watcher; | ||
}; | ||
|
||
ProfilesModel::ProfilesModel(QObject* parent) : | ||
QAbstractListModel(parent), d{new ProfilesModelPrivate()} { | ||
auto profilesDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).absoluteFilePath("profiles"); | ||
|
||
if (!QDir(profilesDir).exists()) { | ||
QDir::root().mkpath(profilesDir); | ||
} | ||
|
||
d->watcher = new QFileSystemWatcher(this); | ||
d->watcher->addPath(profilesDir); | ||
connect(d->watcher, &QFileSystemWatcher::directoryChanged, this, &ProfilesModel::updateProfiles); | ||
|
||
updateProfiles(); | ||
} | ||
|
||
ProfilesModel::~ProfilesModel() { | ||
delete d; | ||
} | ||
|
||
int ProfilesModel::rowCount(const QModelIndex& parent) const { | ||
if (parent.isValid()) | ||
return 0; | ||
|
||
return d->profileUuids.length(); | ||
} | ||
|
||
QVariant ProfilesModel::data(const QModelIndex& index, int role) const { | ||
if (!index.isValid()) | ||
return {}; | ||
|
||
auto uuid = d->profileUuids.at(index.row()); | ||
switch (role) { | ||
case Roles::UuidRole: | ||
return uuid; | ||
} | ||
return {}; | ||
} | ||
|
||
void ProfilesModel::updateProfiles() { | ||
QStringList foundProfiles; | ||
|
||
QDir profilesDir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).absoluteFilePath("profiles"); | ||
for (auto profile : profilesDir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot)) { | ||
foundProfiles.append(profile.baseName()); | ||
if (!d->profileUuids.contains(profile.baseName())) { | ||
beginInsertRows({}, d->profileUuids.length(), d->profileUuids.length()); | ||
d->profileUuids.append(profile.baseName()); | ||
endInsertRows(); | ||
} | ||
} | ||
|
||
for (auto i = 0; i < d->profileUuids.length(); i++) { | ||
if (!foundProfiles.contains(d->profileUuids.at(i))) { | ||
beginRemoveRows({}, i, i); | ||
d->profileUuids.removeAt(i); | ||
endRemoveRows(); | ||
i--; | ||
} | ||
} | ||
} | ||
|
||
QHash<int, QByteArray> ProfilesModel::roleNames() const { | ||
return { | ||
{Roles::UuidRole, "uuid"} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef PROFILESMODEL_H | ||
#define PROFILESMODEL_H | ||
|
||
#include <QAbstractListModel> | ||
#include <QQmlEngine> | ||
|
||
struct ProfilesModelPrivate; | ||
class ProfilesModel : public QAbstractListModel { | ||
Q_OBJECT | ||
QML_ELEMENT | ||
|
||
public: | ||
explicit ProfilesModel(QObject* parent = nullptr); | ||
~ProfilesModel(); | ||
|
||
enum Roles { | ||
UuidRole = Qt::UserRole | ||
}; | ||
|
||
// Basic functionality: | ||
int rowCount(const QModelIndex& parent = QModelIndex()) const override; | ||
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; | ||
|
||
private: | ||
ProfilesModelPrivate* d; | ||
|
||
void updateProfiles(); | ||
|
||
// QAbstractItemModel interface | ||
public: | ||
QHash<int, QByteArray> roleNames() const override; | ||
}; | ||
|
||
#endif // PROFILESMODEL_H |
Oops, something went wrong.