vcsmanager.cpp 5.25 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2 3 4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11 12 13 14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18 19 20 21 22 23
** 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.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "vcsmanager.h"
#include "iversioncontrol.h"

#include <extensionsystem/pluginmanager.h>

#include <QtCore/QString>
#include <QtCore/QList>
#include <QtCore/QMap>
#include <QtCore/QCoreApplication>

#include <QtCore/QDebug>
#include <QtCore/QFileInfo>
#include <QtGui/QMessageBox>

44 45
enum { debug = 0 };

con's avatar
con committed
46 47
namespace Core {

48 49 50 51 52 53 54 55
typedef QList<IVersionControl *> VersionControlList;

static inline VersionControlList allVersionControls()
{
    return ExtensionSystem::PluginManager::instance()->getObjects<IVersionControl>();
}

// ---- VCSManagerPrivate
con's avatar
con committed
56 57 58 59
struct VCSManagerPrivate {
    QMap<QString, IVersionControl *> m_cachedMatches;
};

60 61
VCSManager::VCSManager(QObject *parent) :
   QObject(parent),
con's avatar
con committed
62 63 64 65 66 67 68 69 70
   m_d(new VCSManagerPrivate)
{
}

VCSManager::~VCSManager()
{
    delete m_d;
}

71 72 73 74 75 76 77 78 79 80 81
void VCSManager::extensionsInitialized()
{
    // Change signal connections
    foreach (IVersionControl *versionControl, allVersionControls()) {
        connect(versionControl, SIGNAL(filesChanged(QStringList)),
                this, SIGNAL(filesChanged(QStringList)));
        connect(versionControl, SIGNAL(repositoryChanged(QString)),
                this, SIGNAL(repositoryChanged(QString)));
    }
}

82 83 84 85 86 87
void VCSManager::setVCSEnabled(const QString &directory)
{
    if (debug)
        qDebug() << Q_FUNC_INFO << directory;
    IVersionControl* managingVCS = findVersionControlForDirectory(directory);
    const VersionControlList versionControls = allVersionControls();
hjk's avatar
hjk committed
88
    foreach (IVersionControl *versionControl, versionControls) {
89 90 91 92 93 94 95 96 97 98 99
        const bool newEnabled = versionControl == managingVCS;
        if (newEnabled != versionControl->isEnabled())
            versionControl->setEnabled(newEnabled);
    }
}

void VCSManager::setAllVCSEnabled()
{
    if (debug)
        qDebug() << Q_FUNC_INFO;
    const VersionControlList versionControls = allVersionControls();
hjk's avatar
hjk committed
100
    foreach (IVersionControl *versionControl, versionControls)
101 102 103 104
        if (!versionControl->isEnabled())
            versionControl->setEnabled(true);
}

con's avatar
con committed
105 106
IVersionControl* VCSManager::findVersionControlForDirectory(const QString &directory)
{
107 108 109 110 111
    // first look into the cache, check the whole name

    {
        const QMap<QString, IVersionControl *>::const_iterator it = m_d->m_cachedMatches.constFind(directory);
        if (it != m_d->m_cachedMatches.constEnd())
con's avatar
con committed
112 113 114
            return it.value();
    }

115 116
    int pos = 0;
    const QChar slash = QLatin1Char('/');
hjk's avatar
hjk committed
117
    while (true) {
118
        int index = directory.indexOf(slash, pos);
con's avatar
con committed
119 120
        if (index == -1)
            break;
121
        const QString directoryPart = directory.left(index);
con's avatar
con committed
122
        QMap<QString, IVersionControl *>::const_iterator it = m_d->m_cachedMatches.constFind(directoryPart);
123
        if (it != m_d->m_cachedMatches.constEnd())
con's avatar
con committed
124 125 126 127 128
            return it.value();
        pos = index+1;
    }

    // ah nothing so ask the IVersionControls directly
129
    const VersionControlList versionControls = allVersionControls();
hjk's avatar
hjk committed
130
    foreach (IVersionControl * versionControl, versionControls) {
con's avatar
con committed
131 132 133 134 135 136 137 138
        if (versionControl->managesDirectory(directory)) {
            m_d->m_cachedMatches.insert(versionControl->findTopLevelForDirectory(directory), versionControl);
            return versionControl;
        }
    }
    return 0;
}

139
bool VCSManager::showDeleteDialog(const QString &fileName)
con's avatar
con committed
140 141
{
    IVersionControl *vc = findVersionControlForDirectory(QFileInfo(fileName).absolutePath());
142 143
    if (!vc || !vc->supportsOperation(IVersionControl::DeleteOperation))
        return true;
con's avatar
con committed
144 145
    const QString title = QCoreApplication::translate("VCSManager", "Version Control");
    const QString msg = QCoreApplication::translate("VCSManager",
146 147
                                                    "Would you like to remove this file from the version control system (%1)?\n"
                                                    "Note: This might remove the local file.").arg(vc->name());
con's avatar
con committed
148 149
    const QMessageBox::StandardButton button =
        QMessageBox::question(0, title, msg, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
150 151 152
    if (button != QMessageBox::Yes)
        return true;
    return vc->vcsDelete(fileName);
con's avatar
con committed
153 154
}

hjk's avatar
hjk committed
155
} // namespace Core