File: syncthingmenuaction.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (95 lines) | stat: -rw-r--r-- 2,857 bytes parent folder | download
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
#include "./syncthingmenuaction.h"
#include "./syncthingfileitemaction.h"

#include <syncthingmodel/syncthingicons.h>

#include <syncthingconnector/syncthingconnection.h>

#ifdef CPP_UTILITIES_DEBUG_BUILD
#include <c++utilities/io/ansiescapecodes.h>
#endif

#include <QAction>
#include <QMenu>

#ifdef CPP_UTILITIES_DEBUG_BUILD
#include <iostream>
#endif

using namespace CppUtilities;
using namespace Data;

SyncthingMenuAction::SyncthingMenuAction(const KFileItemListProperties &properties, const QList<QAction *> &actions, QWidget *parentWidget)
    : QAction(parentWidget)
    , m_properties(properties)
    , m_notifier(SyncthingFileItemAction::staticData().connection())
    , m_parentWidget(parentWidget)
{
#ifdef CPP_UTILITIES_DEBUG_BUILD
    std::cerr << EscapeCodes::Phrases::Info << "Creating SyncthingMenuAction: " << this << EscapeCodes::Phrases::EndFlush;
#endif

    // 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);
}

#ifdef CPP_UTILITIES_DEBUG_BUILD
SyncthingMenuAction::~SyncthingMenuAction()
{
    std::cerr << EscapeCodes::Phrases::Info << "Destroying SyncthingMenuAction: " << this << EscapeCodes::Phrases::EndFlush;
}
#endif

void SyncthingMenuAction::handleConnectedChanged()
{
    // update the current menu
    if (QMenu *const menu = this->menu()) {
        menu->deleteLater();
        setMenu(nullptr);
    }
    createMenu(SyncthingFileItemAction::createActions(m_properties, parent()));

    // update status of action itself
    updateActionStatus();
}

void SyncthingMenuAction::updateActionStatus()
{
    auto &data = SyncthingFileItemAction::staticData();
    auto &connection = data.connection();

    // handle case when already connected
    if (connection.isConnected()) {
        setText(tr("Syncthing"));
        setIcon(QIcon(QStringLiteral("syncthing.fa")));
        return;
    }

    // attempt to connect if not reconnecting anyway and there's no configuration issue
    if (connection.status() != SyncthingStatus::Reconnecting && !data.hasError()) {
        connection.connect();
    }

    if (connection.hasPendingRequests()) {
        setText(tr("Syncthing - connecting"));
    } else {
        setText(tr("Syncthing - not connected"));
    }
    setIcon(statusIcons().disconnected);
}

void SyncthingMenuAction::createMenu(const QList<QAction *> &actions)
{
    if (actions.isEmpty()) {
        return;
    }
    auto *const menu = new QMenu(m_parentWidget);
    menu->addActions(actions);
    setMenu(menu);
}