Allow closing tabs, and throw warning when trying to close tabs that contain an active server

This commit is contained in:
Micah Lee 2019-10-27 15:01:14 -07:00
parent fd2046b976
commit df658a0e70
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
3 changed files with 70 additions and 42 deletions

View File

@ -50,7 +50,7 @@ class Tab(QtWidgets.QWidget):
self.status_bar = status_bar self.status_bar = status_bar
self.filenames = filenames self.filenames = filenames
self.mode = self.common.gui.MODE_SHARE self.mode = None
# Start the OnionShare app # Start the OnionShare app
self.app = OnionShare(common, self.common.gui.onion, self.common.gui.local_only) self.app = OnionShare(common, self.common.gui.onion, self.common.gui.local_only)
@ -511,44 +511,57 @@ class Tab(QtWidgets.QWidget):
""" """
self.status_bar.clearMessage() self.status_bar.clearMessage()
def close_event(self, e): def close_tab(self):
self.common.log("Tab", "close_event") self.common.log("Tab", "close_tab")
try: if self.mode is None:
if self.mode == self.common.gui.MODE_WEBSITE: return True
server_status = self.share_mode.server_status
if self.mode == self.common.gui.MODE_WEBSITE: if self.mode == self.common.gui.MODE_SHARE:
server_status = self.website_mode.server_status server_status = self.share_mode.server_status
elif self.mode == self.common.gui.MODE_RECEIVE:
server_status = self.receive_mode.server_status
else:
server_status = self.website_mode.server_status
if server_status.status == server_status.STATUS_STOPPED:
return True
else:
self.common.log("Tab", "close_tab, opening warning dialog")
dialog = QtWidgets.QMessageBox()
dialog.setWindowTitle(strings._("gui_close_tab_warning_title"))
if self.mode == self.common.gui.MODE_SHARE:
dialog.setText(strings._("gui_close_tab_warning_share_description"))
elif self.mode == self.common.gui.MODE_RECEIVE:
dialog.setText(strings._("gui_close_tab_warning_receive_description"))
else: else:
server_status = self.receive_mode.server_status dialog.setText(strings._("gui_close_tab_warning_website_description"))
if server_status.status != server_status.STATUS_STOPPED: dialog.setIcon(QtWidgets.QMessageBox.Critical)
self.common.log("MainWindow", "closeEvent, opening warning dialog") dialog.addButton(
dialog = QtWidgets.QMessageBox() strings._("gui_close_tab_warning_close"), QtWidgets.QMessageBox.YesRole
dialog.setWindowTitle(strings._("gui_quit_title")) )
if self.mode == self.common.gui.MODE_WEBSITE: cancel_button = dialog.addButton(
dialog.setText(strings._("gui_share_quit_warning")) strings._("gui_close_tab_warning_cancel"), QtWidgets.QMessageBox.NoRole
else: )
dialog.setText(strings._("gui_receive_quit_warning")) dialog.setDefaultButton(cancel_button)
dialog.setIcon(QtWidgets.QMessageBox.Critical) reply = dialog.exec_()
quit_button = dialog.addButton(
strings._("gui_quit_warning_quit"), QtWidgets.QMessageBox.YesRole
)
dont_quit_button = dialog.addButton(
strings._("gui_quit_warning_dont_quit"),
QtWidgets.QMessageBox.NoRole,
)
dialog.setDefaultButton(dont_quit_button)
reply = dialog.exec_()
# Quit # Close
if reply == 0: if reply == 0:
self.stop_server() self.common.log("Tab", "close_tab", "close, closing tab")
e.accept()
# Don't Quit
else:
e.ignore()
except: if self.mode == self.common.gui.MODE_SHARE:
e.accept() self.share_mode.stop_server()
elif self.mode == self.common.gui.MODE_RECEIVE:
self.receive_mode.stop_server()
else:
self.website_mode.stop_server()
self.app.cleanup()
return True
# Cancel
else:
self.common.log("Tab", "close_tab", "cancel, keeping tab open")
return False
def cleanup(self): def cleanup(self):
self.app.cleanup() self.app.cleanup()

View File

@ -60,6 +60,8 @@ class TabWidget(QtWidgets.QTabWidget):
self.setTabsClosable(True) self.setTabsClosable(True)
self.setUsesScrollButtons(True) self.setUsesScrollButtons(True)
self.tabCloseRequested.connect(self.close_tab)
self.move_new_tab_button() self.move_new_tab_button()
def move_new_tab_button(self): def move_new_tab_button(self):
@ -81,11 +83,6 @@ class TabWidget(QtWidgets.QTabWidget):
self.new_tab_button.move(pos) self.new_tab_button.move(pos)
self.new_tab_button.raise_() self.new_tab_button.raise_()
def resizeEvent(self, event):
# Make sure to move new tab button on each resize
super(TabWidget, self).resizeEvent(event)
self.move_new_tab_button()
def new_tab_clicked(self): def new_tab_clicked(self):
# Create the tab # Create the tab
tab = Tab(self.common, self.tab_id, self.system_tray, self.status_bar) tab = Tab(self.common, self.tab_id, self.system_tray, self.status_bar)
@ -101,6 +98,18 @@ class TabWidget(QtWidgets.QTabWidget):
index = self.indexOf(self.tabs[tab_id]) index = self.indexOf(self.tabs[tab_id])
self.setTabText(index, title) self.setTabText(index, title)
def close_tab(self, index):
self.common.log("TabWidget", "close_tab", f"{index}")
tab = self.widget(index)
if tab.close_tab():
self.removeTab(index)
del self.tabs[tab.tab_id]
def resizeEvent(self, event):
# Make sure to move new tab button on each resize
super(TabWidget, self).resizeEvent(event)
self.move_new_tab_button()
class TabBar(QtWidgets.QTabBar): class TabBar(QtWidgets.QTabBar):
""" """

View File

@ -187,5 +187,11 @@
"gui_new_tab_receive_button": "Receive Files", "gui_new_tab_receive_button": "Receive Files",
"gui_new_tab_receive_description": "Turn your computer into an online dropbox. People will be able to use Tor Browser to send files to your computer.", "gui_new_tab_receive_description": "Turn your computer into an online dropbox. People will be able to use Tor Browser to send files to your computer.",
"gui_new_tab_website_button": "Publish Website", "gui_new_tab_website_button": "Publish Website",
"gui_new_tab_website_description": "Host a static HTML onion website from your computer." "gui_new_tab_website_description": "Host a static HTML onion website from your computer.",
"gui_close_tab_warning_title": "Are you sure?",
"gui_close_tab_warning_share_description": "You're in the process of sending files. Are you sure you want to close this tab?",
"gui_close_tab_warning_receive_description": "You're in the process of receiving files. Are you sure you want to close this tab?",
"gui_close_tab_warning_website_description": "You're actively hosting a website. Are you sure you want to close this tab?",
"gui_close_tab_warning_close": "Close",
"gui_close_tab_warning_cancel": "Cancel"
} }