nextcloud-desktop/src/gui/notificationwidget.cpp

138 lines
4.5 KiB
C++
Raw Normal View History

/*
* 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
* the Free Software Foundation; version 2 of the License.
*
* 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"
#include "QProgressIndicator.h"
#include "utility.h"
#include <QPushButton>
#include "ocsjob.h"
namespace OCC {
NotificationWidget::NotificationWidget(QWidget *parent) : QWidget(parent)
{
_ui.setupUi(this);
_progressIndi = new QProgressIndicator(this);
_ui.horizontalLayout->addWidget(_progressIndi);
}
void NotificationWidget::setActivity(const Activity& activity)
{
_myActivity = activity;
Q_ASSERT( !activity._accName.isEmpty() );
_accountName = activity._accName;
// _ui._headerLabel->setText( );
_ui._subjectLabel->setText(activity._subject);
if( activity._message.isEmpty()) {
_ui._messageLabel->hide();
} else {
_ui._messageLabel->setText(activity._message);
}
_ui._notifIcon->setPixmap(QPixmap(":/client/resources/bell.png"));
_ui._notifIcon->setMinimumWidth(64);
_ui._notifIcon->setMinimumHeight(64);
_ui._notifIcon->show();
QString tText = tr("Created at %1").arg(Utility::timeAgoInWords(activity._dateTime));
_ui._timeLabel->setText(tText);
// always remove the buttons
foreach( auto button, _ui._buttonBox->buttons() ) {
_ui._buttonBox->removeButton(button);
}
// display buttons for the links
foreach( auto link, activity._links ) {
QPushButton *b = _ui._buttonBox->addButton( link._label, QDialogButtonBox::AcceptRole);
b->setDefault(link._isPrimary);
connect(b, SIGNAL(clicked()), this, SLOT(slotButtonClicked()));
_buttons.append(b);
}
}
QString NotificationWidget::accountName() const
{
return _myActivity._accName;
}
void NotificationWidget::slotButtonClicked()
{
QObject *buttonWidget = QObject::sender();
int index = -1;
if( buttonWidget ) {
// find the button that was clicked, it has to be in the list
// of buttons that were added to the button box before.
for( int i = 0; i < _buttons.count(); i++ ) {
if( _buttons.at(i) == buttonWidget ) {
index = i;
}
_buttons.at(i)->setEnabled(false);
}
// if the button was found, the link must be called
if( index > -1 && index < _myActivity._links.count() ) {
ActivityLink triggeredLink = _myActivity._links.at(index);
_actionLabel = triggeredLink._label;
qDebug() << Q_FUNC_INFO << "Notification Link: "<< triggeredLink._verb << triggeredLink._link;
_progressIndi->startAnimation();
emit sendNotificationRequest( _accountName, triggeredLink._link, triggeredLink._verb );
}
}
}
void NotificationWidget::slotNotificationRequestFinished(int statusCode)
{
int i = 0;
QString doneText;
QLocale locale;
QString timeStr = locale.toString(QTime::currentTime());
// the ocs API returns stat code 100 if it succeeded.
if( statusCode != OCS_SUCCESS_STATUS_CODE ) {
qDebug() << Q_FUNC_INFO << "Notification Request to Server failed, leave button visible.";
for( i = 0; i < _buttons.count(); i++ ) {
_buttons.at(i)->setEnabled(true);
}
//* The second parameter is a time, such as 'failed at 09:58pm'
doneText = tr("%1 request failed at %2").arg(_actionLabel).arg(timeStr);
} else {
// the call to the ocs API succeeded.
_ui._buttonBox->hide();
//* The second parameter is a time, such as 'selected at 09:58pm'
doneText = tr("'%1' selected at %2").arg(_actionLabel).arg(timeStr);
// start a timer, so that activity widget can remove this widget after a
// certain time. It needs to be done by ActivityWidget because it also
// needs to hide the scrollview if no widget is left any more.
// method readyToClose() checks for the timer value to see if it is expired
_closeTimer.start();
}
_ui._timeLabel->setText( doneText );
_progressIndi->stopAnimation();
}
bool NotificationWidget::readyToClose()
{
return _closeTimer.isValid() && _closeTimer.elapsed() > NOTIFICATION_WIDGET_CLOSE_AFTER_MILLISECS;
}
}