Chris@0: # redMine - project management software Chris@0: # Copyright (C) 2006-2007 Jean-Philippe Lang Chris@0: # Chris@0: # This program is free software; you can redistribute it and/or Chris@0: # modify it under the terms of the GNU General Public License Chris@0: # as published by the Free Software Foundation; either version 2 Chris@0: # of the License, or (at your option) any later version. Chris@0: # Chris@0: # This program is distributed in the hope that it will be useful, Chris@0: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@0: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@0: # GNU General Public License for more details. Chris@0: # Chris@0: # You should have received a copy of the GNU General Public License Chris@0: # along with this program; if not, write to the Free Software Chris@0: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@0: Chris@0: module Redmine Chris@0: module Themes Chris@0: Chris@0: # Return an array of installed themes Chris@0: def self.themes Chris@0: @@installed_themes ||= scan_themes Chris@0: end Chris@0: Chris@0: # Rescan themes directory Chris@0: def self.rescan Chris@0: @@installed_themes = scan_themes Chris@0: end Chris@0: Chris@0: # Return theme for given id, or nil if it's not found Chris@0: def self.theme(id) Chris@0: themes.find {|t| t.id == id} Chris@0: end Chris@0: Chris@0: # Class used to represent a theme Chris@0: class Theme Chris@0: attr_reader :name, :dir, :stylesheets Chris@0: Chris@0: def initialize(path) Chris@0: @dir = File.basename(path) Chris@0: @name = @dir.humanize Chris@0: @stylesheets = Dir.glob("#{path}/stylesheets/*.css").collect {|f| File.basename(f).gsub(/\.css$/, '')} Chris@0: end Chris@0: Chris@0: # Directory name used as the theme id Chris@0: def id; dir end Chris@0: Chris@0: def <=>(theme) Chris@0: name <=> theme.name Chris@0: end Chris@0: end Chris@0: Chris@0: private Chris@0: Chris@0: def self.scan_themes Chris@0: dirs = Dir.glob("#{Rails.public_path}/themes/*").select do |f| Chris@0: # A theme should at least override application.css Chris@0: File.directory?(f) && File.exist?("#{f}/stylesheets/application.css") Chris@0: end Chris@0: dirs.collect {|dir| Theme.new(dir)}.sort Chris@0: end Chris@0: end Chris@0: end Chris@0: Chris@0: module ApplicationHelper Chris@0: def stylesheet_path(source) Chris@0: @current_theme ||= Redmine::Themes.theme(Setting.ui_theme) Chris@0: super((@current_theme && @current_theme.stylesheets.include?(source)) ? Chris@0: "/themes/#{@current_theme.dir}/stylesheets/#{source}" : source) Chris@0: end Chris@0: Chris@0: def path_to_stylesheet(source) Chris@0: stylesheet_path source Chris@0: end Chris@0: end