OSDN Git Service

Update license.
[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) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.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
40 #include <QtCore/QCoreApplication>
41 #include <QtCore/QFileInfo>
42 #include <QtCore/QDir>
43 #include <QtGui/QMessageBox>
44
45 namespace VCSBase {
46
47 struct BaseCheckoutWizardPrivate {
48     BaseCheckoutWizardPrivate() : dialog(0) {}
49     void clear();
50
51     Internal::CheckoutWizardDialog *dialog;
52     QList<QWizardPage *> parameterPages;
53     QString checkoutPath;
54     QString id;
55 };
56
57 void BaseCheckoutWizardPrivate::clear()
58 {
59     parameterPages.clear();
60     dialog = 0;
61     checkoutPath.clear();
62 }
63
64 BaseCheckoutWizard::BaseCheckoutWizard(QObject *parent) :
65     Core::IWizard(parent),
66     d(new BaseCheckoutWizardPrivate)
67 {
68 }
69
70 BaseCheckoutWizard::~BaseCheckoutWizard()
71 {
72     delete d;
73 }
74
75 Core::IWizard::WizardKind BaseCheckoutWizard::kind() const
76 {
77     return Core::IWizard::ProjectWizard;
78 }
79
80 QString BaseCheckoutWizard::category() const
81 {
82     return QLatin1String(VCSBase::Constants::VCS_WIZARD_CATEGORY);
83 }
84
85 QString BaseCheckoutWizard::displayCategory() const
86 {
87     return QCoreApplication::translate("VCSBase", VCSBase::Constants::VCS_WIZARD_TR_CATEGORY);
88 }
89
90 QString BaseCheckoutWizard::id() const
91 {
92     return d->id;
93 }
94
95 void BaseCheckoutWizard::setId(const QString &id)
96 {
97     d->id = id;
98 }
99
100 void BaseCheckoutWizard::runWizard(const QString &path, QWidget *parent)
101 {
102     // Create dialog and launch
103     d->parameterPages = createParameterPages(path);
104     Internal::CheckoutWizardDialog dialog(d->parameterPages, parent);
105     d->dialog = &dialog;
106     connect(&dialog, SIGNAL(progressPageShown()), this, SLOT(slotProgressPageShown()));
107     dialog.setWindowTitle(displayName());
108     if (dialog.exec() != QDialog::Accepted)
109         return;
110     // Now try to find the project file and open
111     const QString checkoutPath = d->checkoutPath;
112     d->clear();
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);
119         msgBox.exec();
120     }
121 }
122
123 static inline QString msgNoProjectFiles(const QDir &dir, const QStringList &patterns)
124 {
125     return BaseCheckoutWizard::tr("Could not find any project files matching (%1) in the directory '%2'.").arg(patterns.join(QLatin1String(", ")), QDir::toNativeSeparators(dir.absolutePath()));
126 }
127
128 // Try to find the project files in a project directory with some smartness
129 static QFileInfoList findProjectFiles(const QDir &projectDir, QString *errorMessage)
130 {
131     const QStringList projectFilePatterns = ProjectExplorer::ProjectExplorerPlugin::projectFilePatterns();
132     // Project directory
133     QFileInfoList projectFiles = projectDir.entryInfoList(projectFilePatterns, QDir::Files|QDir::NoDotAndDotDot|QDir::Readable);
134     if (!projectFiles.empty())
135         return projectFiles;
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();
141     }
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();
147     }
148     return projectFiles;;
149 }
150
151 QString BaseCheckoutWizard::openProject(const QString &path, QString *errorMessage)
152 {
153     ProjectExplorer::ProjectExplorerPlugin *pe  = ProjectExplorer::ProjectExplorerPlugin::instance();
154     if (!pe) {
155         *errorMessage = tr("The Project Explorer is not available.");
156         return QString();
157     }
158
159     // Search the directory for project files
160     const QDir dir(path);
161     if (!dir.exists()) {
162         *errorMessage = tr("'%1' does not exist.").
163                         arg(QDir::toNativeSeparators(path)); // Should not happen
164         return QString();
165     }
166     QFileInfoList projectFiles = findProjectFiles(dir, errorMessage);
167     if (projectFiles.empty())
168         return QString();
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));
174         return QString();
175     }
176     return projectFile;
177 }
178
179 void BaseCheckoutWizard::slotProgressPageShown()
180 {
181     const QSharedPointer<AbstractCheckoutJob> job = createJob(d->parameterPages, &(d->checkoutPath));
182     if (!job.isNull())
183         d->dialog->start(job);
184 }
185
186 } // namespace VCSBase