Add desktop file interface

This commit is contained in:
reionwong 2021-08-29 19:46:37 +08:00
parent b0864bff80
commit 943a02d3e9
7 changed files with 88 additions and 1 deletions

View file

@ -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

View file

@ -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<QString, QString> 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)) {

View file

@ -47,6 +47,9 @@ public:
QHash<int, QByteArray> 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();

View file

@ -22,6 +22,7 @@
#include <QQuickView>
#include <QTranslator>
#include <QLocale>
#include <QDBusConnection>
#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<DockSettings>("Cutefish.Dock", 1, 0, "DockSettings");
qmlRegisterType<DockBackground>("Cutefish.Dock", 1, 0, "DockBackground");

View file

@ -19,6 +19,7 @@
#include "mainwindow.h"
#include "processprovider.h"
#include "dockadaptor.h"
#include <QGuiApplication>
#include <QScreen>
@ -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();

View file

@ -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:

View file

@ -0,0 +1,7 @@
<!DOCTYPE node PUBLIC "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd">
<node>
<interface name="org.cutefish.Dock">
<method name="add"><arg name="desktopFile" type="s" direction="in"/></method>
<method name="remove"><arg name="desktopFile" type="s" direction="in"/></method>
</interface>
</node>