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>
23using namespace Qt::Literals::StringLiterals;
24
25using namespace Akonadi::Search;
26
27class Akonadi::Search::AkonadiSearchDebugDialogPrivate
28{
29public:
30 AkonadiSearchDebugWidget *mAkonadiSearchDebugWidget = nullptr;
31};
32
33namespace
34{
35static const char myAkonadiSearchDebugDialogConfigGroupName[] = "AkonadiSearchDebugDialog";
36}
37AkonadiSearchDebugDialog::AkonadiSearchDebugDialog(QWidget *parent)
38 : QDialog(parent)
39 , d(new Akonadi::Search::AkonadiSearchDebugDialogPrivate)
40{
41 // Don't translate it's just a dialog to debug
42 setWindowTitle(QStringLiteral("Debug Akonadi Search"));
43
44 auto mainLayout = new QVBoxLayout(this);
45 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
46 auto user1Button = new QPushButton;
47 buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
48 // Don't translate it.
49 user1Button->setText(QStringLiteral("Save As..."));
50 connect(user1Button, &QPushButton::clicked, this, &AkonadiSearchDebugDialog::slotSaveAs);
51 connect(buttonBox, &QDialogButtonBox::rejected, this, &AkonadiSearchDebugDialog::reject);
52 d->mAkonadiSearchDebugWidget = new AkonadiSearchDebugWidget(this);
53 d->mAkonadiSearchDebugWidget->setObjectName("akonadisearchdebugwidget"_L1);
54 mainLayout->addWidget(d->mAkonadiSearchDebugWidget);
55 mainLayout->addWidget(buttonBox);
56 readConfig();
57}
58
59AkonadiSearchDebugDialog::~AkonadiSearchDebugDialog()
60{
61 writeConfig();
62 delete d;
63}
64
65void AkonadiSearchDebugDialog::readConfig()
66{
67 create(); // ensure a window is created
68 windowHandle()->resize(QSize(800, 600));
69 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAkonadiSearchDebugDialogConfigGroupName));
71 resize(windowHandle()->size()); // workaround for QTBUG-40584
72}
73
74void AkonadiSearchDebugDialog::writeConfig()
75{
76 KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myAkonadiSearchDebugDialogConfigGroupName));
78 group.sync();
79}
80
81void AkonadiSearchDebugDialog::setAkonadiId(Akonadi::Item::Id akonadiId)
82{
83 d->mAkonadiSearchDebugWidget->setAkonadiId(akonadiId);
84}
85
86void AkonadiSearchDebugDialog::setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)
87{
88 d->mAkonadiSearchDebugWidget->setSearchType(type);
89}
90
91void AkonadiSearchDebugDialog::doSearch()
92{
93 d->mAkonadiSearchDebugWidget->doSearch();
94}
95
96void AkonadiSearchDebugDialog::slotSaveAs()
97{
98 const QString filter = i18n("Text Files (*.txt);;All Files (*)");
99 saveTextAs(d->mAkonadiSearchDebugWidget->plainText(), filter);
100}
101
102void AkonadiSearchDebugDialog::saveTextAs(const QString &text, const QString &filter)
103{
104 QPointer<QFileDialog> fdlg(new QFileDialog(this, QString(), QString(), filter));
105 fdlg->setAcceptMode(QFileDialog::AcceptSave);
106 if (fdlg->exec() == QDialog::Accepted && fdlg) {
107 const QString fileName = fdlg->selectedFiles().at(0);
108 if (!saveToFile(fileName, text)) {
110 i18n("Could not write the file %1:\n"
111 "\"%2\" is the detailed error description.",
112 fileName,
113 QString::fromLocal8Bit(strerror(errno))),
114 i18nc("@title:window", "Save File Error"));
115 }
116 }
117 delete fdlg;
118}
119
120bool AkonadiSearchDebugDialog::saveToFile(const QString &filename, const QString &text)
121{
122 QFile file(filename);
123 if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
124 return false;
125 }
126 QTextStream out(&file);
127 out << text;
128 file.close();
129 return true;
130}
131
132#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)
const QChar at(qsizetype position) const const
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 Mon Nov 4 2024 16:37:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.