2
* Copyright (C) 2012 - 2015 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Lesser General Public
6
* License as published by the Free Software Foundation; either
7
* version 2.1 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Lesser General Public License for more details.
14
* You should have received a copy of the GNU Lesser General Public
15
* License along with this library; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
#include "foldermodel.h"
22
#include "icontheme.h"
24
#include <QtAlgorithms>
26
#include <qmimedata.h>
31
#include "utilities.h"
32
#include "fileoperation.h"
33
#include "thumbnailloader.h"
37
FolderModel::FolderModel() :
46
thumbnailRefCounts.reserve(4);
48
// reload all icons when the icon theme is changed
49
connect(IconTheme::instance(), &IconTheme::changed, this, &FolderModel::updateIcons);
52
FolderModel::~FolderModel() {
53
qDebug("delete FolderModel");
58
// if the thumbnail requests list is not empty, cancel them
59
if(!thumbnailResults.empty()) {
60
Q_FOREACH(FmThumbnailLoader* res, thumbnailResults) {
61
ThumbnailLoader::cancel(res);
66
void FolderModel::setFolder(FmFolder* new_folder) {
68
removeAll(); // remove old items
69
g_signal_handlers_disconnect_by_func(folder_, gpointer(onStartLoading), this);
70
g_signal_handlers_disconnect_by_func(folder_, gpointer(onFinishLoading), this);
71
g_signal_handlers_disconnect_by_func(folder_, gpointer(onFilesAdded), this);
72
g_signal_handlers_disconnect_by_func(folder_, gpointer(onFilesChanged), this);
73
g_signal_handlers_disconnect_by_func(folder_, gpointer(onFilesRemoved), this);
74
g_object_unref(folder_);
77
folder_ = FM_FOLDER(g_object_ref(new_folder));
78
g_signal_connect(folder_, "start-loading", G_CALLBACK(onStartLoading), this);
79
g_signal_connect(folder_, "finish-loading", G_CALLBACK(onFinishLoading), this);
80
g_signal_connect(folder_, "files-added", G_CALLBACK(onFilesAdded), this);
81
g_signal_connect(folder_, "files-changed", G_CALLBACK(onFilesChanged), this);
82
g_signal_connect(folder_, "files-removed", G_CALLBACK(onFilesRemoved), this);
83
// handle the case if the folder is already loaded
84
if(fm_folder_is_loaded(folder_))
85
insertFiles(0, fm_folder_get_files(folder_));
91
void FolderModel::onStartLoading(FmFolder* folder, gpointer user_data) {
92
FolderModel* model = static_cast<FolderModel*>(user_data);
97
void FolderModel::onFinishLoading(FmFolder* folder, gpointer user_data) {
102
void FolderModel::onFilesAdded(FmFolder* folder, GSList* files, gpointer user_data) {
103
FolderModel* model = static_cast<FolderModel*>(user_data);
104
int n_files = g_slist_length(files);
105
model->beginInsertRows(QModelIndex(), model->items.count(), model->items.count() + n_files - 1);
106
for(GSList* l = files; l; l = l->next) {
107
FmFileInfo* info = FM_FILE_INFO(l->data);
108
FolderModelItem item(info);
110
if(fm_file_info_is_hidden(info)) {
111
model->hiddenItems.append(item);
115
model->items.append(item);
117
model->endInsertRows();
121
void FolderModel::onFilesChanged(FmFolder* folder, GSList* files, gpointer user_data) {
122
FolderModel* model = static_cast<FolderModel*>(user_data);
123
for(GSList* l = files; l; l = l->next) {
124
FmFileInfo* info = FM_FILE_INFO(l->data);
126
QList<FolderModelItem>::iterator it = model->findItemByFileInfo(info, &row);
127
if(it != model->items.end()) {
128
FolderModelItem& item = *it;
129
// try to update the item
130
item.displayName = QString::fromUtf8(fm_file_info_get_disp_name(info));
132
item.thumbnails.clear();
133
QModelIndex index = model->createIndex(row, 0, &item);
134
Q_EMIT model->dataChanged(index, index);
140
void FolderModel::onFilesRemoved(FmFolder* folder, GSList* files, gpointer user_data) {
141
FolderModel* model = static_cast<FolderModel*>(user_data);
142
for(GSList* l = files; l; l = l->next) {
143
FmFileInfo* info = FM_FILE_INFO(l->data);
144
const char* name = fm_file_info_get_name(info);
146
QList<FolderModelItem>::iterator it = model->findItemByName(name, &row);
147
if(it != model->items.end()) {
148
model->beginRemoveRows(QModelIndex(), row, row);
149
model->items.erase(it);
150
model->endRemoveRows();
155
void FolderModel::insertFiles(int row, FmFileInfoList* files) {
156
int n_files = fm_file_info_list_get_length(files);
157
beginInsertRows(QModelIndex(), row, row + n_files - 1);
158
for(GList* l = fm_file_info_list_peek_head_link(files); l; l = l->next) {
159
FolderModelItem item(FM_FILE_INFO(l->data));
165
void FolderModel::removeAll() {
168
beginRemoveRows(QModelIndex(), 0, items.size() - 1);
173
int FolderModel::rowCount(const QModelIndex & parent) const {
179
int FolderModel::columnCount (const QModelIndex & parent = QModelIndex()) const {
185
FolderModelItem* FolderModel::itemFromIndex(const QModelIndex& index) const {
186
return reinterpret_cast<FolderModelItem*>(index.internalPointer());
189
FmFileInfo* FolderModel::fileInfoFromIndex(const QModelIndex& index) const {
190
FolderModelItem* item = itemFromIndex(index);
191
return item ? item->info : nullptr;
194
QVariant FolderModel::data(const QModelIndex & index, int role/* = Qt::DisplayRole*/) const {
195
if(!index.isValid() || index.row() > items.size() || index.column() >= NumOfColumns) {
198
FolderModelItem* item = itemFromIndex(index);
199
FmFileInfo* info = item->info;
202
case Qt::ToolTipRole:
203
return QVariant(item->displayName);
204
case Qt::DisplayRole: {
205
switch(index.column()) {
206
case ColumnFileName: {
207
return QVariant(item->displayName);
209
case ColumnFileType: {
210
FmMimeType* mime = fm_file_info_get_mime_type(info);
211
const char* desc = fm_mime_type_get_desc(mime);
212
return QString::fromUtf8(desc);
214
case ColumnFileMTime: {
215
const char* name = fm_file_info_get_disp_mtime(info);
216
return QString::fromUtf8(name);
218
case ColumnFileSize: {
219
const char* name = fm_file_info_get_disp_size(info);
220
return QString::fromUtf8(name);
222
case ColumnFileOwner: {
223
const char* name = fm_file_info_get_disp_owner(info);
224
return QString::fromUtf8(name);
228
case Qt::DecorationRole: {
229
if(index.column() == 0) {
230
// QPixmap pix = IconTheme::loadIcon(fm_file_info_get_icon(info), iconSize_);
231
return QVariant(item->icon);
232
// return QVariant(pix);
237
return qVariantFromValue((void*)info);
242
QVariant FolderModel::headerData(int section, Qt::Orientation orientation, int role/* = Qt::DisplayRole*/) const {
243
if(role == Qt::DisplayRole) {
244
if(orientation == Qt::Horizontal) {
256
case ColumnFileMTime:
257
title = tr("Modified");
259
case ColumnFileOwner:
263
return QVariant(title);
269
QModelIndex FolderModel::index(int row, int column, const QModelIndex & parent) const {
270
if(row <0 || row >= items.size() || column < 0 || column >= NumOfColumns)
271
return QModelIndex();
272
const FolderModelItem& item = items.at(row);
273
return createIndex(row, column, (void*)&item);
276
QModelIndex FolderModel::parent(const QModelIndex & index) const {
277
return QModelIndex();
280
Qt::ItemFlags FolderModel::flags(const QModelIndex& index) const {
281
// FIXME: should not return same flags unconditionally for all columns
283
if(index.isValid()) {
284
flags = Qt::ItemIsEnabled|Qt::ItemIsSelectable;
285
if(index.column() == ColumnFileName)
286
flags |= (Qt::ItemIsDragEnabled|Qt::ItemIsDropEnabled);
289
flags = Qt::ItemIsDropEnabled;
294
// FIXME: this is very inefficient and should be replaced with a
295
// more reasonable implementation later.
296
QList<FolderModelItem>::iterator FolderModel::findItemByPath(FmPath* path, int* row) {
297
QList<FolderModelItem>::iterator it = items.begin();
299
while(it != items.end()) {
300
FolderModelItem& item = *it;
301
FmPath* item_path = fm_file_info_get_path(item.info);
302
if(fm_path_equal(item_path, path)) {
312
// FIXME: this is very inefficient and should be replaced with a
313
// more reasonable implementation later.
314
QList<FolderModelItem>::iterator FolderModel::findItemByName(const char* name, int* row) {
315
QList<FolderModelItem>::iterator it = items.begin();
317
while(it != items.end()) {
318
FolderModelItem& item = *it;
319
const char* item_name = fm_file_info_get_name(item.info);
320
if(strcmp(name, item_name) == 0) {
330
QList< FolderModelItem >::iterator FolderModel::findItemByFileInfo(FmFileInfo* info, int* row) {
331
QList<FolderModelItem>::iterator it = items.begin();
333
while(it != items.end()) {
334
FolderModelItem& item = *it;
335
if(item.info == info) {
345
QStringList FolderModel::mimeTypes() const {
346
qDebug("FolderModel::mimeTypes");
347
QStringList types = QAbstractItemModel::mimeTypes();
348
// now types contains "application/x-qabstractitemmodeldatalist"
349
types << "text/uri-list";
350
// types << "x-special/gnome-copied-files";
354
QMimeData* FolderModel::mimeData(const QModelIndexList& indexes) const {
355
QMimeData* data = QAbstractItemModel::mimeData(indexes);
356
qDebug("FolderModel::mimeData");
359
urilist.reserve(4096);
361
for(const auto &index : indexes) {
362
FolderModelItem* item = itemFromIndex(index);
364
FmPath* path = fm_file_info_get_path(item->info);
365
char* uri = fm_path_to_uri(path);
367
urilist.append('\n');
371
data->setData("text/uri-list", urilist);
376
bool FolderModel::dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) {
377
qDebug("FolderModel::dropMimeData");
381
if(parent.isValid()) { // drop on an item
383
if(row == -1 && column == -1)
384
info = fileInfoFromIndex(parent);
386
QModelIndex itemIndex = parent.child(row, column);
387
info = fileInfoFromIndex(itemIndex);
390
destPath = fm_file_info_get_path(info);
394
else { // drop on blank area of the folder
398
// FIXME: should we put this in dropEvent handler of FolderView instead?
399
if(data->hasUrls()) {
400
qDebug("drop action: %d", action);
401
FmPathList* srcPaths = pathListFromQUrls(data->urls());
404
FileOperation::copyFiles(srcPaths, destPath);
407
FileOperation::moveFiles(srcPaths, destPath);
410
FileOperation::symlinkFiles(srcPaths, destPath);
412
fm_path_list_unref(srcPaths);
415
fm_path_list_unref(srcPaths);
418
else if(data->hasFormat("application/x-qabstractitemmodeldatalist")) {
421
return QAbstractListModel::dropMimeData(data, action, row, column, parent);
424
Qt::DropActions FolderModel::supportedDropActions() const {
425
qDebug("FolderModel::supportedDropActions");
426
return Qt::CopyAction|Qt::MoveAction|Qt::LinkAction;
429
// ask the model to load thumbnails of the specified size
430
void FolderModel::cacheThumbnails(const int size) {
431
QVector<QPair<int, int>>::iterator it = thumbnailRefCounts.begin();
432
while (it != thumbnailRefCounts.end()) {
433
if (it->first == size) {
438
thumbnailRefCounts.append(QPair<int, int>(size, 1));
441
// ask the model to free cached thumbnails of the specified size
442
void FolderModel::releaseThumbnails(int size) {
443
QVector<QPair<int, int> >::iterator it;
444
for(it = thumbnailRefCounts.begin(); it != thumbnailRefCounts.end(); ++it) {
445
if(it->first == size) {
449
if(it != thumbnailRefCounts.end()) {
451
if(it->second == 0) {
452
thumbnailRefCounts.erase(it);
454
// remove thumbnails that ara queued for loading from thumbnailResults
455
QLinkedList<FmThumbnailLoader*>::iterator it;
456
for(it = thumbnailResults.begin(); it != thumbnailResults.end();) {
457
QLinkedList<FmThumbnailLoader*>::iterator next = it + 1;
458
FmThumbnailLoader* res = *it;
459
if(ThumbnailLoader::size(res) == size) {
460
ThumbnailLoader::cancel(res);
461
thumbnailResults.erase(it);
466
// remove all cached thumbnails of the specified size
467
QList<FolderModelItem>::iterator itemIt;
468
for(itemIt = items.begin(); itemIt != items.end(); ++itemIt) {
469
FolderModelItem& item = *itemIt;
470
item.removeThumbnail(size);
476
void FolderModel::onThumbnailLoaded(FmThumbnailLoader* res, gpointer user_data) {
477
FolderModel* pThis = reinterpret_cast<FolderModel*>(user_data);
478
QLinkedList<FmThumbnailLoader*>::iterator it;
479
for(it = pThis->thumbnailResults.begin(); it != pThis->thumbnailResults.end(); ++it) {
480
if(*it == res) { // the thumbnail result is in our list
481
pThis->thumbnailResults.erase(it); // remove it from the list
482
FmFileInfo* info = ThumbnailLoader::fileInfo(res);
484
// find the model item this thumbnail belongs to
485
QList<FolderModelItem>::iterator it = pThis->findItemByFileInfo(info, &row);
486
if(it != pThis->items.end()) {
487
// the file is found in our model
488
FolderModelItem& item = *it;
489
QModelIndex index = pThis->createIndex(row, 0, (void*)&item);
490
// store the image in the folder model item.
491
int size = ThumbnailLoader::size(res);
492
QImage image = ThumbnailLoader::image(res);
493
FolderModelItem::Thumbnail* thumbnail = item.findThumbnail(size);
494
thumbnail->image = image;
495
// qDebug("thumbnail loaded for: %s, size: %d", item.displayName.toUtf8().constData(), size);
497
thumbnail->status = FolderModelItem::ThumbnailFailed;
499
thumbnail->status = FolderModelItem::ThumbnailLoaded;
500
// FIXME: due to bugs in Qt's QStyledItemDelegate, if the image width and height
501
// are not the same, painting errors will happen. It's quite unfortunate.
502
// Let's do some padding to make its width and height equals.
503
// This greatly decrease performance :-(
504
// Later if we can re-implement our own item delegate, this can be avoided.
505
QPixmap pixmap = QPixmap(size, size);
506
pixmap.fill(QColor(0, 0, 0, 0)); // fill the pixmap with transparent color (alpha:0)
507
QPainter painter(&pixmap);
508
int x = (size - image.width()) / 2;
509
int y = (size - image.height()) / 2;
510
painter.drawImage(QPoint(x, y), image); // draw the image to the pixmap at center.
511
// FIXME: should we cache QPixmap instead for performance reason?
512
thumbnail->image = pixmap.toImage(); // convert it back to image
514
// tell the world that we have the thumbnail loaded
515
Q_EMIT pThis->thumbnailLoaded(index, size);
523
// get a thumbnail of size at the index
524
// if a thumbnail is not yet loaded, this will initiate loading of the thumbnail.
525
QImage FolderModel::thumbnailFromIndex(const QModelIndex& index, int size) {
526
FolderModelItem* item = itemFromIndex(index);
528
FolderModelItem::Thumbnail* thumbnail = item->findThumbnail(size);
529
// qDebug("FolderModel::thumbnailFromIndex: %d, %s", thumbnail->status, item->displayName.toUtf8().data());
530
switch(thumbnail->status) {
531
case FolderModelItem::ThumbnailNotChecked: {
532
// load the thumbnail
533
FmThumbnailLoader* res = ThumbnailLoader::load(item->info, size, onThumbnailLoaded, this);
534
thumbnailResults.push_back(res);
535
thumbnail->status = FolderModelItem::ThumbnailLoading;
538
case FolderModelItem::ThumbnailLoaded:
539
return thumbnail->image;
546
void FolderModel::updateIcons() {
547
QList<FolderModelItem>::iterator it = items.begin();
548
for(;it != items.end(); ++it) {