-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathsnapshotsdialog.py
450 lines (355 loc) · 15.7 KB
/
snapshotsdialog.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
# Back In Time
# Copyright (C) 2008-2022 Oprea Dan, Bart de Koning, Richard Bailey, Germar Reitze
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import os
import subprocess
import shlex
from PyQt6.QtGui import *
from PyQt6.QtWidgets import *
from PyQt6.QtCore import *
import tools
import restoredialog
import messagebox
import qttools
import snapshots
if tools.checkCommand('meld'):
DIFF_CMD = 'meld'
DIFF_PARAMS = '%1 %2'
elif tools.checkCommand('kompare'):
DIFF_CMD = 'kompare'
DIFF_PARAMS = '%1 %2'
else:
DIFF_CMD = 'false'
DIFF_PARAMS = '%1 %2'
class DiffOptionsDialog(QDialog):
def __init__(self, parent):
super(DiffOptionsDialog, self).__init__(parent)
self.config = parent.config
import icon
self.setWindowIcon(icon.DIFF_OPTIONS)
self.setWindowTitle(_('Options about comparing snapshots'))
self.mainLayout = QGridLayout(self)
self.diffCmd = self.config.strValue('qt.diff.cmd', DIFF_CMD)
self.diffParams = self.config.strValue('qt.diff.params', DIFF_PARAMS)
self.mainLayout.addWidget(QLabel(_('Command') + ':'), 0, 0)
self.editCmd = QLineEdit(self.diffCmd, self)
self.mainLayout.addWidget(self.editCmd, 0, 1)
self.mainLayout.addWidget(QLabel(_('Parameters') + ':'), 1, 0)
self.editParams = QLineEdit(self.diffParams, self)
self.mainLayout.addWidget(self.editParams, 1, 1)
self.mainLayout.addWidget(QLabel(_('Use %1 and %2 for path parameters')), 2, 1)
buttonBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
self.mainLayout.addWidget(buttonBox, 3, 0, 3, 2)
def accept(self):
diffCmd = self.editCmd.text()
diffParams = self.editParams.text()
if diffCmd != self.diffCmd or diffParams != self.diffParams:
self.config.setStrValue('qt.diff.cmd', diffCmd)
self.config.setStrValue('qt.diff.params', diffParams)
self.config.save()
super(DiffOptionsDialog, self).accept()
class SnapshotsDialog(QDialog):
def __init__(self, parent, sid, path):
super(SnapshotsDialog, self).__init__(parent)
self.parent = parent
self.config = parent.config
self.snapshots = parent.snapshots
self.snapshotsList = parent.snapshotsList
self.qapp = parent.qapp
import icon
self.sid = sid
self.path = path
self.setWindowIcon(icon.SNAPSHOTS)
self.setWindowTitle(_('Snapshots'))
self.mainLayout = QVBoxLayout(self)
#path
self.editPath = QLineEdit(self.path, self)
self.editPath.setReadOnly(True)
self.mainLayout.addWidget(self.editPath)
#list different snapshots only
self.cbOnlyDifferentSnapshots = QCheckBox(
_('Differing snapshots only'), self)
self.mainLayout.addWidget(self.cbOnlyDifferentSnapshots)
self.cbOnlyDifferentSnapshots.stateChanged.connect(self.cbOnlyDifferentSnapshotsChanged)
#list equal snapshots only
layout = QHBoxLayout()
self.mainLayout.addLayout(layout)
self.cbOnlyEqualSnapshots = QCheckBox(
_('List only equal snapshots to: '), self)
self.cbOnlyEqualSnapshots.stateChanged.connect(
self.cbOnlyEqualSnapshotsChanged)
layout.addWidget(self.cbOnlyEqualSnapshots)
self.comboEqualTo = qttools.SnapshotCombo(self)
self.comboEqualTo.currentIndexChanged.connect(self.comboEqualToChanged)
self.comboEqualTo.setEnabled(False)
layout.addWidget(self.comboEqualTo)
# deep check
self.cbDeepCheck = QCheckBox(_('Deep check (more accurate, but slow)'), self)
self.mainLayout.addWidget(self.cbDeepCheck)
self.cbDeepCheck.stateChanged.connect(self.cbDeepCheckChanged)
#toolbar
self.toolbar = QToolBar(self)
self.toolbar.setFloatable(False)
self.mainLayout.addWidget(self.toolbar)
#toolbar restore
menuRestore = QMenu(self)
action = menuRestore.addAction(icon.RESTORE, _('Restore'))
action.triggered.connect(self.restoreThis)
action = menuRestore.addAction(icon.RESTORE_TO, _('Restore to …'))
action.triggered.connect(self.restoreThisTo)
self.btnRestore = self.toolbar.addAction(icon.RESTORE, _('Restore'))
self.btnRestore.setMenu(menuRestore)
self.btnRestore.triggered.connect(self.restoreThis)
#btn delete
self.btnDelete = self.toolbar.addAction(icon.DELETE_FILE, _('Delete'))
self.btnDelete.triggered.connect(self.btnDeleteClicked)
#btn select_all
self.btnSelectAll = self.toolbar.addAction(icon.SELECT_ALL, _('Select All'))
self.btnSelectAll.triggered.connect(self.btnSelectAllClicked)
#snapshots list
self.timeLine = qttools.TimeLine(self)
self.mainLayout.addWidget(self.timeLine)
self.timeLine.itemSelectionChanged.connect(self.timeLineChanged)
self.timeLine.itemActivated.connect(self.timeLineExecute)
#diff
layout = QHBoxLayout()
self.mainLayout.addLayout(layout)
self.btnDiff = QPushButton(_('Compare'), self)
layout.addWidget(self.btnDiff)
self.btnDiff.clicked.connect(self.btnDiffClicked)
self.comboDiff = qttools.SnapshotCombo(self)
layout.addWidget(self.comboDiff, 2)
#buttons
buttonBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
self.btnGoto = buttonBox.button(QDialogButtonBox.StandardButton.Ok)
self.btnCancel = buttonBox.button(QDialogButtonBox.StandardButton.Cancel)
self.btnGoto.setText(_('Go To'))
btnDiffOptions = buttonBox.addButton(_('Options'), QDialogButtonBox.ButtonRole.HelpRole)
btnDiffOptions.setIcon(icon.DIFF_OPTIONS)
self.mainLayout.addWidget(buttonBox)
buttonBox.accepted.connect(self.accept)
buttonBox.rejected.connect(self.reject)
btnDiffOptions.clicked.connect(self.btnDiffOptionsClicked)
#
self.cbDeepCheck.setEnabled(False)
full_path = self.sid.pathBackup(self.path)
if os.path.islink(full_path):
self.cbDeepCheck.hide()
elif os.path.isdir(full_path):
self.cbOnlyDifferentSnapshots.hide()
self.cbOnlyEqualSnapshots.hide()
self.comboEqualTo.hide()
self.cbDeepCheck.hide()
#update list and combobox
self.UpdateSnapshotsAndComboEqualTo()
def addSnapshot(self, sid):
self.timeLine.addSnapshot(sid)
#add to combo
self.comboDiff.addSnapshotID(sid)
if self.sid == sid:
self.comboDiff.setCurrentSnapshotID(sid)
self.comboDiff.checkSelection()
def updateSnapshots(self):
self.timeLine.clear()
self.comboDiff.clear()
equal_to_sid = self.comboEqualTo.currentSnapshotID()
if self.cbOnlyEqualSnapshots.isChecked() and equal_to_sid:
equal_to = equal_to_sid.pathBackup(self.path)
else:
equal_to = False
snapshotsFiltered = self.snapshots.filter(self.sid, self.path,
self.snapshotsList,
self.cbOnlyDifferentSnapshots.isChecked(),
self.cbDeepCheck.isChecked(),
equal_to)
for sid in snapshotsFiltered:
self.addSnapshot(sid)
self.updateToolbar()
def UpdateComboEqualTo(self):
self.comboEqualTo.clear()
snapshotsFiltered = self.snapshots.filter(self.sid, self.path, self.snapshotsList)
for sid in snapshotsFiltered:
self.comboEqualTo.addSnapshotID(sid)
if sid == self.sid:
self.comboEqualTo.setCurrentSnapshotID(sid)
self.comboEqualTo.checkSelection()
def UpdateSnapshotsAndComboEqualTo(self):
self.updateSnapshots()
self.UpdateComboEqualTo()
def cbOnlyDifferentSnapshotsChanged(self):
enabled = self.cbOnlyDifferentSnapshots.isChecked()
self.cbOnlyEqualSnapshots.setEnabled(not enabled)
self.cbDeepCheck.setEnabled(enabled)
self.updateSnapshots()
def cbOnlyEqualSnapshotsChanged(self):
enabled = self.cbOnlyEqualSnapshots.isChecked()
self.comboEqualTo.setEnabled(enabled)
self.cbOnlyDifferentSnapshots.setEnabled(not enabled)
self.cbDeepCheck.setEnabled(enabled)
self.updateSnapshots()
def cbDeepCheckChanged(self):
self.updateSnapshots()
def updateToolbar(self):
sids = self.timeLine.selectedSnapshotIDs()
if not sids:
enable_restore = False
enable_delete = False
elif len(sids) == 1:
enable_restore = not sids[0].isRoot
enable_delete = not sids[0].isRoot
else:
enable_restore = False
enable_delete = True
for sid in sids:
if sid.isRoot:
enable_delete = False
self.btnRestore.setEnabled(enable_restore)
self.btnDelete.setEnabled(enable_delete)
def restoreThis(self):
# See #1485 as related bug report
sid = self.timeLine.currentSnapshotID()
if not sid.isRoot:
restoredialog.restore(self, sid, self.path) # pylint: disable=E1101
def restoreThisTo(self):
# See #1485 as related bug report
sid = self.timeLine.currentSnapshotID()
if not sid.isRoot:
restoredialog.restore(self, sid, self.path, None) # pylint: disable=E1101
def timeLineChanged(self):
self.updateToolbar()
def timeLineExecute(self, item, column):
if self.qapp.keyboardModifiers() and Qt.ControlModifier:
return
sid = self.timeLine.currentSnapshotID()
if not sid:
return
full_path = sid.pathBackup(self.path)
if not os.path.exists(full_path):
return
# prevent backup data from being accidentally overwritten
# by create a temporary local copy and only open that one
if not isinstance(self.sid, snapshots.RootSnapshot):
full_path = self.parent.tmpCopy(full_path, sid)
self.run = QDesktopServices.openUrl(QUrl(full_path))
def btnDiffClicked(self):
sid1 = self.timeLine.currentSnapshotID()
sid2 = self.comboDiff.currentSnapshotID()
if not sid1 or not sid2:
return
path1 = sid1.pathBackup(self.path)
path2 = sid2.pathBackup(self.path)
# check if the 2 paths are different
if path1 == path2:
messagebox.critical(
self, _("You can't compare a snapshot to itself."))
return
diffCmd = self.config.strValue('qt.diff.cmd', DIFF_CMD)
diffParams = self.config.strValue('qt.diff.params', DIFF_PARAMS)
if not tools.checkCommand(diffCmd):
messagebox.critical(
self, '{}: {}'.format(_('Command not found'), diffCmd)
)
return
# prevent backup data from being accidentally overwritten
# by create a temporary local copy and only open that one
if not isinstance(sid1, snapshots.RootSnapshot):
path1 = self.parent.tmpCopy(path1, sid1)
if not isinstance(sid2, snapshots.RootSnapshot):
path2 = self.parent.tmpCopy(path2, sid2)
params = diffParams
params = params.replace('%1', '"%s"' %path1)
params = params.replace('%2', '"%s"' %path2)
cmd = diffCmd + ' ' + params
subprocess.Popen(shlex.split(cmd))
def btnDiffOptionsClicked(self):
DiffOptionsDialog(self).exec()
def comboEqualToChanged(self, index):
self.updateSnapshots()
def btnDeleteClicked(self):
items = self.timeLine.selectedItems()
if not items:
return
elif len(items) == 1:
msg = _('Do you really want to delete {file} in snapshot '
'{snapshot_id}?').format(
file=f'"{self.path}"',
snapshot_id=f'"{items[0].snapshotID()}"')
else:
msg = _('Do you really want to delete {file} in {count} '
'snapshots?').format(
file=f'"{self.path}"', count=len(items))
msg = '{}\n{}: {}'.format(
msg, _('WARNING'), _('This cannot be revoked!'))
answer = messagebox.warningYesNo(self, msg)
if answer == QMessageBox.StandardButton.Yes:
for item in items:
item.setFlags(Qt.ItemFlag.NoItemFlags)
thread = RemoveFileThread(self, items)
thread.started.connect(lambda: self.btnGoto.setDisabled(True))
thread.finished.connect(lambda: self.btnGoto.setDisabled(False))
thread.started.connect(lambda: self.btnDelete.setDisabled(True))
thread.finished.connect(lambda: self.btnDelete.setDisabled(False))
thread.finished.connect(self.UpdateSnapshotsAndComboEqualTo)
self.btnCancel.clicked.connect(thread.terminate)
thread.start()
exclude = self.config.exclude()
msg = _('Exclude {path} from future snapshots?').format(
path=f'"{self.path}"')
if self.path not in exclude:
answer = messagebox.warningYesNo(self, msg)
if answer == QMessageBox.StandardButton.Yes:
exclude.append(self.path)
self.config.setExclude(exclude)
def btnSelectAllClicked(self):
"""
select all expect 'Now'
"""
self.timeLine.clearSelection()
for item in self.timeLine.iterSnapshotItems():
if not isinstance(item.snapshotID(), snapshots.RootSnapshot):
item.setSelected(True)
def accept(self):
sid = self.timeLine.currentSnapshotID()
if sid:
self.sid = sid
super(SnapshotsDialog, self).accept()
class RemoveFileThread(QThread):
"""
remove files in background thread so GUI will not freeze
"""
def __init__(self, parent, items):
self.parent = parent
self.config = parent.config
self.snapshots = parent.snapshots
self.items = items
super(RemoveFileThread, self).__init__(parent)
def run(self):
#inhibit suspend/hibernate during delete
self.config.inhibitCookie = tools.inhibitSuspend(toplevel_xid = self.config.xWindowId,
reason = 'deleting files')
for item in self.items:
self.snapshots.deletePath(item.snapshotID(), self.parent.path)
try:
item.setHidden(True)
except RuntimeError:
#item has been deleted
#probably because user refreshed treeview
pass
#release inhibit suspend
if self.config.inhibitCookie:
self.config.inhibitCookie = tools.unInhibitSuspend(*self.config.inhibitCookie)