OSDN Git Service

Move database.yml to template
[redminele/redminele.git] / redmine / lib / redmine / themes.rb
1 # redMine - project management software
2 # Copyright (C) 2006-2007  Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 module Redmine
19   module Themes
20   
21     # Return an array of installed themes
22     def self.themes
23       @@installed_themes ||= scan_themes
24     end
25     
26     # Rescan themes directory
27     def self.rescan
28       @@installed_themes = scan_themes
29     end
30     
31     # Return theme for given id, or nil if it's not found
32     def self.theme(id)
33       themes.find {|t| t.id == id}
34     end
35   
36     # Class used to represent a theme
37     class Theme
38       attr_reader :name, :dir, :stylesheets
39       
40       def initialize(path)
41         @dir = File.basename(path)
42         @name = @dir.humanize
43         @stylesheets = Dir.glob("#{path}/stylesheets/*.css").collect {|f| File.basename(f).gsub(/\.css$/, '')}
44       end
45       
46       # Directory name used as the theme id
47       def id; dir end
48
49       def <=>(theme)
50         name <=> theme.name
51       end
52     end
53     
54     private
55         
56     def self.scan_themes
57       dirs = Dir.glob("#{RAILS_ROOT}/public/themes/*").select do |f|
58         # A theme should at least override application.css
59         File.directory?(f) && File.exist?("#{f}/stylesheets/application.css")
60       end
61       dirs.collect {|dir| Theme.new(dir)}.sort
62     end
63   end
64 end
65
66 module ApplicationHelper
67   def stylesheet_path(source)
68     @current_theme ||= Redmine::Themes.theme(Setting.ui_theme)
69     super((@current_theme && @current_theme.stylesheets.include?(source)) ?
70       "/themes/#{@current_theme.dir}/stylesheets/#{source}" : source)
71   end
72   
73   def path_to_stylesheet(source)
74     stylesheet_path source
75   end
76 end