Files
DemonEditor/app/ui/download_dialog.py

200 lines
8.6 KiB
Python
Raw Normal View History

2021-08-30 15:04:15 +03:00
# -*- coding: utf-8 -*-
#
# The MIT License (MIT)
#
# Copyright (c) 2018-2021 Dmitriy Yefremov
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Author: Dmitriy Yefremov
#
import os
from gi.repository import GLib
2020-06-08 13:33:46 +03:00
from app.commons import run_idle, run_task, log
2018-11-04 00:33:50 +03:00
from app.connections import download_data, DownloadType, upload_data
2019-12-22 20:42:29 +03:00
from app.settings import SettingsType
2019-05-19 00:37:07 +03:00
from app.ui.backup import backup_data, restore_data
2018-11-06 21:21:47 +03:00
from app.ui.main_helper import append_text_to_tview
from app.ui.settings_dialog import show_settings_dialog
2021-04-28 14:12:59 +03:00
from .dialogs import show_dialog, DialogType, get_message, get_builder
from .uicommons import Gtk, UI_RESOURCES_PATH
2017-11-06 22:17:43 +03:00
class DownloadDialog:
2019-12-13 13:31:07 +03:00
def __init__(self, transient, settings, open_data_callback, update_settings_callback):
2019-12-22 20:42:29 +03:00
self._s_type = settings.setting_type
2019-12-13 13:31:07 +03:00
self._settings = settings
self._open_data_callback = open_data_callback
self._update_settings_callback = update_settings_callback
2017-11-06 22:17:43 +03:00
handlers = {"on_receive": self.on_receive,
"on_send": self.on_send,
"on_settings": self.on_settings,
"on_profile_changed": self.on_profile_changed,
2020-07-04 13:38:39 +03:00
"on_use_http_state_set": self.on_use_http_state_set,
"on_remove_unused_bouquets_toggled": self.on_remove_unused_bouquets_toggled,
2017-11-06 22:17:43 +03:00
"on_info_bar_close": self.on_info_bar_close}
2021-04-28 14:12:59 +03:00
builder = get_builder(UI_RESOURCES_PATH + "download_dialog.glade", handlers)
2017-11-06 22:17:43 +03:00
2018-11-06 21:21:47 +03:00
self._dialog_window = builder.get_object("download_dialog_window")
self._dialog_window.set_transient_for(transient)
2017-11-06 22:17:43 +03:00
self._info_bar = builder.get_object("info_bar")
self._message_label = builder.get_object("info_bar_message_label")
2018-11-06 21:21:47 +03:00
self._text_view = builder.get_object("text_view")
self._expander = builder.get_object("expander")
self._host_entry = builder.get_object("host_entry")
self._data_path_entry = builder.get_object("data_path_entry")
2017-11-06 22:17:43 +03:00
self._remove_unused_check_button = builder.get_object("remove_unused_check_button")
self._all_radio_button = builder.get_object("all_radio_button")
self._bouquets_radio_button = builder.get_object("bouquets_radio_button")
self._satellites_radio_button = builder.get_object("satellites_radio_button")
2018-02-12 13:34:00 +03:00
self._webtv_radio_button = builder.get_object("webtv_radio_button")
self._use_http_switch = builder.get_object("use_http_switch")
self._http_radio_button = builder.get_object("http_radio_button")
self._use_http_box = builder.get_object("use_http_box")
self._profile_combo_box = builder.get_object("profile_combo_box")
2019-12-13 13:31:07 +03:00
self.init_settings()
2018-11-06 21:21:47 +03:00
def show(self):
2018-11-06 21:21:47 +03:00
self._dialog_window.show()
2019-12-13 13:31:07 +03:00
def init_settings(self):
self.update_profiles()
self.init_ui_settings()
def init_ui_settings(self):
2019-12-13 13:31:07 +03:00
self._host_entry.set_text(self._settings.host)
2021-08-30 15:04:15 +03:00
self._data_path_entry.set_text(self._settings.profile_data_path)
2019-12-22 20:42:29 +03:00
is_enigma = self._s_type is SettingsType.ENIGMA_2
self._webtv_radio_button.set_visible(not is_enigma)
self._use_http_box.set_visible(is_enigma)
2020-07-04 13:38:39 +03:00
self._use_http_switch.set_active(is_enigma and self._settings.use_http)
self._remove_unused_check_button.set_active(self._settings.remove_unused_bouquets)
def update_profiles(self):
self._profile_combo_box.remove_all()
for p in self._settings.profiles:
self._profile_combo_box.append(p, p)
self._profile_combo_box.set_active_id(self._settings.current_profile)
2017-11-14 19:20:16 +03:00
@run_idle
2017-11-06 22:17:43 +03:00
def on_receive(self, item):
2018-02-12 13:34:00 +03:00
self.download(True, self.get_download_type())
2017-11-06 22:17:43 +03:00
2017-11-14 19:20:16 +03:00
@run_idle
2017-11-06 22:17:43 +03:00
def on_send(self, item):
2018-11-06 21:21:47 +03:00
if show_dialog(DialogType.QUESTION, self._dialog_window) != Gtk.ResponseType.CANCEL:
2018-02-12 13:34:00 +03:00
self.download(False, self.get_download_type())
2017-11-06 22:17:43 +03:00
def get_download_type(self):
2018-08-04 11:38:38 +03:00
download_type = DownloadType.ALL
2017-11-06 22:17:43 +03:00
if self._bouquets_radio_button.get_active():
2018-08-04 11:38:38 +03:00
download_type = DownloadType.BOUQUETS
2017-11-06 22:17:43 +03:00
elif self._satellites_radio_button.get_active():
2018-08-04 11:38:38 +03:00
download_type = DownloadType.SATELLITES
2018-02-12 13:34:00 +03:00
elif self._webtv_radio_button.get_active():
2020-02-10 14:45:05 +03:00
download_type = DownloadType.WEBTV
2017-11-06 22:17:43 +03:00
return download_type
def destroy(self):
2018-11-06 21:21:47 +03:00
self._dialog_window.destroy()
def on_settings(self, item):
2019-12-13 13:31:07 +03:00
response = show_settings_dialog(self._dialog_window, self._settings)
if response != Gtk.ResponseType.CANCEL:
2019-12-22 20:42:29 +03:00
self._s_type = self._settings.setting_type
self.update_profiles()
2019-12-13 13:31:07 +03:00
gen = self._update_settings_callback()
GLib.idle_add(lambda: next(gen, False), priority=GLib.PRIORITY_LOW)
def on_profile_changed(self, box):
active = box.get_active_text()
if active in self._settings.profiles:
self._settings.current_profile = active
self._profile_combo_box.set_active_id(active)
2020-02-10 17:00:46 +03:00
self._s_type = self._settings.setting_type
self.init_ui_settings()
2020-07-04 13:38:39 +03:00
def on_use_http_state_set(self, button, state):
self._settings.use_http = state
def on_remove_unused_bouquets_toggled(self, button):
self._settings.remove_unused_bouquets = button.get_active()
2018-01-11 17:59:59 +03:00
def on_info_bar_close(self, bar=None, resp=None):
2017-11-06 22:17:43 +03:00
self._info_bar.set_visible(False)
@run_task
2018-02-12 13:34:00 +03:00
def download(self, download, d_type):
2017-11-06 22:17:43 +03:00
""" Download/upload data from/to receiver """
2020-07-12 22:28:49 +03:00
GLib.idle_add(self._expander.set_expanded, True)
2019-05-19 00:37:07 +03:00
self.clear_output()
2019-12-13 13:31:07 +03:00
backup, backup_src, data_path = self._settings.backup_before_downloading, None, None
2018-11-06 21:21:47 +03:00
2019-05-19 00:37:07 +03:00
try:
2017-11-06 22:17:43 +03:00
if download:
2019-05-19 00:37:07 +03:00
if backup and d_type is not DownloadType.SATELLITES:
2021-08-30 15:04:15 +03:00
data_path = self._settings.profile_data_path or self._data_path_entry.get_text()
os.makedirs(os.path.dirname(data_path), exist_ok=True)
2021-08-30 15:04:15 +03:00
backup_path = self._settings.profile_backup_path or self._settings.default_backup_path
2019-05-19 00:37:07 +03:00
backup_src = backup_data(data_path, backup_path, d_type is DownloadType.ALL)
2019-12-13 13:31:07 +03:00
download_data(settings=self._settings, download_type=d_type, callback=self.append_output)
2017-11-06 22:17:43 +03:00
else:
2018-03-27 23:03:01 +03:00
self.show_info_message(get_message("Please, wait..."), Gtk.MessageType.INFO)
2019-12-13 13:31:07 +03:00
upload_data(settings=self._settings,
2017-11-10 13:38:03 +03:00
download_type=d_type,
2018-01-07 16:33:18 +03:00
remove_unused=self._remove_unused_check_button.get_active(),
2018-11-06 21:21:47 +03:00
callback=self.append_output,
done_callback=lambda: self.show_info_message(get_message("Done!"), Gtk.MessageType.INFO),
use_http=self._use_http_switch.get_active())
2017-11-06 22:17:43 +03:00
except Exception as e:
2020-07-11 12:58:03 +03:00
msg = "Downloading data error: {}"
log(msg.format(e), debug=self._settings.debug_mode, fmt_message=msg)
2020-06-08 13:33:46 +03:00
self.show_info_message(str(e), Gtk.MessageType.ERROR)
2019-05-19 00:37:07 +03:00
if all((download, backup, data_path)):
restore_data(backup_src, data_path)
2017-11-06 22:17:43 +03:00
else:
2018-08-04 11:38:38 +03:00
if download and d_type is not DownloadType.SATELLITES:
GLib.idle_add(self._open_data_callback)
2017-11-06 22:17:43 +03:00
2017-11-14 19:20:16 +03:00
@run_idle
def show_info_message(self, text, message_type):
self._info_bar.set_visible(True)
self._info_bar.set_message_type(message_type)
self._message_label.set_text(text)
2018-11-06 21:21:47 +03:00
@run_idle
def append_output(self, text):
append_text_to_tview(text, self._text_view)
@run_idle
def clear_output(self):
self._text_view.get_buffer().set_text("")
2017-11-06 22:17:43 +03:00
if __name__ == "__main__":
pass