1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Brian McGillion
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "authenticationdialog.h"
27 #include "srcdestdialog.h"
28 #include "ui_srcdestdialog.h"
29 
30 #include <QSettings>
31 #include <QUrl>
32 
33 using namespace VcsBase;
34 
35 namespace Mercurial {
36 namespace Internal  {
37 
SrcDestDialog(const VcsBasePluginState & state,Direction dir,QWidget * parent)38 SrcDestDialog::SrcDestDialog(const VcsBasePluginState &state, Direction dir, QWidget *parent) :
39     QDialog(parent),
40     m_ui(new Ui::SrcDestDialog),
41     m_direction(dir),
42     m_state(state)
43 {
44     m_ui->setupUi(this);
45     m_ui->localPathChooser->setExpectedKind(Utils::PathChooser::ExistingDirectory);
46     m_ui->localPathChooser->setHistoryCompleter(QLatin1String("Hg.SourceDir.History"));
47     QUrl repoUrl(getRepoUrl());
48     if (repoUrl.isEmpty())
49         return;
50     if (!repoUrl.password().isEmpty())
51         repoUrl.setPassword(QLatin1String("***"));
52     m_ui->defaultPath->setText(repoUrl.toString());
53     m_ui->promptForCredentials->setChecked(!repoUrl.scheme().isEmpty() && repoUrl.scheme() != QLatin1String("file"));
54 }
55 
~SrcDestDialog()56 SrcDestDialog::~SrcDestDialog()
57 {
58     delete m_ui;
59 }
60 
setPathChooserKind(Utils::PathChooser::Kind kind)61 void SrcDestDialog::setPathChooserKind(Utils::PathChooser::Kind kind)
62 {
63     m_ui->localPathChooser->setExpectedKind(kind);
64 }
65 
getRepositoryString() const66 QString SrcDestDialog::getRepositoryString() const
67 {
68     if (m_ui->defaultButton->isChecked()) {
69         QUrl repoUrl(getRepoUrl());
70         if (m_ui->promptForCredentials->isChecked() && !repoUrl.scheme().isEmpty() && repoUrl.scheme() != QLatin1String("file")) {
71             QScopedPointer<AuthenticationDialog> authDialog(new AuthenticationDialog(repoUrl.userName(), repoUrl.password()));
72             authDialog->setPasswordEnabled(repoUrl.scheme() != QLatin1String("ssh"));
73             if (authDialog->exec()== 0)
74                 return repoUrl.toString();
75 
76             QString user = authDialog->getUserName();
77             if (user.isEmpty())
78                 return repoUrl.toString();
79             if (user != repoUrl.userName())
80                 repoUrl.setUserName(user);
81 
82             QString pass = authDialog->getPassword();
83             if (!pass.isEmpty() && pass != repoUrl.password())
84                 repoUrl.setPassword(pass);
85         }
86         return repoUrl.toString();
87     }
88     if (m_ui->localButton->isChecked())
89         return m_ui->localPathChooser->filePath().toString();
90     return m_ui->urlLineEdit->text();
91 }
92 
workingDir() const93 QString SrcDestDialog::workingDir() const
94 {
95     return m_workingdir;
96 }
97 
getRepoUrl() const98 QUrl SrcDestDialog::getRepoUrl() const
99 {
100     // Repo to use: Default to the project repo, but use the current
101     const QString projectLoc = m_state.currentProjectPath();
102     const QString fileLoc = m_state.currentFileTopLevel();
103     m_workingdir = projectLoc;
104     if (!fileLoc.isEmpty())
105         m_workingdir = fileLoc;
106     if (!projectLoc.isEmpty() && fileLoc.startsWith(projectLoc + QLatin1Char('/')))
107         m_workingdir = projectLoc;
108     QSettings settings(QString::fromLatin1("%1/.hg/hgrc").arg(m_workingdir), QSettings::IniFormat);
109     QUrl url;
110     if (m_direction == outgoing)
111         url = settings.value(QLatin1String("paths/default-push")).toUrl();
112     if (url.isEmpty())
113         url = settings.value(QLatin1String("paths/default")).toUrl();
114     return url;
115 }
116 
117 } // namespace Internal
118 } // namespace Mercurial
119