d9527358c46c114acf4dfd0a2ac2040ae9f9aa71
[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 <QMessageBox>
24
25 #include "bufferwidget.h"
26 #include "client.h"
27 #include "icon.h"
28
29 CoreInfoDlg::CoreInfoDlg(QWidget *parent) : QDialog(parent) {
30     ui.setupUi(this);
31
32     // Listen for resynchronization events (pre-0.13 cores only)
33     connect(Client::instance(), SIGNAL(coreInfoResynchronized()),
34             this, SLOT(coreInfoResynchronized()));
35
36     // Update legacy core info for Quassel cores earlier than 0.13.  This does nothing on modern
37     // cores.
38     refreshLegacyCoreInfo();
39
40     // Display existing core info, set up signal handlers
41     coreInfoResynchronized();
42
43     // Warning icon
44     ui.coreUnsupportedIcon->setPixmap(icon::get("dialog-warning").pixmap(16));
45
46     updateUptime();
47     startTimer(1000);
48 }
49
50
51 void CoreInfoDlg::refreshLegacyCoreInfo() {
52     if (!Client::isConnected() || Client::isCoreFeatureEnabled(Quassel::Feature::SyncedCoreInfo)) {
53         // If we're not connected, or the core supports SyncedCoreInfo (0.13+), bail out
54         return;
55     }
56
57     // Request legacy (pre-0.13) CoreInfo object to be resynchronized (does nothing on modern cores)
58     Client::refreshLegacyCoreInfo();
59
60     // On legacy cores, CoreInfo data does not send signals.  Periodically poll for information.
61     // 15 seconds seems like a reasonable trade-off as this only happens while the dialog is open.
62     QTimer::singleShot(15 * 1000, this, SLOT(refreshLegacyCoreInfo()));
63 }
64
65
66 void CoreInfoDlg::coreInfoResynchronized() {
67     // CoreInfo object has been recreated, or this is the first time the dialog's been shown
68
69     CoreInfo *coreInfo = Client::coreInfo();
70     // Listen for changes to core information
71     connect(coreInfo, SIGNAL(coreDataChanged(const QVariantMap &)),
72             this, SLOT(coreInfoChanged(const QVariantMap &)));
73
74     // Update with any known core information set before connecting the signal.  This is needed for
75     // both modern (0.13+) and legacy cores.
76     coreInfoChanged(coreInfo->coreData());
77 }
78
79
80 void CoreInfoDlg::coreInfoChanged(const QVariantMap &coreInfo) {
81     if(coreInfo.isEmpty()) {
82         // We're missing data for some reason
83         if (Client::isConnected()) {
84             // Core info is entirely empty despite being connected.  Something's probably wrong.
85             ui.labelCoreVersion->setText(tr("Unknown"));
86             ui.labelCoreVersionDate->setText(tr("Unknown"));
87         } else {
88             // We're disconnected.  Mark as such.
89             ui.labelCoreVersion->setText(tr("Disconnected from core"));
90             ui.labelCoreVersionDate->setText(tr("Not available"));
91         }
92         ui.labelClientCount->setNum(0);
93         // Don't return, allow the code below to remove any existing session widgets
94     } else {
95         ui.labelCoreVersion->setText(coreInfo["quasselVersion"].toString());
96         // "BuildDate" for compatibility
97         ui.labelCoreVersionDate->setText(coreInfo["quasselBuildDate"].toString());
98         ui.labelClientCount->setNum(coreInfo["sessionConnectedClients"].toInt());
99     }
100
101     auto coreSessionSupported = false;
102     auto ids = _widgets.keys();
103     for (const auto &peerData : coreInfo["sessionConnectedClientData"].toList()) {
104         coreSessionSupported = true;
105
106         auto peerMap = peerData.toMap();
107         int peerId = peerMap["id"].toInt();
108
109         ids.removeAll(peerId);
110
111         bool isNew = false;
112         CoreSessionWidget *coreSessionWidget = _widgets[peerId];
113         if (coreSessionWidget == nullptr) {
114             coreSessionWidget = new CoreSessionWidget(ui.coreSessionScrollContainer);
115             isNew = true;
116         }
117         coreSessionWidget->setData(peerMap);
118         if (isNew) {
119             _widgets[peerId] = coreSessionWidget;
120             // Add this to the end of the session list, but before the default layout stretch item.
121             // The layout stretch item should never be removed, so count should always be >= 1.
122             ui.coreSessionContainer->insertWidget(ui.coreSessionContainer->count() - 1,
123                                                   coreSessionWidget, 0, Qt::AlignTop);
124             connect(coreSessionWidget, SIGNAL(disconnectClicked(int)), this, SLOT(disconnectClicked(int)));
125         }
126     }
127
128     for (const auto &key : ids) {
129         delete _widgets[key];
130         _widgets.remove(key);
131     }
132
133     ui.coreSessionScrollArea->setVisible(coreSessionSupported);
134
135     // Hide the information bar when core sessions are supported or when disconnected
136     ui.coreUnsupportedWidget->setVisible(
137                 !(coreSessionSupported || Client::isConnected() == false));
138
139     // Update uptime for immediate display, don't wait for the timer
140     updateUptime();
141 }
142
143
144 void CoreInfoDlg::updateUptime() {
145     CoreInfo *coreInfo = Client::coreInfo();
146
147     if (!Client::isConnected()) {
148         // Not connected, don't bother trying to calculate the uptime
149         ui.labelUptime->setText(tr("Not available"));
150     } else if (coreInfo->coreData().isEmpty()) {
151         // Core info is entirely empty despite being connected.  Something's probably wrong.
152         ui.labelUptime->setText(tr("Unknown"));
153     } else {
154         // Connected, format the uptime for display
155         QDateTime startTime = coreInfo->at("startTime").toDateTime();
156
157         int64_t uptime = startTime.secsTo(QDateTime::currentDateTime().toUTC());
158         int64_t updays = uptime / 86400;
159         uptime %= 86400;
160         int64_t uphours = uptime / 3600;
161         uptime %= 3600;
162         int64_t upmins = uptime / 60;
163         uptime %= 60;
164
165         QString uptimeText = tr("%n Day(s)", "", updays) +
166                              tr(" %1:%2:%3 (since %4)")
167                                      .arg(uphours, 2, 10, QChar('0'))
168                                      .arg(upmins, 2, 10, QChar('0'))
169                                      .arg(uptime, 2, 10, QChar('0'))
170                                      .arg(startTime.toLocalTime().toString(Qt::TextDate));
171         ui.labelUptime->setText(uptimeText);
172     }
173 }
174
175 void CoreInfoDlg::disconnectClicked(int peerId) {
176     Client::kickClient(peerId);
177 }
178
179 void CoreInfoDlg::on_coreUnsupportedDetails_clicked()
180 {
181     QMessageBox::warning(this,
182                          tr("Active sessions unsupported"),
183                          QString("<p><b>%1</b></p></br><p>%2</p>"
184                                  ).arg(tr("Your Quassel core is too old to show active sessions"),
185                                        tr("You need a Quassel core v0.13.0 or newer to view and "
186                                           "disconnect other connected clients.")));
187 }