Backported de09752972bd6e1c8a68d8c1d06c3311721dc1bd.
[kaya.git] / lib / plugins / engines / loader.rb
blob16187520c41a497a02f704a7a79cba1040d55c1d
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'qtutils'
9 require 'plugins/plugin'
10 require 'factory'
13 class EngineLoader
14   include Plugin
15   include Enumerable
16   
17   class Entry < Factory
18     attr_reader :name
19     attr_reader :game
20     attr_reader :protocol
21     attr_reader :workdir
22     attr_reader :path
23   
24     def initialize(data)
25       loader = PluginLoader.new
26       plugin = loader.get_all_matching(:engine).find do |e|
27         e.data(:protocol) == data[:protocol]
28       end
29       fact = lambda do |color, match|
30         plugin.new(@path, @name, color, match, 
31                    :workdir => @workdir)
32       end
33       super(&fact)
34       
35       @name = data[:name]
36       @game = Game.get(data[:game])
37       @protocol = data[:protocol]
38       @workdir = data[:workdir]
39       @path = data[:path]
40     end
41     
42     def self.load(name, group)
43       new :name => name,
44           :game => group.read_entry('game').to_sym,
45           :path => group.read_entry('path'),
46           :protocol => group.read_entry('protocol'),
47           :workdir => group.read_entry('workdir')
48     end
49     
50     def save(group)
51       group.write_entry('game', @game.class.data(:id).to_s)
52       group.write_entry('path', @path)
53       group.write_entry('protocol', @protocol)
54       group.write_entry('workdir', @workdir)
55     end
56   end
57   
58   plugin :name => 'Default Engine Loader',
59          :interface => :engine_loader
61   def reload
62     @entries = { }
63     config = KDE::Global.config.group("Engines")
64     engine_groups = config.group_list
65     engine_groups.each do |engine_group|
66       entry = Entry.load(engine_group, config.group(engine_group))
67       @entries[entry.name] = entry
68     end
69   end
70   
71   def update_entries(entries)
72     @entries = entries.dup
73     
74     config = KDE::Global.config.group("Engines")
75     config.delete_group
76     @entries.each do |name, engine|
77       group = config.group(name)
78       engine.save(group)
79     end
80     config.sync
81   end
82   
83   def [](name)
84     @entries[name]
85   end
87   def find_by_game(game)
88     @entries.select do |name, e|
89       e.game == game
90     end
91   end
92   
93   def each(&blk)
94     @entries.each(&blk)
95   end
96   
97   def size
98     @entries.size
99   end
100   
101   def engine
102     Entry
103   end