2018-04-15 13:59:44 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
"""
|
|
|
|
Because writing GUI is non-trivial (cf. https://0xacab.org/jvoisin/mat2/issues/3),
|
|
|
|
we decided to write a Nautilus extensions instead
|
|
|
|
(cf. https://0xacab.org/jvoisin/mat2/issues/2).
|
|
|
|
|
|
|
|
The code is a little bit convoluted because Gtk isn't thread-safe,
|
|
|
|
so we're not allowed to call anything Gtk-related outside of the main
|
|
|
|
thread, so we'll have to resort to using a `queue` to pass "messages" around.
|
|
|
|
"""
|
2018-07-19 00:11:30 +02:00
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
# pylint: disable=no-name-in-module,unused-argument,no-self-use,import-error
|
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
import queue
|
|
|
|
import threading
|
2018-11-08 21:40:33 +01:00
|
|
|
from typing import Tuple, Optional, List
|
2018-07-18 23:20:51 +02:00
|
|
|
from urllib.parse import unquote
|
|
|
|
|
2018-04-15 13:59:44 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version('Nautilus', '3.0')
|
2018-06-13 18:50:10 +02:00
|
|
|
gi.require_version('Gtk', '3.0')
|
2018-07-25 01:44:02 +02:00
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
|
|
|
from gi.repository import Nautilus, GObject, Gtk, Gio, GLib, GdkPixbuf
|
2018-06-13 18:50:10 +02:00
|
|
|
|
|
|
|
from libmat2 import parser_factory
|
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
|
2018-11-08 21:40:33 +01:00
|
|
|
def _remove_metadata(fpath) -> Tuple[bool, Optional[str]]:
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This is a simple wrapper around libmat2, because it's
|
|
|
|
easier and cleaner this way.
|
|
|
|
"""
|
|
|
|
parser, mtype = parser_factory.get_parser(fpath)
|
|
|
|
if parser is None:
|
|
|
|
return False, mtype
|
|
|
|
return parser.remove_all(), mtype
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2019-01-16 23:10:17 +01:00
|
|
|
class Mat2Extension(GObject.GObject, Nautilus.MenuProvider, Nautilus.LocationWidgetProvider):
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This class adds an item to the right-clic menu in Nautilus. """
|
2018-07-25 01:44:02 +02:00
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.infobar_hbox = None
|
|
|
|
self.infobar = None
|
|
|
|
self.failed_items = list()
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
def __infobar_failure(self):
|
|
|
|
""" Add an hbox to the `infobar` warning about the fact that we didn't
|
|
|
|
manage to remove the metadata from every single file.
|
|
|
|
"""
|
2018-07-19 00:11:30 +02:00
|
|
|
self.infobar.set_show_close_button(True)
|
2018-07-23 23:39:06 +02:00
|
|
|
self.infobar_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
2018-07-19 00:11:30 +02:00
|
|
|
|
|
|
|
btn = Gtk.Button("Show")
|
|
|
|
btn.connect("clicked", self.__cb_show_failed)
|
2018-07-23 23:39:06 +02:00
|
|
|
self.infobar_hbox.pack_end(btn, False, False, 0)
|
2018-07-19 00:11:30 +02:00
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
infobar_msg = Gtk.Label("Failed to clean some items")
|
|
|
|
self.infobar_hbox.pack_start(infobar_msg, False, False, 0)
|
|
|
|
|
|
|
|
self.infobar.get_content_area().pack_start(self.infobar_hbox, True, True, 0)
|
|
|
|
self.infobar.show_all()
|
|
|
|
|
2018-11-08 21:40:33 +01:00
|
|
|
def get_widget(self, uri, window) -> Gtk.Widget:
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This is the method that we have to implement (because we're
|
|
|
|
a LocationWidgetProvider) in order to show our infobar.
|
|
|
|
"""
|
|
|
|
self.infobar = Gtk.InfoBar()
|
|
|
|
self.infobar.set_message_type(Gtk.MessageType.ERROR)
|
|
|
|
self.infobar.connect("response", self.__cb_infobar_response)
|
2018-07-19 00:11:30 +02:00
|
|
|
|
|
|
|
return self.infobar
|
|
|
|
|
|
|
|
def __cb_infobar_response(self, infobar, response):
|
2018-07-23 23:39:06 +02:00
|
|
|
""" Callback for the infobar close button.
|
|
|
|
"""
|
2018-07-19 00:11:30 +02:00
|
|
|
if response == Gtk.ResponseType.CLOSE:
|
2018-07-23 23:39:06 +02:00
|
|
|
self.infobar_hbox.destroy()
|
2018-07-19 00:11:30 +02:00
|
|
|
self.infobar.hide()
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
def __cb_show_failed(self, button):
|
2018-07-23 23:39:06 +02:00
|
|
|
""" Callback to show a popup containing a list of files
|
|
|
|
that we didn't manage to clean.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# FIXME this should be done only once the window is destroyed
|
|
|
|
self.infobar_hbox.destroy()
|
2018-07-19 00:11:30 +02:00
|
|
|
self.infobar.hide()
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
window = Gtk.Window()
|
2018-07-23 23:39:06 +02:00
|
|
|
headerbar = Gtk.HeaderBar()
|
|
|
|
window.set_titlebar(headerbar)
|
|
|
|
headerbar.props.title = "Metadata removal failed"
|
2018-07-19 00:11:30 +02:00
|
|
|
|
2018-07-26 08:57:09 +02:00
|
|
|
close_buton = Gtk.Button("Close")
|
|
|
|
close_buton.connect("clicked", lambda _: window.close())
|
|
|
|
headerbar.pack_end(close_buton)
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
|
|
|
|
window.add(box)
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
box.add(self.__create_treeview())
|
|
|
|
window.show_all()
|
2018-07-18 23:12:55 +02:00
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
@staticmethod
|
|
|
|
def __validate(fileinfo) -> Tuple[bool, str]:
|
|
|
|
""" Validate if a given file FileInfo `fileinfo` can be processed.
|
|
|
|
Returns a boolean, and a textreason why"""
|
|
|
|
if fileinfo.get_uri_scheme() != "file" or fileinfo.is_directory():
|
|
|
|
return False, "Not a file"
|
|
|
|
elif not fileinfo.can_write():
|
|
|
|
return False, "Not writeable"
|
|
|
|
return True, ""
|
2018-07-18 23:12:55 +02:00
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
def __create_treeview(self) -> Gtk.TreeView:
|
|
|
|
liststore = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str)
|
|
|
|
treeview = Gtk.TreeView(model=liststore)
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
renderer_pixbuf = Gtk.CellRendererPixbuf()
|
2018-07-26 09:01:27 +02:00
|
|
|
column_pixbuf = Gtk.TreeViewColumn("Icon", renderer_pixbuf, pixbuf=0)
|
2018-07-25 01:44:02 +02:00
|
|
|
treeview.append_column(column_pixbuf)
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-26 09:01:01 +02:00
|
|
|
for idx, name in enumerate(['File', 'Reason']):
|
2018-07-25 01:44:02 +02:00
|
|
|
renderer_text = Gtk.CellRendererText()
|
|
|
|
column_text = Gtk.TreeViewColumn(name, renderer_text, text=idx+1)
|
|
|
|
treeview.append_column(column_text)
|
|
|
|
|
|
|
|
for (fname, mtype, reason) in self.failed_items:
|
|
|
|
# This part is all about adding mimetype icons to the liststore
|
|
|
|
icon = Gio.content_type_get_icon('text/plain' if not mtype else mtype)
|
|
|
|
# in case we don't have the corresponding icon,
|
|
|
|
# we're adding `text/plain`, because we have this one for sure™
|
|
|
|
names = icon.get_names() + ['text/plain', ]
|
|
|
|
icon_theme = Gtk.IconTheme.get_default()
|
|
|
|
for name in names:
|
|
|
|
try:
|
|
|
|
img = icon_theme.load_icon(name, Gtk.IconSize.BUTTON, 0)
|
|
|
|
break
|
|
|
|
except GLib.GError:
|
|
|
|
pass
|
|
|
|
|
2018-07-26 09:01:01 +02:00
|
|
|
liststore.append([img, fname, reason])
|
2018-07-25 01:44:02 +02:00
|
|
|
|
|
|
|
treeview.show_all()
|
|
|
|
return treeview
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
def __create_progressbar(self) -> Gtk.ProgressBar:
|
2018-07-23 23:39:06 +02:00
|
|
|
""" Create the progressbar used to notify that files are currently
|
|
|
|
being processed.
|
|
|
|
"""
|
|
|
|
self.infobar.set_show_close_button(False)
|
|
|
|
self.infobar.set_message_type(Gtk.MessageType.INFO)
|
|
|
|
self.infobar_hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
|
|
|
|
|
|
|
progressbar = Gtk.ProgressBar()
|
|
|
|
self.infobar_hbox.pack_start(progressbar, True, True, 0)
|
|
|
|
progressbar.set_show_text(True)
|
|
|
|
|
|
|
|
self.infobar.get_content_area().pack_start(self.infobar_hbox, True, True, 0)
|
|
|
|
self.infobar.show_all()
|
|
|
|
|
|
|
|
return progressbar
|
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
def __update_progressbar(self, processing_queue, progressbar) -> bool:
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This method is run via `Glib.add_idle` to update the progressbar."""
|
|
|
|
try:
|
|
|
|
fname = processing_queue.get(block=False)
|
|
|
|
except queue.Empty:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# `None` is the marker put in the queue to signal that every selected
|
|
|
|
# file was processed.
|
|
|
|
if fname is None:
|
|
|
|
self.infobar_hbox.destroy()
|
|
|
|
self.infobar.hide()
|
2019-02-28 16:47:16 +01:00
|
|
|
if self.failed_items:
|
2018-07-23 23:39:06 +02:00
|
|
|
self.__infobar_failure()
|
|
|
|
if not processing_queue.empty():
|
|
|
|
print("Something went wrong, the queue isn't empty :/")
|
|
|
|
return False
|
|
|
|
|
|
|
|
progressbar.pulse()
|
|
|
|
progressbar.set_text("Cleaning %s" % fname)
|
|
|
|
progressbar.show_all()
|
|
|
|
self.infobar_hbox.show_all()
|
|
|
|
self.infobar.show_all()
|
|
|
|
return True
|
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
def __clean_files(self, files: list, processing_queue: queue.Queue) -> bool:
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This method is threaded in order to avoid blocking the GUI
|
|
|
|
while cleaning up the files.
|
|
|
|
"""
|
|
|
|
for fileinfo in files:
|
|
|
|
fname = fileinfo.get_name()
|
|
|
|
processing_queue.put(fname)
|
|
|
|
|
2018-07-25 01:44:02 +02:00
|
|
|
valid, reason = self.__validate(fileinfo)
|
|
|
|
if not valid:
|
|
|
|
self.failed_items.append((fname, None, reason))
|
2018-07-23 23:39:06 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
fpath = unquote(fileinfo.get_uri()[7:]) # `len('file://') = 7`
|
|
|
|
success, mtype = _remove_metadata(fpath)
|
|
|
|
if not success:
|
2018-07-25 01:44:02 +02:00
|
|
|
self.failed_items.append((fname, mtype, 'Unsupported/invalid'))
|
2018-07-23 23:39:06 +02:00
|
|
|
processing_queue.put(None) # signal that we processed all the files
|
|
|
|
return True
|
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
def __cb_menu_activate(self, menu, files):
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This method is called when the user clicked the "clean metadata"
|
|
|
|
menu item.
|
|
|
|
"""
|
2018-07-19 00:11:30 +02:00
|
|
|
self.failed_items = list()
|
2018-07-23 23:39:06 +02:00
|
|
|
progressbar = self.__create_progressbar()
|
|
|
|
progressbar.set_pulse_step = 1.0 / len(files)
|
|
|
|
self.infobar.show_all()
|
|
|
|
|
|
|
|
processing_queue = queue.Queue()
|
|
|
|
GLib.idle_add(self.__update_progressbar, processing_queue, progressbar)
|
2018-07-19 00:11:30 +02:00
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
thread = threading.Thread(target=self.__clean_files, args=(files, processing_queue))
|
|
|
|
thread.daemon = True
|
|
|
|
thread.start()
|
2018-07-19 00:11:30 +02:00
|
|
|
|
2018-04-15 13:59:44 +02:00
|
|
|
def get_background_items(self, window, file):
|
|
|
|
""" https://bugzilla.gnome.org/show_bug.cgi?id=784278 """
|
|
|
|
return None
|
|
|
|
|
2018-11-08 21:40:33 +01:00
|
|
|
def get_file_items(self, window, files) -> Optional[List[Nautilus.MenuItem]]:
|
2018-07-23 23:39:06 +02:00
|
|
|
""" This method is the one allowing us to create a menu item.
|
|
|
|
"""
|
2018-06-13 18:50:10 +02:00
|
|
|
# Do not show the menu item if not a single file has a chance to be
|
|
|
|
# processed by mat2.
|
2018-07-25 01:44:02 +02:00
|
|
|
if not any([is_valid for (is_valid, _) in map(self.__validate, files)]):
|
2018-07-23 23:39:06 +02:00
|
|
|
return None
|
2018-04-15 13:59:44 +02:00
|
|
|
|
|
|
|
item = Nautilus.MenuItem(
|
2019-11-28 03:15:20 +01:00
|
|
|
name="mat2::Remove_metadata",
|
2018-06-13 18:50:10 +02:00
|
|
|
label="Remove metadata",
|
|
|
|
tip="Remove metadata"
|
2018-04-15 13:59:44 +02:00
|
|
|
)
|
2018-07-19 00:11:30 +02:00
|
|
|
item.connect('activate', self.__cb_menu_activate, files)
|
2018-04-15 13:59:44 +02:00
|
|
|
|
2018-07-23 23:39:06 +02:00
|
|
|
return [item, ]
|