Class: Ruby_do_plugin_filesearch

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Ruby_do_plugin_filesearch

Returns a new instance of Ruby_do_plugin_filesearch.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_do_plugin_filesearch.rb', line 9

def initialize(*args, &block)
  super(*args, &block)
  
  
  #Update database to contain tables for the plugin.
  Knj::Db::Revision.new.init_db("db" => rdo_plugin_args[:rdo].db, "schema" => Ruby_do_plugin_filesearch::Database::SCHEMA)
  
  
  #Initialize models-framework.
  @ob = Knj::Objects.new(
    :datarow => true,
    :class_path => "#{File.dirname(__FILE__)}/../models",
    :class_pre => "",
    :db => rdo_plugin_args[:rdo].db,
    :module => Ruby_do_plugin_filesearch::Models
  )
end

Instance Attribute Details

#obObject (readonly)

Returns the value of attribute ob.



7
8
9
# File 'lib/ruby_do_plugin_filesearch.rb', line 7

def ob
  @ob
end

Class Method Details

.const_missing(name) ⇒ Object



2
3
4
5
# File 'lib/ruby_do_plugin_filesearch.rb', line 2

def self.const_missing(name)
  require "#{File.dirname(__FILE__)}/../include/#{name.to_s.downcase}.rb"
  return const_get(name)
end

Instance Method Details

#execute_result(args) ⇒ Object



55
56
57
58
# File 'lib/ruby_do_plugin_filesearch.rb', line 55

def execute_result(args)
  Knj::Os.subproc("xdg-open \"#{args[:res].args[:path]}\"")
  return :close_win_main
end

#on_optionsObject



27
28
29
30
31
32
33
# File 'lib/ruby_do_plugin_filesearch.rb', line 27

def on_options
  win_options = Ruby_do_plugin_filesearch::Gui::Win_options.new(:rdo => self.rdo_plugin_args[:rdo], :filesearch => self)
  
  return {
    :widget => win_options.box
  }
end

#on_search(args) ⇒ 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_filesearch.rb', line 35

def on_search(args)
  return Enumerator.new do |yielder|
    if !args[:words].empty?
      @ob.list(:Plugin_filesearch_folder) do |folder|
        files = self.scan_dir(folder[:path], :words => args[:words], :depth => 0, :max_depth => folder[:search_depth].to_i) do |file|
          title = sprintf(_("Open '%s'."), file[:name])
          
          yielder << Ruby_do::Plugin::Result.new(
            :plugin => self,
            :title => title,
            :title_html => "<b>#{Knj::Web.html(title)}</b>",
            :descr => sprintf(_("Open the filepath: '%s'."), file[:path]),
            :path => file[:path]
          )
        end
      end
    end
  end
end

#scan_dir(path, args, &block) ⇒ Object



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
# File 'lib/ruby_do_plugin_filesearch.rb', line 60

def scan_dir(path, args, &block)
  depth = args[:depth] + 1
  
  Dir.foreach(path) do |file|
    begin
      next if file[0, 1] == "."
      fp = "#{path}/#{file}"
      filel = file.downcase
      
      if File.directory?(fp)
        self.scan_dir(fp, args.merge(:depth => depth), &block) if depth < args[:max_depth]
      else
        all_found = true
        args[:words].each do |word|
          if filel.index(word) == nil
            all_found = false
            break
          end
        end
        
        block.call(:name => file, :path => fp) if all_found
      end
    rescue => e
      $stderr.puts "Error while searching item: '#{e.message}' for '#{fp}'."
    end
  end
end