Class: Ruby_do_plugin_execute_application

Inherits:
Ruby_do::Plugin::Base
  • Object
show all
Defined in:
lib/ruby_do_plugin_execute_application.rb

Instance Method Summary collapse

Instance Method Details

#execute_static_result(args) ⇒ Object



157
158
159
160
# File 'lib/ruby_do_plugin_execute_application.rb', line 157

def execute_static_result(args)
  Knj::Os.subproc(args[:sres].data[:exec])
  return :close_win_main
end

#load_iconsObject



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_do_plugin_execute_application.rb', line 34

def load_icons
  print "Loading icons.\n" if @debug
  
  #Find icon-paths to scan for icons.
  @icon_paths = []
  
  self.scan_icon_dir("/usr/share/pixmaps")
  self.scan_icon_dir("/usr/share/icons")
  
  @icon_exts = ["png", "xpm", "svg"]
end

#on_optionsObject



151
152
153
154
155
# File 'lib/ruby_do_plugin_execute_application.rb', line 151

def on_options
  return {
    :widget => Gtk::Label.new("Test execute app")
  }
end

#scan_dir(path) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/ruby_do_plugin_execute_application.rb', line 79

def scan_dir(path)
  print "Path: #{path}\n" if @debug
  
  Dir.foreach(path) do |file|
    next if file[0, 1] == "."
    fp = "#{path}/#{file}".gsub("//", "/")
    
    if File.directory?(fp)
      self.scan_dir(fp)
    else
      if sres = self.static_result_get(fp) and fp.to_s.downcase.index("poedit") == nil
        @results_found << sres.id
        print "Skipping because exists: #{fp}\n" if @debug
        next
      end
      
      cont = File.read(fp)
      
      data = {}
      cont.scan(/^(.+?)=(.+)$/) do |match|
        data[match[0].to_s.downcase] = match[1]
      end
      
      if !data["name"] or !data["exec"]
        print "Skipping because no name or no exec: #{fp}\n" if @debug
        next
      end
      
      icon_paths = []
      icon_path = nil
      
      [data["icon"], data["name"]].each do |icon|
        next if icon.to_s.strip.empty?
        
        if icon
          icon_paths << icon if File.exists?(icon)
          
          self.load_icons if !@icon_paths
          @icon_paths.each do |path|
            icon_fp = "#{path}/#{icon}"
            icon_paths << icon_fp if File.exists?(icon_fp)
            
            @icon_exts.each do |ext|
              icon_fp = "#{path}/#{icon}.#{ext}"
              icon_paths << icon_fp if File.exists?(icon_fp)
            end
          end
        end
      end
      
      if !icon_paths.empty?
        icon_paths.sort!(&self.method(:sortmethod))
        icon_path = icon_paths.first
      end
      
      print "Registering: #{fp}\n" if @debug
      exec_data = data["exec"].gsub("%U", "").gsub("%F", "").strip
      res = self.register_static_result(
        :id_str => fp,
        :title => data["name"],
        :descr => sprintf(_("Open the application: '%1$s' with the command '%2$s'."), data["name"], exec_data),
        :icon_path => icon_path,
        :data => {
          :exec => exec_data
        }
      )
      
      @results_found << res[:sres].id
    end
  end
end

#scan_icon_dir(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_do_plugin_execute_application.rb', line 46

def scan_icon_dir(path)
  @icon_paths << path
  
  Dir.foreach(path) do |file|
    next if file[0, 1] == "."
    fp = "#{path}/#{file}"
    
    if File.directory?(fp)
      self.scan_icon_dir(fp)
    end
  end
end

#sortmethod(path1, path2) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ruby_do_plugin_execute_application.rb', line 59

def sortmethod(path1, path2)
  #puts "Paths (#{path1}, #{path2})"
  
  path1l = path1.downcase
  path2l = path2.downcase
  
  sizem1 = path1.match(/(\d+)x(\d+)/)
  sizem2 = path2.match(/(\d+)x(\d+)/)
  
  if path1l.index("highcontrast") != nil
    return 1
  elsif path2l.index("highcontrast") != nil
    return -1
  elsif sizem1 and sizem2
    return sizem2[1].to_i <=> sizem1[1].to_i
  else
    return path2 <=> path1
  end
end

#startObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_do_plugin_execute_application.rb', line 2

def start
  #Scan XDG-dirs for .desktop application files.
  dirs_scanned = []
  @results_found = []
  @debug = false
  
  #Do this in thread to avoid locking app.
  Thread.new do
    begin
      ENV["XDG_DATA_DIRS"].split(":").each do |val|
        val = val.to_s.gsub("//", "/").gsub(/\/$/, "")
        
        next if dirs_scanned.index(val) != nil
        dirs_scanned << val
        
        path = "#{val}/applications"
        self.scan_dir(path) if File.exists?(path)
      end
      
      #Delete static results which were not found after scanning.
      self.rdo_plugin_args[:rdo].ob.list(:Static_result, "plugin_id" => self.model.id, "id_not" => @results_found) do |sres|
        print "Deleting result because it not longer exists: '#{sres[:id_str]}'.\n" if @debug
        self.rdo_plugin_args[:rdo].ob.delete(sres)
      end
    rescue => e
      $stderr.puts "Error when updating 'execute_application'-plugin."
      $stderr.puts e.inspect
      $stderr.puts e.backtrace
    end
  end
end