-
Notifications
You must be signed in to change notification settings - Fork 16
/
timerwindow.cpp
561 lines (408 loc) · 15.8 KB
/
timerwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
/*
* Copyright 2010 Kyle M Hall <[email protected]>
*
* This file is part of Libki.
*
* Libki 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, either version 3 of the License, or
* (at your option) any later version.
*
* Libki 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.
*
* You should have received a copy of the GNU General Public License
* along with Libki. If not, see <http://www.gnu.org/licenses/>.
*/
#include "timerwindow.h"
#include <QtWidgets/qdesktopwidget.h>
#include <QPainter>
#include <QPixmap>
#include <QIcon>
#include <QScreen>
#include "sessionlockedwindow.h"
#include "utils.h"
#include "timesplash.h"
#ifdef Q_OS_WINDOWS
#include <windows.h>
#include <winuser.h>
#include <sysinfoapi.h>
#endif
#define INACTIVITY_CHECK_INTERVAL 10
TimerWindow::TimerWindow(QWidget *parent) : QMainWindow(parent) {
qDebug("ENTER TimerWindow::TimerWindow");
setAllowClose(false);
setupUi(this);
libkiIcon = QIcon(":/images/images/libki_clock.png");
this->setWindowIcon(libkiIcon);
// Prevent the window from being resized
setFixedSize(width(), height());
// Remove the maximize window button
setWindowFlags((windowFlags() | Qt::CustomizeWindowHint) &
~Qt::WindowMaximizeButtonHint);
// Remove the close window button
setWindowFlags((windowFlags() | Qt::CustomizeWindowHint) &
~Qt::WindowSystemMenuHint);
setupActions();
setupTrayIcon();
// Set up the timer splash
QPixmap pixmap(":/images/images/time_splash_background.png");
timeSplash = new TimeSplash( this, pixmap, Qt::WindowStaysOnTopHint );
trayIconPopupTimer = new QTimer(this);
connect(trayIconPopupTimer, SIGNAL(timeout()), this,
SLOT(showSystemTrayIconTimeLeftMessage()));
inactivityTimer = new QTimer(this);
connect(inactivityTimer, SIGNAL(timeout()), this, SLOT(checkForInactivity()));
this->move(QApplication::desktop()->screen()->rect().center() -
this->rect().center());
this->hide();
qDebug("LEAVE TimerWindow::TimerWindow");
}
TimerWindow::~TimerWindow() {}
void TimerWindow::startTimer(QString newUsername, QString newPassword,
int minutes, int hold_items_count) {
qDebug("ENTER TimerWindow::startTimer");
username = newUsername;
password = newPassword;
sessionLockedWindow = new SessionLockedWindow(0, username, password);
connect(sessionLockedWindow, SIGNAL(unlockSession()), this,
SLOT(unlockSession()));
sessionLockedWindow->hide();
trayIconPopupTimer->start(1000 * 60); // Fire once a minute
secondsSinceLastActivity = 0;
inactivityTimer->start(1000 * INACTIVITY_CHECK_INTERVAL);
this->show();
trayIcon->show();
minutesRemaining = minutesAtStart = minutes;
updateClock();
QSettings settings;
settings.setIniCodec("UTF-8");
if (settings.value("session/EnableClientSessionLocking").toBool()) {
connect(lockSessionButton, SIGNAL(clicked(bool)), this,
SLOT(lockSession()));
} else {
lockSessionButton->hide();
}
QString waiting_holds_message =
tr("You have one or more items on hold waiting for pickup. Please "
"contact a librarian for more details");
QString label = getLabel("waiting_holds");
if (!label.isEmpty()) {
waiting_holds_message = label;
}
if (hold_items_count > 0) {
this->showMessage(waiting_holds_message);
qApp->processEvents();
}
qDebug("LEAVE TimerWindow::startTimer");
}
void TimerWindow::stopTimer() {
qDebug("ENTER TimerWindow::stopTimer");
inactivityTimer->stop();
trayIconPopupTimer->stop();
trayIcon->hide();
this->hide();
if (sessionLockedWindow) {
sessionLockedWindow->hide();
delete sessionLockedWindow;
sessionLockedWindow = Q_NULLPTR;
}
emit timerStopped();
qDebug("LEAVE TimerWindow::stopTimer");
}
void TimerWindow::updateClock() {
qDebug("ENTER TimerWindow::updateClock");
QSettings settings;
settings.setIniCodec("UTF-8");
/* Convert minutes remaining into Hours::Minutes */
int hours = minutesRemaining / 60;
int minutes = minutesRemaining % 60;
QString time = QString::number(hours) + ":" +
QString::number(minutes).rightJustified(2, '0');
lcdNumber->display(time);
/* Update the progress bar */
if (minutesRemaining > minutesAtStart) minutesAtStart = minutesRemaining;
progressBar->setRange(0, minutesAtStart);
progressBar->setValue(minutesRemaining);
QString minutesString = QString::number(minutesRemaining);
trayIcon->setToolTip( minutesString + " " +
tr("Minutes Left"));
this->setWindowTitle("Libki " + time);
if ( settings.value("node/showTimeRemainingInTray").toInt() == 1 ) {
// Update the system tray icon
QPixmap libkiIcon = QPixmap(":/images/images/tray.png");
QPainter painter(&libkiIcon);
QFont font = painter.font() ;
font.setBold(true);
if ( minutesRemaining < 10 ) {
font.setPointSize(14);
} else if ( minutesRemaining < 100 ) {
font.setPointSize(11);
} else {
font.setPointSize(9);
}
painter.setFont(font);
if ( this->swapColors ) {
painter.setPen(QColor(Qt::black));
} else {
painter.setPen(QColor(Qt::white));
}
this->swapColors = !this->swapColors;
painter.drawText(libkiIcon.rect(), Qt::AlignCenter, minutesString);
trayIcon->setIcon(libkiIcon);
}
bool showSplash = settings.value("node/showTimeRemainingInSplash").toInt() == 1;
if ( sessionLockedWindow && sessionLockedWindow->isVisible() ) showSplash = false;
if ( showSplash ) {
// Update the time splash
QScreen* screen = QGuiApplication::screens()[0];
QRect screenrect = screen->availableGeometry();
QPixmap pixmap(":/images/images/time_splash_background.png");
QPainter painter(&pixmap);
QFont font = painter.font() ;
font.setBold(true);
font.setPointSize(30);
painter.setFont(font);
painter.drawText(pixmap.rect(), Qt::AlignCenter, minutesString);
timeSplash->setPixmap(pixmap);
timeSplash->move(screenrect.right() - timeSplash->width(), screenrect.bottom() - timeSplash->height());
timeSplash->show();
timeSplash->raise(); // Some X11 window managers do not support the "stays on top" flag. A solution is to set up a timer that periodically calls raise() on the splash screen to simulate the "stays on top" effect.
QCoreApplication::processEvents();
} else {
timeSplash->hide();
}
qDebug("LEAVE TimerWindow::updateClock");
}
void TimerWindow::updateTimeLeft(int minutes) {
qDebug() << QString("ENTER TimerWindow::updateTimeLeft(%1)").arg(minutes);
minutesRemaining = minutes;
updateClock();
if (minutesRemaining <= 0) {
emit requestLogout();
}
qDebug() << QString("LEAVE TimerWindow::updateTimeLeft(%1)").arg(minutes);
}
void TimerWindow::doLogoutDialog() {
qDebug("ENTER TimerWindow::doLogoutDialog");
QMessageBox msgBox;
msgBox.setWindowIcon(libkiIcon);
msgBox.setIcon(QMessageBox::Question);
msgBox.setText(tr("Log Out?"));
msgBox.setInformativeText(tr("Are you sure you want to log out?"));
msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Yes);
msgBox.setButtonText(QMessageBox::Yes, tr("Yes"));
msgBox.setButtonText(QMessageBox::Cancel, tr("Cancel"));
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Yes:
emit requestLogout();
break;
case QMessageBox::Cancel:
break;
default:
break;
}
qDebug("LEAVE TimerWindow::doLogoutDialog");
}
void TimerWindow::setupActions() {
qDebug("ENTER TimerWindow::setupActions");
connect(logoutButton, SIGNAL(clicked()), this, SLOT(doLogoutDialog()));
qDebug("LEAVE TimerWindow::setupActions");
}
void TimerWindow::setupTrayIcon() {
qDebug("ENTER TimerWindow::setupTrayIcon");
trayIconMenu = new QMenu(this);
QAction *logoutAction =
new QAction(QIcon(":icons/system_log_out.png"), tr("Log Out"), this);
connect(logoutAction, SIGNAL(triggered()), this, SLOT(doLogoutDialog()));
trayIconMenu->addAction(logoutAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
QPixmap libkiIcon = QPixmap(":/icons/images/icons/libki.ico");
trayIcon->setIcon(libkiIcon);
connect(trayIcon, SIGNAL(messageClicked()), this, SLOT(restoreTimerWindow()));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this,
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
qDebug("LEAVE TimerWindow::setupTrayIcon");
}
void TimerWindow::iconActivated(QSystemTrayIcon::ActivationReason reason) {
qDebug("ENTER TimerWindow::iconActivated");
switch (reason) {
case QSystemTrayIcon::Trigger:
break;
case QSystemTrayIcon::DoubleClick:
restoreTimerWindow();
break;
case QSystemTrayIcon::MiddleClick:
break;
case QSystemTrayIcon::Context:
qDebug("CONTEXT MENU");
trayIcon->contextMenu()->showNormal();
break;
default:
break;
}
qDebug("LEAVE TimerWindow::iconActivated");
}
void TimerWindow::restoreTimerWindow() {
qDebug("ENTER TimerWindow::restoreTimerWindow");
// TODO: TimerWindow will not come to front if behind other windows. Needed
// for showMessage().
// this->setWindowState(Qt::WindowStates(Qt::WindowActive)); // Doesn't help.
this->activateWindow();
this->raise();
this->showNormal();
qDebug("LEAVE TimerWindow::restoreTimerWindow");
}
void TimerWindow::showSystemTrayIconTimeLeftMessage() {
qDebug("ENTER TimerWindow::showSystemTrayIconTimeLeftMessage");
QSettings settings;
settings.setIniCodec("UTF-8");
int clientTimeNotificationFrequency = 5;
if (!settings.value("session/ClientTimeNotificationFrequency").toString().isEmpty()) {
clientTimeNotificationFrequency = settings.value("session/ClientTimeNotificationFrequency").toInt();
clientTimeNotificationFrequency = clientTimeNotificationFrequency > 0 ? clientTimeNotificationFrequency : 5;
}
int clientTimeWarningThreshold = 5;
if (!settings.value("session/ClientTimeWarningThreshold").toString().isEmpty()) {
clientTimeWarningThreshold = settings.value("session/ClientTimeWarningThreshold").toInt();
clientTimeWarningThreshold = clientTimeWarningThreshold > 0 ? clientTimeWarningThreshold : 5;
}
QString title = tr("Time Remaining");
QString message =
QString::number(minutesRemaining) + " " + tr("Minutes Left");
if (!(minutesRemaining % clientTimeNotificationFrequency)) {
trayIcon->showMessage(title, message, QSystemTrayIcon::Information, 1000);
qApp->processEvents();
} else if (minutesRemaining <= clientTimeWarningThreshold) {
trayIcon->showMessage(title, message, QSystemTrayIcon::Warning, 1000);
qApp->processEvents();
}
qDebug("LEAVE TimerWindow::showSystemTrayIconTimeLeftMessage");
}
void TimerWindow::checkForInactivity() {
qDebug("ENTER TimerWindow::checkForInactivity");
// TODO: keep one object level instance of settings for each of TimerWindow
// and NetworkClient
QSettings settings;
settings.setIniCodec("UTF-8");
int inactivityLogout = 0;
if (!settings.value("node/inactivityLogout").toString().isEmpty()) {
inactivityLogout = settings.value("node/inactivityLogout").toInt();
} else if (!settings.value("session/inactivityLogout").toString().isEmpty()) {
inactivityLogout = settings.value("session/inactivityLogout").toInt();
}
qDebug() << "INACTIVIY LOGOUT: " << inactivityLogout;
int inactivityWarning = 5;
if (!settings.value("node/inactivityWarning").toString().isEmpty()) {
inactivityWarning = settings.value("node/inactivityWarning").toInt();
} else if (!settings.value("session/inactivityWarning")
.toString()
.isEmpty()) {
inactivityWarning = settings.value("session/inactivityWarning").toInt();
}
qDebug() << "INACTIVIY WARNING: " << inactivityWarning;
if (inactivityLogout > 0) {
#ifdef Q_OS_WIN
systemTickRangeEnd = (GetTickCount());
LASTINPUTINFO lastInput;
lastInput.cbSize = sizeof(lastInput);
GetLastInputInfo(&lastInput);
lastInputTick = lastInput.dwTime;
if ((lastInputTick >= systemTickRangeStart) && (lastInputTick <= systemTickRangeEnd)) {
userIdle = false;
} else {
userIdle = true;
}
systemTickRangeStart = (GetTickCount());
#else
QPoint pos = QCursor::pos();
int x = pos.x();
int y = pos.y();
// TODO: Implement ranges to account for mouse wobble?
if (prevMousePosX == x && prevMousePosY == y) {
userIdle = true;
} else {
userIdle = false;
}
prevMousePosX = x;
prevMousePosY = y;
#endif
if (userIdle == true) {
secondsSinceLastActivity += INACTIVITY_CHECK_INTERVAL;
qDebug() << "No activity detected. Seconds since last activity: "
<< secondsSinceLastActivity;
} else {
secondsSinceLastActivity = 0;
qDebug() << "Activity detected. Seconds since last activity: 0";
}
if (secondsSinceLastActivity / 60 >= inactivityWarning) {
QString title = tr("Inactivity detected");
QString message = tr("Please confirm you are still using this computer.");
/* This would be useful if we could bring the timer window to the
* forefront, but there appears to be no way to do that QMessageBox*
* msgBox = new QMessageBox( this ); msgBox->setAttribute(
* Qt::WA_DeleteOnClose ); //makes sure the msgbox is deleted
* automatically when closed msgBox->setStandardButtons( QMessageBox::Ok
* ); msgBox->setWindowTitle( title ); msgBox->setText( message );
* msgBox->setWindowState( (windowState() & ~Qt::WindowMinimized) |
* Qt::WindowActive); msgBox->raise(); msgBox->activateWindow();
* msgBox->open();
*/
QCoreApplication::processEvents(); // Should clear out any previous
// system tray message
trayIcon->showMessage(title, message, QSystemTrayIcon::Critical, 100000);
qApp->processEvents();
}
if (secondsSinceLastActivity / 60 >= inactivityLogout) {
emit requestLogout();
}
}
qDebug("LEAVE TimerWindow::checkForInactivity");
}
void TimerWindow::showMessage(QString message) {
qDebug() << QString("ENTER TimerWindow::showMessage(%1)").arg(message);
QMessageBox msgBox;
msgBox.setWindowIcon(libkiIcon);
msgBox.setIcon(QMessageBox::Information);
msgBox.setText(tr("You have a message"));
msgBox.setInformativeText(message);
this->restoreTimerWindow();
msgBox.exec();
qDebug() << QString("LEAVE TimerWindow::showMessage(%1)").arg(message);
}
void TimerWindow::lockSession() {
qDebug("ENTER TimerWindow::lockSession()");
QProcess::startDetached("windows/on_startup.exe");
this->hide();
timeSplash->hide();
sessionLockedWindow->show();
qDebug("LEAVE TimerWindow::lockSession()");
}
void TimerWindow::unlockSession() {
qDebug("ENTER TimerWindow::unlockSession");
QProcess::startDetached("windows/on_login.exe");
sessionLockedWindow->hide();
this->show();
qDebug("LEAVE TimerWindow::unlockSession");
}
void TimerWindow::getSettings() {}
void TimerWindow::setAllowClose(bool close) {
qDebug("ENTER TimerWindow::setAllowClose");
allowClose = close;
qDebug("LEAVE TimerWindow::setAllowClose");
}
/* Reimplemented closeEvent to prevent application from being closed. */
void TimerWindow::closeEvent(QCloseEvent *event) {
qDebug("ENTER TimerWindow::closeEvent");
if (allowClose) {
event->accept();
} else {
event->ignore();
}
qDebug("LEAVE TimerWindow::closeEvent");
}