File: vcseventmodel.cpp

package info (click to toggle)
kdevelop 4%3A22.12.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 70,096 kB
  • sloc: cpp: 284,635; javascript: 3,558; python: 3,422; sh: 1,319; ansic: 685; xml: 331; php: 95; lisp: 66; makefile: 39; sed: 12
file content (194 lines) | stat: -rw-r--r-- 5,057 bytes parent folder | download
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
/*
    SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>

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

#include "vcseventmodel.h"

#include <QModelIndex>
#include <QVariant>
#include <QDateTime>
#include <QList>
#include <QLocale>

#include <KLocalizedString>

#include "../vcsevent.h"
#include "../vcsrevision.h"
#include <vcsjob.h>
#include <interfaces/ibasicversioncontrol.h>
#include <interfaces/icore.h>
#include <interfaces/iruncontroller.h>

namespace KDevelop
{

class VcsBasicEventModelPrivate
{
public:
    QList<KDevelop::VcsEvent> m_events;
};

VcsBasicEventModel::VcsBasicEventModel(QObject* parent)
    : QAbstractTableModel(parent)
    , d_ptr(new VcsBasicEventModelPrivate)
{
}

VcsBasicEventModel::~VcsBasicEventModel() = default;

int VcsBasicEventModel::rowCount(const QModelIndex& parent) const
{
    Q_D(const VcsBasicEventModel);

    return parent.isValid() ? 0 : d->m_events.count();
}

int VcsBasicEventModel::columnCount(const QModelIndex& parent) const
{
    return parent.isValid() ? 0 : ColumnCount;
}

QVariant VcsBasicEventModel::data(const QModelIndex& idx, int role) const
{
    Q_D(const VcsBasicEventModel);

    if( !idx.isValid() || role != Qt::DisplayRole )
        return QVariant();

    if( idx.row() < 0 || idx.row() >= rowCount() || idx.column() < 0 || idx.column() >= columnCount() )
        return QVariant();

    KDevelop::VcsEvent ev = d->m_events.at( idx.row() );
    switch( idx.column() )
    {
        case RevisionColumn:
            return QVariant( ev.revision().revisionValue() );
        case SummaryColumn:
            // show the first line only
            return QVariant( ev.message().section(QLatin1Char('\n'), 0, 0) );
        case AuthorColumn:
            return QVariant( ev.author() );
        case DateColumn:
            return QVariant( QLocale().toString( ev.date() ) );
        default:
            break;
    }
    return QVariant();
}

QVariant VcsBasicEventModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if( section < 0 || section >= columnCount() || orientation != Qt::Horizontal || role != Qt::DisplayRole )
        return QVariant();
    switch( section )
    {
        case RevisionColumn:
            return QVariant( i18nc("@title:column", "Revision") );
        case SummaryColumn:
            return QVariant( i18nc("@title:column", "Message") );
        case AuthorColumn:
            return QVariant( i18nc("@title:column", "Author") );
        case DateColumn:
            return QVariant( i18nc("@title:column", "Date") );
        default:
            break;
    }
    return QVariant();
}

void VcsBasicEventModel::addEvents(const QList<KDevelop::VcsEvent>& list)
{
    Q_D(VcsBasicEventModel);

    if( list.isEmpty() )
        return;

    beginInsertRows( QModelIndex(), rowCount(), rowCount()+list.count()-1 );
    d->m_events += list;
    endInsertRows();
}

KDevelop::VcsEvent VcsBasicEventModel::eventForIndex(const QModelIndex& idx) const
{
    Q_D(const VcsBasicEventModel);

    if( !idx.isValid() || idx.row() < 0 || idx.row() >= rowCount() )
    {
        return KDevelop::VcsEvent();
    }
    return d->m_events.at( idx.row() );
}

class VcsEventLogModelPrivate
{
public:
    KDevelop::IBasicVersionControl* m_iface;
    VcsRevision m_rev;
    QUrl m_url;
    bool done;
    bool fetching;
};

VcsEventLogModel::VcsEventLogModel(KDevelop::IBasicVersionControl* iface, const VcsRevision& rev, const QUrl& url, QObject* parent)
    : KDevelop::VcsBasicEventModel(parent)
    , d_ptr(new VcsEventLogModelPrivate)
{
    Q_D(VcsEventLogModel);

    d->m_iface = iface;
    d->m_rev = rev;
    d->m_url = url;
    d->done = false;
    d->fetching = false;
}

VcsEventLogModel::~VcsEventLogModel() = default;

bool VcsEventLogModel::canFetchMore(const QModelIndex& parent) const
{
    Q_D(const VcsEventLogModel);

    return !d->done && !d->fetching && !parent.isValid();
}

void VcsEventLogModel::fetchMore(const QModelIndex& parent)
{
    Q_D(VcsEventLogModel);

    d->fetching = true;
    Q_ASSERT(!parent.isValid());
    Q_UNUSED(parent);
    VcsJob* job = d->m_iface->log(d->m_url, d->m_rev, qMax(rowCount(), 100));
    connect(this, &VcsEventLogModel::destroyed, job, [job] { job->kill(); });
    connect(job, &VcsJob::finished, this, &VcsEventLogModel::jobReceivedResults);
    ICore::self()->runController()->registerJob( job );
}

void VcsEventLogModel::jobReceivedResults(KJob* job)
{
    Q_D(VcsEventLogModel);

    const QList<QVariant> l = qobject_cast<KDevelop::VcsJob *>(job)->fetchResults().toList();
    if(l.isEmpty() || job->error()!=0) {
        d->done = true;
        return;
    }
    QList<KDevelop::VcsEvent> newevents;
    for (const QVariant& v : l) {
        if( v.canConvert<KDevelop::VcsEvent>() )
        {
            newevents << v.value<KDevelop::VcsEvent>();
        }
    }
    d->m_rev = newevents.last().revision();
    if (rowCount()) {
        newevents.removeFirst();
    }
    d->done = newevents.isEmpty();
    addEvents( newevents );
    d->fetching = false;
}

}