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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
|
#include "./trayicon.h"
#include "./traywidget.h"
#include <syncthingwidgets/misc/internalerrorsdialog.h>
#include <syncthingwidgets/misc/statusinfo.h>
#include <syncthingwidgets/misc/textviewdialog.h>
#include <syncthingwidgets/settings/settings.h>
#include <syncthingmodel/syncthingicons.h>
#include <syncthingconnector/syncthingconnection.h>
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
#include <syncthingconnector/syncthingservice.h>
#endif
#include <syncthingconnector/utils.h>
#include <qtutilities/misc/dialogutils.h>
#include <QCoreApplication>
#include <QPainter>
#include <QPixmap>
#include <QStringBuilder>
#ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD
#include <QNetworkReply>
#endif
using namespace std;
using namespace QtUtilities;
using namespace Data;
namespace QtGui {
/*!
* \brief Instantiates a new tray icon.
*/
TrayIcon::TrayIcon(const QString &connectionConfig, QObject *parent)
: QSystemTrayIcon(parent)
, m_trayMenu(new TrayMenu(this, &m_parentWidget))
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
, m_dbusNotificationsEnabled(Settings::values().dbusNotifications)
#endif
, m_notifyOnSyncthingErrors(Settings::values().notifyOn.syncthingErrors)
, m_messageClickedAction(TrayIconMessageClickedAction::None)
{
// get widget, connection and notifier
const auto &widget(trayMenu().widget());
const auto &connection(widget.connection());
const auto ¬ifier(widget.notifier());
// set context menu
#ifndef SYNCTHINGTRAY_UNIFY_TRAY_MENUS
connect(m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("syncthing"), QIcon(QStringLiteral(":/icons/hicolor/scalable/status/syncthing-default.svg"))),
tr("Open Syncthing")),
&QAction::triggered, &widget, &TrayWidget::showWebUI);
connect(m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("preferences-other"), QIcon(QStringLiteral(":/icons/hicolor/scalable/apps/preferences-other.svg"))),
tr("Settings")),
&QAction::triggered, &widget, &TrayWidget::showSettingsDialog);
connect(m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("folder-sync"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/folder-sync.svg"))),
tr("Rescan all")),
&QAction::triggered, &widget.connection(), &SyncthingConnection::rescanAllDirs);
connect(m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("text-x-generic"), QIcon(QStringLiteral(":/icons/hicolor/scalable/mimetypes/text-x-generic.svg"))),
tr("Log")),
&QAction::triggered, &widget, &TrayWidget::showLog);
m_errorsAction = m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("emblem-error"), QIcon(QStringLiteral(":/icons/hicolor/scalable/emblems/8/emblem-error.svg"))),
tr("Show internal errors"));
m_errorsAction->setVisible(false);
connect(m_errorsAction, &QAction::triggered, this, &TrayIcon::showInternalErrorsDialog);
auto *const notificationsAction = m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("emblem-warning"), QIcon(QStringLiteral(":/icons/hicolor/scalable/emblems/8/emblem-warning.svg"))),
tr("Show notifications/errors"));
notificationsAction->setVisible(widget.connection().hasErrors());
connect(&widget.connection(), &SyncthingConnection::newErrors, notificationsAction,
[notificationsAction](const std::vector<SyncthingError> &errors) { notificationsAction->setVisible(!errors.empty()); });
connect(notificationsAction, &QAction::triggered, &widget, &TrayWidget::showNotifications);
m_contextMenu.addMenu(trayMenu().widget().connectionsMenu());
connect(m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("help-about"), QIcon(QStringLiteral(":/icons/hicolor/scalable/apps/help-about.svg"))), tr("About")),
&QAction::triggered, &widget, &TrayWidget::showAboutDialog);
m_contextMenu.addSeparator();
connect(m_contextMenu.addAction(
QIcon::fromTheme(QStringLiteral("window-close"), QIcon(QStringLiteral(":/icons/hicolor/scalable/actions/window-close.svg"))),
tr("Close")),
&QAction::triggered, this, &TrayIcon::deleteLater);
setContextMenu(&m_contextMenu);
#endif
// connect signals and slots
connect(this, &TrayIcon::activated, this, &TrayIcon::handleActivated);
connect(this, &TrayIcon::messageClicked, this, &TrayIcon::handleMessageClicked);
connect(&connection, &SyncthingConnection::error, this, &TrayIcon::showInternalError);
connect(&connection, &SyncthingConnection::newNotification, this, &TrayIcon::showSyncthingNotification);
connect(¬ifier, &SyncthingNotifier::syncthingProcessError, this, &TrayIcon::showLauncherError);
connect(¬ifier, &SyncthingNotifier::disconnected, this, &TrayIcon::showDisconnected);
connect(¬ifier, &SyncthingNotifier::syncComplete, this, &TrayIcon::showSyncComplete);
connect(¬ifier, &SyncthingNotifier::newDevice, this, &TrayIcon::showNewDev);
connect(¬ifier, &SyncthingNotifier::newDir, this, &TrayIcon::showNewDir);
connect(&connection, &SyncthingConnection::statusChanged, this, &TrayIcon::updateStatusIconAndText);
connect(&connection, &SyncthingConnection::autoReconnectIntervalChanged, this, &TrayIcon::updateStatusIconAndText);
connect(&connection, &SyncthingConnection::hasOutOfSyncDirsChanged, this, &TrayIcon::updateStatusIconAndText);
connect(&connection, &SyncthingConnection::newDevices, this, &TrayIcon::updateStatusIconAndText);
connect(&connection, &SyncthingConnection::devStatusChanged, this, &TrayIcon::updateStatusIconAndText);
connect(&IconManager::instance(), &IconManager::statusIconsChanged, this, &TrayIcon::updateStatusIconAndText);
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
connect(&m_dbusNotifier, &DBusStatusNotifier::connectRequested, &connection,
static_cast<void (SyncthingConnection::*)(void)>(&SyncthingConnection::connect));
connect(&m_dbusNotifier, &DBusStatusNotifier::dismissNotificationsRequested, &connection, &SyncthingConnection::requestClearingErrors);
connect(&m_dbusNotifier, &DBusStatusNotifier::showNotificationsRequested, &widget, &TrayWidget::showNotifications);
connect(&m_dbusNotifier, &DBusStatusNotifier::errorDetailsRequested, this, &TrayIcon::showInternalErrorsDialog);
connect(&m_dbusNotifier, &DBusStatusNotifier::webUiRequested, &widget, &TrayWidget::showWebUI);
connect(¬ifier, &SyncthingNotifier::connected, &m_dbusNotifier, &DBusStatusNotifier::hideDisconnect);
#endif
// apply settings, this also establishes the connection to Syncthing (according to settings)
// note: It is important to apply settings only after all Signals & Slots have been connected (e.g. to handle SyncthingConnection::error()).
// note: This weirdly calls updateStatusIconAndText(). So there is not need to call it again within this constructor.
trayMenu().widget().applySettings(connectionConfig);
}
/*!
* \brief Moves the specified \a point in the specified \a rect.
*/
void moveInside(QPoint &point, const QRect &rect)
{
if (point.y() < rect.top()) {
point.setY(rect.top());
} else if (point.y() > rect.bottom()) {
point.setY(rect.bottom());
}
if (point.x() < rect.left()) {
point.setX(rect.left());
} else if (point.x() > rect.right()) {
point.setX(rect.right());
}
}
void TrayIcon::handleActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason) {
case QSystemTrayIcon::Context:
// can't catch that event on Plasma anyway
break;
case QSystemTrayIcon::MiddleClick:
trayMenu().widget().showWebUI();
break;
case QSystemTrayIcon::Trigger:
trayMenu().showUsingPositioningSettings();
break;
default:;
}
}
void TrayIcon::handleMessageClicked()
{
switch (m_messageClickedAction) {
case TrayIconMessageClickedAction::None:
return;
case TrayIconMessageClickedAction::DismissNotification:
trayMenu().widget().connection().requestClearingErrors();
break;
case TrayIconMessageClickedAction::ShowInternalErrors:
showInternalErrorsDialog();
break;
case TrayIconMessageClickedAction::ShowWebUi:
trayMenu().widget().showWebUI();
break;
}
}
void TrayIcon::showDisconnected()
{
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showDisconnect();
} else
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::None;
showMessage(QCoreApplication::applicationName(), tr("Disconnected from Syncthing"), QSystemTrayIcon::Warning);
}
}
void TrayIcon::showSyncComplete(const QString &message)
{
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showSyncComplete(message);
} else
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::None;
showMessage(QCoreApplication::applicationName(), message, QSystemTrayIcon::Information);
}
}
void TrayIcon::handleErrorsCleared()
{
#ifndef SYNCTHINGTRAY_UNIFY_TRAY_MENUS
m_errorsAction->setVisible(false);
#endif
}
void TrayIcon::showInternalError(
const QString &errorMessage, SyncthingErrorCategory category, int networkError, const QNetworkRequest &request, const QByteArray &response)
{
if (!InternalError::isRelevant(trayMenu().widget().connection(), category, errorMessage, networkError)) {
return;
}
auto error = InternalError(errorMessage, request.url(), response);
if (Settings::values().notifyOn.internalErrors) {
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showInternalError(error);
} else
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::ShowInternalErrors;
showMessage(tr("Error"), errorMessage, QSystemTrayIcon::Critical);
}
}
InternalErrorsDialog::addError(std::move(error));
#ifdef SYNCTHINGTRAY_UNIFY_TRAY_MENUS
trayMenu().widget().showInternalErrorsButton();
#else
m_errorsAction->setVisible(true);
#endif
}
void TrayIcon::showLauncherError(const QString &errorMessage, const QString &additionalInfo)
{
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showLauncherError(errorMessage, additionalInfo);
} else
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::None;
showMessage(tr("Launcher error"), QStringList({ errorMessage, additionalInfo }).join(QChar('\n')), QSystemTrayIcon::Critical);
}
}
void TrayIcon::showSyncthingNotification(CppUtilities::DateTime when, const QString &message)
{
if (m_notifyOnSyncthingErrors) {
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showSyncthingNotification(when, message);
} else
#else
Q_UNUSED(when)
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::DismissNotification;
showMessage(tr("Syncthing notification - click to dismiss"), message, QSystemTrayIcon::Warning);
}
}
updateStatusIconAndText();
}
void TrayIcon::updateStatusIconAndText()
{
auto &trayWidget = trayMenu().widget();
const auto statusInfo = StatusInfo(trayMenu().widget().connection(),
TrayWidget::instances().size() > 1 && trayWidget.selectedConnection() ? trayWidget.selectedConnection()->label : QString());
if (statusInfo.additionalStatusText().isEmpty()) {
setToolTip(statusInfo.statusText());
} else {
setToolTip(statusInfo.statusText() % QChar('\n') % statusInfo.additionalStatusText());
}
setIcon(statusInfo.statusIcon());
}
void TrayIcon::showNewDev(const QString &devId, const QString &message)
{
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showNewDev(devId, message);
} else
#else
Q_UNUSED(devId)
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::ShowWebUi;
showMessage(tr("Syncthing device wants to connect - click for web UI"), message, QSystemTrayIcon::Information);
}
}
void TrayIcon::showNewDir(const QString &devId, const QString &dirId, const QString &dirLabel, const QString &message)
{
#ifdef QT_UTILITIES_SUPPORT_DBUS_NOTIFICATIONS
if (m_dbusNotificationsEnabled) {
m_dbusNotifier.showNewDir(devId, dirId, dirLabel, message);
} else
#else
Q_UNUSED(devId)
Q_UNUSED(dirId)
Q_UNUSED(dirLabel)
#endif
{
m_messageClickedAction = TrayIconMessageClickedAction::ShowWebUi;
showMessage(tr("New Syncthing folder - click for web UI"), message, QSystemTrayIcon::Information);
}
}
void TrayIcon::showInternalErrorsDialog()
{
if (!InternalErrorsDialog::hasInstance()) {
connect(InternalErrorsDialog::instance(), &InternalErrorsDialog::errorsCleared, this, &TrayIcon::handleErrorsCleared);
}
trayMenu().widget().showInternalErrorsDialog();
}
} // namespace QtGui
|