Folder.java revision 0d1078363581db8caded06cf94e729e88a88761a
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.email.mail;
18
19
20public abstract class Folder {
21    public enum OpenMode {
22        READ_WRITE, READ_ONLY,
23    }
24
25    public enum FolderType {
26        HOLDS_FOLDERS, HOLDS_MESSAGES,
27    }
28
29    /**
30     * Identifiers of "special" folders.
31     */
32    public enum FolderRole {
33        INBOX,      // NOTE:  The folder's name must be INBOX
34        TRASH,
35        SENT,
36        DRAFTS,
37
38        OUTBOX,     // Local folders only - not used in remote Stores
39        OTHER,      // this folder has no specific role
40        UNKNOWN     // the role of this folder is unknown
41    }
42
43    /**
44     * Forces an open of the MailProvider. If the provider is already open this
45     * function returns without doing anything.
46     *
47     * @param mode READ_ONLY or READ_WRITE
48     * @param callbacks Pointer to callbacks class.  This may be used by the folder between this
49     * time and when close() is called.  This is only used for remote stores - should be null
50     * for LocalStore.LocalFolder.
51     */
52    public abstract void open(OpenMode mode, PersistentDataCallbacks callbacks)
53            throws MessagingException;
54
55    /**
56     * Forces a close of the MailProvider. Any further access will attempt to
57     * reopen the MailProvider.
58     *
59     * @param expunge If true all deleted messages will be expunged.
60     */
61    public abstract void close(boolean expunge) throws MessagingException;
62
63    /**
64     * @return True if further commands are not expected to have to open the
65     *         connection.
66     */
67    // TODO not used, get rid of this - it's a transport function
68    public abstract boolean isOpen();
69
70    /**
71     * Get the mode the folder was opened with. This may be different than the mode the open
72     * was requested with.
73     * @return
74     */
75    public abstract OpenMode getMode() throws MessagingException;
76
77    public abstract boolean create(FolderType type) throws MessagingException;
78
79    public abstract boolean exists() throws MessagingException;
80
81    /**
82     * @return A count of the messages in the selected folder.
83     */
84    public abstract int getMessageCount() throws MessagingException;
85
86    public abstract int getUnreadMessageCount() throws MessagingException;
87
88    public abstract Message getMessage(String uid) throws MessagingException;
89
90    public abstract Message[] getMessages(int start, int end, MessageRetrievalListener listener)
91            throws MessagingException;
92
93    /**
94     * Fetches the given list of messages. The specified listener is notified as
95     * each fetch completes. Messages are downloaded as (as) lightweight (as
96     * possible) objects to be filled in with later requests. In most cases this
97     * means that only the UID is downloaded.
98     *
99     * @param uids
100     * @param listener
101     */
102    public abstract Message[] getMessages(MessageRetrievalListener listener)
103            throws MessagingException;
104
105    public abstract Message[] getMessages(String[] uids, MessageRetrievalListener listener)
106            throws MessagingException;
107
108    /**
109     * Return a set of messages based on the state of the flags.
110     * Note: Not typically implemented in remote stores, so not abstract.
111     *
112     * @param setFlags The flags that should be set for a message to be selected (can be null)
113     * @param clearFlags The flags that should be clear for a message to be selected (can be null)
114     * @param listener
115     * @return A list of messages matching the desired flag states.
116     * @throws MessagingException
117     */
118    public Message[] getMessages(Flag[] setFlags, Flag[] clearFlags,
119            MessageRetrievalListener listener) throws MessagingException {
120        throw new MessagingException("Not implemented");
121    }
122
123    public abstract void appendMessages(Message[] messages) throws MessagingException;
124
125    public abstract void copyMessages(Message[] msgs, Folder folder,
126            MessageUpdateCallbacks callbacks) throws MessagingException;
127
128    public abstract void setFlags(Message[] messages, Flag[] flags, boolean value)
129            throws MessagingException;
130
131    public abstract Message[] expunge() throws MessagingException;
132
133    public abstract void fetch(Message[] messages, FetchProfile fp,
134            MessageRetrievalListener listener) throws MessagingException;
135
136    public abstract void delete(boolean recurse) throws MessagingException;
137
138    public abstract String getName();
139
140    public abstract Flag[] getPermanentFlags() throws MessagingException;
141
142    /**
143     * This method returns a string identifying the name of a "role" folder
144     * (such as inbox, draft, sent, or trash).  Stores that do not implement this
145     * feature can be used - the account UI will provide default strings.  To
146     * let the server identify specific folder roles, simply override this method.
147     *
148     * @return The server- or protocol- specific role for this folder.  If some roles are known
149     * but this is not one of them, return FolderRole.OTHER.  If roles are unsupported here,
150     * return FolderRole.UNKNOWN.
151     */
152    public FolderRole getRole() {
153        return FolderRole.UNKNOWN;
154    }
155
156    /**
157     * This function will be called after the messaging controller has called
158     * getPersonalNamespaces() and has created a matching LocalFolder object.  This can
159     * be used as a trigger for the folder to write back any folder-specific persistent data using
160     * callbacks.
161     *
162     * This is not abstract because most folders do not require this functionality and do not
163     * need to implement it.
164     */
165    @SuppressWarnings("unused")
166    public void localFolderSetupComplete(Folder localFolder) throws MessagingException {
167        // Do nothing - return immediately
168    }
169
170    /**
171     * Create an empty message of the appropriate type for the Folder.
172     */
173    public abstract Message createMessage(String uid) throws MessagingException;
174
175    /**
176     * Callback interface by which a Folder can read and write persistent data.
177     * TODO This needs to be made more generic & flexible
178     */
179    public interface PersistentDataCallbacks {
180
181        /**
182         * Provides keyed storage of strings.  Should be used for per-folder data.  Do not use for
183         * per-message data.
184         * @param key identifier for the data (e.g. "sync.key" or "folder.id")
185         * @param value Data to persist.  All data must be encoded into a string,
186         * so use base64 or some other encoding if necessary.
187         */
188        public void setPersistentString(String key, String value);
189
190        /**
191         * @param key identifier for the data of interest
192         * @return the data saved by the Folder, or defaultValue if never set.
193         */
194        public String getPersistentString(String key, String defaultValue);
195
196        /**
197         * In a single transaction:  Set a key/value pair for the folder, and bulk set or clear
198         * message flags.  Typically used at the beginning or conclusion of a bulk sync operation.
199         *
200         * @param key if non-null, the transaction will set this folder persistent value
201         * @param value the value that will be stored for the key
202         * @param setFlags if non-null, flag(s) will be set for all messages in the folder
203         * @param clearFlags if non-null, flag(s) will be cleared for all messages in the folder
204         */
205        public void setPersistentStringAndMessageFlags(String key, String value,
206                Flag[] setFlags, Flag[] clearFlags) throws MessagingException;
207    }
208
209    /**
210     * Callback interface by which a folder can report UID changes caused by certain operations.
211     */
212    public interface MessageUpdateCallbacks {
213        /**
214         * The operation caused the message's UID to change
215         * @param message The message for which the UID changed
216         * @param newUid The new UID for the message
217         */
218        public void onMessageUidChange(Message message, String newUid) throws MessagingException;
219
220        /**
221         * The operation could not be completed because the message doesn't exist
222         * (for example, it was already deleted from the server side.)
223         * @param message The message that does not exist
224         * @throws MessagingException
225         */
226        public void onMessageNotFound(Message message) throws MessagingException;
227    }
228
229    @Override
230    public String toString() {
231        return getName();
232    }
233}
234