filebrowserproxymodel.cpp 6.82 KB
Newer Older
Alexander Stippich's avatar
Alexander Stippich committed
1
/*
Matthieu Gallien's avatar
Matthieu Gallien committed
2
3
4
5
   SPDX-FileCopyrightText: 2016 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
   SPDX-FileCopyrightText: 2018 (c) Alexander Stippich <a.stippich@gmx.net>

   SPDX-License-Identifier: LGPL-3.0-or-later
Alexander Stippich's avatar
Alexander Stippich committed
6
7
8
9
10
 */

#include "filebrowserproxymodel.h"

#include "filebrowsermodel.h"
11
#include "mediaplaylistproxymodel.h"
Alexander Stippich's avatar
Alexander Stippich committed
12
13
14

#include <QReadLocker>
#include <QDir>
Laurent Montel's avatar
Laurent Montel committed
15
16
#include <QtConcurrent>

Alexander Stippich's avatar
Alexander Stippich committed
17
18
19
20
21
22
23
24
25
26
27
#include <KIOWidgets/KDirLister>
#include "elisautils.h"

FileBrowserProxyModel::FileBrowserProxyModel(QObject *parent) : KDirSortFilterProxyModel(parent)
{
    setFilterCaseSensitivity(Qt::CaseInsensitive);
    mThreadPool.setMaxThreadCount(1);
    setSortFoldersFirst(true);
    sort(Qt::AscendingOrder);
}

28
FileBrowserProxyModel::~FileBrowserProxyModel() = default;
Alexander Stippich's avatar
Alexander Stippich committed
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

QString FileBrowserProxyModel::filterText() const
{
    return mFilterText;
}

void FileBrowserProxyModel::setFilterText(const QString &filterText)
{
    QWriteLocker writeLocker(&mDataLock);

    if (mFilterText == filterText)
        return;

    mFilterText = filterText;

    mFilterExpression.setPattern(mFilterText);
    mFilterExpression.setPatternOptions(QRegularExpression::CaseInsensitiveOption);
    mFilterExpression.optimize();

    invalidate();

    Q_EMIT filterTextChanged(mFilterText);
}

bool FileBrowserProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
{
    bool result = false;

    for (int column = 0, columnCount = sourceModel()->columnCount(source_parent); column < columnCount; ++column) {
        auto currentIndex = sourceModel()->index(source_row, column, source_parent);

60
        const auto &nameValue = sourceModel()->data(currentIndex, Qt::DisplayRole).toString();
Alexander Stippich's avatar
Alexander Stippich committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

        if (mFilterExpression.match(nameValue).hasMatch()) {
            result = true;
            continue;
        }

        if (result) {
            continue;
        }

        if (!result) {
            break;
        }
    }

    return result;
}

79
80
81
void FileBrowserProxyModel::genericEnqueueToPlayList(QModelIndex rootIndex,
                                                     ElisaUtils::PlayListEnqueueMode enqueueMode,
                                                     ElisaUtils::PlayListEnqueueTriggerPlay triggerPlay)
Alexander Stippich's avatar
Alexander Stippich committed
82
83
84
{
    QtConcurrent::run(&mThreadPool, [=] () {
        QReadLocker locker(&mDataLock);
85
        auto allTrackUrls = DataTypes::EntryDataList{};
Alexander Stippich's avatar
Alexander Stippich committed
86
        for (int rowIndex = 0, maxRowCount = rowCount(); rowIndex < maxRowCount; ++rowIndex) {
87
            auto currentIndex = index(rowIndex, 0, rootIndex);
88
89
            if (!data(currentIndex, DataTypes::IsDirectoryRole).toBool()) {
                allTrackUrls.push_back({{}, {}, data(currentIndex, DataTypes::ResourceRole).toUrl()});
Alexander Stippich's avatar
Alexander Stippich committed
90
91
            }
        }
92
        Q_EMIT entriesToEnqueue(allTrackUrls, enqueueMode, triggerPlay);
Alexander Stippich's avatar
Alexander Stippich committed
93
94
95
    });
}

96
void FileBrowserProxyModel::enqueueToPlayList(QModelIndex rootIndex)
97
{
98
99
    genericEnqueueToPlayList(rootIndex,
                             ElisaUtils::AppendPlayList,
100
101
102
                             ElisaUtils::DoNotTriggerPlay);
}

103
void FileBrowserProxyModel::replaceAndPlayOfPlayList(QModelIndex rootIndex)
Alexander Stippich's avatar
Alexander Stippich committed
104
{
105
106
107
    genericEnqueueToPlayList(rootIndex,
                             ElisaUtils::ReplacePlayList,
                             ElisaUtils::TriggerPlay);
Alexander Stippich's avatar
Alexander Stippich committed
108
109
110
111
}

QString FileBrowserProxyModel::parentFolder() const
{
112
113
114
115
116
117
    auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());

    if (!fileBrowserModel) {
        return {};
    }

Alexander Stippich's avatar
Alexander Stippich committed
118
    //return to the top folder if parent directory does not exist
119
    QDir dir(fileBrowserModel->dirLister()->url().toLocalFile());
Alexander Stippich's avatar
Alexander Stippich committed
120
121
122
123
124
125
126
    if (dir.cdUp()) {
        return dir.path();
    } else {
        return mTopFolder;
    }
}

127
128
129
void FileBrowserProxyModel::disconnectPlayList()
{
    if (mPlayList) {
130
        disconnect(this, &FileBrowserProxyModel::entriesToEnqueue,
131
                   mPlayList, static_cast<void(MediaPlayListProxyModel::*)(const DataTypes::EntryDataList&, ElisaUtils::PlayListEnqueueMode, ElisaUtils::PlayListEnqueueTriggerPlay)>(&MediaPlayListProxyModel::enqueue));
132
133
134
135
136
137
    }
}

void FileBrowserProxyModel::connectPlayList()
{
    if (mPlayList) {
138
        connect(this, &FileBrowserProxyModel::entriesToEnqueue,
139
                mPlayList, static_cast<void(MediaPlayListProxyModel::*)(const DataTypes::EntryDataList&, ElisaUtils::PlayListEnqueueMode, ElisaUtils::PlayListEnqueueTriggerPlay)>(&MediaPlayListProxyModel::enqueue));
140
141
142
    }
}

Alexander Stippich's avatar
Alexander Stippich committed
143
144
void FileBrowserProxyModel::openParentFolder()
{
145
146
147
148
149
150
    auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());

    if (!fileBrowserModel) {
        return;
    }

Alexander Stippich's avatar
Alexander Stippich committed
151
152
    if (canGoBack()) {
        QString parent = parentFolder();
153
        fileBrowserModel->setUrl(parent);
Alexander Stippich's avatar
Alexander Stippich committed
154
155
156
157
158
159
160
161
        if (parent == mTopFolder) {
            Q_EMIT canGoBackChanged();
        }
    }
}

bool FileBrowserProxyModel::canGoBack() const
{
162
163
164
165
166
167
168
    auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());

    if (!fileBrowserModel) {
        return false;
    }

    return fileBrowserModel->dirLister()->url().toLocalFile() != mTopFolder;
Alexander Stippich's avatar
Alexander Stippich committed
169
170
171
172
}

void FileBrowserProxyModel::openFolder(const QString &folder, bool isDisplayRoot)
{
173
174
175
176
177
178
    auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());

    if (!fileBrowserModel) {
        return;
    }

Alexander Stippich's avatar
Alexander Stippich committed
179
180
181
    if (folder.isEmpty()) {
        return;
    }
182
183
184

    fileBrowserModel->setUrl(folder);

Alexander Stippich's avatar
Alexander Stippich committed
185
186
187
188
189
190
191
    if (!isDisplayRoot) {
        Q_EMIT canGoBackChanged();
    }
}

QString FileBrowserProxyModel::url() const
{
192
193
194
195
196
197
198
    auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel());

    if (!fileBrowserModel) {
        return {};
    }

    return fileBrowserModel->dirLister()->url().toLocalFile();
Alexander Stippich's avatar
Alexander Stippich committed
199
200
}

Alexander Stippich's avatar
Alexander Stippich committed
201
202
203
204
205
bool FileBrowserProxyModel::sortedAscending() const
{
    return sortOrder() ? false : true;
}

206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
void FileBrowserProxyModel::setSourceModel(QAbstractItemModel *sourceModel)
{
    KDirSortFilterProxyModel::setSourceModel(sourceModel);

    auto fileBrowserModel = dynamic_cast<FileBrowserModel*>(sourceModel);

    if (!fileBrowserModel) {
        return;
    }

    connect(fileBrowserModel, &FileBrowserModel::urlChanged,this, &FileBrowserProxyModel::urlChanged);
    mTopFolder = QDir::homePath();
    openFolder(mTopFolder, true);
}

221
MediaPlayListProxyModel *FileBrowserProxyModel::playList() const
222
223
224
225
{
    return mPlayList;
}

226
227
228
229
230
int FileBrowserProxyModel::filterRating() const
{
    return mFilterRating;
}

Alexander Stippich's avatar
Alexander Stippich committed
231
232
233
234
235
236
void FileBrowserProxyModel::sortModel(Qt::SortOrder order)
{
    this->sort(0,order);
    Q_EMIT sortedAscendingChanged();
}

237
void FileBrowserProxyModel::setPlayList(MediaPlayListProxyModel *playList)
238
239
240
241
242
243
244
245
246
247
248
249
250
{
    disconnectPlayList();

    if (mPlayList == playList) {
        return;
    }

    mPlayList = playList;
    Q_EMIT playListChanged();

    connectPlayList();
}

251
252
253
254
255
256
257
258
259
260
void FileBrowserProxyModel::setFilterRating(int filterRating)
{
    if (mFilterRating == filterRating) {
        return;
    }

    mFilterRating = filterRating;
    Q_EMIT filterRatingChanged();
}

Alexander Stippich's avatar
Alexander Stippich committed
261
#include "moc_filebrowserproxymodel.cpp"