Debian Codegraph @

lib/ninix/dll/gomi.rb

raw
1 # -*- coding: utf-8 -*-
2 #
3 # gomi.rb - a gomi.dll compatible Saori module for ninix
4 # Copyright (C) 2012-2019 by Shyouzou Sugitani <[email protected]>
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 require "optparse"
15
16 require "gio2"
17
18 require_relative "../dll"
19
20
21 module Gomi
22
23 class Saori < DLL::SAORI
24
25 def initialize
26 if ENV.include?('XDG_DATA_HOME')
27 @xdg_data_home = ENV['XDG_DATA_HOME']
28 else
29 @xdg_data_home = File.join(ENV['HOME'], '.local' , 'share')
30 end
31 @home_trash = File.join(@xdg_data_home, 'Trash')
32 super()
33 end
34
35 def setup
36 @parser = OptionParser.new
37 begin
38 @notify = Gio::DBusProxy.new(
39 Gio::BusType::SESSION, 0, nil,
40 'org.gnome.Nautilus', '/org/gnome/Nautilus',
41 'org.gnome.Nautilus.FileOperations', nil)
42 return 1
43 rescue
44 @notify = nil
45 return 0
46 end
47 end
48
49 def get_volume_trash
50 vm = Gio::VolumeMonitor.get()
51 result = []
52 for m in vm.mounts
53 mp = m.default_location.path
54 next if mp.nil?
55 volume_trash = File.join(mp, '.Trash', Process::UID.eid.to_s)
56 unless File.exist?(volume_trash)
57 volume_trash = File.join(mp, '.Trash-' + Process::UID.eid.to_s)
58 end
59 next unless File.exist?(volume_trash)
60 result << volume_trash
61 end
62 return result
63 end
64
65 def get_dir_size(dir_name)
66 file_count = 0
67 dir_size = 0
68 for file in Dir.glob(File.join(dir_name, '**', '*'))
69 file_count += 1
70 if File.file?(file)
71 dir_size += File.size(file)
72 end
73 end
74 return [file_count, dir_size]
75 end
76
77 def empty_trash(path)
78 Dir.foreach(File.join(path, 'info')) { |info|
79 next if info == '.' or info == '..'
80 trash = info[0..-'.trashinfo'.length-1]
81 filepath = File.join(path, 'files', trash)
82 infopath = File.join(path, 'info', info)
83 if File.file?(filepath) or File.symlink?(filepath)
84 File.delete(filepath)
85 File.delete(infopath)
86 elsif File.directory?(filepath)
87 FileUtils.rm_rf(filepath)
88 File.delete(infopath)
89 end
90 }
91 end
92
93 def execute(argument)
94 return RESPONSE[400] if argument.nil? or argument.empty?
95 args = @parser.getopts(
96 argument[0].split(nil, 0),
97 'enVafqsvw:',
98 'empty', 'number-of-items', 'version', 'asynchronous', 'force',
99 'quiet', 'silent', 'verbose', 'hwnd:')
100 return RESPONSE[400] if @notify.nil?
101 if args['number-of-items'] or args['n']
102 file_count, dir_size = get_dir_size(@home_trash)
103 for volume_trash in get_volume_trash()
104 count, size = get_dir_size(volume_trash)
105 file_count += count
106 dir_size += size
107 end
108 return ["SAORI/1.0 200 OK\r\n",
109 "Result: ",
110 file_count.to_s.encode('ascii', :invalid => :replace, :undef => :replace),
111 "\r\n",
112 "Reference0: ",
113 dir_size.to_s.encode('ascii', :invalid => :replace, :undef => :replace),
114 "\r\n\r\n"].join("")
115 elsif args['empty'] or args['e']
116 if args['force'] or args['f']
117 empty_trash(@home_trash)
118 for volume_trash in get_volume_trash()
119 empty_trash(volume_trash)
120 end
121 else
122 result = @notify.call_sync(
123 'EmptyTrash', nil, ## GLib.Variant('()', ()),
124 Gio::DBusCallFlags::NONE, -1, nil)
125 end
126 return ["SAORI/1.0 200 OK\r\n",
127 "Result: ",
128 "1", # FIXME
129 "\r\n\r\n"].join("")
130 end
131 end
132 end
133 end