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