Commit b41a165f authored by LordTermor's avatar LordTermor
Browse files

Initial commit

parents
cmake_minimum_required(VERSION 3.5)
project(pamac-tray-plasma LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Widgets)
find_package(PkgConfig)
add_definitions(-DQT_NO_KEYWORDS)
pkg_check_modules(DEPS REQUIRED libnotify gdk-pixbuf-2.0 pamac)
add_executable(pamac-tray-plasma
main.cpp
pamac_tray.cpp
pamac_tray.h
)
target_include_directories(pamac-tray-plasma PUBLIC ${DEPS_INCLUDE_DIRS})
target_link_libraries(pamac-tray-plasma Qt5::Core Qt5::Widgets ${DEPS_LDFLAGS})
This diff is collapsed.
#include <QApplication>
#include <libnotify/notify.h>
#include <pamac_tray.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
notify_init("Package Manager");
PamacTray tray;
tray.show();
return a.exec();
}
#include "pamac_tray.h"
#include <QProcess>
#include <QAction>
#include <QApplication>
void PamacTray::execute_updater()
{
QProcess::startDetached("pamac-manager", {"--updates"});
}
void PamacTray::execute_manager()
{
QProcess::startDetached("pamac-manager", {{}});
}
void PamacTray::show_notification(QString info)
{
close_notification();
m_notification = notify_notification_new(tr("Package Manager").toUtf8(), info.toUtf8(), "system-software-install-symbolic");
notify_notification_set_timeout(m_notification, NOTIFY_EXPIRES_DEFAULT);
notify_notification_add_action(m_notification, "default", tr("Details").toUtf8(),+[](NotifyNotification*, char*, void* selfPtr){
auto self = reinterpret_cast<PamacTray*>(selfPtr);
self->execute_updater();
},this,nullptr);
notify_notification_show(m_notification,nullptr);
}
void PamacTray::show_or_update_notification(uint updates_nb)
{
QString info = tr("%n available update(s)", "", updates_nb);
setIcon(QIcon::fromTheme("pamac-tray-update"));
setToolTip(info);
setVisible(true);
update_notification(info);
}
void PamacTray::update_notification(QString info)
{
if(m_notification!=nullptr){
if(notify_notification_get_closed_reason(m_notification) == -1)
notify_notification_update(m_notification, tr("Package Manager").toUtf8(),
info.toUtf8(), "system-software-install-symbolic");
notify_notification_show(m_notification,nullptr);
} else{
show_notification(info);
}
}
void PamacTray::close_notification()
{
notify_notification_close(m_notification,nullptr);
m_notification = nullptr;
}
QMenu* PamacTray::create_menu()
{
auto menu = new QMenu();
auto action = new QAction(menu);
action->setText(tr("Package Manager"));
connect(action, &QAction::triggered, this, &PamacTray::execute_manager);
menu->addAction(action);
action = new QAction(menu);
action->setText(tr("Quit"));
connect(action, &QAction::triggered, [](){QApplication::quit();});
menu->addAction(action);
return menu;
}
#pragma once
#include <QSystemTrayIcon>
#include <QMenu>
#include <QTimer>
#include <pamac.h>
#include <glib.h>
#include <QTimerEvent>
#include <libnotify/notification.h>
class PamacTray : public QSystemTrayIcon
{
Q_OBJECT
public:
PamacTray(QObject* parent = nullptr):QSystemTrayIcon(QIcon::fromTheme("pamac-tray-no-update"),parent),
m_updatesChecker(pamac_updates_checker_new())
{
setToolTip("Pamac tray");
this->setContextMenu(create_menu());
connect(this, &QSystemTrayIcon::activated, this, &PamacTray::execute_manager);
g_signal_connect(m_updatesChecker, "updates_available", reinterpret_cast<GCallback>(+[](PamacUpdatesChecker* checker, int updates_nb, void* selfPtr){
auto self = reinterpret_cast<PamacTray*>(selfPtr);
if(updates_nb == 0){
self->setIcon(QIcon::fromTheme("pamac-tray-no-update"));
self->setToolTip(tr("Your system is up-to-date"));
self->setVisible(!pamac_updates_checker_get_no_update_hide_icon(checker));
self->close_notification();
} else{
self->show_or_update_notification(updates_nb);
}
}), this);
QTimer::singleShot(30 * 1000, this, [=](){
pamac_updates_checker_check_updates(m_updatesChecker);
});
updateCheckerTimerId = startTimer(3600*1000);
}
void timerEvent(QTimerEvent* e) override {
if (e->timerId() == updateCheckerTimerId){
pamac_updates_checker_check_updates(m_updatesChecker);
}
}
private Q_SLOTS:
void execute_updater();
void execute_manager();
void show_notification(QString info);
void show_or_update_notification(uint updates_nb);
void update_notification(QString info);
void close_notification();
private:
int updateCheckerTimerId;
PamacUpdatesChecker* m_updatesChecker = nullptr;
NotifyNotification* m_notification = nullptr;
QMenu* create_menu();
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment