summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Astals Cid <[email protected]>2017-11-17 14:19:12 +0100
committerAlbert Astals Cid <[email protected]>2017-11-20 14:53:17 +0100
commit559836c39270506e1f7c7b58bcf6b088a097b246 (patch)
treed3f557553723f76c3361478ebe5440a6995b6db5
parentf099d5fa2aac03a823c8ad358d6d47fa536eeb1e (diff)
Give warnings when the file is modified externally
Summary: Unfortunately, poppler (the only backed that supports saving) is not able to save properly if the file is modified by a third party while it is opened So we give the user a warning saying things went wrong and give him the option to not reload/close, that way if there was something very important in the annotations she added she can try to save them (even if by copy&paste the contents to a third program) Reviewers: rkflx Reviewed By: rkflx Subscribers: ngraham, rkflx, ltoscano, #okular Tags: #okular Differential Revision: https://phabricator.kde.org/D8863
-rw-r--r--part.cpp42
-rw-r--r--part.h1
2 files changed, 43 insertions, 0 deletions
diff --git a/part.cpp b/part.cpp
index 97baedc..d2f179a 100644
--- a/part.cpp
+++ b/part.cpp
@@ -1366,6 +1366,7 @@ Document::OpenResult Part::doOpenFile( const QMimeType &mimeA, const QString &fi
return Document::OpenError;
}
+ m_fileLastModified = QFileInfo( fileNameToOpen ).lastModified();
return Document::OpenSuccess;
}
@@ -1459,6 +1460,10 @@ Document::OpenResult Part::doOpenFile( const QMimeType &mimeA, const QString &fi
}
}
+ if ( openResult == Document::OpenSuccess )
+ {
+ m_fileLastModified = QFileInfo( fileNameToOpen ).lastModified();
+ }
return openResult;
}
@@ -1703,6 +1708,31 @@ bool Part::queryClose()
if ( !isReadWrite() || !isModified() )
return true;
+ // TODO When we get different saving backends we need to query the backend
+ // as to if it can save changes even if the open file has been modified,
+ // since we only have poppler as saving backend for now we're skipping that check
+ if ( m_fileLastModified != QFileInfo( localFilePath() ).lastModified() )
+ {
+ int res;
+ if ( m_isReloading )
+ {
+ res = KMessageBox::warningYesNo( widget(),
+ i18n( "There are unsaved changes, and the file '%1' has been modified by another program. Your changes will be lost, because the file can no longer be saved.<br>Do you want to continue reloading the file?", url().fileName() ),
+ i18n( "File Changed" ),
+ KGuiItem( i18n( "Continue Reloading" ) ), // <- KMessageBox::Yes
+ KGuiItem( i18n( "Abort Reloading" ) ));
+ }
+ else
+ {
+ res = KMessageBox::warningYesNo( widget(),
+ i18n( "There are unsaved changes, and the file '%1' has been modified by another program. Your changes will be lost, because the file can no longer be saved.<br>Do you want to continue closing the file?", url().fileName() ),
+ i18n( "File Changed" ),
+ KGuiItem( i18n( "Continue Closing" ) ), // <- KMessageBox::Yes
+ KGuiItem( i18n( "Abort Closing" ) ));
+ }
+ return res == KMessageBox::Yes;
+ }
+
const int res = KMessageBox::warningYesNoCancel( widget(),
i18n( "Do you want to save your changes to \"%1\" or discard them?", url().fileName() ),
i18n( "Close Document" ),
@@ -1783,6 +1813,7 @@ bool Part::closeUrl(bool promptToSave)
factory()->removeClient( m_generatorGuiClient );
m_generatorGuiClient = nullptr;
m_document->closeDocument();
+ m_fileLastModified = QDateTime();
updateViewActions();
delete m_tempfile;
m_tempfile = nullptr;
@@ -2448,6 +2479,17 @@ bool Part::saveAs(const QUrl & saveUrl)
bool Part::saveAs( const QUrl & saveUrl, SaveAsFlags flags )
{
+ // TODO When we get different saving backends we need to query the backend
+ // as to if it can save changes even if the open file has been modified,
+ // since we only have poppler as saving backend for now we're skipping that check
+ if ( m_fileLastModified != QFileInfo( localFilePath() ).lastModified() )
+ {
+ QMessageBox::warning( widget(),
+ i18n( "File Changed" ),
+ i18n( "The file '%1' has been modified by another program, which means it can no longer be saved.", url().fileName() ) );
+ return false;
+ }
+
bool hasUserAcceptedReload = false;
if ( m_documentOpenWithPassword )
{
diff --git a/part.h b/part.h
index 8d85ccb..55aba9e 100644
--- a/part.h
+++ b/part.h
@@ -293,6 +293,7 @@ class OKULARPART_EXPORT Part : public KParts::ReadWritePart, public Okular::Docu
// the document
Okular::Document * m_document;
+ QDateTime m_fileLastModified;
QString m_temporaryLocalFile;
bool isDocumentArchive;
bool m_documentOpenWithPassword;