File indexing completed on 2022-09-27 16:24:37

0001 /***************************************************************************
0002  *   Copyright (C) 2009-2018 by Daniel Nicoletti                           *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "FilesModel.h"
0022 
0023 #include <QMimeData>
0024 #include <QUrl>
0025 #include <QFileInfo>
0026 
0027 #include <QLoggingCategory>
0028 #include <PkIcons.h>
0029 #include <KLocalizedString>
0030 #include <QMimeType>
0031 #include <QMimeDatabase>
0032 #include <KIconLoader>
0033 #include <KService>
0034 
0035 Q_DECLARE_LOGGING_CATEGORY(APPER_SESSION)
0036 
0037 FilesModel::FilesModel(const QStringList &files, const QStringList &mimes, QObject *parent)
0038     : QStandardItemModel(parent)
0039     ,  m_mimes(mimes)
0040 {
0041     if (!files.isEmpty()) {
0042         QList<QUrl> urls;
0043         for (const QString &file : files) {
0044             urls << QUrl(file);
0045         }
0046         insertFiles(urls);
0047     } else if (!mimes.isEmpty()) {
0048         QMimeDatabase db;
0049         // we are searching for mimetypes
0050         for (const QString &mimeName : mimes) {
0051             QMimeType mime = db.mimeTypeForName(mimeName);
0052             if (mime.isValid()) {
0053                 auto item = new QStandardItem(mimeName);
0054                 item->setData(mimeName);
0055                 item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime.iconName(),
0056                                                                       KIconLoader::Desktop));
0057                 appendRow(item);
0058             }
0059         }
0060     }
0061 }
0062 
0063 bool FilesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
0064 {
0065     Q_UNUSED(action)
0066     Q_UNUSED(row)
0067     Q_UNUSED(column)
0068     Q_UNUSED(parent)
0069     bool ret = false;
0070     if (data->hasUrls()) {
0071         ret = insertFiles(data->urls());
0072     }
0073     return ret;
0074 }
0075 
0076 bool FilesModel::insertFiles(const QList<QUrl> &urls)
0077 {
0078     bool ret = false;
0079     for (const QUrl &url : urls) {
0080         const QString path = QUrl::fromPercentEncoding(url.path().toLatin1());
0081         if (files().contains(path)) {
0082             continue;
0083         }
0084 
0085         QFileInfo fileInfo(path);
0086         QStandardItem *item = nullptr;
0087         if (fileInfo.isFile()) {
0088             QMimeDatabase db;
0089             QMimeType mime = db.mimeTypeForFile(path, QMimeDatabase::MatchContent);
0090             qCDebug(APPER_SESSION) << url << mime.name();
0091             for (const QString &mimeType : qAsConst(m_mimes)) {
0092                 if (mime.name() == mimeType) {
0093                     ret = true;
0094                     qCDebug(APPER_SESSION) << "Found Supported Mime" << mimeType << mime.iconName();
0095                     item = new QStandardItem(fileInfo.fileName());
0096                     item->setData(path);
0097                     item->setToolTip(path);
0098                     item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime.iconName(),
0099                                                                           KIconLoader::Desktop));
0100                     break;
0101                 }
0102             }
0103 
0104             if (ret == false && m_mimes.isEmpty()) {
0105                 if (mime.name() == QLatin1String("application/x-desktop")) {
0106                     auto service = new KService(path);
0107                     item = new QStandardItem(service->name());
0108                     item->setData(true, Qt::UserRole);
0109                     item->setIcon(KIconLoader::global()->loadMimeTypeIcon(service->icon(),
0110                                                                           KIconLoader::Desktop));
0111                 } else {
0112                     item = new QStandardItem(fileInfo.fileName());
0113                     item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime.iconName(),
0114                                                                         KIconLoader::Desktop));
0115                 }
0116                 item->setData(path);
0117                 item->setToolTip(path);
0118             } else if (ret == false && !m_mimes.isEmpty()) {
0119                 item = new QStandardItem(fileInfo.fileName());
0120                 item->setData(path);
0121                 item->setToolTip(path);
0122                 item->setEnabled(false);
0123                 item->setIcon(KIconLoader::global()->loadIcon(QLatin1String("dialog-cancel"), KIconLoader::Desktop));
0124             }
0125         } else if (m_mimes.isEmpty()) {
0126             // It's not a file but we don't have a mime so it's ok
0127             item = new QStandardItem(fileInfo.fileName());
0128             item->setData(path);
0129             item->setToolTip(path);
0130             item->setIcon(KIconLoader::global()->loadIcon(QLatin1String("unknown"), KIconLoader::Desktop));
0131         }
0132 
0133         if (item) {
0134             appendRow(item);
0135         }
0136     }
0137     return ret;
0138 }
0139 
0140 QStringList FilesModel::mimeTypes() const
0141 {
0142     return { QStringLiteral("text/uri-list") };
0143 }
0144 
0145 Qt::DropActions FilesModel::supportedDropActions() const
0146 {
0147     return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
0148 }
0149 
0150 Qt::ItemFlags FilesModel::flags(const QModelIndex &index) const
0151 {
0152     Q_UNUSED(index)
0153     return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
0154 }
0155 
0156 QStringList FilesModel::files() const
0157 {
0158     QStringList ret;
0159     for (int i = 0; i < rowCount(); ++i) {
0160         if (item(i)->isEnabled()) {
0161             ret << item(i)->data().toString();
0162         }
0163     }
0164     return ret;
0165 }
0166 
0167 bool FilesModel::onlyApplications() const
0168 {
0169     for (int i = 0; i < rowCount(); ++i) {
0170         // UserRole is true if it is an application
0171         if (item(i)->isEnabled() == true &&
0172             item(i)->data(Qt::UserRole).toBool() == false) {
0173             return false; // there is something that is not an app
0174         }
0175     }
0176     return true;
0177 }
0178 
0179 #include "moc_FilesModel.cpp"