"Save Copy As" removed, make it possible to create .okular files with "Save As"

remotes/origin/dont-use-docdata-for-annots-and-forms
Fabio D'Urso 12 years ago
parent 19a216a275
commit a20afdff7d
  1. 222
      part.cpp
  2. 5
      part.h
  3. 1
      part.rc

@ -654,7 +654,6 @@ void Part::setupViewerActions()
m_findPrev = KStandardAction::findPrev( this, SLOT(slotFindPrev()), ac ); m_findPrev = KStandardAction::findPrev( this, SLOT(slotFindPrev()), ac );
m_findPrev->setEnabled( false ); m_findPrev->setEnabled( false );
m_saveCopyAs = 0;
m_saveAs = 0; m_saveAs = 0;
QAction * prefs = KStandardAction::preferences( this, SLOT(slotPreferences()), ac); QAction * prefs = KStandardAction::preferences( this, SLOT(slotPreferences()), ac);
@ -752,12 +751,6 @@ void Part::setupActions()
m_selectAll = KStandardAction::selectAll( m_pageView, SLOT(selectAll()), ac ); m_selectAll = KStandardAction::selectAll( m_pageView, SLOT(selectAll()), ac );
m_saveCopyAs = KStandardAction::saveAs( this, SLOT(slotSaveCopyAs()), ac );
m_saveCopyAs->setText( i18n( "Save &Copy As..." ) );
m_saveCopyAs->setShortcut( KShortcut() );
ac->addAction( "file_save_copy", m_saveCopyAs );
m_saveCopyAs->setEnabled( false );
m_saveAs = KStandardAction::saveAs( this, SLOT(slotSaveFileAs()), ac ); m_saveAs = KStandardAction::saveAs( this, SLOT(slotSaveFileAs()), ac );
m_saveAs->setEnabled( false ); m_saveAs->setEnabled( false );
@ -1340,8 +1333,7 @@ bool Part::openFile()
m_find->setEnabled( ok && canSearch ); m_find->setEnabled( ok && canSearch );
m_findNext->setEnabled( ok && canSearch ); m_findNext->setEnabled( ok && canSearch );
m_findPrev->setEnabled( ok && canSearch ); m_findPrev->setEnabled( ok && canSearch );
if( m_saveAs ) m_saveAs->setEnabled( ok && (m_document->canSaveChanges() || isDocumentArchive) ); if( m_saveAs ) m_saveAs->setEnabled( ok );
if( m_saveCopyAs ) m_saveCopyAs->setEnabled( ok );
emit enablePrintAction( ok && m_document->printingSupport() != Okular::Document::NoPrinting ); emit enablePrintAction( ok && m_document->printingSupport() != Okular::Document::NoPrinting );
m_printPreview->setEnabled( ok && m_document->printingSupport() != Okular::Document::NoPrinting ); m_printPreview->setEnabled( ok && m_document->printingSupport() != Okular::Document::NoPrinting );
m_showProperties->setEnabled( ok ); m_showProperties->setEnabled( ok );
@ -1540,7 +1532,6 @@ bool Part::closeUrl(bool promptToSave)
m_findNext->setEnabled( false ); m_findNext->setEnabled( false );
m_findPrev->setEnabled( false ); m_findPrev->setEnabled( false );
if( m_saveAs ) m_saveAs->setEnabled( false ); if( m_saveAs ) m_saveAs->setEnabled( false );
if( m_saveCopyAs ) m_saveCopyAs->setEnabled( false );
m_printPreview->setEnabled( false ); m_printPreview->setEnabled( false );
m_showProperties->setEnabled( false ); m_showProperties->setEnabled( false );
if ( m_showEmbeddedFiles ) m_showEmbeddedFiles->setEnabled( false ); if ( m_showEmbeddedFiles ) m_showEmbeddedFiles->setEnabled( false );
@ -2138,53 +2129,59 @@ bool Part::saveFile()
return false; return false;
} }
void Part::slotSaveFileAs() bool Part::slotSaveFileAs( bool showOkularArchiveAsDefaultFormat )
{ {
if ( m_embedMode == PrintPreviewMode ) if ( m_embedMode == PrintPreviewMode )
return; return false;
/* Show a warning before saving if the generator can't save annotations,
* unless we are going to save a .okular archive. */
if ( !isDocumentArchive && !m_document->canSaveChanges( Document::SaveAnnotationsCapability ) )
{
/* Search local annotations */
bool containsLocalAnnotations = false;
const int pagecount = m_document->pages();
for ( int pageno = 0; pageno < pagecount; ++pageno ) // Determine the document's mimetype
{ KMimeType::Ptr originalMimeType;
const Okular::Page *page = m_document->page( pageno ); if ( const Okular::DocumentInfo *documentInfo = m_document->documentInfo() )
foreach ( const Okular::Annotation *ann, page->annotations() )
{
if ( !(ann->flags() & Okular::Annotation::External) )
{ {
containsLocalAnnotations = true; QString typeName = documentInfo->get("mimeType");
break; if ( !typeName.isEmpty() )
} originalMimeType = KMimeType::mimeType( typeName );
}
if ( containsLocalAnnotations )
break;
} }
/* Don't show it if there are no local annotations */ // What format choice should we show as default?
if ( containsLocalAnnotations ) const QString defaultMimeType = (isDocumentArchive || showOkularArchiveAsDefaultFormat) ?
"application/vnd.kde.okular-archive" : originalMimeType->name();
// Prepare "Save As" dialog
KFileDialog fd( url(), QString(), widget() );
fd.setWindowTitle( i18n("Save As") );
fd.setOperationMode( KFileDialog::Saving );
fd.setMode( KFile::File );
fd.setConfirmOverwrite( true );
fd.setMimeFilter(
QStringList() << originalMimeType->name() << "application/vnd.kde.okular-archive",
defaultMimeType );
// Show it
if ( fd.exec() == QDialog::Accepted )
{ {
int res = KMessageBox::warningContinueCancel( widget(), i18n("Your annotations will not be exported.\nYou can export the annotated document using File -> Export As -> Document Archive") ); const KUrl saveUrl = fd.selectedUrl();
if ( res != KMessageBox::Continue ) if ( !saveUrl.isValid() || saveUrl.isEmpty() )
return; // Canceled return false;
// Has the user chosen to save in .okular archive format?
const bool saveAsOkularArchive = ( fd.currentMimeFilter() == "application/vnd.kde.okular-archive" );
return saveAs( saveUrl, saveAsOkularArchive );
} }
else
{
return false;
} }
}
KUrl saveUrl = KFileDialog::getSaveUrl( url(), bool Part::saveAs(const KUrl & saveUrl)
QString(), widget(), QString(), {
KFileDialog::ConfirmOverwrite ); // Save in the same format (.okular vs native) as the current file
if ( !saveUrl.isValid() || saveUrl.isEmpty() ) return saveAs( saveUrl, isDocumentArchive /* saveAsOkularArchive */ );
return;
saveAs( saveUrl );
} }
bool Part::saveAs( const KUrl & saveUrl ) bool Part::saveAs( const KUrl & saveUrl, bool saveAsOkularArchive )
{ {
KTemporaryFile tf; KTemporaryFile tf;
QString fileName; QString fileName;
@ -2196,53 +2193,108 @@ bool Part::saveAs( const KUrl & saveUrl )
fileName = tf.fileName(); fileName = tf.fileName();
tf.close(); tf.close();
QString errorText; QScopedPointer<KTemporaryFile> tempFile;
bool saved; KIO::Job *copyJob; // this will be filled with the job that writes to saveUrl
if ( isDocumentArchive )
saved = m_document->saveDocumentArchive( fileName );
else
saved = m_document->saveChanges( fileName, &errorText );
if ( !saved ) // Does the user want a .okular archive?
if ( saveAsOkularArchive )
{ {
if (errorText.isEmpty()) if ( !m_document->saveDocumentArchive( fileName ) )
{ {
KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", fileName ) ); KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", fileName ) );
return false;
}
copyJob = KIO::file_copy( fileName, saveUrl, -1, KIO::Overwrite );
} }
else else
{ {
KMessageBox::information( widget(), i18n("File could not be saved in '%1'. %2", fileName, errorText ) ); // If the user wants to save in the original file's format, some features
// might not be available. Find out what can't be saved in this format
bool wontSaveAnnotations = false;
bool wontSaveForms = false;
if ( !m_document->canSaveChanges( Document::SaveFormsCapability ) )
{
/* Set wontSaveForms only if there are forms */
const int pagecount = m_document->pages();
for ( int pageno = 0; pageno < pagecount; ++pageno )
{
const Okular::Page *page = m_document->page( pageno );
if ( !page->formFields().empty() )
{
wontSaveForms = true;
break;
}
}
}
if ( !m_document->canSaveChanges( Document::SaveAnnotationsCapability ) )
{
/* Set wontSaveAnnotations only if there are local annotations */
const int pagecount = m_document->pages();
for ( int pageno = 0; pageno < pagecount; ++pageno )
{
const Okular::Page *page = m_document->page( pageno );
foreach ( const Okular::Annotation *ann, page->annotations() )
{
if ( !(ann->flags() & Okular::Annotation::External) )
{
wontSaveAnnotations = true;
break;
}
}
if ( wontSaveAnnotations )
break;
} }
return false;
} }
KIO::Job *copyJob = KIO::file_copy( fileName, saveUrl, -1, KIO::Overwrite ); // If something can't be saved in this format, ask for confirmation
if ( !KIO::NetAccess::synchronousRun( copyJob, widget() ) ) QStringList listOfwontSaves;
if ( wontSaveForms ) listOfwontSaves << i18n( "Filled form contents" );
if ( wontSaveAnnotations ) listOfwontSaves << i18n( "User annotations" );
if ( !listOfwontSaves.isEmpty() )
{ {
KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", saveUrl.prettyUrl() ) ); int result = KMessageBox::warningYesNoCancelList( widget(),
i18n( "The following elements <b>cannot be saved</b> in this format and will be lost.<br>If you want to preserve them, please use the <i>Okular document archive</i> format." ),
listOfwontSaves, i18n( "Warning" ),
KGuiItem( i18n( "Save as Okular document archive..." ), "document-save-as" ), // <- KMessageBox::Yes
KStandardGuiItem::cont() ); // <- KMessageBox::NO
switch (result)
{
case KMessageBox::Yes: // -> Save as Okular document archive
return slotSaveFileAs( true /* showOkularArchiveAsDefaultFormat */ );
case KMessageBox::No: // -> Continue
break;
case KMessageBox::Cancel:
return false; return false;
} }
}
setModified( false ); if ( m_document->canSaveChanges() )
return true; {
} // If the generator supports saving changes, save them
QString errorText;
if ( !m_document->saveChanges( fileName, &errorText ) )
{
if (errorText.isEmpty())
KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", fileName ) );
else
KMessageBox::information( widget(), i18n("File could not be saved in '%1'. %2", fileName, errorText ) );
void Part::slotSaveCopyAs() return false;
{ }
if ( m_embedMode == PrintPreviewMode )
return;
KUrl saveUrl = KFileDialog::getSaveUrl( KUrl("kfiledialog:///okular/" + url().fileName()), copyJob = KIO::file_copy( fileName, saveUrl, -1, KIO::Overwrite );
QString(), widget(), QString(), }
KFileDialog::ConfirmOverwrite ); else
if ( saveUrl.isValid() && !saveUrl.isEmpty() )
{ {
// make use of the already downloaded (in case of remote URLs) file, // make use of the already downloaded (in case of remote URLs) file,
// no point in downloading that again // no point in downloading that again
KUrl srcUrl = KUrl::fromPath( localFilePath() ); KUrl srcUrl = KUrl::fromPath( localFilePath() );
KTemporaryFile * tempFile = 0;
// duh, our local file disappeared... // duh, our local file disappeared...
if ( !QFile::exists( localFilePath() ) ) if ( !QFile::exists( localFilePath() ) )
{ {
@ -2250,12 +2302,13 @@ void Part::slotSaveCopyAs()
{ {
#ifdef OKULAR_KEEP_FILE_OPEN #ifdef OKULAR_KEEP_FILE_OPEN
// local file: try to get it back from the open handle on it // local file: try to get it back from the open handle on it
if ( ( tempFile = m_keeper->copyToTemporary() ) ) tempFile.reset( m_keeper->copyToTemporary() );
if ( tempFile )
srcUrl = KUrl::fromPath( tempFile->fileName() ); srcUrl = KUrl::fromPath( tempFile->fileName() );
#else #else
const QString msg = i18n( "Okular cannot copy %1 to the specified location.\n\nThe document does not exist anymore.", localFilePath() ); const QString msg = i18n( "Okular cannot copy %1 to the specified location.\n\nThe document does not exist anymore.", localFilePath() );
KMessageBox::sorry( widget(), msg ); KMessageBox::sorry( widget(), msg );
return; return false;
#endif #endif
} }
else else
@ -2266,14 +2319,29 @@ void Part::slotSaveCopyAs()
} }
} }
KIO::Job *copyJob = KIO::file_copy( srcUrl, saveUrl, -1, KIO::Overwrite ); if ( srcUrl != saveUrl )
{
copyJob = KIO::file_copy( srcUrl, saveUrl, -1, KIO::Overwrite );
}
else
{
// Don't do a real copy in this case, just update the timestamps
copyJob = KIO::setModificationTime( saveUrl, QDateTime::currentDateTime() );
}
}
}
if ( !KIO::NetAccess::synchronousRun( copyJob, widget() ) ) if ( !KIO::NetAccess::synchronousRun( copyJob, widget() ) )
{
KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", saveUrl.prettyUrl() ) ); KMessageBox::information( widget(), i18n("File could not be saved in '%1'. Try to save it to another location.", saveUrl.prettyUrl() ) );
return false;
delete tempFile;
} }
}
// TODO: Load new file instead of the old one
setModified( false );
return true;
}
void Part::slotGetNewStuff() void Part::slotGetNewStuff()
{ {

@ -166,6 +166,7 @@ class OKULAR_PART_EXPORT Part : public KParts::ReadWritePart, public Okular::Doc
bool openUrl(const KUrl &url); bool openUrl(const KUrl &url);
void guiActivateEvent(KParts::GUIActivateEvent *event); void guiActivateEvent(KParts::GUIActivateEvent *event);
void displayInfoMessage( const QString &message, KMessageWidget::MessageType messageType = KMessageWidget::Information, int duration = -1 );; void displayInfoMessage( const QString &message, KMessageWidget::MessageType messageType = KMessageWidget::Information, int duration = -1 );;
public: public:
bool saveFile(); bool saveFile();
bool queryClose(); bool queryClose();
@ -190,8 +191,7 @@ class OKULAR_PART_EXPORT Part : public KParts::ReadWritePart, public Okular::Doc
void slotNextBookmark(); void slotNextBookmark();
void slotFindNext(); void slotFindNext();
void slotFindPrev(); void slotFindPrev();
void slotSaveFileAs(); bool slotSaveFileAs(bool showOkularArchiveAsDefaultFormat = false);
void slotSaveCopyAs();
void slotGetNewStuff(); void slotGetNewStuff();
void slotNewConfig(); void slotNewConfig();
void slotShowMenu(const Okular::Page *page, const QPoint &point); void slotShowMenu(const Okular::Page *page, const QPoint &point);
@ -246,6 +246,7 @@ class OKULAR_PART_EXPORT Part : public KParts::ReadWritePart, public Okular::Doc
void unsetDummyMode(); void unsetDummyMode();
void slotRenameBookmark( const DocumentViewport &viewport ); void slotRenameBookmark( const DocumentViewport &viewport );
void resetStartArguments(); void resetStartArguments();
bool saveAs( const KUrl & saveUrl, bool saveAsOkularArchive );
static int numberOfParts; static int numberOfParts;

@ -5,7 +5,6 @@
<Action name="get_new_stuff" group="file_open"/> <Action name="get_new_stuff" group="file_open"/>
<Action name="import_ps" group="file_open"/> <Action name="import_ps" group="file_open"/>
<Action name="file_save_as" group="file_save"/> <Action name="file_save_as" group="file_save"/>
<Action name="file_save_copy" group="file_save"/>
<Action name="file_reload" group="file_save"/> <Action name="file_reload" group="file_save"/>
<Action name="file_print" group="file_print"/> <Action name="file_print" group="file_print"/>
<Action name="file_print_preview" group="file_print"/> <Action name="file_print_preview" group="file_print"/>

Loading…
Cancel
Save