-
Notifications
You must be signed in to change notification settings - Fork 5
/
optionsWindow.py
222 lines (197 loc) · 8.69 KB
/
optionsWindow.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
from Sources.json_handler import json_handler
from UI.ui_OptionsWindow import Ui_OptionsWindow
from UI.ui_MainWindow import *
import logWindow
import handler
import Sources.drive as drive
from PyQt5.QtWidgets import (
QMessageBox,
QDialog
)
from PyQt5.QtCore import QObject, QThread, pyqtSignal
from logger_settings import logger
class OptionsWindow(QtWidgets.QMainWindow, Ui_OptionsWindow):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
Ui_OptionsWindow.__init__(self)
self.init_ui()
def init_ui(self):
self.setupUi(self)
self.setFixedSize(812,706)
#Setting values
self.set_backup_options()
# view handler
self.bt_back.clicked.connect(self.backToMain)
self.bt_log_viewer.clicked.connect(self.reload)
self.bt_drive.clicked.connect(self.backToMain)
# Enable local delete backups
self.chk_delete_local.toggled.connect(self.delete_toggle_local)
self.bt_number_backups_local.clicked.connect(self.save_backups_local)
# Disable/Enabled download button in function of authenticated
json_data = json_handler()
self.bt_download.setEnabled(json_data.get_list("DRIVE","AUTHENTICATED"))
# Enable cloud delete backups
self.chk_delete_cloud.toggled.connect(self.delete_toggle_cloud)
self.bt_number_backups_cloud.clicked.connect(self.save_backups_cloud)
# Handlers
self.bt_download.clicked.connect(self.download_dialog)
def set_backup_options(self):
json_data = json_handler()
# Local
state_auto_local = json_data.get_list("OPTIONS","DELETE_BACKUP_LOCAL")
self.chk_delete_local.setChecked(state_auto_local)
self.sp_number_local.setEnabled(state_auto_local)
self.bt_number_backups_local.setEnabled(state_auto_local)
self.sp_number_local.setValue(json_data.get_list("OPTIONS","NUM_BACKUP_LOCAL"))
# Cloud
state_auto_cloud = json_data.get_list("OPTIONS","DELETE_BACKUP_CLOUD")
self.chk_delete_cloud.setChecked(state_auto_cloud)
self.sp_number_cloud.setEnabled(state_auto_cloud)
self.bt_number_backups_cloud.setEnabled(state_auto_cloud)
self.sp_number_cloud.setValue(json_data.get_list("OPTIONS","NUM_BACKUP_CLOUD"))
def delete_toggle_local(self):
json_data = json_handler()
state = True
if not self.chk_delete_local.isChecked():
state = False
self.sp_number_local.setEnabled(state)
self.bt_number_backups_local.setEnabled(state)
json_data.write_field("OPTIONS",state,"DELETE_BACKUP_LOCAL")
def save_backups_local(self):
json_data = json_handler()
json_data.write_field("OPTIONS",int(self.sp_number_local.text()),"NUM_BACKUP_LOCAL")
QMessageBox.information(self, "Info", "Local backup options saved")
def delete_toggle_cloud(self):
json_data = json_handler()
state = True
if not self.chk_delete_cloud.isChecked():
state = False
self.sp_number_cloud.setEnabled(state)
self.bt_number_backups_cloud.setEnabled(state)
json_data.write_field("OPTIONS",state,"DELETE_BACKUP_CLOUD")
def save_backups_cloud(self):
json_data = json_handler()
json_data.write_field("OPTIONS",int(self.sp_number_cloud.text()),"NUM_BACKUP_CLOUD")
QMessageBox.information(self, "Info", "Cloud backup options saved")
def reload(self):
self.hide()
self.ui = logWindow.LogWindow()
self.ui.show()
def backToMain(self):
self.hide()
self.ui = handler.MainWindow()
self.ui.show()
def download_dialog(self):
self.dialog = Download_Backup(self)
self.dialog.show()
class Download_Backup(QDialog):
def __init__(self, *args, **kwargs):
super(QDialog, self).__init__(*args, **kwargs)
self.init_ui()
def init_ui(self):
self.setObjectName("Download Backups")
self.setFixedSize(400, 322)
#self.setFixedSize(400, 300)
self.list_backups = QtWidgets.QListWidget(self)
self.list_backups.setGeometry(QtCore.QRect(70, 20, 256, 192))
self.list_backups.setObjectName("list_backups")
self.bt_download = QtWidgets.QPushButton(self)
self.bt_download.setGeometry(QtCore.QRect(160, 240, 75, 23))
self.bt_download.setObjectName("bt_download")
self.retranslateUi(self)
QtCore.QMetaObject.connectSlotsByName(self)
self.pr_download = QtWidgets.QProgressBar(self)
self.pr_download.setGeometry(QtCore.QRect(140, 280, 112, 23))
self.pr_download.setProperty("value", 0)
self.pr_download.setObjectName("pr_download")
self.chk_unzip = QtWidgets.QCheckBox(self)
self.chk_unzip.setGeometry(QtCore.QRect(320, 240, 51, 21))
self.chk_unzip.setObjectName("chk_unzip")
palette = QtGui.QPalette()
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.WindowText, brush)
brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
brush.setStyle(QtCore.Qt.SolidPattern)
palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.WindowText, brush)
self.chk_unzip.setPalette(palette)
self.chk_unzip.setText("Unzip")
# Data
json_data = json_handler()
self.chk_unzip.setChecked(json_data.get_list("OPTIONS", "UNZIP"))
files = drive.get_files(False)
# Load the data into the list
if files: [self.list_backups.addItem(name) for name in files.keys()]
# Handlers
self.bt_download.clicked.connect(lambda: self.download_thread(files))
self.chk_unzip.toggled.connect(self.unzip)
def unzip(self):
json_data = json_handler()
state = True
if not self.chk_unzip.isChecked():
state = False
json_data.write_field("OPTIONS", state, "UNZIP")
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Download Backups"))
self.bt_download.setText(_translate("Dialog", "Download"))
def download_thread(self, files):
# Initial actions
# If there's no backup selected, no actions will be executed
try:
if self.list_backups.selectedItems():
filename = self.list_backups.currentItem().text()
else:
QMessageBox.warning(self, "Warning", "Select a item to donwload")
return False
except:
return False
self.update_progress(0)
# Create a QThread object
self.thread = QThread()
# Create a worker object
self.worker = Worker()
# Move worker to the thread
self.worker.moveToThread(self.thread)
# Connect signals and slots
self.worker.error.connect(self.show_error)
self.thread.started.connect(lambda: self.worker.run(files[filename], filename))
self.worker.finished.connect(self.thread.quit)
self.worker.finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.worker.blk.connect(self.show_problems)
self.worker.progress.connect(self.update_progress)
# Start the thread
self.thread.start()
def update_progress(self, progress):
self.pr_download.setValue(progress)
self.bt_download.setEnabled(progress == 100 or progress == 0)
def show_problems(self, output):
if not output:
self.bt_download.setEnabled(True)
QMessageBox.warning(
self, "Warning", "You need to be authenticated")
def show_error(self):
QMessageBox.warning(
self, "Warning", "The backup you are trying to download already exist")
class Worker(QObject):
finished = pyqtSignal()
progress = pyqtSignal(int)
blk = pyqtSignal(bool)
error = pyqtSignal()
def run(self, file_id, filename):
try:
out = drive.download_drive(file_id, filename, self.update_progress)
self.blk.emit(out)
self.finished.emit()
except Exception as e:
self.error.emit()
print(e)
logger.error(e)
self.progress.emit(0)
self.finished.emit()
def update_progress(self, percent):
self.progress.emit(percent)