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

Constructor Details

#initialize(*args, &block) ⇒ Ruby_do_plugin_execute_application

Returns a new instance of Ruby_do_plugin_execute_application.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ruby_do_plugin_execute_application.rb', line 2

def initialize(*args, &block)
  super(*args, &block)
  
  #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"]
  
  
  #Scan XDG-dirs for .desktop application files.
  @apps = []
  ENV["XDG_DATA_DIRS"].split(":").each do |val|
    path = "#{val}/applications"
    self.scan_dir(path) if File.exists?(path)
  end
end

Instance Method Details

#execute_result(args) ⇒ Object



137
138
139
140
# File 'lib/ruby_do_plugin_execute_application.rb', line 137

def execute_result(args)
  Knj::Os.subproc(args[:res].args[:exec])
  return :close_win_main
end

#on_optionsObject



106
107
108
109
110
# File 'lib/ruby_do_plugin_execute_application.rb', line 106

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

#on_search(args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ruby_do_plugin_execute_application.rb', line 112

def on_search(args)
  return Enumerator.new do |yielder|
    @apps.each do |app|
      found_all = true
      args[:words].each do |word|
        if app[:namel].index(word) == nil
          found_all = false
          break
        end
      end
      
      if found_all
        yielder << Ruby_do::Plugin::Result.new(
          :plugin => self,
          :title => app[:name],
          :title_html => "<b>#{Knj::Web.html(app[:name])}</b>",
          :descr => sprintf(_("Open the application: '%1$s' with the command '%2$s'."), app[:name], app[:exec]),
          :exec => app[:exec],
          :icon => app[:icon_path]
        )
      end
    end
  end
end

#scan_dir(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
# File 'lib/ruby_do_plugin_execute_application.rb', line 55

def scan_dir(path)
  Dir.foreach(path) do |file|
    next if file[0, 1] == "."
    fp = "#{path}/#{file}"
    
    if File.directory?(fp)
      self.scan_dir(fp)
    else
      cont = File.read(fp)
      
      data = {}
      cont.scan(/^(.+?)=(.+)$/) do |match|
        data[match[0].to_s.downcase] = match[1]
      end
      
      icon_paths = []
      icon_path = nil
      
      if icon = data["icon"]
        if File.exists?(icon)
          icon_path = icon
        else
          @icon_paths.each do |path|
            @icon_exts.each do |ext|
              fp = "#{path}/#{icon}.#{ext}"
              if File.exists?(fp)
                icon_paths << fp
              end
            end
          end
        end
      end
      
      if !icon_paths.empty?
        icon_paths.sort!(&self.method(:sortmethod))
        icon_path = icon_paths.first
      end
      
      if data["name"] and data["exec"]
        @apps << {
          :name => data["name"],
          :namel => data["name"].to_s.downcase,
          :exec => data["exec"],
          :icon => icon,
          :icon_path => icon_path
        }
      end
    end
  end
end

#scan_icon_dir(path) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ruby_do_plugin_execute_application.rb', line 22

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ruby_do_plugin_execute_application.rb', line 35

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