aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/core/libLumina/LFileInfo.cpp
blob: 3b659c752e662ba468956ff3472a8a298e2278c2 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2013-2017, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "LFileInfo.h"
#include <LUtils.h>

LFileInfo::LFileInfo() : QFileInfo(){
  desk = 0;
}

LFileInfo::LFileInfo(QString filepath) : QFileInfo(){ //overloaded contructor
  desk = 0;
  this->setFile(filepath);
  loadExtraInfo();
}

LFileInfo::LFileInfo(QFileInfo info) : QFileInfo(){ //overloaded contructor
  desk = 0;
  this->swap(info); //use the given QFileInfo without re-loading it
  loadExtraInfo();
}
LFileInfo::~LFileInfo(){
  if(desk!=0){ desk->deleteLater(); }
}

//Need some extra information not usually available by a QFileInfo
void LFileInfo::loadExtraInfo(){
  if(desk!=0){ desk->deleteLater(); }
  desk = 0;
  //Now load the extra information
  if(this->absoluteFilePath().startsWith("/net/") || this->isDir() ){
    mime = "inode/directory";
    //Special directory icons
    QString name = this->fileName().toLower();
    if(name=="desktop"){ iconList << "user-desktop"; }
    else if(name=="tmp"){ iconList << "folder-temp"; }
    else if(name=="video" || name=="videos"){ iconList << "folder-video" << "camera-photo-film" ; }
    else if(name=="music" || name=="audio"){ iconList << "folder-sound" << "media-playlist-audio"; }
    else if(name=="projects" || name=="devel"){ iconList << "folder-development"; }
    else if(name=="notes"){ iconList << "folder-txt" << "note-multiple-outline" << "note-multiple"; }
    else if(name=="downloads"){ iconList << "folder-downloads" << "folder-download"; }
    else if(name=="documents"){ iconList << "folder-documents"; }
    else if(name=="images" || name=="pictures"){ iconList << "folder-image"; }
    else if(this->absoluteFilePath().startsWith("/net/")){ iconList << "folder-remote"; }
    else if( !this->isReadable() ){ iconList << "folder-locked"<< "folder-lock"; }
    iconList << "folder";
  }else if( this->suffix()=="desktop"){
    mime = "application/x-desktop";
    iconList << "application-x-desktop"; //default value
    desk = new XDGDesktop(this->absoluteFilePath(), 0);
    if(desk->type!=XDGDesktop::BAD){
      //use the specific desktop file info (if possible)
      if(!desk->icon.isEmpty()){ iconList << desk->icon; }
    }
  }else{
    //Generic file, just determine the mimetype
    mime = LXDG::findAppMimeForFile(this->fileName());
  }
  //check the mimetype for an icon as needed
  QString tmp = mime;
    tmp.replace("/","-");
    iconList << tmp;
  if(this->isExecutable()){
    iconList << "application-x-executable";
  }
}

bool LFileInfo::zfsAvailable(){
  static unsigned int avail = 2;
  if(avail == 2){ avail = (LUtils::isValidBinary("zfs") ? 0 : 1); }
  return (avail == 0);
}

void LFileInfo::getZfsDataset(){
  if(zfs_ds.isEmpty()){
    //First run - need to probe the current directory
    bool ok = false;
    //Use the "atime" property for this check - been around since the earliest versions of ZFS and should take no time to probe
    QString out = LUtils::runCommand(ok, "zfs", QStringList() << "get" << "-H" << "atime" << this->canonicalFilePath() );
    if(!ok){ zfs_ds = "."; } //just something that is not empty - but is clearly not a valid dataset
    else{  zfs_ds = out.section("\n",0,0).section("\t",0,0).simplified(); }
    //qDebug() << "Found Dataset:" << zfs_ds << out << ok;
  }
}

bool LFileInfo::goodZfsDataset(){
  if(!zfsAvailable()){ return false; }
  getZfsDataset(); //ensure this field is populated
  if(zfs_ds=="." || zfs_ds.isEmpty()){ return false; }
  return true;
}

//Functions for accessing the extra information
// -- Return the mimetype for the file
QString LFileInfo::mimetype(){
  if(mime=="inode/directory"){ return ""; }
  else{ return mime; }
}

// -- Return the icon to use for this file
QString LFileInfo::iconfile(){
  //Go through the icon list and find the first one that exists in the current theme
  for(int i=0; i<iconList.length(); i++){
    if( QIcon::hasThemeIcon(iconList[i])){ return iconList[i]; }
  }
  return ""; //Fall back to nothing
}

// -- Check if this is an XDG desktop file
bool LFileInfo::isDesktopFile(){
  if(desk==0){ return false; }
  return (!desk->filePath.isEmpty());
}

// -- Allow access to the XDG desktop data structure
XDGDesktop* LFileInfo::XDG(){
  return desk;
}

// -- Check if this is a readable video file (for thumbnail support)
bool LFileInfo::isVideo(){
  if(!mime.startsWith("video/")){ return false; }
  //Check the hardcoded list of known supported video formats to see if the thumbnail can be generated
  return ( !LUtils::videoExtensions().filter(this->suffix().toLower()).isEmpty() );
}

// -- Check if this is a readable image file
bool LFileInfo::isImage(){
  if(!mime.startsWith("image/")){ return false; } //quick return for non-image files
  //Check the Qt subsystems to see if this image file can be read
  return ( !LUtils::imageExtensions().filter(this->suffix().toLower()).isEmpty() );
}

bool LFileInfo::isAVFile(){
  return (mime.startsWith("audio/") || mime.startsWith("video/") );
}

bool LFileInfo::isZfsDataset(){
  if(!goodZfsDataset()){ return false; }
  return ( ("/"+zfs_ds.section("/",1,-1)) == this->canonicalFilePath());
}

QString LFileInfo::zfsPool(){
  if(!goodZfsDataset()){ return ""; }
  return zfs_ds.section("/",0,0);
}

QStringList LFileInfo::zfsSnapshots(){
  if(!goodZfsDataset()){ return QStringList(); }
  QString relpath = this->canonicalFilePath().remove(0, QString("/"+zfs_ds.section("/",1,-1)).length() );
  //qDebug() << "Got Relative path:" << zfs_ds << this->canonicalFilePath() << relpath;
  QDir dir("/"+zfs_ds.section("/",1,-1)+"/.zfs/snapshot/");
  QStringList snaps = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Time);
  for(int i=0; i<snaps.length(); i++){
    if( QFile::exists(dir.absoluteFilePath(snaps[i])+relpath) ){  snaps[i].append("::::" + dir.absoluteFilePath(snaps[i])+relpath ); }
    else{ snaps.removeAt(i); i--; }
  }
  return snaps;
}

QJsonObject LFileInfo::zfsProperties(){
  QJsonObject props;
  if(!goodZfsDataset()){ return props; }
  bool ok = false;
  QStringList out = LUtils::runCommand(ok, "zfs", QStringList() << "get" << "-H" << "all"  << zfs_ds).split("\n");
  //Note: Formating of zfs output: tab-delimited, with columns [dataset, property, value, source]
  for(int i=0; i<out.length() && ok; i++){
    if(out[i].simplified().isEmpty()){ continue; }
    QJsonObject prop;
    prop.insert("property", out[i].section("\t",1,1).simplified());
    prop.insert("value", out[i].section("\t",2,2).simplified());
    prop.insert("source", out[i].section("\t",3,-1).simplified());
    props.insert(prop.value("property").toString(), prop);
  }
  return props;
}

bool LFileInfo::zfsSetProperty(QString property, QString value){
  if(!goodZfsDataset()){ return false; }
  bool ok = false;
  QString info = LUtils::runCommand(ok, "zfs", QStringList() << "set" << property+"="+value << zfs_ds);
  if(!ok){ qDebug() << "Error Setting ZFS Property:" << property+"="+value << info; }
  return ok;
}
bgstack15