akonadisearchdebugdialog.cpp 3.79 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
2
  SPDX-FileCopyrightText: 2014-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
  SPDX-License-Identifier: GPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5 6
*/

7 8
#include <errno.h>

Laurent Montel's avatar
Laurent Montel committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include "akonadisearchdebugdialog.h"
#include "akonadisearchdebugwidget.h"

#include <KLocalizedString>
#include <KMessageBox>
#include <QVBoxLayout>
#include <KSharedConfig>
#include <KConfigGroup>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QPointer>
#include <QFileDialog>

using namespace Akonadi::Search;

class Akonadi::Search::AkonadiSearchDebugDialogPrivate
{
public:
Laurent Montel's avatar
Laurent Montel committed
27
    AkonadiSearchDebugWidget *mAkonadiSearchDebugWidget = nullptr;
Laurent Montel's avatar
Laurent Montel committed
28 29 30
};

AkonadiSearchDebugDialog::AkonadiSearchDebugDialog(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
31 32
    : QDialog(parent)
    , d(new Akonadi::Search::AkonadiSearchDebugDialogPrivate)
Laurent Montel's avatar
Laurent Montel committed
33 34 35 36
{
    //Don't translate it's just a dialog to debug
    setWindowTitle(QStringLiteral("Debug Akonadi Search"));

Laurent Montel's avatar
Laurent Montel committed
37 38
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
Laurent Montel's avatar
Laurent Montel committed
39 40 41 42 43 44
    QPushButton *user1Button = new QPushButton;
    buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
    //Don't translate it.
    user1Button->setText(QStringLiteral("Save As..."));
    connect(user1Button, &QPushButton::clicked, this, &AkonadiSearchDebugDialog::slotSaveAs);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &AkonadiSearchDebugDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
45 46 47
    d->mAkonadiSearchDebugWidget = new AkonadiSearchDebugWidget(this);
    d->mAkonadiSearchDebugWidget->setObjectName(QStringLiteral("akonadisearchdebugwidget"));
    mainLayout->addWidget(d->mAkonadiSearchDebugWidget);
Laurent Montel's avatar
Laurent Montel committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    mainLayout->addWidget(buttonBox);
    readConfig();
}

AkonadiSearchDebugDialog::~AkonadiSearchDebugDialog()
{
    writeConfig();
    delete d;
}

void AkonadiSearchDebugDialog::readConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), "AkonadiSearchDebugDialog");
    const QSize sizeDialog = group.readEntry("Size", QSize(800, 600));
    if (sizeDialog.isValid()) {
        resize(sizeDialog);
    }
}

void AkonadiSearchDebugDialog::writeConfig()
{
    KConfigGroup group(KSharedConfig::openConfig(), "AkonadiSearchDebugDialog");
    group.writeEntry("Size", size());
}

void AkonadiSearchDebugDialog::setAkonadiId(Akonadi::Item::Id akonadiId)
{
Laurent Montel's avatar
Laurent Montel committed
75
    d->mAkonadiSearchDebugWidget->setAkonadiId(akonadiId);
Laurent Montel's avatar
Laurent Montel committed
76 77 78 79
}

void AkonadiSearchDebugDialog::setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)
{
Laurent Montel's avatar
Laurent Montel committed
80
    d->mAkonadiSearchDebugWidget->setSearchType(type);
Laurent Montel's avatar
Laurent Montel committed
81 82 83 84
}

void AkonadiSearchDebugDialog::doSearch()
{
Laurent Montel's avatar
Laurent Montel committed
85
    d->mAkonadiSearchDebugWidget->doSearch();
Laurent Montel's avatar
Laurent Montel committed
86 87 88 89 90
}

void AkonadiSearchDebugDialog::slotSaveAs()
{
    const QString filter = i18n("Text Files (*.txt);;All Files (*)");
Laurent Montel's avatar
Laurent Montel committed
91
    saveTextAs(d->mAkonadiSearchDebugWidget->plainText(), filter);
Laurent Montel's avatar
Laurent Montel committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
}

void AkonadiSearchDebugDialog::saveTextAs(const QString &text, const QString &filter)
{
    QPointer<QFileDialog> fdlg(new QFileDialog(this, QString(), QString(), filter));
    fdlg->setAcceptMode(QFileDialog::AcceptSave);
    if (fdlg->exec() == QDialog::Accepted && fdlg) {
        const QString fileName = fdlg->selectedFiles().at(0);
        if (!saveToFile(fileName, text)) {
            KMessageBox::error(this,
                               i18n("Could not write the file %1:\n"
                                    "\"%2\" is the detailed error description.",
                                    fileName,
                                    QString::fromLocal8Bit(strerror(errno))),
                               i18n("Save File Error"));
        }
    }
    delete fdlg;
}

bool AkonadiSearchDebugDialog::saveToFile(const QString &filename, const QString &text)
{
    QFile file(filename);
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
        return false;
    }
    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << text;
    file.close();
    return true;
}