vcsbaseplugin.h 9.03 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
hjk's avatar
hjk committed
5
** Copyright (c) 2010 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
#ifndef VCSBASEPLUGIN_H
#define VCSBASEPLUGIN_H

33
34
#include "vcsbase_global.h"

con's avatar
con committed
35
36
#include <extensionsystem/iplugin.h>

37
38
#include <QtCore/QSharedDataPointer>
#include <QtCore/QList>
con's avatar
con committed
39

40
QT_BEGIN_NAMESPACE
41
class QAction;
42
43
class QProcessEnvironment;
class QTextCodec;
44
45
QT_END_NAMESPACE

46
47
48
49
namespace Utils {
    struct SynchronousProcessResponse;
}

50
51
52
53
namespace Core {
    class IVersionControl;
}

con's avatar
con committed
54
55
namespace VCSBase {
namespace Internal {
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    struct State;
}

class VCSBaseSubmitEditor;
struct VCSBasePluginPrivate;
class VCSBasePluginStateData;
class VCSBasePlugin;

/* VCSBasePlugin and VCSBasePluginState: Provide a base class for
 * VCS plugins. It mainly takes care of maintaining the
 * VCS-relevant state of Qt Creator which is a tuple of
 *
 * 1) Current file    and it's version system control/top level
 * 2) Current project and it's version system control/top level
 *
 * (reflected in VCSBasePluginState). The plugin connects to the
 * relevant change signals in Qt Creator and calls the virtual
 * updateActions() for the plugins to update their menu actions
 * according to the new state. This is done centrally to avoid
 * single plugins repeatedly invoking searches/QFileInfo on files,
 * etc.
77
78
79
 * Independently, there are accessors for current patch files, which return
 * a file name if the current file could be a patch file which could be applied
 * and a repository exists.
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 *
 * If current file/project are managed
 * by different version controls, the project is discarded and only
 * the current file is taken into account, allowing to do a diff
 * also when the project of a file is not opened.
 *
 * When triggering an action, a copy of the state should be made to
 * keep it, as it may rapidly change due to context changes, etc.
 *
 * The class also detects the VCS plugin submit editor closing and calls
 * the virtual submitEditorAboutToClose() to trigger the submit process. */

class VCSBASE_EXPORT VCSBasePluginState
{
public:
    VCSBasePluginState();
    VCSBasePluginState(const VCSBasePluginState &);
    VCSBasePluginState &operator=(const VCSBasePluginState &);
    ~VCSBasePluginState();

    void clear();

    bool isEmpty() const;
    bool hasFile() const;
104
    bool hasPatchFile() const;
105
106
107
108
109
110
111
112
113
114
115
    bool hasProject() const;
    bool hasTopLevel() const;

    // Current file.
    QString currentFile() const;
    QString currentFileName() const;
    QString currentFileDirectory() const;
    QString currentFileTopLevel() const;
    // Convenience: Returns file relative to top level.
    QString relativeCurrentFile() const;

116
117
118
119
120
    // If the current file looks like a patch and there is a top level,
    // it will end up here (for VCS that offer patch functionality).
    QString currentPatchFile() const;
    QString currentPatchFileDisplayName() const;

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    // Current project.
    QString currentProjectPath() const;
    QString currentProjectName() const;
    QString currentProjectTopLevel() const;
    /* Convenience: Returns project path relative to top level if it
     * differs from top level (else empty()) as an argument list to do
     * eg a 'vcs diff <args>' */
    QStringList relativeCurrentProject() const;

    // Top level directory for actions on the top level. Preferably
    // the file one.
    QString topLevel() const;

    bool equals(const VCSBasePluginState &rhs) const;

    friend VCSBASE_EXPORT QDebug operator<<(QDebug in, const VCSBasePluginState &state);
con's avatar
con committed
137

138
139
140
141
private:
    friend class VCSBasePlugin;
    bool equals(const Internal::State &s) const;
    void setState(const Internal::State &s);
142

143
144
145
146
147
148
149
150
151
152
153
    QSharedDataPointer<VCSBasePluginStateData> data;
};

VCSBASE_EXPORT QDebug operator<<(QDebug in, const VCSBasePluginState &state);

inline bool operator==(const VCSBasePluginState &s1, const VCSBasePluginState &s2)
{ return s1.equals(s2); }
inline bool operator!=(const VCSBasePluginState &s1, const VCSBasePluginState &s2)
{ return !s1.equals(s2); }

class VCSBASE_EXPORT VCSBasePlugin : public ExtensionSystem::IPlugin
con's avatar
con committed
154
155
156
{
    Q_OBJECT

157
protected:
158
    explicit VCSBasePlugin(const QString &submitEditorId);
con's avatar
con committed
159

160
161
    virtual void initialize(Core::IVersionControl *vc);
    virtual void extensionsInitialized();
con's avatar
con committed
162
163

public:
164
    virtual ~VCSBasePlugin();
con's avatar
con committed
165

166
    const VCSBasePluginState &currentState() const;
167
    Core::IVersionControl *versionControl() const;
con's avatar
con committed
168

169
170
171
    // For internal tests: Create actions driving IVersionControl's snapshot interface.
    QList<QAction*> createSnapShotTestActions();

172
173
174
175
176
177
    // Convenience that searches for the repository specifically for version control
    // systems that do not have directories like "CVS" in each managed subdirectory
    // but have a directory at the top of the repository like ".git" containing
    // a well known file. See implementation for gory details.
    static QString findRepositoryForDirectory(const QString &dir, const QString &checkFile);

178
179
180
181
182
183
184
185
186
187
188
189
    // Set up the environment for a version control command line call.
    // Sets LANG to 'C' to force English (suppress LOCALE warnings)
    // and sets up SSH graphical password prompting (note that the latter
    // requires a terminal-less process).
    static void setProcessEnvironment(QProcessEnvironment *e);
    // Returns whether an SSH prompt is configured.
    static bool isSshPromptConfigured();

    // Convenience to synchronously run VCS commands
    enum RunVCSFlags {
        ShowStdOutInLogWindow = 0x1, // Append standard output to VCS output window.
        MergeOutputChannels = 0x2,   // see QProcess: Merge stderr/stdout.
190
        SshPasswordPrompt = 0x4,    // Disable terminal on UNIX to force graphical prompt.
191
192
        SuppressStdErrInLogWindow = 0x8, // No standard error output to VCS output window.
        SuppressFailMessageInLogWindow = 0x10, // No message VCS about failure in VCS output window.
193
194
        SuppressCommandLogging = 0x20, // No command log entry in VCS output window.
        ShowSuccessMessage = 0x40      // Show message about successful completion in VCS output window.
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    };

    static Utils::SynchronousProcessResponse
            runVCS(const QString &workingDir,
                   const QString &binary,
                   const QStringList &arguments,
                   int timeOutMS,
                   QProcessEnvironment env,
                   unsigned flags = 0,
                   QTextCodec *outputCodec = 0);

    static Utils::SynchronousProcessResponse
            runVCS(const QString &workingDir,
                   const QString &binary,
                   const QStringList &arguments,
                   int timeOutMS,
                   unsigned flags = 0,
                   QTextCodec *outputCodec = 0);

214
215
216
217
public slots:
    // Convenience slot for "Delete current file" action. Prompts to
    // delete the file via VCSManager.
    void promptToDeleteCurrentFile();
218
    // Prompt to initialize version control in a directory, initially
219
220
    // pointing to the current project.
    void createRepository();
221

222
223
protected:
    enum ActionState { NoVCSEnabled, OtherVCSEnabled, VCSEnabled };
224

225
226
    // Implement to enable the plugin menu actions according to state.
    virtual void updateActions(ActionState as) = 0;
227
    // Implement to start the submit process.
228
    virtual bool submitEditorAboutToClose(VCSBaseSubmitEditor *submitEditor) = 0;
229

230
    // A helper to enable the VCS menu action according to state:
231
232
233
234
235
    // NoVCSEnabled    -> visible, enabled if repository creation is supported
    // OtherVCSEnabled -> invisible
    // Else:           -> fully enabled.
    // Returns whether actions should be set up further.
    bool enableMenuAction(ActionState as, QAction *in) const;
236
237

private slots:
238
239
    void slotSubmitEditorAboutToClose(VCSBaseSubmitEditor *submitEditor, bool *result);
    void slotStateChanged(const VCSBase::Internal::State &s, Core::IVersionControl *vc);
240
241
242
243
    void slotTestSnapshot();
    void slotTestListSnapshots();
    void slotTestRestoreSnapshot();
    void slotTestRemoveSnapshot();
244

con's avatar
con committed
245
private:
246
    VCSBasePluginPrivate *d;
con's avatar
con committed
247
248
249
250
251
};

} // namespace VCSBase

#endif // VCSBASEPLUGIN_H