vorta/src/vorta/views/snapshots_tab.py

95 lines
3.8 KiB
Python
Raw Normal View History

from datetime import timedelta
from PyQt5 import uic
2018-10-27 17:24:34 +00:00
from PyQt5.QtWidgets import QFileDialog, QTableWidgetItem, QTableView, QHeaderView
from vorta.borg.borg_thread import BorgThread
from ..utils import get_asset, keyring, pretty_bytes
from ..models import BackupProfileMixin
2018-10-27 17:24:34 +00:00
uifile = get_asset('UI/snapshottab.ui')
2018-10-27 17:24:34 +00:00
SnapshotUI, SnapshotBase = uic.loadUiType(uifile)
class SnapshotTab(SnapshotBase, SnapshotUI, BackupProfileMixin):
2018-10-27 17:24:34 +00:00
def __init__(self, parent=None):
super().__init__(parent)
self.setupUi(parent)
header = self.snapshotTable.horizontalHeader()
header.setVisible(True)
header.setSectionResizeMode(0, QHeaderView.ResizeToContents)
2018-10-27 17:24:34 +00:00
header.setSectionResizeMode(1, QHeaderView.ResizeToContents)
header.setSectionResizeMode(2, QHeaderView.ResizeToContents)
header.setSectionResizeMode(3, QHeaderView.Stretch)
2018-10-27 17:24:34 +00:00
self.snapshotTable.setSelectionBehavior(QTableView.SelectRows)
self.snapshotTable.setEditTriggers(QTableView.NoEditTriggers)
self.snapshotMountButton.clicked.connect(self.snapshot_mount)
self.snapshotDeleteButton.clicked.connect(self.snapshot_mount)
self.snapshotRefreshButton.clicked.connect(self.snapshot_mount)
self.populate()
def set_status(self, text):
self.mountErrors.setText(text)
self.mountErrors.repaint()
2018-10-27 17:24:34 +00:00
def populate(self):
if self.profile.repo:
snapshots = [s for s in self.profile.repo.snapshots.select()]
for row, snapshot in enumerate(snapshots):
self.snapshotTable.insertRow(row)
formatted_time = snapshot.time.strftime('%Y-%m-%d %H:%M')
self.snapshotTable.setItem(row, 0, QTableWidgetItem(formatted_time))
self.snapshotTable.setItem(row, 1, QTableWidgetItem(pretty_bytes(snapshot.size)))
if snapshot.duration:
formatted_duration = str(timedelta(seconds=round(snapshot.duration)))
else:
formatted_duration = 'N/A'
self.snapshotTable.setItem(row, 2, QTableWidgetItem(formatted_duration))
self.snapshotTable.setItem(row, 3, QTableWidgetItem(snapshot.name))
2018-10-27 17:24:34 +00:00
self.snapshotTable.setRowCount(len(snapshots))
2018-11-02 11:14:54 +00:00
else:
self.snapshotTable.setRowCount(0)
2018-10-27 17:24:34 +00:00
def snapshot_mount(self):
profile = self.profile
2018-10-27 17:24:34 +00:00
cmd = ['borg', 'mount', '--log-json']
row_selected = self.snapshotTable.selectionModel().selectedRows()
if row_selected:
snapshot_cell = self.snapshotTable.item(row_selected[0].row(), 3)
2018-10-27 17:24:34 +00:00
if snapshot_cell:
snapshot_name = snapshot_cell.text()
cmd.append(f'{profile.repo.url}::{snapshot_name}')
2018-10-27 17:24:34 +00:00
else:
cmd.append(f'{profile.repo.url}')
2018-10-27 17:24:34 +00:00
else:
cmd.append(f'{profile.repo.url}')
2018-10-27 17:24:34 +00:00
options = QFileDialog.Options()
options |= QFileDialog.ShowDirsOnly
options |= QFileDialog.DontUseNativeDialog
mountPoint = QFileDialog.getExistingDirectory(
self, "Choose Mount Point", "", options=options)
if mountPoint:
cmd.append(mountPoint)
self.set_status('Mounting snapshot into folder...')
params = {'password': keyring.get_password("vorta-repo", profile.repo.url)}
self.snapshotMountButton.setEnabled(False)
thread = BorgThread(cmd, params, parent=self)
2018-10-27 17:24:34 +00:00
thread.updated.connect(self.mount_update_log)
thread.result.connect(self.mount_get_result)
thread.start()
def mount_update_log(self, text):
self.mountErrors.setText(text)
def mount_get_result(self, result):
self.snapshotMountButton.setEnabled(True)
2018-10-27 17:24:34 +00:00
if result['returncode'] == 0:
self.set_status('Mounted successfully.')