1
/***************************************************************************
2
* Copyright (C) 2009-2010 by Daniel Nicoletti *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; see the file COPYING. If not, write to *
17
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18
* Boston, MA 02110-1301, USA. *
19
***************************************************************************/
21
#include "FilesModel.h"
31
#include <KIconLoader>
34
FilesModel::FilesModel(const QStringList &files, const QStringList &mimes, QObject *parent)
35
: QStandardItemModel(parent),
38
if (!files.isEmpty()) {
40
foreach (const QString &file, files) {
44
} else if (!mimes.isEmpty()) {
45
// we are searching for mimetypes
46
foreach (const QString &mime, mimes) {
47
KMimeType::Ptr mimePtr = KMimeType::mimeType(mime);
48
// Make sure we have a pointer
49
if (mimePtr && mimePtr->isValid()) {
50
QStandardItem *item = new QStandardItem(mime);
52
item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mimePtr->iconName(),
53
KIconLoader::Desktop));
60
bool FilesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
67
if (data->hasUrls()) {
68
ret = insertFiles(data->urls());
73
bool FilesModel::insertFiles(const QList<QUrl> &urls)
76
foreach (const QUrl &url, urls) {
77
if (files().contains(url.path())) {
81
QFileInfo fileInfo(url.path());
82
QStandardItem *item = 0;
83
if (fileInfo.isFile()) {
84
KMimeType::Ptr mime = KMimeType::findByFileContent(url.path());
85
kDebug() << url << mime->name();
86
foreach (const QString &mimeType, m_mimes) {
87
if (mime->is(mimeType)) {
89
/* kDebug() << "Found Supported Mime" << mimeType << mime->iconName();*/
90
item = new QStandardItem(fileInfo.fileName());
91
item->setData(url.path());
92
item->setToolTip(url.path());
93
item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime->iconName(),
94
KIconLoader::Desktop));
99
if (ret == false && m_mimes.isEmpty()) {
100
if (mime->name() == "application/x-desktop") {
101
KService *service = new KService(url.path());
102
item = new QStandardItem(service->name());
103
item->setData(true, Qt::UserRole);
104
item->setIcon(KIconLoader::global()->loadMimeTypeIcon(service->icon(),
105
KIconLoader::Desktop));
107
item = new QStandardItem(fileInfo.fileName());
108
item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime->iconName(),
109
KIconLoader::Desktop));
111
item->setData(url.path());
112
item->setToolTip(url.path());
113
} else if (ret == false && !m_mimes.isEmpty()) {
114
item = new QStandardItem(fileInfo.fileName());
115
item->setData(url.path());
116
item->setToolTip(url.path());
117
item->setEnabled(false);
118
item->setIcon(KIconLoader::global()->loadIcon("dialog-cancel", KIconLoader::Desktop));
120
} else if (m_mimes.isEmpty()) {
121
// It's not a file but we don't have a mime so it's ok
122
item = new QStandardItem(fileInfo.fileName());
123
item->setData(url.path());
124
item->setToolTip(url.path());
125
item->setIcon(KIconLoader::global()->loadIcon("unknown", KIconLoader::Desktop));
135
QStringList FilesModel::mimeTypes() const
137
return QStringList() << "text/uri-list";
140
Qt::DropActions FilesModel::supportedDropActions() const
142
return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
145
Qt::ItemFlags FilesModel::flags(const QModelIndex &index) const
148
return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
151
QStringList FilesModel::files() const
154
for (int i = 0; i < rowCount(); ++i) {
155
if (item(i)->isEnabled()) {
156
ret << item(i)->data().toString();
162
bool FilesModel::onlyApplications() const
164
for (int i = 0; i < rowCount(); ++i) {
165
// UserRole is true if it is an application
166
if (item(i)->isEnabled() == true &&
167
item(i)->data(Qt::UserRole).toBool() == false) {
168
return false; // there is something that is not an app
174
#include "FilesModel.moc"