-
Notifications
You must be signed in to change notification settings - Fork 25
/
browser.py
1253 lines (1121 loc) · 48.6 KB
/
browser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
"""
This is the main script for WCGBrowser, a kiosk-oriented web browser
Written by Alan D Moore, http://www.alandmoore.com
Released under the GNU GPL v3
"""
# QT Binding imports
while True:
# This is a little odd, but seemed cleaner than
# progressively nesting try/except blocks.
try:
"""Try to import PyQt5"""
from PyQt5.QtGui import QIcon, QKeySequence
from PyQt5.QtCore import (
QUrl, QTimer, QObject, QT_VERSION_STR, QEvent,
Qt, QTemporaryFile, QDir, QCoreApplication, qVersion, pyqtSignal,
QSizeF
)
from PyQt5.QtWebKit import QWebSettings
from PyQt5.QtWidgets import (
QMainWindow, QAction, QWidget, QApplication, QSizePolicy,
QToolBar, QDialog, QMenu
)
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
from PyQt5.QtWebKitWidgets import QWebView, QWebPage
from PyQt5.QtNetwork import (QNetworkRequest, QNetworkAccessManager,
QNetworkProxy)
break
except ImportError as e:
print(f"PyQt5 not found: {e}")
print(f"Trying PyQt4")
pass
try:
"""If not PyQt5, try PyQt4"""
from PyQt4.QtGui import (
QMainWindow, QAction, QIcon, QWidget,
QApplication, QSizePolicy, QKeySequence, QToolBar, QPrinter,
QPrintDialog, QDialog, QMenu
)
from PyQt4.QtCore import (
QUrl, QTimer, QObject, QT_VERSION_STR, QEvent,
Qt, QTemporaryFile, QDir, QCoreApplication, qVersion, pyqtSignal
)
from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings
from PyQt4.QtNetwork import (
QNetworkRequest, QNetworkAccessManager, QNetworkProxy
)
break
except ImportError as e:
print(f"PyQt4 not found: {e}")
print("Trying PySide")
pass
try:
"""If not PyQT, try PySide"""
from PySide.QtGui import (
QMainWindow, QAction, QIcon, QWidget,
QApplication, QSizePolicy, QKeySequence, QToolBar, QPrinter,
QPrintDialog, QDialog, QMenu
)
from PySide.QtCore import (
QUrl, QTimer, QObject, QEvent, Qt, QTemporaryFile,
QDir, QCoreApplication, qVersion, Signal
)
from PySide.QtWebKit import QWebView, QWebPage, QWebSettings
from PySide.QtNetwork import (
QNetworkRequest, QNetworkAccessManager, QNetworkProxy
)
QT_VERSION_STR = qVersion()
pyqtSignal = Signal
break
except ImportError as e:
print(f"PySide not found: {e}")
print("You don't seem to have a Python QT library installed;"
" please install PyQt4, PyQt5, or PySide.")
exit(1)
# Standard library imports
import sys
import os
import argparse
import yaml
import re
import subprocess
import datetime
import socket
from functools import partial
# MESSAGE STRINGS
# You can override this string with the "page_unavailable_html" setting.
# Just set it to a filename of the HTML you want to display.
# It will be formatted agains the configuration file, so you can
# include any config settings using {config_key_name}
DEFAULT_404 = """<h2>Sorry, can't go there</h2>
<p>This page is not available on this computer.</p>
<p>You can return to the <a href='{start_url}'>start page</a>,
or wait and you'll be returned to the
<a href='javascript: history.back();'>previous page</a>.</p>
<script>setTimeout('history.back()', 5000);</script>
"""
# This text will be shown when the start_url can't be loaded
# Usually indicates lack of network connectivity.
# It can be overridden by giving a filename in "network_down_html"
# and will be formatted against the config.
DEFAULT_NETWORK_DOWN = """<h2>Network Error</h2>
<p>The start page, {start_url}, cannot be reached.
This indicates a network connectivity problem.</p>
<p>Staff, please check the following:</p>
<ul>
<li>Ensure the network connections at the computer and at the switch,
hub, or wall panel are secure</li>
<li>Restart the computer</li>
<li>Ensure other systems at your location can access the same URL</li>
</ul>
<p>If you continue to get this error, contact technical support</p> """
# This is shown when an https site has a bad certificate and ssl_mode is set
# to "strict".
CERTIFICATE_ERROR = """<h1>Certificate Problem</h1>
<p>The URL <strong>{url}</strong> has a problem with its SSL certificate.
For your security and protection, you will not be able to access it from
this browser.</p>
<p>If this URL is supposed to be reachable,
please contact technical support for help.</p>
<p>You can return to the <a href='{start_url}'>start page</a>, or wait and
you'll be returned to the
<a href='javascript: history.back();'>previous page</a>.</p>
<script>setTimeout('history.back()', 5000);</script>
"""
# Shown when content is requested that is not HTML, text, or
# something specified in the content handlers.
UNKNOWN_CONTENT_TYPE = """<h1>Failed: unrenderable content</h1>
<p>The browser does not know how to handle the content type
<strong>{mime_type}</strong> of the file <strong>{file_name}</strong>
supplied by <strong>{url}</strong>.</p>"""
# This is displayed while a file is being downloaded.
DOWNLOADING_MESSAGE = """<H1>Downloading</h1>
<p>Please wait while the file <strong>{filename}</strong> ({mime_type})
downloads from <strong>{url}</strong>."""
def debug(message):
"""Log or print a message if the global DEBUG is true."""
if not DEBUG and not DEBUG_LOG:
pass
else:
message = message.__str__()
ts = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
debug_message = ts + ":: " + message
if DEBUG:
print(debug_message)
if DEBUG_LOG:
try:
fh = open(DEBUG_LOG, 'a')
fh.write(debug_message + "\n")
fh.close
except:
print("unable to write to log file {}".format(DEBUG_LOG))
def get_ip():
"""Get the local routing IP.
Liberally borrowed from https://stackoverflow.com/a/25850698/1454109
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
s.connect(('1.1.1.1', 1)) # IP used is kind of irrelevant
except OSError:
return ''
return s.getsockname()[0]
# Define our default configuration settings
CONFIG_OPTIONS = {
"allow_external_content": {"default": False, "type": bool},
"allow_plugins": {"default": False, "type": bool},
"allow_popups": {"default": False, "type": bool},
"allow_printing": {"default": False, "type": bool},
"bookmarks": {"default": {}, "type": dict},
"content_handlers": {"default": {}, "type": dict},
"default_encoding": {"default": "utf-8", "type": str},
"default_password": {"default": None, "type": str},
"default_user": {"default": None, "type": str},
"enable_diagnostic": {"default": False, "type": bool},
"force_js_confirm": {"default": "ask", "type": str,
"values": ("ask", "accept", "deny")},
"fullscreen": {"default": False, "type": bool},
"icon_theme": {"default": None, "type": str},
"navigation": {"default": True, "type": bool},
"navigation_layout": {"default":
['back', 'forward', 'refresh', 'stop',
'zoom_in', 'zoom_out', 'separator',
'bookmarks', 'separator', 'spacer',
'quit'], "type": list},
"network_down_html": {"default": DEFAULT_NETWORK_DOWN,
"type": str, "is_file": True},
"page_unavailable_html": {"default": DEFAULT_404, "type": str,
"is_file": True},
"print_settings": {"default": {}, "type": dict},
"privacy_mode": {"default": True, "type": bool},
"proxy_server": {"default": None, "type": str,
"env": "http_proxy"},
"quit_button_mode": {"default": "reset", "type": str,
"values": ["reset", "close"]},
"quit_button_text": {"default": "I'm &Finished", "type": str},
"screensaver_url": {"default": "about:blank", "type": str},
"ssl_mode": {"default": "strict", "type": str,
"values": ["strict", "ignore"]},
"start_url": {"default": "about:blank", "type": str},
"stylesheet": {"default": None, "type": str},
"suppress_alerts": {"default": False, "type": bool},
"timeout": {"default": 0, "type": int},
"timeout_mode": {"default": "reset", "type": str,
"values": ["reset", "close", "screensaver"]},
"user_agent": {"default": None, "type": str},
"user_css": {"default": None, "type": str},
"whitelist": {"default": None}, # don't check type here
"window_size": {"default": None}, # don't check type
"zoom_factor": {"default": 1.0, "type": float}
}
class MainWindow(QMainWindow):
"""This is the main application window class
it defines the GUI window for the browser
"""
def parse_config(self, file_config, options):
self.config = {}
options = vars(options)
for key, metadata in CONFIG_OPTIONS.items():
options_val = options.get(key)
file_val = file_config.get(key)
env_val = os.environ.get(metadata.get("env", ''))
default_val = metadata.get("default")
vals = metadata.get("values")
debug("key: {}, default: {}, file: {}, options: {}".format(
key, default_val, file_val, options_val
))
if vals:
options_val = (options_val in vals and options_val) or None
file_val = (file_val in vals and file_val) or None
env_val = (env_val in vals and env_val) or None
if metadata.get("is_file"):
filename = options_val or env_val
if not filename:
self.config[key] = default_val
else:
try:
with open(filename, 'r') as fh:
self.config[key] = fh.read()
except IOError:
debug("Could not open file {} for reading.".format(
filename)
)
self.config[key] = default_val
else:
set_values = [
val for val in (options_val, env_val, file_val)
if val is not None
]
if len(set_values) > 0:
self.config[key] = set_values[0]
else:
self.config[key] = default_val
if metadata.get("type") and self.config[key]:
debug("{} cast to {}".format(key, metadata.get("type")))
self.config[key] = metadata.get("type")(self.config[key])
debug(repr(self.config))
def createAction(self, text, slot=None, shortcut=None, icon=None, tip=None,
checkable=False, signal="triggered"):
"""Return a QAction given a number of common QAction attributes
Just a shortcut function Originally borrowed from
'Rapid GUI Development with PyQT' by Mark Summerset
"""
action = QAction(text, self)
if icon is not None:
action.setIcon(QIcon.fromTheme(
icon, QIcon(":/{}.png".format(icon))
))
if shortcut is not None and not shortcut.isEmpty():
action.setShortcut(shortcut)
tip += " ({})".format(shortcut.toString())
if tip is not None:
action.setToolTip(tip)
action.setStatusTip(tip)
if slot is not None:
getattr(action, signal).connect(slot)
if checkable:
action.setCheckable()
return action
def __init__(self, options, parent=None):
"""Construct a MainWindow Object."""
super(MainWindow, self).__init__(parent)
# Load config file
self.setWindowTitle("Browser")
debug("loading configuration from '{}'".format(options.config_file))
configfile = {}
if options.config_file:
configfile = yaml.safe_load(open(options.config_file, 'r'))
self.parse_config(configfile, options)
# self.popup will hold a reference to the popup window
# if it gets opened
self.popup = None
# Stylesheet support
if self.config.get("stylesheet"):
try:
with open(self.config.get("stylesheet")) as ss:
self.setStyleSheet(ss.read())
except:
debug(
"""Problem loading stylesheet file "{}", """
"""using default style."""
.format(self.config.get("stylesheet"))
)
self.setObjectName("global")
# If the whitelist is activated, add the bookmarks and start_url
if self.config.get("whitelist"):
# we can just specify whitelist = True,
# which should whitelist just the start_url and bookmark urls.
whitelist = self.config.get("whitelist")
if type(whitelist) is not list:
whitelist = []
whitelist.append(str(QUrl(
self.config.get("start_url")
).host()))
bookmarks = self.config.get("bookmarks")
if bookmarks:
whitelist += [
str(QUrl(b.get("url")).host())
for k, b in bookmarks.items()
]
# uniquify and optimize
self.config["whitelist"] = set(whitelist)
debug("Generated whitelist: " + str(whitelist))
# If diagnostic is enabled:
# connect CTRL+ALT+? to show some diagnistic info
if (self.config.get("enable_diagnostic")):
self.diagnostic_action = self.createAction(
"Show Diagnostic",
self.show_diagnostic,
QKeySequence("Ctrl+Alt+/"),
tip=''
)
self.addAction(self.diagnostic_action)
# Set the default encoding if using python 2
if sys.version_info[0] < 3:
reload(sys)
sys.setdefaultencoding(self.config.get("default_encoding"))
self.build_ui()
# ## END OF CONSTRUCTOR ## #
def build_ui(self):
"""Set up the user interface for the main window.
Unlike the constructor, this method is re-run
whenever the browser is "reset" by the user.
"""
debug("build_ui")
inactivity_timeout = self.config.get("timeout")
quit_button_tooltip = (
self.config.get("quit_button_mode") == 'close'
and "Click here to quit the browser."
or """Click here when you are done.
It will clear your browsing history"""
""" and return you to the start page.""")
qb_mode_callbacks = {'close': self.close, 'reset': self.reset_browser}
to_mode_callbacks = {'close': self.close,
'reset': self.reset_browser,
'screensaver': self.screensaver}
self.screensaver_active = False
# ##Start GUI configuration## #
self.browser_window = WcgWebView(self.config)
self.browser_window.setObjectName("web_content")
if (
self.config.get("icon_theme") is not None
and QT_VERSION_STR > '4.6'
):
QIcon.setThemeName(self.config.get("icon_theme"))
self.setCentralWidget(self.browser_window)
debug("loading {}".format(self.config.get("start_url")))
self.browser_window.setUrl(QUrl(self.config.get("start_url")))
if self.config.get("fullscreen"):
self.showFullScreen()
elif (
self.config.get("window_size") and
self.config.get("window_size").lower() == 'max'
):
self.showMaximized()
elif self.config.get("window_size"):
size = re.match(r"(\d+)x(\d+)", self.config.get("window_size"))
if size:
width, height = size.groups()
self.setFixedSize(int(width), int(height))
else:
debug('Ignoring invalid window size "{}"'.format(
self.config.get("window_size")
))
# Set up the top navigation bar if it's configured to exist
if self.config.get("navigation"):
self.navigation_bar = QToolBar("Navigation")
self.navigation_bar.setObjectName("navigation")
self.addToolBar(Qt.TopToolBarArea, self.navigation_bar)
self.navigation_bar.setMovable(False)
self.navigation_bar.setFloatable(False)
# Standard navigation tools
self.nav_items = {}
self.nav_items["back"] = self.browser_window.pageAction(QWebPage.Back)
self.nav_items["forward"] = self.browser_window.pageAction(QWebPage.Forward)
self.nav_items["refresh"] = self.browser_window.pageAction(QWebPage.Reload)
self.nav_items["stop"] = self.browser_window.pageAction(QWebPage.Stop)
# The "I'm finished" button.
self.nav_items["quit"] = self.createAction(
self.config.get("quit_button_text"),
qb_mode_callbacks.get(self.config.get("quit_button_mode"), self.reset_browser),
QKeySequence("Alt+F"),
None,
quit_button_tooltip)
# Zoom buttons
self.nav_items["zoom_in"] = self.createAction(
"Zoom In",
self.zoom_in,
QKeySequence("Alt++"),
"zoom-in",
"Increase the size of the text and images on the page")
self.nav_items["zoom_out"] = self.createAction(
"Zoom Out",
self.zoom_out,
QKeySequence("Alt+-"),
"zoom-out",
"Decrease the size of text and images on the page")
if self.config.get("allow_printing"):
self.nav_items["print"] = self.createAction(
"Print",
self.browser_window.print_webpage,
QKeySequence("Ctrl+p"),
"document-print",
"Print this page")
# Add all the actions to the navigation bar.
for item in self.config.get("navigation_layout"):
if item == "separator":
self.navigation_bar.addSeparator()
elif item == "spacer":
# an expanding spacer.
spacer = QWidget()
spacer.setSizePolicy(
QSizePolicy.Expanding, QSizePolicy.Preferred)
self.navigation_bar.addWidget(spacer)
elif item == "bookmarks":
# Insert bookmarks buttons here.
self.bookmark_buttons = []
for bookmark in self.config.get("bookmarks", {}).items():
debug("Bookmark:\n" + bookmark.__str__())
# bookmark name will use the "name" attribute, if present
# or else just the key:
bookmark_name = bookmark[1].get("name") or bookmark[0]
# Create a button for the bookmark as a QAction,
# which we'll add to the toolbar
bookmark_url = bookmark[1].get("url", "about:blank")
bookmark_callback = partial(
self.browser_window.load,
QUrl(bookmark_url)
)
button = self.createAction(
bookmark_name,
bookmark_callback,
QKeySequence.mnemonic(bookmark_name),
None,
bookmark[1].get("description")
)
self.navigation_bar.addAction(button)
self.navigation_bar.widgetForAction(button).setObjectName("navigation_button")
else:
action = self.nav_items.get(item, None)
if action:
self.navigation_bar.addAction(action)
self.navigation_bar.widgetForAction(action).setObjectName("navigation_button")
# This removes the ability to toggle off the navigation bar:
self.nav_toggle = self.navigation_bar.toggleViewAction()
self.nav_toggle.setVisible(False)
# End "if show_navigation is True" block
# set hidden quit action
# For reasons I haven't adequately ascertained,
# this shortcut fails now and then claiming
# "Ambiguous shortcut overload".
# No idea why, as it isn't consistent.
self.really_quit = self.createAction(
"", self.close, QKeySequence("Ctrl+Alt+Q"), None, ""
)
self.addAction(self.really_quit)
# Call a reset function after timeout
if inactivity_timeout != 0:
self.event_filter = InactivityFilter(inactivity_timeout)
QCoreApplication.instance().installEventFilter(self.event_filter)
self.browser_window.page().installEventFilter(self.event_filter)
self.event_filter.timeout.connect(
to_mode_callbacks.get(self.config.get("timeout_mode"),
self.reset_browser))
else:
self.event_filter = None
# ##END OF UI SETUP## #
def screensaver(self):
"""Enter "screensaver" mode
This method puts the browser in screensaver mode, where a URL
is displayed while the browser is idle. Activity causes the browser to
return to the home screen.
"""
debug("screensaver started")
self.screensaver_active = True
if self.popup:
self.popup.close()
if self.config.get("navigation"):
self.navigation_bar.hide()
self.browser_window.setZoomFactor(self.config.get("zoom_factor"))
self.browser_window.load(QUrl(self.config.get("screensaver_url")))
self.event_filter.timeout.disconnect()
self.event_filter.activity.connect(self.reset_browser)
def reset_browser(self):
"""Clear the history and reset the UI.
Called whenever the inactivity filter times out,
or when the user clicks the "finished" button in
'reset' mode.
"""
# Clear out the memory cache
QWebSettings.clearMemoryCaches()
self.browser_window.history().clear()
# self.navigation_bar.clear() doesn't do its job,
# so remove the toolbar first, then rebuild the UI.
debug("RESET BROWSER")
if self.event_filter:
self.event_filter.blockSignals(True)
if self.screensaver_active is True:
self.screensaver_active = False
self.event_filter.activity.disconnect()
if self.event_filter:
self.event_filter.blockSignals(False)
if hasattr(self, "navigation_bar"):
self.removeToolBar(self.navigation_bar)
self.build_ui()
def zoom_in(self):
"""Zoom in action callback.
Note that we cap zooming in at a factor of 3x.
"""
if self.browser_window.zoomFactor() < 3.0:
self.browser_window.setZoomFactor(
self.browser_window.zoomFactor() + 0.1
)
self.nav_items["zoom_out"].setEnabled(True)
else:
self.nav_items["zoom_in"].setEnabled(False)
def zoom_out(self):
"""Zoom out action callback.
Note that we cap zooming out at 0.1x.
"""
if self.browser_window.zoomFactor() > 0.1:
self.browser_window.setZoomFactor(
self.browser_window.zoomFactor() - 0.1
)
self.nav_items["zoom_in"].setEnabled(True)
else:
self.nav_items["zoom_out"].setEnabled(False)
def show_diagnostic(self):
"Display a dialog box with some diagnostic info"
data = {
"OS": os.uname(),
"USER": (os.environ.get("USER")
or os.environ.get("USERNAME")),
"IP": get_ip(),
"Python": sys.version,
"Qt": QT_VERSION_STR,
"Script Date": (
datetime.datetime.fromtimestamp(
os.stat(__file__).st_mtime).isoformat()
)
}
html = "\n".join([
"<h1>System Information</h1>",
"<h2>Please click "",
self.config.get("quit_button_text").replace("&", ''),
"" when you are finished.</h2>",
"<ul>",
"\n".join([
"<li><b>{}</b>: {}</li>".format(k, v)
for k, v in data.items()
]),
"</ul>"
])
self.browser_window.setHtml(html)
# ## END Main Application Window Class def ## #
class InactivityFilter(QTimer):
"""This defines an inactivity filter.
It's basically a timer that resets when user "activity"
(Mouse/Keyboard events) are detected in the main application.
"""
activity = pyqtSignal()
def __init__(self, timeout=0, parent=None):
"""Constructor for the class.
args:
timeout -- number of seconds before timer times out (integer)
"""
super(InactivityFilter, self).__init__(parent)
# timeout needs to be converted from seconds to milliseconds
self.timeout_time = timeout * 1000
self.setInterval(self.timeout_time)
self.start()
def eventFilter(self, object, event):
"""Overridden from QTimer.eventFilter"""
if event.type() in (
QEvent.MouseMove, QEvent.MouseButtonPress,
QEvent.HoverMove, QEvent.KeyPress,
QEvent.KeyRelease
):
self.activity.emit()
self.start(self.timeout_time)
# commented this debug code,
# because it spits out way to much information.
# uncomment if you're having trouble with the timeout detecting
# user inactivity correctly to determine what it's detecting
# and ignoring:
# debug ("Activity: %s type %d" % (event, event.type()))
# else:
# debug("Ignored event: %s type %d" % (event, event.type()))
return QObject.eventFilter(self, object, event)
class WcgNetworkAccessManager(QNetworkAccessManager):
"""Overridden so we can get debug info from responses"""
def __init__(self):
super(WcgNetworkAccessManager, self).__init__()
# add event listener on "load finished" event
self.finished.connect(self._finished)
self.failed_urls = []
def _finished(self, reply):
headers = [
(str(k), str(v))
for k, v in reply.rawHeaderPairs()
]
url = reply.url().toString()
# getting status is bit of a pain
status = reply.attribute(
QNetworkRequest.HttpStatusCodeAttribute
)
# track the URLs that failed
if status is None or status >= 400:
self.failed_urls.append(url)
debug(
"Got {status} from {url}, headers: {headers}"
.format(status=status, headers=headers, url=url)
)
def reset_failed_urls(self):
self.failed_urls = []
def createRequest(self, op, request, iodata):
url = str(request.url())
headers = [str(x) for x in request.rawHeaderList()]
debug(
"{op} request to {url}, headers: {headers}"
.format(op=op, url=url, headers=headers)
)
return super(WcgNetworkAccessManager, self).createRequest(op, request, iodata)
class WcgWebView(QWebView):
"""This is the webview for the application.
It represents a browser window, either the main one or a popup.
It's a simple wrapper around QWebView that configures some basic settings.
"""
def __init__(self, config, parent=None, **kwargs):
"""Constructor for the class"""
super(WcgWebView, self).__init__(parent)
self.kwargs = kwargs
self.config = config
self.nam = (kwargs.get('networkAccessManager')
or WcgNetworkAccessManager())
self.setPage(WCGWebPage(config=config))
self.page().setNetworkAccessManager(self.nam)
self.settings().setAttribute(
QWebSettings.JavascriptCanOpenWindows,
config.get("allow_popups")
)
if config.get('user_css'):
self.settings().setUserStyleSheetUrl(QUrl(config.get('user_css')))
# JavascriptCanCloseWindows is in the API documentation,
# but apparently only exists after 4.8
if QT_VERSION_STR >= '4.8':
self.settings().setAttribute(
QWebSettings.JavascriptCanCloseWindows,
config.get("allow_popups")
)
self.settings().setAttribute(
QWebSettings.PrivateBrowsingEnabled,
config.get("privacy_mode")
)
self.settings().setAttribute(QWebSettings.LocalStorageEnabled, True)
self.settings().setAttribute(
QWebSettings.PluginsEnabled,
config.get("allow_plugins")
)
self.page().setForwardUnsupportedContent(
config.get("allow_external_content")
)
self.setZoomFactor(config.get("zoom_factor"))
# add printing to context menu if it's allowed
if config.get("allow_printing"):
self.print_action = QAction("Print", self)
self.print_action.setIcon(QIcon.fromTheme("document-print"))
self.print_action.triggered.connect(self.print_webpage)
self.page().printRequested.connect(self.print_webpage)
self.print_action.setToolTip("Print this web page")
# Set up the proxy if there is one set
if config.get("proxy_server"):
if ":" in config["proxy_server"]:
proxyhost, proxyport = config["proxy_server"].split(":")
else:
proxyhost = config["proxy_server"]
proxyport = 8080
self.nam.setProxy(QNetworkProxy(
QNetworkProxy.HttpProxy, proxyhost, int(proxyport)
))
# connections for wcgwebview
self.page().networkAccessManager().authenticationRequired.connect(
self.auth_dialog
)
self.page().unsupportedContent.connect(self.handle_unsupported_content, type=Qt.QueuedConnection)
self.page().downloadRequested.connect(self.download)
self.page().networkAccessManager().sslErrors.connect(
self.sslErrorHandler
)
self.urlChanged.connect(self.onLinkClick)
self.loadFinished.connect(self.onLoadFinished)
def createWindow(self, type):
"""Handle requests for a new browser window.
Method called whenever the browser requests a new window
(e.g., <a target='_blank'> or window.open()).
Overridden from QWebView to allow for popup windows, if enabled.
"""
if self.config.get("allow_popups"):
self.kwargs["networkAccessManager"] = self.nam
self.popup = WcgWebView(
self.config,
**self.kwargs
)
# This assumes the window manager has an "X" icon
# for closing the window somewhere to the right.
self.popup.setObjectName("web_content")
self.popup.setWindowTitle(
"Click the 'X' to close this window! ---> "
)
self.popup.page().windowCloseRequested.connect(self.popup.close)
self.popup.show()
return self.popup
else:
debug("Popup not loaded on {}".format(self.url().toString()))
def contextMenuEvent(self, event):
"""Handle requests for a context menu in the browser.
Overridden from QWebView,
to provide right-click functions according to user settings.
"""
menu = QMenu(self)
for action in [
QWebPage.Back, QWebPage.Forward,
QWebPage.Reload, QWebPage.Stop
]:
action = self.pageAction(action)
if action.isEnabled():
menu.addAction(action)
if self.config.get("allow_printing"):
menu.addAction(self.print_action)
menu.exec_(event.globalPos())
def sslErrorHandler(self, reply, errorList):
"""Handle SSL errors in the browser.
Overridden from QWebView.
Called whenever the browser encounters an SSL error.
Checks the ssl_mode and responds accordingly.
"""
if self.config.get("ssl_mode") == 'ignore':
reply.ignoreSslErrors()
debug("SSL error ignored")
debug(", ".join([str(error.errorString()) for error in errorList]))
else:
self.setHtml(
CERTIFICATE_ERROR.format(
url=reply.url().toString(),
start_url=self.config.get("start_url")
))
def auth_dialog(self, reply, authenticator):
"""Handle requests for HTTP authentication
This is called when a page requests authentication.
It might be nice to actually have a dialog here,
but for now we just use the default credentials from the config file.
"""
debug("Auth required on {}".format(reply.url().toString()))
default_user = self.config.get("default_user")
default_password = self.config.get("default_password")
if (default_user):
authenticator.setUser(default_user)
if (default_password):
authenticator.setPassword(default_password)
def download(self, request):
"""Handle a download request
This is needed for certain types of download links
"""
reply = self.nam.get(request)
reply.finished.connect(partial(self.handle_unsupported_content, reply))
def handle_unsupported_content(self, reply):
"""Handle requests to open non-web content
Called basically when the reply from the request is not HTML
or something else renderable by qwebview. It checks the configured
content-handlers for a matching MIME type, and opens the file or
displays an error per the configuration.
"""
self.reply = reply
self.content_type = self.reply.header(
QNetworkRequest.ContentTypeHeader
)
self.content_filename = re.match(
'.*;\s*filename=(.*);',
bytes(self.reply.rawHeader(b'Content-Disposition')).decode('UTF-8')
)
self.content_filename = QUrl.fromPercentEncoding(
(
self.content_filename.group(1).encode('UTF-8')
if self.content_filename
else b''
)
)
content_url = self.reply.url()
debug(
"Loading url {} of type {}".format(
content_url.toString(), self.content_type
))
if not self.config.get("content_handlers").get(str(self.content_type)):
self.setHtml(UNKNOWN_CONTENT_TYPE.format(
mime_type=self.content_type,
file_name=self.content_filename,
url=content_url.toString()))
else:
if self.reply.isFinished:
self.display_downloaded_content()
else:
self.reply.finished.connect(self.display_downloaded_content)
if str(self.url().toString()) in ('', 'about:blank'):
self.setHtml(DOWNLOADING_MESSAGE.format(
filename=self.content_filename,
mime_type=self.content_type,
url=content_url.toString()))
else:
# print(self.url())
self.load(self.url())
def display_downloaded_content(self):
"""Open downloaded non-html content in a separate application.
Called when an unsupported content type is finished downloading.
"""
debug("displaying downloaded content from {}".format(self.reply.url()))
file_path = (
QDir.toNativeSeparators(
QDir.tempPath() + "/XXXXXX_" + self.content_filename
)
)
myfile = QTemporaryFile(file_path)
myfile.setAutoRemove(False)
if myfile.open():
myfile.write(self.reply.readAll())
myfile.close()
subprocess.Popen([
(self.config.get("content_handlers")
.get(str(self.content_type))),
myfile.fileName()
])
# Sometimes downloading files opens an empty window.
# So if the current window has no URL, close it.
if(str(self.url().toString()) in ('', 'about:blank')):
self.close()
def onLinkClick(self, url):
"""Handle clicked hyperlinks.
Overridden from QWebView.
Called whenever the browser navigates to a URL;
handles the whitelisting logic and does some debug logging.
"""
debug("Request URL: {}".format(url.toString()))
if not url.isEmpty():
# If whitelisting is enabled, and this isn't the start_url host,
# check the url to see if the host's domain matches.
if (
self.config.get("whitelist")
and not (url.host() ==
QUrl(self.config.get("start_url")).host())
and not str(url.toString()) == 'about:blank'
):
site_ok = False
pattern = re.compile(str("(^|.*\.)(" + "|".join(
[re.escape(w)
for w
in self.config.get("whitelist")]
) + ")$"))
debug("Whitelist pattern: {}".format(pattern.pattern))
if re.match(pattern, url.host()):
site_ok = True
if not site_ok:
debug("Site violates whitelist: {}, {}".format(
url.host(), url.toString())
)
self.setHtml(self.config.get("page_unavailable_html")
.format(**self.config))
if not url.isValid():
debug("Invalid URL {}".format(url.toString()))
else:
debug("Load URL {}".format(url.toString()))
def onLoadFinished(self, ok):
"""Handle loadFinished events.
Overridden from QWebView.
This function is called when a page load finishes.
We're checking to see if the load was successful;
if it's not, we display either the 404 error (if
it's just some random page), or a "network is down" message
(if it's the start page that failed).
"""
# "ok==false" doesn't always mean a complete failure
# and we shouldn't automatically assume the entire page failed to load.