-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
225 lines (174 loc) · 6.76 KB
/
main.py
File metadata and controls
225 lines (174 loc) · 6.76 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
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
#!/usr/bin/env python3
import sys
import os
import signal
from PySide6.QtWidgets import (
QApplication,
QWidget,
QVBoxLayout,
QHBoxLayout,
QLabel,
QComboBox,
QPushButton,
QFileDialog,
QMessageBox,
)
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont, QPalette, QColor, QColorConstants, QIcon
import locations
import utils
def show_file_error():
QMessageBox.critical(None, "Error", "File not found.")
def show_empty_alert():
QMessageBox.critical(
None, "Error", f"No data found in the backup file {utils.BACKUP_FILE}"
)
def show_success_export():
QMessageBox.information(
None,
"Success",
f"Search Engines exported successfully in {utils.BACKUP_FILE}",
)
def show_success_import(path):
QMessageBox.information(
None, "Success", f"Search Engines imported successfully in {path}"
)
def handle_conflicts_dialogs(conflicts):
"""Show dialogs for each conflict and return rows to replace."""
to_replace = []
for key, old_row, new_row in conflicts:
diff = utils.compare_rows(old_row, new_row)
name = new_row[1] if new_row[1] else "Unknown"
url = new_row[4]
shortcut = new_row[2] if new_row[2] else ""
msg_box = QMessageBox()
msg_box.setWindowTitle("Conflict Detected")
msg_box.setTextFormat(Qt.TextFormat.RichText)
text = f"""<p>{name}</p>
<p>Shortcut: {shortcut}</p>
<p>URL: {url}</p>
<p>{diff}</p>"""
msg_box.setText(text)
msg_box.setStandardButtons(
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
)
msg_box.button(QMessageBox.StandardButton.Yes).setText("Replace")
msg_box.button(QMessageBox.StandardButton.No).setText("Keep Existing")
response = msg_box.exec()
if response == QMessageBox.StandardButton.Yes:
to_replace.append(new_row)
return to_replace
def import_into_browser():
"""Import the JSON backup into the selected browser."""
base_path = locations.get_browser_path(bw_sel.currentText().strip()) or ""
# Construct the full path to Web Data file
web_data_path = os.path.join(base_path, "Web Data") if base_path else ""
# If Web Data exists, use it as the pre-selected file; otherwise, use the directory
start_path = web_data_path if os.path.exists(web_data_path) else base_path
file_path, _ = QFileDialog.getOpenFileName(
None,
"Select Web Data file",
start_path,
"Web Data SQLite (Web Data*);;All Files (*)",
)
if not file_path:
show_file_error()
return
print(f"Importing from {file_path}")
filas = utils.json_read(utils.BACKUP_FILE)
if len(filas) == 0:
show_empty_alert()
return
to_insert, conflicts = utils.handle_import_conflicts(file_path, filas)
to_replace = handle_conflicts_dialogs(conflicts)
try:
if to_insert:
utils.db_insert_rows(file_path, to_insert, "ignore")
if to_replace:
utils.db_insert_rows(file_path, to_replace, "replace")
show_success_import(file_path)
except Exception as e:
QMessageBox.critical(None, "Error", f"{e}")
def export_from_browser(bw_sel):
"""Export Search Engines from the selected browser to a JSON file."""
base_path = locations.get_browser_path(bw_sel.currentText().strip()) or ""
# Construct the full path to Web Data file
web_data_path = os.path.join(base_path, "Web Data") if base_path else ""
# If Web Data exists, use it as the pre-selected file; otherwise, use the directory
start_path = web_data_path if os.path.exists(web_data_path) else base_path
file_path, _ = QFileDialog.getOpenFileName(
None,
"Select Web Data file",
start_path,
"Web Data SQLite (Web Data*);;All Files (*)",
)
if not file_path:
show_file_error()
return
filas = utils.db_read_keywords(file_path)
utils.json_write(filas)
show_success_export()
def select_browser():
print(f"Browser: {bw_sel.currentText()}")
def setup_dark_theme(app):
"""Configure the dark theme using the Fusion style."""
app.setStyle("Fusion")
dark_palette = QPalette()
dark_palette.setColor(QPalette.ColorRole.Window, QColor(53, 53, 53))
dark_palette.setColor(QPalette.ColorRole.WindowText, QColorConstants.White)
dark_palette.setColor(QPalette.ColorRole.Base, QColor(35, 35, 35))
dark_palette.setColor(QPalette.ColorRole.AlternateBase, QColor(53, 53, 53))
dark_palette.setColor(QPalette.ColorRole.ToolTipBase, QColor(25, 25, 25))
dark_palette.setColor(QPalette.ColorRole.ToolTipText, QColorConstants.White)
dark_palette.setColor(QPalette.ColorRole.Text, QColorConstants.White)
dark_palette.setColor(QPalette.ColorRole.Button, QColor(53, 53, 53))
dark_palette.setColor(QPalette.ColorRole.ButtonText, QColorConstants.White)
dark_palette.setColor(QPalette.ColorRole.BrightText, QColorConstants.Red)
dark_palette.setColor(QPalette.ColorRole.Link, QColor(42, 130, 218))
dark_palette.setColor(QPalette.ColorRole.Highlight, QColor(42, 130, 218))
dark_palette.setColor(
QPalette.ColorRole.HighlightedText, QColor(35, 35, 35)
)
app.setPalette(dark_palette)
app = QApplication(sys.argv)
setup_dark_theme(app)
win = QWidget()
win.setWindowTitle("Browser Search Engines")
win.setWindowIcon(QIcon("images/icon.png"))
win.setMinimumSize(400, 200)
main_layout = QVBoxLayout()
main_layout.setSpacing(10)
frame_layout = QVBoxLayout()
frame_layout.setSpacing(10)
label_warning = QLabel("Close Browser before import or export")
label_warning.setFont(QFont("Arial", 14, QFont.Weight.Bold))
label_warning.setAlignment(Qt.AlignmentFlag.AlignCenter)
frame_layout.addWidget(label_warning)
label_instructions = QLabel(
"Export from Browser Web Data SQLite to a JSON file\n"
"Import from a JSON file into a Browser Web Data SQLite"
)
label_instructions.setAlignment(Qt.AlignmentFlag.AlignCenter)
frame_layout.addWidget(label_instructions)
bw_sel = QComboBox()
bw_sel.addItems([b.capitalize() for b in locations.LOCATIONS.keys()])
bw_sel.currentTextChanged.connect(select_browser)
bw_sel.setMinimumHeight(35)
frame_layout.addWidget(bw_sel)
main_layout.addLayout(frame_layout)
main_layout.addStretch()
buttons_layout = QHBoxLayout()
buttons_layout.setSpacing(10)
btn_export = QPushButton("Export from Browser")
btn_export.clicked.connect(lambda: export_from_browser(bw_sel))
btn_export.setMinimumHeight(35)
buttons_layout.addWidget(btn_export)
btn_import = QPushButton("Import into Browser")
btn_import.clicked.connect(import_into_browser)
btn_import.setMinimumHeight(35)
buttons_layout.addWidget(btn_import)
main_layout.addLayout(buttons_layout)
win.setLayout(main_layout)
win.show()
signal.signal(signal.SIGINT, signal.SIG_DFL)
sys.exit(app.exec())