1// Copyright (C) 2020 The Qt Company Ltd and/or its subsidiary(-ies).
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#include "mainwindow.h"
5
6#include "qdbusviewer.h"
7
8#include <QtWidgets/QApplication>
9#include <QtWidgets/QTabWidget>
10#include <QtWidgets/QMenuBar>
11#include <QtWidgets/QMenu>
12#include <QtGui/QAction>
13#include <QtWidgets/QMessageBox>
14
15#include <QtDBus/QDBusConnection>
16#include <QtCore/QSettings>
17
18using namespace Qt::StringLiterals;
19
20MainWindow::MainWindow(QWidget *parent)
21 : QMainWindow(parent)
22 , systemBusViewer(nullptr)
23{
24 QMenu *fileMenu = menuBar()->addMenu(title: tr(s: "&File"));
25 QAction *quitAction = fileMenu->addAction(text: tr(s: "&Quit"), args: this, args: &QWidget::close);
26 quitAction->setShortcut(QKeySequence::Quit);
27 quitAction->setMenuRole(QAction::QuitRole);
28
29 QMenu *helpMenu = menuBar()->addMenu(title: tr(s: "&Help"));
30 QAction *aboutAction = helpMenu->addAction(text: tr(s: "&About"));
31 aboutAction->setMenuRole(QAction::AboutRole);
32 QObject::connect(sender: aboutAction, signal: &QAction::triggered, context: this, slot: &MainWindow::about);
33
34 QAction *aboutQtAction = helpMenu->addAction(text: tr(s: "About &Qt"));
35 aboutQtAction->setMenuRole(QAction::AboutQtRole);
36 QObject::connect(sender: aboutQtAction, signal: &QAction::triggered, qApp, slot: &QApplication::aboutQt);
37
38 tabWidget = new QTabWidget;
39 setCentralWidget(tabWidget);
40
41 sessionBusViewer = new QDBusViewer(QDBusConnection::sessionBus());
42 tabWidget->addTab(widget: sessionBusViewer, tr(s: "Session Bus"));
43
44 QDBusConnection connection = QDBusConnection::systemBus();
45 if (connection.isConnected()) {
46 systemBusViewer = new QDBusViewer(connection);
47 tabWidget->addTab(widget: systemBusViewer, tr(s: "System Bus"));
48 }
49
50 restoreSettings();
51}
52
53MainWindow::~MainWindow()
54{
55 saveSettings();
56}
57
58void MainWindow::addCustomBusTab(const QString &busAddress)
59{
60 QDBusConnection connection = QDBusConnection::connectToBus(address: busAddress, name: "QDBusViewer");
61 if (connection.isConnected()) {
62 QDBusViewer *customBusViewer = new QDBusViewer(connection);
63 tabWidget->addTab(widget: customBusViewer, tr(s: "Custom Bus"));
64 }
65}
66
67void MainWindow::about()
68{
69 QMessageBox box(this);
70
71 box.setText(tr(s: "<center><img src=\":/qt-project.org/qdbusviewer/images/qdbusviewer-128.png\">"
72 "<h3>%1</h3>"
73 "<p>Version %2</p></center>"
74 "<p>Copyright (C) %3 The Qt Company Ltd.</p>")
75 .arg(args: tr(s: "D-Bus Viewer"), args: QLatin1String(QT_VERSION_STR), args: u"2023"_s));
76 box.setWindowTitle(tr(s: "D-Bus Viewer"));
77 box.exec();
78}
79
80static inline QString windowGeometryKey()
81{
82 return u"WindowGeometry"_s;
83}
84
85static inline QString sessionTabGroup()
86{
87 return u"SessionTab"_s;
88}
89
90static inline QString systemTabGroup()
91{
92 return u"SystemTab"_s;
93}
94
95void MainWindow::saveSettings()
96{
97 QSettings settings;
98
99 settings.setValue(key: windowGeometryKey(), value: saveGeometry());
100
101 settings.beginGroup(prefix: sessionTabGroup());
102 sessionBusViewer->saveState(settings: &settings);
103 settings.endGroup();
104
105 if (systemBusViewer) {
106 settings.beginGroup(prefix: systemTabGroup());
107 systemBusViewer->saveState(settings: &settings);
108 settings.endGroup();
109 }
110}
111
112void MainWindow::restoreSettings()
113{
114 QSettings settings;
115
116 restoreGeometry(geometry: settings.value(key: windowGeometryKey()).toByteArray());
117
118 settings.beginGroup(prefix: sessionTabGroup());
119 sessionBusViewer->restoreState(settings: &settings);
120 settings.endGroup();
121
122 if (systemBusViewer) {
123 settings.beginGroup(prefix: systemTabGroup());
124 systemBusViewer->restoreState(settings: &settings);
125 settings.endGroup();
126 }
127}
128

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