This repository has been archived on 2023-02-02. You can view files and clone it, but cannot push or open issues or pull requests.
barkshark-web/barkshark_web/component/application.py
2022-04-16 06:23:04 -04:00

265 lines
6.4 KiB
Python

from izzylib.http_client2 import HttpClient
from .window import Window
from .. import dbus
from .. import __version__ as version
from ..config import var
from ..database import db
from ..exceptions import AccountNotFoundError, NoAccountsError
class Application(Gtk.Application):
library_pages = (
'bookmarks', 'downloads', 'history', 'passwords',
'search', 'fediverse', 'extensions', 'preferences'
)
def __init__(self, *urls):
super().__init__()
#self.set_flags(Gio.ApplicationFlags.NON_UNIQUE)
self.window = None
self.clipboard = Gtk.Clipboard.get_default(Gdk.Display.get_default())
self.startup_urls = urls
self.accounts = []
self.connect('activate', self.handle_activate)
self.connect('startup', self.handle_startup)
def create_tabs(self):
with db.session as s:
for row in s.fetch('tabs', orderby='order'):
self.window.new_tab(row=row)
for url in self.startup_urls:
self.window.new_tab(url, switch=row.active)
if len(self.window.tabdata.keys()) < 1:
self.window.new_tab(None, switch=False)
self.window.startup = False
def get_account_by_handle(self, username, domain=None):
if not len(self.accounts):
raise NoAccountsError('No accounts')
for acct in self.accounts:
if not domain and username == acct.handle:
return acct
elif domain and username == acct.handle and domain == acct.domain:
return acct
raise AccountNotFoundError('Cannot find account')
def get_account_by_id(self, id):
if not len(self.accounts):
raise NoAccountsError('No accounts')
for acct in self.accounts:
if acct.id == id:
return acct
raise AccountNotFoundError('Cannot find account')
def get_default_account(self):
if not len(self.accounts):
raise NoAccountsError('No accounts')
with db.session as s:
default = s.get_config('active_acct')
for acct in self.accounts:
if default == acct.id:
return acct
s.put_config('active_acct', self.accounts[0].id)
return self.accounts[0]
def set_clipboard_text(self, *text):
self.clipboard.set_text(' '.join(text), -1)
def quit(self, *args):
db.unregister_all_callbacks()
if self.window:
#self.window.passwords.db.disconnect()
self.window.handle_window_close()
super().quit()
def handle_activate(self, *args):
self.window = Window(self)
self.dbus = dbus.Server(self.window)
self.add_window(self.window)
self.create_tabs()
self.window.show()
def handle_clipboard_clear_password(self, password):
if self.clipboard.wait_for_text() == password:
logging.debug('Clear clipboard text')
self.clipboard.set_text('', 0)
def handle_startup(self, *args):
Gtk.Application.do_startup(self)
accel = Gio.SimpleAction.new('accel', GLib.VariantType.new('s'))
accel.connect('activate', self.handle_accel)
self.add_action(accel)
self.keybinds = {
'Close current tab': ('close_tab', ['<Ctrl>W']),
'Focus url bar': ('focus_urlbar', ['<Ctrl>L']),
'Force reload page': ('force_reload', ['<Ctrl><Shift>R', '<Shift>F5']),
'Add or edit Bookmark': ('new_bookmark', ['<Ctrl>B']),
'Open new tab': ('new_tab', ['<Ctrl>T']),
'Open file': ('open_file', ['<Ctrl>O']),
'Open help page': ('open_help', ['<Ctrl>H', 'F1']),
'Open web inspector': ('open_inspector', ['<Ctrl><Shift>I', 'F12']),
'Open page search bar': ('open_search', ['<Ctrl>F', 'F3']),
'Print page': ('print', ['<Ctrl>P']),
'Quit app': ('quit', ['<Ctrl>Q']),
'Reload page': ('reload', ['<Ctrl>R', 'F5']),
'Reopen last closed tab': ('reopen', ['<Ctrl><Shift>T']),
'Save page as MHTML': ('save_page', ['<Ctrl>S']),
'Toggle fullscreen state': ('toggle_fullscreen', ['<Alt>Return', 'F11']),
'Open or close library': ('toggle_library', ['<Ctrl>U'])
}
for action, keybind in self.keybinds.values():
self.set_accels_for_action(f'app.accel::{action}', keybind)
for idx, page in enumerate(self.library_pages):
self.set_accels_for_action(f'app.accel::library_page_{page}', [f'<Ctrl>{idx+1}'])
## Load up a webview, set the proper user-agent, and apply the new agent to the http client
webview = WebKit2.WebView.new()
webview.get_settings().set_user_agent_with_application_details('Barkshark Web', version)
self.http_client = HttpClient()
self.http_client.set_useragent(webview.get_settings().get_user_agent())
webview.destroy()
del webview
with db.session as s:
self.accounts = s.fetch('accounts').all()
def handle_accel(self, signal, action):
action = action.get_string()
tab = self.window.active_tab
if action.startswith('library_page'):
self.handle_library_page(action, tab)
return
try:
func = getattr(self, f'handle_accel_{action}')
except AttributeError:
logging.error('Unhandled keyboard shortcut action:', action)
return
func(action, tab)
def handle_accel_close_tab(self, action, tab):
tab.close_tab()
def handle_accel_focus_urlbar(self, action, tab):
self.window['navbar-url'].grab_focus()
def handle_accel_force_reload(self, action, tab):
tab.page_action('reload', ignore_cache=True)
def handle_library_page(self, action, tab):
page = action.replace('library_page_', '')
url = f'{var.local}/{page}'
if tab.url.proto == var.local[:-3]:
tab.load_url(url)
return
self.window.new_tab(url, switch=True)
def handle_accel_new_bookmark(self, action, tab):
widget = self.window['statusbar-bookmark']
if widget.get_sensitive():
widget.activate()
def handle_accel_new_tab(self, action, tab):
self.window.new_tab(switch=True)
def handle_accel_open_file(self, action, tab):
self.window.open_file()
def handle_accel_open_help(self, action, tab):
url = var.local + 'help'
if tab.url.proto == var.local[:-3]:
tab.load_url(url)
return
self.window.new_tab(url, switch=True)
def handle_accel_open_inspector(self, action, tab):
tab.toggle_inspector()
def handle_accel_open_search(self, action, tab):
tab.search_toggle()
def handle_accel_print(self, action, tab):
tab.page_print()
def handle_accel_quit(self, action, tab):
self.quit()
def handle_accel_reload(self, action, tab):
tab.page_action('reload')
def handle_accel_reopen(self, action, tab):
if not len(self.window.closed.keys()):
return
last_tab = list(self.window.closed.keys())[-1]
self.window.reopen_tab(last_tab)
def handle_accel_save_page(self, action, tab):
tab.page_save()
def handle_accel_toggle_fullscreen(self, action, tab):
self.window.fullscreen_toggle()
def handle_accel_toggle_library(self, action, tab):
self.window.library_toggle()