def __real_update_account(self, acc): if acc in self.accounts_checking: logger.warn("The account %s is being checked" % (acc.get_name())) return logger.debug("Starting checker") if not acc.get_active(): logger.debug("The account %s is not active, it will not be updated" % (acc.get_name())) return self.accounts_checking.append(acc) max_notifications = int(float(self.config.get_prefs()["max_notifications"])) try: logger.debug('Updating account: ' + acc.get_name()) #Process events to show the main icon while gtk.events_pending(): gtk.main_iteration(False) self.am.update_account(acc) acc.error_notified = False if hasattr(acc, "indicator"): self.im.get_indicator().update_account(acc) #Process events to show the indicator menu while gtk.events_pending(): gtk.main_iteration(False) if acc.get_provider().has_notifications() and \ acc.get_show_notifications(): nots = acc.get_new_unread_notifications() message = None if len(nots) > max_notifications: notification.notify(acc.get_name(), _("New messages: ") + str(len(nots)), acc.get_icon()) if len(nots) > 0 and len(nots) <= max_notifications: for n in nots: if n.icon: icon = n.icon else: icon = acc.get_icon() notification.notify(acc.get_name() + ": " + n.sender, n.message, icon) self.emit("account-checked", acc) except notification.NotificationError, ne: logger.exception("Error trying to notify with libnotify: %s", e)
def _start_idle(self): try: check_auth_configuration() self.nm.set_statechange_callback(self.on_nm_state_changed) self.set_active (True) self.update_accounts() self.started = True except Exception, e: logger.exception ("Error starting the application: %s", e) try: notification.notify(_("Application Error"), _("Error starting the application: %s") % (str(e)), utils.get_error_pixbuf()) except Exception, e: logger.exception ("Error notifying the error: %s", e)
class IndicatorManager(): __default = None def __init__(self): if IndicatorManager.__default: raise IndicatorManager.__default self.indicator = None self.indicators = {} #TODO Use the correct indicator from cloudsn.ui.indicators import statusicon statusindi = statusicon.StatusIconIndicator() self.indicators[statusindi.get_name()] = statusindi try: from cloudsn.ui.indicators import indicatorapplet indi = indicatorapplet.IndicatorApplet() self.indicators[indi.get_name()] = indi except Exception, e: logger.exception( "The indicator applet provider cannot be loaded: %s", e) self.config = config.SettingsController.get_instance() indicator_conf = self.config.get_prefs()["indicator"] if indicator_conf: for name in self.indicators: if name == indicator_conf: self.indicator = self.indicators[name] break if not self.indicator: logger.error( "The indicator named %s is configured but it cannot be found" % (indicator_conf)) notification.notify( _("Indicator error"), _("The indicator named %s is configured but it cannot be found" ) % (indicator_conf), utils.get_error_pixbuf()) if not self.indicator: self.indicator = statusindi self.indicator.set_active(True)
class Controller (gobject.GObject): __default = None __gtype_name__ = "Controller" __gsignals__ = { "account-checked" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,)), "account-check-error" : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_PYOBJECT,))} timeout_id = -1 interval = 60 def __init__(self): if Controller.__default: raise Controller.__default #Prevent various instances import os, fcntl, sys, tempfile, getpass self.lockfile = os.path.normpath(tempfile.gettempdir() + '/cloudsn-'+getpass.getuser()+'.lock') self.fp = open(self.lockfile, 'w') try: fcntl.lockf(self.fp, fcntl.LOCK_EX | fcntl.LOCK_NB) except IOError: message = _("Another instance is already running, close it first.") logger.warn (message) print message sys.exit(-1) gobject.GObject.__init__(self) self.started = False self.config = config.SettingsController.get_instance() self.config.connect("value-changed", self._settings_changed) self.prov_manager = ProviderManager.get_instance() self.im = indicator.IndicatorManager.get_instance() self.am = account.AccountManager.get_instance() self.am.connect("account-added", self._account_added_cb) self.am.connect("account-deleted", self._account_deleted_cb) self.am.connect("account-changed", self._account_changed_cb) self.am.connect("account-active-changed", self._account_active_cb) self.am.load_accounts() self.accounts_checking = [] self.nm = networkmanager.NetworkManager() @staticmethod def get_instance(): if not Controller.__default: Controller.__default = Controller() return Controller.__default def _account_added_cb(self, am, acc): indi = self.im.get_indicator() if indi and acc.get_active(): indi.create_indicator(acc) if self.started: self.update_account(acc) def _account_deleted_cb(self, am, acc): self.im.get_indicator().remove_indicator(acc) def _account_active_cb(self, am, acc): if acc.get_active(): self.im.get_indicator().create_indicator(acc) self.update_account(acc) else: self.im.get_indicator().remove_indicator(acc) def _account_changed_cb (self, am, acc): self.update_account(acc) def _settings_changed(self, config, section, key, value): if section == "preferences" and key == "minutes": self._update_interval() def _update_interval(self): old = self.interval self.interval = int(float(self.config.get_prefs()["minutes"]) * 60) if not self.get_active(): return if self.timeout_id < 0: logger.debug("new source: "+str(self.timeout_id)) self.timeout_id = gobject.timeout_add_seconds(self.interval, self.update_accounts, None) elif self.interval != old: logger.debug("removed source: "+str(self.timeout_id)) gobject.source_remove(self.timeout_id) logger.debug("restart source: "+str(self.timeout_id)) self.timeout_id = gobject.timeout_add_seconds(self.interval, self.update_accounts, None) def on_nm_state_changed (self): if self.nm.state == networkmanager.STATE_CONNECTED: logger.debug("Network connected") #Force update self.update_accounts() else: logger.debug("Network disconnected") def set_active(self, active): if active and not self.get_active(): self.timeout_id = gobject.timeout_add_seconds(self.interval, self.update_accounts, None) logger.debug("activated source: "+str(self.timeout_id)) elif not active and self.get_active(): gobject.source_remove(self.timeout_id) logger.debug("deactivated source "+str(self.timeout_id)) self.timeout_id = -1 def get_active(self): return self.timeout_id > -1 def update_account(self, acc): if not self.get_active(): return """acc=None will check all accounts""" if self.nm.offline(): logger.warn ("The network is not connected, the account cannot be updated") return #TODO Check if the updater is running if acc is None: for acc in self.am.get_accounts(): thread.start_new_thread(self.__real_update_account, (acc,)) else: thread.start_new_thread(self.__real_update_account, (acc,)) #self.__real_update_account(acc) def __real_update_account(self, acc): if acc in self.accounts_checking: logger.warn("The account %s is being checked" % (acc.get_name())) return logger.debug("Starting checker") if not acc.get_active(): logger.debug("The account %s is not active, it will not be updated" % (acc.get_name())) return self.accounts_checking.append(acc) max_notifications = int(float(self.config.get_prefs()["max_notifications"])) try: logger.debug('Updating account: ' + acc.get_name()) #Process events to show the main icon while gtk.events_pending(): gtk.main_iteration(False) self.am.update_account(acc) acc.error_notified = False if hasattr(acc, "indicator"): self.im.get_indicator().update_account(acc) #Process events to show the indicator menu while gtk.events_pending(): gtk.main_iteration(False) if acc.get_provider().has_notifications() and \ acc.get_show_notifications(): nots = acc.get_new_unread_notifications() message = None if len(nots) > max_notifications: notification.notify(acc.get_name(), _("New messages: ") + str(len(nots)), acc.get_icon()) if len(nots) > 0 and len(nots) <= max_notifications: for n in nots: if n.icon: icon = n.icon else: icon = acc.get_icon() notification.notify(acc.get_name() + ": " + n.sender, n.message, icon) self.emit("account-checked", acc) except notification.NotificationError, ne: logger.exception("Error trying to notify with libnotify: %s", e) except Exception, e: logger.exception("Error trying to update the account %s: %s", acc.get_name(), e) if not acc.error_notified: acc.error_notified = True notification.notify (_("Error checking account %s") % (acc.get_name()), str(e), acc.get_icon()) self.im.get_indicator().update_error(acc) self.emit("account-check-error", acc)
indi_messagingmenu = messagingmenu.IndicatorApplet() self.indicators[indi_messagingmenu.get_name()] = indi_messagingmenu except Exception,e: logger.exception("The message menu applet provider cannot be loaded: %s", e) self.config = config.SettingsController.get_instance() indicator_conf = self.config.get_prefs()["indicator"] if indicator_conf: for name in self.indicators: if name == indicator_conf: self.indicator = self.indicators[name] break if not self.indicator: logger.error("The indicator named %s is configured but it cannot be found" % (indicator_conf)) notification.notify (_("Indicator error"), _("The indicator named %s is configured but it cannot be found") % (indicator_conf), utils.get_error_pixbuf()) if not self.indicator: if "DESKTOP_SESSION" in os.environ and os.environ["DESKTOP_SESSION"] == 'ubuntu': indi_fin = indi_messagingmenu if indi_messagingmenu else indi_indicator if not indi_fin: notification.notify (_("Indicator error"), _("The indicator for ubuntu cannot be loaded "), utils.get_error_pixbuf()) raise Error(_("The indicator for ubuntu cannot be loaded ")) self.indicator = indi_fin else: self.indicator = indi_statusicon self.indicator.set_active(True)
logger.exception( "The message menu applet provider cannot be loaded: %s", e) self.config = config.SettingsController.get_instance() indicator_conf = self.config.get_prefs()["indicator"] if indicator_conf: for name in self.indicators: if name == indicator_conf: self.indicator = self.indicators[name] break if not self.indicator: logger.error( "The indicator named %s is configured but it cannot be found" % (indicator_conf)) notification.notify( _("Indicator error"), _("The indicator named %s is configured but it cannot be found" ) % (indicator_conf), utils.get_error_pixbuf()) if not self.indicator: if "DESKTOP_SESSION" in os.environ and os.environ[ "DESKTOP_SESSION"] == 'ubuntu': indi_fin = indi_messagingmenu if indi_messagingmenu else indi_indicator if not indi_fin: notification.notify( _("Indicator error"), _("The indicator for ubuntu cannot be loaded "), utils.get_error_pixbuf()) raise Error( _("The indicator for ubuntu cannot be loaded ")) self.indicator = indi_fin else: self.indicator = indi_statusicon