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@117: def self.theme(id, options={}) Chris@117: return nil if id.blank? Chris@117: Chris@117: found = themes.find {|t| t.id == id} Chris@117: if found.nil? && options[:rescan] != false Chris@117: rescan Chris@117: found = theme(id, :rescan => false) Chris@117: end Chris@117: found Chris@0: end Chris@0: Chris@0: # Class used to represent a theme Chris@0: class Theme Chris@117: attr_reader :path, :name, :dir Chris@0: Chris@0: def initialize(path) Chris@117: @path = path Chris@0: @dir = File.basename(path) Chris@0: @name = @dir.humanize Chris@117: @stylesheets = nil Chris@117: @javascripts = nil Chris@0: end Chris@0: Chris@0: # Directory name used as the theme id Chris@0: def id; dir end Chris@117: Chris@117: def ==(theme) Chris@117: theme.is_a?(Theme) && theme.dir == dir Chris@117: end Chris@117: Chris@0: def <=>(theme) Chris@0: name <=> theme.name Chris@0: end Chris@117: Chris@117: def stylesheets Chris@117: @stylesheets ||= assets("stylesheets", "css") Chris@117: end Chris@117: Chris@117: def javascripts Chris@117: @javascripts ||= assets("javascripts", "js") Chris@117: end Chris@117: Chris@117: def stylesheet_path(source) Chris@117: "/themes/#{dir}/stylesheets/#{source}" Chris@117: end Chris@117: Chris@117: def javascript_path(source) Chris@117: "/themes/#{dir}/javascripts/#{source}" Chris@117: end Chris@117: Chris@117: private Chris@117: Chris@117: def assets(dir, ext) Chris@117: Dir.glob("#{path}/#{dir}/*.#{ext}").collect {|f| File.basename(f).gsub(/\.#{ext}$/, '')} Chris@117: end Chris@0: end Chris@0: Chris@0: private Chris@117: 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@117: def current_theme Chris@117: unless instance_variable_defined?(:@current_theme) Chris@117: @current_theme = Redmine::Themes.theme(Setting.ui_theme) Chris@117: end Chris@117: @current_theme Chris@117: end Chris@117: Chris@0: def stylesheet_path(source) Chris@117: if current_theme && current_theme.stylesheets.include?(source) Chris@117: super current_theme.stylesheet_path(source) Chris@117: else Chris@117: super Chris@117: end Chris@0: end Chris@0: Chris@0: def path_to_stylesheet(source) Chris@0: stylesheet_path source Chris@0: end Chris@117: Chris@117: # Returns the header tags for the current theme Chris@117: def heads_for_theme Chris@117: if current_theme && current_theme.javascripts.include?('theme') Chris@117: javascript_include_tag current_theme.javascript_path('theme') Chris@117: end Chris@117: end Chris@0: end