2019-10-27 17:21:40 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
|
|
OnionShare | https://onionshare.org/
|
|
|
|
|
|
|
|
Copyright (C) 2014-2018 Micah Lee <micah@micahflee.com>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
from PyQt5 import QtCore, QtWidgets, QtGui
|
2019-11-28 15:24:26 -05:00
|
|
|
from watchdog.observers import Observer
|
2019-10-27 17:21:40 -04:00
|
|
|
|
|
|
|
from onionshare import strings
|
2019-11-03 00:45:55 -04:00
|
|
|
from onionshare.mode_settings import ModeSettings
|
2019-10-27 17:21:40 -04:00
|
|
|
|
|
|
|
from .tab import Tab
|
2019-11-28 15:24:26 -05:00
|
|
|
from .event_handler import EventHandler
|
2019-10-27 17:21:40 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TabWidget(QtWidgets.QTabWidget):
|
|
|
|
"""
|
|
|
|
A custom tab widget, that has a "+" button for adding new tabs
|
|
|
|
"""
|
|
|
|
|
2019-11-28 15:24:26 -05:00
|
|
|
bring_to_front = QtCore.pyqtSignal()
|
|
|
|
|
2019-10-27 17:21:40 -04:00
|
|
|
def __init__(self, common, system_tray, status_bar):
|
|
|
|
super(TabWidget, self).__init__()
|
|
|
|
self.common = common
|
|
|
|
self.common.log("TabWidget", "__init__")
|
|
|
|
|
|
|
|
self.system_tray = system_tray
|
|
|
|
self.status_bar = status_bar
|
|
|
|
|
2019-10-27 17:35:11 -04:00
|
|
|
# Keep track of tabs in a dictionary
|
|
|
|
self.tabs = {}
|
2019-11-03 01:55:22 -04:00
|
|
|
self.current_tab_id = 0 # Each tab has a unique id
|
2019-10-27 17:35:11 -04:00
|
|
|
|
|
|
|
# Define the new tab button
|
2019-10-27 17:21:40 -04:00
|
|
|
self.new_tab_button = QtWidgets.QPushButton("+", parent=self)
|
|
|
|
self.new_tab_button.setFlat(True)
|
|
|
|
self.new_tab_button.setAutoFillBackground(True)
|
|
|
|
self.new_tab_button.setFixedSize(30, 30)
|
|
|
|
self.new_tab_button.clicked.connect(self.new_tab_clicked)
|
2019-10-27 18:21:46 -04:00
|
|
|
self.new_tab_button.setStyleSheet(
|
|
|
|
self.common.gui.css["tab_widget_new_tab_button"]
|
|
|
|
)
|
2019-10-27 17:21:40 -04:00
|
|
|
self.new_tab_button.setToolTip(strings._("gui_new_tab_tooltip"))
|
|
|
|
|
|
|
|
# Use a custom tab bar
|
|
|
|
tab_bar = TabBar()
|
|
|
|
tab_bar.move_new_tab_button.connect(self.move_new_tab_button)
|
|
|
|
self.setTabBar(tab_bar)
|
|
|
|
|
|
|
|
# Set up the tab widget
|
|
|
|
self.setMovable(True)
|
|
|
|
self.setTabsClosable(True)
|
|
|
|
self.setUsesScrollButtons(True)
|
|
|
|
|
2019-10-27 18:01:14 -04:00
|
|
|
self.tabCloseRequested.connect(self.close_tab)
|
|
|
|
|
2019-10-27 17:21:40 -04:00
|
|
|
self.move_new_tab_button()
|
|
|
|
|
2019-11-28 15:24:26 -05:00
|
|
|
# Watch the events file for changes
|
|
|
|
self.event_handler = EventHandler(common)
|
|
|
|
self.event_handler.new_tab.connect(self.add_tab)
|
|
|
|
self.event_handler.new_share_tab.connect(self.new_share_tab)
|
|
|
|
self.observer = Observer()
|
|
|
|
self.observer.schedule(self.event_handler, self.common.gui.events_dir)
|
|
|
|
self.observer.start()
|
|
|
|
|
2019-10-27 17:21:40 -04:00
|
|
|
def move_new_tab_button(self):
|
|
|
|
# Find the width of all tabs
|
|
|
|
tabs_width = sum(
|
|
|
|
[self.tabBar().tabRect(i).width() for i in range(self.count())]
|
|
|
|
)
|
|
|
|
|
|
|
|
# The current positoin of the new tab button
|
|
|
|
pos = self.new_tab_button.pos()
|
|
|
|
|
|
|
|
# If there are so many tabs it scrolls, move the button to the left of the scroll buttons
|
|
|
|
if tabs_width > self.width():
|
|
|
|
pos.setX(self.width() - 61)
|
|
|
|
else:
|
|
|
|
# Otherwise move the button to the right of the tabs
|
|
|
|
pos.setX(self.tabBar().sizeHint().width())
|
|
|
|
|
|
|
|
self.new_tab_button.move(pos)
|
|
|
|
self.new_tab_button.raise_()
|
|
|
|
|
|
|
|
def new_tab_clicked(self):
|
2019-11-03 00:45:55 -04:00
|
|
|
# Create a new tab
|
2019-11-03 01:55:22 -04:00
|
|
|
self.add_tab()
|
2019-11-03 00:45:55 -04:00
|
|
|
|
|
|
|
def load_tab(self, mode_settings_id):
|
|
|
|
# Load the tab's mode settings
|
|
|
|
mode_settings = ModeSettings(self.common, id=mode_settings_id)
|
2019-11-03 01:55:22 -04:00
|
|
|
self.add_tab(mode_settings)
|
2019-11-03 00:45:55 -04:00
|
|
|
|
2019-11-28 15:24:26 -05:00
|
|
|
def new_share_tab(self, filenames):
|
|
|
|
mode_settings = ModeSettings(self.common)
|
|
|
|
mode_settings.set("persistent", "mode", "share")
|
|
|
|
mode_settings.set("share", "filenames", filenames)
|
|
|
|
self.add_tab(mode_settings)
|
|
|
|
|
2019-11-03 01:55:22 -04:00
|
|
|
def add_tab(self, mode_settings=None):
|
|
|
|
tab = Tab(self.common, self.current_tab_id, self.system_tray, self.status_bar)
|
2019-10-27 17:35:11 -04:00
|
|
|
tab.change_title.connect(self.change_title)
|
2019-10-27 19:32:12 -04:00
|
|
|
tab.change_icon.connect(self.change_icon)
|
2019-10-27 21:41:24 -04:00
|
|
|
tab.change_persistent.connect(self.change_persistent)
|
2019-10-27 17:35:11 -04:00
|
|
|
|
2019-11-03 01:55:22 -04:00
|
|
|
self.tabs[self.current_tab_id] = tab
|
|
|
|
self.current_tab_id += 1
|
|
|
|
|
|
|
|
index = self.addTab(tab, strings._("gui_new_tab"))
|
2019-10-27 17:23:45 -04:00
|
|
|
self.setCurrentIndex(index)
|
2019-10-27 17:21:40 -04:00
|
|
|
|
2019-11-03 01:55:22 -04:00
|
|
|
tab.init(mode_settings)
|
|
|
|
# If it's persistent, set the persistent image in the tab
|
|
|
|
self.change_persistent(tab.tab_id, tab.settings.get("persistent", "enabled"))
|
|
|
|
|
2019-11-28 15:24:26 -05:00
|
|
|
# Bring the window to front, in case this is being added by an event
|
|
|
|
self.bring_to_front.emit()
|
|
|
|
|
2019-10-27 17:35:11 -04:00
|
|
|
def change_title(self, tab_id, title):
|
|
|
|
index = self.indexOf(self.tabs[tab_id])
|
|
|
|
self.setTabText(index, title)
|
|
|
|
|
2019-10-27 19:32:12 -04:00
|
|
|
def change_icon(self, tab_id, icon_path):
|
|
|
|
index = self.indexOf(self.tabs[tab_id])
|
|
|
|
self.setTabIcon(index, QtGui.QIcon(self.common.get_resource_path(icon_path)))
|
|
|
|
|
2019-10-27 21:41:24 -04:00
|
|
|
def change_persistent(self, tab_id, is_persistent):
|
|
|
|
index = self.indexOf(self.tabs[tab_id])
|
|
|
|
if is_persistent:
|
|
|
|
self.tabBar().setTabButton(
|
|
|
|
index,
|
|
|
|
QtWidgets.QTabBar.LeftSide,
|
|
|
|
self.tabs[tab_id].persistent_image_label,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
invisible_widget = QtWidgets.QWidget()
|
|
|
|
invisible_widget.setFixedSize(0, 0)
|
|
|
|
self.tabBar().setTabButton(
|
|
|
|
index, QtWidgets.QTabBar.LeftSide, invisible_widget
|
|
|
|
)
|
|
|
|
|
2019-11-03 00:45:55 -04:00
|
|
|
self.save_persistent_tabs()
|
|
|
|
|
|
|
|
def save_persistent_tabs(self):
|
|
|
|
# Figure out the order of persistent tabs to save in settings
|
|
|
|
persistent_tabs = []
|
|
|
|
for index in range(self.count()):
|
|
|
|
tab = self.widget(index)
|
|
|
|
if tab.settings.get("persistent", "enabled"):
|
|
|
|
persistent_tabs.append(tab.settings.id)
|
|
|
|
self.common.settings.set("persistent_tabs", persistent_tabs)
|
|
|
|
self.common.settings.save()
|
|
|
|
|
2019-10-27 18:01:14 -04:00
|
|
|
def close_tab(self, index):
|
|
|
|
self.common.log("TabWidget", "close_tab", f"{index}")
|
|
|
|
tab = self.widget(index)
|
|
|
|
if tab.close_tab():
|
2019-11-03 01:06:26 -05:00
|
|
|
# If the tab is persistent, delete the settings file from disk
|
|
|
|
if tab.settings.get("persistent", "enabled"):
|
|
|
|
tab.settings.delete()
|
|
|
|
|
|
|
|
# Remove the tab
|
2019-10-27 18:01:14 -04:00
|
|
|
self.removeTab(index)
|
|
|
|
del self.tabs[tab.tab_id]
|
|
|
|
|
2019-10-27 18:04:04 -04:00
|
|
|
# If the last tab is closed, open a new one
|
|
|
|
if self.count() == 0:
|
|
|
|
self.new_tab_clicked()
|
|
|
|
|
2019-11-03 01:06:26 -05:00
|
|
|
self.save_persistent_tabs()
|
|
|
|
|
2019-10-27 19:18:56 -04:00
|
|
|
def are_tabs_active(self):
|
|
|
|
"""
|
|
|
|
See if there are active servers in any open tabs
|
|
|
|
"""
|
|
|
|
for tab_id in self.tabs:
|
|
|
|
mode = self.tabs[tab_id].get_mode()
|
2019-10-27 19:36:32 -04:00
|
|
|
if mode:
|
|
|
|
if mode.server_status.status != mode.server_status.STATUS_STOPPED:
|
|
|
|
return True
|
2019-10-27 19:18:56 -04:00
|
|
|
return False
|
|
|
|
|
2019-11-03 00:45:55 -04:00
|
|
|
def changeEvent(self, event):
|
|
|
|
# TODO: later when I have internet, figure out the right event for re-ordering tabs
|
|
|
|
|
|
|
|
# If tabs get move
|
|
|
|
super(TabWidget, self).changeEvent(event)
|
|
|
|
self.save_persistent_tabs()
|
|
|
|
|
2019-10-27 18:01:14 -04:00
|
|
|
def resizeEvent(self, event):
|
|
|
|
# Make sure to move new tab button on each resize
|
|
|
|
super(TabWidget, self).resizeEvent(event)
|
|
|
|
self.move_new_tab_button()
|
|
|
|
|
2019-10-27 17:21:40 -04:00
|
|
|
|
|
|
|
class TabBar(QtWidgets.QTabBar):
|
|
|
|
"""
|
|
|
|
A custom tab bar
|
|
|
|
"""
|
|
|
|
|
|
|
|
move_new_tab_button = QtCore.pyqtSignal()
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super(TabBar, self).__init__()
|
|
|
|
|
|
|
|
def tabLayoutChange(self):
|
|
|
|
self.move_new_tab_button.emit()
|