From 7a55cf2d93c9daa13f2fa2ec1ae25646507cfc0c Mon Sep 17 00:00:00 2001 From: Michel Ludwig Date: Sun, 1 Dec 2019 15:56:05 +0100 Subject: [PATCH] Fix overriding of existing templates Also, don't delete the original file after the template has been created from it. BUG: 413473 * asturm 2020-10-17: backport to 2.9.93 (drop unnecessary string change) --- src/dialogs/managetemplatesdialog.cpp | 7 ++- src/templates.cpp | 78 ++------------------------- src/templates.h | 10 ++-- 3 files changed, 12 insertions(+), 83 deletions(-) diff --git a/src/dialogs/managetemplatesdialog.cpp b/src/dialogs/managetemplatesdialog.cpp index c5298b21..77d5d6ff 100644 --- a/src/dialogs/managetemplatesdialog.cpp +++ b/src/dialogs/managetemplatesdialog.cpp @@ -292,11 +292,10 @@ void ManageTemplatesDialog::addTemplate() reject(); return; } - returnValue = m_templateManager->replace(templateInfo, m_sourceURL, templateName, iconURL); - } - else { - returnValue = m_templateManager->add(m_sourceURL, templateName, iconURL); } + + returnValue = m_templateManager->add(m_sourceURL, templateName, iconURL); + if (!returnValue) { KMessageBox::error(this, i18n("Failed to create the template.")); reject(); diff --git a/src/templates.cpp b/src/templates.cpp --- a/src/templates.cpp +++ b/src/templates.cpp @@ -82,7 +82,10 @@ bool Manager::copyAppData(const QUrl &src, const QString& subdir, const QString& testDir.mkpath(dir); } // copy file - KIO::FileCopyJob* copyJob = KIO::file_copy(src, targetURL); + if(src == targetURL) { // copying a file over itself + return true; + } + KIO::FileCopyJob* copyJob = KIO::file_copy(src, targetURL, -1, KIO::Overwrite); KJobWidgets::setWindow(copyJob, m_kileInfo->mainWindow()); return copyJob->exec(); } @@ -130,77 +133,6 @@ bool Manager::remove(Info ti) { return removeAppData(ti.path) && removeAppData(ti.icon); } -bool Manager::replace(const KileTemplate::Info& toBeReplaced, const QUrl &newTemplateSourceURL, const QString& newName, const QUrl& newIcon) { - KileDocument::Type type = m_kileInfo->extensions()->determineDocumentType(newTemplateSourceURL); - - //start by copying the files that belong to the new template to a safe place - QString templateTempFile, iconTempFile; - - if( newTemplateSourceURL.isLocalFile() ) { - // file protocol. We do not need the network - templateTempFile = newTemplateSourceURL.toLocalFile(); - } - else { - QTemporaryFile tmpFile; - tmpFile.setAutoRemove( false ); - tmpFile.open(); - - templateTempFile = tmpFile.fileName(); - m_TempFilePath = tmpFile.fileName(); - KIO::FileCopyJob* fileCopyJob = KIO::file_copy( newTemplateSourceURL, QUrl::fromLocalFile(templateTempFile), -1, KIO::Overwrite ); - KJobWidgets::setWindow( fileCopyJob, m_kileInfo->mainWindow() ); - - if( ! fileCopyJob->exec() ) { - return false; - } - } - - if( newIcon.isLocalFile() ) { - // file protocol. We do not need the network - iconTempFile = newIcon.toLocalFile(); - } - else { - QTemporaryFile tmpFile; - tmpFile.setAutoRemove( false ); - tmpFile.open(); - - iconTempFile = tmpFile.fileName(); - m_TempFilePath = tmpFile.fileName(); - KIO::FileCopyJob* fileCopyJob = KIO::file_copy( newIcon, QUrl::fromLocalFile(iconTempFile), -1, KIO::Overwrite ); - KJobWidgets::setWindow( fileCopyJob, m_kileInfo->mainWindow() ); - - if( ! fileCopyJob->exec() ) { - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - return false; - } - } - - //now delete the template that should be replaced - if(!remove(toBeReplaced)) { - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - if( ! iconTempFile.isEmpty() ) - QFile::remove( iconTempFile ); - } - - //finally, create the new template - if(!add(QUrl::fromUserInput(templateTempFile), type, newName, QUrl::fromUserInput(iconTempFile))) { - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - if( ! iconTempFile.isEmpty() ) - QFile::remove( iconTempFile ); - return false; - } - - if( ! templateTempFile.isEmpty() ) - QFile::remove( templateTempFile ); - if( ! iconTempFile.isEmpty() ) - QFile::remove( iconTempFile ); - - return true; -} - void Manager::scanForTemplates() { KILE_DEBUG_MAIN << "===scanForTemplates()==================="; QStringList dirs = KileUtilities::locateAll(QStandardPaths::AppDataLocation, "templates", QStandardPaths::LocateDirectory); diff --git a/src/templates.h b/src/templates.h --- a/src/templates.h +++ b/src/templates.h @@ -1,7 +1,7 @@ /*************************************************************************************** begin : Sat Apr 26 2003 copyright : (C) 2003 by Jeroen Wijnhout (wijnhout@science.uva.nl) - 2007, 2008 by Michel Ludwig (michel.ludwig@kdemail.net) + 2007-2019 by Michel Ludwig (michel.ludwig@kdemail.net) ***************************************************************************************/ /*************************************************************************** @@ -79,15 +79,13 @@ public: **/ bool searchForTemplate(const QString& name, KileDocument::Type& type) const; - // add a template in $HOME/kile/templates/ + // Add a template in .kde/share/kile/templates/ + // This function will override any existing template. bool add(const QUrl &templateSourceURL, const QString &name, const QUrl &icon); - // remove a template from $HOME/kile/templates/ + // Remove a template from .kde/share/kile/templates/ bool remove(KileTemplate::Info ti); - // replaces a template - bool replace(const KileTemplate::Info& toBeReplaced, const QUrl &newTemplateSourceURL, const QString& newName, const QUrl& newIcon); - // these have to be methods as we cannot use i18n calls in global objects static QString defaultEmptyTemplateCaption(); static QString defaultEmptyLaTeXTemplateCaption(); -- GitLab