File: syncthingrecentchangesmodel.cpp

package info (click to toggle)
syncthingtray 1.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,804 kB
  • sloc: cpp: 31,085; xml: 1,694; java: 570; sh: 81; javascript: 53; makefile: 25
file content (255 lines) | stat: -rw-r--r-- 7,813 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
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
#include "./syncthingrecentchangesmodel.h"
#include "./colors.h"
#include "./syncthingicons.h"

#include <syncthingconnector/syncthingconnection.h>
#include <syncthingconnector/utils.h>

#include <c++utilities/conversion/stringconversion.h>

#include <QStringBuilder>

using namespace std;
using namespace CppUtilities;

namespace Data {

SyncthingRecentChangesModel::SyncthingRecentChangesModel(SyncthingConnection &connection, int maxRows, QObject *parent)
    : SyncthingModel(connection, parent)
    , m_maxRows(maxRows)
{
    connect(&m_connection, &SyncthingConnection::fileChanged, this, &SyncthingRecentChangesModel::fileChanged);
    connect(&m_connection, &SyncthingConnection::statusChanged, this, &SyncthingRecentChangesModel::handleStatusChanged);
}

QHash<int, QByteArray> SyncthingRecentChangesModel::roleNames() const
{
    const static QHash<int, QByteArray> roles{
        { Action, "action" },
        { ActionIcon, "actionIcon" },
        { ModifiedBy, "modifiedBy" },
        { DirectoryId, "directoryId" },
        { DirectoryName, "directoryName" },
        { Path, "path" },
        { EventTime, "eventTime" },
        { ExtendedAction, "extendedAction" },
        { ItemType, "itemType" },
    };
    return roles;
}

const QVector<int> &SyncthingRecentChangesModel::colorRoles() const
{
    static const QVector<int> colorRoles({ Qt::DecorationRole, Qt::ForegroundRole, ActionIcon });
    return colorRoles;
}

QModelIndex SyncthingRecentChangesModel::index(int row, int column, const QModelIndex &parent) const
{
    if (static_cast<size_t>(row) >= m_changes.size() || parent.isValid()) {
        return QModelIndex();
    }
    return createIndex(row, column, static_cast<quintptr>(-1));
}

QModelIndex SyncthingRecentChangesModel::parent(const QModelIndex &child) const
{
    Q_UNUSED(child)
    return QModelIndex();
}

QVariant SyncthingRecentChangesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    switch (orientation) {
    case Qt::Horizontal:
        switch (role) {
        case Qt::DisplayRole:
            switch (section) {
            case 0:
                return tr("Action");
            case 1:
                return tr("Device");
            case 2:
                return tr("Folder");
            case 3:
                return tr("Path");
            }
            break;
        default:;
        }
        break;
    default:;
    }
    return QVariant();
}

QVariant SyncthingRecentChangesModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.parent().isValid() || static_cast<size_t>(index.row()) >= m_changes.size()) {
        return QVariant();
    }

    const SyncthingRecentChange &change = m_changes[static_cast<size_t>(index.row())];
    switch (role) {
    case Qt::DisplayRole:
    case Qt::EditRole:
        switch (index.column()) {
        case 0:
            return change.fileChange.action;
        case 1:
            return change.deviceName.isEmpty() ? change.fileChange.modifiedBy : change.deviceName;
        case 2:
            return change.directoryName.isEmpty() ? change.directoryId : change.directoryName;
        case 3:
            return change.fileChange.path;
        }
        break;
    case Qt::DecorationRole:
    case ActionIcon:
        switch (index.column()) {
        case 0:
            return change.fileChange.local ? commonForkAwesomeIcons().home : commonForkAwesomeIcons().globe;
        }
        break;
    case Qt::ToolTipRole:
        switch (index.column()) {
        case 0:
            return QString((change.fileChange.local ? tr("Locally") : tr("Remotely")) % QChar(' ') % change.fileChange.action % QStringLiteral(", ")
                % QString::fromStdString(change.fileChange.eventTime.toString(DateTimeOutputFormat::DateAndTime, true)));
        case 1:
            return change.deviceId.isEmpty() ? change.fileChange.modifiedBy : change.deviceId;
        case 2:
            return change.directoryId;
        case 3:
            return change.fileChange.path; // usually too long so add a tooltip
        }
        break;
    case Action:
        return change.fileChange.action;
    case ModifiedBy:
        return change.deviceName.isEmpty() ? change.fileChange.modifiedBy : change.deviceName;
    case DirectoryId:
        return change.directoryId;
    case DirectoryName:
        return change.directoryName;
    case Path:
        return change.fileChange.path;
    case EventTime:
        return QString::fromStdString(change.fileChange.eventTime.toString(DateTimeOutputFormat::DateAndTime, true));
    case ExtendedAction: {
        auto extendedAction = change.fileChange.action;
        extendedAction[0] = extendedAction[0].toUpper();
        return QVariant(std::move(extendedAction));
    }
    case ItemType:
        return change.fileChange.type;
    default:;
    }

    return QVariant();
}

bool SyncthingRecentChangesModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(index)
    Q_UNUSED(value)
    Q_UNUSED(role)
    return false;
}

int SyncthingRecentChangesModel::rowCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        return static_cast<int>(m_changes.size());
    } else {
        return 0;
    }
}

int SyncthingRecentChangesModel::columnCount(const QModelIndex &parent) const
{
    if (!parent.isValid()) {
        return 4; // action, device, folder, path
    } else {
        return 0;
    }
}

void SyncthingRecentChangesModel::fileChanged(const SyncthingDir &dir, int index, const SyncthingFileChange &change)
{
    Q_UNUSED(index)

    const SyncthingDev *relatedDev = nullptr;
    for (const SyncthingDev &dev : m_connection.devInfo()) {
        if (dev.id.startsWith(change.modifiedBy)) {
            relatedDev = &dev;
            break;
        }
    }
    if (index >= 0) {
        beginInsertRows(QModelIndex(), 0, 0);
    }
    m_changes.emplace_front(SyncthingRecentChange{
        .directoryId = dir.id,
        .directoryName = dir.displayName(),
        .deviceId = relatedDev ? relatedDev->id : QString(),
        .deviceName = relatedDev ? relatedDev->name : QString(),
        .fileChange = change,
    });
    if (index >= 0) {
        endInsertRows();
    }

    ensureWithinLimit();
}

void SyncthingRecentChangesModel::handleConfigInvalidated()
{
}

void SyncthingRecentChangesModel::handleNewConfigAvailable()
{
}

void SyncthingRecentChangesModel::handleStatusChanged(SyncthingStatus status)
{
    // clear history when re-connecting
    // note: This model is independent of changes to the current configuration (it is about the past and not
    //       the current state) so the re-connect event is used here instead of handleConfigInvalidated()
    //       and handleNewConfigAvailable() as usual.
    if (status != SyncthingStatus::Reconnecting) {
        return;
    }
    beginResetModel();
    m_changes.clear();
    endResetModel();
}

void SyncthingRecentChangesModel::handleForkAwesomeIconsChanged()
{
    invalidateTopLevelIndicies(QVector<int>({ Qt::DecorationRole, ActionIcon }));
}

/*!
 * \brief Sets the maximum number of rows.
 * \remark Specify a negative value for using the disk event limit of the connection.
 */
void SyncthingRecentChangesModel::setMaxRows(int maxRows)
{
    m_maxRows = maxRows;
    ensureWithinLimit();
}

void SyncthingRecentChangesModel::ensureWithinLimit()
{
    const auto maxRows = m_maxRows >= 0 ? m_maxRows : m_connection.diskEventLimit();
    const auto rowsToDelete = static_cast<int>(m_changes.size()) - maxRows;
    if (rowsToDelete <= 0) {
        return;
    }
    beginRemoveRows(QModelIndex(), maxRows, static_cast<int>(m_changes.size()) - 1);
    m_changes.erase(m_changes.begin() + maxRows, m_changes.end());
    endRemoveRows();
}

} // namespace Data