2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20 #include "notificationmonitor.h"
21 #include "notificationmodel.h"
23 #include <AkonadiWidgets/ControlGui>
25 #include <KLocalizedString>
28 #include <QHeaderView>
32 #include <QVBoxLayout>
33 #include <QPushButton>
34 #include <QFileDialog>
36 NotificationMonitor::NotificationMonitor(QWidget
*parent
) :
39 m_model
= new NotificationModel(this);
40 m_model
->setEnabled(false); // since it can be slow, default to off
42 QVBoxLayout
*layout
= new QVBoxLayout(this);
44 QCheckBox
*enableCB
= new QCheckBox(this);
45 enableCB
->setText(QStringLiteral("Enable notification monitor"));
46 enableCB
->setChecked(m_model
->isEnabled());
47 connect(enableCB
, &QCheckBox::toggled
, m_model
, &NotificationModel::setEnabled
);
48 layout
->addWidget(enableCB
);
50 QTreeView
*tv
= new QTreeView(this);
51 tv
->setModel(m_model
);
53 tv
->setAlternatingRowColors(true);
54 tv
->setContextMenuPolicy(Qt::CustomContextMenu
);
55 tv
->header()->setResizeMode(QHeaderView::ResizeToContents
);
56 connect(tv
, &QTreeView::customContextMenuRequested
, this, &NotificationMonitor::contextMenu
);
57 layout
->addWidget(tv
);
59 QHBoxLayout
*layout2
= new QHBoxLayout
;
60 QPushButton
*button
= new QPushButton(QStringLiteral("Save to file..."), this);
61 connect(button
, &QPushButton::clicked
, this, &NotificationMonitor::slotSaveToFile
);
62 layout2
->addWidget(button
);
63 layout2
->addStretch(1);
64 layout
->addLayout(layout2
);
66 Akonadi::ControlGui::widgetNeedsAkonadi(this);
69 void NotificationMonitor::contextMenu(const QPoint
& /*pos*/)
72 menu
.addAction(QStringLiteral("Clear View"), m_model
, SLOT(clear()));
73 menu
.exec(QCursor::pos());
76 void NotificationMonitor::slotSaveToFile()
78 const QString fileName
= QFileDialog::getSaveFileName(this);
79 if (fileName
.isEmpty()) {
84 if (!file
.open(QIODevice::WriteOnly
| QIODevice::Truncate
)) {
88 file
.write("Operation/ID\tType/RID\tSession/REV\tResource/MimeType\tDestination Resource\tParent\tDestination\tParts\tAdded Flags\tRemoved Flags\n");
90 writeRows(QModelIndex(), file
, 0);
95 void NotificationMonitor::writeRows(const QModelIndex
&parent
, QFile
&file
, int indentLevel
)
97 for (int row
= 0; row
< m_model
->rowCount(parent
); ++row
) {
99 for (int tabs
= 0; tabs
< indentLevel
; ++tabs
) {
102 const int columnCount
= m_model
->columnCount(parent
);
103 for (int column
= 0; column
< columnCount
; ++column
) {
104 const QModelIndex index
= m_model
->index(row
, column
, parent
);
105 data
+= index
.data().toByteArray();
106 if (column
< columnCount
- 1) {
113 const QModelIndex index
= m_model
->index(row
, 0, parent
);
114 writeRows(index
, file
, indentLevel
+ 1);