mailr/app/controllers/folders_controller.rb

168 lines
5.8 KiB
Ruby
Raw Permalink Normal View History

2011-08-16 23:37:13 +02:00
require 'imap_mailbox'
require 'imap_session'
2011-07-29 20:05:47 +02:00
class FoldersController < ApplicationController
include ImapMailboxModule
include ImapSessionModule
2011-09-03 13:07:40 +02:00
before_filter :check_current_user,:selected_folder, :get_current_folders
2011-07-29 20:05:47 +02:00
2011-09-03 13:07:40 +02:00
before_filter :open_imap_session, :except => [:index,:show_hide,:system]
after_filter :close_imap_session, :except => [:index,:show_hide,:system]
2011-07-29 20:05:47 +02:00
2011-07-31 22:45:29 +02:00
before_filter :get_folders
2011-07-29 20:05:47 +02:00
theme :theme_resolver
def index
2011-09-03 13:07:40 +02:00
@buttons = []
2011-09-09 22:10:25 +02:00
@buttons << {:text => 'show_hide',:scope=>'folder',:image => 'flag.png'}
@buttons << {:text => 'refresh',:scope=>'folder',:image => 'refresh.png'}
2011-07-29 20:05:47 +02:00
end
def create
2011-09-03 13:07:40 +02:00
if params[:folder][:target].empty?
2011-08-16 20:05:58 +02:00
flash[:warning] = t(:to_create_empty,:scope=>:folder)
2011-07-29 20:05:47 +02:00
render "index"
else
begin
2011-08-24 19:20:13 +02:00
#TODO recreate local copy of folders
2011-09-03 13:07:40 +02:00
if params[:folder][:parent].empty?
@mailbox.create_folder(params[:folder][:target])
2011-07-29 20:05:47 +02:00
else
2011-09-03 13:07:40 +02:00
parent_folder = @current_user.folders.find(params[:folder][:parent])
2011-07-29 20:05:47 +02:00
if parent_folder.depth >= $defaults["mailbox_max_parent_folder_depth"].to_i
2011-08-16 20:05:58 +02:00
raise Exception, t(:max_depth,:scope=>:folder)
2011-07-29 20:05:47 +02:00
end
2011-09-03 13:07:40 +02:00
@mailbox.create_folder(parent_folder.full_name + parent_folder.delim + params[:folder][:target])
2011-07-29 20:05:47 +02:00
end
rescue Exception => e
2011-08-16 20:05:58 +02:00
flash[:error] = t(:can_not_create,:scope=>:folder) + ' (' + e.to_s + ')'
2011-07-29 20:05:47 +02:00
render 'index'
return
end
2011-08-24 19:20:13 +02:00
flash[:notice] = t(:was_created,:scope=>:folder)
redirect_to :action => 'index'
2011-07-29 20:05:47 +02:00
end
end
2011-08-24 19:20:13 +02:00
2011-07-29 20:05:47 +02:00
def delete
2011-09-03 13:07:40 +02:00
if params[:folder][:delete].empty?
2011-08-16 20:05:58 +02:00
flash[:warning] = t(:to_delete_empty,:scope=>:folder)
2011-07-29 20:05:47 +02:00
render "index"
else
begin
2011-09-03 13:07:40 +02:00
folder = @current_user.folders.find(params[:folder][:delete])
if @folders_system.include?(folder)
raise Exception, t(:system,:scope=>:folder)
2011-07-29 20:05:47 +02:00
end
2011-08-16 20:05:58 +02:00
@mailbox.delete_folder(folder.full_name)
2011-08-24 19:20:13 +02:00
if @current_folder.eql? folder
2011-09-03 13:07:40 +02:00
session[:selected_folder] = nil
2011-08-24 19:20:13 +02:00
end
folder.destroy
2011-07-29 20:05:47 +02:00
rescue Exception => e
2011-08-16 20:05:58 +02:00
flash[:error] = t(:can_not_delete,:scope=>:folder) + ' (' + e.to_s + ')'
2011-07-29 20:05:47 +02:00
render 'index'
return
end
2011-08-24 19:20:13 +02:00
flash[:notice] = t(:was_deleted,:scope=>:folder)
redirect_to :action => 'index'
2011-07-29 20:05:47 +02:00
end
end
2011-09-03 13:07:40 +02:00
def system
logger.custom('sss',params[:folder].inspect)
@folders.each do |f|
logger.custom('s',f.inspect)
if f.isSystem?
f.setNone
end
if f.id == params[:folder][:mailbox_inbox].to_i
f.setInbox
2011-08-24 19:20:13 +02:00
end
2011-09-03 13:07:40 +02:00
if f.id == params[:folder][:mailbox_sent].to_i
f.setSent
end
if f.id == params[:folder][:mailbox_trash].to_i
f.setTrash
end
if f.id == params[:folder][:mailbox_drafts].to_i
f.setDrafts
end
end
2011-07-31 22:45:29 +02:00
redirect_to :action => 'index'
2011-07-29 20:05:47 +02:00
end
2011-07-31 22:45:29 +02:00
def refresh
2011-09-03 13:07:40 +02:00
# TODO save system folders
if params[:refresh]
Folder.refresh(@mailbox,@current_user)
flash.keep
elsif params[:show_hide]
if !params["folders_to_show"].nil?
@folders.each do |f|
if params["folders_to_show"].include?(f.id.to_s)
f.shown = true
f.save
else
f.shown = false
f.save
end
end
end
end
redirect_to :action => 'index'
2011-07-31 22:45:29 +02:00
end
2011-07-29 20:05:47 +02:00
2011-08-24 19:20:13 +02:00
def select
session[:selected_folder] = params[:id]
redirect_to :controller => 'messages', :action => 'index'
end
def refresh_status
@folders_shown.each do |f|
@mailbox.set_folder(f.full_name)
folder_status = @mailbox.status
f.update_attributes(:total => folder_status['MESSAGES'], :unseen => folder_status['UNSEEN'])
end
redirect_to :controller=> 'messages', :action => 'index'
end
def emptybin
begin
2011-09-03 13:07:40 +02:00
trash_folder = @current_user.folders.trash.first
if trash_folder.nil?
raise Exception, t(:not_configured_trash,:scope=>:folder)
end
2011-08-24 19:20:13 +02:00
@mailbox.set_folder(trash_folder.full_name)
trash_folder.messages.each do |m|
@mailbox.delete_message(m.uid)
end
@mailbox.expunge
trash_folder.messages.destroy_all
trash_folder.update_attributes(:unseen => 0, :total => 0)
rescue Exception => e
2011-09-16 19:44:29 +02:00
flash[:error] = "#{t(:imap_error,:scope=>:common)} (#{e.to_s})"
2011-08-24 19:20:13 +02:00
end
redirect_to :controller => 'messages', :action => 'index'
end
############################################# protected section #######################################
2011-07-29 20:05:47 +02:00
protected
def get_folders
2011-09-03 13:07:40 +02:00
@folders = @current_user.folders
@folders_shown = @current_user.folders.shown
#@folders_system = @current_user.folders.sys
@current_user.folders.inbox.first.nil? ? @folder_inbox = "" : @folder_inbox = @current_user.folders.inbox.first.id
@current_user.folders.drafts.first.nil? ? @folder_drafts = "" : @folder_drafts = @current_user.folders.drafts.first.id
@current_user.folders.sent.first.nil? ? @folder_sent = "" : @folder_sent = @current_user.folders.sent.first.id
@current_user.folders.trash.first.nil? ? @folder_trash = "" : @folder_trash = @current_user.folders.trash.first.id
2011-07-29 20:05:47 +02:00
end
end