1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.emailcommon.utility;
18
19import com.android.emailcommon.internet.MimeHeader;
20import com.android.emailcommon.internet.MimeUtility;
21import com.android.emailcommon.mail.MessagingException;
22import com.android.emailcommon.mail.Part;
23import com.android.emailcommon.provider.EmailContent;
24
25import android.text.TextUtils;
26
27import java.util.ArrayList;
28
29public class ConversionUtilities {
30    /**
31     * Values for HEADER_ANDROID_BODY_QUOTED_PART to tag body parts
32     */
33    public static final String BODY_QUOTED_PART_REPLY = "quoted-reply";
34    public static final String BODY_QUOTED_PART_FORWARD = "quoted-forward";
35    public static final String BODY_QUOTED_PART_INTRO = "quoted-intro";
36
37    /**
38     * Helper function to append text to a StringBuffer, creating it if necessary.
39     * Optimization:  The majority of the time we are *not* appending - we should have a path
40     * that deals with single strings.
41     */
42    private static StringBuffer appendTextPart(StringBuffer sb, String newText) {
43        if (newText == null) {
44            return sb;
45        }
46        else if (sb == null) {
47            sb = new StringBuffer(newText);
48        } else {
49            if (sb.length() > 0) {
50                sb.append('\n');
51            }
52            sb.append(newText);
53        }
54        return sb;
55    }
56
57    /**
58     * Copy body text (plain and/or HTML) from MimeMessage to provider Message
59     */
60    public static boolean updateBodyFields(EmailContent.Body body,
61            EmailContent.Message localMessage, ArrayList<Part> viewables)
62    throws MessagingException {
63
64        body.mMessageKey = localMessage.mId;
65
66        StringBuffer sbHtml = null;
67        StringBuffer sbText = null;
68        StringBuffer sbHtmlReply = null;
69        StringBuffer sbTextReply = null;
70        StringBuffer sbIntroText = null;
71
72        for (Part viewable : viewables) {
73            String text = MimeUtility.getTextFromPart(viewable);
74            String[] replyTags = viewable.getHeader(MimeHeader.HEADER_ANDROID_BODY_QUOTED_PART);
75            String replyTag = null;
76            if (replyTags != null && replyTags.length > 0) {
77                replyTag = replyTags[0];
78            }
79            // Deploy text as marked by the various tags
80            boolean isHtml = "text/html".equalsIgnoreCase(viewable.getMimeType());
81
82            if (replyTag != null) {
83                boolean isQuotedReply = BODY_QUOTED_PART_REPLY.equalsIgnoreCase(replyTag);
84                boolean isQuotedForward = BODY_QUOTED_PART_FORWARD.equalsIgnoreCase(replyTag);
85                boolean isQuotedIntro = BODY_QUOTED_PART_INTRO.equalsIgnoreCase(replyTag);
86
87                if (isQuotedReply || isQuotedForward) {
88                    if (isHtml) {
89                        sbHtmlReply = appendTextPart(sbHtmlReply, text);
90                    } else {
91                        sbTextReply = appendTextPart(sbTextReply, text);
92                    }
93                    // Set message flags as well
94                    localMessage.mFlags &= ~EmailContent.Message.FLAG_TYPE_MASK;
95                    localMessage.mFlags |= isQuotedReply
96                        ? EmailContent.Message.FLAG_TYPE_REPLY
97                            : EmailContent.Message.FLAG_TYPE_FORWARD;
98                    continue;
99                }
100                if (isQuotedIntro) {
101                    sbIntroText = appendTextPart(sbIntroText, text);
102                    continue;
103                }
104            }
105
106            // Most of the time, just process regular body parts
107            if (isHtml) {
108                sbHtml = appendTextPart(sbHtml, text);
109            } else {
110                sbText = appendTextPart(sbText, text);
111            }
112        }
113
114        // write the combined data to the body part
115        if (!TextUtils.isEmpty(sbText)) {
116            String text = sbText.toString();
117            body.mTextContent = text;
118            localMessage.mSnippet = TextUtilities.makeSnippetFromPlainText(text);
119        }
120        if (!TextUtils.isEmpty(sbHtml)) {
121            String text = sbHtml.toString();
122            body.mHtmlContent = text;
123            if (localMessage.mSnippet == null) {
124                localMessage.mSnippet = TextUtilities.makeSnippetFromHtmlText(text);
125            }
126        }
127        if (sbHtmlReply != null && sbHtmlReply.length() != 0) {
128            body.mHtmlReply = sbHtmlReply.toString();
129        }
130        if (sbTextReply != null && sbTextReply.length() != 0) {
131            body.mTextReply = sbTextReply.toString();
132        }
133        if (sbIntroText != null && sbIntroText.length() != 0) {
134            body.mIntroText = sbIntroText.toString();
135        }
136        return true;
137    }
138}
139