Commit 559836c3 authored by Albert Astals Cid's avatar Albert Astals Cid Committed by Albert Astals Cid

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
parent f099d5fa
......@@ -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 )
{
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment