flashing_lock.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import subprocess
  2. from gi.repository import Gtk
  3. from gi.repository import GObject
  4. from gajim.common import app
  5. from gajim.plugins import GajimPlugin
  6. from gajim.plugins.helpers import log_calls
  7. from gajim.plugins.gui import GajimPluginConfigDialog
  8. # Since Gajim 1.1.0 _() has to be imported
  9. try:
  10. from gajim.common.i18n import _
  11. except ImportError:
  12. pass
  13. class FlashingLock(GajimPlugin):
  14. @log_calls('FlashingLock')
  15. def init(self):
  16. self.description = _('Flashing keyboard led when there are unread messages.')
  17. self.config_dialog = FlashingLockPluginConfigDialog(self)
  18. self.config_default_values = {
  19. 'command1': ("xset led named 'Scroll Lock'", ''),
  20. 'command2': ("xset -led named 'Scroll Lock'", ''),
  21. 'flash': (True, ''),
  22. 'group_cb': (True, ''),
  23. 'group': ("Defaul group", ''),
  24. }
  25. self.contact_group_written = None
  26. self.is_active = None
  27. self.timeout = 500
  28. self.timeout_off = int(self.timeout / 2)
  29. self.id_0 = None
  30. def on_event_added(self, event):
  31. group_found = False
  32. if event.show_in_systray:
  33. # Check if event is send by user in specific group
  34. accounts_for_contact = app.contacts.get_accounts()
  35. found = False
  36. contact = ""
  37. for account in accounts_for_contact:
  38. if not found:
  39. try:
  40. contact = app.contacts.get_first_contact_from_jid(account, event.jid)
  41. found = True
  42. except Exception:
  43. print("contact not found")
  44. if found:
  45. contact_groups = contact.groups
  46. for group in contact_groups:
  47. if group == self.config['group']:
  48. group_found = True
  49. if group_found:
  50. self.flash_trigger(True)
  51. def on_event_removed(self, event_list):
  52. '''
  53. If there's an event sent by a user in specific group,
  54. do not stop flash trigger
  55. If there isn't, stop it
  56. '''
  57. remove = True
  58. for account in app.events.get_systray_events():
  59. for jid in app.events.get_systray_events()[account]:
  60. accounts_for_contact = app.contacts.get_accounts()
  61. found = False
  62. contact = ""
  63. try:
  64. contact = app.contacts.get_first_contact_from_jid(account, jid)
  65. found = True
  66. except Exception:
  67. print("contact not found")
  68. if found:
  69. contact_groups = contact.groups
  70. for group in contact_groups:
  71. if group == self.config['group']:
  72. remove = False
  73. if remove:
  74. self.flash_trigger(False)
  75. def flash_trigger(self, activate):
  76. if activate:
  77. if self.id_0:
  78. return
  79. if self.config['flash']:
  80. self.id_0 = GObject.timeout_add(self.timeout, self.led_on)
  81. else:
  82. self.led_on()
  83. self.id_0 = True
  84. else:
  85. if self.id_0:
  86. if self.config['flash']:
  87. GObject.source_remove(self.id_0)
  88. self.id_0 = None
  89. self.led_off()
  90. def led_on(self):
  91. subprocess.Popen('%s' % self.config['command1'], shell=True).wait()
  92. if self.config['flash']:
  93. GObject.timeout_add(self.timeout_off, self.led_off)
  94. return True
  95. def led_off(self):
  96. subprocess.Popen('%s' % self.config['command2'], shell=True).wait()
  97. @log_calls('FlashingLock')
  98. def activate(self):
  99. app.events.event_added_subscribe(self.on_event_added)
  100. app.events.event_removed_subscribe(self.on_event_removed)
  101. if app.events.get_nb_systray_events():
  102. if self.config['flash']:
  103. self.id_0 = GObject.timeout_add(self.timeout, self.led_on)
  104. else:
  105. self.led_on()
  106. self.id_0 = True
  107. @log_calls('FlashingLock')
  108. def deactivate(self):
  109. app.events.event_added_unsubscribe(self.on_event_added)
  110. app.events.event_removed_unsubscribe(self.on_event_removed)
  111. self.led_off()
  112. if self.id_0:
  113. GObject.source_remove(self.id_0)
  114. class FlashingLockPluginConfigDialog(GajimPluginConfigDialog):
  115. def init(self):
  116. self.GTK_BUILDER_FILE_PATH = self.plugin.local_file_path(
  117. 'config_dialog.ui')
  118. self.xml = Gtk.Builder()
  119. self.xml.set_translation_domain('gajim_plugins')
  120. self.xml.add_objects_from_file(self.GTK_BUILDER_FILE_PATH,
  121. ['config_table'])
  122. config_table = self.xml.get_object('config_table')
  123. self.get_child().pack_start(config_table, True, True, 0)
  124. self.xml.connect_signals(self)
  125. def on_run(self):
  126. self.isactive = self.plugin.active
  127. if self.plugin.active:
  128. app.plugin_manager.deactivate_plugin(self.plugin)
  129. for name in ('command1', 'command2', 'group'):
  130. widget = self.xml.get_object(name)
  131. widget.set_text(self.plugin.config[name])
  132. widget = self.xml.get_object('flash_cb')
  133. widget.set_active(not self.plugin.config['flash'])
  134. widget = self.xml.get_object('group_cb')
  135. widget.set_active(self.plugin.config['group_cb'])
  136. def on_close_button_clicked(self, widget):
  137. widget2 = self.xml.get_object('command1')
  138. self.plugin.config['command1'] = widget2.get_text()
  139. widget2 = self.xml.get_object('command2')
  140. self.plugin.config['command2'] = widget2.get_text()
  141. widget2 = self.xml.get_object('group_cb')
  142. self.plugin.config['group_cb'] = widget2.get_active()
  143. if widget2.get_active():
  144. widget2 = self.xml.get_object('group')
  145. self.plugin.config['group'] = widget2.get_text()
  146. widget2 = self.xml.get_object('flash_cb')
  147. self.plugin.config['flash'] = not widget2.get_active()
  148. if self.isactive:
  149. app.plugin_manager.activate_plugin(self.plugin)
  150. GajimPluginConfigDialog.on_close_button_clicked(self, widget2)