vcsplugin.cpp 5.54 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29
30

#include "vcsplugin.h"
31
32
33

#include "vcsbaseconstants.h"

34
#include "commonsettingspage.h"
35
36
37
38
#include "nicknamedialog.h"
#include "vcsbaseoutputwindow.h"
#include "corelistener.h"

39
#include <coreplugin/iversioncontrol.h>
40
#include <coreplugin/mimedatabase.h>
41
42
43
44
#include <coreplugin/variablemanager.h>
#include <coreplugin/vcsmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
45

46
47
#include <QtPlugin>
#include <QDebug>
48

hjk's avatar
hjk committed
49
namespace VcsBase {
50
51
namespace Internal {

hjk's avatar
hjk committed
52
VcsPlugin *VcsPlugin::m_instance = 0;
53

hjk's avatar
hjk committed
54
VcsPlugin::VcsPlugin() :
55
56
57
58
59
60
61
    m_settingsPage(0),
    m_nickNameModel(0),
    m_coreListener(0)
{
    m_instance = this;
}

hjk's avatar
hjk committed
62
VcsPlugin::~VcsPlugin()
63
64
65
66
{
    m_instance = 0;
}

hjk's avatar
hjk committed
67
bool VcsPlugin::initialize(const QStringList &arguments, QString *errorMessage)
68
69
70
{
    Q_UNUSED(arguments)

hjk's avatar
hjk committed
71
    if (!Core::MimeDatabase::addMimeTypes(QLatin1String(":/vcsbase/VcsBase.mimetypes.xml"), errorMessage))
72
73
74
75
76
        return false;

    m_coreListener = new CoreListener;
    addAutoReleasedObject(m_coreListener);

77
    m_settingsPage = new CommonOptionsPage;
78
    addAutoReleasedObject(m_settingsPage);
hjk's avatar
hjk committed
79
80
81
82
    addAutoReleasedObject(VcsBaseOutputWindow::instance());
    connect(m_settingsPage, SIGNAL(settingsChanged(VcsBase::Internal::CommonVcsSettings)),
            this, SIGNAL(settingsChanged(VcsBase::Internal::CommonVcsSettings)));
    connect(m_settingsPage, SIGNAL(settingsChanged(VcsBase::Internal::CommonVcsSettings)),
83
84
            this, SLOT(slotSettingsChanged()));
    slotSettingsChanged();
85
86
87
88
89
90
91
92
93

    connect(Core::VariableManager::instance(), SIGNAL(variableUpdateRequested(QByteArray)),
            this, SLOT(updateVariable(QByteArray)));

    Core::VariableManager::registerVariable(Constants::VAR_VCS_NAME,
        tr("Name of the version control system in use by the current project."));
    Core::VariableManager::registerVariable(Constants::VAR_VCS_TOPIC,
        tr("The current version control topic (branch or tag) identification of the current project."));

94
95
96
    return true;
}

hjk's avatar
hjk committed
97
void VcsPlugin::extensionsInitialized()
98
99
100
{
}

hjk's avatar
hjk committed
101
VcsPlugin *VcsPlugin::instance()
102
103
104
105
{
    return m_instance;
}

hjk's avatar
hjk committed
106
CoreListener *VcsPlugin::coreListener() const
107
108
109
110
{
    return m_coreListener;
}

hjk's avatar
hjk committed
111
CommonVcsSettings VcsPlugin::settings() const
112
113
114
115
116
{
    return m_settingsPage->settings();
}

/* Delayed creation/update of the nick name model. */
hjk's avatar
hjk committed
117
QStandardItemModel *VcsPlugin::nickNameModel()
118
119
120
121
122
123
124
125
{
    if (!m_nickNameModel) {
        m_nickNameModel = NickNameDialog::createModel(this);
        populateNickNameModel();
    }
    return m_nickNameModel;
}

hjk's avatar
hjk committed
126
void VcsPlugin::populateNickNameModel()
127
128
129
130
131
{
    QString errorMessage;
    if (!NickNameDialog::populateModelFromMailCapFile(settings().nickNameMailMap,
                                                      m_nickNameModel,
                                                      &errorMessage)) {
132
133
        qWarning("%s", qPrintable(errorMessage));
    }
134
135
}

hjk's avatar
hjk committed
136
void VcsPlugin::slotSettingsChanged()
137
138
139
140
141
{
    if (m_nickNameModel)
        populateNickNameModel();
}

142
143
144
145
146
147
148
149
void VcsPlugin::updateVariable(const QByteArray &variable)
{
    static ProjectExplorer::Project *cachedProject = 0;
    static Core::IVersionControl *cachedVc = 0;
    static QString cachedTopLevel;

    ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject();
    if (cachedProject != project) {
150
151
152
153
154
155
156
        if (project) {
            cachedVc = Core::VcsManager::findVersionControlForDirectory(project->projectDirectory(),
                                                                        &cachedTopLevel);
        } else {
            cachedVc = 0;
            cachedTopLevel.clear();
        }
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        cachedProject = project;
    }

    if (variable == Constants::VAR_VCS_NAME) {
        if (cachedVc)
            Core::VariableManager::insert(variable, cachedVc->displayName());
        else
            Core::VariableManager::remove(variable);
    } else if (variable == Constants::VAR_VCS_TOPIC) {
        if (cachedVc)
            Core::VariableManager::insert(variable, cachedVc->vcsTopic(cachedTopLevel));
        else
            Core::VariableManager::remove(variable);
    }
}

173
} // namespace Internal
hjk's avatar
hjk committed
174
} // namespace VcsBase
175

hjk's avatar
hjk committed
176
Q_EXPORT_PLUGIN(VcsBase::Internal::VcsPlugin)