1/*
2 * Copyright (C) 2008 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.mail;
18
19import java.util.Date;
20import java.util.HashSet;
21
22public abstract class Message implements Part, Body {
23    public static final Message[] EMPTY_ARRAY = new Message[0];
24
25    public enum RecipientType {
26        TO, CC, BCC,
27    }
28
29    protected String mUid;
30
31    private HashSet<Flag> mFlags = null;
32
33    protected Date mInternalDate;
34
35    protected Folder mFolder;
36
37    public String getUid() {
38        return mUid;
39    }
40
41    public void setUid(String uid) {
42        this.mUid = uid;
43    }
44
45    public Folder getFolder() {
46        return mFolder;
47    }
48
49    public abstract String getSubject() throws MessagingException;
50
51    public abstract void setSubject(String subject) throws MessagingException;
52
53    public Date getInternalDate() {
54        return mInternalDate;
55    }
56
57    public void setInternalDate(Date internalDate) {
58        this.mInternalDate = internalDate;
59    }
60
61    public abstract Date getReceivedDate() throws MessagingException;
62
63    public abstract Date getSentDate() throws MessagingException;
64
65    public abstract void setSentDate(Date sentDate) throws MessagingException;
66
67    public abstract Address[] getRecipients(RecipientType type) throws MessagingException;
68
69    public abstract void setRecipients(RecipientType type, Address[] addresses)
70            throws MessagingException;
71
72    public void setRecipient(RecipientType type, Address address) throws MessagingException {
73        setRecipients(type, new Address[] {
74            address
75        });
76    }
77
78    public abstract Address[] getFrom() throws MessagingException;
79
80    public abstract void setFrom(Address from) throws MessagingException;
81
82    public abstract Address[] getReplyTo() throws MessagingException;
83
84    public abstract void setReplyTo(Address[] from) throws MessagingException;
85
86    public abstract Body getBody() throws MessagingException;
87
88    public abstract String getContentType() throws MessagingException;
89
90    public abstract void addHeader(String name, String value) throws MessagingException;
91
92    public abstract void setHeader(String name, String value) throws MessagingException;
93
94    public abstract String[] getHeader(String name) throws MessagingException;
95
96    public abstract void removeHeader(String name) throws MessagingException;
97
98    // Always use these instead of getHeader("Message-ID") or setHeader("Message-ID");
99    public abstract void setMessageId(String messageId) throws MessagingException;
100    public abstract String getMessageId() throws MessagingException;
101
102    public abstract void setBody(Body body) throws MessagingException;
103
104    public boolean isMimeType(String mimeType) throws MessagingException {
105        return getContentType().startsWith(mimeType);
106    }
107
108    private HashSet<Flag> getFlagSet() {
109        if (mFlags == null) {
110            mFlags = new HashSet<Flag>();
111        }
112        return mFlags;
113    }
114
115    /*
116     * TODO Refactor Flags at some point to be able to store user defined flags.
117     */
118    public Flag[] getFlags() {
119        return getFlagSet().toArray(new Flag[] {});
120    }
121
122    /**
123     * Set/clear a flag directly, without involving overrides of {@link #setFlag} in subclasses.
124     * Only used for testing.
125     */
126    public final void setFlagDirectlyForTest(Flag flag, boolean set) throws MessagingException {
127        if (set) {
128            getFlagSet().add(flag);
129        } else {
130            getFlagSet().remove(flag);
131        }
132    }
133
134    public void setFlag(Flag flag, boolean set) throws MessagingException {
135        setFlagDirectlyForTest(flag, set);
136    }
137
138    /**
139     * This method calls setFlag(Flag, boolean)
140     * @param flags
141     * @param set
142     */
143    public void setFlags(Flag[] flags, boolean set) throws MessagingException {
144        for (Flag flag : flags) {
145            setFlag(flag, set);
146        }
147    }
148
149    public boolean isSet(Flag flag) {
150        return getFlagSet().contains(flag);
151    }
152
153    public abstract void saveChanges() throws MessagingException;
154
155    @Override
156    public String toString() {
157        return getClass().getSimpleName() + ':' + mUid;
158    }
159}
160