From f5ce0690314f79ed22d9615226aab52ec71c2a1f Mon Sep 17 00:00:00 2001 From: Micah Lee Date: Sun, 20 May 2018 14:40:27 -0700 Subject: [PATCH] Make it so the open folder button works in Linux, with nautilus --- onionshare_gui/receive_mode/uploads.py | 36 +++++++++++++++++++++++++- share/locale/en.json | 3 ++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/onionshare_gui/receive_mode/uploads.py b/onionshare_gui/receive_mode/uploads.py index 784ecaaa..203a9804 100644 --- a/onionshare_gui/receive_mode/uploads.py +++ b/onionshare_gui/receive_mode/uploads.py @@ -17,18 +17,23 @@ 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 . """ +import os +import subprocess from datetime import datetime from PyQt5 import QtCore, QtWidgets, QtGui from onionshare import strings +from ..widgets import Alert class File(QtWidgets.QWidget): def __init__(self, common, filename): super(File, self).__init__() self.common = common - self.filename = filename + self.common.log('File', '__init__', 'filename: {}'.format(filename)) + + self.filename = filename self.started = datetime.now() # Filename label @@ -43,6 +48,7 @@ class File(QtWidgets.QWidget): folder_pixmap = QtGui.QPixmap.fromImage(QtGui.QImage(self.common.get_resource_path('images/open_folder.png'))) folder_icon = QtGui.QIcon(folder_pixmap) self.folder_button = QtWidgets.QPushButton() + self.folder_button.clicked.connect(self.open_folder) self.folder_button.setIcon(folder_icon) self.folder_button.setIconSize(folder_pixmap.rect().size()) self.folder_button.setFlat(True) @@ -67,6 +73,34 @@ class File(QtWidgets.QWidget): self.filename = new_filename self.filename_label.setText(self.filename) + def open_folder(self): + """ + Open the downloads folder, with the file selected, in a cross-platform manner + """ + self.common.log('File', 'open_folder') + + abs_filename = os.path.join(self.common.settings.get('downloads_dir'), self.filename) + + # Linux + if self.common.platform == 'Linux' or self.common.platform == 'BSD': + try: + # If nautilus is available, open it + subprocess.Popen(['nautilus', abs_filename]) + except: + Alert(self.common, strings._('gui_open_folder_error_nautilus').format(abs_filename)) + + # macOS + elif self.common.platform == 'Darwin': + # TODO: Implement opening folder with file selected in macOS + # This seems helpful: https://stackoverflow.com/questions/3520493/python-show-in-finder + self.common.log('File', 'open_folder', 'not implemented for Darwin yet') + + # Windows + elif self.common.platform == 'Windows': + # TODO: Implement opening folder with file selected in Windows + # This seems helpful: https://stackoverflow.com/questions/6631299/python-opening-a-folder-in-explorer-nautilus-mac-thingie + self.common.log('File', 'open_folder', 'not implemented for Windows yet') + class Upload(QtWidgets.QWidget): def __init__(self, common, upload_id, content_length): diff --git a/share/locale/en.json b/share/locale/en.json index bd0f939d..e0658a49 100644 --- a/share/locale/en.json +++ b/share/locale/en.json @@ -193,5 +193,6 @@ "gui_no_uploads": "No uploads yet.", "gui_upload_in_progress": "Upload Started {}", "gui_upload_finished_range": "Uploaded {} to {}", - "gui_upload_finished": "Uploaded {}" + "gui_upload_finished": "Uploaded {}", + "gui_open_folder_error_nautilus": "Cannot open folder the because nautilus is not available. You can find this file here: {}" }