-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notification.cpp
178 lines (143 loc) · 5.19 KB
/
Notification.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Notification.h"
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QScreen>
#include <QGuiApplication>
#include <QTimer>
#include <QSettings>
#include <QInputDialog>
#include <QSystemTrayIcon>
#include <QFileInfo>
#include <QStandardPaths>
#include <QDir>
#include <QAction>
#include <QMenu>
#include <QDebug>
Notification::Notification(QWidget *parent)
: QWidget(parent)
{
setWindowFlag(Qt::X11BypassWindowManagerHint); // Shows on all desktops, always on top
setAttribute(Qt::WA_X11NetWmWindowTypeNotification); // For compton or whatever you call it, avoids active check e. g. for darkening
QVBoxLayout *layout = new QVBoxLayout;
setLayout(layout);
m_title = new QLabel("Title");
m_body = new QLabel("Body");
m_body->setTextFormat(Qt::RichText);
QPushButton *dismiss = new QPushButton("Dismiss");
QPushButton *quit = new QPushButton("Quit");
QHBoxLayout *quitLayout = new QHBoxLayout;
quitLayout->addStretch();
quitLayout->addWidget(quit);
layout->addLayout(quitLayout);
layout->addWidget(m_title);
layout->addWidget(m_body);
layout->addWidget(dismiss);
connect(dismiss, &QPushButton::clicked, this, &Notification::onDismiss);
connect(quit, &QPushButton::clicked, qGuiApp, &QGuiApplication::quit);
setMinimumSize(100, 100);
QMenu *trayMenu = new QMenu(this);
m_autostartAction = trayMenu->addAction("Enable autostart", this, &Notification::onToggleAutostart);
m_autostartAction->setCheckable(true);
m_autostartAction->setChecked(QFile::exists(autostartPath()));
trayMenu->addAction("Quit", qApp, &QCoreApplication::quit);
m_trayIcon = new QSystemTrayIcon(QIcon(":/icon.png"), this);
m_trayIcon->setContextMenu(trayMenu);
connect(m_trayIcon, &QSystemTrayIcon::activated, this, [this](const QSystemTrayIcon::ActivationReason reason) {
if (reason != QSystemTrayIcon::Context) {
show();
}
});
m_trayIcon->show();
QSettings settings;
if (!settings.contains("token")) {
settings.setValue("token", QInputDialog::getText(nullptr, "Need token", "Please enter an API token"));
}
}
Notification::~Notification()
{
}
void Notification::onNewEvent(const QString &title, const QString &body, const QString &id)
{
if (id == m_id && isVisible()) {
return;
}
m_id = id;
m_title->setText(title);
m_body->setText(body);
m_trayIcon->setIcon(QIcon(":/icon-active.png"));
show();
}
void Notification::onNoEvents()
{
m_trayIcon->setIcon(QIcon(":/icon.png"));
}
void Notification::showEvent(QShowEvent *showEvent)
{
adjustSize();
const QScreen *screen = QGuiApplication::primaryScreen();
QRect geometry = rect();
geometry.moveTop(0);
geometry.moveLeft(screen->size().width() - geometry.width());
setGeometry(geometry);
QWidget::showEvent(showEvent);
}
void Notification::onDismiss()
{
emit dismissed(m_id);
m_id.clear();
hide();
}
void Notification::onToggleAutostart()
{
if (QFile::exists(autostartPath())) {
disableAutostart();
} else {
enableAutostart();
}
m_autostartAction->setChecked(QFile::exists(autostartPath()));
}
void Notification::enableAutostart()
{
QFileInfo fileInfo(QCoreApplication::applicationFilePath());
#if defined(Q_OS_WIN)
const QString targetDir = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + QDir::separator() + "Startup" + QDir::separator();
QFile::link(fileInfo.canonicalFilePath(), autostartPath());
#elif defined(Q_OS_LINUX)
QSettings desktopFile(autostartPath(), QSettings::IniFormat);
desktopFile.beginGroup("Desktop Entry");
desktopFile.setValue("Exec", fileInfo.canonicalFilePath());
#elif defined(Q_OS_MACOS)
const QString targetDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) +"Library/LaunchAgents/";
QSettings plist(autostartPath(), QSettings::NativeFormat); // TODO check how it encodes QStringList and bools
plist.setValue("Label", QCoreApplication::applicationName());
plist.setValue("ProgramArguments", QStringList({fileInfo.canonicalFilePath()}));
plist.setValue("ProcessType", "Interactive");
plist.setValue("RunAtLoad", true);
plist.setValue("KeepAlive", false);
#else
qWarning() << "Unsupported OS";
#endif
}
void Notification::disableAutostart()
{
QFile::remove(autostartPath());
}
QString Notification::autostartPath()
{
QFileInfo fileInfo(QCoreApplication::applicationFilePath());
#if defined(Q_OS_WIN)
const QString targetDir = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation) + QDir::separator() + "Startup" + QDir::separator();
return targetDir + fileInfo.completeBaseName() + ".lnk";
#elif defined(Q_OS_LINUX)
const QString targetDir = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + "/autostart/";
QDir().mkpath(targetDir);
return targetDir + fileInfo.completeBaseName() + ".desktop";
#elif defined(Q_OS_MACOS)
const QString targetDir = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) + "Library/LaunchAgents/";
return targetDir + fileInfo.completeBaseName() + ".plist";
#else
#error "Unsupported OS";
#endif
}