Make ShareMode and ReceiveMode inherit from the same class, Mode

This commit is contained in:
Micah Lee 2018-04-25 21:54:28 -07:00
parent df346ad0ab
commit 691db6343d
No known key found for this signature in database
GPG Key ID: 403C2657CD994F73
4 changed files with 90 additions and 55 deletions

67
onionshare_gui/mode.py Normal file
View File

@ -0,0 +1,67 @@
# -*- 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
from onionshare import strings
from .server_status import ServerStatus
class Mode(QtWidgets.QWidget):
"""
The class that ShareMode and ReceiveMode inherit from.
"""
def __init__(self, common, qtapp, app, web, status_bar, server_share_status_label, system_tray, filenames=None):
super(Mode, self).__init__()
self.common = common
self.qtapp = qtapp
self.app = app
self.web = web
self.status_bar = status_bar
self.server_share_status_label = server_share_status_label
self.system_tray = system_tray
self.filenames = filenames
# Server status
self.server_status = ServerStatus(self.common, self.qtapp, self.app, self.web, False)
# Primary action layout
self.primary_action_layout = QtWidgets.QVBoxLayout()
self.primary_action_layout.addWidget(self.server_status)
self.primary_action = QtWidgets.QWidget()
self.primary_action.setLayout(self.primary_action_layout)
# Layout
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.primary_action)
self.setLayout(self.layout)
def init(self):
"""
Add custom initialization of the mode here.
"""
pass
def timer_callback(self):
"""
This method is called regularly on a timer.
"""
pass

View File

@ -157,7 +157,8 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.status_bar.insertWidget(0, self.server_share_status_label)
# Share and receive mode widgets
self.share_mode = ShareMode(self.common, filenames, qtapp, app, web, self.status_bar, self.server_share_status_label, self.system_tray)
self.share_mode = ShareMode(self.common, qtapp, app, web, self.status_bar, self.server_share_status_label, self.system_tray, filenames)
self.share_mode.init()
self.share_mode.server_status.server_started.connect(self.update_server_status_indicator)
self.share_mode.server_status.server_stopped.connect(self.update_server_status_indicator)
self.share_mode.start_server_finished.connect(self.update_server_status_indicator)
@ -169,6 +170,7 @@ class OnionShareGui(QtWidgets.QMainWindow):
self.share_mode.server_status.hidservauth_copied.connect(self.copy_hidservauth)
self.share_mode.set_share_server_active.connect(self.set_share_server_active)
self.receive_mode = ReceiveMode(self.common, qtapp, app, web, self.status_bar, self.server_share_status_label, self.system_tray)
self.receive_mode.init()
self.update_mode_switcher()
self.update_server_status_indicator()

View File

@ -21,42 +21,23 @@ from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings
from ..server_status import ServerStatus
from ..mode import Mode
class ReceiveMode(QtWidgets.QWidget):
class ReceiveMode(Mode):
"""
Parts of the main window UI for receiving files.
"""
def __init__(self, common, qtapp, app, web, status_bar, server_share_status_label, system_tray):
super(ReceiveMode, self).__init__()
self.common = common
self.qtapp = qtapp
self.app = app
self.web = web
self.status_bar = status_bar
self.server_share_status_label = server_share_status_label
self.system_tray = system_tray
def init(self):
"""
Custom initialization for ReceiveMode.
"""
# Receive mode info
self.receive_info = QtWidgets.QLabel(strings._('gui_receive_mode_warning', True))
self.receive_info.setMinimumHeight(80)
self.receive_info.setWordWrap(True)
# Server status
self.server_status = ServerStatus(self.common, self.qtapp, self.app, self.web, False)
# Primary action layout
primary_action_layout = QtWidgets.QVBoxLayout()
primary_action_layout.addWidget(self.server_status)
self.primary_action = QtWidgets.QWidget()
self.primary_action.setLayout(primary_action_layout)
# Layout
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.receive_info)
layout.addWidget(self.primary_action)
self.setLayout(layout)
self.layout.insertWidget(0, self.receive_info)
def timer_callback(self):
"""

View File

@ -28,12 +28,12 @@ from onionshare.onion import *
from .file_selection import FileSelection
from .downloads import Downloads
from ..server_status import ServerStatus
from ..mode import Mode
from ..onion_thread import OnionThread
from ..widgets import Alert
class ShareMode(QtWidgets.QWidget):
class ShareMode(Mode):
"""
Parts of the main window UI for sharing files.
"""
@ -44,27 +44,19 @@ class ShareMode(QtWidgets.QWidget):
starting_server_error = QtCore.pyqtSignal(str)
set_share_server_active = QtCore.pyqtSignal(bool)
def __init__(self, common, filenames, qtapp, app, web, status_bar, server_share_status_label, system_tray):
super(ShareMode, self).__init__()
self.common = common
self.qtapp = qtapp
self.app = app
self.web = web
self.status_bar = status_bar
self.server_share_status_label = server_share_status_label
self.system_tray = system_tray
def init(self):
"""
Custom initialization for ReceiveMode.
"""
# File selection
self.file_selection = FileSelection(self.common)
if filenames:
for filename in filenames:
if self.filenames:
for filename in self.filenames:
self.file_selection.file_list.add_file(filename)
# Server status
self.server_status = ServerStatus(self.common, self.qtapp, self.app, self.web, True, self.file_selection)
self.server_status.server_started.connect(self.file_selection.server_started)
self.server_status.server_started.connect(self.start_server)
self.server_status.server_started.connect(self.file_selection.server_started)
self.server_status.server_stopped.connect(self.file_selection.server_stopped)
self.server_status.server_stopped.connect(self.stop_server)
self.server_status.server_stopped.connect(self.update_primary_action)
@ -122,11 +114,7 @@ class ShareMode(QtWidgets.QWidget):
self.info_widget.hide()
# Primary action layout
primary_action_layout = QtWidgets.QVBoxLayout()
primary_action_layout.addWidget(self.server_status)
primary_action_layout.addWidget(self.filesize_warning)
self.primary_action = QtWidgets.QWidget()
self.primary_action.setLayout(primary_action_layout)
self.primary_action_layout.addWidget(self.filesize_warning)
self.primary_action.hide()
self.update_primary_action()
@ -134,11 +122,8 @@ class ShareMode(QtWidgets.QWidget):
self._zip_progress_bar = None
# Layout
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.info_widget)
layout.addLayout(self.file_selection)
layout.addWidget(self.primary_action)
self.setLayout(layout)
self.layout.insertWidget(1, self.info_widget)
self.layout.insertLayout(0, self.file_selection)
# Always start with focus on file selection
self.file_selection.setFocus()