1 /*
2    SPDX-FileCopyrightText: 2013-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "vacationscriptindicatorwidget.h"
8 
9 #include "util.h"
10 #include <KLocalizedString>
11 #include <QIcon>
12 
13 #include <QHBoxLayout>
14 
15 using namespace KMail;
16 
ServerLabel(const QString & serverName,QWidget * parent)17 ServerLabel::ServerLabel(const QString &serverName, QWidget *parent)
18     : QLabel(parent)
19     , mServerName(serverName)
20 {
21     setToolTip(serverName);
22     setPixmap(QIcon::fromTheme(QStringLiteral("network-server")).pixmap(16, 16));
23     setStyleSheet(QStringLiteral("background-color: %1; color: %2;").arg(QColor(Qt::yellow).name(), QColor(Qt::black).name()));
24     setContentsMargins(2, 0, 4, 0);
25 }
26 
27 ServerLabel::~ServerLabel() = default;
28 
mouseReleaseEvent(QMouseEvent * event)29 void ServerLabel::mouseReleaseEvent(QMouseEvent *event)
30 {
31     Q_EMIT clicked(mServerName);
32     QLabel::mouseReleaseEvent(event);
33 }
34 
VacationLabel(const QString & text,QWidget * parent)35 VacationLabel::VacationLabel(const QString &text, QWidget *parent)
36     : QLabel(text, parent)
37 {
38     // changing the palette doesn't work, seems to be overwriten by the
39     // statusbar again, stylesheets seems to work though
40     setStyleSheet(QStringLiteral("background-color: %1; color: %2;").arg(QColor(Qt::yellow).name(), QColor(Qt::black).name()));
41     setContentsMargins(4, 0, 2, 0);
42     setCursor(QCursor(Qt::PointingHandCursor));
43 }
44 
45 VacationLabel::~VacationLabel() = default;
46 
mouseReleaseEvent(QMouseEvent * event)47 void VacationLabel::mouseReleaseEvent(QMouseEvent *event)
48 {
49     Q_EMIT vacationLabelClicked();
50     QLabel::mouseReleaseEvent(event);
51 }
52 
VacationScriptIndicatorWidget(QWidget * parent)53 VacationScriptIndicatorWidget::VacationScriptIndicatorWidget(QWidget *parent)
54     : QWidget(parent)
55 {
56 }
57 
58 VacationScriptIndicatorWidget::~VacationScriptIndicatorWidget() = default;
59 
setVacationScriptActive(bool active,const QString & serverName)60 void VacationScriptIndicatorWidget::setVacationScriptActive(bool active, const QString &serverName)
61 {
62     if (serverName.isEmpty()) {
63         return;
64     }
65 
66     if (active) {
67         if (!mServerActive.contains(serverName)) {
68             mServerActive.append(serverName);
69             updateIndicator();
70         }
71     } else {
72         int countRemoveServerName = mServerActive.removeAll(serverName);
73         if (countRemoveServerName > 0) {
74             updateIndicator();
75         }
76     }
77 }
78 
createIndicator()79 void VacationScriptIndicatorWidget::createIndicator()
80 {
81     delete mBoxLayout;
82     mBoxLayout = new QHBoxLayout(this);
83     mBoxLayout->setContentsMargins({});
84     mBoxLayout->setSpacing(0);
85     mInfo = new VacationLabel(i18np("Out of office reply active on server", "Out of office reply active on servers", mServerActive.count()));
86     connect(mInfo, &VacationLabel::vacationLabelClicked, this, &VacationScriptIndicatorWidget::slotVacationLabelClicked);
87     mBoxLayout->addWidget(mInfo);
88     for (const QString &server : std::as_const(mServerActive)) {
89         auto lab = new ServerLabel(server);
90         connect(lab, &ServerLabel::clicked, this, &VacationScriptIndicatorWidget::clicked);
91         mBoxLayout->addWidget(lab);
92     }
93 }
94 
slotVacationLabelClicked()95 void VacationScriptIndicatorWidget::slotVacationLabelClicked()
96 {
97     Q_EMIT clicked(QString());
98 }
99 
updateIndicator()100 void VacationScriptIndicatorWidget::updateIndicator()
101 {
102     if (mServerActive.isEmpty()) {
103         hide();
104     } else {
105         createIndicator();
106         show();
107     }
108 }
109 
hasVacationScriptActive() const110 bool VacationScriptIndicatorWidget::hasVacationScriptActive() const
111 {
112     return !mServerActive.isEmpty();
113 }
114