Don't use const'ref for QChar
[kdepim.git] / akonadiconsole / notificationmonitor.cpp
blob246e3617cdd958efad53deb0c4eed8318c5ebec5
1 /*
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,
17 USA.
20 #include "notificationmonitor.h"
21 #include "notificationmodel.h"
23 #include <AkonadiWidgets/ControlGui>
25 #include <KLocalizedString>
26 #include <QUrl>
28 #include <QHeaderView>
29 #include <QCheckBox>
30 #include <QMenu>
31 #include <QTreeView>
32 #include <QVBoxLayout>
33 #include <QPushButton>
34 #include <QFileDialog>
36 NotificationMonitor::NotificationMonitor(QWidget *parent) :
37 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);
52 tv->expandAll();
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*/)
71 QMenu menu;
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()) {
80 return;
83 QFile file(fileName);
84 if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
85 return;
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);
92 file.close();
95 void NotificationMonitor::writeRows(const QModelIndex &parent, QFile &file, int indentLevel)
97 for (int row = 0; row < m_model->rowCount(parent); ++row) {
98 QByteArray data;
99 for (int tabs = 0; tabs < indentLevel; ++tabs) {
100 data += '\t';
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) {
107 data += '\t';
110 data += '\n';
111 file.write(data);
113 const QModelIndex index = m_model->index(row, 0, parent);
114 writeRows(index, file, indentLevel + 1);