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

spacesmodel.cpp « spaces « gui « src - github.com/owncloud/client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97fa78eac086fbfe588cf64ebf67d87c0cdd341b (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
/*
 * Copyright (C) by Hannah von Reth <hannah.vonreth@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 "spacesmodel.h"

#include "networkjobs.h"

#include <QPixmap>

namespace {
constexpr QSize ImageSizeC(128, 128);
}

using namespace OCC::Spaces;

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

QVariant SpacesModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal) {
        const auto actionRole = static_cast<Columns>(section);
        switch (role) {
        case Qt::DisplayRole:
            switch (actionRole) {
            case Columns::Name:
                return tr("Name");
            case Columns::Description:
                return tr("Description");
            case Columns::WebUrl:
                return tr("Web URL");
            case Columns::WebDavUrl:
                return tr("Web Dav URL");
            case Columns::Image:
                return tr("Image");
            case Columns::ColumnCount:
                Q_UNREACHABLE();
                break;
            }
        }
    }
    return QAbstractTableModel::headerData(section, orientation, role);
}

int SpacesModel::rowCount(const QModelIndex &parent) const
{
    Q_ASSERT(checkIndex(parent));
    if (parent.isValid())
        return 0;
    return static_cast<int>(_data.size());
}

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

QVariant SpacesModel::data(const QModelIndex &index, int role) const
{
    Q_ASSERT(checkIndex(index, QAbstractItemModel::CheckIndexOption::IndexIsValid));

    const auto column = static_cast<Columns>(index.column());
    const auto &item = _data.at(index.row());
    switch (role) {
    case Qt::DisplayRole:
        switch (column) {
        case Columns::Name:
            return GraphApi::Drives::getDriveDisplayName(item);
        case Columns::Description:
            return item.getDescription();
        case Columns::WebUrl:
            return item.getWebUrl();
        case Columns::WebDavUrl:
            return item.getRoot().getWebDavUrl();
        case Columns::Image: {
            const auto &special = item.getSpecial();
            const auto img = std::find_if(special.cbegin(), special.cend(), [](OpenAPI::OAIDriveItem it) {
                return it.getSpecialFolder().getName() == QLatin1String("image");
            });
            return img == special.cend() ? QString() : img->getWebDavUrl();
        }
        case Columns::ColumnCount:
            Q_UNREACHABLE();
            break;
        }
        break;
    case Qt::DecorationRole:
        switch (column) {
        case Columns::Image: {
            auto it = _images.constFind(item.getId());
            if (it != _images.cend()) {
                return QVariant::fromValue(*it);
            }
            const auto imgUrl = data(index, Qt::DisplayRole);
            if (!imgUrl.isValid()) {
                return {};
            }
            // TODO: placeholder
            _images[item.getId()] = QPixmap();
            auto davUrl = QUrl(item.getRoot().getWebDavUrl());
            auto path = imgUrl.toString().remove(item.getRoot().getWebDavUrl());
            auto job = new OCC::SimpleNetworkJob(_acc, davUrl, path, "GET", {}, {}, nullptr);
            connect(job, &OCC::SimpleNetworkJob::finishedSignal, this, [job, id = item.getId(), index, this] {
                QPixmap img;
                qDebug() << img.loadFromData(job->reply()->readAll());
                img = img.scaled(ImageSizeC, Qt::KeepAspectRatio);
                _images[id] = img;
                Q_EMIT const_cast<SpacesModel *>(this)->dataChanged(index, index, { Qt::DecorationRole });
            });
            job->start();
            return _images[item.getId()];
        }
        default:
            return {};
        }
    case Qt::SizeHintRole: {
        switch (column) {
        case Columns::Image:
            return ImageSizeC;
        default:
            return {};
        }
    }
    }
    return {};
}

void SpacesModel::setData(OCC::AccountPtr acc, const QList<OpenAPI::OAIDrive> &data)
{
    beginResetModel();
    _acc = acc;
    _data = data;
    endResetModel();
}