OSDN Git Service

5debe9cd04913bb2d49e52f50f8aacc8076145c0
[ninix-aya/master.git] / lib / ninix / dll / gomi.py
1 # -*- coding: utf-8 -*-
2 #
3 #  gomi.py - a gomi.dll compatible Saori module for ninix
4 #  Copyright (C) 2012, 2013 by Shyouzou Sugitani <shy@users.sourceforge.jp>
5 #
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.
12 #
13
14 import os
15 import argparse
16 import shutil
17
18 from gi.repository import Gio
19 from gi.repository import GLib
20
21 from ninix.dll import SAORI
22
23 class Saori(SAORI):
24
25     def __init__(self):
26         if 'XDG_DATA_HOME' in os.environ:
27             XDG_DATA_HOME = os.environ['XDG_DATA_HOME']
28         else:
29             XDG_DATA_HOME = os.environ['HOME'] + '/.local/share'
30         self.HOME_TRASH = os.fsencode(os.path.join(XDG_DATA_HOME, 'Trash'))
31         SAORI.__init__(self)
32
33     def setup(self):
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)
45         try:
46             self.notify = Gio.DBusProxy.new_sync(
47                 bus, 0, None, 'org.gnome.Nautilus', '/org/gnome/Nautilus',
48                 'org.gnome.Nautilus.FileOperations', None)
49             return 1
50         except:
51             self.notify = None
52             return 0
53
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()
58             if mp is None:
59                 continue
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):
64                 continue
65             yield os.fsencode(volume_trash)
66
67     def get_dir_size(self, dir_name):
68         file_count = 0
69         dir_size = 0
70         for (path, dirs, files) in os.walk(dir_name):
71             for file in files:
72                 file_count += 1
73                 dir_size += os.path.getsize(os.path.join(path, file))
74         return (file_count, dir_size)
75
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):
82                 os.remove(filepath)
83                 os.remove(infopath)
84             elif os.path.isdir(filepath):
85                 shutil.rmtree(filepath)
86                 os.remove(infopath)
87
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)
96                 file_count += count
97                 dir_size += size
98             return b''.join((b'SAORI/1.0 200 OK\r\n',
99                              b'Result: ',
100                              str(file_count).encode('ascii'),
101                              b'\r\n'
102                              b'Reference0: ',
103                              str(dir_size).encode('ascii'),
104                              b'\r\n\r\n'))
105         elif args.empty:
106             if args.force:
107                 self.empty_trash(self.HOME_TRASH)
108                 for volume_trash in self.get_volume_trash():
109                     self.empty_trash(volume_trash)
110             else:
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',
115                                  b'Result: ',
116                                  b'1', # FIXME
117                                  b'\r\n\r\n'))