2022-02-11 06:29:50 +00:00
|
|
|
# Python imports
|
2022-02-20 06:35:29 +00:00
|
|
|
import hashlib, re
|
2022-02-11 06:29:50 +00:00
|
|
|
from os import listdir
|
|
|
|
from os.path import isdir, isfile, join
|
|
|
|
|
|
|
|
from random import randint
|
|
|
|
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
from .utils.settings import Settings
|
|
|
|
from .utils.launcher import Launcher
|
|
|
|
from .utils.filehandler import FileHandler
|
|
|
|
|
|
|
|
from .icons.icon import Icon
|
|
|
|
from .path import Path
|
|
|
|
|
|
|
|
|
2022-02-26 00:30:17 +00:00
|
|
|
class Tab(Settings, FileHandler, Launcher, Icon, Path):
|
2022-02-11 06:29:50 +00:00
|
|
|
def __init__(self):
|
2022-10-28 02:23:12 +00:00
|
|
|
self.logger = None
|
|
|
|
self._id_length: int = 10
|
|
|
|
|
|
|
|
self._id: str = ""
|
|
|
|
self._wid: str = None
|
|
|
|
self._dir_watcher = None
|
|
|
|
self._hide_hidden: bool = self.HIDE_HIDDEN_FILES
|
|
|
|
self._files: list = []
|
|
|
|
self._dirs: list = []
|
|
|
|
self._vids: list = []
|
|
|
|
self._images: list = []
|
|
|
|
self._desktop: list = []
|
|
|
|
self._ungrouped: list = []
|
|
|
|
self._hidden: list = []
|
2022-02-11 06:29:50 +00:00
|
|
|
|
|
|
|
self._generate_id()
|
|
|
|
self.set_to_home()
|
|
|
|
|
2022-10-28 02:23:12 +00:00
|
|
|
def load_directory(self) -> None:
|
2022-02-11 06:29:50 +00:00
|
|
|
path = self.get_path()
|
|
|
|
self._dirs = []
|
|
|
|
self._vids = []
|
|
|
|
self._images = []
|
|
|
|
self._desktop = []
|
|
|
|
self._ungrouped = []
|
|
|
|
self._hidden = []
|
|
|
|
self._files = []
|
|
|
|
|
|
|
|
if not isdir(path):
|
|
|
|
self.set_to_home()
|
|
|
|
return ""
|
|
|
|
|
|
|
|
for f in listdir(path):
|
|
|
|
file = join(path, f)
|
|
|
|
if self._hide_hidden:
|
|
|
|
if f.startswith('.'):
|
|
|
|
self._hidden.append(f)
|
|
|
|
continue
|
|
|
|
|
|
|
|
if isfile(file):
|
|
|
|
lowerName = file.lower()
|
|
|
|
if lowerName.endswith(self.fvideos):
|
|
|
|
self._vids.append(f)
|
|
|
|
elif lowerName.endswith(self.fimages):
|
|
|
|
self._images.append(f)
|
|
|
|
elif lowerName.endswith((".desktop",)):
|
|
|
|
self._desktop.append(f)
|
|
|
|
else:
|
|
|
|
self._ungrouped.append(f)
|
|
|
|
else:
|
|
|
|
self._dirs.append(f)
|
|
|
|
|
|
|
|
self._dirs.sort(key=self._natural_keys)
|
|
|
|
self._vids.sort(key=self._natural_keys)
|
|
|
|
self._images.sort(key=self._natural_keys)
|
|
|
|
self._desktop.sort(key=self._natural_keys)
|
|
|
|
self._ungrouped.sort(key=self._natural_keys)
|
|
|
|
|
|
|
|
self._files = self._dirs + self._vids + self._images + self._desktop + self._ungrouped
|
|
|
|
|
|
|
|
def is_folder_locked(self, hash):
|
|
|
|
if self.lock_folder:
|
|
|
|
path_parts = self.get_path().split('/')
|
|
|
|
file = self.get_path_part_from_hash(hash)
|
|
|
|
|
|
|
|
# Insure chilren folders are locked too.
|
|
|
|
lockedFolderInPath = False
|
|
|
|
for folder in self.locked_folders:
|
|
|
|
if folder in path_parts:
|
|
|
|
lockedFolderInPath = True
|
|
|
|
break
|
|
|
|
|
|
|
|
return (file in self.locked_folders or lockedFolderInPath)
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-10-28 02:23:12 +00:00
|
|
|
def get_not_hidden_count(self) -> int:
|
2022-02-11 06:29:50 +00:00
|
|
|
return len(self._files) + \
|
|
|
|
len(self._dirs) + \
|
|
|
|
len(self._vids) + \
|
|
|
|
len(self._images) + \
|
|
|
|
len(self._desktop) + \
|
|
|
|
len(self._ungrouped)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_hidden_count(self) -> int:
|
2022-02-11 06:29:50 +00:00
|
|
|
return len(self._hidden)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_files_count(self) -> int:
|
2022-02-11 06:29:50 +00:00
|
|
|
return len(self._files)
|
|
|
|
|
2022-10-28 02:23:12 +00:00
|
|
|
def get_path_part_from_hash(self, hash: str) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
files = self.get_files()
|
|
|
|
file = None
|
|
|
|
|
|
|
|
for f in files:
|
|
|
|
if hash == f[1]:
|
|
|
|
file = f[0]
|
|
|
|
break
|
|
|
|
|
|
|
|
return file
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_files_formatted(self) -> dict:
|
2022-02-11 06:29:50 +00:00
|
|
|
files = self._hash_set(self._files),
|
|
|
|
dirs = self._hash_set(self._dirs),
|
|
|
|
videos = self.get_videos(),
|
|
|
|
images = self._hash_set(self._images),
|
|
|
|
desktops = self._hash_set(self._desktop),
|
|
|
|
ungrouped = self._hash_set(self._ungrouped)
|
|
|
|
hidden = self._hash_set(self._hidden)
|
|
|
|
|
|
|
|
return {
|
|
|
|
'path_head': self.get_path(),
|
|
|
|
'list': {
|
|
|
|
'files': files,
|
|
|
|
'dirs': dirs,
|
|
|
|
'videos': videos,
|
|
|
|
'images': images,
|
|
|
|
'desktops': desktops,
|
|
|
|
'ungrouped': ungrouped,
|
|
|
|
'hidden': hidden
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def get_pixbuf_icon_str_combo(self):
|
|
|
|
data = []
|
|
|
|
dir = self.get_current_directory()
|
|
|
|
for file in self._files:
|
|
|
|
icon = self.create_icon(dir, file).get_pixbuf()
|
|
|
|
data.append([icon, file])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
2022-10-28 02:23:12 +00:00
|
|
|
def get_gtk_icon_str_combo(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
data = []
|
|
|
|
dir = self.get_current_directory()
|
|
|
|
for file in self._files:
|
|
|
|
icon = self.create_icon(dir, file)
|
|
|
|
data.append([icon, file[0]])
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2022-10-28 02:23:12 +00:00
|
|
|
def get_current_directory(self) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self.get_path()
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_current_sub_path(self) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
path = self.get_path()
|
|
|
|
home = f"{self.get_home()}/"
|
|
|
|
return path.replace(home, "")
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_end_of_path(self) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
parts = self.get_current_directory().split("/")
|
|
|
|
size = len(parts)
|
|
|
|
return parts[size - 1]
|
|
|
|
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def set_hiding_hidden(self, state: bool) -> None:
|
2022-02-11 06:29:50 +00:00
|
|
|
self._hide_hidden = state
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def is_hiding_hidden(self) -> bool:
|
2022-02-11 06:56:30 +00:00
|
|
|
return self._hide_hidden
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_dot_dots(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(['.', '..'])
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_files(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(self._files)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_dirs(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(self._dirs)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_videos(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(self._vids)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_images(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(self._images)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_desktops(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(self._desktop)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_ungrouped(self) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._hash_set(self._ungrouped)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_hidden(self) -> list:
|
2022-02-11 06:56:30 +00:00
|
|
|
return self._hash_set(self._hidden)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_id(self) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._id
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def set_wid(self, _wid: str) -> None:
|
2022-02-11 06:29:50 +00:00
|
|
|
self._wid = _wid
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def get_wid(self) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
return self._wid
|
|
|
|
|
|
|
|
def set_dir_watcher(self, watcher):
|
|
|
|
self._dir_watcher = watcher
|
|
|
|
|
|
|
|
def get_dir_watcher(self):
|
|
|
|
return self._dir_watcher
|
|
|
|
|
|
|
|
def _atoi(self, text):
|
|
|
|
return int(text) if text.isdigit() else text
|
|
|
|
|
|
|
|
def _natural_keys(self, text):
|
|
|
|
return [ self._atoi(c) for c in re.split('(\d+)',text) ]
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def _hash_text(self, text) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
return hashlib.sha256(str.encode(text)).hexdigest()[:18]
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def _hash_set(self, arry: list) -> list:
|
2022-02-11 06:29:50 +00:00
|
|
|
data = []
|
|
|
|
for arr in arry:
|
|
|
|
data.append([arr, self._hash_text(arr)])
|
|
|
|
return data
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def _random_with_N_digits(self, n: int) -> int:
|
2022-02-11 06:29:50 +00:00
|
|
|
range_start = 10**(n-1)
|
|
|
|
range_end = (10**n)-1
|
|
|
|
return randint(range_start, range_end)
|
|
|
|
|
2022-10-28 01:41:27 +00:00
|
|
|
def _generate_id(self) -> str:
|
2022-02-11 06:29:50 +00:00
|
|
|
self._id = str(self._random_with_N_digits(self._id_length))
|