AccountsManager: avoid using uneeded threads

This commit is contained in:
Bilal Elmoussaoui 2019-02-13 00:12:56 +01:00
parent b7694d94c6
commit 34694833eb
3 changed files with 12 additions and 12 deletions

View file

@ -64,10 +64,10 @@ class Application(Gtk.Application):
Application.__setup_css()
# Set the default night mode
is_night_mode = Settings.get_default().is_night_mode
gtk_settings = Gtk.Settings.get_default()
gtk_settings.set_property("gtk-application-prefer-dark-theme",
is_night_mode)
Settings.get_default().bind("night-mode", gtk_settings,
"gtk-application-prefer-dark-theme",
Gio.SettingsBindFlags.GET)
@staticmethod
def __setup_css():

View file

@ -18,25 +18,24 @@
"""
from threading import Thread
from time import sleep
from gi.repository import GObject
from gi.repository import GObject, GLib
class AccountsManager(GObject.GObject, Thread):
class AccountsManager(GObject.GObject):
__gsignals__ = {
'counter_updated': (GObject.SignalFlags.RUN_LAST, None, (str,)),
'counter_updated': (GObject.SignalFlags.RUN_LAST, None, (int,)),
}
instance = None
def __init__(self):
GObject.GObject.__init__(self)
Thread.__init__(self)
self._accounts = []
self._alive = True
self.__fill_accounts()
self.counter_max = 30
self.counter = self.counter_max
self.start()
GLib.timeout_add_seconds(1, self.__update_counter, None)
@staticmethod
def get_default():
@ -64,14 +63,15 @@ class AccountsManager(GObject.GObject, Thread):
else:
child.emit(signal)
def run(self):
while self._alive:
def __update_counter(self, *args):
if self._alive:
self.counter -= 1
if self.counter == 0:
self.counter = self.counter_max
self.update_childes("otp_out_of_date")
self.emit("counter_updated", self.counter)
sleep(1)
return True
return False
def __fill_accounts(self):
from .database import Database

View file

@ -139,7 +139,7 @@ class AccountsWidget(Gtk.Box, GObject.GObject):
self.show_all()
def _on_counter_updated(self, accounts_manager, counter):
counter_fraction = int(counter) / accounts_manager.counter_max
counter_fraction = counter / accounts_manager.counter_max
self.otp_progress_bar.set_fraction(counter_fraction)
self.otp_progress_bar.set_tooltip_text(
_("The One-Time Passwords expires in {} seconds").format(counter))