Class: Sh::Global

Inherits:
Object show all
Defined in:
lib/sh_global.rb

Constant Summary collapse

SUPPORTED_EXTENSIONS =
['.mp3', '.m4a', '.ogg', '.flac', '.wma', '.wav']
GLADE =
{}
PATHS =
{}

Class Method Summary collapse

Class Method Details

.initObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sh_global.rb', line 19

def self.init
	# Setup some commonly used directories
  PATHS[:home_dir] = home_dir = GLib.home_dir
  PATHS[:config_dir] = config_dir = "#{home_dir}/.shroom"
  FileUtils.mkdir config_dir unless File.exists? config_dir
  PATHS[:cover_dir] = cover_dir = "#{config_dir}/covers"
  FileUtils.mkdir cover_dir unless File.exists? cover_dir
  # Store paths to relevant Shroom files
  PATHS[:prefs_file] = "#{config_dir}/preferences.yml"
  PATHS[:plugin_prefs_file] = "#{config_dir}/plugin_prefs.yml"
  PATHS[:db_file] = "#{config_dir}/songs.db"
  PATHS[:log_file] = log_file = "#{config_dir}/shroom.log"
  PATHS[:playlists_file] = "#{config_dir}/playlists.yml"

			# Set default preferences
  @@prefs = {
    :library_dir => "#{home_dir}/Music",
    :cover_art => true,
    :lyrics => true
  }
  
  # Load saved preferences
  load_prefs
  
  # Set log outputs
  Sh::Log.add_output_stream :stdout
  Sh::Log.add_output_stream open(log_file, "w")

			# Parse Glade interface
  add_toplevel_glade('dlg_preferences')
  add_toplevel_glade('dlg_song')
  add_toplevel_glade('dlg_plugins')
  GLADE.freeze

			# Initialize plugins
  Plugin.init
  load_plugins
end

.load_pluginsObject

Search for Shroom plugins and load them



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sh_global.rb', line 84

def self.load_plugins
  plugin_dirs = ["#{self.locate("plugins")}", "#{PATHS[:config_dir]}/.plugins"]
  plugin_dirs.each do |dir|
    Dir["#{dir}/*.rb"].each do |rb_file|
      begin
        require rb_file
      rescue Exception
      	Sh::Log.warning "Error loading plugin \"#{rb_file}\"", $!
      end
    end
  end
end

.load_prefsObject

Set current preferences to those in the preferences file



67
68
69
70
71
72
73
74
# File 'lib/sh_global.rb', line 67

def self.load_prefs
  if File.exists?(PATHS[:prefs_file])
    prefs = YAML::load(File.read(PATHS[:prefs_file]))
    prefs.each do |k, v|
      @@prefs[k] = v
    end
  end
end

.locate(file) ⇒ Object



58
59
60
# File 'lib/sh_global.rb', line 58

def self.locate(file)
  find_in_load_path("shroom-res/#{file}") || "shroom-res/#{file}"
end

.prefsObject



62
63
64
# File 'lib/sh_global.rb', line 62

def self.prefs
  return @@prefs
end

.save_prefsObject

Store current preferences in the preferences file



77
78
79
80
81
# File 'lib/sh_global.rb', line 77

def self.save_prefs
  File.open(PATHS[:prefs_file], 'w') do |f|
    f.write @@prefs.to_yaml
  end
end