blob: 0a0578ba845c733b7cada15cfe6b250824fe5655 (
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
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 <QDir>
#include <QDateTime>
#include <QFile>
#include <sys/stat.h>
#include "smdirmodel.h"
#include "smdirwatcher.h"
#include "smtreeitem.h"
#include "helper.h"
SmDirModel::SmDirModel(const QStringList &headers, QObject *parent) : SmTreeModel(headers, parent), mHeaders(headers){
mWatch = new SmDirWatcher(this);
}
SmDirModel::~SmDirModel(){
delete mWatch;
}
QVariant SmDirModel::data(const QModelIndex &index, int role) const{
if(!index.isValid()){
return QVariant();
}
SmTreeItem *i = itemAt(index);
switch (role){
case NameRole:
return i->data(Name);
case SizeRole:
return i->data(Size);
case TypeRole:
return i->data(Type);
case CreatedRole:
return i->data(Created);
case Md5sumRole:
return i->data(Md5sum);
case DurationRole:
return i->data(Duration);
case BitrateRole:
return i->data(Bitrate);
case FullPathRole:
return i->data(FullPath);
default:
return SmTreeModel::data(index, role);
}
}
bool SmDirModel::setData(const QModelIndex &index, const QVariant &value, int role){
if(!index.isValid()){
return false;
}
if(role == Qt::EditRole && index.column() == Name){
QString newName = value.toString();
if(newName.contains(QDir::separator())){
return false;
}
SmTreeItem *i = itemAt(index);
QString old = i->data(FullPath).toString();
QString dir = fileInfo(index).absolutePath();
QString newPath = QString("%1/%2").arg(dir).arg(newName);
QFile::rename(old, newPath);
return true;
}
return false;
}
bool SmDirModel::isDir(const QModelIndex &idx) const {
if(!idx.isValid()){
return false;
}
SmTreeItem *i = itemAt(idx);
QFileInfo fi(i->data(FullPath).toString());
return fi.isDir();
}
QFileInfo SmDirModel::fileInfo(const QModelIndex &idx) const {
if(!idx.isValid()){
return QFileInfo();
}
SmTreeItem *i = itemAt(idx);
return QFileInfo(i->data(FullPath).toString());
}
void SmDirModel::setDir(const QString &dir){
QFileInfo fi(dir);
if(!fi.isDir()){
return;
}
mCur = dir;
populate();
mWatch->setDir(dir);
}
void SmDirModel::populate(){
SmTreeItem *root = new SmTreeItem(mHeaders.size());
QDir d = QDir(mCur);
foreach(QFileInfo fi, d.entryInfoList()){
if(fi.fileName() == "."){
continue;
}
QList<QVariant> data = fileData(fi);
SmTreeItem *n = new SmTreeItem(data, root);
root->appendChild(n);
}
setRoot(root);
}
const QList<QVariant> SmDirModel::fileData(const QFileInfo &fi) const{
QList<QVariant> data;
data << fi.fileName() << fi.size();
QString mime = Helper::mimeType(fi.absoluteFilePath());
data << mime;
data << fi.lastModified();
QList<QVariant> si = QList<QVariant>() << QVariant() << QVariant();
if(mime.startsWith("video")){
si = Helper::duration(fi.absoluteFilePath());
}
data << Helper::md5Sum(fi.absoluteFilePath());
data << si << fi.absoluteFilePath();
return data;
}
|