File indexing completed on 2024-05-05 05:48:57

0001 /*
0002     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "logViewExport.h"
0008 
0009 #include <QApplication>
0010 #include <QClipboard>
0011 #include <QDesktopServices>
0012 #include <QFileDialog>
0013 #include <QPrintDialog>
0014 #include <QPrinter>
0015 #include <QUrlQuery>
0016 
0017 #include <KLocalizedString>
0018 #include <KMessageBox>
0019 #include <QPrintPreviewDialog>
0020 
0021 #include "ksystemlog_debug.h"
0022 
0023 #include "logLine.h"
0024 #include "logViewWidget.h"
0025 #include "logViewWidgetItem.h"
0026 
0027 #include "levelPrintPage.h"
0028 
0029 LogViewExport::LogViewExport(QWidget *parent, LogViewWidget *logViewWidget)
0030     : mParent(parent)
0031     , mLogViewWidget(logViewWidget)
0032 {
0033 }
0034 
0035 LogViewExport::~LogViewExport()
0036 {
0037 }
0038 
0039 void LogViewExport::sendMail()
0040 {
0041     qCDebug(KSYSTEMLOG) << "Exporting to mail...";
0042 
0043     QString body(i18n("Here are my logs:\n"));
0044 
0045     body += i18n("---------------------------------------\n");
0046 
0047     int i = 0;
0048     QTreeWidgetItemIterator it(mLogViewWidget, QTreeWidgetItemIterator::Selected);
0049     while (*it) {
0050         auto item = static_cast<LogViewWidgetItem *>(*it);
0051 
0052         body += item->logLine()->exportToText();
0053         body += QLatin1Char('\n');
0054 
0055         ++it;
0056         ++i;
0057     }
0058 
0059     body += i18n("---------------------------------------\n");
0060 
0061     // Too much lines selected
0062     if (i > 1000) {
0063         KMessageBox::error(mParent, i18n("You have selected too many lines. Please only select important log lines."), i18n("Too Many Lines Selected"));
0064         return;
0065     }
0066 
0067     const QUrlQuery urlQuery = {{QStringLiteral("subject"), i18n("Log Lines of my problem")}, {QStringLiteral("body"), body}};
0068     QUrl url;
0069     url.setScheme(QStringLiteral("mailto"));
0070     url.setQuery(urlQuery);
0071 
0072     QDesktopServices::openUrl(url);
0073 }
0074 
0075 void LogViewExport::print(QPrinter *printer)
0076 {
0077     // create a painter to paint on the printer object
0078     QPainter painter;
0079 
0080     // start painting
0081     painter.begin(printer);
0082 
0083     QPen const originalPen(painter.pen());
0084 
0085     QPaintDevice *painterDevice = painter.device();
0086     int const dpiy = painterDevice->logicalDpiY();
0087     const int margin = (int)((2 / 2.54) * dpiy); // 2 cm margins
0088     QRect printView(margin, margin, painterDevice->width() - 2 * margin, painterDevice->height() - 2 * margin);
0089 
0090     int page = 1;
0091 
0092     int movement = 0;
0093 
0094     QTreeWidgetItemIterator it(mLogViewWidget, QTreeWidgetItemIterator::Selected);
0095     while (*it) {
0096         auto item = static_cast<LogViewWidgetItem *>(*it);
0097         const QString body = item->logLine()->exportToText();
0098         painter.setPen(originalPen);
0099         painter.drawText(printView, Qt::AlignLeft | Qt::TextWordWrap, body);
0100         const int fontHeight = painter.fontMetrics().height();
0101         const int lines = painter.fontMetrics().boundingRect(body).width() / printView.width() + 1;
0102         const int moveBy = (fontHeight + 2) * lines;
0103         painter.translate(0, moveBy);
0104         movement += moveBy;
0105         if (movement + margin >= printView.height()) {
0106             painter.setPen(originalPen);
0107             printPageNumber(painter, printView, movement, page, margin);
0108             printer->newPage();
0109             page++;
0110             movement = 0;
0111         }
0112         ++it;
0113     }
0114 
0115     // stop painting, this will automatically send the print data to the printer
0116     painter.end();
0117 }
0118 
0119 void LogViewExport::printSelection()
0120 {
0121     qCDebug(KSYSTEMLOG) << "Printing selection...";
0122 
0123     QPrinter printer;
0124 
0125     // do some printer initialization
0126     printer.setFullPage(true);
0127 
0128     /*
0129      LevelPrintPage* dialogPage = new LevelPrintPage(parent);
0130      printer.addDialogPage(dialogPage);
0131      */
0132 
0133     // initialize the printer using the print dialog
0134     auto printDialog = new QPrintDialog(&printer, mParent);
0135     if (!printDialog->exec()) {
0136         delete printDialog;
0137         return;
0138     }
0139     delete printDialog;
0140     print(&printer);
0141 }
0142 
0143 void LogViewExport::printPreview()
0144 {
0145     qCDebug(KSYSTEMLOG) << "Printing selection...";
0146 
0147     auto dialog = new QPrintPreviewDialog(mParent);
0148     dialog->setAttribute(Qt::WA_DeleteOnClose);
0149     dialog->resize(800, 750);
0150 
0151     connect(dialog, &QPrintPreviewDialog::paintRequested, this, [=](QPrinter *printing) {
0152         print(printing);
0153     });
0154 
0155     dialog->open();
0156 }
0157 
0158 void LogViewExport::printPageNumber(QPainter &painter, QRect &printView, int movement, int page, int margin)
0159 {
0160     qCDebug(KSYSTEMLOG) << "Printing page number...";
0161 
0162     painter.translate(0, -movement);
0163     printView.moveTo(QPoint(margin, printView.height() * page + margin));
0164     painter.translate(0, -printView.height());
0165     painter.drawText(printView.right() - painter.fontMetrics().boundingRect(QString::number(page)).width(),
0166                      printView.bottom() + painter.fontMetrics().ascent() + 5,
0167                      QString::number(page));
0168 }
0169 
0170 void LogViewExport::copyToClipboard()
0171 {
0172     qCDebug(KSYSTEMLOG) << "Copying to clipboard...";
0173 
0174     int nbCopied = 0;
0175     QString text;
0176 
0177     QTreeWidgetItemIterator it(mLogViewWidget, QTreeWidgetItemIterator::Selected);
0178     while (*it) {
0179         auto item = static_cast<LogViewWidgetItem *>(*it);
0180 
0181         // Copy the item content to the text string
0182         text.append(item->logLine()->exportToText());
0183         text.append(QLatin1Char('\n'));
0184 
0185         ++it;
0186         nbCopied++;
0187     }
0188 
0189     // Copy text value only if it is not empty
0190     if (nbCopied == 0) {
0191         Q_EMIT statusBarChanged(i18n("No items selected. Nothing copied to clipboard."));
0192     } else {
0193         // Copy both to clipboard and X11-selection
0194         QApplication::clipboard()->setText(text, QClipboard::Clipboard);
0195         QApplication::clipboard()->setText(text, QClipboard::Selection);
0196 
0197         Q_EMIT statusBarChanged(i18np("1 log line copied to clipboard.", "%1 log lines copied to clipboard.", nbCopied));
0198     }
0199 
0200     qCDebug(KSYSTEMLOG) << "Copied " << nbCopied << " log lines";
0201 }
0202 
0203 void LogViewExport::fileSave()
0204 {
0205     qCDebug(KSYSTEMLOG) << "Saving to a file...";
0206 
0207     QTreeWidgetItemIterator it(mLogViewWidget, QTreeWidgetItemIterator::Selected);
0208 
0209     // No item selected
0210     if (!(*it)) {
0211         Q_EMIT statusBarChanged(i18n("No items selected. Please select items to be able to save them."));
0212         return;
0213     }
0214 
0215     const QString filename = QFileDialog::getSaveFileName(mParent, i18n("Save selected log entries to..."), QString());
0216     if (filename.isEmpty()) {
0217         return;
0218     }
0219 
0220     auto ioDev = new QFile(filename);
0221 
0222     if (ioDev->open(QIODevice::WriteOnly)) {
0223         QTextStream stream(ioDev);
0224 
0225         int nbCopied = 0;
0226 
0227         while (*it) {
0228             auto item = static_cast<LogViewWidgetItem *>(*it);
0229 
0230             // Copy the item content to the stream
0231             stream << item->logLine()->exportToText() << '\n';
0232 
0233             // Retrieve the next item
0234             ++it;
0235             nbCopied++;
0236         }
0237 
0238         ioDev->close();
0239 
0240         delete ioDev;
0241 
0242         Q_EMIT statusBarChanged(i18np("1 log line saved to '%2'.", "%1 log lines saved to '%2'.", nbCopied, filename));
0243     } else {
0244         const QString message(i18n("Unable to save file '%1': Permission Denied.", filename));
0245         KMessageBox::error(mParent, message, i18n("Unable to save file."));
0246     }
0247 }
0248 
0249 #include "moc_logViewExport.cpp"