set default to false
[opensuse:software-o-o.git] / app / controllers / application_controller.rb
1 # Filters added to this controller apply to all controllers in the application.
2 # Likewise, all the methods added will be available for all controllers.
3
4 class ApplicationController < ActionController::Base
5
6   before_filter :set_language
7   before_filter :set_distributions
8   
9   helper :all # include all helpers, all the time
10   require "rexml/document"
11
12   init_gettext('software')
13
14   protected
15
16   def rescue_action_locally( exception )
17     rescue_action_in_public( exception )
18   end
19
20   def rescue_action_in_public(exception)
21     @message = exception.message
22     if request.xhr?
23       render :template => "error", :layout => false, :status => 400
24     else
25       render :template => 'error', :layout => "application", :status => 400
26     end
27   end
28
29   def set_language
30     if cookies[:lang]
31       @lang = cookies[:lang]
32     elsif params[:lang]
33       @lang = params[:lang][0]
34     end
35     @lang.gsub!(/_/, '-') if @lang
36     if !@lang || !LANGUAGES.include?( @lang )
37       if !request.compatible_language_from(LANGUAGES).blank?
38         @lang = request.compatible_language_from(LANGUAGES).dup
39       else
40         @lang = "en"
41       end
42     end
43     @lang.gsub!(/-/, '_')
44     GetText.locale = @lang
45   end
46
47
48   def set_distributions
49     @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
50       load_distributions
51     end
52   end
53
54
55   # load available distributions
56   def load_distributions
57     uri = URI.parse("http://#{API_HOST}/distributions")
58     request = Net::HTTP::Get.new(uri.path)
59     logger.debug "Loading distributions from #{uri}"
60     @distributions = Array.new
61     begin
62       Net::HTTP.start(uri.host, uri.port) do |http|
63         http.read_timeout = 30
64         response = http.request(request)
65         unless( response.kind_of? Net::HTTPSuccess )
66           logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
67         else
68           doc = REXML::Document.new response.body
69           doc.elements.each("distributions/distribution") { |element|
70             dist = [element.elements['name'].text, element.elements['project'].text]
71             @distributions << dist
72           }
73         end
74       end
75       @distributions << ["ALL Distributions", 'ALL']
76       return @distributions
77     rescue Exception => e
78       logger.error "Error while loading distributions from '#{uri}': " + e.to_s
79     end
80     return nil
81   end
82
83
84 end