File indexing completed on 2024-05-05 05:43:48

0001 /*
0002     SPDX-FileCopyrightText: 2019-2020 Nikolai Krasheninnikov <nkrasheninnikov@yandex.ru>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "svncheckoutdialog.h"
0008 
0009 #include <QApplication>
0010 #include <QFileDialog>
0011 #include <QClipboard>
0012 #include <QUrl>
0013 #include <QDir>
0014 
0015 #include "svncommands.h"
0016 
0017 namespace{
0018 
0019 // Helper function: removes trailing slashes.
0020 QString rstrip(const QString &str)
0021 {
0022     for (int i = str.size() - 1; i >= 0; --i) {
0023         if (str.at(i) != QLatin1Char('/')) {
0024             return str.left(i + 1);
0025         }
0026     }
0027 
0028     return {};
0029 }
0030 
0031 // Helper function: check if path is a valid svn repository URL.
0032 // Information about URL prefix at http://svnbook.red-bean.com/en/1.2/svn-book.html#svn.basic.in-action.wc.tbl-1.
0033 bool isValidSvnRepoUrl(const QString &path)
0034 {
0035     static const QStringList schemes = {
0036         QStringLiteral("file"),
0037         QStringLiteral("http"),
0038         QStringLiteral("https"),
0039         QStringLiteral("svn"),
0040         QStringLiteral("svn+ssh"),
0041     };
0042 
0043     const QUrl url = QUrl::fromUserInput(path);
0044 
0045     return url.isValid() && schemes.contains( url.scheme() );
0046 }
0047 
0048 }
0049 
0050 SvnCheckoutDialog::SvnCheckoutDialog(const QString& contextDir, QWidget *parent) :
0051     QDialog(parent),
0052     m_dir(contextDir)
0053 {
0054     m_ui.setupUi(this);
0055 
0056     /*
0057      * Add actions, establish connections.
0058      */
0059     connect(m_ui.pbCancel, &QPushButton::clicked, this, &QWidget::close);
0060     QAction *pickDirectory = m_ui.leCheckoutDir->addAction(QIcon::fromTheme(QStringLiteral("folder")), QLineEdit::TrailingPosition);
0061     connect(pickDirectory, &QAction::triggered, this, [this] () {
0062         const QString dir = QFileDialog::getExistingDirectory(this, i18nc("@title:window", "Choose a directory to checkout"),
0063                                                               QString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
0064 
0065         if (!dir.isEmpty()) {
0066             m_ui.leCheckoutDir->setText(dir);
0067         }
0068     } );
0069 
0070     /*
0071      * Additional setup.
0072      */
0073     const QString repoPath = QApplication::clipboard()->text();
0074     if (isValidSvnRepoUrl(repoPath)) {
0075         m_ui.leRepository->setText(repoPath);
0076     } else {
0077         m_ui.leCheckoutDir->setText(m_dir);
0078     }
0079 }
0080 
0081 SvnCheckoutDialog::~SvnCheckoutDialog() = default;
0082 
0083 void SvnCheckoutDialog::on_leRepository_textChanged(const QString &text)
0084 {
0085     if (isValidSvnRepoUrl(text)) {
0086         const QString stripped = rstrip(text);
0087         // If URL ends with a 'trunk' this is a branch - lets consider upper folder name as an
0088         // extraction path. So for '.../SomeRepo/trunk/' result would be 'SomeRepo'.
0089         int astart = -1;
0090         if (stripped.endsWith(QLatin1String("trunk"))) {
0091             astart = -2;
0092         }
0093         const QString suffix = QDir::separator() + stripped.section(QLatin1Char('/'), astart, astart);
0094 
0095         m_ui.leCheckoutDir->setText(m_dir + suffix);
0096         m_ui.pbOk->setEnabled(true);
0097     } else {
0098         m_ui.pbOk->setEnabled(false);
0099     }
0100 }
0101 
0102 void SvnCheckoutDialog::on_pbOk_clicked()
0103 {
0104     const QString &url = m_ui.leRepository->text();
0105     const bool omitExternals = m_ui.cbOmitExternals->isChecked();
0106     const QString &whereto = m_ui.leCheckoutDir->text();
0107 
0108     Q_EMIT infoMessage(i18nc("@info:status", "SVN checkout: checkout in process..."));
0109 
0110     if (!SvnCommands::checkoutRepository(url, omitExternals, whereto)) {
0111         Q_EMIT errorMessage(i18nc("@info:status", "SVN checkout: checkout failed."));
0112     } else {
0113         Q_EMIT operationCompletedMessage(i18nc("@info:status", "SVN checkout: checkout successful."));
0114     }
0115 
0116     close();
0117 }
0118 
0119 #include "moc_svncheckoutdialog.cpp"