aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/desktop-utils/lumina-fm/Browser.cpp
blob: b207604cc4ed5cef181749a91720e4a8a6d8ce87 (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
//===========================================
//  Lumina-DE source code
//  Copyright (c) 2016, Ken Moore
//  Available under the 3-clause BSD license
//  See the LICENSE file for full details
//===========================================
#include "Browser.h"

Browser::Browser(QObject *parent) : QObject(parent){
  watcher = new QFileSystemWatcher(this);
  connect(watcher, SIGNAL(fileChanged(const QString&)), this, SLOT(fileChanged(QString)) );
  connect(watcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(dirChanged(QString)) );
  showHidden = false;
}

Browser::~Browser(){
  watcher->deleteLater();
}

QString Browser::currentDirectory(){ return currentDir; }

void Browser::showHiddenFiles(bool show){
  if(show !=showHidden){
    showHidden = show;
    QTimer::singleShot(0, this, SLOT(loadDirectory()) );
  }
}
bool Browser::showingHiddenFiles(){
  return showHidden;
}

//   PRIVATE
void Browser::loadItem(QFileInfo info){
  LFileInfo linfo(info);
  QIcon ico;
  if(linfo.isImage()){
    QPixmap pix;
    if(pix.load(info.absoluteFilePath()) ){
      if(pix.height()>128){ pix = pix.scaled(128, 128, Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation); }
      ico.addPixmap(pix);
    }
  }else if(linfo.isDirectory()){
    ico = LXDG::findIcon("folder","inode/directory");
  }
  if(ico.isNull()){ ico = LXDG::findIcon(linfo.mimetype(), "unknown"); }
  emit ItemDataAvailable(ico, linfo);
}

// PRIVATE SLOTS
void Browser::fileChanged(QString file){
  if(file.startsWith(currentDir+"/")){ emit itemUpdated(file); }
  else if(file==currentDir){ QTimer::singleShot(0, this, SLOT(loadDirectory()) ); }
}

void Browser::dirChanged(QString dir){
  if(dir==currentDir){ QTimer::singleShot(0, this, SLOT(loadDirectory()) ); }
  else if(dir.startsWith(currentDir)){ emit itemUpdated(dir); }
}

// PUBLIC SLOTS
QString Browser::loadDirectory(QString dir){
  if(dir.isEmpty()){ dir = currentDir; } //reload current directory
  if(dir.isEmpty()){ return; } //nothing to do - nothing previously loaded
  //clean up the watcher first
  QStringList watched; watched << watcher->files() << watcher->directories();
  if(!watched.isEmpty()){ watcher->removePaths(watched); }
  emit clearItems(); //let the main widget know to clear all current items
  //QApplication::processEvents();
  // read the given directory
  QDir directory(dir);
  if(directory.exists()){
    QFileInfoList files;
    if(showHidden){ files = directory.entryInfoList( QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotOrDotDot, QDir::NoSort); }
    else{ files = directory.entryInfoList( QDir::Dirs | QDir::Files | QDir::NoDotOrDotDot, QDir::NoSort); }
    for(int i=0; i<files.length(); i++){
      watcher->addPath(files[i].absoluteFilePath());
      QtConcurrent::run(this, &Browser::loadDirectory, files[i]);
    }
    watcher->addPath(directory.absolutePath());
  }
}
bgstack15