mozchecker.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-"
  3. # vim: set expandtab tabstop=4 shiftwidth=4:
  4. """
  5. $Id$
  6. This file is part of the xsser project, http://xsser.03c8.net
  7. Copyright (c) 2011/2016 psy <epsylon@riseup.net>
  8. xsser is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation version 3 of the License.
  11. xsser is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  13. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  14. details.
  15. You should have received a copy of the GNU General Public License along
  16. with xsser; if not, write to the Free Software Foundation, Inc., 51
  17. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. """
  19. import gtk
  20. import sys
  21. import gobject
  22. import subprocess
  23. from threading import Thread
  24. try:
  25. from gtkmozembed import MozEmbed
  26. except:
  27. MozEmbed = None
  28. import webbrowser
  29. class CheckerThread(Thread):
  30. def __init__(self, parent, url):
  31. Thread.__init__(self)
  32. self.daemon = True
  33. self._armed = True
  34. self._url = url
  35. self._parent = parent
  36. def shutdown(self):
  37. if self.result:
  38. self._armed = False
  39. self.result.terminate()
  40. def run(self):
  41. self.result = subprocess.Popen([sys.executable, __file__, self._url],
  42. stderr=subprocess.PIPE)
  43. self.result.wait()
  44. if self._armed:
  45. self._parent.on_net_stop()
  46. self.result = None
  47. class MozChecker(object):
  48. def __init__(self, parent):
  49. self._busy = False
  50. self._urlqueue = []
  51. self._parent = parent
  52. self._armed = True
  53. if MozEmbed:
  54. pass
  55. else:
  56. self.open = self.open_webbrowser
  57. def remaining(self):
  58. return len(self._urlqueue)
  59. def init_mozembed(self):
  60. self.moz = MozEmbed()
  61. self.moz.connect('net-stop', self.on_net_stop)
  62. self.moz.connect('net-state', self.on_net_state)
  63. self.moz.connect('new-window', self.on_new_window)
  64. self.add(self.moz)
  65. self.moz.show()
  66. def on_new_window(self, widget, retval, chromemask):
  67. print("new window")
  68. print(widget, retval, chromemask)
  69. return False
  70. def open_webbrowser(self, url):
  71. webbrowser.open(url, 2, False)
  72. def open_job(self, url):
  73. if self._parent:
  74. self._parent.start_token_check(url)
  75. self._busy = CheckerThread(self, url)
  76. self._busy.start()
  77. def shutdown(self):
  78. if self._busy:
  79. self._armed = False
  80. self._busy.shutdown()
  81. self._busy.join()
  82. def open(self, url):
  83. if not self._busy:
  84. self.open_job(url)
  85. else:
  86. self._urlqueue.append(url)
  87. def on_js_status(self, widget):
  88. widget.get_js_status()
  89. def on_net_state(self, widget, flags, status):
  90. print("net_state", widget, flags, status)
  91. def on_net_stop(self, widget=None):
  92. gtk.gdk.threads_enter()
  93. gobject.timeout_add(0, self.process_next)
  94. gtk.gdk.threads_leave()
  95. def process_next(self):
  96. if self._urlqueue and self._armed:
  97. next_url = self._urlqueue.pop(0)
  98. self.open_job(next_url)
  99. else:
  100. self._busy = False
  101. if __name__ == '__main__':
  102. win = gtk.Window()
  103. def finished(widget):
  104. gtk.main_quit()
  105. def alertkill():
  106. for a in gtk.window_list_toplevels():
  107. if a.get_title() and (a.get_title() == 'Alert' or 'says' in a.get_title() or 'Warning' in a.get_title()):
  108. print(a.get_children())
  109. a.hide()
  110. a.destroy()
  111. gtk.main_quit()
  112. gobject.timeout_add(100, alertkill)
  113. def bailout():
  114. gtk.main_quit()
  115. sys.exit()
  116. def unmap(widget):
  117. widget.hide()
  118. def new_window(widget, retval, mask):
  119. print("new window!!")
  120. gobject.timeout_add(30000, bailout)
  121. gobject.timeout_add(100, alertkill)
  122. win = gtk.Window()
  123. win.set_property('skip-taskbar-hint', True)
  124. win.set_property('skip-pager-hint', True)
  125. win.set_keep_below(True)
  126. win.connect('map', unmap)
  127. moz = MozEmbed()
  128. moz.load_url(sys.argv[1])
  129. moz.connect('net-stop', finished)
  130. moz.connect('new-window', new_window)
  131. win.set_title(sys.argv[1])
  132. win.add(moz)
  133. win.show_all()
  134. gtk.main()