Skip to content

Commit

Permalink
Shutdown after completion feature
Browse files Browse the repository at this point in the history
- Shutdown after completion feature added
- Made the pause and cancel download buttons round
- Added relevant translations
  • Loading branch information
giantpinkrobots committed Mar 19, 2024
1 parent 04a889b commit 4f4af13
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 24 deletions.
15 changes: 15 additions & 0 deletions po/Varia.pot
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ msgstr ""
msgid "Browser Extension"
msgstr ""

msgid "Shutdown on Completion"
msgstr ""

msgid "Varia is about to shut down your computer."
msgstr ""

msgid "Press Cancel to cancel and disable."
msgstr ""

msgid "Cancel"
msgstr ""

msgid "Warning"
msgstr ""

#: src/gtk/help-overlay.ui:11
msgctxt "shortcut window"
msgid "General"
Expand Down
19 changes: 17 additions & 2 deletions po/tr.po
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2023-11-30 03:17+0300\n"
"PO-Revision-Date: 2023-11-30 03:18+0300\n"
"Last-Translator: Sabri Ünal <libreajans@gmail.com>\n"
"Language-Team: Turkish <takim@gnome.org.tr>\n"
"Last-Translator: Giant Pink Robots! <giantpinkrobots@protonmail.com>\n"
"Language-Team: \n"
"Language: tr\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
Expand Down Expand Up @@ -222,6 +222,21 @@ msgstr "İndirmeler arka planda devam ediyor."
msgid "Browser Extension"
msgstr "Tarayıcı Uzantısı"

msgid "Shutdown on Completion"
msgstr "İndirmeler Bittiğinde Bilgisayarı Kapat"

msgid "Varia is about to shut down your computer."
msgstr "Varia birazdan bilgisayarınızı kapatacak."

msgid "Press Cancel to cancel and disable."
msgstr "İptal edip devre dışı bırakmak için Vazgeç'e basın."

msgid "Cancel"
msgstr "Vazgeç"

msgid "Warning"
msgstr "Uyarı"

#: src/gtk/help-overlay.ui:11
msgctxt "shortcut window"
msgid "General"
Expand Down
13 changes: 7 additions & 6 deletions src/download/actionrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ def create_actionrow(self, filename):

pause_button = Gtk.Button.new_from_icon_name("media-playback-pause-symbolic")
pause_button.set_valign(Gtk.Align.CENTER)
pause_button.get_style_context().add_class("circular")
pause_button.connect("clicked", on_pause_clicked, self, pause_button, download_item)

self.pause_buttons.append(pause_button)
button_box.append(pause_button)

stop_button = Gtk.Button.new_from_icon_name("process-stop-symbolic")
stop_button.set_valign(Gtk.Align.CENTER)
stop_button.get_style_context().add_class("circular")
stop_button.connect("clicked", on_stop_clicked, self, download_item)
button_box.append(stop_button)

Expand All @@ -70,7 +72,7 @@ def create_actionrow(self, filename):
box_2.append(progress_bar)

self.download_list.append(download_item)
download_item.index = len(self.downloads)-1
download_item.index = len(self.downloads)

return [progress_bar, speed_label, self.pause_buttons[len(self.pause_buttons)-1], download_item, filename_label]

Expand All @@ -94,10 +96,9 @@ def on_pause_clicked(button, self, pause_button, download_item):

all_paused = True

for download_thread in self.downloads:
if (download_thread.download):
if (download_thread.download.is_paused) == False:
all_paused = False
for download_thread in self.downloads.copy():
if (download_thread.download) and (download_thread.is_alive()) and (download_thread.download.is_paused == False):
all_paused = False

if (all_paused == True):
self.all_paused = True
Expand All @@ -107,7 +108,7 @@ def on_pause_clicked(button, self, pause_button, download_item):

def on_stop_clicked(button, self, download_item):
index = download_item.index
download_thread = self.downloads[index+1]
download_thread = self.downloads[index]
deletefiles = True
if (download_thread.download) and ((download_thread.download.is_torrent) and (download_thread.download.seeder)):
deletefiles = False
Expand Down
72 changes: 63 additions & 9 deletions src/download/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@
import requests
import json
import gi
from gi.repository import GLib
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, GLib, Gio
from download.actionrow import create_actionrow
from download.thread import DownloadThread
import threading
import string
import random
import textwrap

def listen_to_aria2(self, variaapp):
if not (self.terminating) and not (self.shutdown_dialog_raised):
currently_downloading = False

def listen_to_aria2(self):
if not self.terminating:
for frontend_download_item in self.downloads.copy():
if ( (frontend_download_item.download) and ( ((frontend_download_item.download.is_metadata) or (frontend_download_item.download.name.endswith(".torrent"))) and (frontend_download_item.download.is_complete) ) ):
frontend_download_item.cancelled = True
frontend_download_item.stop(True)
self.download_list.remove(frontend_download_item.actionrow)
self.downloads.remove(frontend_download_item)
if frontend_download_item.download:
if (frontend_download_item.is_alive()):
currently_downloading = True
if ( ((frontend_download_item.download.is_metadata) or (frontend_download_item.download.name.endswith(".torrent"))) and (frontend_download_item.download.is_complete) ):
frontend_download_item.cancelled = True
frontend_download_item.stop(True)
self.download_list.remove(frontend_download_item.actionrow)
self.downloads.remove(frontend_download_item)

try:
downloads_in_frontend = set(download_item.download.gid for download_item in self.downloads.copy())
Expand All @@ -26,7 +36,15 @@ def listen_to_aria2(self):
except:
pass

GLib.timeout_add(2000, listen_to_aria2, self)
if currently_downloading == True:
self.shutdown_action.set_enabled(True)
else:
self.shutdown_action.set_enabled(False)
if (self.shutdown_dialog_raised == False) and (self.shutdown_mode == True):
self.shutdown_dialog_raised = True
raise_shutdown_dialog(self, variaapp)

GLib.timeout_add(2000, listen_to_aria2, self, variaapp)

def add_download_to_ui(self, download_item_to_be_added):
if download_item_to_be_added.is_torrent:
Expand All @@ -39,3 +57,39 @@ def add_download_to_ui(self, download_item_to_be_added):
self.downloads.append(download_thread)
download_thread.start()
download_thread.pause_button.show()

def raise_shutdown_dialog(variamain, variaapp):
while (True):
shutdown_id = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10))
if (shutdown_id != variamain.shutdown_id):
variamain.shutdown_id = shutdown_id
break

notification = Gio.Notification.new(_("Warning"))
notification.set_body(_("Varia is about to shut down your computer."))
variaapp.send_notification(None, notification)

dialog = Adw.AlertDialog()
dialog.set_body(textwrap.fill(_("Varia is about to shut down your computer.") + " " + _("Press Cancel to cancel and disable."), 50))
dialog.add_response("cancel", _("Cancel"))
dialog.set_default_response("cancel")
dialog.set_close_response("cancel")
dialog.connect("response", shutdown_dialog_cancel_pressed, variamain, variaapp)
dialog.present(variamain)

GLib.timeout_add(30000, initiate_shutdown, variamain, shutdown_id)

def shutdown_dialog_cancel_pressed(dialog, response_id, variamain, variaapp):
variamain.shutdown_dialog_raised = False
variamain.shutdown_mode = False
variamain.sidebar_remote_mode_label.set_text("")
GLib.timeout_add(2000, listen_to_aria2, variamain, variaapp)

def initiate_shutdown(variamain, shutdown_id):
if (variamain.shutdown_dialog_raised == True) and (shutdown_id == variamain.shutdown_id):
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
proxy = Gio.DBusProxy.new_sync(bus, Gio.DBusProxyFlags.NONE, None,
'org.freedesktop.login1', '/org/freedesktop/login1',
'org.freedesktop.login1.Manager', None)
proxy.call_sync('PowerOff', GLib.Variant('(b)', (True,)), Gio.DBusCallFlags.NONE, -1, None)
exit()
4 changes: 4 additions & 0 deletions src/initiate.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ def initiate(self):
self.pause_buttons = []
self.all_paused = False

self.shutdown_mode = False
self.shutdown_dialog_raised = False
self.shutdown_id = ""

def on_dialog_dismiss(dialog, response_id):
dialog.destroy()
2 changes: 1 addition & 1 deletion src/variamain.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, variaapp, appdir, appconf, aria2c_subprocess, *args, **kwargs
set_aria2c_download_simultaneous_amount(self)

# Listen to aria2c:
thread = threading.Thread(target=listen_to_aria2(self))
thread = threading.Thread(target=listen_to_aria2(self, variaapp))
thread.start()

# Load incomplete downloads:
Expand Down
20 changes: 14 additions & 6 deletions src/window/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
gi.require_version('Adw', '1')
from gi.repository import Gtk, Adw, GLib, Gio
from gettext import gettext as _
import textwrap

from window.preferences import show_preferences
from download.actionrow import on_download_clicked
Expand Down Expand Up @@ -41,10 +42,10 @@ def window_create_sidebar(self, variaapp, DownloadThread, variaVersion):
downloads_folder_action.connect("activate", show_about, self, variaVersion)
variaapp.add_action(downloads_folder_action)

shutdown_action = Gio.SimpleAction.new("shutdown_on_completion", None)
shutdown_action.connect("activate", shutdown_on_completion, self)
shutdown_action.set_enabled(False)
variaapp.add_action(shutdown_action)
self.shutdown_action = Gio.SimpleAction.new("shutdown_on_completion", None)
self.shutdown_action.connect("activate", shutdown_on_completion, self)
self.shutdown_action.set_enabled(False)
variaapp.add_action(self.shutdown_action)

hamburger_menu_item_background = Gio.MenuItem.new(_("Background Mode"), "app.background_mode")
if (os.name != 'nt'):
Expand Down Expand Up @@ -119,9 +120,10 @@ def window_create_sidebar(self, variaapp, DownloadThread, variaVersion):
sidebar_expanding_box = Gtk.Box()
Gtk.Widget.set_vexpand(sidebar_expanding_box, True)

self.sidebar_shutdown_mode_label = Gtk.Label()
self.sidebar_remote_mode_label = Gtk.Label()
if (self.appconf['remote'] == '1'):
self.sidebar_remote_mode_label.set_text(_("Remote Mode"))
self.sidebar_remote_mode_label.set_text(textwrap.fill(_("Remote Mode"), 23))
self.sidebar_speed_limited_label = Gtk.Label()

sidebar_content_box.set_margin_start(6)
Expand All @@ -139,6 +141,7 @@ def window_create_sidebar(self, variaapp, DownloadThread, variaVersion):
sidebar_content_box.append(sidebar_separator)
sidebar_content_box.append(sidebar_filter_buttons_box)
sidebar_content_box.append(sidebar_expanding_box)
sidebar_content_box.append(self.sidebar_shutdown_mode_label)
sidebar_content_box.append(self.sidebar_remote_mode_label)
sidebar_content_box.append(self.sidebar_speed_limited_label)
sidebar_box.append(sidebar_content_box)
Expand Down Expand Up @@ -190,4 +193,9 @@ def open_downloads_folder(self, app, variaapp, appconf):
subprocess.Popen(["xdg-open", appconf["download_directory"]])

def shutdown_on_completion(self, app, variaapp):
print("sus")
if (variaapp.shutdown_mode == False):
variaapp.shutdown_mode = True
variaapp.sidebar_remote_mode_label.set_text(textwrap.fill(_("Shutdown on Completion"), 23))
else:
variaapp.shutdown_mode = False
variaapp.sidebar_remote_mode_label.set_text("")

0 comments on commit 4f4af13

Please sign in to comment.