2018-11-28 20:57:48 +01:00
|
|
|
#include "./syncthingmenuaction.h"
|
|
|
|
#include "./syncthingfileitemaction.h"
|
|
|
|
|
|
|
|
#include "../model/syncthingicons.h"
|
|
|
|
|
|
|
|
#include "../connector/syncthingconnection.h"
|
|
|
|
|
2019-06-12 21:00:49 +02:00
|
|
|
#ifdef CPP_UTILITIES_DEBUG_BUILD
|
2018-12-26 01:16:38 +01:00
|
|
|
#include <c++utilities/io/ansiescapecodes.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-28 20:57:48 +01:00
|
|
|
#include <QAction>
|
|
|
|
#include <QMenu>
|
|
|
|
|
2019-06-12 21:00:49 +02:00
|
|
|
#ifdef CPP_UTILITIES_DEBUG_BUILD
|
2018-12-26 01:16:38 +01:00
|
|
|
#include <iostream>
|
|
|
|
#endif
|
|
|
|
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace CppUtilities;
|
2018-11-28 20:57:48 +01:00
|
|
|
using namespace Data;
|
|
|
|
|
2018-12-26 01:18:28 +01:00
|
|
|
SyncthingMenuAction::SyncthingMenuAction(const KFileItemListProperties &properties, const QList<QAction *> &actions, QObject *parent)
|
|
|
|
: QAction(parent)
|
2018-11-28 20:57:48 +01:00
|
|
|
, m_properties(properties)
|
2018-12-24 17:19:11 +01:00
|
|
|
, m_notifier(SyncthingFileItemAction::staticData().connection())
|
2018-11-28 20:57:48 +01:00
|
|
|
{
|
2019-06-12 21:00:49 +02:00
|
|
|
#ifdef CPP_UTILITIES_DEBUG_BUILD
|
2018-12-26 01:16:38 +01:00
|
|
|
std::cerr << EscapeCodes::Phrases::Info << "Creating SyncthingMenuAction: " << this << EscapeCodes::Phrases::EndFlush;
|
|
|
|
#endif
|
|
|
|
|
2018-12-24 17:19:11 +01:00
|
|
|
// init according to current state
|
|
|
|
createMenu(actions);
|
|
|
|
updateActionStatus();
|
|
|
|
|
|
|
|
// setup handling future state change
|
|
|
|
m_notifier.setEnabledNotifications(SyncthingHighLevelNotification::ConnectedDisconnected);
|
|
|
|
connect(&m_notifier, &SyncthingNotifier::connected, this, &SyncthingMenuAction::handleConnectedChanged);
|
|
|
|
connect(&m_notifier, &SyncthingNotifier::disconnected, this, &SyncthingMenuAction::handleConnectedChanged);
|
|
|
|
}
|
|
|
|
|
2019-06-12 21:00:49 +02:00
|
|
|
#ifdef CPP_UTILITIES_DEBUG_BUILD
|
2018-12-26 01:16:38 +01:00
|
|
|
SyncthingMenuAction::~SyncthingMenuAction()
|
|
|
|
{
|
|
|
|
std::cerr << EscapeCodes::Phrases::Info << "Destroying SyncthingMenuAction: " << this << EscapeCodes::Phrases::EndFlush;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-24 17:19:11 +01:00
|
|
|
void SyncthingMenuAction::handleConnectedChanged()
|
|
|
|
{
|
|
|
|
// update the current menu
|
|
|
|
if (QMenu *const menu = this->menu()) {
|
2018-12-26 01:15:28 +01:00
|
|
|
menu->deleteLater();
|
2018-12-24 17:19:11 +01:00
|
|
|
setMenu(nullptr);
|
2018-11-28 20:57:48 +01:00
|
|
|
}
|
2018-12-26 01:18:28 +01:00
|
|
|
createMenu(SyncthingFileItemAction::createActions(m_properties, parent()));
|
2018-12-24 17:19:11 +01:00
|
|
|
|
|
|
|
// update status of action itself
|
|
|
|
updateActionStatus();
|
2018-11-28 20:57:48 +01:00
|
|
|
}
|
|
|
|
|
2018-12-24 17:19:11 +01:00
|
|
|
void SyncthingMenuAction::updateActionStatus()
|
2018-11-28 20:57:48 +01:00
|
|
|
{
|
2018-12-24 17:19:11 +01:00
|
|
|
auto &data = SyncthingFileItemAction::staticData();
|
|
|
|
auto &connection = data.connection();
|
|
|
|
|
|
|
|
// handle case when already connected
|
|
|
|
if (connection.isConnected()) {
|
2018-11-28 20:57:48 +01:00
|
|
|
setText(tr("Syncthing"));
|
2019-05-19 18:04:28 +02:00
|
|
|
setIcon(renderSvgImage(makeSyncthingIcon(), QSize(16, 16)));
|
2018-12-24 17:19:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to connect if not reconnecting anyways and there's no configuration issue
|
|
|
|
if (connection.status() != SyncthingStatus::Reconnecting && !data.hasError()) {
|
|
|
|
connection.connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (connection.hasPendingRequests()) {
|
2018-11-28 20:57:48 +01:00
|
|
|
setText(tr("Syncthing - connecting"));
|
2018-12-24 17:19:11 +01:00
|
|
|
} else {
|
|
|
|
setText(tr("Syncthing - not connected"));
|
|
|
|
}
|
|
|
|
setIcon(statusIcons().disconnected);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncthingMenuAction::createMenu(const QList<QAction *> &actions)
|
|
|
|
{
|
|
|
|
if (actions.isEmpty()) {
|
|
|
|
return;
|
2018-11-28 20:57:48 +01:00
|
|
|
}
|
2018-12-24 17:19:11 +01:00
|
|
|
auto *const menu = new QMenu(parentWidget());
|
|
|
|
menu->addActions(actions);
|
|
|
|
setMenu(menu);
|
2018-11-28 20:57:48 +01:00
|
|
|
}
|