Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete file in TagStudio #282

Merged
merged 7 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions tagstudio/src/qt/helpers/file_deleter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import logging
import pathlib
import subprocess
import sys
import traceback
from pathlib import Path
from typing import Callable

ERROR = f"[ERROR]"
WARNING = f"[WARNING]"
INFO = f"[INFO]"

logging.basicConfig(format="%(message)s", level=logging.INFO)


def delete_file(path: str | Path, callback: Callable):
_path = str(path)
_file = Path(_path)
logging.info(f"Deleting file: {_path}")
if not _file.exists():
logging.error(f"File not found: {_path}")
return
try:
_file.unlink()
callback()
except:
traceback.print_exc()


class FileDeleterHelper:
def __init__(self, filepath: str | Path):
self.filepath = str(filepath)

def set_filepath(self, filepath: str | Path):
self.filepath = str(filepath)

def set_delete_callback(self, callback: Callable):
self.delete_callback = callback

def delete_file(self):
delete_file(self.filepath, self.delete_callback)
13 changes: 13 additions & 0 deletions tagstudio/src/qt/widgets/item_thumb.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from src.core.constants import AUDIO_TYPES, VIDEO_TYPES, IMAGE_TYPES
from src.qt.flowlayout import FlowWidget
from src.qt.helpers.file_opener import FileOpenerHelper
from src.qt.helpers.file_deleter import FileDeleterHelper
from src.qt.widgets.thumb_renderer import ThumbRenderer
from src.qt.widgets.thumb_button import ThumbButton

Expand Down Expand Up @@ -191,12 +192,16 @@ def __init__(

self.thumb_button.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)
self.opener = FileOpenerHelper("")
self.deleter = FileDeleterHelper("")
open_file_action = QAction("Open file", self)
open_file_action.triggered.connect(self.opener.open_file)
open_explorer_action = QAction("Open file in explorer", self)
open_explorer_action.triggered.connect(self.opener.open_explorer)
delete_action = QAction("Delete", self)
delete_action.triggered.connect(self.deleter.delete_file)
self.thumb_button.addAction(open_file_action)
self.thumb_button.addAction(open_explorer_action)
self.thumb_button.addAction(delete_action)

# Static Badges ========================================================

Expand Down Expand Up @@ -414,6 +419,8 @@ def set_item_id(self, id: int):
entry = self.lib.get_entry(self.item_id)
filepath = self.lib.library_dir / entry.path / entry.filename
self.opener.set_filepath(filepath)
self.deleter.set_filepath(filepath)
self.deleter.set_delete_callback(self._on_delete)

def assign_favorite(self, value: bool):
# Switching mode to None to bypass mode-specific operations when the
Expand Down Expand Up @@ -491,3 +498,9 @@ def toggle_tag(entry: Entry):
if self.panel.isOpen:
self.panel.update_widgets()
self.panel.driver.update_badges()

def _on_delete(self):
entry = self.lib.get_entry(self.item_id)
self.lib.remove_entry(self.item_id)
self.panel.driver.purge_item_from_navigation(entry.type, self.item_id)
self.panel.driver.filter_items()