~dantti/apper/master

« back to all changes in this revision

Viewing changes to PkSession/FilesModel.cpp

  • Committer: Daniel Nicoletti
  • Date: 2012-12-16 13:51:04 UTC
  • Revision ID: git-v1:4e1499a55d1ec8a637f03979cde84aaa4a4440ba
Renaming ApperSentinel to ApperPkSession since that's all it will do now

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2009-2010 by Daniel Nicoletti                           *
 
3
 *   dantti12@gmail.com                                                    *
 
4
 *                                                                         *
 
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.                                   *
 
9
 *                                                                         *
 
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.                          *
 
14
 *                                                                         *
 
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
 ***************************************************************************/
 
20
 
 
21
#include "FilesModel.h"
 
22
 
 
23
#include <QMimeData>
 
24
#include <QUrl>
 
25
#include <QFileInfo>
 
26
 
 
27
#include <KDebug>
 
28
#include <PkIcons.h>
 
29
#include <KLocale>
 
30
#include <KMimeType>
 
31
#include <KIconLoader>
 
32
#include <KService>
 
33
 
 
34
FilesModel::FilesModel(const QStringList &files, const QStringList &mimes, QObject *parent)
 
35
: QStandardItemModel(parent),
 
36
  m_mimes(mimes)
 
37
{
 
38
    if (!files.isEmpty()) {
 
39
        QList<QUrl> urls;
 
40
        foreach (const QString &file, files) {
 
41
            urls << file;
 
42
        }
 
43
        insertFiles(urls);
 
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);
 
51
                item->setData(mime);
 
52
                item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mimePtr->iconName(),
 
53
                                                                      KIconLoader::Desktop));
 
54
                appendRow(item);
 
55
            }
 
56
        }
 
57
    }
 
58
}
 
59
 
 
60
bool FilesModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
 
61
{
 
62
    Q_UNUSED(action)
 
63
    Q_UNUSED(row)
 
64
    Q_UNUSED(column)
 
65
    Q_UNUSED(parent)
 
66
    bool ret = false;
 
67
    if (data->hasUrls()) {
 
68
        ret = insertFiles(data->urls());
 
69
    }
 
70
    return ret;
 
71
}
 
72
 
 
73
bool FilesModel::insertFiles(const QList<QUrl> &urls)
 
74
{
 
75
    bool ret = false;
 
76
    foreach (const QUrl &url, urls) {
 
77
        if (files().contains(url.path())) {
 
78
            continue;
 
79
        }
 
80
 
 
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)) {
 
88
                    ret = true;
 
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));
 
95
                    break;
 
96
                }
 
97
            }
 
98
 
 
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));
 
106
                } else {
 
107
                    item = new QStandardItem(fileInfo.fileName());
 
108
                    item->setIcon(KIconLoader::global()->loadMimeTypeIcon(mime->iconName(),
 
109
                                                                        KIconLoader::Desktop));
 
110
                }
 
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));
 
119
            }
 
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));
 
126
        }
 
127
 
 
128
        if (item) {
 
129
            appendRow(item);
 
130
        }
 
131
    }
 
132
    return ret;
 
133
}
 
134
 
 
135
QStringList FilesModel::mimeTypes() const
 
136
{
 
137
    return QStringList() << "text/uri-list";
 
138
}
 
139
 
 
140
Qt::DropActions FilesModel::supportedDropActions() const
 
141
{
 
142
    return Qt::CopyAction | Qt::MoveAction | Qt::LinkAction;
 
143
}
 
144
 
 
145
Qt::ItemFlags FilesModel::flags(const QModelIndex &index) const
 
146
{
 
147
    Q_UNUSED(index)
 
148
    return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled;
 
149
}
 
150
 
 
151
QStringList FilesModel::files() const
 
152
{
 
153
    QStringList ret;
 
154
    for (int i = 0; i < rowCount(); ++i) {
 
155
        if (item(i)->isEnabled()) {
 
156
            ret << item(i)->data().toString();
 
157
        }
 
158
    }
 
159
    return ret;
 
160
}
 
161
 
 
162
bool FilesModel::onlyApplications() const
 
163
{
 
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
 
169
        }
 
170
    }
 
171
    return true;
 
172
}
 
173
 
 
174
#include "FilesModel.moc"