Automatically synchronize CoreInfo when on connect and disconnect
[quassel.git] / src / qtui / coreinfodlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "coreinfodlg.h"
22
23 #include "client.h"
24 #include "bufferwidget.h"
25
26 CoreInfoDlg::CoreInfoDlg(QWidget *parent) : QDialog(parent) {
27     ui.setupUi(this);
28     CoreInfo *coreInfo = Client::coreInfo();
29     connect(coreInfo, SIGNAL(coreDataChanged(const QVariantMap &)), this, SLOT(coreInfoChanged(const QVariantMap &)));
30
31     coreInfoChanged(coreInfo->coreData());
32
33     updateUptime();
34     startTimer(1000);
35 }
36
37
38 void CoreInfoDlg::coreInfoChanged(const QVariantMap &coreInfo) {
39     ui.labelCoreVersion->setText(coreInfo["quasselVersion"].toString());
40     ui.labelCoreVersionDate->setText(coreInfo["quasselBuildDate"].toString()); // "BuildDate" for compatibility
41     ui.labelClientCount->setNum(coreInfo["sessionConnectedClients"].toInt());
42
43     auto coreSessionSupported = false;
44     auto ids = _widgets.keys();
45     for (const auto &peerData : coreInfo["sessionConnectedClientData"].toList()) {
46         coreSessionSupported = true;
47
48         auto peerMap = peerData.toMap();
49         int peerId = peerMap["id"].toInt();
50
51         ids.removeAll(peerId);
52
53         bool isNew = false;
54         CoreSessionWidget *coreSessionWidget = _widgets[peerId];
55         if (coreSessionWidget == nullptr) {
56             coreSessionWidget = new CoreSessionWidget(ui.coreSessionScrollContainer);
57             isNew = true;
58         }
59         coreSessionWidget->setData(peerMap);
60         if (isNew) {
61             _widgets[peerId] = coreSessionWidget;
62             ui.coreSessionContainer->addWidget(coreSessionWidget);
63             connect(coreSessionWidget, SIGNAL(disconnectClicked(int)), this, SLOT(disconnectClicked(int)));
64         }
65     }
66
67     for (const auto &key : ids) {
68         delete _widgets[key];
69         _widgets.remove(key);
70     }
71
72     ui.coreSessionScrollArea->setVisible(coreSessionSupported);
73     ui.coreSessionContainer->addStretch(1);
74 }
75
76
77 void CoreInfoDlg::updateUptime() {
78     CoreInfo *coreInfo = Client::coreInfo();
79     if (coreInfo) {
80         QDateTime startTime = coreInfo->at("startTime").toDateTime();
81
82         int64_t uptime = startTime.secsTo(QDateTime::currentDateTime().toUTC());
83         int64_t updays = uptime / 86400;
84         uptime %= 86400;
85         int64_t uphours = uptime / 3600;
86         uptime %= 3600;
87         int64_t upmins = uptime / 60;
88         uptime %= 60;
89
90         QString uptimeText = tr("%n Day(s)", "", updays) +
91                              tr(" %1:%2:%3 (since %4)")
92                                      .arg(uphours, 2, 10, QChar('0'))
93                                      .arg(upmins, 2, 10, QChar('0'))
94                                      .arg(uptime, 2, 10, QChar('0'))
95                                      .arg(startTime.toLocalTime().toString(Qt::TextDate));
96         ui.labelUptime->setText(uptimeText);
97     }
98 }
99
100 void CoreInfoDlg::disconnectClicked(int peerId) {
101     Client::kickClient(peerId);
102 }