File indexing completed on 2024-12-22 04:45:36

0001 /*
0002    SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "viewlogplaintextedit.h"
0008 #include <KLocalizedString>
0009 #include <KMessageBox>
0010 #include <QAction>
0011 #include <QFileDialog>
0012 #include <QMenu>
0013 #include <QPointer>
0014 #include <QTextStream>
0015 #if HAVE_TEXT_CUSTOM_EDITOR
0016 ViewLogPlainTextEdit::ViewLogPlainTextEdit(QWidget *parent)
0017     : TextCustomEditor::PlainTextEditorWidget(new ViewLogPlainTextEditor(parent), parent)
0018 {
0019 }
0020 
0021 ViewLogPlainTextEdit::~ViewLogPlainTextEdit() = default;
0022 
0023 ViewLogPlainTextEditor::ViewLogPlainTextEditor(QWidget *parent)
0024     : TextCustomEditor::PlainTextEditor(parent)
0025 {
0026     setReadOnly(true);
0027     setSearchSupport(true);
0028 }
0029 
0030 ViewLogPlainTextEditor::~ViewLogPlainTextEditor() = default;
0031 
0032 void ViewLogPlainTextEditor::addExtraMenuEntry(QMenu *menu, QPoint pos)
0033 {
0034     Q_UNUSED(pos)
0035     auto action = new QAction(QIcon::fromTheme(QStringLiteral("document-save")), i18n("Save as &File"), this);
0036     connect(action, &QAction::triggered, this, &ViewLogPlainTextEditor::slotSaveAsFile);
0037     menu->addSeparator();
0038     menu->addAction(action);
0039 }
0040 
0041 void ViewLogPlainTextEditor::slotSaveAsFile()
0042 {
0043     saveTextAs(toPlainText(), QString(), this);
0044 }
0045 
0046 void ViewLogPlainTextEditor::saveTextAs(const QString &text, const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
0047 {
0048     QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
0049     if (!caption.isEmpty()) {
0050         fdlg->setWindowTitle(caption);
0051     }
0052     fdlg->setAcceptMode(QFileDialog::AcceptSave);
0053     if (fdlg->exec() == QDialog::Accepted) {
0054         const QString fileName = fdlg->selectedFiles().at(0);
0055         if (!saveToFile(fileName, text)) {
0056             KMessageBox::error(parent,
0057                                i18n("Could not write the file %1:\n"
0058                                     "\"%2\" is the detailed error description.",
0059                                     fileName,
0060                                     QString::fromLocal8Bit(strerror(errno))),
0061                                i18n("Save File Error"));
0062         }
0063     }
0064     delete fdlg;
0065 }
0066 
0067 bool ViewLogPlainTextEditor::saveToFile(const QString &filename, const QString &text)
0068 {
0069     QFile file(filename);
0070     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0071         return false;
0072     }
0073     QTextStream out(&file);
0074     out << text;
0075     file.close();
0076     return true;
0077 }
0078 #else
0079 ViewLogPlainTextEdit::ViewLogPlainTextEdit(QWidget *parent)
0080     : QPlainTextEdit(parent)
0081 {
0082     setReadOnly(true);
0083 }
0084 
0085 ViewLogPlainTextEdit::~ViewLogPlainTextEdit() = default;
0086 
0087 void ViewLogPlainTextEdit::contextMenuEvent(QContextMenuEvent *event)
0088 {
0089     QMenu *popup = createStandardContextMenu();
0090     if (popup) {
0091         auto action = new QAction(QIcon::fromTheme(QStringLiteral("document-save")), i18n("Save as &File"), this);
0092         connect(action, &QAction::triggered, this, &ViewLogPlainTextEdit::slotSaveAsFile);
0093         popup->addSeparator();
0094         popup->addAction(action);
0095 
0096         popup->exec(event->globalPos());
0097         delete popup;
0098     }
0099 }
0100 
0101 void ViewLogPlainTextEdit::slotSaveAsFile()
0102 {
0103     saveTextAs(toPlainText(), QString(), this);
0104 }
0105 
0106 void ViewLogPlainTextEdit::saveTextAs(const QString &text, const QString &filter, QWidget *parent, const QUrl &url, const QString &caption)
0107 {
0108     QPointer<QFileDialog> fdlg(new QFileDialog(parent, QString(), url.path(), filter));
0109     if (!caption.isEmpty()) {
0110         fdlg->setWindowTitle(caption);
0111     }
0112     fdlg->setAcceptMode(QFileDialog::AcceptSave);
0113     if (fdlg->exec() == QDialog::Accepted) {
0114         const QString fileName = fdlg->selectedFiles().at(0);
0115         if (!saveToFile(fileName, text)) {
0116             KMessageBox::error(parent,
0117                                i18n("Could not write the file %1:\n"
0118                                     "\"%2\" is the detailed error description.",
0119                                     fileName,
0120                                     QString::fromLocal8Bit(strerror(errno))),
0121                                i18n("Save File Error"));
0122         }
0123     }
0124     delete fdlg;
0125 }
0126 
0127 bool ViewLogPlainTextEdit::saveToFile(const QString &filename, const QString &text)
0128 {
0129     QFile file(filename);
0130     if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
0131         return false;
0132     }
0133     QTextStream out(&file);
0134     out << text;
0135     file.close();
0136     return true;
0137 }
0138 
0139 #endif
0140 
0141 #include "moc_viewlogplaintextedit.cpp"