ShellFM/src/shellfm/windows/WindowController.py

166 lines
4.6 KiB
Python
Raw Normal View History

2021-04-26 03:34:12 +00:00
# Python imports
import json
from os import path
# Lib imports
# Application imports
2021-03-18 05:29:58 +00:00
from . import Window
class WindowController:
def __init__(self):
USER_HOME = path.expanduser('~')
CONFIG_PATH = USER_HOME + "/.config/pyfm"
self.config_file = CONFIG_PATH + "/session.json"
2021-04-26 03:34:12 +00:00
self.active_window_id = ""
2021-04-28 03:46:56 +00:00
self.active_tab_id = ""
self.windows = []
2021-04-26 03:34:12 +00:00
2021-03-18 05:29:58 +00:00
2021-04-28 03:46:56 +00:00
def set_active_data(self, wid, tid):
self.active_window_id = str(wid)
self.active_tab_id = str(tid)
def get_active_data(self):
return self.active_window_id, self.active_tab_id
2021-03-18 05:29:58 +00:00
def create_window(self):
window = Window()
window.name = "window_" + window.id
window.nickname = "window_" + str(len(self.windows) + 1)
2021-03-18 05:29:58 +00:00
self.windows.append(window)
return window
2021-03-18 05:29:58 +00:00
def add_view_for_window(self, win_id):
for window in self.windows:
if window.id == win_id:
2021-04-24 11:39:09 +00:00
return window.create_view()
2021-03-18 05:29:58 +00:00
def add_view_for_window_by_name(self, name):
for window in self.windows:
if window.name == name:
return window.create_view()
def add_view_for_window_by_nickname(self, nickname):
for window in self.windows:
if window.nickname == nickname:
return window.create_view()
2021-03-18 05:29:58 +00:00
def pop_window(self):
self.windows.pop()
def delete_window_by_id(self, win_id):
for window in self.windows:
if window.id == win_id:
self.windows.remove(window)
2021-03-18 05:29:58 +00:00
break
def delete_window_by_name(self, name):
for window in self.windows:
if window.name == name:
self.windows.remove(window)
break
def delete_window_by_nickname(self, nickname):
for window in self.windows:
if window.nickname == nickname:
self.windows.remove(window)
break
def get_window_by_id(self, win_id):
for window in self.windows:
if window.id == win_id:
return window
raise(f"No Window by ID {win_id} found!")
def get_window_by_name(self, name):
for window in self.windows:
if window.name == name:
return window
raise(f"No Window by Name {name} found!")
def get_window_by_nickname(self, nickname):
for window in self.windows:
if window.nickname == nickname:
return window
raise(f"No Window by Nickname {nickname} found!")
def get_window_by_index(self, index):
return self.windows[index]
def get_all_windows(self):
return self.windows
2021-03-18 05:29:58 +00:00
def set_window_nickname(self, win_id = None, nickname = ""):
for window in self.windows:
if window.id == win_id:
window.nickname = nickname
def list_windows(self):
2021-04-28 04:30:58 +00:00
print("\n[ ---- Windows ---- ]\n")
2021-03-18 05:29:58 +00:00
for window in self.windows:
2021-04-28 04:30:58 +00:00
print(f"\nID: {window.id}")
print(f"Name: {window.name}")
print(f"Nickname: {window.nickname}")
print(f"View Count: {window.get_views_count()}")
2021-04-28 04:30:58 +00:00
print("\n-------------------------\n")
2021-03-18 05:29:58 +00:00
def list_files_from_views_of_window(self, win_id):
2021-03-18 05:29:58 +00:00
for window in self.windows:
if window.id == win_id:
window.list_files_from_views()
2021-03-18 05:29:58 +00:00
break
def get_views_count(self, win_id):
for window in self.windows:
if window.id == win_id:
return window.get_views_count()
def get_views_from_window(self, win_id):
2021-03-18 05:29:58 +00:00
for window in self.windows:
if window.id == win_id:
return window.get_all_views()
2021-04-26 03:34:12 +00:00
def save_state(self):
windows = []
for window in self.windows:
views = []
for view in window.views:
views.append(view.get_current_directory())
windows.append(
[
{
'window':{
"ID": window.id,
2021-04-26 03:34:12 +00:00
"Name": window.name,
"Nickname": window.nickname,
'views': views
}
}
]
)
with open(self.config_file, 'w') as outfile:
json.dump(windows, outfile, separators=(',', ':'), indent=4)
def load_state(self):
if path.isfile(self.config_file):
with open(self.config_file) as infile:
return json.load(infile)