File: FilesModel.cpp

package info (click to toggle)
apper 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,868 kB
  • sloc: cpp: 14,381; xml: 941; makefile: 15; sh: 4
file content (179 lines) | stat: -rw-r--r-- 6,804 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
/***************************************************************************
 *   Copyright (C) 2009-2018 by Daniel Nicoletti                           *
 *   dantti12@gmail.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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; see the file COPYING. If not, write to       *
 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
 *   Boston, MA 02110-1301, USA.                                           *
 ***************************************************************************/

#include "FilesModel.h"

#include <QMimeData>
#include <QUrl>
#include <QFileInfo>

#include <QLoggingCategory>
#include <PkIcons.h>
#include <KLocalizedString>
#include <QMimeType>
#include <QMimeDatabase>
#include <KIconLoader>
#include <KService>

Q_DECLARE_LOGGING_CATEGORY(APPER_SESSION)

FilesModel::FilesModel(const QStringList &files, const QStringList &mimes, QObject *parent)
    : QStandardItemModel(parent)
    ,  m_mimes(mimes)
{
    if (!files.isEmpty()) {
        QList<QUrl> urls;
        for (const QString &file : files) {
            urls << QUrl(file);
        }
        insertFiles(urls);
    } else if (!mimes.isEmpty()) {
        QMimeDatabase db;
        // we are searching for mimetypes
        for (const QString &mimeName : mimes) {
            QMimeType mime = db.mimeTypeForName(mimeName);
            if (mime.isValid()) {
                auto item = new QStandardItem(mimeName);
                item->setData(mimeName);
                item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime.iconName(),
                                                                      KIconLoader::Desktop));
                appendRow(item);
            }
        }
    }
}

bool FilesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
{
    Q_UNUSED(action)
    Q_UNUSED(row)
    Q_UNUSED(column)
    Q_UNUSED(parent)
    bool ret = false;
    if (data->hasUrls()) {
        ret = insertFiles(data->urls());
    }
    return ret;
}

bool FilesModel::insertFiles(const QList<QUrl> &urls)
{
    bool ret = false;
    for (const QUrl &url : urls) {
        const QString path = QUrl::fromPercentEncoding(url.path().toLatin1());
        if (files().contains(path)) {
            continue;
        }

        QFileInfo fileInfo(path);
        QStandardItem *item = 0;
        if (fileInfo.isFile()) {
            QMimeDatabase db;
            QMimeType mime = db.mimeTypeForFile(path, QMimeDatabase::MatchContent);
            qCDebug(APPER_SESSION) << url << mime.name();
            for (const QString &mimeType : qAsConst(m_mimes)) {
                if (mime.name() == mimeType) {
                    ret = true;
                    qCDebug(APPER_SESSION) << "Found Supported Mime" << mimeType << mime.iconName();
                    item = new QStandardItem(fileInfo.fileName());
                    item->setData(path);
                    item->setToolTip(path);
                    item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime.iconName(),
                                                                          KIconLoader::Desktop));
                    break;
                }
            }

            if (ret == false && m_mimes.isEmpty()) {
                if (mime.name() == QLatin1String("application/x-desktop")) {
                    auto service = new KService(path);
                    item = new QStandardItem(service->name());
                    item->setData(true, Qt::UserRole);
                    item->setIcon(KIconLoader::global()->loadMimeTypeIcon(service->icon(),
                                                                          KIconLoader::Desktop));
                } else {
                    item = new QStandardItem(fileInfo.fileName());
                    item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime.iconName(),
                                                                        KIconLoader::Desktop));
                }
                item->setData(path);
                item->setToolTip(path);
            } else if (ret == false && !m_mimes.isEmpty()) {
                item = new QStandardItem(fileInfo.fileName());
                item->setData(path);
                item->setToolTip(path);
                item->setEnabled(false);
                item->setIcon(KIconLoader::global()->loadIcon(QLatin1String("dialog-cancel"), KIconLoader::Desktop));
            }
        } else if (m_mimes.isEmpty()) {
            // It's not a file but we don't have a mime so it's ok
            item = new QStandardItem(fileInfo.fileName());
            item->setData(path);
            item->setToolTip(path);
            item->setIcon(KIconLoader::global()->loadIcon(QLatin1String("unknown"), KIconLoader::Desktop));
        }

        if (item) {
            appendRow(item);
        }
    }
    return ret;
}

QStringList FilesModel::mimeTypes() const
{
    return { QStringLiteral("text/uri-list") };
}

Qt::DropActions FilesModel::supportedDropActions() const
{
    return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
}

Qt::ItemFlags FilesModel::flags(const QModelIndex &index) const
{
    Q_UNUSED(index)
    return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
}

QStringList FilesModel::files() const
{
    QStringList ret;
    for (int i = 0; i < rowCount(); ++i) {
        if (item(i)->isEnabled()) {
            ret << item(i)->data().toString();
        }
    }
    return ret;
}

bool FilesModel::onlyApplications() const
{
    for (int i = 0; i < rowCount(); ++i) {
        // UserRole is true if it is an application
        if (item(i)->isEnabled() == true &&
            item(i)->data(Qt::UserRole).toBool() == false) {
            return false; // there is something that is not an app
        }
    }
    return true;
}

#include "moc_FilesModel.cpp"