From 943a02d3e912f1a58c5eeff91ca24a36713fafd6 Mon Sep 17 00:00:00 2001 From: reionwong Date: Sun, 29 Aug 2021 19:46:37 +0800 Subject: [PATCH] Add desktop file interface --- CMakeLists.txt | 7 +++++- src/applicationmodel.cpp | 47 +++++++++++++++++++++++++++++++++++++++ src/applicationmodel.h | 5 +++++ src/main.cpp | 6 +++++ src/mainwindow.cpp | 14 ++++++++++++ src/mainwindow.h | 3 +++ src/org.Cutefish.Dock.xml | 7 ++++++ 7 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/org.Cutefish.Dock.xml diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f614ae..a99a7f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,7 +38,12 @@ set(RESOURCES resources.qrc ) -add_executable(${PROJECT_NAME} ${SRCS} ${DBUS_SRCS} ${RESOURCES}) +qt5_add_dbus_adaptor(DBUS_SOURCES + src/org.Cutefish.Dock.xml + src/mainwindow.h MainWindow) +set_source_files_properties(${DBUS_SOURCES} PROPERTIES SKIP_AUTOGEN ON) + +add_executable(${PROJECT_NAME} ${SRCS} ${DBUS_SOURCES} ${RESOURCES}) target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Core Qt5::Widgets diff --git a/src/applicationmodel.cpp b/src/applicationmodel.cpp index e839152..7039ebb 100644 --- a/src/applicationmodel.cpp +++ b/src/applicationmodel.cpp @@ -88,6 +88,43 @@ QVariant ApplicationModel::data(const QModelIndex &index, int role) const return QVariant(); } +void ApplicationModel::addItem(const QString &desktopFile) +{ + if (findItemByDesktop(desktopFile)) + return; + + beginInsertRows(QModelIndex(), rowCount(), rowCount()); + ApplicationItem *item = new ApplicationItem; + QMap desktopInfo = Utils::instance()->readInfoFromDesktop(desktopFile); + item->iconName = desktopInfo.value("Icon"); + item->visibleName = desktopInfo.value("Name"); + item->exec = desktopInfo.value("Exec"); + item->desktopPath = desktopFile; + m_appItems << item; + endInsertRows(); + + savePinAndUnPinList(); +} + +void ApplicationModel::removeItem(const QString &desktopFile) +{ + ApplicationItem *item = findItemByDesktop(desktopFile); + + if (item) { + int index = indexOf(item->id); + + if (index != -1) { + beginRemoveRows(QModelIndex(), index, index); + m_appItems.removeAll(item); + endRemoveRows(); + savePinAndUnPinList(); + + emit itemRemoved(); + emit countChanged(); + } + } +} + void ApplicationModel::clicked(const QString &id) { ApplicationItem *item = findItemById(id); @@ -252,6 +289,16 @@ ApplicationItem *ApplicationModel::findItemById(const QString &id) return nullptr; } +ApplicationItem *ApplicationModel::findItemByDesktop(const QString &desktop) +{ + for (ApplicationItem *item : m_appItems) { + if (item->desktopPath == desktop) + return item; + } + + return nullptr; +} + bool ApplicationModel::contains(const QString &id) { for (ApplicationItem *item : qAsConst(m_appItems)) { diff --git a/src/applicationmodel.h b/src/applicationmodel.h index cad5243..9c9bfe6 100644 --- a/src/applicationmodel.h +++ b/src/applicationmodel.h @@ -47,6 +47,9 @@ public: QHash roleNames() const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + void addItem(const QString &desktopFile); + void removeItem(const QString &desktopFile); + Q_INVOKABLE void save() { savePinAndUnPinList(); } Q_INVOKABLE void clicked(const QString &id); @@ -70,6 +73,8 @@ signals: private: ApplicationItem *findItemByWId(quint64 wid); ApplicationItem *findItemById(const QString &id); + ApplicationItem *findItemByDesktop(const QString &desktop); + bool contains(const QString &id); int indexOf(const QString &id); void initPinnedApplications(); diff --git a/src/main.cpp b/src/main.cpp index a62d346..3957b26 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "applicationmodel.h" #include "dockbackground.h" @@ -33,6 +34,11 @@ int main(int argc, char *argv[]) QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true); QApplication app(argc, argv); + if (!QDBusConnection::sessionBus().registerService("org.cutefish.Dock")) { + app.exit(); + return 0; + } + qmlRegisterType("Cutefish.Dock", 1, 0, "DockSettings"); qmlRegisterType("Cutefish.Dock", 1, 0, "DockBackground"); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9e7c173..c6906a1 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -19,6 +19,7 @@ #include "mainwindow.h" #include "processprovider.h" +#include "dockadaptor.h" #include #include @@ -44,6 +45,9 @@ MainWindow::MainWindow(QQuickView *parent) , m_showTimer(new QTimer(this)) , m_hideTimer(new QTimer(this)) { + new DockAdaptor(this); + QDBusConnection::sessionBus().registerObject(QStringLiteral("/Dock"), this); + setDefaultAlphaBuffer(false); setColor(Qt::transparent); @@ -91,6 +95,16 @@ MainWindow::~MainWindow() { } +void MainWindow::add(const QString &desktop) +{ + m_appModel->addItem(desktop); +} + +void MainWindow::remove(const QString &desktop) +{ + m_appModel->removeItem(desktop); +} + void MainWindow::updateSize() { resizeWindow(); diff --git a/src/mainwindow.h b/src/mainwindow.h index d3af43d..eabfaf3 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -37,6 +37,9 @@ public: explicit MainWindow(QQuickView *parent = nullptr); ~MainWindow(); + void add(const QString &desktop); + void remove(const QString &desktop); + Q_INVOKABLE void updateSize(); signals: diff --git a/src/org.Cutefish.Dock.xml b/src/org.Cutefish.Dock.xml new file mode 100644 index 0000000..e937e91 --- /dev/null +++ b/src/org.Cutefish.Dock.xml @@ -0,0 +1,7 @@ + + + + + + +