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