folder managing refactor

This commit is contained in:
Eugene Korbut 2009-02-08 07:22:25 +10:00
parent 69b208f27a
commit 0d20ce14b8
10 changed files with 109 additions and 88 deletions

View file

@ -0,0 +1,23 @@
class FoldersController < ApplicationController
include ImapUtils
before_filter :login_required
before_filter :load_imap_session
after_filter :close_imap_session
layout 'public'
def index
@folders = @mailbox.folders
end
def create
@mailbox.create_folder(CDF::CONFIG[:mail_inbox] + '.' + params[:folder])
redirect_to folders_path
end
def destroy
@mailbox.delete_folder params[:id]
redirect_to folders_path
end
end

View file

@ -5,7 +5,7 @@ require 'mail2screen'
require 'ezcrypto' require 'ezcrypto'
class WebmailController < ApplicationController class WebmailController < ApplicationController
# uses_component_template_root include ImapUtils
# Administrative functions # Administrative functions
before_filter :login_required before_filter :login_required
@ -35,17 +35,6 @@ class WebmailController < ApplicationController
redirect_to(:action=>'messages') redirect_to(:action=>'messages')
end end
def manage_folders
if operation_param == _('Add folder')
@mailbox.create_folder(CDF::CONFIG[:mail_inbox]+"."+params["folder_name"])
elsif operation_param == _('(Delete)')
@mailbox.delete_folder(params["folder_name"])
elsif operation_param == _('(Subscribe)')
elsif operation_param == _('(Select)')
end
@folders = @mailbox.folders
end
def messages def messages
session["return_to"] = nil session["return_to"] = nil
@search_field = params['search_field'] @search_field = params['search_field']
@ -334,62 +323,12 @@ class WebmailController < ApplicationController
private private
def get_upass
if CDF::CONFIG[:crypt_session_pass]
EzCrypto::Key.decrypt_with_password(CDF::CONFIG[:encryption_password], CDF::CONFIG[:encryption_salt], session["wmp"])
else
# retrun it plain
session["wmp"]
end
end
def get_to_folders def get_to_folders
res = Array.new res = Array.new
@folders.each{|f| res << f unless f.name == CDF::CONFIG[:mail_sent] or f.name == CDF::CONFIG[:mail_inbox] } @folders.each{|f| res << f unless f.name == CDF::CONFIG[:mail_sent] or f.name == CDF::CONFIG[:mail_inbox] }
res res
end end
def load_imap_session
return if ['error_connection'].include?(action_name)
get_imap_session
end
def get_imap_session
begin
@mailbox = IMAPMailbox.new
uname = (get_mail_prefs.check_external_mail == 1 ? user.email : user.local_email)
upass = get_upass
@mailbox.connect(uname, upass)
load_folders
rescue Exception => ex
logger.error("Exception on loggin webmail session - #{ex} - #{ex.backtrace.join("\t\n")}")
render :action => "error_connection"
end
end
def close_imap_session
return if @mailbox.nil? or not(@mailbox.connected)
@mailbox.disconnect
@mailbox = nil
end
def have_to_load_folders?
return true if ['messages', 'delete', 'reply', 'forward', 'empty', 'message', 'download',
'filter', 'filter_add', 'view_source', 'compose', 'prefs', 'filters'].include?(action_name)
return false
end
def load_folders
if have_to_load_folders?()
if params["folder_name"]
@folder_name = params["folder_name"]
else
@folder_name = session["folder_name"] ? session["folder_name"] : CDF::CONFIG[:mail_inbox]
end
session["folder_name"] = @folder_name
@folders = @mailbox.folders if @folders.nil?
end
end
def create_mail def create_mail
m = CDF::Mail.new(user.mail_temporary_path) m = CDF::Mail.new(user.mail_temporary_path)
@ -416,19 +355,6 @@ class WebmailController < ApplicationController
m m
end end
def user
@user = Customer.find(logged_customer) if @user.nil?
@user
end
def get_mail_prefs
if not(@mailprefs)
if not(@mailprefs = MailPref.find_by_customer_id(logged_customer))
@mailprefs = MailPref.create("customer_id"=>logged_customer)
end
end
@mailprefs
end
def send_part(part) def send_part(part)
if part.content_type == "text/html" if part.content_type == "text/html"

View file

@ -0,0 +1,2 @@
module FoldersHelper
end

View file

@ -19,11 +19,12 @@ module NavigationHelper
if folder.name == CDF::CONFIG[:mail_trash] or folder.name == CDF::CONFIG[:mail_inbox] or folder.name == CDF::CONFIG[:mail_sent] if folder.name == CDF::CONFIG[:mail_trash] or folder.name == CDF::CONFIG[:mail_inbox] or folder.name == CDF::CONFIG[:mail_sent]
short_fn(folder) short_fn(folder)
else else
return short_fn(folder) + short_fn(folder) + '&nbsp;' + link_to(_('Delete'), folder_path(folder.name), :method => :delete)
("&nbsp;" + link_to(_('(Delete)'), :controller=>"webmail", :action=>"manage_folders", :params=>{"op"=>_('(Delete)'), "folder_name"=>folder.name}))
end end
end end
private
def short_fn(folder) def short_fn(folder)
if folder.name.include? folder.delim if folder.name.include? folder.delim
"&nbsp; &nbsp;" + folder.name.split(folder.delim).last "&nbsp; &nbsp;" + folder.name.split(folder.delim).last

View file

@ -35,10 +35,6 @@ module WebmailHelper
link_to(_('View source'), {:controller=>"webmail", :action=>"view_source", :params=>{"msg_id"=>msg_id}}, {'target'=>"_blank"}) link_to(_('View source'), {:controller=>"webmail", :action=>"view_source", :params=>{"msg_id"=>msg_id}}, {'target'=>"_blank"})
end end
def link_manage_folders
link_to(_('add/edit'), :controller=>"webmail", :action=>"manage_folders")
end
def link_filter_add def link_filter_add
link_to(_('Add filter'), :controller=>'webmail', :action=>'filter_add') link_to(_('Add filter'), :controller=>'webmail', :action=>'filter_add')
end end

View file

@ -19,13 +19,13 @@
<div id="folders"> <div id="folders">
<h4><%=_('Add folder')%></h4> <h4><%=_('Add folder')%></h4>
<hr/> <hr/>
<form action="/webmail/webmail/manage_folders" method="post"> <% form_tag folders_path, :id => 'new_folder' do %>
<ul> <ul>
<li><label for='folder_name'><%=_('Name')%>:</label></li> <li><label for='folder'><%=_('Name')%>:</label></li>
<li><input type="text" name="folder_name" value="" size="18" id='folder_name'/></li> <li><%= text_field_tag 'folder', '', :size => 18 %></li>
<li><input type="submit" name="op" value="<%=_('Add folder')%>"/></li> <li><%= submit_tag _('Add folder') %></li>
</ul> </ul>
</form> <% end %>
</div> </div>
<% } %> <% } %>

View file

@ -1,5 +1,5 @@
<div id="folders"> <div id="folders">
<h4><%=_('Folders')%><br/><%=link_manage_folders%></h4> <h4><%=_('Folders')%><br/><%= link_to 'add/edit', folders_path %></h4>
<hr/> <hr/>
<ul> <% for folder in @folders %> <ul> <% for folder in @folders %>
<li><%=folder_link(folder)%></li> <% end %> <li><%=folder_link(folder)%></li> <% end %>

View file

@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :folders, :requirements => {:id => /[^\/]+/}
# Add your own custom routes here. # Add your own custom routes here.
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.

69
lib/imap_utils.rb Normal file
View file

@ -0,0 +1,69 @@
module ImapUtils
private
def load_imap_session
return if ['error_connection'].include?(action_name)
get_imap_session
end
def get_imap_session
begin
@mailbox = IMAPMailbox.new
uname = (get_mail_prefs.check_external_mail == 1 ? user.email : user.local_email)
upass = get_upass
@mailbox.connect(uname, upass)
load_folders
rescue Exception => ex
# logger.error("Exception on loggin webmail session - #{ex} - #{ex.backtrace.join("\t\n")}")
# render :action => "error_connection"
render :text => ex.inspect, :content_type => 'text/plain'
end
end
def close_imap_session
return if @mailbox.nil? or not(@mailbox.connected)
@mailbox.disconnect
@mailbox = nil
end
def get_mail_prefs
if not(@mailprefs)
if not(@mailprefs = MailPref.find_by_customer_id(logged_customer))
@mailprefs = MailPref.create("customer_id"=>logged_customer)
end
end
@mailprefs
end
def get_upass
if CDF::CONFIG[:crypt_session_pass]
EzCrypto::Key.decrypt_with_password(CDF::CONFIG[:encryption_password], CDF::CONFIG[:encryption_salt], session["wmp"])
else
# retrun it plain
session["wmp"]
end
end
def load_folders
if have_to_load_folders?()
if params["folder_name"]
@folder_name = params["folder_name"]
else
@folder_name = session["folder_name"] ? session["folder_name"] : CDF::CONFIG[:mail_inbox]
end
session["folder_name"] = @folder_name
@folders = @mailbox.folders if @folders.nil?
end
end
def user
@user = Customer.find(logged_customer) if @user.nil?
@user
end
def have_to_load_folders?
return true if ['messages', 'delete', 'reply', 'forward', 'empty', 'message', 'download',
'filter', 'filter_add', 'view_source', 'compose', 'prefs', 'filters'].include?(action_name)
return false
end
end

View file

@ -57,4 +57,6 @@ b.br {bottom: -1px; right: -1px; background-position: bottom right;}
#wholepage { color: #000 } #wholepage { color: #000 }
#logout {font-size: 10px; color: #5350b9; float: right;} #logout {font-size: 10px; color: #5350b9; float: right;}
#new_folder ul {list-style: none;}