From 19aef7a5edb478f00034a80dd1b13f5d3deefa26 Mon Sep 17 00:00:00 2001 From: Wojciech Todryk Date: Sat, 27 Aug 2011 22:00:06 +0200 Subject: [PATCH] html view almost done --- app/controllers/messages_controller.rb | 23 +++++++++----- app/models/attachment.rb | 24 +++++++------- app/models/message.rb | 31 +++++++++++++------ config/locales/pl.yml | 1 + config/routes.rb | 2 +- themes/olive/stylesheets/style.css | 3 +- themes/olive/views/layouts/html_view.erb | 1 + .../{mail_view.erb => old_html_view.erb} | 0 .../olive/views/messages/_attachment.html.erb | 3 ++ .../views/messages/_attachments.html.erb | 5 +-- themes/olive/views/messages/_body.html.erb | 3 -- .../olive/views/messages/_html_part.html.erb | 3 ++ .../olive/views/messages/html_view.html.erb | 2 ++ ...l_view.html.erb => old_html_view.html.erb} | 0 themes/olive/views/messages/show.html.erb | 4 +-- 15 files changed, 68 insertions(+), 37 deletions(-) create mode 100755 themes/olive/views/layouts/html_view.erb rename themes/olive/views/layouts/{mail_view.erb => old_html_view.erb} (100%) delete mode 100755 themes/olive/views/messages/_body.html.erb create mode 100755 themes/olive/views/messages/_html_part.html.erb create mode 100755 themes/olive/views/messages/html_view.html.erb rename themes/olive/views/messages/{mail_view.html.erb => old_html_view.html.erb} (100%) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index b05d8e8..72fab77 100755 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -139,6 +139,7 @@ class MessagesController < ApplicationController @attachments = [] @render_as_text = [] + @render_as_html_idx = nil @message = @current_user.messages.find_by_uid(params[:id]) @message.update_attributes(:unseen => false) @@ -152,21 +153,29 @@ class MessagesController < ApplicationController Attachment.fromSinglePart(@attachments,@message.id,@mail) end - @attachments.each do |a| - a.isText? ? @render_as_text << a.content_normalized : @render_as_text + for idx in 0..@attachments.size-1 + @attachments[idx].isText? ? @render_as_text << @attachments[idx].decode_and_charset : @render_as_text + @attachments[idx].isHtml? ? @render_as_html_idx ||= idx : @render_as_html_idx end end def body - message = @mailbox.fetch_body(params[:id].to_i) - mail = Mail.new(message) - @title = '' - @body = '' + attachments = [] + message = @current_user.messages.find(params[:id]) + mail = Mail.new(@mailbox.fetch_body(message.uid)) + if mail.multipart? + Attachment.fromMultiParts(attachments,message.id,mail.parts) + else + Attachment.fromSinglePart(attachments,message.id,mail) + end + a = attachments[params[:idx].to_i] + @title = 'aaaaa' + @body = a.decode_and_charset # #header = parts[0] #body = parts[1] #@body = "ala
#{header}
#{mail.inspect}" - render 'mail_view',:layout => 'mail_view' + render 'html_view',:layout => 'html_view' end def attachment diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 9eae108..de46307 100755 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -32,35 +32,31 @@ class Attachment multipart end - def self.fromMultiParts(attachments,id,parts,idx=0) + def self.fromMultiParts(attachments,id,parts) parts.each do |part| a = Attachment.new( :message_id => id, :description => part.content_description, :type => part.content_type, :content => part.body.raw_source, :encoding => part.content_transfer_encoding, - :idx => idx, :multipart => part.multipart? ) if a.multipart? - idx += 1 - fromMultiParts(attachments,id,part.parts,idx) + fromMultiParts(attachments,id,part.parts) else attachments << a - idx += 1 end - #FIXME problem with enueration of attachemnts end end - def self.fromSinglePart(attachments,id,part,idx=0) + def self.fromSinglePart(attachments,id,part) a = Attachment.new( :message_id => id, :description => part.content_description, :type => part.content_type, :encoding => part.body.encoding, :charset => part.body.charset, - :content => part.body.raw_source, - :idx => idx + :content => part.body.raw_source + ) attachments << a end @@ -103,6 +99,10 @@ class Attachment @type.nil? or @type =~ /^text\/plain/ end + def isHtml? + @type =~ /^text\/html/ + end + def title @description.nil? ? name : @description end @@ -128,9 +128,11 @@ class Attachment end def decode + case @encoding when /quoted-printable/ - decoded = @content.gsub(/_/," ").unpack("M").first + #decoded = @content.gsub(/_/," ").unpack("M").first + decoded = @content.unpack("M").first when /base64/ decoded = @content.unpack("m").first when /uuencode/ @@ -148,7 +150,7 @@ class Attachment end end - def content_normalized + def decode_and_charset decoded = decode diff --git a/app/models/message.rb b/app/models/message.rb index e676b4e..df21096 100755 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -10,18 +10,29 @@ class Message < ActiveRecord::Base def self.decode(text,unknown_encoding = $defaults["msg_unknown_encoding"]) begin - if text.=~(/^=\?/).nil? + if text.=~(/=\?/).nil? after = Iconv.conv('UTF-8',unknown_encoding,text) else - f = text.split(/\?/) - case f[2].downcase - when 'q': - after = f[3].gsub(/_/," ").unpack("M").first - when 'b': - after = f[3].gsub(/_/," ").unpack("m").first - else - return text - end +# TODO support multiple showing of =?xxx?= + text =~ /(=\?\S+\?=)/ + after = text + match = $1 + logger.custom('match',match) + 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 + logger.custom('replace',replace) + match.gsub!(/\?/,'\?') + match.gsub!(/\)/,'\)') + after = text.gsub(/#{match}/,replace) + + logger.custom('after',after) if f[1].downcase != 'utf-8' after = Iconv.conv('UTF-8',f[1],after) diff --git a/config/locales/pl.yml b/config/locales/pl.yml index 6af3a9c..9a2bcf7 100755 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -139,5 +139,6 @@ pl: no_data: Brak danych logout: Wyloguj download: Pobierz + view: Pokaż version: Wersja diff --git a/config/routes.rb b/config/routes.rb index 028fbb9..edad3f2 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -35,7 +35,7 @@ Mailr::Application.routes.draw do match "messages/reply/:id" => 'messages#reply' post "messages/sendout" match "messages/show/:id" => 'messages#show' - match "messages/body/:id" => 'messages#body' , :as => :messages_body + match "messages/body/:id/:idx" => 'messages#body' , :as => :messages_part_body match "messages/attachment/:id/:idx" => 'messages#attachment', :as => :messages_attachment_download get "user/logout" diff --git a/themes/olive/stylesheets/style.css b/themes/olive/stylesheets/style.css index c3df1e6..ffa0545 100755 --- a/themes/olive/stylesheets/style.css +++ b/themes/olive/stylesheets/style.css @@ -444,7 +444,7 @@ div.msg_header { iframe { width: 937px; - height: 600px; + height: 800px; margin-top: 10px; } @@ -495,6 +495,7 @@ div.render_text span{ } div.render_text pre { + font-size: 12px; background-color: #EFF3E4; padding: 5px; border: 1px solid #5E634E; diff --git a/themes/olive/views/layouts/html_view.erb b/themes/olive/views/layouts/html_view.erb new file mode 100755 index 0000000..37f0bdd --- /dev/null +++ b/themes/olive/views/layouts/html_view.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/themes/olive/views/layouts/mail_view.erb b/themes/olive/views/layouts/old_html_view.erb similarity index 100% rename from themes/olive/views/layouts/mail_view.erb rename to themes/olive/views/layouts/old_html_view.erb diff --git a/themes/olive/views/messages/_attachment.html.erb b/themes/olive/views/messages/_attachment.html.erb index 7041190..5cb65cb 100755 --- a/themes/olive/views/messages/_attachment.html.erb +++ b/themes/olive/views/messages/_attachment.html.erb @@ -13,3 +13,6 @@ <%= link_to t(:download), messages_attachment_download_path(attachment.message_id,attachment.idx) %> + +<%= link_to t(:view), messages_part_body_path(attachment.message_id,attachment.idx) %> + diff --git a/themes/olive/views/messages/_attachments.html.erb b/themes/olive/views/messages/_attachments.html.erb index a0061ba..9cbc76e 100755 --- a/themes/olive/views/messages/_attachments.html.erb +++ b/themes/olive/views/messages/_attachments.html.erb @@ -3,9 +3,10 @@ <% trclass = :even %> -<% @attachments.each do |a| %> +<% for idx in 0..@attachments.size-1 %> +<% @attachments[idx].idx = idx %> - <%= render :partial => 'attachment', :object => a %> + <%= render :partial => 'attachment', :object => @attachments[idx] %> <% trclass == :even ? trclass = :odd : trclass = :even %> <% end %> diff --git a/themes/olive/views/messages/_body.html.erb b/themes/olive/views/messages/_body.html.erb deleted file mode 100755 index 326981c..0000000 --- a/themes/olive/views/messages/_body.html.erb +++ /dev/null @@ -1,3 +0,0 @@ - - diff --git a/themes/olive/views/messages/_html_part.html.erb b/themes/olive/views/messages/_html_part.html.erb new file mode 100755 index 0000000..be33489 --- /dev/null +++ b/themes/olive/views/messages/_html_part.html.erb @@ -0,0 +1,3 @@ + + diff --git a/themes/olive/views/messages/html_view.html.erb b/themes/olive/views/messages/html_view.html.erb new file mode 100755 index 0000000..b1f0d7c --- /dev/null +++ b/themes/olive/views/messages/html_view.html.erb @@ -0,0 +1,2 @@ +<%= raw @body -%> + diff --git a/themes/olive/views/messages/mail_view.html.erb b/themes/olive/views/messages/old_html_view.html.erb similarity index 100% rename from themes/olive/views/messages/mail_view.html.erb rename to themes/olive/views/messages/old_html_view.html.erb diff --git a/themes/olive/views/messages/show.html.erb b/themes/olive/views/messages/show.html.erb index 21956c1..cb70ed7 100755 --- a/themes/olive/views/messages/show.html.erb +++ b/themes/olive/views/messages/show.html.erb @@ -19,8 +19,8 @@ <%= render :partial => 'attachments' %> <% end %> - <% if not @render_rich.nil? %> - <%= render :partial => 'body' ,:object => @render_body %> + <% if not @render_as_html_idx.nil? %> + <%= render :partial => 'html_part' %> <% else %>
<%= t(:content,:scope=>:message) %>