1 # -*- coding: utf-8 -*-
3 # gomi.py - a gomi.dll compatible Saori module for ninix
4 # Copyright (C) 2012, 2013 by Shyouzou Sugitani <shy@users.sourceforge.jp>
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License (version 2) as
8 # published by the Free Software Foundation. It is distributed in the
9 # hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
10 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 # PURPOSE. See the GNU General Public License for more details.
18 from gi.repository import Gio
19 from gi.repository import GLib
21 from ninix.dll import SAORI
26 if 'XDG_DATA_HOME' in os.environ:
27 XDG_DATA_HOME = os.environ['XDG_DATA_HOME']
29 XDG_DATA_HOME = os.environ['HOME'] + '/.local/share'
30 self.HOME_TRASH = os.fsencode(os.path.join(XDG_DATA_HOME, 'Trash'))
34 self.parser = argparse.ArgumentParser()
35 self.parser.add_argument('-e', '--empty', action='store_true')
36 self.parser.add_argument('-n', '--number-of-items', action='store_true')
37 self.parser.add_argument('-V', '--version', action='store_true')
38 self.parser.add_argument('-a', '--asynchronous', action='store_true')
39 self.parser.add_argument('-f', '--force', action='store_true')
40 self.parser.add_argument('-q', '--quiet', action='store_true')
41 self.parser.add_argument('-s', '--silent', action='store_true')
42 self.parser.add_argument('-v', '--verbose', action='store_true')
43 self.parser.add_argument('-w', '--hwnd')
44 bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
46 self.notify = Gio.DBusProxy.new_sync(
47 bus, 0, None, 'org.gnome.Nautilus', '/org/gnome/Nautilus',
48 'org.gnome.Nautilus.FileOperations', None)
54 def get_volume_trash(self):
55 vm = Gio.VolumeMonitor.get()
56 for m in vm.get_mounts():
57 mp = m.get_default_location().get_path()
60 volume_trash = os.path.join(mp, '.Trash', str(os.getuid()))
61 if not os.path.exists(volume_trash):
62 volume_trash = os.path.join(mp, '.Trash-' + str(os.getuid()))
63 if not os.path.exists(volume_trash):
65 yield os.fsencode(volume_trash)
67 def get_dir_size(self, dir_name):
70 for (path, dirs, files) in os.walk(dir_name):
73 dir_size += os.path.getsize(os.path.join(path, file))
74 return (file_count, dir_size)
76 def empty_trash(self, path):
77 for info in os.listdir(os.path.join(path, b'info')):
78 trash = info[0:-len(b'.trashinfo')]
79 filepath = os.path.join(path, b'files', trash)
80 infopath = os.path.join(path, b'info', info)
81 if os.path.isfile(filepath) or os.path.islink(filepath):
84 elif os.path.isdir(filepath):
85 shutil.rmtree(filepath)
88 def execute(self, argument):
89 args = self.parser.parse_args(argument[0].split())
90 if self.notify is None:
91 return self.RESPONSE[400]
92 if args.number_of_items:
93 file_count, dir_size = self.get_dir_size(self.HOME_TRASH)
94 for volume_trash in self.get_volume_trash():
95 count, size = self.get_dir_size(volume_trash)
98 return b''.join((b'SAORI/1.0 200 OK\r\n',
100 str(file_count).encode('ascii'),
103 str(dir_size).encode('ascii'),
107 self.empty_trash(self.HOME_TRASH)
108 for volume_trash in self.get_volume_trash():
109 self.empty_trash(volume_trash)
111 result = self.notify.call_sync(
112 'EmptyTrash', GLib.Variant('()', ()),
113 Gio.DBusCallFlags.NONE, -1, None)
114 return b''.join((b'SAORI/1.0 200 OK\r\n',