From 917517cd37f4dd1994bafde28819a325ba31414e Mon Sep 17 00:00:00 2001
From: Tobias Hunger <tobias.hunger@digia.com>
Date: Wed, 26 Sep 2012 15:18:44 +0200
Subject: [PATCH] Mercurial/Bazaar: Make commit work on windows

Do not keep an open file around when passing the file name to the
editor. The editor won't be able to save on windows since the file
is locked.

Task-number: QTCREATORBUG-5972
Change-Id: Ib424c76d2e3e57d43fdc1e1c222dbeb148669038
Reviewed-by: Hugues Delorme <delorme.hugues@fougsys.fr>
---
 src/plugins/bazaar/bazaarplugin.cpp       | 48 +++++++---------------
 src/plugins/bazaar/bazaarplugin.h         |  4 +-
 src/plugins/mercurial/mercurialplugin.cpp | 49 +++++++----------------
 src/plugins/mercurial/mercurialplugin.h   |  5 +--
 4 files changed, 31 insertions(+), 75 deletions(-)

diff --git a/src/plugins/bazaar/bazaarplugin.cpp b/src/plugins/bazaar/bazaarplugin.cpp
index 5f3d52ea992..f857843fe06 100644
--- a/src/plugins/bazaar/bazaarplugin.cpp
+++ b/src/plugins/bazaar/bazaarplugin.cpp
@@ -71,8 +71,6 @@
 #include <QDir>
 #include <QDialog>
 #include <QFileDialog>
-#include <QTemporaryFile>
-
 
 using namespace Bazaar::Internal;
 using namespace Bazaar;
@@ -123,10 +121,10 @@ BazaarPlugin::BazaarPlugin()
       m_optionsPage(0),
       m_client(0),
       m_commandLocator(0),
-      m_changeLog(0),
       m_addAction(0),
       m_deleteAction(0),
-      m_menuAction(0)
+      m_menuAction(0),
+      m_submitActionTriggered(false)
 {
     m_instance = this;
 }
@@ -138,8 +136,6 @@ BazaarPlugin::~BazaarPlugin()
         m_client = 0;
     }
 
-    deleteCommitLog();
-
     m_instance = 0;
 }
 
@@ -552,20 +548,16 @@ void BazaarPlugin::showCommitWidget(const QList<VcsBase::VcsBaseClient::StatusIt
         return;
     }
 
-    deleteCommitLog();
-
-    // Open commit log
-    QString changeLogPattern = QDir::tempPath();
-    if (!changeLogPattern.endsWith(QLatin1Char('/')))
-        changeLogPattern += QLatin1Char('/');
-    changeLogPattern += QLatin1String("qtcreator-bzr-XXXXXX.msg");
-    m_changeLog = new QTemporaryFile(changeLogPattern, this);
-    if (!m_changeLog->open()) {
-        outputWindow->appendError(tr("Unable to generate a temporary file for the commit editor."));
+    // Start new temp file
+    Utils::TempFileSaver saver;
+    // Keep the file alive, else it removes self and forgets its name
+    saver.setAutoRemove(false);
+    if (!saver.finalize()) {
+        VcsBase::VcsBaseOutputWindow::instance()->append(saver.errorString());
         return;
     }
 
-    Core::IEditor *editor = Core::EditorManager::openEditor(m_changeLog->fileName(),
+    Core::IEditor *editor = Core::EditorManager::openEditor(saver.fileName(),
                                                             Constants::COMMIT_ID,
                                                             Core::EditorManager::ModeSwitch);
     if (!editor) {
@@ -602,17 +594,13 @@ void BazaarPlugin::diffFromEditorSelected(const QStringList &files)
 
 void BazaarPlugin::commitFromEditor()
 {
-    if (!m_changeLog)
-        return;
-
-    //use the same functionality than if the user closes the file without completing the commit
-    Core::ICore::editorManager()->closeEditors(Core::ICore::editorManager()->editorsForFileName(m_changeLog->fileName()));
+    // Close the submit editor
+    m_submitActionTriggered = true;
+    Core::ICore::editorManager()->closeEditor();
 }
 
 bool BazaarPlugin::submitEditorAboutToClose(VcsBase::VcsBaseSubmitEditor *submitEditor)
 {
-    if (!m_changeLog)
-        return true;
     Core::IDocument *editorDocument = submitEditor->document();
     const CommitEditor *commitEditor = qobject_cast<const CommitEditor *>(submitEditor);
     if (!editorDocument || !commitEditor)
@@ -622,13 +610,13 @@ bool BazaarPlugin::submitEditorAboutToClose(VcsBase::VcsBaseSubmitEditor *submit
     const VcsBase::VcsBaseSubmitEditor::PromptSubmitResult response =
             commitEditor->promptSubmit(tr("Close Commit Editor"), tr("Do you want to commit the changes?"),
                                        tr("Message check failed. Do you want to proceed?"),
-                                       &dummyPrompt, dummyPrompt);
+                                       &dummyPrompt, !m_submitActionTriggered);
+    m_submitActionTriggered = false;
 
     switch (response) {
     case VcsBase::VcsBaseSubmitEditor::SubmitCanceled:
         return false;
     case VcsBase::VcsBaseSubmitEditor::SubmitDiscarded:
-        deleteCommitLog();
         return true;
     default:
         break;
@@ -666,14 +654,6 @@ bool BazaarPlugin::submitEditorAboutToClose(VcsBase::VcsBaseSubmitEditor *submit
     return true;
 }
 
-void BazaarPlugin::deleteCommitLog()
-{
-    if (m_changeLog) {
-        delete m_changeLog;
-        m_changeLog = 0;
-    }
-}
-
 void BazaarPlugin::updateActions(VcsBase::VcsBasePlugin::ActionState as)
 {
     if (!enableMenuAction(as, m_menuAction)) {
diff --git a/src/plugins/bazaar/bazaarplugin.h b/src/plugins/bazaar/bazaarplugin.h
index 0cda4e3b514..1677b03de5f 100644
--- a/src/plugins/bazaar/bazaarplugin.h
+++ b/src/plugins/bazaar/bazaarplugin.h
@@ -44,7 +44,6 @@
 
 QT_BEGIN_NAMESPACE
 class QAction;
-class QTemporaryFile;
 QT_END_NAMESPACE
 
 namespace Core {
@@ -127,7 +126,6 @@ private:
     void createFileActions(const Core::Context &context);
     void createDirectoryActions(const Core::Context &context);
     void createRepositoryActions(const Core::Context &context);
-    void deleteCommitLog();
 
     // Variables
     static BazaarPlugin *m_instance;
@@ -139,7 +137,6 @@ private:
     Core::ActionContainer *m_bazaarContainer;
 
     QList<QAction *> m_repositoryActionList;
-    QTemporaryFile *m_changeLog;
 
     // Menu Items (file actions)
     Utils::ParameterAction *m_addAction;
@@ -159,6 +156,7 @@ private:
     QAction *m_menuAction;
 
     QString m_submitRepository;
+    bool m_submitActionTriggered;
 };
 
 } // namespace Internal
diff --git a/src/plugins/mercurial/mercurialplugin.cpp b/src/plugins/mercurial/mercurialplugin.cpp
index 1c8e5e9f3b7..f28cb65f64a 100644
--- a/src/plugins/mercurial/mercurialplugin.cpp
+++ b/src/plugins/mercurial/mercurialplugin.cpp
@@ -68,8 +68,6 @@
 #include <QDir>
 #include <QDialog>
 #include <QFileDialog>
-#include <QTemporaryFile>
-
 
 using namespace Mercurial::Internal;
 using namespace Mercurial;
@@ -122,11 +120,11 @@ MercurialPlugin::MercurialPlugin() :
         m_client(0),
         core(0),
         m_commandLocator(0),
-        changeLog(0),
         m_addAction(0),
         m_deleteAction(0),
         m_createRepositoryAction(0),
-        m_menuAction(0)
+        m_menuAction(0),
+        m_submitActionTriggered(false)
 {
     m_instance = this;
 }
@@ -138,8 +136,6 @@ MercurialPlugin::~MercurialPlugin()
         m_client = 0;
     }
 
-    deleteCommitLog();
-
     m_instance = 0;
 }
 
@@ -553,7 +549,6 @@ void MercurialPlugin::commit()
 
 void MercurialPlugin::showCommitWidget(const QList<VcsBaseClient::StatusItem> &status)
 {
-
     VcsBaseOutputWindow *outputWindow = VcsBaseOutputWindow::instance();
     //Once we receive our data release the connection so it can be reused elsewhere
     disconnect(m_client, SIGNAL(parsedStatus(QList<VcsBase::VcsBaseClient::StatusItem>)),
@@ -564,20 +559,16 @@ void MercurialPlugin::showCommitWidget(const QList<VcsBaseClient::StatusItem> &s
         return;
     }
 
-    deleteCommitLog();
-
-    // Open commit log
-    QString changeLogPattern = QDir::tempPath();
-    if (!changeLogPattern.endsWith(QLatin1Char('/')))
-        changeLogPattern += QLatin1Char('/');
-    changeLogPattern += QLatin1String("qtcreator-hg-XXXXXX.msg");
-    changeLog = new QTemporaryFile(changeLogPattern,  this);
-    if (!changeLog->open()) {
-        outputWindow->appendError(tr("Unable to generate a temporary file for the commit editor."));
+    // Start new temp file
+    Utils::TempFileSaver saver;
+    // Keep the file alive, else it removes self and forgets its name
+    saver.setAutoRemove(false);
+    if (!saver.finalize()) {
+        VcsBase::VcsBaseOutputWindow::instance()->append(saver.errorString());
         return;
     }
 
-    Core::IEditor *editor = Core::EditorManager::openEditor(changeLog->fileName(),
+    Core::IEditor *editor = Core::EditorManager::openEditor(saver.fileName(),
                                                             Constants::COMMIT_ID,
                                                             Core::EditorManager::ModeSwitch);
     if (!editor) {
@@ -610,17 +601,13 @@ void MercurialPlugin::diffFromEditorSelected(const QStringList &files)
 
 void MercurialPlugin::commitFromEditor()
 {
-    if (!changeLog)
-        return;
-
-    //use the same functionality than if the user closes the file without completing the commit
-    core->editorManager()->closeEditors(core->editorManager()->editorsForFileName(changeLog->fileName()));
+    // Close the submit editor
+    m_submitActionTriggered = true;
+    Core::ICore::editorManager()->closeEditor();
 }
 
 bool MercurialPlugin::submitEditorAboutToClose(VcsBaseSubmitEditor *submitEditor)
 {
-    if (!changeLog)
-        return true;
     Core::IDocument *editorFile = submitEditor->document();
     CommitEditor *commitEditor = qobject_cast<CommitEditor *>(submitEditor);
     if (!editorFile || !commitEditor)
@@ -630,13 +617,13 @@ bool MercurialPlugin::submitEditorAboutToClose(VcsBaseSubmitEditor *submitEditor
     const VcsBaseSubmitEditor::PromptSubmitResult response =
             commitEditor->promptSubmit(tr("Close Commit Editor"), tr("Do you want to commit the changes?"),
                                        tr("Message check failed. Do you want to proceed?"),
-                                       &dummyPrompt, dummyPrompt);
+                                       &dummyPrompt, !m_submitActionTriggered);
+    m_submitActionTriggered = false;
 
     switch (response) {
     case VcsBaseSubmitEditor::SubmitCanceled:
         return false;
     case VcsBaseSubmitEditor::SubmitDiscarded:
-        deleteCommitLog();
         return true;
     default:
         break;
@@ -657,14 +644,6 @@ bool MercurialPlugin::submitEditorAboutToClose(VcsBaseSubmitEditor *submitEditor
     return true;
 }
 
-void MercurialPlugin::deleteCommitLog()
-{
-    if (changeLog) {
-        delete changeLog;
-        changeLog = 0;
-    }
-}
-
 void MercurialPlugin::createRepositoryManagementActions(const Core::Context &context)
 {
     //TODO create menu for these options
diff --git a/src/plugins/mercurial/mercurialplugin.h b/src/plugins/mercurial/mercurialplugin.h
index 0ef33adc915..4b994ffc852 100644
--- a/src/plugins/mercurial/mercurialplugin.h
+++ b/src/plugins/mercurial/mercurialplugin.h
@@ -39,7 +39,6 @@
 
 QT_BEGIN_NAMESPACE
 class QAction;
-class QTemporaryFile;
 QT_END_NAMESPACE
 
 namespace Core {
@@ -132,7 +131,6 @@ private:
     void createRepositoryActions(const Core::Context &context);
     void createRepositoryManagementActions(const Core::Context &context);
     void createLessUsedActions(const Core::Context &context);
-    void deleteCommitLog();
 
     // Variables
     static MercurialPlugin *m_instance;
@@ -145,7 +143,6 @@ private:
     Core::ActionContainer *mercurialContainer;
 
     QList<QAction *> m_repositoryActionList;
-    QTemporaryFile *changeLog;
 
     // Menu items (file actions)
     Utils::ParameterAction *m_addAction;
@@ -166,6 +163,8 @@ private:
     QAction *m_menuAction;
 
     QString m_submitRepository;
+
+    bool m_submitActionTriggered;
 };
 
 } // namespace Internal
-- 
GitLab