vacationscriptindicatorwidget.cpp 3.13 KB
Newer Older
1
/*
2
   SPDX-FileCopyrightText: 2013-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: GPL-2.0-or-later
5 6 7
*/

#include "vacationscriptindicatorwidget.h"
8

Laurent Montel's avatar
Laurent Montel committed
9
#include "util.h"
Laurent Montel's avatar
Laurent Montel committed
10
#include <QIcon>
11 12 13 14 15 16
#include <KLocalizedString>

#include <QHBoxLayout>

using namespace KMail;

Laurent Montel's avatar
Laurent Montel committed
17
ServerLabel::ServerLabel(const QString &serverName, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
18 19
    : QLabel(parent)
    , mServerName(serverName)
20 21
{
    setToolTip(serverName);
Laurent Montel's avatar
Laurent Montel committed
22
    setPixmap(QIcon::fromTheme(QStringLiteral("network-server")).pixmap(16, 16));
23
    setStyleSheet(QStringLiteral("background-color: %1;").arg(QColor(Qt::yellow).name()));
Laurent Montel's avatar
Laurent Montel committed
24
    setContentsMargins(2, 0, 4, 0);
25 26 27 28 29 30
}

ServerLabel::~ServerLabel()
{
}

Laurent Montel's avatar
Laurent Montel committed
31
void ServerLabel::mouseReleaseEvent(QMouseEvent *event)
32
{
33
    Q_EMIT clicked(mServerName);
Laurent Montel's avatar
Laurent Montel committed
34
    QLabel::mouseReleaseEvent(event);
35 36
}

Laurent Montel's avatar
Laurent Montel committed
37 38
VacationLabel::VacationLabel(const QString &text, QWidget *parent)
    : QLabel(text, parent)
39 40 41
{
    // changing the palette doesn't work, seems to be overwriten by the
    // statusbar again, stylesheets seems to work though
42
    setStyleSheet(QStringLiteral("background-color: %1; color: %2;").arg(QColor(Qt::yellow).name(), QColor(Qt::black).name()));
Laurent Montel's avatar
Laurent Montel committed
43 44
    setContentsMargins(4, 0, 2, 0);
    setCursor(QCursor(Qt::PointingHandCursor));
45 46 47 48 49 50
}

VacationLabel::~VacationLabel()
{
}

Laurent Montel's avatar
Laurent Montel committed
51
void VacationLabel::mouseReleaseEvent(QMouseEvent *event)
52
{
Laurent Montel's avatar
Laurent Montel committed
53
    Q_EMIT vacationLabelClicked();
Laurent Montel's avatar
Laurent Montel committed
54
    QLabel::mouseReleaseEvent(event);
55 56 57
}

VacationScriptIndicatorWidget::VacationScriptIndicatorWidget(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
58
    : QWidget(parent)
59 60 61 62 63 64 65 66 67
{
}

VacationScriptIndicatorWidget::~VacationScriptIndicatorWidget()
{
}

void VacationScriptIndicatorWidget::setVacationScriptActive(bool active, const QString &serverName)
{
68 69 70 71
    if (serverName.isEmpty()) {
        return;
    }

72 73 74 75 76 77
    if (active) {
        if (!mServerActive.contains(serverName)) {
            mServerActive.append(serverName);
            updateIndicator();
        }
    } else {
78 79
        int countRemoveServerName = mServerActive.removeAll(serverName);
        if (countRemoveServerName > 0) {
80 81 82 83 84 85 86 87
            updateIndicator();
        }
    }
}

void VacationScriptIndicatorWidget::createIndicator()
{
    delete mBoxLayout;
Laurent Montel's avatar
Laurent Montel committed
88
    mBoxLayout = new QHBoxLayout(this);
89
    mBoxLayout->setContentsMargins(0, 0, 0, 0);
90
    mBoxLayout->setSpacing(0);
91
    mInfo = new VacationLabel(i18np("Out of office reply active on server", "Out of office reply active on servers", mServerActive.count()));
Laurent Montel's avatar
Laurent Montel committed
92
    connect(mInfo, &VacationLabel::vacationLabelClicked, this, &VacationScriptIndicatorWidget::slotVacationLabelClicked);
93
    mBoxLayout->addWidget(mInfo);
Laurent Montel's avatar
Laurent Montel committed
94
    for (const QString &server : qAsConst(mServerActive)) {
95
        ServerLabel *lab = new ServerLabel(server);
Laurent Montel's avatar
Laurent Montel committed
96
        connect(lab, &ServerLabel::clicked, this, &VacationScriptIndicatorWidget::clicked);
97 98 99 100
        mBoxLayout->addWidget(lab);
    }
}

Laurent Montel's avatar
Laurent Montel committed
101 102 103 104 105
void VacationScriptIndicatorWidget::slotVacationLabelClicked()
{
    Q_EMIT clicked(QString());
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119
void VacationScriptIndicatorWidget::updateIndicator()
{
    if (mServerActive.isEmpty()) {
        hide();
    } else {
        createIndicator();
        show();
    }
}

bool VacationScriptIndicatorWidget::hasVacationScriptActive() const
{
    return !mServerActive.isEmpty();
}