-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
564 lines (465 loc) · 20.4 KB
/
main.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
from PyQt6 import QtCore, QtGui, QtWidgets
import BrowserUI, MenuUI, HistoryUI
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl, QSize
import sqlite3
import os
class Browser(QtWidgets.QWidget, BrowserUI.Ui_Form):
def __init__(self):
global sessionSettings
super().__init__()
self.webPage = QWebEngineView()
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.webPage)
self.setupUi(self)
self.setWindowTitle(" ")
self.SetUpFunctions()
self.LoadIcons()
self.SetSearchStart()
def SetUpFunctions(self):
self.urlLineEdit.returnPressed.connect(self.OnEnterPressed)
self.urlLineEdit2.returnPressed.connect(self.OnEnterPressed)
self.reloadButton.clicked.connect(self.Reload)
self.backButton.clicked.connect(self.Back)
self.forwardButton.clicked.connect(self.Forward)
self.searchButton.clicked.connect(self.OnEnterPressed)
self.newTabButton.clicked.connect(self.CreateNewWindow)
self.menuButton.clicked.connect(self.OpenMenu)
self.addToBookmarksButton.clicked.connect(self.AddToBookmarks)
self.searchHistoryList.itemClicked.connect(self.LoadFromSearchHistory)
self.LoadSearchHistory()
self.bookmarksTab.setParent(None)
self.bookmarkButton.setParent(None)
self.bookmarksTab = QtWidgets.QToolBar(self)
self.gridLayout.addWidget(self.bookmarksTab, 1, 0)
self.LoadBookmarksTab()
def OnEnterPressed(self):
global sessionSettings, urlStarts
self.webPage.setParent(None)
self.webPage = QWebEngineView()
self.gridLayout.addWidget(self.webPage, 2, 0)
self.webPage.urlChanged.connect(self.UrlChanged)
self.SetSearchStart()
if (self.sender().objectName() == "urlLineEdit"):
url = self.CheckUrlStart(self.urlLineEdit.text())
else:
url = self.CheckUrlStart(self.urlLineEdit2.text())
self.webPage.setUrl(QUrl(url[0]))
self.urlLineEdit.setText(url[0])
if bool(sessionSettings[2][2] and url[1] != None):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
id = list(cursor.execute("SELECT id FROM browserHistory WHERE url = ?", (url[0], )))
cursor.execute("INSERT INTO searchHistory (id, query) VALUES (?, ?)", (id[-1][0], url[1]))
conn.commit()
conn.close()
def CreateNewWindow(self):
global windows
newWindow = Browser()
windows.append(newWindow)
windows[-1].show()
windows[-1].LoadBookmarksTab()
def Back(self):
self.webPage.back()
def Forward(self):
self.webPage.forward()
def Reload(self):
self.webPage.reload()
def OpenMenu(self):
self.menu = Menu()
self.menu.show()
def UrlChanged(self):
global sessionSettings
print("CHANGED URL")
self.urlLineEdit.setText(self.webPage.url().toString())
if bool(sessionSettings[1][2]):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
cursor.execute('INSERT INTO browserHistory (url, title) VALUES (?, ?)', (self.webPage.url().toString(), self.webPage.title()))
conn.commit()
conn.close()
def LoadUrl(self, url):
self.webPage.setParent(None)
self.webPage = QWebEngineView()
self.gridLayout.addWidget(self.webPage, 2, 0)
self.webPage.urlChanged.connect(self.UrlChanged)
self.SetSearchStart()
self.webPage.setUrl(QUrl(url))
def CheckUrlStart(self, prompt):
global urlStarts
if any([urlStart in prompt for urlStart in urlStarts]):
return (prompt, None)
else:
return (self.searchStart + prompt, prompt)
def LoadSearchHistory(self):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
searchHistory = list(cursor.execute("SELECT query FROM searchHistory"))
conn.close()
self.searchHistoryList.clear()
for i in range(len(searchHistory) - 1, -1, -1):
self.searchHistoryList.addItem(searchHistory[i][0])
def LoadFromSearchHistory(self):
self.webPage.setParent(None)
self.webPage = QWebEngineView()
self.gridLayout.addWidget(self.webPage, 2, 0)
self.webPage.urlChanged.connect(self.UrlChanged)
self.SetSearchStart()
self.LoadUrl(QUrl(self.searchStart + self.searchHistoryList.currentItem().text()))
def LoadBookmarksTab(self):
for window in windows:
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
bookmarks = list(cursor.execute("SELECT * FROM bookmarks"))
conn.close()
window.bookmarksTab.clear()
for item in bookmarks:
window.bookmarksTab.addAction(self.CreateAction(item))
def CreateAction(self, item):
action = QtWidgets.QWidgetAction(self)
action.setText(item[1])
action.triggered.connect(lambda : self.LoadFromBookmarksTab(item[2]))
return action
def AddToBookmarks(self):
if (self.webPage.url().toString() != ""):
print("ADDED TO BOOKMARKS")
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO bookmarks (bookmarkName, bookmarkUrl) VALUES (?, ?)", (self.webPage.page().title(), self.webPage.url().toString()))
conn.commit()
conn.close()
self.LoadBookmarksTab()
def LoadFromBookmarksTab(self, url):
self.webPage.setParent(None)
self.webPage = QWebEngineView()
self.gridLayout.addWidget(self.webPage, 2, 0)
self.webPage.urlChanged.connect(self.UrlChanged)
self.webPage.setUrl(QUrl(url))
def SetSearchStart(self):
if int(sessionSettings[0][2]) == 0:
self.searchStart = "https://www.google.com/search?q="
elif int(sessionSettings[0][2]) == 1:
self.searchStart = "https://ya.ru/search/?text="
elif int(sessionSettings[0][2]) == 2:
self.searchStart = "https://www.bing.com/search?q="
elif int(sessionSettings[0][2]) == 3:
self.searchStart = "https://search.yahoo.com/search?p="
elif int(sessionSettings[0][2]) == 4:
self.searchStart = "https://duckduckgo.com/?t=h_&q="
def LoadIcons(self):
self.menuButton.setIcon(QtGui.QIcon(ResourcePath("svgs/menu.svg")))
self.menuButton.setIconSize(QSize(20, 20))
self.reloadButton.setIcon(QtGui.QIcon(ResourcePath("svgs/refresh.svg")))
self.reloadButton.setIconSize(QSize(20, 20))
self.backButton.setIcon(QtGui.QIcon(ResourcePath("svgs/back1.svg")))
self.backButton.setIconSize(QSize(20, 20))
self.forwardButton.setIcon(QtGui.QIcon(ResourcePath("svgs/forward.svg")))
self.forwardButton.setIconSize(QSize(20, 20))
self.addToBookmarksButton.setIcon(QtGui.QIcon(ResourcePath("svgs/bookmark.svg")))
self.addToBookmarksButton.setIconSize(QSize(20, 20))
self.newTabButton.setIcon(QtGui.QIcon(ResourcePath("svgs/newtab.svg")))
self.newTabButton.setIconSize(QSize(20, 20))
self.searchButton.setIcon(QtGui.QIcon(ResourcePath("svgs/search.svg")))
self.searchButton.setIconSize(QSize(35, 35))
class Menu(QtWidgets.QWidget, MenuUI.Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle(" ")
self.SetUpFunctions()
self.LoadSettingsIntoUI()
def SetUpFunctions(self):
self.browserHistoryButton.clicked.connect(self.OpenBrowserHistory)
self.searchHistoryButton.clicked.connect(self.OpenSearchHistory)
self.saveButton.clicked.connect(self.Save)
self.backButton.clicked.connect(self.Close)
self.clearBrowserHistoryButton.clicked.connect(self.ClearBrowserHistory)
self.clearSearchHistoryButton.clicked.connect(self.ClearSearchHistory)
self.bookmarksButton.clicked.connect(self.OpenBookmarks)
self.clearBookmarksButton.clicked.connect(self.ClearBookmarks)
self.searchSystem.addItems(["Google", "Yandex", "Bing", "Yahoo!", "DuckDuckGo"])
self.LoadIcons()
def OpenBrowserHistory(self):
self.browserHistory = BrowserHistory()
self.browserHistory.show()
def OpenSearchHistory(self):
self.searchHistory = SearchHistory()
self.searchHistory.show()
def Close(self):
self.close()
def Save(self):
global sessionSettings
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
cursor.execute("UPDATE settings SET settingValue = ? WHERE settingName = 'searchSystem'", (self.searchSystem.currentIndex(), ))
cursor.execute("UPDATE settings SET settingValue = ? WHERE settingName = 'saveBrowserHistoryFlag'", (self.saveBrowserHistoryFlag.isChecked(), ))
cursor.execute("UPDATE settings SET settingValue = ? WHERE settingName = 'saveSearchHistoryFlag'", (self.saveSearchHistoryFlag.isChecked(), ))
conn.commit()
sessionSettings = list(cursor.execute("SELECT * FROM settings"))
conn.close()
def LoadSettingsIntoUI(self):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
settings = list(cursor.execute("SELECT * FROM settings"))
self.searchSystem.setCurrentIndex(int(settings[0][2]))
self.saveBrowserHistoryFlag.setChecked(bool(settings[1][2]))
self.saveSearchHistoryFlag.setChecked(bool(settings[2][2]))
conn.close()
def ClearBrowserHistory(self):
clearDialog = ClearDialog()
if clearDialog.exec():
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM browserHistory")
conn.commit()
conn.close()
def ClearSearchHistory(self):
clearDialog = ClearDialog()
if clearDialog.exec():
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM searchHistory")
conn.commit()
conn.close()
for window in windows:
window.LoadSearchHistory()
def OpenBookmarks(self):
self.bookmarks = Bookmarks()
self.bookmarks.show()
def ClearBookmarks(self):
clearDialog = ClearDialog()
if clearDialog.exec():
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM bookmarks")
conn.commit()
conn.close()
for window in windows:
window.LoadBookmarksTab()
def LoadIcons(self):
icons = [
QtGui.QIcon(ResourcePath("svgs/google.png")),
QtGui.QIcon(ResourcePath("svgs/yandex.png")),
QtGui.QIcon(ResourcePath("svgs/bing.png")),
QtGui.QIcon(ResourcePath("svgs/yahoo.png")),
QtGui.QIcon(ResourcePath("svgs/duckduckgo.png"))]
for i, icon in enumerate(icons):
self.searchSystem.setItemIcon(i, icon)
self.searchSystem.setIconSize(QSize(35, 35))
class BrowserHistory(QtWidgets.QWidget, HistoryUI.Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle(" ")
self.SetUpFunctions()
def SetUpFunctions(self):
self.backButton.clicked.connect(self.Close)
self.historyName.setText("История браузера")
self.LoadBrowserHistory()
self.historyTable.itemDoubleClicked.connect(self.OpenPage)
self.deleteSelectedButton.clicked.connect(self.DeleteSelected)
def Close(self):
self.close()
def LoadBrowserHistory(self):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
data = list(cursor.execute("SELECT id, url, title, timestamp FROM browserHistory"))
self.historyTable.setColumnCount(4)
self.historyTable.setHorizontalHeaderLabels(("ID", "Ссылка", "Заголовок", "Время"))
self.historyTable.setRowCount(len(data))
for row in range(len(data)):
for column in range(4):
item = QtWidgets.QTableWidgetItem(str(data[row][column]))
self.historyTable.setItem(row, column, item)
self.historyTable.setColumnWidth(0, 60)
self.historyTable.setColumnWidth(1, 400)
self.historyTable.setColumnWidth(2, 400)
self.historyTable.setColumnWidth(3, 200)
self.historyTable.setColumnHidden(0, True)
def OpenPage(self):
url = self.historyTable.item(self.historyTable.currentRow(), 1).text()
page = Browser()
page.LoadUrl(url)
windows.append(page)
windows[-1].show()
def DeleteSelected(self):
if self.historyTable.item(self.historyTable.currentRow(), 0) != None:
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
selectedID = int(self.historyTable.item(self.historyTable.currentRow(), 0).text())
self.historyTable.removeRow(self.historyTable.currentRow())
cursor.execute("DELETE FROM browserHistory WHERE id = ?", (selectedID, ))
conn.commit()
conn.close()
class SearchHistory(QtWidgets.QWidget, HistoryUI.Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle(" ")
self.SetUpFunctions()
def SetUpFunctions(self):
self.backButton.clicked.connect(self.Close)
self.historyName.setText("История поиска")
self.LoadSearchHistory()
self.historyTable.itemDoubleClicked.connect(self.OpenPage)
self.deleteSelectedButton.clicked.connect(self.DeleteSelected)
def Close(self):
self.close()
def LoadSearchHistory(self):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
data = list(cursor.execute("SELECT searchHistory.id, searchHistory.query, browserHistory.url, searchHistory.timestamp FROM searchHistory JOIN browserHistory ON searchHistory.id = browserHistory.id"))
self.historyTable.setColumnCount(4)
self.historyTable.setHorizontalHeaderLabels(("ID", "Запрос", "url", "Время"))
self.historyTable.setRowCount(len(data))
for row in range(len(data)):
for column in range(4):
item = QtWidgets.QTableWidgetItem(str(data[row][column]))
self.historyTable.setItem(row, column, item)
self.historyTable.setColumnWidth(0, 60)
self.historyTable.setColumnWidth(1, 400)
self.historyTable.setColumnWidth(2, 400)
self.historyTable.setColumnHidden(0, True)
def OpenPage(self):
url = self.historyTable.item(self.historyTable.currentRow(), 1).text()
page = Browser()
page.LoadUrl(page.searchStart + url)
windows.append(page)
windows[-1].show()
def DeleteSelected(self):
if self.historyTable.item(self.historyTable.currentRow(), 0) != None:
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
selectedID = int(self.historyTable.item(self.historyTable.currentRow(), 0).text())
self.historyTable.removeRow(self.historyTable.currentRow())
cursor.execute("DELETE FROM searchHistory WHERE id = ?", (selectedID, ))
conn.commit()
conn.close()
for window in windows:
window.LoadSearchHistory()
class Bookmarks(QtWidgets.QWidget, HistoryUI.Ui_Form):
def __init__(self):
super().__init__()
self.setupUi(self)
self.setWindowTitle(" ")
self.SetUpFunctions()
def SetUpFunctions(self):
self.backButton.clicked.connect(self.Close)
self.historyName.setText("Закладки")
self.LoadBookmarks()
self.historyTable.itemDoubleClicked.connect(self.OpenPage)
self.deleteSelectedButton.clicked.connect(self.DeleteSelected)
def Close(self):
self.close()
def LoadBookmarks(self):
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
data = list(cursor.execute("SELECT id, bookmarkName, bookmarkUrl FROM bookmarks"))
self.historyTable.setColumnCount(3)
self.historyTable.setHorizontalHeaderLabels(("ID", "Название", "Ссылка"))
self.historyTable.setRowCount(len(data))
for row in range(len(data)):
for column in range(3):
item = QtWidgets.QTableWidgetItem(str(data[row][column]))
self.historyTable.setItem(row, column, item)
self.historyTable.setColumnWidth(0, 60)
self.historyTable.setColumnWidth(1, 400)
self.historyTable.setColumnWidth(2, 400)
self.historyTable.setColumnHidden(0, True)
def OpenPage(self):
url = self.historyTable.item(self.historyTable.currentRow(), 2).text()
page = Browser()
page.LoadUrl(url)
windows.append(page)
windows[-1].show()
def DeleteSelected(self):
if self.historyTable.item(self.historyTable.currentRow(), 0) != None:
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
selectedID = int(self.historyTable.item(self.historyTable.currentRow(), 0).text())
self.historyTable.removeRow(self.historyTable.currentRow())
cursor.execute("DELETE FROM bookmarks WHERE id = ?", (selectedID, ))
conn.commit()
conn.close()
for window in windows:
window.LoadBookmarksTab()
class ClearDialog(QtWidgets.QDialog):
def __init__(self):
super().__init__()
self.setWindowTitle("Подтверждение очистки")
QBtn = QtWidgets.QDialogButtonBox.StandardButton.Yes | QtWidgets.QDialogButtonBox.StandardButton.No
self.buttonBox = QtWidgets.QDialogButtonBox(QBtn)
self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject)
self.layout = QtWidgets.QVBoxLayout()
message = QtWidgets.QLabel("Вы уверены что хотите очистить?")
self.layout.addWidget(message)
self.layout.addWidget(self.buttonBox)
self.setLayout(self.layout)
def CreateTables():
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
# История поиска
cursor.execute('''
CREATE TABLE IF NOT EXISTS searchHistory (
id INTEGER PRIMARY KEY,
query TEXT NOT NULL,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# История посещений
cursor.execute('''
CREATE TABLE IF NOT EXISTS browserHistory (
id INTEGER PRIMARY KEY,
url TEXT NOT NULL,
title TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
# Настройки
cursor.execute('''
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY,
settingName TEXT NOT NULL,
settingValue INTEGER
)
''')
# Закладки
cursor.execute('''
CREATE TABLE IF NOT EXISTS bookmarks (
id INTEGER PRIMARY KEY,
bookmarkName TEXT NOT NULL,
bookmarkUrl TEXT NOT NULL
)
''')
settingsExist = cursor.execute("SELECT * FROM settings")
if list(settingsExist) == []:
cursor.executemany("INSERT INTO settings (id, settingName, settingValue) VALUES (?, ?, ?)", defaultSettings)
conn.commit()
conn.close()
def ResourcePath(relativePath):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relativePath)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
windows = []
defaultSettings = [
(0, "searchSystem", 0),
(1, "saveBrowserHistoryFlag", 1),
(2, "saveSearchHistoryFlag", 1)]
urlStarts = [
"https://",
"http://"]
CreateTables()
conn = sqlite3.connect("browser.db")
cursor = conn.cursor()
sessionSettings = list(cursor.execute("SELECT * FROM settings"))
conn.close()
browser = Browser()
windows.append(browser)
windows[0].show()
app.exec()