Files
pip/utils/system_daemon/file_manager.cpp

183 lines
5.5 KiB
C++

#include "file_manager.h"
#include "shared.h"
FileManager::FileManager() {
setName("FileManager");
offset = cur = height = 0;
enabled = del_commit = false;
CONNECTU(&console, keyPressed, this, keyEvent)
dir = PIDir::current();
//dir.setDir("/home/peri4/Documents");
dir.setDir("/home/peri4");
updateDir();
}
void FileManager::keyEvent(char key) {
if (!enabled) return;
if (key == 'D') {
if (cur >= files.size_s() || cur < 0) return;
if (del_commit) {
piForeachC (PIString & f, selected) {
PIFile::remove(dir.absolutePath() + PIDir::separator + f);
//piCout << "remove" << (dir.absolutePath() + PIDir::separator + f);
}
selected.clear();
updateDir();
console.clearCustomStatus();
del_commit = false;
} else {
if (selected.isEmpty()) selected << files[cur].path;
console.addCustomStatus("Delete " + PIString(selected.size_s()) + " file, are you sure? D as yes");
del_commit = true;
}
updateConsole();
return;
}
console.clearCustomStatus();
del_commit = false;
PIStringList nsel;
switch (key) {
case PIKbdListener::UpArrow:
cur--;
if (cur < 0) cur = 0;
if (cur - offset < 3) offset--;
if (offset < 0) offset = 0;
updateConsole();
break;
case PIKbdListener::Space:
if (cur < 0 || cur >= files.size_s()) return;
if (selected.contains(files[cur].path)) selected.removeOne(files[cur].path);
else selected << files[cur].path;
case PIKbdListener::DownArrow:
cur++;
if (cur >= files.size_s()) cur = files.size_s() - 1;
if (cur - offset >= height - 3) offset++;
if (offset >= files.size_s() - height) offset = files.size_s() - height;
updateConsole();
//piCout << offset << files.size_s() << height;
break;
case PIKbdListener::Home:
cur = offset = 0;
updateConsole();
break;
case PIKbdListener::End:
cur = files.size_s() - 1;
offset = files.size_s() - height;
updateConsole();
//piCout << offset << files.size_s() << height;
break;
case PIKbdListener::Return:
if (cur < files.size_s() && cur >= 0) {
piCout << files[cur];
if (files[cur].isDir()) {
prev_pos[dir.path()] = cur;
prev_off[dir.path()] = offset;
dir.cd(files[cur].name());
cur = prev_pos.value(dir.path(), 0);
offset = prev_off.value(dir.path(), 0);
selected.clear();
updateDir();
updateConsole();
}
}
break;
case 'A':
selected.clear();
piForeach (PIFile::FileInfo & e, files)
selected << e.path;
updateConsole();
break;
case 'R':
updateDir();
piForeach (PIFile::FileInfo & e, files)
if (selected.contains(e.path))
nsel << e.path;
selected = nsel;
updateConsole();
break;
case PIKbdListener::Esc:
//selected.clear();
//updateConsole();
menuRequest();
break;
default: break;
}
}
void FileManager::updateConsole() {
if (!enabled) return;
startTab(2);
console.addString("File manager", 1, PIConsole::Yellow | PIConsole::Inverse);
console.addString("Path: " + dir.absolutePath(), 1, PIConsole::Green | PIConsole::Inverse | PIConsole::Bold);
console.addString("Name", 1, PIConsole::Green | PIConsole::Inverse | PIConsole::Bold);
console.addString(" ", 2, PIConsole::Yellow | PIConsole::Inverse);
console.addString(" ", 2, PIConsole::Green | PIConsole::Inverse);
console.addString(" ", 2, PIConsole::Green | PIConsole::Inverse);
buildNames();
console.addString("A - select all, D - remove, R - refresh, Esc - exit", 1, PIConsole::Green | PIConsole::Inverse | PIConsole::Bold);
finishTab();
}
void FileManager::buildNames() {
if (!enabled) return;
height = console.windowHeight() - 10;
int is = piClampi(offset, 0, piMaxi(0, files.size_s() - 1)), ie = piClampi(offset + height, 0, files.size_s());
console.addString((is > 0) ? (PIString(" /\\ ").repeat(console.windowWidth() / 8)) : " ", 1, PIConsole::Green | PIConsole::Bold);
console.addString(" ", 2);
PIChar t;
PIConsole::FormatFlags f = 0;
PIString scol;
piCout << cur;
for (int i = is; i < ie; ++i) {
if (files[i].isDir()) {
t = '/';
f = PIConsole::Bold;
scol = " dir";
} else {
if (files[i].perm_user.exec || files[i].perm_group.exec || files[i].perm_other.exec) {
f = PIConsole::Green | PIConsole::Bold;
t = '*';
} else {
t = ' ';
f = 0;
}
scol = PIString::readableSize(files[i].size);
}
if (files[i].isSymbolicLink() && (t != '*')) t = '~';
scol = scol.expandRightTo(9, ' ') + "| " + files[i].time_modification.toString("dd.MM hh:mm:ss") + " | "
+ files[i].perm_user.toString() + " " + files[i].perm_group.toString() + " " + files[i].perm_other.toString();
if (i == cur) f |= PIConsole::BackBlue;
if (selected.contains(files[i].path)) f |= PIConsole::Yellow | PIConsole::Bold;
console.addString(t + files[i].name(), 1, f);
console.addString(scol, 2, f);
}
console.addString((ie < files.size_s()) ? (PIString(" \\/ ").repeat(console.windowWidth() / 8)) : " ", 1, PIConsole::Green | PIConsole::Bold);
console.addString(" ", 2);
}
void FileManager::updateDir() {
if (!enabled) return;
files.clear();
PIVector<PIFile::FileInfo> el = dir.entries(), fl, dl;
for (int i = 0; i < el.size_s(); ++i) {
if (el[i].path == ".") continue;
if (el[i].path == "..") {
dl.push_front(el[i]);
continue;
}
if (el[i].isDir()) dl << el[i];
else fl << el[i];
}
files << dl << fl;
if (cur >= files.size_s()) cur = files.size_s() - 1;
if (offset >= files.size_s() - height) offset = files.size_s() - height;
if (cur < 0) cur = 0;
if (offset < 0) offset = 0;
}