File: repostatusmodel.h

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (282 lines) | stat: -rw-r--r-- 9,450 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
    SPDX-FileCopyrightText: 2020 Jonathan L. Verner <jonathan.verner@matfyz.cz>

    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#ifndef KDEVPLATFORM_PLUGIN_REPO_STATUS_MODEL_H
#define KDEVPLATFORM_PLUGIN_REPO_STATUS_MODEL_H

#include "gitplugin.h"

#include <project/projectchangesmodel.h>
#include <vcs/interfaces/ibasicversioncontrol.h>

class KJob;

namespace KDevelop {
    class IProject;
    class IDocument;
}

/**
 * This implements the model for vcs status of files in a project.
 *
 * The tree structure of the model is:
 *
 *   - project item
 *     - index
 *       - file with staged changes
 *       - ...
 *     - worktree
 *       - file with unstaged changes
 *       - ...
 *     - conflicts
 *       - file with unresolved conflicts
 *       - ...
 *     - untrackded
 *       - untracked file
 *       - ...
 *
 * It is modeled on (and part of the code is copied from) the @class ProjectChangesModel
 * (see kdevplatform/vcs/interfaces/ibasicversioncontrol.h)
 *
 * @author Jonathan L. Verner <jonathan.verner@matfyz.cz>
 */
class RepoStatusModel : public QStandardItemModel
{
    Q_OBJECT

public:
    enum ItemRoles {
        UrlRole = Qt::UserRole+1, /**< an url to the item */
        AreaRole,                 /**< the area where the item belongs (index, worktree, untracked) **/
        NameRole,                 /**< A human readable name of the item */
        BranchNameRole,           /**< the git branch on which the item is located */
        StatusRole,               /**< the git status of the item (GitPlugin::ExtendedState) */
        ReadableStatusRole,       /**< a human-readable git status of the item */
        ProjectUrlRole,          /**< the url of the project */
    };

    /**
     * The areas into which files are sorted. A file may be in
     * more than one area (e.g. if it has both staged and unstaged
     * changes, it will be in Index and WorkTree). The special
     * areas ProjectRoot, IndexRoot, WorkTreeRoot, UntrackedRoot and ConflictsRoot
     * correspond to the root items.
     */
    enum Areas {
        ProjectRoot,    /**< The root item of the project */
        IndexRoot,      /**< The root item of the staged changes */
        WorkTreeRoot,   /**< The root item of the unstaged changes */
        UntrackedRoot,  /**< The root item of the untracked files */
        ConflictRoot,   /**< The root item of the files with unresolved conflicts */
        Index,          /**< An item with staged changes */
        WorkTree,       /**< An item with unstaged changes */
        Untracked,      /**< An untracked item */
        Conflicts,      /**< An item with unresolved conflicts */
        None,           /**< None of the above */
    };

    /**
     * A structure containing the collection of top-level items (i.e. the ones not containing file items)
     * corresponding to a project.
     */
    struct ProjectItem {
        QStandardItem *project, *index, *worktree, *conflicts, *untracked;
        bool isValid() const {return project;}
    };

    explicit RepoStatusModel(QObject* parent);
    ~RepoStatusModel() override;

    /**
     * Updates the items described by `status`, i.e. moves the
     * item(s) corresponding to status.url to the respective areas and
     * sets their area role data.
     *
     * @param pItem the model item for the project that the items belongs to
     * @param status the status data
     *
     * @note the @p pItem must be valid!
     */
    void updateState(const ProjectItem& pItem, const KDevelop::VcsStatusInfo& status);

    /**
     * Runs a job to fetches the statuses of elements (given by `urls`)
     * of project `project`.
     *
     * @param project the project the elements belong to
     * @param urls the urls to fetch the status for
     * @param mode whether to recurse into subdirectories, if an url is a directory
     */
    void fetchStatusesForUrls(KDevelop::IProject* project, const QList<QUrl>& urls,
                              KDevelop::IBasicVersionControl::RecursionMode mode);

    /**
     * Returns a list of items corresponding to project roots.
     *
     * @note we return a const so that the result can be used in for-range
     * loops without detaching the container.
     */
    const QList<QStandardItem*> projectRoots() const;

    /**
     * Retrieves the items in a given area of a project.
     *
     * @param project a project root
     * @param area    the area to get the items from
     *
     * @returns a list of items in the given area of the project
     *
     * @note we return a const so that the result can be used in for-range
     * loops without detaching the container.
     */
    const QList<QStandardItem*> items(const QStandardItem* project, Areas area) const;

    /**
     * Returns the ProjectItem structure (containing the project, index, worktree,
     * conflicts & untracked items) for project `p`.
     *
     * @note: Returns a default-constructed ProjectItem{} if @p p is absent from the model.
     *        This includes the case when @p p is null.
     *
     * @param p the project
     */
    ProjectItem projectItem(const KDevelop::IProject* p) const;

    /**
     * Returns the ProjectItem structure (containing the project, index, worktree,
     * conflicts & untracked items) for the project whose ProjectRoot
     * item is `p_item`
     *
     * @param p_item the ProjectRoot item
     */
    ProjectItem projectItem(QStandardItem* p_item) const;

public Q_SLOTS:

    /**
     * Updates all open projects and removes projects which are not open anymore.
     */
    void reloadAll();

    /**
     * Updates all items belonging to the projects `p` (and removes the ones
     * no longer in the projects).
     *
     * @param projects the projects to update
     */
    void reload(const QList<KDevelop::IProject*>& projects);

    /**
     * Updates the projects given byl urls `p`.
     *
     * @param projects the urls of projects to update
     */
    void reload(const QList<QUrl>& projects);

    /**
     * Adds a new project to the model (e.g. when a new project is opened).
     *
     * @param p the project to add.
     */
    void addProject(const KDevelop::IProject* p);

    /**
     * Removes a project from the model (e.g. when a project is closed).
     *
     * @param p the project to remove
     */
    void removeProject(const KDevelop::IProject* p);

    /**
     * Processes the result of a job started by `fetchStatusesForUrls`
     *
     * @param job the KJob to whose results are processed.
     */
    void statusReady(KJob* job);

    /**
     * A handler called to update the status of the item(s) in the model
     * when a document is saved
     *
     * @param doc the document (the item(s) corresponding to this will be
     * updated)
     */
    void documentSaved(const KDevelop::IDocument* doc);

    /**
     * A handler called when items are added to a project
     */
    void itemsAdded(const QModelIndex& idx, int start, int end);

    /**
     * A handler called when a job is unregistered.
     *
     * This is used to monitor when jobs run by the VCS
     * (e.g. git add, git commit, etc.) finish so that
     * we can schedule an update of the corresponding projects.
     */
    void jobUnregistered(KJob*);

    /**
     * A handler called when the branch is switched in a repo
     * to schedule the update of the name of the project.
     */
    void repositoryBranchChanged(const QUrl& url);

    /**
     * A handler for processing the result of a job getting
     * the current branch name of a repo. (this job is scheduled
     * by the `repositoryBranchChanged` method)
     */
    void branchNameReady(KDevelop::VcsJob* job);

private:
    /* Functions determining which area(s) a file should be listed in
     * depending on its status */
    static bool showInIndex(const GitPlugin::ExtendedState state);
    static bool showInWorkTree(const GitPlugin::ExtendedState state);
    static bool showInConflicts(const GitPlugin::ExtendedState state);
    static bool showInUntracked(const GitPlugin::ExtendedState state);

    /**
     * Removes all items with url `url` from the model.
     *
     * @param url the url to remove
     * @param parent the parent whose children will be processed (if it is
     * null all elements in the model will be processed)
     */
    void removeUrl(const QUrl& url, const QStandardItem* parent = nullptr);

    /**
     * Return the list of all urls for items in project `project`
     *
     * @param project the project
     */
    QList<QUrl> childUrls(const ProjectItem& project) const;

    /**
     * A helper function to recursively compute all the descendants of
     * `parent` and return them as a list. If `parent` is null, all items
     * (except the invisible root) are returned
     *
     * @param parent the node whose children will be returned (if it is
     * null all non-root nodes will be returned)
     *
     * @note we return a const so that the result can be used in for-range
     * loops without detaching the container.
     */
    const QList<QStandardItem*> allItems(const QStandardItem* parent = nullptr) const;

    /**
     * A helper function to find the model item corresponding to the project
     * @p project
     *
     * Returns a nullptr if the project is not in the model (e.g. it does not use git)
     */
    QStandardItem* findProject(const KDevelop::IProject* project) const;
};

#endif