Welcome to mirror list, hosted at ThFree Co, Russian Federation.

activitylistmodel.cpp « models « gui « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 26aa14805cabc16bd65fcbfa8fbce4bf1c804810 (plain)
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
283
284
285
286
287
288
289
290
291
292
/*
 * Copyright (C) by Klaas Freitag <freitag@owncloud.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 */

#include <QtCore>
#include <QAbstractListModel>
#include <QWidget>
#include <QIcon>
#include <QJsonObject>
#include <QJsonDocument>

#include "accessmanager.h"
#include "account.h"
#include "accountmanager.h"
#include "accountstate.h"
#include "folderman.h"
#include "guiutility.h"
#include "models.h"
#include "networkjobs/jsonjob.h"

#include "activitydata.h"
#include "activitylistmodel.h"

namespace OCC {

Q_LOGGING_CATEGORY(lcActivity, "gui.activity", QtInfoMsg)

ActivityListModel::ActivityListModel(QObject *parent)
    : QAbstractTableModel(parent)
{
}

QVariant ActivityListModel::data(const QModelIndex &index, int role) const
{
    Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));
    if (!index.isValid()) {
        return {};
    }

    const auto &a = _finalList.at(index.row());
    const AccountStatePtr accountState = AccountManager::instance()->account(a.uuid());
    if (!accountState) {
        return {};
    }
    const auto column = static_cast<ActivityRole>(index.column());
    switch (role) {
    case Models::UnderlyingDataRole:
        Q_FALLTHROUGH();
    case Qt::DisplayRole:
        switch (column) {
        case ActivityRole::Account:
            return a.accName();
        case ActivityRole::Text:
            return a.subject();
        case ActivityRole::PointInTime:
            if (role == Models::UnderlyingDataRole) {
                return a.dateTime();
            } else {
                return Utility::timeAgoInWords(a.dateTime());
            }
        case ActivityRole::Path: {
            QStringList list = FolderMan::instance()->findFileInLocalFolders(a.file(), accountState->account());
            if (!list.isEmpty()) {
                return list.at(0);
            }
            // File does not exist anymore? Let's try to open its path
            list = FolderMan::instance()->findFileInLocalFolders(QFileInfo(a.file()).path(), accountState->account());
            if (!list.isEmpty()) {
                return list.at(0);
            }
            return {};
        }
        case ActivityRole::ColumnCount:
            Q_UNREACHABLE();
            break;
        }
        break;
    case Qt::ToolTipRole:
        return tr("%1 %2 on %3").arg(a.subject(), Utility::timeAgoInWords(a.dateTime()), a.accName());
    case Qt::DecorationRole:
        switch (column) {
        case ActivityRole::Text:
            if (!accountState->account()->avatar().isNull()) {
                return QIcon(accountState->account()->avatar());
            } else {
                return Utility::getCoreIcon(QStringLiteral("account"));
            }
        default:
            return {};
        }
    default:
        return {};
    }
    Q_UNREACHABLE();
}

QVariant ActivityListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal) {
        const auto actionRole = static_cast<ActivityRole>(section);
        switch (role) {
        case Qt::DisplayRole:
            switch (actionRole) {
            case ActivityRole::Text:
                return tr("Activity");
            case ActivityRole::Account:
                return tr("Account");
            case ActivityRole::PointInTime:
                return tr("Time");
            case ActivityRole::Path:
                return tr("Local path");
            case ActivityRole::ColumnCount:
                Q_UNREACHABLE();
                break;
            }
            break;
        case Models::StringFormatWidthRole:
            switch (actionRole) {
            case ActivityRole::Text:
                return 120;
            case ActivityRole::Account:
                return 20;
            case ActivityRole::PointInTime:
                return 20;
            case ActivityRole::Path:
                return 30;
            case ActivityRole::ColumnCount:
                Q_UNREACHABLE();
                break;
            }
            break;
        }
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

int ActivityListModel::rowCount(const QModelIndex &parent) const
{
    Q_ASSERT(checkIndex(parent));
    if (parent.isValid()) {
        return 0;
    }
    return _finalList.count();
}

int ActivityListModel::columnCount(const QModelIndex &parent) const
{
    Q_ASSERT(checkIndex(parent));
    if (parent.isValid()) {
        return 0;
    }
    return static_cast<int>(ActivityRole::ColumnCount);
}

// current strategy: Fetch 100 items per Account
// ATTENTION: This method is const and thus it is not possible to modify
// the _activityLists hash or so. Doesn't make it easier...
bool ActivityListModel::canFetchMore(const QModelIndex &) const
{
    if (_activityLists.isEmpty())
        return true;

    for (auto i = _activityLists.begin(); i != _activityLists.end(); ++i) {
        AccountStatePtr accountState = i.key();
        if (accountState && accountState->isConnected()) {
            ActivityList activities = i.value();
            if (activities.count() == 0 && !_currentlyFetching.contains(accountState)) {
                return true;
            }
        }
    }

    return false;
}

void ActivityListModel::startFetchJob(AccountStatePtr ast)
{
    if (!ast || !ast->isConnected()) {
        return;
    }
    auto *job = new JsonApiJob(ast->account(), QStringLiteral("ocs/v2.php/cloud/activity"), { { QStringLiteral("page"), QStringLiteral("0") }, { QStringLiteral("pagesize"), QStringLiteral("100") } }, {}, this);

    QObject::connect(job, &JsonApiJob::finishedSignal,
        this, [job, ast, this] {
            _currentlyFetching.remove(ast);
            const auto activities = job->data().value(QStringLiteral("ocs")).toObject().value(QStringLiteral("data")).toArray();

            /*
             * in case the activity app is disabled or not installed, the server returns an empty 500 response instead of a response
             * with the expected status code 999
             * we are not entirely sure when this has changed, but it is likely that there is a regression in the activity addon
             * to support this new behavior, we have to fake the expected status code
             */
            if (job->reply()->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() != 200) {
                emit activityJobStatusCode(ast, 999);
                return;
            }

            ActivityList list;
            list.reserve(activities.size());
            for (const auto &activ : activities) {
                const auto json = activ.toObject();
                list.append(Activity { Activity::ActivityType,
                    json.value(QStringLiteral("id")).toVariant().value<Activity::Identifier>(),
                    ast->account(),
                    json.value(QStringLiteral("subject")).toString(),
                    json.value(QStringLiteral("message")).toString(),
                    json.value(QStringLiteral("file")).toString(),
                    QUrl(json.value(QStringLiteral("link")).toString()),
                    QDateTime::fromString(json.value(QStringLiteral("date")).toString(), Qt::ISODate) });
            }

            _activityLists[ast] = std::move(list);

            emit activityJobStatusCode(ast, job->ocsStatus());

            combineActivityLists();
        });

    _currentlyFetching.insert(ast);
    qCInfo(lcActivity) << "Start fetching activities for " << ast->account()->displayName();
    job->start();
}


void ActivityListModel::combineActivityLists()
{
    ActivityList resultList;
    for (const ActivityList &list : qAsConst(_activityLists)) {
        resultList.append(list);
    }
    setActivityList(std::move(resultList));
}

void ActivityListModel::setActivityList(const ActivityList &&resultList)
{
    beginResetModel();
    _finalList = resultList;
    endResetModel();
}

void ActivityListModel::fetchMore(const QModelIndex &)
{
    for (const AccountStatePtr &asp : AccountManager::instance()->accounts()) {
        if (!_activityLists.contains(asp) && asp->isConnected()) {
            _activityLists[asp] = ActivityList();
            startFetchJob(asp);
        }
    }
}

void ActivityListModel::slotRefreshActivity(AccountStatePtr ast)
{
    if (ast && _activityLists.contains(ast)) {
        _activityLists.remove(ast);
    }
    startFetchJob(ast);
}

void ActivityListModel::slotRemoveAccount(AccountStatePtr ast)
{
    if (_activityLists.contains(ast)) {
        const auto accountToRemove = ast->account()->uuid();

        QMutableListIterator<Activity> it(_finalList);

        int i = 0;
        while (it.hasNext()) {
            Activity activity = it.next();
            if (activity.uuid() == accountToRemove) {
                beginRemoveRows(QModelIndex(), i, i);
                it.remove();
                endRemoveRows();
            } else {
                ++i;
            }
        }
        _activityLists.remove(ast);
        _currentlyFetching.remove(ast);
    }
}
}