2018-04-15 13:59:44 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
# TODO:
|
|
|
|
# - Test with a large amount of files.
|
|
|
|
# - Show a progression bar when the removal takes time.
|
|
|
|
# - Improve the MessageDialog list for failed items.
|
|
|
|
|
2018-07-18 23:20:51 +02:00
|
|
|
import os
|
|
|
|
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-18 22:53:56 +02:00
|
|
|
from gi.repository import Nautilus, GObject, Gtk, Gio
|
2018-06-13 18:50:10 +02:00
|
|
|
|
|
|
|
from libmat2 import parser_factory
|
|
|
|
|
|
|
|
class Mat2Wrapper():
|
|
|
|
def __init__(self, filepath):
|
2018-07-19 00:11:30 +02:00
|
|
|
self.__filepath = filepath
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
def remove_metadata(self):
|
|
|
|
parser, mtype = parser_factory.get_parser(self.__filepath)
|
|
|
|
if parser is None:
|
|
|
|
return False, mtype
|
|
|
|
return parser.remove_all(), mtype
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
class ColumnExtension(GObject.GObject, Nautilus.MenuProvider, Nautilus.LocationWidgetProvider):
|
|
|
|
def notify(self):
|
|
|
|
self.infobar_msg.set_text("Failed to clean some items")
|
|
|
|
self.infobar.show_all()
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
def get_widget(self, uri, window):
|
|
|
|
self.infobar = Gtk.InfoBar()
|
|
|
|
self.infobar.set_message_type(Gtk.MessageType.ERROR)
|
|
|
|
self.infobar.set_show_close_button(True)
|
|
|
|
self.infobar.connect("response", self.__cb_infobar_response)
|
|
|
|
|
|
|
|
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
|
|
|
self.infobar.get_content_area().pack_start(hbox, False, False, 0)
|
|
|
|
|
|
|
|
btn = Gtk.Button("Show")
|
|
|
|
btn.connect("clicked", self.__cb_show_failed)
|
|
|
|
self.infobar.get_content_area().pack_end(btn, False, False, 0)
|
|
|
|
|
|
|
|
self.infobar_msg = Gtk.Label()
|
|
|
|
hbox.pack_start(self.infobar_msg, False, False, 0)
|
|
|
|
|
|
|
|
return self.infobar
|
|
|
|
|
|
|
|
def __cb_infobar_response(self, infobar, response):
|
|
|
|
if response == Gtk.ResponseType.CLOSE:
|
|
|
|
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):
|
|
|
|
self.infobar.hide()
|
2018-06-13 18:50:10 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
window = Gtk.Window()
|
|
|
|
hb = Gtk.HeaderBar()
|
|
|
|
window.set_titlebar(hb)
|
|
|
|
hb.props.title = "Metadata removal failed"
|
|
|
|
|
|
|
|
exit_buton = Gtk.Button("Exit")
|
|
|
|
exit_buton.connect("clicked", lambda _: window.close())
|
|
|
|
hb.pack_end(exit_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
|
|
|
|
|
|
|
listbox = Gtk.ListBox()
|
|
|
|
listbox.set_selection_mode(Gtk.SelectionMode.NONE)
|
2018-07-19 00:11:30 +02:00
|
|
|
box.pack_start(listbox, True, True, 0)
|
2018-07-18 23:12:55 +02:00
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
for i, mtype in self.failed_items:
|
2018-07-18 22:53:56 +02:00
|
|
|
row = Gtk.ListBoxRow()
|
|
|
|
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
|
|
|
|
row.add(hbox)
|
2018-07-18 23:12:55 +02:00
|
|
|
|
2018-07-18 23:20:51 +02:00
|
|
|
icon = Gio.content_type_get_icon('text/plain' if not mtype else mtype)
|
2018-07-18 22:53:56 +02:00
|
|
|
select_image = Gtk.Image.new_from_gicon(icon, Gtk.IconSize.BUTTON)
|
|
|
|
hbox.pack_start(select_image, False, False, 0)
|
2018-07-18 23:12:55 +02:00
|
|
|
|
2018-07-18 22:53:56 +02:00
|
|
|
label = Gtk.Label(os.path.basename(i))
|
|
|
|
hbox.pack_start(label, True, False, 0)
|
2018-07-18 23:12:55 +02:00
|
|
|
|
2018-07-18 22:53:56 +02:00
|
|
|
listbox.add(row)
|
2018-06-13 18:50:10 +02:00
|
|
|
|
|
|
|
listbox.show_all()
|
2018-07-19 00:11:30 +02:00
|
|
|
window.show_all()
|
2018-06-13 18:50:10 +02:00
|
|
|
|
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
@staticmethod
|
|
|
|
def __validate(f):
|
|
|
|
if f.get_uri_scheme() != "file" or f.is_directory():
|
2018-06-13 18:50:10 +02:00
|
|
|
return False
|
2018-07-19 00:11:30 +02:00
|
|
|
elif not f.can_write():
|
2018-06-13 18:50:10 +02:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2018-07-19 00:11:30 +02:00
|
|
|
def __cb_menu_activate(self, menu, files):
|
|
|
|
self.failed_items = list()
|
|
|
|
for f in files:
|
|
|
|
if not self.__validate(f):
|
|
|
|
self.failed_items.append((f.get_name(), None))
|
|
|
|
continue
|
|
|
|
|
|
|
|
fname = unquote(f.get_uri()[7:])
|
|
|
|
ret, mtype = Mat2Wrapper(fname).remove_metadata()
|
|
|
|
if not ret:
|
|
|
|
self.failed_items.append((f.get_name(), mtype))
|
|
|
|
|
|
|
|
if len(self.failed_items):
|
|
|
|
self.notify()
|
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
|
|
|
|
|
|
|
|
def get_file_items(self, window, files):
|
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-18 23:15:47 +02:00
|
|
|
if not any(map(self.__validate, files)):
|
2018-04-15 13:59:44 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
item = Nautilus.MenuItem(
|
|
|
|
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
|
|
|
|
|
|
|
return [item]
|