Akonadi Search

akonadisearchdebugdialog.cpp
1/*
2 SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include <cerrno>
8
9#include "akonadisearchdebugdialog.h"
10#include "akonadisearchdebugwidget.h"
11
12#include <KConfigGroup>
13#include <KLocalizedString>
14#include <KMessageBox>
15#include <KSharedConfig>
16#include <KWindowConfig>
17#include <QDialogButtonBox>
18#include <QFileDialog>
19#include <QPointer>
20#include <QPushButton>
21#include <QVBoxLayout>
22#include <QWindow>
23
24using namespace Akonadi::Search;
25
26class Akonadi::Search::AkonadiSearchDebugDialogPrivate
27{
28public:
29 AkonadiSearchDebugWidget *mAkonadiSearchDebugWidget = nullptr;
30};
31
32namespace
33{
34static const char myAkonadiSearchDebugDialogConfigGroupName[] = "AkonadiSearchDebugDialog";
35}
36AkonadiSearchDebugDialog::AkonadiSearchDebugDialog(QWidget *parent)
37 : QDialog(parent)
38 , d(new Akonadi::Search::AkonadiSearchDebugDialogPrivate)
39{
40 // Don't translate it's just a dialog to debug
41 setWindowTitle(QStringLiteral("Debug Akonadi Search"));
42
43 auto mainLayout = new QVBoxLayout(this);
44 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
45 auto user1Button = new QPushButton;
46 buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
47 // Don't translate it.
48 user1Button->setText(QStringLiteral("Save As..."));
49 connect(user1Button, &QPushButton::clicked, this, &AkonadiSearchDebugDialog::slotSaveAs);
50 connect(buttonBox, &QDialogButtonBox::rejected, this, &AkonadiSearchDebugDialog::reject);
51 d->mAkonadiSearchDebugWidget = new AkonadiSearchDebugWidget(this);
52 d->mAkonadiSearchDebugWidget->setObjectName(QLatin1StringView("akonadisearchdebugwidget"));
53 mainLayout->addWidget(d->mAkonadiSearchDebugWidget);
54 mainLayout->addWidget(buttonBox);
55 readConfig();
56}
57
58AkonadiSearchDebugDialog::~AkonadiSearchDebugDialog()
59{
60 writeConfig();
61 delete d;
62}
63
64void AkonadiSearchDebugDialog::readConfig()
65{
66 create(); // ensure a window is created
67 windowHandle()->resize(QSize(800, 600));
70 resize(windowHandle()->size()); // workaround for QTBUG-40584
71}
72
73void AkonadiSearchDebugDialog::writeConfig()
74{
77 group.sync();
78}
79
80void AkonadiSearchDebugDialog::setAkonadiId(Akonadi::Item::Id akonadiId)
81{
82 d->mAkonadiSearchDebugWidget->setAkonadiId(akonadiId);
83}
84
85void AkonadiSearchDebugDialog::setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)
86{
87 d->mAkonadiSearchDebugWidget->setSearchType(type);
88}
89
90void AkonadiSearchDebugDialog::doSearch()
91{
92 d->mAkonadiSearchDebugWidget->doSearch();
93}
94
95void AkonadiSearchDebugDialog::slotSaveAs()
96{
97 const QString filter = i18n("Text Files (*.txt);;All Files (*)");
98 saveTextAs(d->mAkonadiSearchDebugWidget->plainText(), filter);
99}
100
101void AkonadiSearchDebugDialog::saveTextAs(const QString &text, const QString &filter)
102{
103 QPointer<QFileDialog> fdlg(new QFileDialog(this, QString(), QString(), filter));
104 fdlg->setAcceptMode(QFileDialog::AcceptSave);
105 if (fdlg->exec() == QDialog::Accepted && fdlg) {
106 const QString fileName = fdlg->selectedFiles().at(0);
107 if (!saveToFile(fileName, text)) {
109 i18n("Could not write the file %1:\n"
110 "\"%2\" is the detailed error description.",
111 fileName,
113 i18nc("@title:window", "Save File Error"));
114 }
115 }
116 delete fdlg;
117}
118
119bool AkonadiSearchDebugDialog::saveToFile(const QString &filename, const QString &text)
120{
121 QFile file(filename);
122 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
123 return false;
124 }
125 QTextStream out(&file);
126 out << text;
127 file.close();
128 return true;
129}
130
131#include "moc_akonadisearchdebugdialog.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Akonadi search infrastructure.
Definition core/query.h:21
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KCONFIGGUI_EXPORT void saveWindowSize(const QWindow *window, KConfigGroup &config, KConfigGroup::WriteConfigFlags options=KConfigGroup::Normal)
KCONFIGGUI_EXPORT void restoreWindowSize(QWindow *window, const KConfigGroup &config)
void clicked(bool checked)
void setText(const QString &text)
T qobject_cast(QObject *object)
QString fromLocal8Bit(QByteArrayView str)
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void create(WId window, bool initializeWindow, bool destroyOldWindow)
void resize(const QSize &)
QWindow * windowHandle() const const
void resize(const QSize &newSize)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:15:27 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.