1/****************************************************************************
2**
3** Copyright (C) 2020 The Qt Company Ltd and/or its subsidiary(-ies).
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the tools applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include "mainwindow.h"
30
31#include "qdbusviewer.h"
32
33#include <QtWidgets/QApplication>
34#include <QtWidgets/QTabWidget>
35#include <QtWidgets/QMenuBar>
36#include <QtWidgets/QMenu>
37#include <QtWidgets/QAction>
38#include <QtWidgets/QMessageBox>
39
40#include <QtDBus/QDBusConnection>
41#include <QtCore/QSettings>
42
43MainWindow::MainWindow(QWidget *parent)
44 : QMainWindow(parent)
45{
46 QMenu *fileMenu = menuBar()->addMenu(title: tr(s: "&File"));
47 QAction *quitAction = fileMenu->addAction(text: tr(s: "&Quit"), object: this, slot: &QWidget::close);
48 quitAction->setShortcut(QKeySequence::Quit);
49 quitAction->setMenuRole(QAction::QuitRole);
50
51 QMenu *helpMenu = menuBar()->addMenu(title: tr(s: "&Help"));
52 QAction *aboutAction = helpMenu->addAction(text: tr(s: "&About"));
53 aboutAction->setMenuRole(QAction::AboutRole);
54 QObject::connect(sender: aboutAction, signal: &QAction::triggered, receiver: this, slot: &MainWindow::about);
55
56 QAction *aboutQtAction = helpMenu->addAction(text: tr(s: "About &Qt"));
57 aboutQtAction->setMenuRole(QAction::AboutQtRole);
58 QObject::connect(sender: aboutQtAction, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt);
59
60 tabWidget = new QTabWidget;
61 setCentralWidget(tabWidget);
62
63 sessionBusViewer = new QDBusViewer(QDBusConnection::sessionBus());
64 systemBusViewer = new QDBusViewer(QDBusConnection::systemBus());
65 tabWidget->addTab(widget: sessionBusViewer, tr(s: "Session Bus"));
66 tabWidget->addTab(widget: systemBusViewer, tr(s: "System Bus"));
67
68 restoreSettings();
69}
70
71MainWindow::~MainWindow()
72{
73 saveSettings();
74}
75
76void MainWindow::addCustomBusTab(const QString &busAddress)
77{
78 QDBusConnection connection = QDBusConnection::connectToBus(address: busAddress, name: "QDBusViewer");
79 if (connection.isConnected()) {
80 QDBusViewer *customBusViewer = new QDBusViewer(connection);
81 tabWidget->addTab(widget: customBusViewer, tr(s: "Custom Bus"));
82 }
83}
84
85void MainWindow::about()
86{
87 QMessageBox box(this);
88
89 box.setText(QString::fromLatin1(str: "<center><img src=\":/qt-project.org/qdbusviewer/images/qdbusviewer-128.png\">"
90 "<h3>%1</h3>"
91 "<p>Version %2</p></center>"
92 "<p>Copyright (C) %3 The Qt Company Ltd.</p>")
93 .arg(args: tr(s: "D-Bus Viewer"), args: QLatin1String(QT_VERSION_STR), QStringLiteral("2022")));
94 box.setWindowTitle(tr(s: "D-Bus Viewer"));
95 box.exec();
96}
97
98static inline QString windowGeometryKey() { return QStringLiteral("WindowGeometry"); }
99static inline QString sessionTabGroup() { return QStringLiteral("SessionTab"); }
100static inline QString systemTabGroup() { return QStringLiteral("SystemTab"); }
101
102void MainWindow::saveSettings()
103{
104 QSettings settings;
105
106 settings.setValue(key: windowGeometryKey(), value: saveGeometry());
107
108 settings.beginGroup(prefix: sessionTabGroup());
109 sessionBusViewer->saveState(settings: &settings);
110 settings.endGroup();
111
112 settings.beginGroup(prefix: systemTabGroup());
113 systemBusViewer->saveState(settings: &settings);
114 settings.endGroup();
115}
116
117void MainWindow::restoreSettings()
118{
119 QSettings settings;
120
121 restoreGeometry(geometry: settings.value(key: windowGeometryKey()).toByteArray());
122
123 settings.beginGroup(prefix: sessionTabGroup());
124 sessionBusViewer->restoreState(settings: &settings);
125 settings.endGroup();
126
127 settings.beginGroup(prefix: systemTabGroup());
128 systemBusViewer->restoreState(settings: &settings);
129 settings.endGroup();
130}
131

source code of qttools/src/qdbus/qdbusviewer/mainwindow.cpp