Add onionshare CLI to cli folder, move GUI to desktop folder, and start refactoring it to work with briefcase

This commit is contained in:
Micah Lee 2020-10-12 22:40:55 -07:00
parent 93e90c89ae
commit a54f99adf6
583 changed files with 14871 additions and 474 deletions

View file

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
Copyright (C) 2014-2020 Micah Lee, et al. <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/>.
"""
import onionshare
onionshare.main()

View file

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
Copyright (C) 2014-2020 Micah Lee, et al. <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/>.
"""
import onionshare_gui
onionshare_gui.main()

View file

@ -0,0 +1,105 @@
import os
import sys
import json
import locale
import subprocess
try:
import urllib.request
except:
import urllib
import gi
gi.require_version("Nautilus", "3.0")
from gi.repository import Nautilus
from gi.repository import GObject
# Put me in /usr/share/nautilus-python/extensions/
class OnionShareExtension(GObject.GObject, Nautilus.MenuProvider):
def __init__(self):
# Get the localized string for "Share via OnionShare" label
self.label = None
default_label = "Share via OnionShare"
try:
# Re-implement localization in python2
default_locale = "en"
locale_dir = os.path.join(sys.prefix, "share/onionshare/locale")
if os.path.exists(locale_dir):
# Load all translations
strings = {}
translations = {}
for filename in os.listdir(locale_dir):
abs_filename = os.path.join(locale_dir, filename)
lang, ext = os.path.splitext(filename)
if ext == ".json":
with open(abs_filename) as f:
translations[lang] = json.load(f)
strings = translations[default_locale]
lc, enc = locale.getdefaultlocale()
if lc:
lang = lc[:2]
if lang in translations:
# if a string doesn't exist, fallback to English
for key in translations[default_locale]:
if key in translations[lang]:
strings[key] = translations[lang][key]
self.label = strings["share_via_onionshare"]
except:
self.label = default_label
if not self.label:
self.label = default_label
"""
# This more elegant solution will only work if nautilus is using python3, and onionshare is installed system-wide.
# But nautilus is using python2, so this is commented out.
try:
import onionshare
onionshare.strings.load_strings(onionshare.common)
self.label = onionshare.strings._('share_via_onionshare')
except:
import sys
print('python version: {}').format(sys.version)
self.label = 'Share via OnionShare'
"""
def url2path(self, url):
file_uri = url.get_activation_uri()
arg_uri = file_uri[7:]
try:
path = urllib.request.url2pathname(arg_uri)
except:
path = urllib.url2pathname(arg_uri)
return path
def exec_onionshare(self, filenames):
# Would prefer this method but there is a conflict between GTK 2.0 vs GTK 3.0 components being loaded at once
# (nautilus:3090): Gtk-ERROR **: GTK+ 2.x symbols detected. Using GTK+ 2.x and GTK+ 3 in the same process is not supported
# sys.argv = ["", "--filenames"] + filenames
# sys.exit(onionshare_gui.main())
path = os.path.join(os.sep, "usr", "bin", "onionshare-gui")
cmd = [path, "--filenames"] + filenames
subprocess.Popen(cmd)
def get_file_items(self, window, files):
menuitem = Nautilus.MenuItem(
name="OnionShare::Nautilus", label=self.label, tip="", icon=""
)
menu = Nautilus.Menu()
menu.append_item(menuitem)
menuitem.connect("activate", self.menu_activate_cb, files)
return (menuitem,)
def menu_activate_cb(self, menu, files):
file_list = []
for file in files:
file_list.append(self.url2path(file))
self.exec_onionshare(file_list)
# Workaround https://bugzilla.gnome.org/show_bug.cgi?id=784278
def get_background_items(self, window, file):
return None

View file

@ -0,0 +1,47 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
OnionShare | https://onionshare.org/
Copyright (C) 2014-2020 Micah Lee, et al. <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/>.
"""
import sys, os, platform
# In macOS, allow both CLI and GUI depending on the filename of the binary
# being executed
if platform.system() == "Darwin":
# If the binary being executed is called 'onionshare', use CLI
basename = os.path.basename(sys.argv[0])
if basename == "onionshare":
import onionshare
onionshare.main()
# Otherwise, use GUI
else:
import onionshare_gui
onionshare_gui.main()
# Unfortunately this trick won't work in Windows because I want to set
# console=False in the PyInstaller spec file, so there isn't a command prompt
# open in the background every you run the GUI. Hopefully Windows can get
# a built-in CLI when PyInstaller 3.3 comes out:
# https://pyinstaller.readthedocs.io/en/stable/spec-files.html#multipackage-bundles
else:
import onionshare_gui
onionshare_gui.main()