OSDN Git Service

Update source strings
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cmakeprojectmanager / cmakeprojectmanager.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 (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "cmakeopenprojectwizard.h"
35 #include "cmakeprojectmanager.h"
36 #include "cmakeprojectconstants.h"
37 #include "cmakeproject.h"
38
39 #include <utils/synchronousprocess.h>
40 #include <utils/qtcprocess.h>
41
42 #include <coreplugin/icore.h>
43 #include <coreplugin/uniqueidmanager.h>
44 #include <coreplugin/actionmanager/actionmanager.h>
45 #include <coreplugin/actionmanager/command.h>
46 #include <coreplugin/actionmanager/actioncontainer.h>
47 #include <projectexplorer/projectexplorerconstants.h>
48 #include <projectexplorer/projectexplorer.h>
49 #include <qtconcurrent/QtConcurrentTools>
50 #include <QtCore/QtConcurrentRun>
51 #include <QtCore/QCoreApplication>
52 #include <QtCore/QSettings>
53 #include <QtGui/QFormLayout>
54 #include <QtGui/QBoxLayout>
55 #include <QtGui/QDesktopServices>
56 #include <QtGui/QApplication>
57 #include <QtGui/QLabel>
58 #include <QtGui/QGroupBox>
59 #include <QtGui/QSpacerItem>
60
61 using namespace CMakeProjectManager::Internal;
62
63 CMakeManager::CMakeManager(CMakeSettingsPage *cmakeSettingsPage)
64     : m_settingsPage(cmakeSettingsPage)
65 {
66     m_projectContext = Core::Context(CMakeProjectManager::Constants::PROJECTCONTEXT);
67     m_projectLanguage = Core::Context(ProjectExplorer::Constants::LANG_CXX);
68
69     ProjectExplorer::ProjectExplorerPlugin *projectExplorer = ProjectExplorer::ProjectExplorerPlugin::instance();
70     connect(projectExplorer, SIGNAL(aboutToShowContextMenu(ProjectExplorer::Project*, ProjectExplorer::Node*)),
71             this, SLOT(updateContextMenu(ProjectExplorer::Project*, ProjectExplorer::Node*)));
72
73     Core::ActionManager *am = Core::ICore::instance()->actionManager();
74
75     Core::ActionContainer *mbuild =
76             am->actionContainer(ProjectExplorer::Constants::M_BUILDPROJECT);
77     Core::ActionContainer *mproject =
78             am->actionContainer(ProjectExplorer::Constants::M_PROJECTCONTEXT);
79     Core::ActionContainer *msubproject =
80             am->actionContainer(ProjectExplorer::Constants::M_SUBPROJECTCONTEXT);
81
82     m_runCMakeAction = new QAction(QIcon(), tr("Run CMake"), this);
83     Core::Command *command = am->registerAction(m_runCMakeAction, Constants::RUNCMAKE, m_projectContext);
84     command->setAttribute(Core::Command::CA_Hide);
85     mbuild->addAction(command, ProjectExplorer::Constants::G_BUILD_PROJECT);
86     connect(m_runCMakeAction, SIGNAL(triggered()), this, SLOT(runCMake()));
87
88     m_runCMakeActionContextMenu = new QAction(QIcon(), tr("Run CMake"), this);
89     command = am->registerAction(m_runCMakeActionContextMenu, Constants::RUNCMAKECONTEXTMENU, m_projectContext);
90     command->setAttribute(Core::Command::CA_Hide);
91     mproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
92     msubproject->addAction(command, ProjectExplorer::Constants::G_PROJECT_BUILD);
93     connect(m_runCMakeActionContextMenu, SIGNAL(triggered()), this, SLOT(runCMakeContextMenu()));
94
95 }
96
97 void CMakeManager::updateContextMenu(ProjectExplorer::Project *project, ProjectExplorer::Node *node)
98 {
99     Q_UNUSED(node);
100     m_contextProject = project;
101 }
102
103 void CMakeManager::runCMake()
104 {
105     runCMake(ProjectExplorer::ProjectExplorerPlugin::instance()->currentProject());
106 }
107
108 void CMakeManager::runCMakeContextMenu()
109 {
110     runCMake(m_contextProject);
111 }
112
113 void CMakeManager::runCMake(ProjectExplorer::Project *project)
114 {
115     if (!project)
116         return;
117     CMakeProject *cmakeProject = qobject_cast<CMakeProject *>(project);
118     if (!cmakeProject)
119         return;
120
121     if (!cmakeProject->activeTarget())
122         return;
123     if (!cmakeProject->activeTarget()->activeBuildConfiguration())
124         return;
125     CMakeBuildConfiguration *bc = cmakeProject->activeTarget()->activeBuildConfiguration();
126     CMakeOpenProjectWizard copw(this,
127                                 cmakeProject->projectDirectory(),
128                                 bc->buildDirectory(),
129                                 CMakeOpenProjectWizard::WantToUpdate,
130                                 bc->environment());
131     if (copw.exec() == QDialog::Accepted) {
132         cmakeProject->parseCMakeLists();
133     }
134 }
135
136 Core::Context CMakeManager::projectContext() const
137 {
138     return m_projectContext;
139 }
140
141 Core::Context CMakeManager::projectLanguage() const
142 {
143     return m_projectLanguage;
144 }
145
146 ProjectExplorer::Project *CMakeManager::openProject(const QString &fileName)
147 {
148     // TODO check whether this project is already opened
149     return new CMakeProject(this, fileName);
150 }
151
152 QString CMakeManager::mimeType() const
153 {
154     return Constants::CMAKEMIMETYPE;
155 }
156
157 QString CMakeManager::cmakeExecutable() const
158 {
159     return m_settingsPage->cmakeExecutable();
160 }
161
162 bool CMakeManager::isCMakeExecutableValid() const
163 {
164     return m_settingsPage->isCMakeExecutableValid();
165 }
166
167 void CMakeManager::setCMakeExecutable(const QString &executable)
168 {
169     m_settingsPage->setCMakeExecutable(executable);
170 }
171
172 bool CMakeManager::hasCodeBlocksMsvcGenerator() const
173 {
174     return m_settingsPage->hasCodeBlocksMsvcGenerator();
175 }
176
177 // TODO need to refactor this out
178 // we probably want the process instead of this function
179 // cmakeproject then could even run the cmake process in the background, adding the files afterwards
180 // sounds like a plan
181 void CMakeManager::createXmlFile(Utils::QtcProcess *proc, const QString &arguments,
182                                  const QString &sourceDirectory, const QDir &buildDirectory,
183                                  const Utils::Environment &env, const QString &generator)
184 {
185     // We create a cbp file, only if we didn't find a cbp file in the base directory
186     // Yet that can still override cbp files in subdirectories
187     // And we are creating tons of files in the source directories
188     // All of that is not really nice.
189     // The mid term plan is to move away from the CodeBlocks Generator and use our own
190     // QtCreator generator, which actually can be very similar to the CodeBlock Generator
191
192     // TODO we need to pass on the same paremeters as the cmakestep
193     QString buildDirectoryPath = buildDirectory.absolutePath();
194     buildDirectory.mkpath(buildDirectoryPath);
195     proc->setWorkingDirectory(buildDirectoryPath);
196     proc->setEnvironment(env);
197
198     const QString srcdir = buildDirectory.exists(QLatin1String("CMakeCache.txt")) ?
199                 QString(QLatin1Char('.')) : sourceDirectory;
200     QString args;
201     Utils::QtcProcess::addArg(&args, srcdir);
202     Utils::QtcProcess::addArgs(&args, arguments);
203     Utils::QtcProcess::addArg(&args, generator);
204     proc->setCommand(cmakeExecutable(), args);
205     proc->start();
206 }
207
208 QString CMakeManager::findCbpFile(const QDir &directory)
209 {
210     // Find the cbp file
211     //   TODO the cbp file is named like the project() command in the CMakeList.txt file
212     //   so this method below could find the wrong cbp file, if the user changes the project()
213     //   2name
214     foreach (const QString &cbpFile , directory.entryList()) {
215         if (cbpFile.endsWith(QLatin1String(".cbp")))
216             return directory.path() + QLatin1Char('/') + cbpFile;
217     }
218     return QString();
219 }
220
221 // This code is duplicated from qtversionmanager
222 QString CMakeManager::qtVersionForQMake(const QString &qmakePath)
223 {
224     QProcess qmake;
225     qmake.start(qmakePath, QStringList(QLatin1String("--version")));
226     if (!qmake.waitForStarted()) {
227         qWarning("Cannot start '%s': %s", qPrintable(qmakePath), qPrintable(qmake.errorString()));
228         return QString();
229     }
230     if (!qmake.waitForFinished())      {
231         Utils::SynchronousProcess::stopProcess(qmake);
232         qWarning("Timeout running '%s'.", qPrintable(qmakePath));
233         return QString();
234     }
235     QString output = qmake.readAllStandardOutput();
236     QRegExp regexp(QLatin1String("(QMake version|Qmake version:)[\\s]*([\\d.]*)"));
237     regexp.indexIn(output);
238     if (regexp.cap(2).startsWith(QLatin1String("2."))) {
239         QRegExp regexp2(QLatin1String("Using Qt version[\\s]*([\\d\\.]*)"));
240         regexp2.indexIn(output);
241         return regexp2.cap(1);
242     }
243     return QString();
244 }
245
246 /////
247 // CMakeSettingsPage
248 ////
249
250
251 CMakeSettingsPage::CMakeSettingsPage()
252     :  m_pathchooser(0)
253 {
254     m_userCmake.process = 0;
255     m_pathCmake.process = 0;
256     m_userCmake.hasCodeBlocksMsvcGenerator = false;
257     m_pathCmake.hasCodeBlocksMsvcGenerator = false;
258     Core::ICore *core = Core::ICore::instance();
259     QSettings * settings = core->settings();
260     settings->beginGroup(QLatin1String("CMakeSettings"));
261     m_userCmake.executable = settings->value(QLatin1String("cmakeExecutable")).toString();
262     settings->endGroup();
263
264     updateInfo(&m_userCmake);
265     m_pathCmake.executable = findCmakeExecutable();
266     updateInfo(&m_pathCmake);
267 }
268
269 void CMakeSettingsPage::startProcess(CMakeValidator *cmakeValidator)
270 {
271     cmakeValidator->process = new QProcess();
272
273     if (cmakeValidator == &m_pathCmake) // ugly
274         connect(cmakeValidator->process, SIGNAL(finished(int)),
275                 this, SLOT(userCmakeFinished()));
276     else
277         connect(cmakeValidator->process, SIGNAL(finished(int)),
278                 this, SLOT(pathCmakeFinished()));
279
280     cmakeValidator->process->start(cmakeValidator->executable, QStringList(QLatin1String("--help")));
281     cmakeValidator->process->waitForStarted();
282 }
283
284 void CMakeSettingsPage::userCmakeFinished()
285 {
286     cmakeFinished(&m_userCmake);
287 }
288
289 void CMakeSettingsPage::pathCmakeFinished()
290 {
291     cmakeFinished(&m_pathCmake);
292 }
293
294 void CMakeSettingsPage::cmakeFinished(CMakeValidator *cmakeValidator) const
295 {
296     if (cmakeValidator->process) {
297         cmakeValidator->process->waitForFinished();
298         QString response = cmakeValidator->process->readAll();
299         QRegExp versionRegexp(QLatin1String("^cmake version ([\\d\\.]*)"));
300         versionRegexp.indexIn(response);
301
302         //m_supportsQtCreator = response.contains(QLatin1String("QtCreator"));
303         cmakeValidator->hasCodeBlocksMsvcGenerator = response.contains(QLatin1String("CodeBlocks - NMake Makefiles"));
304         cmakeValidator->version = versionRegexp.cap(1);
305         if (!(versionRegexp.capturedTexts().size() > 3))
306             cmakeValidator->version += QLatin1Char('.') + versionRegexp.cap(3);
307
308         if (cmakeValidator->version.isEmpty())
309             cmakeValidator->state = CMakeValidator::INVALID;
310         else
311             cmakeValidator->state = CMakeValidator::VALID;
312
313         cmakeValidator->process->deleteLater();
314         cmakeValidator->process = 0;
315     }
316 }
317
318 bool CMakeSettingsPage::isCMakeExecutableValid() const
319 {
320     if (m_userCmake.state == CMakeValidator::RUNNING) {
321         disconnect(m_userCmake.process, SIGNAL(finished(int)),
322                    this, SLOT(cmakeFinished()));
323         m_userCmake.process->waitForFinished();
324         // Parse the output now
325         cmakeFinished(&m_userCmake);
326     }
327
328     if (m_userCmake.state == CMakeValidator::VALID)
329         return true;
330     if (m_pathCmake.state == CMakeValidator::RUNNING) {
331         disconnect(m_userCmake.process, SIGNAL(finished(int)),
332                    this, SLOT(cmakeFinished()));
333         m_pathCmake.process->waitForFinished();
334         // Parse the output now
335         cmakeFinished(&m_pathCmake);
336     }
337     return m_pathCmake.state == CMakeValidator::VALID;
338 }
339
340 CMakeSettingsPage::~CMakeSettingsPage()
341 {
342     if (m_userCmake.process)
343         m_userCmake.process->waitForFinished();
344     delete m_userCmake.process;
345     if (m_pathCmake.process)
346         m_pathCmake.process->waitForFinished();
347     delete m_pathCmake.process;
348 }
349
350 QString CMakeSettingsPage::findCmakeExecutable() const
351 {
352     Utils::Environment env = Utils::Environment::systemEnvironment();
353     return env.searchInPath(QLatin1String("cmake"));
354 }
355
356 QString CMakeSettingsPage::id() const
357 {
358     return QLatin1String("Z.CMake");
359 }
360
361 QString CMakeSettingsPage::displayName() const
362 {
363     return tr("CMake");
364 }
365
366 QString CMakeSettingsPage::category() const
367 {
368     return QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY);
369 }
370
371 QString CMakeSettingsPage::displayCategory() const
372 {
373     return QCoreApplication::translate("ProjectExplorer",
374                                        ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_TR_CATEGORY);
375 }
376
377 QIcon CMakeSettingsPage::categoryIcon() const
378 {
379     return QIcon(QLatin1String(ProjectExplorer::Constants::PROJECTEXPLORER_SETTINGS_CATEGORY_ICON));
380 }
381
382 QWidget *CMakeSettingsPage::createPage(QWidget *parent)
383 {
384     QWidget *outerWidget = new QWidget(parent);
385     QFormLayout *formLayout = new QFormLayout(outerWidget);
386     m_pathchooser = new Utils::PathChooser;
387     m_pathchooser->setExpectedKind(Utils::PathChooser::ExistingCommand);
388     formLayout->addRow(tr("Executable:"), m_pathchooser);
389     formLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding));
390     m_pathchooser->setPath(m_userCmake.executable);
391     return outerWidget;
392 }
393
394 void CMakeSettingsPage::updateInfo(CMakeValidator *cmakeValidator)
395 {
396     QFileInfo fi(cmakeValidator->executable);
397     if (fi.exists() && fi.isExecutable()) {
398         // Run it to find out more
399         cmakeValidator->state = CMakeValidator::RUNNING;
400         startProcess(cmakeValidator);
401     } else {
402         cmakeValidator->state = CMakeValidator::INVALID;
403     }
404     saveSettings();
405 }
406
407 void CMakeSettingsPage::saveSettings() const
408 {
409     QSettings *settings = Core::ICore::instance()->settings();
410     settings->beginGroup(QLatin1String("CMakeSettings"));
411     settings->setValue(QLatin1String("cmakeExecutable"), m_userCmake.executable);
412     settings->endGroup();
413 }
414
415 void CMakeSettingsPage::apply()
416 {
417     if (!m_pathchooser) // page was never shown
418         return;
419     if (m_userCmake.executable == m_pathchooser->path())
420         return;
421     m_userCmake.executable = m_pathchooser->path();
422     updateInfo(&m_userCmake);
423 }
424
425 void CMakeSettingsPage::finish()
426 {
427
428 }
429
430 QString CMakeSettingsPage::cmakeExecutable() const
431 {
432     if (!isCMakeExecutableValid())
433         return QString();
434     if (m_userCmake.state == CMakeValidator::VALID)
435         return m_userCmake.executable;
436     else
437         return m_pathCmake.executable;
438 }
439
440 void CMakeSettingsPage::setCMakeExecutable(const QString &executable)
441 {
442     if (m_userCmake.executable == executable)
443         return;
444     m_userCmake.executable = executable;
445     updateInfo(&m_userCmake);
446 }
447
448 bool CMakeSettingsPage::hasCodeBlocksMsvcGenerator() const
449 {
450     if (!isCMakeExecutableValid())
451         return false;
452     if (m_userCmake.state == CMakeValidator::VALID)
453         return m_userCmake.hasCodeBlocksMsvcGenerator;
454     else
455         return m_pathCmake.hasCodeBlocksMsvcGenerator;
456 }