filteredfoldermodel.cpp 6.68 KB
Newer Older
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
/*
 * This file is part of the KDE Baloo Project
 * Copyright (C) 2014  Vishesh Handa <me@vhanda.in>
 * Copyright (C) 2019 Tomaz Canabrava <tcanabrava@kde.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "filteredfoldermodel.h"

#include <Solid/Device>
#include <Solid/StorageAccess>
#include <Solid/StorageDrive>

#include <QIcon>

#include <QDir>
#include <QTimer>
#include <QUrl>
#include <KLocalizedString>
#include <QStringList>

namespace {
    QStringList addTrailingSlashes(const QStringList& input) {
        QStringList output = input;

        for (QString& str : output) {
            if (!str.endsWith(QDir::separator()))
                str.append(QDir::separator());
        }

        return output;
    }

    QString makeHomePretty(const QString& url) {
        if (url.startsWith(QDir::homePath()))
            return QString(url).replace(0, QDir::homePath().length(), QStringLiteral("~"));
        return url;
    }
}

FilteredFolderModel::FilteredFolderModel(QObject* parent)
    : QAbstractListModel(parent)
{
}

void FilteredFolderModel::setDirectoryList(const QStringList& include, const QStringList& exclude)
{
    beginResetModel();

    m_mountPoints.clear();

    QList<Solid::Device> devices = Solid::Device::listFromType(Solid::DeviceInterface::StorageAccess);

    for (const Solid::Device& dev : devices) {
        const Solid::StorageAccess* sa = dev.as<Solid::StorageAccess>();
        if (!sa->isAccessible())
            continue;

        const QString mountPath = sa->filePath();
        if (!shouldShowMountPoint(mountPath))
            continue;

        m_mountPoints.append(mountPath);
    }
    m_mountPoints.append(QDir::homePath());
    m_mountPoints = addTrailingSlashes(m_mountPoints);

    QStringList includeList = addTrailingSlashes(include);

    m_excludeList = addTrailingSlashes(exclude);

    // This algorithm seems bogus. verify later.
    for (const QString& mountPath : m_mountPoints) {
        if (includeList.contains(mountPath))
            continue;

        if (exclude.contains(mountPath))
            continue;

        if (!m_excludeList.contains(mountPath)) {
            m_excludeList.append(mountPath);
        }
    }

    endResetModel();
}

QVariant FilteredFolderModel::data(const QModelIndex& idx, int role) const
{
    if (!idx.isValid() || idx.row() >= m_excludeList.size()) {
        return {};
    }

    const auto currentUrl = m_excludeList.at(idx.row());
    switch (role) {
        case Qt::DisplayRole: return folderDisplayName(currentUrl);
        case Qt::WhatsThisRole: return currentUrl;
        case Qt::DecorationRole: return QIcon::fromTheme(iconName(currentUrl));
        case Qt::ToolTipRole: return makeHomePretty(currentUrl);
        case Url: return currentUrl;
        case Folder: return  folderDisplayName(currentUrl);
        default:
            return {};
    }

    return {};
 }

int FilteredFolderModel::rowCount(const QModelIndex& parent) const
{
    Q_UNUSED(parent);
    return m_excludeList.count();
}

QStringList FilteredFolderModel::includeFolders() const
{
    const QSet<QString> mountPointSet =
          QSet<QString>::fromList(m_mountPoints)
        - QSet<QString>::fromList(m_excludeList);

135
    return mountPointSet.values();
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
}

QStringList FilteredFolderModel::excludeFolders() const
{
    return m_excludeList;
}

QString FilteredFolderModel::fetchMountPoint(const QString& url) const
{
    QString mountPoint;

    for (const QString& mount : qAsConst(m_mountPoints)) {
        if (url.startsWith(mount) && mount.size() > mountPoint.size())
            mountPoint = mount;
    }

    return mountPoint;
}


void FilteredFolderModel::addFolder(const QString& url)
{
    if (m_excludeList.contains(url)) {
        return;
    }
    beginResetModel();
    m_excludeList.append(QUrl(url).toLocalFile());
    std::sort(std::begin(m_excludeList), std::end(m_excludeList));
    endResetModel();
    Q_EMIT folderAdded();
}

void FilteredFolderModel::removeFolder(int row)
{
    beginRemoveRows(QModelIndex(), row, row);
    m_excludeList.removeAt(row);
    endRemoveRows();
    Q_EMIT folderRemoved();
}


QString FilteredFolderModel::folderDisplayName(const QString& url) const
{
    QString name = url;

    // Check Home Dir
    QString homePath = QDir::homePath() + QLatin1Char('/');
    if (url == homePath) {
        return QDir(homePath).dirName();
    }

    if (url.startsWith(homePath)) {
        name = url.mid(homePath.size());
    }
    else {
        // Check Mount allMountPointsExcluded
        for (QString mountPoint : m_mountPoints) {
            if (url.startsWith(mountPoint)) {
194
                name = QLatin1Char('[') + mountPoint+ QLatin1String("]/") + url.mid(mountPoint.length());
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
                break;
            }
        }
    }

    if (name.endsWith(QLatin1Char('/'))) {
        name = name.mid(0, name.size() - 1);
    }
    return name;
}

bool FilteredFolderModel::shouldShowMountPoint(const QString& mountPoint)
{
    if (mountPoint == QLatin1String("/"))
        return false;

    if (mountPoint.startsWith(QLatin1String("/boot")))
        return false;

    if (mountPoint.startsWith(QLatin1String("/tmp")))
        return false;

    // The user's home directory is forcibly added so we can ignore /home
    // if /home actually contains the home directory
    return !(mountPoint.startsWith(QLatin1String("/home")) || !QDir::homePath().startsWith(QLatin1String("/home")));
}

QHash<int, QByteArray> FilteredFolderModel::roleNames() const
{
    return {
        {Url, "url"},
        {Folder, "folder"},
        {Qt::DecorationRole, "decoration"}
    };
}

QString FilteredFolderModel::iconName(QString path) const
{
    // Ensure paths end with /
    if (!path.endsWith(QDir::separator()))
        path.append(QDir::separator());

    QString homePath = QDir::homePath();
    if (!homePath.endsWith(QDir::separator()))
        homePath.append(QDir::separator());

    if (path == homePath)
        return QStringLiteral("user-home");

    if (m_mountPoints.contains(path))
        return QStringLiteral("drive-harddisk");

    return QStringLiteral("folder");
}