OSDN Git Service

New File Dialog: Choose right default project for adding
[qt-creator-jp/qt-creator-jp.git] / src / plugins / vcsbase / basecheckoutwizard.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "basecheckoutwizard.h"
34 #include "vcsbaseconstants.h"
35 #include "checkoutwizarddialog.h"
36 #include "checkoutjobs.h"
37
38 #include <projectexplorer/projectexplorer.h>
39 #include <projectexplorer/projectexplorerconstants.h>
40
41 #include <QCoreApplication>
42 #include <QFileInfo>
43 #include <QDir>
44 #include <QMessageBox>
45
46 /*!
47     \class VcsBase::BaseCheckoutWizard
48
49     \brief A Core::IWizard implementing a wizard for initially checking out a project using
50     a version control system.
51
52    Implements all of Core::IWizard with the exception of
53    name()/description() and icon().
54
55    Pops up a QWizard consisting of a Parameter Page which is created
56    by a virtual factory function and a progress
57    page containing a log text. The factory function createJob()
58    creates a job with the output connected to the log window,
59    returning the path to the checkout.
60
61    On success, the wizard tries to locate a project file
62    and open it.
63
64    \sa VcsBase::BaseCheckoutWizardPage
65 */
66
67 namespace VcsBase {
68 namespace Internal {
69
70 class BaseCheckoutWizardPrivate
71 {
72 public:
73     BaseCheckoutWizardPrivate() : dialog(0) {}
74     void clear();
75
76     Internal::CheckoutWizardDialog *dialog;
77     QList<QWizardPage *> parameterPages;
78     QString checkoutPath;
79     QString id;
80 };
81
82 void BaseCheckoutWizardPrivate::clear()
83 {
84     parameterPages.clear();
85     dialog = 0;
86     checkoutPath.clear();
87 }
88
89 } // namespace Internal
90
91 BaseCheckoutWizard::BaseCheckoutWizard(QObject *parent) :
92     Core::IWizard(parent),
93     d(new Internal::BaseCheckoutWizardPrivate)
94 {
95 }
96
97 BaseCheckoutWizard::~BaseCheckoutWizard()
98 {
99     delete d;
100 }
101
102 Core::IWizard::WizardKind BaseCheckoutWizard::kind() const
103 {
104     return Core::IWizard::ProjectWizard;
105 }
106
107 QString BaseCheckoutWizard::category() const
108 {
109     return QLatin1String(ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY);
110 }
111
112 QString BaseCheckoutWizard::displayCategory() const
113 {
114     return QCoreApplication::translate("ProjectExplorer", ProjectExplorer::Constants::IMPORT_WIZARD_CATEGORY_DISPLAY);
115 }
116
117 QString BaseCheckoutWizard::id() const
118 {
119     return d->id;
120 }
121
122 QString BaseCheckoutWizard::descriptionImage() const
123 {
124     return QString();
125 }
126
127 void BaseCheckoutWizard::setId(const QString &id)
128 {
129     d->id = id;
130 }
131
132 void BaseCheckoutWizard::runWizard(const QString &path, QWidget *parent, const QString & /*platform*/, const QVariantMap &extraValues)
133 {
134     Q_UNUSED(extraValues)
135     // Create dialog and launch
136     d->parameterPages = createParameterPages(path);
137     Internal::CheckoutWizardDialog dialog(d->parameterPages, parent);
138     d->dialog = &dialog;
139     connect(&dialog, SIGNAL(progressPageShown()), this, SLOT(slotProgressPageShown()));
140     dialog.setWindowTitle(displayName());
141     if (dialog.exec() != QDialog::Accepted)
142         return;
143     // Now try to find the project file and open
144     const QString checkoutPath = d->checkoutPath;
145     d->clear();
146     QString errorMessage;
147     const QString projectFile = openProject(checkoutPath, &errorMessage);
148     if (projectFile.isEmpty()) {
149         QMessageBox msgBox(QMessageBox::Warning, tr("Cannot Open Project"),
150                            tr("Failed to open project in '%1'.").arg(QDir::toNativeSeparators(checkoutPath)));
151         msgBox.setDetailedText(errorMessage);
152         msgBox.exec();
153     }
154 }
155
156 Core::FeatureSet BaseCheckoutWizard::requiredFeatures() const
157 {
158     return Core::FeatureSet();
159 }
160
161 Core::IWizard::WizardFlags BaseCheckoutWizard::flags() const
162 {
163     return Core::IWizard::PlatformIndependent;
164 }
165
166 static inline QString msgNoProjectFiles(const QDir &dir, const QStringList &patterns)
167 {
168     return BaseCheckoutWizard::tr("Could not find any project files matching (%1) in the directory '%2'.").arg(patterns.join(QLatin1String(", ")), QDir::toNativeSeparators(dir.absolutePath()));
169 }
170
171 // Try to find the project files in a project directory with some smartness
172 static QFileInfoList findProjectFiles(const QDir &projectDir, QString *errorMessage)
173 {
174     const QStringList projectFilePatterns = ProjectExplorer::ProjectExplorerPlugin::projectFilePatterns();
175     // Project directory
176     QFileInfoList projectFiles = projectDir.entryInfoList(projectFilePatterns, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
177     if (!projectFiles.empty())
178         return projectFiles;
179     // Try a 'src' directory
180     QFileInfoList srcDirs = projectDir.entryInfoList(QStringList(QLatin1String("src")), QDir::Dirs|QDir::NoDotAndDotDot|QDir::Readable);
181     if (srcDirs.empty()) {
182         *errorMessage = msgNoProjectFiles(projectDir, projectFilePatterns);
183         return QFileInfoList();
184     }
185     const QDir srcDir = QDir(srcDirs.front().absoluteFilePath());
186     projectFiles = srcDir.entryInfoList(projectFilePatterns, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
187     if (projectFiles.empty()) {
188         *errorMessage = msgNoProjectFiles(srcDir, projectFilePatterns);
189         return QFileInfoList();
190     }
191     return projectFiles;;
192 }
193
194 QString BaseCheckoutWizard::openProject(const QString &path, QString *errorMessage)
195 {
196     ProjectExplorer::ProjectExplorerPlugin *pe  = ProjectExplorer::ProjectExplorerPlugin::instance();
197     if (!pe) {
198         *errorMessage = tr("The Project Explorer is not available.");
199         return QString();
200     }
201
202     // Search the directory for project files
203     const QDir dir(path);
204     if (!dir.exists()) {
205         *errorMessage = tr("'%1' does not exist.").
206                         arg(QDir::toNativeSeparators(path)); // Should not happen
207         return QString();
208     }
209     QFileInfoList projectFiles = findProjectFiles(dir, errorMessage);
210     if (projectFiles.empty())
211         return QString();
212     // Open. Do not use a busy cursor here as additional wizards might pop up
213     const QString projectFile = projectFiles.front().absoluteFilePath();
214     if (!pe->openProject(projectFile, errorMessage))
215         return QString();
216
217     return projectFile;
218 }
219
220 void BaseCheckoutWizard::slotProgressPageShown()
221 {
222     const QSharedPointer<AbstractCheckoutJob> job = createJob(d->parameterPages, &(d->checkoutPath));
223     d->dialog->start(job);
224 }
225
226 } // namespace VcsBase