-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb_delegate.py
More file actions
136 lines (119 loc) · 4.16 KB
/
Copy pathweb_delegate.py
File metadata and controls
136 lines (119 loc) · 4.16 KB
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
import console
import webbrowser
import urllib
from gopher_client import GopherClient
from gopher_item import GopherItem
class WebViewDelegate (object):
header = """
<html>
<head>
<style>
body {
font-family: "Courier New", Courier, monospace;
}
a {
text-decoration: none;
}
pre {
font-family: "Courier New", Courier, monospace;
}
</style>
</head>
<body>
"""
footer = """
</body>
</html>
"""
history = []
def __init__(self, main):
self.main = main
def webview_should_start_load(self, webview, url, nav_type):
print(url)
if "file://URL:" in url:
if "URL:http" in url:
url = url.replace("file://URL:", 'safari-')
else:
url = url.replace("file://URL:", '')
webbrowser.open(url)
return False
self.url = url
return True
def webview_did_start_load(self, webview):
print("Webview start load with " + self.url)
if self.url == 'about:blank':
return False
elif self.url == 'about:welcome':
return self.print_welcome(webview)
else:
return self.load_gopher(webview, self.url)
return True
def webview_did_finish_load(self, webview):
#print("Webview finished loading")
pass
def webview_did_fail_load(self, webview, error_code, error_msg):
print("webview failed")
#print(error_code)
#print(error_msg)
pass
def print_welcome(self, webview):
print("print_welcome")
webview.load_html("<center><h1>Welcome to iPyGopher</h1></center>")
return True
def parse_url(self, url):
item = GopherItem()
print("Using url: " + url)
item.url = url
type_host_port = url.split('/')[2].split(":")
print(type_host_port)
item.type = type_host_port[0].split("_")[0]
item.host = type_host_port[0].split("_")[1]
if not type_host_port[1]:
item.port = 70
else:
item.port = type_host_port[1]
selectors = url.split('/')[3:]
item.selector = ''
for x in range(len(selectors)):
item.selector += "/" + selectors[x]
print("Parsed selector: " + item.selector)
return item
def load_gopher(self, webview, url):
print("load_gopher")
print(url)
if url.startswith('file://'):
#lazy hack
self.wv = webview
print("Loading gophers")
item = self.parse_url(url)
if item.type == '7' and not "?" in item.selector:
# only prompt if the selector doesn't have a search term
print("Type is search")
search = console.input_alert("Enter search term")
item.selector += "?" + search
url += "?" + search #laaazzzy
elif item.type == '9' or item.type == 'g' or item.type == '4' or item.type == '5':
print("Unsupported link: " + url)
console.alert("Binary file download currently not supported")
return
elif item.type == 'h':
print(url)
console.alert("H")
return
self.history.append(url)
self.main.ab.text = urllib.parse.unquote(url[9:])
g = GopherClient()
txt = g.get(item.type, item.host, item.port, item.selector)
webview.load_html(self.header + txt + self.footer)
return True
def go_back(self, sender):
print("Go back called")
for x in range(len(self.history)):
print(self.history[x])
print(len(self.history))
# get rid of the current url... this feels wrong, probably is
if len(self.history) > 1:
self.history.pop()
load_url = self.history.pop()
print("Loading url: " + load_url)
self.wv.load_url(load_url)