feat: live update items.

pull/16/head
kateleet 5 years ago
parent 983e449507
commit b2df7858b7

@ -202,7 +202,7 @@ Item {
width: 6
height: 6
visible: model.newInstalled
visible: !dragStarted && model.newInstalled
anchors {
horizontalCenter: parent.horizontalCenter

@ -46,6 +46,7 @@ static QByteArray detectDesktopEnvironment()
LauncherModel::LauncherModel(QObject *parent)
: QAbstractListModel(parent)
, m_fileWatcher(new QFileSystemWatcher(this))
, m_settings("cutefishos", "launcher-applist", this)
, m_mode(NormalMode)
, m_firstLoad(false)
@ -60,9 +61,9 @@ LauncherModel::LauncherModel(QObject *parent)
QtConcurrent::run(LauncherModel::refresh, this);
QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
watcher->addPath("/usr/share/applications");
connect(watcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString &) {
m_fileWatcher->addPath("/usr/share/applications");
connect(m_fileWatcher, &QFileSystemWatcher::fileChanged, this, &LauncherModel::onFileChanged);
connect(m_fileWatcher, &QFileSystemWatcher::directoryChanged, this, [this](const QString &) {
QtConcurrent::run(LauncherModel::refresh, this);
});
@ -234,8 +235,7 @@ void LauncherModel::refresh(LauncherModel *manager)
}
for (const QString &fileName : allEntries) {
//if (!addedEntries.contains(fileName))
QMetaObject::invokeMethod(manager, "addApp", Q_ARG(QString, fileName));
QMetaObject::invokeMethod(manager, "addApp", Q_ARG(QString, fileName));
}
for (const AppItem &item : qAsConst(manager->m_appItems))
@ -330,9 +330,39 @@ void LauncherModel::onRefreshed()
delaySave();
}
void LauncherModel::onFileChanged(const QString &path)
{
int index = findById(path);
if (index == 0) {
return;
}
AppItem &item = m_appItems[index];
DesktopProperties desktop(item.id, "Desktop Entry");
QString appName = desktop.value(QString("Name[%1]").arg(QLocale::system().name())).toString();
QString appExec = desktop.value("Exec").toString();
// Update datas.
if (appName.isEmpty())
appName = desktop.value("Name").toString();
appExec.remove(QRegularExpression("%."));
appExec.remove(QRegularExpression("^\""));
appExec = appExec.replace("\"", "");
appExec = appExec.simplified();
item.name = appName;
item.genericName = desktop.value("Comment").toString();
item.comment = desktop.value("Comment").toString();
item.iconName = desktop.value("Icon").toString();
item.args = appExec.split(" ");
emit dataChanged(LauncherModel::index(index), LauncherModel::index(index));
}
void LauncherModel::addApp(const QString &fileName)
{
int index = findById(fileName) ;
int index = findById(fileName);
DesktopProperties desktop(fileName, "Desktop Entry");
@ -383,13 +413,17 @@ void LauncherModel::addApp(const QString &fileName)
beginInsertRows(QModelIndex(), m_appItems.count(), m_appItems.count());
m_appItems.append(appItem);
qDebug() << "added: " << appItem.name;
qDebug() << "added: " << appItem.name << appItem.newInstalled;
endInsertRows();
if (!m_firstLoad) {
delaySave();
}
}
// Update desktop files.
if (!m_fileWatcher->files().contains(fileName))
m_fileWatcher->addPath(fileName);
}
void LauncherModel::removeApp(const QString &fileName)
@ -403,4 +437,8 @@ void LauncherModel::removeApp(const QString &fileName)
endRemoveRows();
delaySave();
// Remove
if (m_fileWatcher->files().contains(fileName))
m_fileWatcher->removePath(fileName);
}

@ -21,6 +21,7 @@
#define LAUNCHERMODEL_H
#include <QObject>
#include <QFileSystemWatcher>
#include <QLoggingCategory>
#include <QAbstractListModel>
#include <QSettings>
@ -88,6 +89,7 @@ Q_SIGNALS:
private Q_SLOTS:
void onRefreshed();
void onFileChanged(const QString &path);
void addApp(const QString &fileName);
void removeApp(const QString &fileName);
@ -95,6 +97,8 @@ private:
QList<AppItem> m_appItems;
QList<AppItem> m_searchItems;
QFileSystemWatcher *m_fileWatcher;
QTimer m_saveTimer;
QSettings m_settings;
Mode m_mode;

Loading…
Cancel
Save