summaryrefslogtreecommitdiffstats
path: root/filesystemfileproxy.cpp
blob: 7c4925894799377d8d8fdd2a41041dc3e4c4c41c (plain)
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
/*
  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.
*/

#include <QModelIndex>
#include <QVariant>
#include <QFileSystemModel>
#include <QFileInfo>
#include <QDateTime>

#include <QDebug>

#include "filesystemfileproxy.h"
#include "helper.h"

FilesystemFileProxy::FilesystemFileProxy(QObject *parent) : QSortFilterProxyModel(parent) {}

QVariant FilesystemFileProxy::data(const QModelIndex &index, int role) const{
	if(!index.isValid()){
		return QVariant();
	}

	if(role == Qt::DisplayRole){
		if(index.column() == 2){
			QString filePath = index.data(QFileSystemModel::FilePathRole).toString();
			QString mimeType = Helper::mimeType(filePath);
			if(mimeType.contains('/')){
				mimeType = mimeType.split('/').at(1);
				mimeType = mimeType.remove(QRegExp("^x-"));
				return mimeType;
			}
		}
		if(index.column() == 3){
			QString filePath = index.data(QFileSystemModel::FilePathRole).toString();
			QFileInfo fi(filePath);
			return fi.lastModified().toString("MM-dd-yyyy hh:mm");
		}
	}
	return QSortFilterProxyModel::data(index, role);
}

bool FilesystemFileProxy::filterAcceptsRow(int sourcerow, const QModelIndex &sourceparent) const{
	QFileSystemModel *m = static_cast<QFileSystemModel*>(sourceModel());
	QModelIndex idx = m->index(sourcerow, 0, sourceparent);
	if(!idx.isValid()){
		return false;
	}
	QString fName = idx.data().toString();
	if(fName == "." ){
		return false;
	}
	return QSortFilterProxyModel::filterAcceptsRow(sourcerow, sourceparent);
}

bool FilesystemFileProxy::lessThan(const QModelIndex &left, const QModelIndex &right) const {
	if(left.model()->headerData(left.column(), Qt::Horizontal).toString() == tr("Name")){
		if(left.data().toString() == ".."){
			if(sortOrder() == Qt::AscendingOrder){
				return true;
			}else{
				return false;
			}
		}
		QFileSystemModel *source = static_cast<QFileSystemModel*>(sourceModel());
		if(source->isDir(left) && source->isDir(right)){
			return left.data().toString().toLower() < right.data().toString().toLower();
		}
		if(source->isDir(left)){
			return true;
		}
		if(source->isDir(right)){
			return false;
		}
		return left.data().toString().toLower() < right.data().toString().toLower();
	}
	if(left.model()->headerData(left.column(), Qt::Horizontal).toString() == tr("Size")){
		QFileSystemModel *source = static_cast<QFileSystemModel*>(sourceModel());
		QFileInfo lInfo = source->fileInfo(left);
		QFileInfo rInfo = source->fileInfo(right);
		if(lInfo.isDir() && rInfo.isDir()){
			return lInfo.fileName().toLower() < rInfo.fileName().toLower();
		}
		if(lInfo.isDir() && !rInfo.isDir()){
			return true;
		}
		return lInfo.size() < rInfo.size();
	}
	return QSortFilterProxyModel::lessThan(left, right);
}