-
Notifications
You must be signed in to change notification settings - Fork 0
/
prisma.py
201 lines (137 loc) · 5.94 KB
/
prisma.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
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2021 Ferhat Geçdoğan All Rights Reserved.
# Distributed under the terms of the MIT License.
#
# Prisma
# Python3 implementation of Webkit2 web-engine based Prism web-browser (executable)
#
# github.com/ferhatgec/prism
#
# TODO:
# More fonts.
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('WebKit2', '4.0')
from gi.repository import Gtk, WebKit2, Soup
from os import path
class Prisma(Gtk.Window):
title = 'Fegeya Prisma'
default_url = 'http://duckduckgo.com'
default_protocol = 'http'
window = Gtk.Window(title=title)
webview = WebKit2.WebView()
cookie_context = ''
cookie_manager = ''
header_bar = Gtk.HeaderBar()
scrolled_window = Gtk.ScrolledWindow()
url_bar = Gtk.Entry()
home_button = Gtk.ToolButton()
back_button = Gtk.ToolButton()
forward_button = Gtk.ToolButton()
reload_button = Gtk.ToolButton()
security = Gtk.Label()
img = Gtk.Image()
def __init__(self):
from pathlib import Path
self.window.connect('destroy', Gtk.main_quit)
self.header_bar.set_title(self.title)
self.header_bar.set_subtitle("Browsing for everyone, everytime.")
self.header_bar.set_show_close_button(True)
self.window.set_size_request(900, 600)
self.cookie_context = WebKit2.WebContext()
self.cookie_manager = WebKit2.WebContext.get_cookie_manager(self.cookie_context)
self.cookie_manager.set_accept_policy(WebKit2.CookieAcceptPolicy.ALWAYS)
self.cookie_manager.set_persistent_storage('{home_dir}/.config/prisma/prismacook.ies'.format(home_dir=Path.home()),
WebKit2.CookiePersistentStorage.TEXT)
self.webview = WebKit2.WebView.new_with_context(self.cookie_context)
if path.exists('/usr/share/pixmaps/prism/homepage/index.html'):
self.default_url = 'file:///usr/share/pixmaps/prism/homepage/index.html'
if 'file:///' in self.default_url:
self.security.set_label('🏠 | Home')
elif 'https' in self.default_url:
self.security.set_label('🔒 | Home')
else:
self.security.set_label('🔓 | Home')
self.initialize_widgets()
self.connect_signals()
self.window.set_titlebar(self.header_bar)
self.window.show_all()
def initialize_widgets(self):
img = Gtk.Image()
if path.exists('/usr/share/pixmaps/prism/prism_32.png'):
img.set_from_file('/usr/share/pixmaps/prism/prism_32.png')
self.home_button.set_icon_widget(img)
self.img = Gtk.Image()
if path.exists('/usr/share/pixmaps/prism/white_arrow_left.png'):
self.img.set_from_file('/usr/share/pixmaps/prism/white_arrow_left.png')
self.back_button.set_icon_widget(self.img)
self.img = Gtk.Image()
if path.exists('/usr/share/pixmaps/prism/white_arrow_right.png'):
self.img.set_from_file('/usr/share/pixmaps/prism/white_arrow_right.png')
else:
self.img.set_from_icon_name('go-next', Gtk.IconSize.SMALL_TOOLBAR)
self.forward_button.set_icon_widget(self.img)
self.img = Gtk.Image()
if path.exists('/usr/share/pixmaps/prism/white_refresh.png'):
self.img.set_from_file('/usr/share/pixmaps/prism/white_refresh.png')
else:
self.img.set_from_icon_name('view-refresh', Gtk.IconSize.SMALL_TOOLBAR)
self.reload_button.set_icon_widget(self.img)
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
self.scrolled_window.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
self.webview.load_uri(self.default_url)
self.back_button.connect('clicked', self.go_back)
self.forward_button.connect('clicked', self.go_forward)
self.reload_button.connect('clicked', self.reload)
self.url_bar.set_icon_from_icon_name(Gtk.EntryIconPosition.PRIMARY, 'system-search-symbolic')
box.add(self.home_button)
box.add(self.url_bar)
box.add(self.back_button)
box.add(self.forward_button)
box.add(self.reload_button)
box.add(self.security)
self.header_bar.pack_start(box)
self.scrolled_window.add(self.webview)
self.window.add(self.scrolled_window)
def connect_signals(self):
self.webview.connect('load_changed', self.on_load_changed)
self.url_bar.connect('activate', self.on_activate)
def on_load_changed(self, data, web):
self.url_bar.set_text(self.webview.get_uri())
self.on_check_security()
def on_check_security(self):
if 'file:///' in self.webview.get_uri():
self.security.set_label('🏠 | Home')
if self.webview.get_uri() == self.default_url:
self.url_bar.set_text('prisma:home')
elif 'https' in self.webview.get_uri():
self.security.set_label('🔒 | Secure')
else:
self.security.set_label('🔓 | Unsecure')
def on_activate(self, data):
url = self.url_bar.get_text()
if url == 'prisma:home':
self.webview.load_uri(self.default_url)
return
self.on_check_security()
if 'http' in url and not 'https' in url:
if 'google' in self.default_url:
url = self.default_url + '/search?q=' + self.url_bar.get_text()
elif 'duckduckgo' in self.default_url:
url = self.default_url + '?q=' + self.url_bar.get_text()
else:
url = self.default_url + '/' + self.url_bar.get_text()
else:
url = self.default_protocol + self.url_bar.get_text()
self.webview.load_uri(url)
def go_back(self, data):
self.webview.go_back()
def go_forward(self, data):
self.webview.go_forward()
def reload(self, data):
self.webview.reload()
prism = Prisma()
prism.__init__()
Gtk.main()