1 /*
2 * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15 #include "notificationwidget.h"
16 #include "QProgressIndicator.h"
17 #include "common/utility.h"
18 #include "common/asserts.h"
19 #include "guiutility.h"
20
21 #include <QPushButton>
22
23 #include "ocsjob.h"
24
25 namespace OCC {
26
27 Q_LOGGING_CATEGORY(lcNotifications, "gui.notifications", QtInfoMsg)
28
NotificationWidget(QWidget * parent)29 NotificationWidget::NotificationWidget(QWidget *parent)
30 : QWidget(parent)
31 {
32 _ui.setupUi(this);
33 _progressIndi = new QProgressIndicator(this);
34 _ui.horizontalLayout->addWidget(_progressIndi);
35 }
36
setActivity(const Activity & activity)37 void NotificationWidget::setActivity(const Activity &activity)
38 {
39 _myActivity = activity;
40
41 _accountName = activity._accName;
42 OC_ASSERT(!_accountName.isEmpty());
43
44 // _ui._headerLabel->setText( );
45 _ui._subjectLabel->setVisible(!activity._subject.isEmpty());
46 _ui._messageLabel->setVisible(!activity._message.isEmpty());
47
48 if (activity._link.isEmpty()) {
49 _ui._subjectLabel->setText(activity._subject);
50 } else {
51 _ui._subjectLabel->setText( QString("<a href=\"%1\">%2</a>")
52 .arg(activity._link.toString(QUrl::FullyEncoded),
53 activity._subject.toHtmlEscaped() ));
54 _ui._subjectLabel->setTextFormat(Qt::RichText);
55 _ui._subjectLabel->setOpenExternalLinks(true);
56 }
57
58 _ui._messageLabel->setText(activity._message);
59 QString tText = tr("Created at %1").arg(Utility::timeAgoInWords(activity._dateTime));
60 _ui._timeLabel->setText(tText);
61
62 // always remove the buttons
63 qDeleteAll(_buttons);
64 _buttons.clear();
65
66 // display buttons for the links
67 if (activity._links.isEmpty()) {
68 // in case there is no action defined, do a close button.
69 QPushButton *b = _ui._buttonBox->addButton(QDialogButtonBox::Close);
70 b->setDefault(true);
71 connect(b, &QAbstractButton::clicked, this, [this]{
72 QString doneText = tr("Closing in a few seconds...");
73 _ui._timeLabel->setText(doneText);
74 emit requestCleanupAndBlacklist(_myActivity);
75 return;
76 });
77 } else {
78 for (const auto &link : activity._links) {
79 QPushButton *b = _ui._buttonBox->addButton(link._label, QDialogButtonBox::AcceptRole);
80 b->setDefault(link._isPrimary);
81 connect(b, &QAbstractButton::clicked, this, [this, b, &link]{
82 slotButtonClicked(b, link);
83 });
84 _buttons.append(b);
85 }
86 }
87 }
88
activity() const89 Activity NotificationWidget::activity() const
90 {
91 return _myActivity;
92 }
93
slotButtonClicked(QPushButton * buttonWidget,const ActivityLink & triggeredLink)94 void NotificationWidget::slotButtonClicked(QPushButton *buttonWidget, const ActivityLink &triggeredLink)
95 {
96 buttonWidget->setEnabled(false);
97 _actionLabel = triggeredLink._label;
98 if (!triggeredLink._link.isEmpty()) {
99 qCInfo(lcNotifications) << "Notification Link: " << triggeredLink._verb << triggeredLink._link;
100 _progressIndi->startAnimation();
101 emit sendNotificationRequest(_accountName, triggeredLink._link, triggeredLink._verb);
102 }
103 }
104
slotNotificationRequestFinished(int statusCode)105 void NotificationWidget::slotNotificationRequestFinished(int statusCode)
106 {
107 QString doneText;
108 QLocale locale;
109
110 QString timeStr = locale.toString(QTime::currentTime());
111
112 // the ocs API returns stat code 100 or 200 inside the xml if it succeeded.
113 if (statusCode != OCS_SUCCESS_STATUS_CODE && statusCode != OCS_SUCCESS_STATUS_CODE_V2) {
114 qCWarning(lcNotifications) << "Notification Request to Server failed, leave button visible.";
115 for (auto button : _buttons) {
116 button->setEnabled(true);
117 }
118 //: The second parameter is a time, such as 'failed at 09:58pm'
119 doneText = tr("%1 request failed at %2").arg(_actionLabel, timeStr);
120 } else {
121 // the call to the ocs API succeeded.
122 _ui._buttonBox->hide();
123
124 //: The second parameter is a time, such as 'selected at 09:58pm'
125 doneText = tr("'%1' selected at %2").arg(_actionLabel, timeStr);
126 }
127 _ui._timeLabel->setText(doneText);
128
129 _progressIndi->stopAnimation();
130 }
131
changeEvent(QEvent * e)132 void NotificationWidget::changeEvent(QEvent *e)
133 {
134 switch (e->type()) {
135 case QEvent::StyleChange:
136 case QEvent::PaletteChange:
137 case QEvent::ThemeChange:
138 _ui._notifIcon->setPixmap(Utility::getCoreIcon(QStringLiteral("bell")).pixmap(_ui._notifIcon->size()));
139 break;
140 default:
141 break;
142 }
143 QWidget::changeEvent(e);
144 }
145 }
146