Changeset View
Changeset View
Standalone View
Standalone View
ui/revisionviewer.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2018 by Chinmoy Ranjan Pradhan <chinmoyrp65@gmail.com> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or modify * | ||||
5 | * it under the terms of the GNU General Public License as published by * | ||||
6 | * the Free Software Foundation; either version 2 of the License, or * | ||||
7 | * (at your option) any later version. * | ||||
8 | ***************************************************************************/ | ||||
9 | | ||||
10 | #include "revisionviewer.h" | ||||
11 | | ||||
12 | #include <KMessageBox> | ||||
13 | #include <KLocalizedString> | ||||
14 | | ||||
15 | #include <QLayout> | ||||
16 | #include <QMimeType> | ||||
17 | #include <QPushButton> | ||||
18 | #include <QFileDialog> | ||||
19 | #include <QMimeDatabase> | ||||
20 | #include <QTemporaryFile> | ||||
21 | #include <QStandardPaths> | ||||
22 | #include <QDialogButtonBox> | ||||
23 | | ||||
24 | #include "fileprinterpreview.h" | ||||
25 | | ||||
26 | static void clearLayout( QLayout* layout ) | ||||
27 | { | ||||
28 | while ( QLayoutItem* item = layout->takeAt(0) ) | ||||
29 | { | ||||
30 | delete item->widget(); | ||||
31 | delete item; | ||||
32 | } | ||||
33 | } | ||||
34 | | ||||
35 | class RevisionPreview : public Okular::FilePrinterPreview | ||||
36 | { | ||||
37 | Q_OBJECT | ||||
38 | | ||||
39 | public: | ||||
40 | explicit RevisionPreview( const QString &revisionFile, QWidget *parent = nullptr ); | ||||
41 | | ||||
42 | private Q_SLOTS: | ||||
43 | void doSave(); | ||||
44 | | ||||
45 | private: | ||||
46 | QString m_filename; | ||||
47 | }; | ||||
48 | | ||||
49 | RevisionPreview::RevisionPreview( const QString &revisionFile, QWidget *parent ) | ||||
50 | : FilePrinterPreview( revisionFile, parent ), m_filename( revisionFile ) | ||||
51 | { | ||||
52 | setWindowTitle( i18n("Revision Preview") ); | ||||
53 | | ||||
54 | auto mainLayout = static_cast<QVBoxLayout*>( layout() ); | ||||
55 | clearLayout( mainLayout ); | ||||
56 | auto btnBox = new QDialogButtonBox( QDialogButtonBox::Close, this ); | ||||
57 | auto saveBtn = new QPushButton( i18n( "Save As"), this ); | ||||
58 | btnBox->button( QDialogButtonBox::Close )->setDefault( true ); | ||||
59 | btnBox->addButton( saveBtn, QDialogButtonBox::ActionRole ); | ||||
60 | connect( btnBox, &QDialogButtonBox::rejected, this, &RevisionPreview::reject ); | ||||
61 | connect( saveBtn, &QPushButton::clicked, this, &RevisionPreview::doSave ); | ||||
62 | mainLayout->addWidget( btnBox ); | ||||
63 | } | ||||
64 | | ||||
65 | void RevisionPreview::doSave() | ||||
66 | { | ||||
67 | QMimeDatabase db; | ||||
68 | QMimeType mime = db.mimeTypeForFile( m_filename ); | ||||
69 | const QString caption = i18n( "Where do you want to save this revision?" ); | ||||
70 | const QString path = QFileDialog::getSaveFileName( this, caption, QStringLiteral("Revision"), mime.filterString() ); | ||||
71 | if ( !path.isEmpty() && !QFile::copy( m_filename, path ) ) | ||||
72 | { | ||||
73 | KMessageBox::error( this, i18n("Could not save file %1.", path) ); | ||||
74 | return; | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | RevisionViewer::RevisionViewer( const QByteArray &revisionData, QWidget *parent ) | ||||
79 | : QObject( parent ), m_parent( parent ), m_revisionData( revisionData ) | ||||
80 | { | ||||
81 | } | ||||
82 | | ||||
83 | void RevisionViewer::viewRevision() | ||||
84 | { | ||||
85 | QMimeDatabase db; | ||||
86 | QMimeType mime = db.mimeTypeForData( m_revisionData ); | ||||
87 | const QString tempDir = QStandardPaths::writableLocation( QStandardPaths::TempLocation ); | ||||
88 | QTemporaryFile tf( tempDir + QStringLiteral("/okular_revision_XXXXXX.%1").arg( mime.suffixes().first() )); | ||||
89 | if ( !tf.open() ) | ||||
90 | { | ||||
91 | KMessageBox::error( m_parent, i18n("Could not view revision.") ); | ||||
92 | return; | ||||
93 | } | ||||
94 | tf.write( m_revisionData ); | ||||
95 | RevisionPreview previewdlg( tf.fileName(), m_parent ); | ||||
96 | previewdlg.exec(); | ||||
97 | } | ||||
98 | | ||||
99 | #include "revisionviewer.moc" |