forked from wasdwasd0105/7zip-pyside
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_bar.py
157 lines (117 loc) · 5.89 KB
/
menu_bar.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import os
import sys
from PySide6.QtWidgets import QMenuBar, QMenu, QFileDialog, QMessageBox
from PySide6.QtGui import QAction, QDesktopServices
from qsetting_manager import SettingsManager
from SevenZHelperMacOS import create_bookmark, resolve_bookmark, start_accessing_resource, stop_accessing_resource
from SevenZUtils import AboutDialog
class MenuBar(QMenuBar):
MAX_HISTORY_COUNT = 5
def __init__(self, window):
super().__init__()
self.current_sandbox_bookmark = None
self.window = window
self.settings_manager = SettingsManager()
self.workspace_history = self.settings_manager.get_value("workspace_history", [])
self.create_menu()
def create_menu(self):
self.clear()
# Workspace Menu
workspace_menu = QMenu("Workspace", self)
close_workspace = QAction("Close Workspace", self.window)
workspace_menu.addAction(close_workspace)
close_workspace.triggered.connect(lambda: self.close_workspace_action())
workspace_menu.addSeparator()
for entry in self.workspace_history:
if self.is_sandboxed() and isinstance(entry, tuple) and len(entry) == 2:
display_path, bookmark = entry
real_path = resolve_bookmark(bookmark)
else:
bookmark = None
display_path = real_path = entry # In non-sandboxed environment, or if entry isn't tuple.
action = QAction(display_path, self.window)
action.triggered.connect(
lambda d_path=display_path, r_path=real_path, b_path=bookmark: self.open_workspace(d_path, r_path,
b_path))
workspace_menu.addAction(action)
workspace_menu.addSeparator()
clean_all_workspace = QAction("Clean All Workspace History", self.window)
workspace_menu.addAction(clean_all_workspace)
clean_all_workspace.triggered.connect(lambda: self.clean_all_workspace_action())
self.addMenu(workspace_menu)
# File Menu
file_menu = QMenu("Archive", self)
open_archive_action = QAction("Open Archive", self.window)
file_menu.addAction(open_archive_action)
open_archive_action.triggered.connect(lambda: self.open_archive())
close_archive_action = QAction("Close Archive", self.window)
file_menu.addAction(close_archive_action)
close_archive_action.triggered.connect(lambda: self.close_archive())
self.addMenu(file_menu)
settings_menu = QMenu("Settings", self)
if self.get_chardet_option():
chardet_option_text = "✔️ Use non-Unicode Encoder"
else:
chardet_option_text = "Use non-Unicode Encoder"
use_chardet_option = QAction(chardet_option_text, self.window)
settings_menu.addAction(use_chardet_option)
use_chardet_option.triggered.connect(lambda: self.toggle_chardet())
reset_option = QAction("Reset to default", self.window)
settings_menu.addAction(reset_option)
reset_option.triggered.connect(lambda: self.toggle_reset())
about_option = QAction("About", self.window)
settings_menu.addAction(about_option)
about_option.triggered.connect(lambda: self.toggle_about_page())
self.addMenu(settings_menu)
def open_archive(self):
file_name, _ = QFileDialog.getOpenFileName(self.window, "Open Archive File", "",
"All Files (*)")
if file_name:
self.window.main_pane.display_archive_contents(file_name)
def close_archive(self):
self.window.main_pane.close_and_clear()
def clean_all_workspace_action(self):
self.settings_manager.set_value("workspace_history", [])
self.update_menu_bar()
def close_workspace_action(self):
self.close_sandbox_resource()
self.window.nav_pane.clean_navigation_pane()
def open_workspace(self, display_path, real_path, bookmark):
if self.is_sandboxed() and bookmark:
res = start_accessing_resource(bookmark)
if res is not True:
QMessageBox.warning(self, "Error", "Failed to open the history. Please reopen the folder")
self.clean_all_workspace_action()
return
self.current_sandbox_bookmark = bookmark
self.window.nav_pane.clean_navigation_pane()
self.window.nav_pane.open_workspace_folder(display_path, real_path)
# print(real_path)
def update_menu_bar(self):
self.workspace_history = self.settings_manager.get_value("workspace_history", [])
self.create_menu()
def is_sandboxed(self):
return sys.platform == "darwin" and os.environ.get("APP_SANDBOX_CONTAINER_ID") is not None
def close_sandbox_resource(self):
if self.is_sandboxed and self.current_sandbox_bookmark is not None:
stop_accessing_resource(self.current_sandbox_bookmark)
self.current_sandbox_bookmark = None
def get_chardet_option(self):
return self.settings_manager.get_value("chardet_option", False)
def set_chardet_option(self, option):
self.settings_manager.set_value("chardet_option", option)
def toggle_chardet(self):
option = self.get_chardet_option()
new_option = not option
self.set_chardet_option(new_option)
self.update_menu_bar()
self.window.main_pane.reload_archive()
def toggle_reset(self):
reply = QMessageBox.question(None, 'Reset to Default', 'Do you want to reset to default?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
if reply == QMessageBox.StandardButton.Yes:
self.settings_manager.reset()
sys.exit()
def toggle_about_page(self):
about_dialog = AboutDialog()
about_dialog.exec_()