2016-03-04 19:40:29 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2016-10-25 12:00:07 +03:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2016-03-04 19:40:29 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notificationwidget.h"
|
2016-03-09 17:21:52 +03:00
|
|
|
#include "QProgressIndicator.h"
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
2017-09-01 19:11:43 +03:00
|
|
|
#include "common/asserts.h"
|
2016-03-04 19:40:29 +03:00
|
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
2016-03-14 17:41:20 +03:00
|
|
|
#include "ocsjob.h"
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
namespace OCC {
|
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcNotifications, "gui.notifications", QtInfoMsg)
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
NotificationWidget::NotificationWidget(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
_ui.setupUi(this);
|
2016-03-09 17:21:52 +03:00
|
|
|
_progressIndi = new QProgressIndicator(this);
|
|
|
|
_ui.horizontalLayout->addWidget(_progressIndi);
|
2016-03-04 19:40:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void NotificationWidget::setActivity(const Activity &activity)
|
|
|
|
{
|
|
|
|
_myActivity = activity;
|
|
|
|
|
|
|
|
_accountName = activity._accName;
|
2017-02-07 15:52:15 +03:00
|
|
|
ASSERT(!_accountName.isEmpty());
|
2016-03-04 19:40:29 +03:00
|
|
|
|
|
|
|
// _ui._headerLabel->setText( );
|
2016-04-04 13:45:39 +03:00
|
|
|
_ui._subjectLabel->setVisible(!activity._subject.isEmpty());
|
|
|
|
_ui._messageLabel->setVisible(!activity._message.isEmpty());
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
_ui._subjectLabel->setText(activity._subject);
|
2016-04-04 13:45:39 +03:00
|
|
|
_ui._messageLabel->setText(activity._message);
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
_ui._notifIcon->setPixmap(QPixmap(":/client/resources/bell.png"));
|
|
|
|
_ui._notifIcon->setMinimumWidth(64);
|
|
|
|
_ui._notifIcon->setMinimumHeight(64);
|
|
|
|
_ui._notifIcon->show();
|
|
|
|
|
2016-03-18 10:21:54 +03:00
|
|
|
QString tText = tr("Created at %1").arg(Utility::timeAgoInWords(activity._dateTime));
|
|
|
|
_ui._timeLabel->setText(tText);
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
// always remove the buttons
|
|
|
|
foreach (auto button, _ui._buttonBox->buttons()) {
|
|
|
|
_ui._buttonBox->removeButton(button);
|
|
|
|
}
|
2016-03-23 18:59:03 +03:00
|
|
|
_buttons.clear();
|
2016-03-04 19:40:29 +03:00
|
|
|
|
|
|
|
// display buttons for the links
|
2016-03-23 18:59:03 +03:00
|
|
|
if (activity._links.isEmpty()) {
|
|
|
|
// in case there is no action defined, do a close button.
|
|
|
|
QPushButton *b = _ui._buttonBox->addButton(QDialogButtonBox::Close);
|
|
|
|
b->setDefault(true);
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(b, &QAbstractButton::clicked, this, &NotificationWidget::slotButtonClicked);
|
2016-03-04 19:40:29 +03:00
|
|
|
_buttons.append(b);
|
2016-03-23 18:59:03 +03:00
|
|
|
} else {
|
|
|
|
foreach (auto link, activity._links) {
|
|
|
|
QPushButton *b = _ui._buttonBox->addButton(link._label, QDialogButtonBox::AcceptRole);
|
|
|
|
b->setDefault(link._isPrimary);
|
2017-09-20 11:14:48 +03:00
|
|
|
connect(b, &QAbstractButton::clicked, this, &NotificationWidget::slotButtonClicked);
|
2016-03-23 18:59:03 +03:00
|
|
|
_buttons.append(b);
|
|
|
|
}
|
2016-03-04 19:40:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:48:38 +03:00
|
|
|
Activity NotificationWidget::activity() const
|
2016-03-22 12:35:24 +03:00
|
|
|
{
|
2016-03-23 18:48:38 +03:00
|
|
|
return _myActivity;
|
2016-03-22 12:35:24 +03:00
|
|
|
}
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
void NotificationWidget::slotButtonClicked()
|
|
|
|
{
|
|
|
|
QObject *buttonWidget = QObject::sender();
|
|
|
|
int index = -1;
|
|
|
|
if (buttonWidget) {
|
2016-03-09 17:21:52 +03:00
|
|
|
// find the button that was clicked, it has to be in the list
|
|
|
|
// of buttons that were added to the button box before.
|
2016-03-04 19:40:29 +03:00
|
|
|
for (int i = 0; i < _buttons.count(); i++) {
|
|
|
|
if (_buttons.at(i) == buttonWidget) {
|
|
|
|
index = i;
|
|
|
|
}
|
2016-03-09 17:21:52 +03:00
|
|
|
_buttons.at(i)->setEnabled(false);
|
2016-03-04 19:40:29 +03:00
|
|
|
}
|
2016-03-09 17:21:52 +03:00
|
|
|
|
|
|
|
// if the button was found, the link must be called
|
2016-03-23 18:59:03 +03:00
|
|
|
if (index > -1 && _myActivity._links.count() == 0) {
|
|
|
|
// no links, that means it was the close button
|
|
|
|
// empty link. Just close and remove the widget.
|
|
|
|
QString doneText = tr("Closing in a few seconds...");
|
|
|
|
_ui._timeLabel->setText(doneText);
|
|
|
|
emit requestCleanupAndBlacklist(_myActivity);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-04 19:40:29 +03:00
|
|
|
if (index > -1 && index < _myActivity._links.count()) {
|
|
|
|
ActivityLink triggeredLink = _myActivity._links.at(index);
|
2016-03-18 12:02:11 +03:00
|
|
|
_actionLabel = triggeredLink._label;
|
2016-03-23 18:59:03 +03:00
|
|
|
|
|
|
|
if (!triggeredLink._link.isEmpty()) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCInfo(lcNotifications) << "Notification Link: " << triggeredLink._verb << triggeredLink._link;
|
2016-03-23 18:59:03 +03:00
|
|
|
_progressIndi->startAnimation();
|
|
|
|
emit sendNotificationRequest(_accountName, triggeredLink._link, triggeredLink._verb);
|
|
|
|
}
|
2016-03-04 19:40:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 17:21:52 +03:00
|
|
|
void NotificationWidget::slotNotificationRequestFinished(int statusCode)
|
|
|
|
{
|
|
|
|
int i = 0;
|
2016-03-18 12:02:11 +03:00
|
|
|
QString doneText;
|
|
|
|
QLocale locale;
|
|
|
|
|
|
|
|
QString timeStr = locale.toString(QTime::currentTime());
|
|
|
|
|
2016-03-09 17:21:52 +03:00
|
|
|
// the ocs API returns stat code 100 if it succeeded.
|
2016-03-14 17:41:20 +03:00
|
|
|
if (statusCode != OCS_SUCCESS_STATUS_CODE) {
|
2017-03-30 14:46:20 +03:00
|
|
|
qCWarning(lcNotifications) << "Notification Request to Server failed, leave button visible.";
|
2016-03-09 17:21:52 +03:00
|
|
|
for (i = 0; i < _buttons.count(); i++) {
|
|
|
|
_buttons.at(i)->setEnabled(true);
|
|
|
|
}
|
2016-03-29 18:18:53 +03:00
|
|
|
//: The second parameter is a time, such as 'failed at 09:58pm'
|
|
|
|
doneText = tr("%1 request failed at %2").arg(_actionLabel, timeStr);
|
2016-03-09 17:21:52 +03:00
|
|
|
} else {
|
|
|
|
// the call to the ocs API succeeded.
|
|
|
|
_ui._buttonBox->hide();
|
2016-03-18 12:02:11 +03:00
|
|
|
|
2016-03-29 18:18:53 +03:00
|
|
|
//: The second parameter is a time, such as 'selected at 09:58pm'
|
|
|
|
doneText = tr("'%1' selected at %2").arg(_actionLabel, timeStr);
|
2016-03-09 17:21:52 +03:00
|
|
|
}
|
2016-03-18 12:02:11 +03:00
|
|
|
_ui._timeLabel->setText(doneText);
|
|
|
|
|
2016-03-09 17:21:52 +03:00
|
|
|
_progressIndi->stopAnimation();
|
2016-03-18 13:25:14 +03:00
|
|
|
}
|
2016-03-04 19:40:29 +03:00
|
|
|
}
|