Commit b9686472 authored by Dan Pascu's avatar Dan Pascu

Refactored some code

parent 3f778101
...@@ -51,6 +51,8 @@ class MainWindow(base_class, ui_class): ...@@ -51,6 +51,8 @@ class MainWindow(base_class, ui_class):
notification_center.add_observer(self, name='SIPAccountGotPendingWatcher') notification_center.add_observer(self, name='SIPAccountGotPendingWatcher')
notification_center.add_observer(self, sender=AccountManager()) notification_center.add_observer(self, sender=AccountManager())
icon_manager = IconManager()
self.pending_watcher_dialogs = [] self.pending_watcher_dialogs = []
self.mwi_icons = [QIcon(Resources.get('icons/mwi-%d.png' % i)) for i in xrange(0, 11)] self.mwi_icons = [QIcon(Resources.get('icons/mwi-%d.png' % i)) for i in xrange(0, 11)]
...@@ -65,7 +67,7 @@ class MainWindow(base_class, ui_class): ...@@ -65,7 +67,7 @@ class MainWindow(base_class, ui_class):
self.default_icon_path = Resources.get('icons/default-avatar.png') self.default_icon_path = Resources.get('icons/default-avatar.png')
self.default_icon = QIcon(self.default_icon_path) self.default_icon = QIcon(self.default_icon_path)
self.last_icon_directory = os.path.expanduser('~') self.last_icon_directory = os.path.expanduser('~')
self.set_user_icon(IconManager().get('avatar')) self.set_user_icon(icon_manager.get('avatar'))
self.active_sessions_label.hide() self.active_sessions_label.hide()
self.enable_call_buttons(False) self.enable_call_buttons(False)
...@@ -369,31 +371,31 @@ class MainWindow(base_class, ui_class): ...@@ -369,31 +371,31 @@ class MainWindow(base_class, ui_class):
self.activity_note.setEnabled(True) self.activity_note.setEnabled(True)
if not self.account_state.state.internal: if not self.account_state.state.internal:
self.saved_account_state = None self.saved_account_state = None
settings = BlinkSettings() blink_settings = BlinkSettings()
settings.presence.current_state = PresenceState(self.account_state.state, self.account_state.note) blink_settings.presence.current_state = PresenceState(self.account_state.state, self.account_state.note)
settings.presence.state_history = [PresenceState(state, note) for state, note in self.account_state.history] blink_settings.presence.state_history = [PresenceState(state, note) for state, note in self.account_state.history]
settings.save() blink_settings.save()
def _SH_AccountStateClicked(self, checked): def _SH_AccountStateClicked(self, checked):
filename = QFileDialog.getOpenFileName(self, u'Select Icon', self.last_icon_directory, u"Images (*.png *.tiff *.jpg *.xmp *.svg)") filename = QFileDialog.getOpenFileName(self, u'Select Icon', self.last_icon_directory, u"Images (*.png *.tiff *.jpg *.xmp *.svg)")
if filename: if filename:
self.last_icon_directory = os.path.dirname(filename) self.last_icon_directory = os.path.dirname(filename)
filename = filename if os.path.realpath(filename) != os.path.realpath(self.default_icon_path) else None filename = filename if os.path.realpath(filename) != os.path.realpath(self.default_icon_path) else None
settings = BlinkSettings() blink_settings = BlinkSettings()
icon_manager = IconManager() icon_manager = IconManager()
if filename is not None: if filename is not None:
icon = icon_manager.store_file('avatar', filename) icon = icon_manager.store_file('avatar', filename)
try: icon_contents = icon_manager.get_image('avatar')
hash = hashlib.sha512(open(icon.filename).read()).hexdigest() if icon_contents is not None:
except Exception: hash = hashlib.sha512(icon_contents).hexdigest()
settings.presence.icon = None blink_settings.presence.icon = IconDescriptor('file://' + icon.filename, hash)
else: else:
settings.presence.icon = IconDescriptor('file://' + icon.filename, hash) icon_manager.remove('avatar')
blink_settings.presence.icon = None
else: else:
icon_manager.remove('avatar') icon_manager.remove('avatar')
icon = None blink_settings.presence.icon = None
settings.presence.icon = None blink_settings.save()
settings.save()
def _SH_ActivityNoteEditingFinished(self): def _SH_ActivityNoteEditingFinished(self):
self.activity_note.clearFocus() self.activity_note.clearFocus()
...@@ -610,10 +612,10 @@ class MainWindow(base_class, ui_class): ...@@ -610,10 +612,10 @@ class MainWindow(base_class, ui_class):
self.load_audio_devices() self.load_audio_devices()
notification.center.add_observer(self, name='CFGSettingsObjectDidChange') notification.center.add_observer(self, name='CFGSettingsObjectDidChange')
notification.center.add_observer(self, name='AudioDevicesDidChange') notification.center.add_observer(self, name='AudioDevicesDidChange')
settings = BlinkSettings() blink_settings = BlinkSettings()
self.account_state.history = [(item.state, item.note) for item in settings.presence.state_history] self.account_state.history = [(item.state, item.note) for item in blink_settings.presence.state_history]
state = getattr(AccountState, settings.presence.current_state.state, AccountState.Available) state = getattr(AccountState, blink_settings.presence.current_state.state, AccountState.Available)
self.account_state.setState(state, settings.presence.current_state.note) self.account_state.setState(state, blink_settings.presence.current_state.note)
def _NH_AudioDevicesDidChange(self, notification): def _NH_AudioDevicesDidChange(self, notification):
for action in self.output_device_menu.actions(): for action in self.output_device_menu.actions():
...@@ -640,6 +642,7 @@ class MainWindow(base_class, ui_class): ...@@ -640,6 +642,7 @@ class MainWindow(base_class, ui_class):
def _NH_CFGSettingsObjectDidChange(self, notification): def _NH_CFGSettingsObjectDidChange(self, notification):
settings = SIPSimpleSettings() settings = SIPSimpleSettings()
blink_settings = BlinkSettings() blink_settings = BlinkSettings()
icon_manager = IconManager()
if notification.sender is settings: if notification.sender is settings:
if 'audio.silent' in notification.data.modified: if 'audio.silent' in notification.data.modified:
self.silent_action.setChecked(settings.audio.silent) self.silent_action.setChecked(settings.audio.silent)
...@@ -672,7 +675,7 @@ class MainWindow(base_class, ui_class): ...@@ -672,7 +675,7 @@ class MainWindow(base_class, ui_class):
state = getattr(AccountState, blink_settings.presence.current_state.state, AccountState.Available) state = getattr(AccountState, blink_settings.presence.current_state.state, AccountState.Available)
self.account_state.setState(state, blink_settings.presence.current_state.note) self.account_state.setState(state, blink_settings.presence.current_state.note)
if 'presence.icon' in notification.data.modified: if 'presence.icon' in notification.data.modified:
self.set_user_icon(IconManager().get('avatar')) self.set_user_icon(icon_manager.get('avatar'))
if 'presence.offline_note' in notification.data.modified: if 'presence.offline_note' in notification.data.modified:
# TODO: set offline note -Saul # TODO: set offline note -Saul
pass pass
......
...@@ -158,29 +158,29 @@ class PresencePublicationHandler(object): ...@@ -158,29 +158,29 @@ class PresencePublicationHandler(object):
return doc return doc
def set_xcap_offline_note(self, account=None): def set_xcap_offline_note(self, account=None):
settings = BlinkSettings() blink_settings = BlinkSettings()
if not account: if not account:
account_manager = AccountManager() account_manager = AccountManager()
accounts = [account for account in account_manager.get_accounts() if hasattr(account, 'xcap') and account.xcap.discovered] accounts = [account for account in account_manager.get_accounts() if hasattr(account, 'xcap') and account.xcap.discovered]
else: else:
accounts = [account] accounts = [account]
for account in accounts: for account in accounts:
if settings.presence.offline_note: if blink_settings.presence.offline_note:
account.xcap_manager.set_offline_status(OfflineStatus(self.build_offline_pidf(account, settings.presence.offline_note))) account.xcap_manager.set_offline_status(OfflineStatus(self.build_offline_pidf(account, blink_settings.presence.offline_note)))
else: else:
account.xcap_manager.set_offline_status(None) account.xcap_manager.set_offline_status(None)
def set_xcap_icon(self, account=None): def set_xcap_icon(self, account=None):
settings = BlinkSettings() blink_settings = BlinkSettings()
if not account: if not account:
account_manager = AccountManager() account_manager = AccountManager()
accounts = [account for account in account_manager.get_accounts() if hasattr(account, 'xcap') and account.xcap.discovered] accounts = [account for account in account_manager.get_accounts() if hasattr(account, 'xcap') and account.xcap.discovered]
else: else:
accounts = [account] accounts = [account]
icon = None icon = None
if settings.presence.icon: if blink_settings.presence.icon:
try: try:
data = open(settings.presence.icon.url.path).read() data = open(blink_settings.presence.icon.url.path).read()
except Exception: except Exception:
pass pass
else: else:
...@@ -190,14 +190,14 @@ class PresencePublicationHandler(object): ...@@ -190,14 +190,14 @@ class PresencePublicationHandler(object):
@run_in_gui_thread @run_in_gui_thread
def _save_icon(self, icon_data, icon_hash): def _save_icon(self, icon_data, icon_hash):
settings = BlinkSettings() blink_settings = BlinkSettings()
if None not in (icon_data, icon_hash): if icon_data is not None is not icon_hash:
icon = IconManager().store_data('avatar', icon_data) icon_manager = IconManager()
if icon: icon = icon_manager.store_data('avatar', icon_data)
settings.presence.icon = IconDescriptor('file://' + icon.filename, icon_hash) blink_settings.presence.icon = IconDescriptor('file://' + icon.filename, icon_hash) if icon is not None else None
else: else:
settings.presence.icon = None blink_settings.presence.icon = None
settings.save() blink_settings.save()
@run_in_twisted_thread @run_in_twisted_thread
def handle_notification(self, notification): def handle_notification(self, notification):
...@@ -205,8 +205,7 @@ class PresencePublicationHandler(object): ...@@ -205,8 +205,7 @@ class PresencePublicationHandler(object):
handler(notification) handler(notification)
def _NH_CFGSettingsObjectDidChange(self, notification): def _NH_CFGSettingsObjectDidChange(self, notification):
settings = BlinkSettings() if notification.sender is BlinkSettings():
if notification.sender is settings:
if 'presence.offline_note' in notification.data.modified: if 'presence.offline_note' in notification.data.modified:
self.set_xcap_offline_note() self.set_xcap_offline_note()
if 'presence.icon' in notification.data.modified: if 'presence.icon' in notification.data.modified:
...@@ -248,7 +247,7 @@ class PresencePublicationHandler(object): ...@@ -248,7 +247,7 @@ class PresencePublicationHandler(object):
services = [service for service in pidf_doc.services if service.status.extended is not None] services = [service for service in pidf_doc.services if service.status.extended is not None]
if not services: if not services:
return return
settings = BlinkSettings() blink_settings = BlinkSettings()
services.sort(key=lambda obj: obj.timestamp.value if obj.timestamp else epoch, reverse=True) services.sort(key=lambda obj: obj.timestamp.value if obj.timestamp else epoch, reverse=True)
service = services[0] service = services[0]
if service.id in ('SID-%s' % uuid.UUID(SIPSimpleSettings().instance_id), 'SID-%s' % hashlib.md5(notification.sender.id).hexdigest()): if service.id in ('SID-%s' % uuid.UUID(SIPSimpleSettings().instance_id), 'SID-%s' % hashlib.md5(notification.sender.id).hexdigest()):
...@@ -260,15 +259,15 @@ class PresencePublicationHandler(object): ...@@ -260,15 +259,15 @@ class PresencePublicationHandler(object):
status = 'Invisible' status = 'Invisible'
note = None note = None
new_state = PresenceState(status, note) new_state = PresenceState(status, note)
settings.presence.current_state = new_state blink_settings.presence.current_state = new_state
if new_state.note: if new_state.note:
try: try:
state = next(state for state in settings.presence.state_history if state==new_state) state = next(state for state in blink_settings.presence.state_history if state==new_state)
except StopIteration: except StopIteration:
settings.presence.state_history = [new_state] + settings.presence.state_history blink_settings.presence.state_history = [new_state] + blink_settings.presence.state_history
else: else:
settings.presence.state_history = [new_state] + [state for state in settings.presence.state_history if state!=new_state] blink_settings.presence.state_history = [new_state] + [state for state in blink_settings.presence.state_history if state!=new_state]
settings.save() blink_settings.save()
def _NH_XCAPManagerDidDiscoverServerCapabilities(self, notification): def _NH_XCAPManagerDidDiscoverServerCapabilities(self, notification):
account = notification.sender.account account = notification.sender.account
...@@ -277,7 +276,7 @@ class PresencePublicationHandler(object): ...@@ -277,7 +276,7 @@ class PresencePublicationHandler(object):
def _NH_XCAPManagerDidReloadData(self, notification): def _NH_XCAPManagerDidReloadData(self, notification):
account = notification.sender.account account = notification.sender.account
settings = BlinkSettings() blink_settings = BlinkSettings()
offline_status = notification.data.offline_status offline_status = notification.data.offline_status
status_icon = notification.data.status_icon status_icon = notification.data.status_icon
...@@ -293,13 +292,13 @@ class PresencePublicationHandler(object): ...@@ -293,13 +292,13 @@ class PresencePublicationHandler(object):
else: else:
offline_note = unicode(note) offline_note = unicode(note)
settings.presence.offline_note = offline_note blink_settings.presence.offline_note = offline_note
settings.save() blink_settings.save()
if status_icon: if status_icon:
icon_desc = IconDescriptor(notification.sender.status_icon.uri, notification.sender.status_icon.etag) icon_desc = IconDescriptor(notification.sender.status_icon.uri, notification.sender.status_icon.etag)
icon_hash = hashlib.sha512(status_icon.data).hexdigest() icon_hash = hashlib.sha512(status_icon.data).hexdigest()
if settings.presence.icon and settings.presence.icon.etag == icon_hash: if blink_settings.presence.icon and blink_settings.presence.icon.etag == icon_hash:
# Icon didn't change # Icon didn't change
pass pass
else: else:
...@@ -509,8 +508,9 @@ class PendingWatcherDialog(base_class, ui_class): ...@@ -509,8 +508,9 @@ class PendingWatcherDialog(base_class, ui_class):
self.contact = None self.contact = None
else: else:
display_name = self.contact.name display_name = self.contact.name
icon = IconManager().get(self.contact.id) icon_manager = IconManager()
if icon: icon = icon_manager.get(self.contact.id)
if icon is not None:
self.user_icon.setPixmap(icon.pixmap(32)) self.user_icon.setPixmap(icon.pixmap(32))
self.account_label.setText(u'For account %s' % account.id) self.account_label.setText(u'For account %s' % account.id)
self.name_label.setText(display_name or uri) self.name_label.setText(display_name or uri)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment