From 1ab194ea764a56500bf27146cb361fc23237f00c Mon Sep 17 00:00:00 2001
From: Gesner Passos <gesner.passos@stfc.ac.uk>
Date: Wed, 30 Jan 2013 15:56:45 +0000
Subject: [PATCH] Deal with the installation of ScriptRepository

make the installation more friendly.

re #6175
---
 .../MantidQt/API/src/ScriptRepositoryView.cpp | 79 +++++++++++++------
 1 file changed, 57 insertions(+), 22 deletions(-)

diff --git a/Code/Mantid/MantidQt/API/src/ScriptRepositoryView.cpp b/Code/Mantid/MantidQt/API/src/ScriptRepositoryView.cpp
index 6c505b595ee..054dc08e9ac 100644
--- a/Code/Mantid/MantidQt/API/src/ScriptRepositoryView.cpp
+++ b/Code/Mantid/MantidQt/API/src/ScriptRepositoryView.cpp
@@ -4,21 +4,30 @@
 #include <QDebug>
 #include "MantidAPI/ScriptRepository.h"
 #include "MantidAPI/ScriptRepositoryFactory.h"
+#include "MantidKernel/ConfigService.h"
 #include <QtConcurrentRun>
 #include <QMessageBox>
 #include <QTime>
 #include <QCoreApplication>
 #include <QLabel>
 #include <QVBoxLayout>
+#include <QPushButton>
+#include <QFileDialog>
 namespace MantidQt
 {
 namespace API
 {
+
+
+
+
+
+
   /** Allow the application to be alive while giving some time to this Widget to ensure that
   the installation process is going on well.*/
   void delay()
 {
-    QTime dieTime= QTime::currentTime().addSecs(1);
+    QTime dieTime= QTime::currentTime().addSecs(3);
     while( QTime::currentTime() < dieTime )
     QCoreApplication::processEvents(QEventLoop::AllEvents, 100);    
 }
@@ -35,7 +44,7 @@ namespace API
     Mantid::API::ScriptRepository_sptr repo_ptr =  ScriptRepositoryFactory::Instance().create("GitScriptRepository");
     try{
       repo_ptr->update();
-      QMessageBox::information(NULL, "Install Script Repository", "Script Repository Installed!\n"); 
+     // QMessageBox::information(NULL, "Install Script Repository", "Script Repository Installed!\n"); 
     }catch(Mantid::API::ScriptRepoException & ex){
        qWarning() << "Update exception: " << ex.what() << endl;        
        return -1; 
@@ -53,7 +62,7 @@ namespace API
     try{
       repo_ptr->update();      
     }catch(Mantid::API::ScriptRepoException & ex){
-       qWarning() << "Update exception: " << ex.what() << endl;        
+       qWarning() << "Update of Script Repository failure: " << ex.what() << endl;        
        return -1; 
     }
     return 0;
@@ -68,6 +77,8 @@ namespace API
     ui(new Ui::ScriptRepositoryView)
   {
     using Mantid::API::ScriptRepositoryFactory; 
+    using Mantid::Kernel::ConfigServiceImpl; 
+    using Mantid::Kernel::ConfigService;
     Mantid::API::ScriptRepository_sptr repo_ptr =   ScriptRepositoryFactory::Instance().create("GitScriptRepository");
  
     if (!repo_ptr->isValid()){
@@ -82,30 +93,54 @@ namespace API
             deleteLater();
             return;
       }
+      ConfigServiceImpl & config = ConfigService::Instance();
+      QString loc = QString::fromStdString(config.getString("ScriptLocalRepository")); 
+       QString dir = QFileDialog::getExistingDirectory(this, tr("Where do you want to install Script Repository?"),
+                                                 loc,
+                                                 QFileDialog::ShowDirsOnly
+                                                 | QFileDialog::DontResolveSymlinks);
+
+      //configuring
+      if (dir.isEmpty())
+      {
+        QMessageBox::warning(this, "Installation Failed",
+            "Invalid Folder to install Script Repository!\n"); 
+        close();
+        deleteLater(); 
+        return;      
+      }
+      QString local_path = dir + "/scriptRepository"; 
+      qDebug() << "setting scriptlocalrepository to " << local_path << "\n"; 
+      config.setString("ScriptLocalRepository", local_path.toStdString()); 
+      config.saveConfig(config.getUserFilename()); 
+
       // installing in a new thread
       QFuture<int> install = QtConcurrent::run(install_repository);
-      // give some time to the thread to start
       delay();
       if (install.isResultReadyAt(0)){
-        // this means that the installation failed (no connection, probably)
-        QMessageBox::information(this,"Installa Script Repository Failed!",
-            "The installation failed. Please check your internet connection and try again\n"); 
-        close(); 
-      }
-
-      if (QMessageBox::Yes == QMessageBox::question(this, "Install Script Repository",
-          "Installing Script Repository. It may take a couple of minutes...\n"
-          "Do you want to do it in background?\n"
-          "You will have to open the interface again after some minutes.\n",
-          QMessageBox::Yes|QMessageBox::No)){
-
-          QLabel * inf = new QLabel("Running Script Repository Installation in background!\n",this); 
-          QVBoxLayout *layout = new QVBoxLayout;
-          layout->addWidget(inf);
-          this->setLayout(layout);
+        if (install.resultAt(0) < 0){
+          QMessageBox::warning(this, "Installatin Failed",
+              "The installation of Script Repository Failed\n"
+              "It may be internet connection or firewall, or the proxy definition.\n"
+              "Look at the result log to get some hints\n");
+          close(); 
+          deleteLater(); 
           return;
-        };
-      install.resultAt(0);       
+        }
+      }else{
+        // give some time to the thread to start
+        QLabel * inf = new QLabel("Running Script Repository Installation in background!\n"
+                                "Please, check the Result Log to see the progress of the installation\n\n"
+                                "After completing installation, please, reopen the Script Repository Interface\n",
+                                this); 
+        QPushButton * close = new QPushButton("Close"); 
+        connect(close, SIGNAL(clicked()), this, SLOT(close())); 
+        QVBoxLayout *layout = new QVBoxLayout;
+        layout->addWidget(inf);
+        layout->addWidget(close);
+        this->setLayout(layout);
+        return;       
+      }
     }
     
     ui->setupUi(this); 
-- 
GitLab