aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/desktop-utils/lumina-fm/gitWizard.cpp
blob: 7edfc95f41161add49e1d4a378db603364cb7667 (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
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2016, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
//  This is the dialog for cloning a git repository
//===========================================
#include "gitWizard.h"
#include "ui_gitWizard.h"

#include "gitCompat.h"
#include <QDebug>
#include <QThread>

GitWizard::GitWizard(QWidget *parent) : QWizard(parent), ui(new Ui::GitWizard){
  ui->setupUi(this); //load the designer form
  proc = 0; //not initialized yet
  connect(this, SIGNAL(currentIdChanged(int)), this, SLOT(pageChanged(int)) );
  //connect(this, SIGNAL(finished(int)), this, SLOT(finished(int)) );
  connect(ui->line_repo_org, SIGNAL(textChanged(const QString&)), this, SLOT(validateRepo()) );
  connect(ui->line_repo_name, SIGNAL(textChanged(const QString&)), this, SLOT(validateRepo()) );
  connect(ui->line_ssh_pass, SIGNAL(textChanged(const QString&)), this, SLOT(validateType()) );
  connect(ui->line_user, SIGNAL(textChanged(const QString&)), this, SLOT(validateType()) );
  connect(ui->line_pass, SIGNAL(textChanged(const QString&)), this, SLOT(validateType()) );
  connect(ui->radio_type_ssh, SIGNAL(clicked()), this, SLOT(validateType()) );
  connect(ui->radio_type_login, SIGNAL(clicked()), this, SLOT(validateType()) );
  connect(ui->radio_type_anon, SIGNAL(clicked()), this, SLOT(validateType()) );
  connect(ui->check_depth, SIGNAL(clicked()), this, SLOT(validateType()) );
  connect(ui->check_branch, SIGNAL(clicked()), this, SLOT(validateType()) );
  validateRepo();
}

GitWizard::~GitWizard(){
  if(proc!=0){ proc->deleteLater(); }
}

//Input values;
void GitWizard::setWorkingDir(QString path){
  inDir = path;
}

//============
//     PRIVATE
// ============
QString GitWizard::assembleURL(){
  QString repo = ui->line_repo_org->text()+"/"+ui->line_repo_name->text()+".git";
  QString url;
  if(ui->radio_type_ssh->isChecked()){ url = "git@github.com:"+repo; }
  else if(ui->radio_type_anon->isChecked()){ url = "https://github.com/"+repo; }
  else if(ui->radio_type_login->isChecked()){
    url = "https://"+ui->line_user->text()+":"+ui->line_pass->text()+"@github.com/"+repo; 
  }

  return url;
}

/*void GitWizard::showDownload(GitProcess *P){
  
  //P->closeWriteChannel();
  //P->closeReadChannel(GitProcess::StandardOutput);
  //P->closeReadChannel(GitProcess::StandardError);
  while(P->state()!=QProcess::NotRunning){
    this->thread()->usleep(50000); //50 ms
    QApplication::processEvents();
  }
  P->deleteLater();
}*/

//================
//   PRIVATE SLOTS
// ================
void GitWizard::pageChanged(int newpage){
   //called when the "next" button is clicked
  if(this->page(newpage)==ui->page_repo){
    validateRepo();
  }else if(this->page(newpage)==ui->page_type){
    //Need to adjust items on this page based on info on last page
    ui->radio_type_anon->setEnabled( !ui->check_privaterepo->isChecked() );
    ui->radio_type_ssh->setEnabled( QFile::exists(QDir::homePath()+"/.ssh/id_rsa") ); //TODO - Disable for now until SSH passphrases can be used properly
    //Now set the preferred type of login based on which are available
    if(ui->radio_type_ssh->isEnabled()){ ui->radio_type_ssh->setChecked(true); } //SSH is preferred if that is available
    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
    else{ ui->radio_type_login->setChecked(true); }
    //Clear any of the UI as needed
    ui->line_user->clear(); ui->line_pass->clear(); ui->line_ssh_pass->clear();
    validateType();
  }else if(this->page(newpage)==ui->page_download){
    qDebug() << "Run git clone";
    QString url  = assembleURL();
    QString branch; if(ui->check_branch->isChecked()){ branch = ui->line_branch->text(); }
    int depth = -1; if(ui->check_depth->isChecked()){ depth = ui->spin_depth->value(); }
    proc = GIT::setupClone(inDir, url, branch, depth);
    if(proc!=0){
     connect(proc, SIGNAL(readyReadStandardOutput()), this, SLOT(readProc()) );
     connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(procFinished(int)) );
      if(ui->radio_type_ssh->isChecked()){ 
        proc->setSSHPassword(ui->line_ssh_pass->text());
      }
      proc->start(QIODevice::ReadOnly);
      this->button(QWizard::FinishButton)->setEnabled(false);
    }
  }
}

//Page validation slots
void GitWizard::validateRepo(){
  bool ok = !ui->line_repo_org->text().isEmpty() && !ui->line_repo_name->text().isEmpty();
  this->button(QWizard::NextButton)->setEnabled(ok);
}

void GitWizard::validateType(){
  bool ok = false;
  //Check types first
  if(ui->radio_type_login->isChecked()){ ok = !ui->line_user->text().isEmpty() && !ui->line_pass->text().isEmpty(); }
  else{ ok = true; }
  //Now check optional settings
  if(ui->check_branch->isChecked()){ ok = ok && !ui->line_branch->text().isEmpty(); }
  if(ui->check_branch->isChecked()){ ok = ok && !ui->line_branch->text().isEmpty(); }
  //Now make interface boxes appear/disappear as needed
  ui->line_ssh_pass->setVisible(ui->radio_type_ssh->isChecked());
  ui->line_user->setVisible(ui->radio_type_login->isChecked());
  ui->line_pass->setVisible(ui->radio_type_login->isChecked());
  ui->spin_depth->setVisible(ui->check_depth->isChecked());
  ui->line_branch->setVisible(ui->check_branch->isChecked());

  //Update the button as needed 
  this->button(QWizard::NextButton)->setEnabled(ok);
}

void GitWizard::readProc(){
  ui->text_procOutput->append( proc->readAllStandardOutput() );
}

void GitWizard::procFinished(int retcode){
  this->button(QWizard::FinishButton)->setEnabled(true);
}

bgstack15