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