mailr/app/controllers/application_controller.rb

137 lines
3.7 KiB
Ruby
Raw Normal View History

2011-07-21 20:20:15 +02:00
require 'yaml'
2011-06-24 23:48:08 +02:00
2011-07-21 20:20:15 +02:00
class ApplicationController < ActionController::Base
2011-06-24 23:48:08 +02:00
2011-07-21 20:20:15 +02:00
protect_from_forgery
2011-09-03 13:07:40 +02:00
2011-08-16 20:05:58 +02:00
before_filter :load_defaults,:current_user,:set_locale
2011-09-03 13:07:40 +02:00
2011-08-16 20:05:58 +02:00
before_filter :plugins_configuration
2011-09-05 19:08:22 +02:00
# rescue_from ActiveRecord::RecordNotFound do
# logger.custom('record_not_found','exc')
# reset_session
# redirect_to :controller=>'user', :action => 'login'
# end
2011-08-24 19:20:13 +02:00
2011-09-03 13:07:40 +02:00
def load_defaults
$defaults ||= YAML::load(File.open(Rails.root.join('config','defaults.yml')))
end
2011-08-02 23:12:17 +02:00
################################# protected section ###########################################
2011-07-21 20:20:15 +02:00
protected
2009-09-01 14:23:05 +02:00
2011-07-21 20:20:15 +02:00
def theme_resolver
2011-07-23 21:55:26 +02:00
if @current_user.nil?
2011-07-24 15:56:47 +02:00
$defaults['theme']
2011-07-23 21:55:26 +02:00
else
2011-07-24 22:22:13 +02:00
@current_user.prefs.theme || $defaults['theme']
2011-07-23 21:55:26 +02:00
end
2011-07-21 20:20:15 +02:00
end
2009-09-01 14:23:05 +02:00
2011-07-21 20:20:15 +02:00
def set_locale
2011-07-23 21:55:26 +02:00
if @current_user.nil?
2011-07-24 15:56:47 +02:00
I18n.locale = $defaults['locale'] || I18n.default_locale
2011-07-23 21:55:26 +02:00
else
2011-08-16 20:05:58 +02:00
I18n.locale = @current_user.prefs.locale.to_sym || I18n.default_locale
2011-07-23 21:55:26 +02:00
end
end
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def check_current_user
if @current_user.nil?
session["return_to"] = request.fullpath
2011-08-24 19:20:13 +02:00
redirect_to :controller => 'user', :action => 'login'
2011-07-23 21:55:26 +02:00
return false
end
2011-07-21 20:20:15 +02:00
end
2011-07-27 20:34:30 +02:00
def selected_folder
2011-09-03 13:07:40 +02:00
if session[:selected_folder]
@selected_folder = session[:selected_folder]
else
folder = @current_user.folders.inbox.first
if not folder.nil?
@selected_folder = folder.full_name
end
end
2011-07-27 20:34:30 +02:00
end
2011-07-31 22:45:29 +02:00
def get_current_folders
@folders_shown = @current_user.folders.shown.order("name asc")
2011-09-03 13:07:40 +02:00
if not @selected_folder.nil?
@current_folder = @current_user.folders.find_by_full_name(@selected_folder)
end
2011-07-31 22:45:29 +02:00
end
2011-08-28 21:52:17 +02:00
def self.decode_quoted(text,unknown_charset = $defaults["msg_unknown_charset"])
begin
2011-09-03 13:07:40 +02:00
if text.match(/\=\?.+\?\w\?.+\?\=/).nil?
2011-08-28 21:52:17 +02:00
after = Iconv.conv('UTF-8',unknown_charset,text)
#after = text
else
2011-09-03 13:07:40 +02:00
# FIXME support multiple showing of =?xxx?=
2011-08-28 21:52:17 +02:00
after = text
2011-09-03 13:07:40 +02:00
match = text.match(/\=\?.+\?\w\?.+\?\=/).to_s
2011-08-28 21:52:17 +02:00
f = match.split(/\?/)
case f[2].downcase
when 'q':
replace = f[3].gsub(/_/," ").unpack("M").first
when 'b':
replace = f[3].gsub(/_/," ").unpack("m").first
else
replace = match
end
match.gsub!(/\?/,'\?')
match.gsub!(/\)/,'\)')
after = text.gsub(/#{match}/,replace)
if f[1].downcase != 'utf-8'
after = Iconv.conv('UTF-8',f[1],after)
end
end
2011-09-03 13:07:40 +02:00
#logger.custom('after',after)
2011-08-28 21:52:17 +02:00
return after
rescue Exception => e
logger.error("Class Message: #{e.to_s}: T: #{text} M: #{match} R: #{replace} A: #{after}")
return text
end
end
2011-09-05 19:08:22 +02:00
def prepare_compose_buttons
@buttons = []
@buttons << {:text => 'send',:image => 'tick.png'}
@buttons << {:text => 'save_as_draft',:image => 'tick.png'}
end
##################################### protected section ########################################
protected
def get_system_folders
@drafts_folder = @current_user.folders.drafts.first
@sent_folder = @current_user.folders.sent.first
@inbox_folder = @current_user.folders.inbox.first
@trash_folder = @current_user.folders.trash.first
end
2011-08-02 23:12:17 +02:00
##################################### private section ##########################################
2011-08-16 20:05:58 +02:00
private
def plugins_configuration
WillPaginate::ViewHelpers.pagination_options[:previous_label] = t(:previous_page)
WillPaginate::ViewHelpers.pagination_options[:next_label] = t(:next_page)
end
2011-08-02 23:12:17 +02:00
end
2011-08-16 20:05:58 +02:00