Files
DemonEditor/app/ui/transmitter.py

88 lines
2.9 KiB
Python
Raw Normal View History

2019-11-05 23:04:21 +03:00
from urllib.parse import urlparse
2019-11-24 21:58:32 +03:00
from gi.repository import GLib
2019-11-05 23:04:21 +03:00
from app.connections import HttpRequestType
2019-11-24 21:58:32 +03:00
from app.tools.yt import YouTube
from app.ui.iptv import get_yt_icon
2019-11-05 23:04:21 +03:00
from .uicommons import Gtk, UI_RESOURCES_PATH, TEXT_DOMAIN
class LinksTransmitter:
2019-11-24 21:58:32 +03:00
def __init__(self, http_api, app_window):
2019-11-05 23:04:21 +03:00
handlers = {"on_popup_menu": self.on_popup_menu,
"on_status_icon_activate": self.on_status_icon_activate,
"on_query_tooltip": self.on_query_tooltip,
"on_drag_data_received": self.on_drag_data_received,
"on_exit": self.on_exit}
self._http_api = http_api
2019-11-24 21:58:32 +03:00
self._app_window = app_window
2019-11-05 23:04:21 +03:00
builder = Gtk.Builder()
builder.set_translation_domain(TEXT_DOMAIN)
builder.add_from_file(UI_RESOURCES_PATH + "transmitter.glade")
builder.connect_signals(handlers)
self._tray = builder.get_object("status_icon")
self._main_window = builder.get_object("main_window")
self._url_entry = builder.get_object("url_entry")
def show(self, show):
self._tray.set_visible(show)
if not show:
2019-11-24 21:58:32 +03:00
self.hide()
def hide(self):
self._main_window.hide()
2019-11-05 23:04:21 +03:00
def on_popup_menu(self, menu, button, time):
menu.popup(None, None, None, None, button, time)
def on_status_icon_activate(self, window):
visible = window.get_visible()
window.hide() if visible else window.show()
2019-11-24 21:58:32 +03:00
self._app_window.present() if visible else self._app_window.iconify()
2019-11-05 23:04:21 +03:00
def on_query_tooltip(self, icon, g, x, y, tooltip: Gtk.Tooltip):
if self._main_window.get_visible():
return False
tooltip.set_text("Test")
return True
def on_drag_data_received(self, widget, drag_context, x, y, data, info, time):
2019-11-24 21:58:32 +03:00
gen = self.activate_url(data.get_text())
GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_LOW)
2019-11-05 23:04:21 +03:00
def activate_url(self, url):
result = urlparse(url)
if result.scheme and result.netloc:
2019-11-24 21:58:32 +03:00
self._url_entry.set_sensitive(False)
yt_id = YouTube.get_yt_id(url)
yield True
if yt_id:
self._url_entry.set_icon_from_pixbuf(Gtk.EntryIconPosition.SECONDARY, get_yt_icon("youtube", 32))
links, title = YouTube.get_yt_link(yt_id)
yield True
if links:
url = links[sorted(links, key=lambda x: int(x.rstrip("p")), reverse=True)[0]]
else:
self._url_entry.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, None)
self._http_api.send(HttpRequestType.PLAY, url, self.on_play)
yield True
def on_play(self, res):
""" Play callback """
self._url_entry.set_sensitive(True)
if res:
print(res)
2019-11-05 23:04:21 +03:00
def on_exit(self, item=None):
self.show(False)
if __name__ == "__main__":
pass