keep language when searching
[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_distributions
7   before_filter :set_language
8   helper :all # include all helpers, all the time
9   require "rexml/document"
10
11   init_gettext('software')
12
13   def rescue_action_in_public(exception)
14     @message = exception.message
15     if request.xhr?
16       render :template => "error", :layout => false, :status => 400
17     else
18       render :template => 'error', :layout => "application", :status => 400
19     end
20   end
21
22   private
23
24   def set_language
25     if cookies[:lang]
26       @lang = cookies[:lang]
27     elsif params[:lang]
28       @lang = params[:lang][0].gsub(/\-/, '_')
29     else
30       @lang = request.compatible_language_from(LANGUAGES) || "en"
31     end
32     GetText.locale = @lang
33   end
34
35
36   def set_distributions
37     @distributions = Rails.cache.fetch('distributions', :expires_in => 120.minutes) do
38       load_distributions
39     end
40   end
41
42
43   # load available distributions
44   def load_distributions
45     uri = URI.parse("http://#{API_HOST}/distributions")
46     request = Net::HTTP::Get.new(uri.path)
47     logger.debug "Loading distributions from #{uri}"
48     @distributions = Array.new
49     begin
50       Net::HTTP.start(uri.host, uri.port) do |http|
51         http.read_timeout = 30
52         response = http.request(request)
53         unless( response.kind_of? Net::HTTPSuccess )
54           logger.error "Cannot load distributions: '#{response.code}', message: \n#{response.body}"
55         else
56           doc = REXML::Document.new response.body
57           doc.elements.each("distributions/distribution") { |element|
58             dist = [element.elements['name'].text, element.elements['project'].text]
59             @distributions << dist
60           }
61         end
62       end
63       @distributions << ["ALL Distributions", 'ALL']
64       return @distributions
65     rescue Exception => e
66       logger.error "Error while loading distributions from '#{uri}': " + e.to_s
67     end
68     return nil
69   end
70
71
72 end