OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / settingspage.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 "settingspage.h"
35 #include "gitsettings.h"
36 #include "gitplugin.h"
37 #include "gitclient.h"
38
39 #include <vcsbase/vcsbaseconstants.h>
40
41 #include <QtCore/QCoreApplication>
42 #include <QtCore/QFileInfo>
43 #include <QtCore/QDebug>
44 #include <QtCore/QTextStream>
45 #include <QtCore/QProcessEnvironment>
46 #include <QtGui/QMessageBox>
47
48 namespace Git {
49 namespace Internal {
50
51 SettingsPageWidget::SettingsPageWidget(QWidget *parent) :
52     QWidget(parent)
53 {
54     m_ui.setupUi(this);
55     connect(m_ui.adoptButton, SIGNAL(clicked()), this, SLOT(setSystemPath()));
56 #ifdef Q_OS_WIN
57     const QByteArray currentHome = qgetenv("HOME");
58     const QString toolTip
59             = tr("Set the environment variable HOME to '%1'\n(%2).\n"
60                  "This causes msysgit to look for the SSH-keys in that location\n"
61                  "instead of its installation directory when run outside git bash.").
62             arg(GitClient::fakeWinHome(QProcessEnvironment::systemEnvironment()),
63                 currentHome.isEmpty() ? tr("not currently set") :
64                                         tr("currently set to '%1'").arg(QString::fromLocal8Bit(currentHome)));
65     m_ui.winHomeCheckBox->setToolTip(toolTip);
66 #else
67     m_ui.winHomeCheckBox->setVisible(false);
68 #endif
69 }
70
71 GitSettings SettingsPageWidget::settings() const
72 {
73     GitSettings rc;
74     rc.path = m_ui.pathLineEdit->text();
75     rc.adoptPath = m_ui.environmentGroupBox->isChecked() && !rc.path.isEmpty();
76     rc.logCount = m_ui.logCountSpinBox->value();
77     rc.timeoutSeconds = m_ui.timeoutSpinBox->value();
78     rc.pullRebase = m_ui.pullRebaseCheckBox->isChecked();
79     rc.promptToSubmit = m_ui.promptToSubmitCheckBox->isChecked();
80     rc.winSetHomeEnvironment = m_ui.winHomeCheckBox->isChecked();
81     rc.gitkOptions = m_ui.gitkOptionsLineEdit->text().trimmed();
82     return rc;
83 }
84
85 void SettingsPageWidget::setSettings(const GitSettings &s)
86 {
87     m_ui.environmentGroupBox->setChecked(s.adoptPath);
88     m_ui.pathLineEdit->setText(s.path);
89     m_ui.logCountSpinBox->setValue(s.logCount);
90     m_ui.timeoutSpinBox->setValue(s.timeoutSeconds);
91     m_ui.pullRebaseCheckBox->setChecked(s.pullRebase);
92     m_ui.promptToSubmitCheckBox->setChecked(s.promptToSubmit);
93     m_ui.winHomeCheckBox->setChecked(s.winSetHomeEnvironment);
94     m_ui.gitkOptionsLineEdit->setText(s.gitkOptions);
95 }
96
97 void SettingsPageWidget::setSystemPath()
98 {
99     m_ui.pathLineEdit->setText(QLatin1String(qgetenv("PATH")));
100 }
101
102 QString SettingsPageWidget::searchKeywords() const
103 {
104     QString rc;
105     QLatin1Char sep(' ');
106     QTextStream(&rc)
107             << sep << m_ui.environmentGroupBox->title()
108             << sep << m_ui.pathlabel->text()
109             << sep << m_ui.winHomeCheckBox->text()
110             << sep << m_ui.groupBox->title()
111             << sep << m_ui.logCountLabel->text()
112             << sep << m_ui.timeoutLabel->text()
113             << sep << m_ui.promptToSubmitCheckBox->text()
114             << sep << m_ui.promptToSubmitCheckBox->text()
115             << sep << m_ui.gitkGroupBox->title()
116             << sep << m_ui.gitkOptionsLabel->text()
117                ;
118     rc.remove(QLatin1Char('&'));
119     return rc;
120 }
121
122 // -------- SettingsPage
123 SettingsPage::SettingsPage() :
124     m_widget(0)
125 {
126 }
127
128 QString SettingsPage::id() const
129 {
130     return QLatin1String(VCSBase::Constants::VCS_ID_GIT);
131 }
132
133 QString SettingsPage::displayName() const
134 {
135     return tr("Git");
136 }
137
138 QWidget *SettingsPage::createPage(QWidget *parent)
139 {
140     m_widget = new SettingsPageWidget(parent);
141     m_widget->setSettings(GitPlugin::instance()->settings());
142     if (m_searchKeywords.isEmpty())
143         m_searchKeywords = m_widget->searchKeywords();
144     return m_widget;
145 }
146
147 void SettingsPage::apply()
148 {
149     const GitSettings newSettings = m_widget->settings();
150     // Warn if git cannot be found in path if the widget is on top
151     if (m_widget->isVisible()) {
152         bool gitFoundOk;
153         QString errorMessage;
154         newSettings.gitBinaryPath(&gitFoundOk, &errorMessage);
155         if (!gitFoundOk)
156             QMessageBox::warning(m_widget, tr("Git Settings"), errorMessage);
157     }
158
159     GitPlugin::instance()->setSettings(newSettings);
160 }
161
162 bool SettingsPage::matches(const QString &s) const
163 {
164     return m_searchKeywords.contains(s, Qt::CaseInsensitive);
165 }
166
167 }
168 }