ShellFM/src/shellfm/windows/view/View.py

197 lines
5.0 KiB
Python
Raw Normal View History

2021-03-18 05:29:58 +00:00
# Python imports
import hashlib
import os
from os import listdir
from os.path import isdir, isfile, join
2021-04-24 11:39:09 +00:00
from random import randint
2021-03-18 05:29:58 +00:00
# Lib imports
# Application imports
2021-04-22 06:32:18 +00:00
from .utils import Settings, Launcher
2021-04-25 02:03:05 +00:00
from .icons import Icon
2021-04-25 02:17:29 +00:00
from . import Path
2021-03-18 05:29:58 +00:00
2021-04-24 11:39:09 +00:00
class View(Settings, Launcher, Icon, Path):
2021-03-18 05:29:58 +00:00
def __init__(self):
2021-04-24 21:43:45 +00:00
self. logger = None
self.id_length = 10
self.id = ""
2021-03-18 05:29:58 +00:00
self.files = []
self.dirs = []
self.vids = []
self.images = []
self.desktop = []
self.ungrouped = []
2021-04-24 11:39:09 +00:00
self.generate_id()
self.set_to_home()
2021-04-24 11:39:09 +00:00
def random_with_N_digits(self, n):
range_start = 10**(n-1)
range_end = (10**n)-1
return randint(range_start, range_end)
def generate_id(self):
self.id = str(self.random_with_N_digits(self.id_length))
2021-03-18 05:29:58 +00:00
2021-04-28 04:30:58 +00:00
def get_tab_id(self):
return self.id
2021-03-18 05:29:58 +00:00
def load_directory(self):
path = self.get_path()
2021-04-26 03:34:12 +00:00
self.dirs = [".", ".."]
2021-03-18 05:29:58 +00:00
self.vids = []
self.images = []
self.desktop = []
self.ungrouped = []
self.files = []
if not isdir(path):
self.set_to_home()
return ""
for f in listdir(path):
file = join(path, f)
if self.HIDE_HIDDEN_FILES:
if f.startswith('.'):
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()
self.vids.sort()
self.images.sort()
self.desktop.sort()
self.ungrouped.sort()
self.files = self.dirs + self.vids + self.images + self.desktop + self.ungrouped
2021-04-25 02:17:29 +00:00
def hash_text(self, text):
2021-03-18 05:29:58 +00:00
return hashlib.sha256(str.encode(text)).hexdigest()[:18]
2021-04-25 02:17:29 +00:00
def hash_set(self, arry):
2021-03-18 05:29:58 +00:00
data = []
for arr in arry:
2021-04-25 02:17:29 +00:00
data.append([arr, self.hash_text(arr)])
2021-03-18 05:29:58 +00:00
return data
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
2021-03-18 05:29:58 +00:00
def get_path_part_from_hash(self, hash):
files = self.get_files()
file = None
for f in files:
if hash == f[1]:
file = f[0]
break
return file
def get_files_formatted(self):
2021-04-25 02:17:29 +00:00
files = self.hash_set(self.files),
dirs = self.hash_set(self.dirs),
2021-03-18 05:29:58 +00:00
videos = self.get_videos(),
2021-04-25 02:17:29 +00:00
images = self.hash_set(self.images),
desktops = self.hash_set(self.desktop),
ungrouped = self.hash_set(self.ungrouped)
2021-03-18 05:29:58 +00:00
return {
'path_head': self.get_path(),
'list': {
'files': files,
'dirs': dirs,
'videos': videos,
'images': images,
'desktops': desktops,
'ungrouped': ungrouped
}
}
2021-04-24 22:05:58 +00:00
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()
2021-04-25 02:17:29 +00:00
data.append([icon, file])
2021-04-24 22:05:58 +00:00
return data
def get_gtk_icon_str_combo(self):
data = []
dir = self.get_current_directory()
for file in self.files:
icon = self.create_icon(dir, file)
data.append([icon, file[0]])
return data
2021-03-18 05:29:58 +00:00
def get_current_directory(self):
return self.get_path()
def get_current_sub_path(self):
path = self.get_path()
home = self.get_home() + "/"
return path.replace(home, "")
2021-04-24 11:39:09 +00:00
def get_end_of_path(self):
parts = self.get_current_directory().split("/")
size = len(parts)
return parts[size - 1]
2021-03-18 05:29:58 +00:00
def get_dot_dots(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(['.', '..'])
2021-03-18 05:29:58 +00:00
def get_files(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(self.files)
2021-03-18 05:29:58 +00:00
def get_dirs(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(self.dirs)
2021-03-18 05:29:58 +00:00
def get_videos(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(self.vids)
2021-03-18 05:29:58 +00:00
def get_images(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(self.images)
2021-03-18 05:29:58 +00:00
def get_desktops(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(self.desktop)
2021-03-18 05:29:58 +00:00
def get_ungrouped(self):
2021-04-25 02:17:29 +00:00
return self.hash_set(self.ungrouped)