1 //===========================================
2 //  Lumina-DE source code
3 //  Copyright (c) 2016, Ken Moore
4 //  Available under the 3-clause BSD license
5 //  See the LICENSE file for full details
6 //===========================================
7 //  This is the dialog for cloning a git repository
8 //===========================================
9 #include "gitWizard.h"
10 #include "ui_gitWizard.h"
11 
12 #include "gitCompat.h"
13 #include <QDebug>
14 #include <QThread>
15 
GitWizard(QWidget * parent)16 GitWizard::GitWizard(QWidget *parent) : QWizard(parent), ui(new Ui::GitWizard){
17   ui->setupUi(this); //load the designer form
18   proc = 0; //not initialized yet
19   connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageChanged(int)) );
20   //connect(this, SIGNAL(finished(int)), this, SLOT(finished(int)) );
21   connect(ui->line_repo_org, SIGNAL(textChanged(const QString&)), this, SLOT(validateRepo()) );
22   connect(ui->line_repo_name, SIGNAL(textChanged(const QString&)), this, SLOT(validateRepo()) );
23   connect(ui->line_ssh_pass, SIGNAL(textChanged(const QString&)), this, SLOT(validateType()) );
24   connect(ui->line_user, SIGNAL(textChanged(const QString&)), this, SLOT(validateType()) );
25   connect(ui->line_pass, SIGNAL(textChanged(const QString&)), this, SLOT(validateType()) );
26   connect(ui->radio_type_ssh, SIGNAL(clicked()), this, SLOT(validateType()) );
27   connect(ui->radio_type_login, SIGNAL(clicked()), this, SLOT(validateType()) );
28   connect(ui->radio_type_anon, SIGNAL(clicked()), this, SLOT(validateType()) );
29   connect(ui->check_depth, SIGNAL(clicked()), this, SLOT(validateType()) );
30   connect(ui->check_branch, SIGNAL(clicked()), this, SLOT(validateType()) );
31   validateRepo();
32 }
33 
~GitWizard()34 GitWizard::~GitWizard(){
35   if(proc!=0){ proc->deleteLater(); }
36 }
37 
38 //Input values;
setWorkingDir(QString path)39 void GitWizard::setWorkingDir(QString path){
40   inDir = path;
41 }
42 
43 //============
44 //     PRIVATE
45 // ============
assembleURL()46 QString GitWizard::assembleURL(){
47   QString repo = ui->line_repo_org->text()+"/"+ui->line_repo_name->text()+".git";
48   QString url;
49   if(ui->radio_type_ssh->isChecked()){ url = "git@github.com:"+repo; }
50   else if(ui->radio_type_anon->isChecked()){ url = "https://github.com/"+repo; }
51   else if(ui->radio_type_login->isChecked()){
52     url = "https://"+ui->line_user->text()+":"+ui->line_pass->text()+"@github.com/"+repo;
53   }
54 
55   return url;
56 }
57 
58 /*void GitWizard::showDownload(GitProcess *P){
59 
60   //P->closeWriteChannel();
61   //P->closeReadChannel(GitProcess::StandardOutput);
62   //P->closeReadChannel(GitProcess::StandardError);
63   while(P->state()!=QProcess::NotRunning){
64     this->thread()->usleep(50000); //50 ms
65     QApplication::processEvents();
66   }
67   P->deleteLater();
68 }*/
69 
70 //================
71 //   PRIVATE SLOTS
72 // ================
pageChanged(int newpage)73 void GitWizard::pageChanged(int newpage){
74    //called when the "next" button is clicked
75   if(this->page(newpage)==ui->page_repo){
76     validateRepo();
77   }else if(this->page(newpage)==ui->page_type){
78     //Need to adjust items on this page based on info on last page
79     ui->radio_type_anon->setEnabled( !ui->check_privaterepo->isChecked() );
80     ui->radio_type_ssh->setEnabled( QFile::exists(QDir::homePath()+"/.ssh/id_rsa") ); //TODO - Disable for now until SSH passphrases can be used properly
81     //Now set the preferred type of login based on which are available
82     if(ui->radio_type_ssh->isEnabled()){ ui->radio_type_ssh->setChecked(true); } //SSH is preferred if that is available
83     else if(ui->radio_type_anon->isEnabled()){ ui->radio_type_anon->setChecked(true); } //anonymous next since it is a public repo - no creds really needed
84     else{ ui->radio_type_login->setChecked(true); }
85     //Clear any of the UI as needed
86     ui->line_user->clear(); ui->line_pass->clear(); ui->line_ssh_pass->clear();
87     validateType();
88   }else if(this->page(newpage)==ui->page_download){
89     qDebug() << "Run git clone";
90     QString url  = assembleURL();
91     QString branch; if(ui->check_branch->isChecked()){ branch = ui->line_branch->text(); }
92     int depth = -1; if(ui->check_depth->isChecked()){ depth = ui->spin_depth->value(); }
93     proc = GIT::setupClone(inDir, url, branch, depth);
94     if(proc!=0){
95      connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(readProc()) );
96      connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procFinished(int)) );
97       if(ui->radio_type_ssh->isChecked()){
98         proc->setSSHPassword(ui->line_ssh_pass->text());
99       }
100       proc->start(QIODevice::ReadOnly);
101       this->button(QWizard::FinishButton)->setEnabled(false);
102     }
103   }
104 }
105 
106 //Page validation slots
validateRepo()107 void GitWizard::validateRepo(){
108   bool ok = !ui->line_repo_org->text().isEmpty() && !ui->line_repo_name->text().isEmpty();
109   this->button(QWizard::NextButton)->setEnabled(ok);
110 }
111 
validateType()112 void GitWizard::validateType(){
113   bool ok = false;
114   //Check types first
115   if(ui->radio_type_login->isChecked()){ ok = !ui->line_user->text().isEmpty() && !ui->line_pass->text().isEmpty(); }
116   else{ ok = true; }
117   //Now check optional settings
118   if(ui->check_branch->isChecked()){ ok = ok && !ui->line_branch->text().isEmpty(); }
119   if(ui->check_branch->isChecked()){ ok = ok && !ui->line_branch->text().isEmpty(); }
120   //Now make interface boxes appear/disappear as needed
121   ui->line_ssh_pass->setVisible(ui->radio_type_ssh->isChecked());
122   ui->line_user->setVisible(ui->radio_type_login->isChecked());
123   ui->line_pass->setVisible(ui->radio_type_login->isChecked());
124   ui->spin_depth->setVisible(ui->check_depth->isChecked());
125   ui->line_branch->setVisible(ui->check_branch->isChecked());
126 
127   //Update the button as needed
128   this->button(QWizard::NextButton)->setEnabled(ok);
129 }
130 
readProc()131 void GitWizard::readProc(){
132   ui->text_procOutput->append( proc->readAllStandardOutput() );
133 }
134 
procFinished(int retcode)135 void GitWizard::procFinished(int retcode){
136   this->button(QWizard::FinishButton)->setEnabled(true);
137 }
138 
139