2017-12-16 21:11:06 +01:00
|
|
|
#include "presencewidget.h"
|
|
|
|
|
|
2017-12-18 23:21:39 +01:00
|
|
|
#include <QMenu>
|
2018-03-24 13:12:49 +01:00
|
|
|
#include <QAction>
|
2017-12-16 21:11:06 +01:00
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QStringBuilder>
|
2017-12-29 02:00:36 +01:00
|
|
|
#include <QDebug>
|
2017-12-16 21:11:06 +01:00
|
|
|
|
|
|
|
|
#include "mainwindow.h"
|
|
|
|
|
#include "zeiterfassungapi.h"
|
|
|
|
|
|
2018-03-24 13:12:49 +01:00
|
|
|
#include "presencesettings.h"
|
|
|
|
|
|
2017-12-16 21:11:06 +01:00
|
|
|
PresenceWidget::PresenceWidget(MainWindow &mainWindow) :
|
2017-12-23 10:41:01 +01:00
|
|
|
QPushButton(&mainWindow),
|
2017-12-16 21:11:06 +01:00
|
|
|
m_mainWindow(mainWindow)
|
|
|
|
|
{
|
2017-12-19 21:20:16 +01:00
|
|
|
connect(&m_mainWindow, &MainWindow::refreshEverything, this, &PresenceWidget::refresh);
|
|
|
|
|
|
2017-12-23 10:41:01 +01:00
|
|
|
m_menu = new QMenu(this);
|
|
|
|
|
setMenu(m_menu);
|
2017-12-16 21:11:06 +01:00
|
|
|
|
2017-12-23 10:41:01 +01:00
|
|
|
m_action = new QAction(QIcon(QStringLiteral(":zeiterfassung/plugins/presenceplugin/images/refresh.png")),
|
|
|
|
|
tr("Refresh presence"), this);
|
|
|
|
|
connect(m_action, &QAction::triggered, this, &PresenceWidget::refresh);
|
|
|
|
|
m_mainWindow.menuView()->addAction(m_action);
|
2017-12-18 23:21:39 +01:00
|
|
|
|
2017-12-16 21:11:06 +01:00
|
|
|
auto timer = new QTimer(this);
|
2018-03-24 13:12:49 +01:00
|
|
|
timer->setInterval(PresenceSettings(mainWindow.settings()).interval());
|
2017-12-19 21:20:16 +01:00
|
|
|
connect(timer, &QTimer::timeout, this, &PresenceWidget::refresh);
|
2017-12-16 21:11:06 +01:00
|
|
|
timer->start();
|
|
|
|
|
|
2017-12-19 21:20:16 +01:00
|
|
|
refresh();
|
2017-12-16 21:11:06 +01:00
|
|
|
}
|
|
|
|
|
|
2017-12-19 21:20:16 +01:00
|
|
|
void PresenceWidget::refresh()
|
2017-12-16 21:11:06 +01:00
|
|
|
{
|
2017-12-23 10:41:01 +01:00
|
|
|
setText(tr("%0 available, %1 not available").arg(tr("???")).arg(tr("???")));
|
|
|
|
|
m_menu->clear();
|
2017-12-16 21:11:06 +01:00
|
|
|
|
2017-12-18 23:21:39 +01:00
|
|
|
m_action->setEnabled(false);
|
|
|
|
|
|
2017-12-16 21:11:06 +01:00
|
|
|
m_reply = m_mainWindow.erfassung().doGetPresenceStatus();
|
|
|
|
|
connect(m_reply.get(), &ZeiterfassungReply::finished, this, &PresenceWidget::finished);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void PresenceWidget::finished()
|
|
|
|
|
{
|
|
|
|
|
if(!m_reply->success())
|
|
|
|
|
{
|
|
|
|
|
QMessageBox::warning(&m_mainWindow, tr("Could not get presence status!"),
|
|
|
|
|
tr("Could not get presence status!") % "\n\n" % m_reply->message());
|
|
|
|
|
goto after;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
{
|
2017-12-23 10:41:01 +01:00
|
|
|
int present = 0,
|
|
|
|
|
notPresent = 0;
|
2017-12-16 21:11:06 +01:00
|
|
|
|
2017-12-23 10:41:01 +01:00
|
|
|
for(const auto &status : m_reply->presenceStatuses())
|
|
|
|
|
{
|
|
|
|
|
QIcon icon;
|
|
|
|
|
if(status.presence == QStringLiteral("J"))
|
|
|
|
|
{
|
|
|
|
|
present++;
|
|
|
|
|
icon = QIcon(QStringLiteral(":zeiterfassung/plugins/presenceplugin/images/present.png"));
|
|
|
|
|
}
|
|
|
|
|
else if(status.presence == QStringLiteral("N"))
|
|
|
|
|
{
|
|
|
|
|
notPresent++;
|
|
|
|
|
icon = QIcon(QStringLiteral(":zeiterfassung/plugins/presenceplugin/images/not-present.png"));
|
|
|
|
|
}
|
|
|
|
|
else
|
2017-12-29 02:00:36 +01:00
|
|
|
qCritical() << "unknown presence" << status.firstName << status.lastName << status.presence;
|
2017-12-23 10:41:01 +01:00
|
|
|
|
|
|
|
|
m_menu->addAction(icon, tr("%0 %1").arg(status.firstName).arg(status.lastName));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setText(tr("%0 available, %1 not available").arg(present).arg(notPresent));
|
|
|
|
|
}
|
2017-12-22 23:14:25 +01:00
|
|
|
|
2017-12-16 21:11:06 +01:00
|
|
|
after:
|
2017-12-18 23:21:39 +01:00
|
|
|
m_action->setEnabled(true);
|
2017-12-16 21:11:06 +01:00
|
|
|
m_reply = Q_NULLPTR;
|
|
|
|
|
}
|