2020-06-22 07:59:28 +00:00
|
|
|
import os
|
2022-03-24 06:27:07 +00:00
|
|
|
from pathlib import PurePath
|
|
|
|
|
|
|
|
from PyQt5 import QtCore, QtGui, uic
|
|
|
|
from PyQt5.QtCore import QFileInfo, QMimeData, QPoint, Qt, QUrl, pyqtSlot
|
|
|
|
from PyQt5.QtWidgets import (QApplication, QHeaderView, QMenu, QMessageBox,
|
|
|
|
QShortcut, QTableWidgetItem)
|
|
|
|
|
|
|
|
from vorta.store.models import (BackupProfileMixin, SettingsModel,
|
|
|
|
SourceFileModel)
|
|
|
|
from vorta.utils import (FilePathInfoAsync, choose_file_dialog, get_asset,
|
|
|
|
pretty_bytes, sort_sizes)
|
|
|
|
from vorta.views.utils import get_colored_icon
|
2018-10-27 17:24:34 +00:00
|
|
|
|
2018-10-29 05:49:25 +00:00
|
|
|
uifile = get_asset('UI/sourcetab.ui')
|
2018-10-27 17:24:34 +00:00
|
|
|
SourceUI, SourceBase = uic.loadUiType(uifile)
|
|
|
|
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
class SourceColumn:
|
|
|
|
Path = 0
|
2022-03-24 06:27:07 +00:00
|
|
|
Size = 1
|
|
|
|
FilesCount = 2
|
2020-10-30 08:30:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SizeItem(QTableWidgetItem):
|
|
|
|
def __lt__(self, other):
|
2020-11-29 00:51:24 +00:00
|
|
|
if other.text() == '':
|
|
|
|
return False
|
|
|
|
elif self.text() == '':
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return sort_sizes([self.text(), other.text()]) == [self.text(), other.text()]
|
2020-10-30 08:30:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FilesCount(QTableWidgetItem):
|
|
|
|
def __lt__(self, other):
|
|
|
|
# Verify that conversion is only performed on valid integers
|
|
|
|
# If one of the 2 elements is no number, put these elements at the end
|
|
|
|
# This is important if the text is "Calculating..." or ""
|
|
|
|
if self.text().isdigit() and other.text().isdigit():
|
|
|
|
return int(self.text()) < int(other.text()) # Compare & return result
|
|
|
|
else:
|
|
|
|
if not self.text().isdigit():
|
|
|
|
return 1 # Move one down if current item has no valid count
|
|
|
|
if not other.text().isdigit():
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2018-10-31 16:09:01 +00:00
|
|
|
class SourceTab(SourceBase, SourceUI, BackupProfileMixin):
|
2020-10-30 08:30:52 +00:00
|
|
|
updateThreads = []
|
|
|
|
|
2018-10-27 17:24:34 +00:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.setupUi(parent)
|
|
|
|
|
2022-03-24 06:27:07 +00:00
|
|
|
# Prepare source files view
|
2020-10-30 08:30:52 +00:00
|
|
|
header = self.sourceFilesWidget.horizontalHeader()
|
|
|
|
header.setVisible(True)
|
|
|
|
header.setSortIndicatorShown(1)
|
|
|
|
|
|
|
|
header.setSectionResizeMode(SourceColumn.Path, QHeaderView.Stretch)
|
|
|
|
header.setSectionResizeMode(SourceColumn.Size, QHeaderView.ResizeToContents)
|
|
|
|
header.setSectionResizeMode(SourceColumn.FilesCount, QHeaderView.ResizeToContents)
|
|
|
|
|
|
|
|
self.sourceFilesWidget.setSortingEnabled(True)
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.setContextMenuPolicy(
|
|
|
|
Qt.ContextMenuPolicy.CustomContextMenu)
|
|
|
|
self.sourceFilesWidget.customContextMenuRequested.connect(
|
|
|
|
self.sourceitem_contextmenu)
|
|
|
|
|
|
|
|
# Prepare add button
|
|
|
|
self.addMenu = QMenu(self.addButton)
|
|
|
|
self.addFilesAction = self.addMenu.addAction(self.tr("Files"),
|
|
|
|
lambda: self.source_add(want_folder=False))
|
|
|
|
self.addFoldersAction = self.addMenu.addAction(self.tr("Folders"),
|
|
|
|
lambda: self.source_add(want_folder=True))
|
|
|
|
self.pasteAction = self.addMenu.addAction(self.tr("Paste"),
|
|
|
|
self.paste_text)
|
|
|
|
|
|
|
|
self.addButton.setMenu(self.addMenu)
|
|
|
|
|
|
|
|
# shortcuts
|
|
|
|
shortcut_copy = QShortcut(QtGui.QKeySequence.StandardKey.Copy,
|
|
|
|
self.sourceFilesWidget)
|
|
|
|
shortcut_copy.activated.connect(self.source_copy)
|
|
|
|
|
|
|
|
# Connect signals
|
|
|
|
self.removeButton.clicked.connect(self.source_remove)
|
|
|
|
self.updateButton.clicked.connect(self.sources_update)
|
2018-11-17 08:51:53 +00:00
|
|
|
self.excludePatternsField.textChanged.connect(self.save_exclude_patterns)
|
|
|
|
self.excludeIfPresentField.textChanged.connect(self.save_exclude_if_present)
|
2022-03-24 06:27:07 +00:00
|
|
|
|
|
|
|
# Populate
|
2018-11-17 08:51:53 +00:00
|
|
|
self.populate_from_profile()
|
2022-03-24 06:27:07 +00:00
|
|
|
self.set_icons()
|
|
|
|
|
|
|
|
def set_icons(self):
|
|
|
|
"Used when changing between light- and dark mode"
|
|
|
|
self.addButton.setIcon(get_colored_icon('plus'))
|
|
|
|
self.removeButton.setIcon(get_colored_icon('minus'))
|
|
|
|
self.updateButton.setIcon(get_colored_icon('refresh'))
|
|
|
|
self.addFilesAction.setIcon(get_colored_icon('file'))
|
|
|
|
self.addFoldersAction.setIcon(get_colored_icon('folder'))
|
|
|
|
self.pasteAction.setIcon(get_colored_icon('paste'))
|
|
|
|
|
|
|
|
for row in range(self.sourceFilesWidget.rowCount()):
|
|
|
|
path_item = self.sourceFilesWidget.item(row, SourceColumn.Path)
|
|
|
|
db_item = SourceFileModel.get(dir=path_item.text(),
|
|
|
|
profile=self.profile())
|
|
|
|
|
|
|
|
if db_item.path_isdir:
|
|
|
|
path_item.setIcon(get_colored_icon('folder'))
|
|
|
|
else:
|
|
|
|
path_item.setIcon(get_colored_icon('file'))
|
|
|
|
|
|
|
|
@pyqtSlot(QPoint)
|
|
|
|
def sourceitem_contextmenu(self, pos: QPoint):
|
|
|
|
"""Show a context menu for the source item at `pos`."""
|
|
|
|
# index under cursor
|
|
|
|
index = self.sourceFilesWidget.indexAt(pos)
|
|
|
|
if not index.isValid():
|
|
|
|
return # popup only for items
|
|
|
|
|
|
|
|
menu = QMenu(self.sourceFilesWidget)
|
|
|
|
|
|
|
|
menu.addAction(get_colored_icon('copy'), self.tr("Copy"),
|
|
|
|
lambda: self.source_copy(index=index))
|
|
|
|
menu.addAction(get_colored_icon('minus'), self.tr("Remove"),
|
|
|
|
self.source_remove)
|
|
|
|
|
|
|
|
menu.popup(self.sourceFilesWidget.viewport().mapToGlobal(pos))
|
2018-10-27 17:24:34 +00:00
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
def set_path_info(self, path, data_size, files_count):
|
2022-03-24 06:27:07 +00:00
|
|
|
# disable sorting temporarily
|
|
|
|
sorting = self.sourceFilesWidget.isSortingEnabled()
|
|
|
|
self.sourceFilesWidget.setSortingEnabled(False)
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
items = self.sourceFilesWidget.findItems(path, QtCore.Qt.MatchExactly)
|
|
|
|
# Conversion int->str->int needed because QT limits int to 32-bit
|
|
|
|
data_size = int(data_size)
|
|
|
|
files_count = int(files_count)
|
|
|
|
|
|
|
|
for item in items:
|
2021-11-19 13:26:59 +00:00
|
|
|
db_item = SourceFileModel.get(dir=path, profile=self.profile())
|
2020-10-30 08:30:52 +00:00
|
|
|
if QFileInfo(path).isDir():
|
|
|
|
self.sourceFilesWidget.item(item.row(), SourceColumn.FilesCount).setText(format(files_count))
|
|
|
|
db_item.path_isdir = True
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.item(
|
|
|
|
item.row(), SourceColumn.Path).setIcon(
|
|
|
|
get_colored_icon('folder'))
|
2020-10-30 08:30:52 +00:00
|
|
|
else:
|
|
|
|
# No files count, if entry itself is a file
|
|
|
|
self.sourceFilesWidget.item(item.row(), SourceColumn.FilesCount).setText("")
|
|
|
|
db_item.path_isdir = False
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.item(
|
|
|
|
item.row(), SourceColumn.Path).setIcon(
|
|
|
|
get_colored_icon('file'))
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
self.sourceFilesWidget.item(item.row(), SourceColumn.Size).setText(pretty_bytes(data_size))
|
|
|
|
|
|
|
|
db_item.dir_size = data_size
|
|
|
|
db_item.dir_files_count = files_count
|
|
|
|
db_item.save()
|
|
|
|
# Remove thread from list when it's done
|
|
|
|
for thrd in self.updateThreads:
|
|
|
|
if thrd.objectName() == path:
|
|
|
|
self.updateThreads.remove(thrd)
|
|
|
|
|
2022-03-24 06:27:07 +00:00
|
|
|
# enable sorting again
|
|
|
|
self.sourceFilesWidget.setSortingEnabled(sorting)
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
def update_path_info(self, index_row):
|
|
|
|
path = self.sourceFilesWidget.item(index_row, SourceColumn.Path).text()
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.item(index_row, SourceColumn.Size).setText(self.tr("Calculating…"))
|
|
|
|
self.sourceFilesWidget.item(index_row, SourceColumn.FilesCount).setText(self.tr("Calculating…"))
|
2021-10-18 11:41:37 +00:00
|
|
|
getDir = FilePathInfoAsync(path, self.profile().exclude_patterns)
|
2020-10-30 08:30:52 +00:00
|
|
|
getDir.signal.connect(self.set_path_info)
|
|
|
|
getDir.setObjectName(path)
|
|
|
|
self.updateThreads.append(getDir) # this is ugly, is there a better way to keep the thread object?
|
|
|
|
getDir.start()
|
|
|
|
|
2021-01-19 20:02:58 +00:00
|
|
|
def add_source_to_table(self, source, update_data=None):
|
2022-03-24 06:27:07 +00:00
|
|
|
# disable sorting temporarily
|
|
|
|
sorting = self.sourceFilesWidget.isSortingEnabled()
|
|
|
|
self.sourceFilesWidget.setSortingEnabled(False)
|
|
|
|
|
2021-01-19 20:02:58 +00:00
|
|
|
if update_data is None:
|
|
|
|
update_data = SettingsModel.get(key="get_srcpath_datasize").value
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
index_row = self.sourceFilesWidget.rowCount()
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.setRowCount(self.sourceFilesWidget.rowCount() + 1)
|
|
|
|
# Insert all items on current row, add tooltip containing the path name
|
2021-10-13 14:40:19 +00:00
|
|
|
new_item = QTableWidgetItem(source.dir)
|
|
|
|
new_item.setToolTip(source.dir)
|
|
|
|
self.sourceFilesWidget.setItem(index_row, SourceColumn.Path, new_item)
|
2020-10-30 08:30:52 +00:00
|
|
|
self.sourceFilesWidget.setItem(index_row, SourceColumn.Size, SizeItem(""))
|
|
|
|
self.sourceFilesWidget.setItem(index_row, SourceColumn.FilesCount, FilesCount(""))
|
|
|
|
|
|
|
|
if update_data:
|
|
|
|
self.update_path_info(index_row)
|
|
|
|
else: # Use cached data from DB
|
|
|
|
if source.dir_size > -1:
|
|
|
|
self.sourceFilesWidget.item(index_row, SourceColumn.Size).setText(pretty_bytes(source.dir_size))
|
|
|
|
|
|
|
|
if source.path_isdir:
|
|
|
|
self.sourceFilesWidget.item(index_row,
|
|
|
|
SourceColumn.FilesCount).setText(format(source.dir_files_count))
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.item(
|
|
|
|
index_row, SourceColumn.Path).setIcon(
|
|
|
|
get_colored_icon('folder'))
|
2020-10-30 08:30:52 +00:00
|
|
|
else:
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.item(
|
|
|
|
index_row, SourceColumn.Path).setIcon(
|
|
|
|
get_colored_icon('file'))
|
|
|
|
|
|
|
|
# enable sorting again
|
|
|
|
self.sourceFilesWidget.setSortingEnabled(sorting)
|
2020-10-30 08:30:52 +00:00
|
|
|
|
2018-11-17 08:51:53 +00:00
|
|
|
def populate_from_profile(self):
|
|
|
|
profile = self.profile()
|
|
|
|
self.excludePatternsField.textChanged.disconnect()
|
|
|
|
self.excludeIfPresentField.textChanged.disconnect()
|
2020-10-30 08:30:52 +00:00
|
|
|
self.sourceFilesWidget.setRowCount(0) # Clear rows
|
2018-11-17 08:51:53 +00:00
|
|
|
self.excludePatternsField.clear()
|
|
|
|
self.excludeIfPresentField.clear()
|
|
|
|
|
2018-12-05 09:05:47 +00:00
|
|
|
for source in SourceFileModel.select().where(SourceFileModel.profile == profile):
|
2020-10-30 08:30:52 +00:00
|
|
|
self.add_source_to_table(source, False)
|
2018-10-28 09:35:25 +00:00
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
# Initially, sort entries by path name in ascending order
|
2022-03-24 06:27:07 +00:00
|
|
|
self.sourceFilesWidget.sortItems(SourceColumn.Path, QtCore.Qt.AscendingOrder)
|
2018-11-17 08:51:53 +00:00
|
|
|
self.excludePatternsField.appendPlainText(profile.exclude_patterns)
|
|
|
|
self.excludeIfPresentField.appendPlainText(profile.exclude_if_present)
|
2018-10-28 09:35:25 +00:00
|
|
|
self.excludePatternsField.textChanged.connect(self.save_exclude_patterns)
|
|
|
|
self.excludeIfPresentField.textChanged.connect(self.save_exclude_if_present)
|
|
|
|
|
2020-10-30 08:30:52 +00:00
|
|
|
def sources_update(self):
|
|
|
|
for row in range(0, self.sourceFilesWidget.rowCount()):
|
|
|
|
self.update_path_info(row) # Update data for each entry
|
|
|
|
|
2018-11-24 04:12:12 +00:00
|
|
|
def source_add(self, want_folder):
|
2018-11-20 00:50:52 +00:00
|
|
|
def receive():
|
2020-08-30 06:04:40 +00:00
|
|
|
dirs = dialog.selectedFiles()
|
|
|
|
for dir in dirs:
|
2021-04-21 07:15:31 +00:00
|
|
|
if not os.access(dir, os.R_OK):
|
|
|
|
msg = QMessageBox()
|
|
|
|
msg.setText(self.tr(f"You don't have read access to {dir}."))
|
|
|
|
msg.exec()
|
|
|
|
return
|
|
|
|
|
2021-01-19 20:02:58 +00:00
|
|
|
new_source, created = SourceFileModel.get_or_create(dir=dir, profile=self.profile())
|
2018-11-20 00:50:52 +00:00
|
|
|
if created:
|
2021-01-19 20:02:58 +00:00
|
|
|
self.add_source_to_table(new_source)
|
2018-11-20 00:50:52 +00:00
|
|
|
new_source.save()
|
|
|
|
|
2020-08-30 06:04:40 +00:00
|
|
|
msg = self.tr("Choose directory to back up") if want_folder else self.tr("Choose file(s) to back up")
|
2019-01-20 03:50:10 +00:00
|
|
|
dialog = choose_file_dialog(self, msg, want_folder=want_folder)
|
2018-11-20 00:50:52 +00:00
|
|
|
dialog.open(receive)
|
|
|
|
|
2022-03-24 06:27:07 +00:00
|
|
|
def source_copy(self, index=None):
|
|
|
|
"""
|
|
|
|
Copy a source path to the clipboard.
|
|
|
|
|
|
|
|
Copies the first selected source if no index is specified.
|
|
|
|
"""
|
|
|
|
if index is None:
|
|
|
|
indexes = self.sourceFilesWidget.selectionModel().selectedRows()
|
|
|
|
|
|
|
|
if not indexes:
|
|
|
|
return
|
|
|
|
|
|
|
|
index = indexes[0]
|
|
|
|
|
|
|
|
path = PurePath(self.sourceFilesWidget.item(index.row(),
|
|
|
|
SourceColumn.Path).text())
|
|
|
|
|
|
|
|
data = QMimeData()
|
|
|
|
data.setUrls([QUrl(path.as_uri())])
|
|
|
|
data.setText(str(path))
|
|
|
|
|
|
|
|
QApplication.clipboard().setMimeData(data)
|
|
|
|
|
2018-10-27 17:24:34 +00:00
|
|
|
def source_remove(self):
|
2020-10-30 08:30:52 +00:00
|
|
|
indexes = self.sourceFilesWidget.selectionModel().selectedRows()
|
2021-11-16 05:25:46 +00:00
|
|
|
profile = self.profile()
|
2020-08-24 02:05:07 +00:00
|
|
|
# sort indexes, starting with lowest
|
|
|
|
indexes.sort()
|
2020-10-30 08:30:52 +00:00
|
|
|
# remove each selected row, starting with highest index (otherways, higher indexes become invalid)
|
2020-08-24 02:05:07 +00:00
|
|
|
for index in reversed(indexes):
|
2021-11-16 05:25:46 +00:00
|
|
|
db_item = SourceFileModel.get(
|
|
|
|
dir=self.sourceFilesWidget.item(index.row(), SourceColumn.Path).text(),
|
|
|
|
profile=profile
|
|
|
|
)
|
2019-04-16 01:51:25 +00:00
|
|
|
db_item.delete_instance()
|
2020-10-30 08:30:52 +00:00
|
|
|
self.sourceFilesWidget.removeRow(index.row())
|
2018-10-28 09:35:25 +00:00
|
|
|
|
|
|
|
def save_exclude_patterns(self):
|
2018-11-04 08:23:17 +00:00
|
|
|
profile = self.profile()
|
2018-10-31 16:09:01 +00:00
|
|
|
profile.exclude_patterns = self.excludePatternsField.toPlainText()
|
|
|
|
profile.save()
|
2018-10-28 09:35:25 +00:00
|
|
|
|
|
|
|
def save_exclude_if_present(self):
|
2018-11-04 08:23:17 +00:00
|
|
|
profile = self.profile()
|
2018-10-31 16:09:01 +00:00
|
|
|
profile.exclude_if_present = self.excludeIfPresentField.toPlainText()
|
|
|
|
profile.save()
|
2020-06-22 07:59:28 +00:00
|
|
|
|
|
|
|
def paste_text(self):
|
|
|
|
sources = QApplication.clipboard().text().splitlines()
|
|
|
|
invalidSources = ""
|
|
|
|
for source in sources:
|
|
|
|
if len(source) > 0: # Ignore empty newlines
|
2021-01-19 20:02:58 +00:00
|
|
|
if source.startswith('file://'): # Allow pasting multiple files/folders copied from file manager
|
|
|
|
source = source[7:]
|
2020-06-22 07:59:28 +00:00
|
|
|
if not os.path.exists(source):
|
|
|
|
invalidSources = invalidSources + "\n" + source
|
|
|
|
else:
|
|
|
|
new_source, created = SourceFileModel.get_or_create(dir=source, profile=self.profile())
|
|
|
|
if created:
|
2021-01-19 20:02:58 +00:00
|
|
|
self.add_source_to_table(new_source)
|
2020-06-22 07:59:28 +00:00
|
|
|
new_source.save()
|
|
|
|
|
|
|
|
if len(invalidSources) != 0: # Check if any invalid paths
|
|
|
|
msg = QMessageBox()
|
2022-03-28 07:22:40 +00:00
|
|
|
msg.setText(self.tr("Some of your sources are invalid:") + invalidSources)
|
2020-06-22 07:59:28 +00:00
|
|
|
msg.exec()
|