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