summaryrefslogtreecommitdiff
blob: f303cff0b0f0232d1620b70aa3e47625dff7eac6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
From 7a55cf2d93c9daa13f2fa2ec1ae25646507cfc0c Mon Sep 17 00:00:00 2001
From: Michel Ludwig <michel.ludwig@kdemail.net>
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