1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "basecheckoutwizard.h"
34 #include "vcsbaseconstants.h"
35 #include "checkoutwizarddialog.h"
36 #include "checkoutjobs.h"
38 #include <projectexplorer/projectexplorer.h>
40 #include <QtCore/QCoreApplication>
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QDir>
43 #include <QtGui/QMessageBox>
47 struct BaseCheckoutWizardPrivate {
48 BaseCheckoutWizardPrivate() : dialog(0) {}
51 Internal::CheckoutWizardDialog *dialog;
52 QList<QWizardPage *> parameterPages;
57 void BaseCheckoutWizardPrivate::clear()
59 parameterPages.clear();
64 BaseCheckoutWizard::BaseCheckoutWizard(QObject *parent) :
65 Core::IWizard(parent),
66 d(new BaseCheckoutWizardPrivate)
70 BaseCheckoutWizard::~BaseCheckoutWizard()
75 Core::IWizard::WizardKind BaseCheckoutWizard::kind() const
77 return Core::IWizard::ProjectWizard;
80 QString BaseCheckoutWizard::category() const
82 return QLatin1String(VCSBase::Constants::VCS_WIZARD_CATEGORY);
85 QString BaseCheckoutWizard::displayCategory() const
87 return QCoreApplication::translate("VCSBase", VCSBase::Constants::VCS_WIZARD_TR_CATEGORY);
90 QString BaseCheckoutWizard::id() const
95 void BaseCheckoutWizard::setId(const QString &id)
100 void BaseCheckoutWizard::runWizard(const QString &path, QWidget *parent)
102 // Create dialog and launch
103 d->parameterPages = createParameterPages(path);
104 Internal::CheckoutWizardDialog dialog(d->parameterPages, parent);
106 connect(&dialog, SIGNAL(progressPageShown()), this, SLOT(slotProgressPageShown()));
107 dialog.setWindowTitle(displayName());
108 if (dialog.exec() != QDialog::Accepted)
110 // Now try to find the project file and open
111 const QString checkoutPath = d->checkoutPath;
113 QString errorMessage;
114 const QString projectFile = openProject(checkoutPath, &errorMessage);
115 if (projectFile.isEmpty()) {
116 QMessageBox msgBox(QMessageBox::Warning, tr("Cannot Open Project"),
117 tr("Failed to open project in '%1'.").arg(QDir::toNativeSeparators(checkoutPath)));
118 msgBox.setDetailedText(errorMessage);
123 static inline QString msgNoProjectFiles(const QDir &dir, const QStringList &patterns)
125 return BaseCheckoutWizard::tr("Could not find any project files matching (%1) in the directory '%2'.").arg(patterns.join(QLatin1String(", ")), QDir::toNativeSeparators(dir.absolutePath()));
128 // Try to find the project files in a project directory with some smartness
129 static QFileInfoList findProjectFiles(const QDir &projectDir, QString *errorMessage)
131 const QStringList projectFilePatterns = ProjectExplorer::ProjectExplorerPlugin::projectFilePatterns();
133 QFileInfoList projectFiles = projectDir.entryInfoList(projectFilePatterns, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
134 if (!projectFiles.empty())
136 // Try a 'src' directory
137 QFileInfoList srcDirs = projectDir.entryInfoList(QStringList(QLatin1String("src")), QDir::Dirs|QDir::NoDotAndDotDot|QDir::Readable);
138 if (srcDirs.empty()) {
139 *errorMessage = msgNoProjectFiles(projectDir, projectFilePatterns);
140 return QFileInfoList();
142 const QDir srcDir = QDir(srcDirs.front().absoluteFilePath());
143 projectFiles = srcDir.entryInfoList(projectFilePatterns, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
144 if (projectFiles.empty()) {
145 *errorMessage = msgNoProjectFiles(srcDir, projectFilePatterns);
146 return QFileInfoList();
148 return projectFiles;;
151 QString BaseCheckoutWizard::openProject(const QString &path, QString *errorMessage)
153 ProjectExplorer::ProjectExplorerPlugin *pe = ProjectExplorer::ProjectExplorerPlugin::instance();
155 *errorMessage = tr("The Project Explorer is not available.");
159 // Search the directory for project files
160 const QDir dir(path);
162 *errorMessage = tr("'%1' does not exist.").
163 arg(QDir::toNativeSeparators(path)); // Should not happen
166 QFileInfoList projectFiles = findProjectFiles(dir, errorMessage);
167 if (projectFiles.empty())
169 // Open. Do not use a busy cursor here as additional wizards might pop up
170 const QString projectFile = projectFiles.front().absoluteFilePath();
171 if (!pe->openProject(projectFile)) {
172 *errorMessage = tr("Unable to open the project '%1'.").
173 arg(QDir::toNativeSeparators(projectFile));
179 void BaseCheckoutWizard::slotProgressPageShown()
181 const QSharedPointer<AbstractCheckoutJob> job = createJob(d->parameterPages, &(d->checkoutPath));
183 d->dialog->start(job);
186 } // namespace VCSBase