KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > config > subscribe > SynchronizeFolderListCommand


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.gui.config.subscribe;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Arrays JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
27 import javax.swing.tree.DefaultTreeModel JavaDoc;
28 import javax.swing.tree.TreeNode JavaDoc;
29
30 import org.columba.api.command.IWorkerStatusController;
31 import org.columba.core.base.ListTools;
32 import org.columba.core.command.Command;
33 import org.columba.mail.folder.imap.IMAPRootFolder;
34 import org.columba.mail.imap.IImapServer;
35 import org.columba.ristretto.imap.ListInfo;
36 import org.columba.ristretto.imap.Namespace;
37 import org.columba.ristretto.imap.NamespaceCollection;
38 import org.frapuccino.checkabletree.CheckableItemImpl;
39
40 public class SynchronizeFolderListCommand extends Command {
41
42     private IMAPRootFolder root;
43
44     private IImapServer store;
45
46     private TreeNode JavaDoc node;
47
48     /**
49      * @param references
50      */

51     public SynchronizeFolderListCommand(SubscribeCommandReference reference) {
52         super(reference);
53     }
54
55     /*
56      * (non-Javadoc)
57      *
58      * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
59      */

60     public void execute(IWorkerStatusController worker) throws Exception JavaDoc {
61         root = (IMAPRootFolder) ((SubscribeCommandReference) getReference())
62                 .getSourceFolder();
63
64         store = root.getServer();
65
66         node = createTreeStructure();
67     }
68
69     private List JavaDoc fetchUnsubscribedFolders(String JavaDoc reference) throws Exception JavaDoc {
70         NamespaceCollection namespaces;
71
72         // Does the server support the namespace extension?
73
if (store.isSupported("NAMESPACE")) {
74             namespaces = store.fetchNamespaces();
75         } else {
76             // create default namespace
77
namespaces = new NamespaceCollection();
78             namespaces.addPersonalNamespace(new Namespace("", "/"));
79         }
80
81         ArrayList JavaDoc result = new ArrayList JavaDoc();
82         Iterator JavaDoc it;
83
84         // Process personal namespaces
85
if (namespaces.getPersonalNamespaceSize() > 0) {
86             it = namespaces.getPersonalIterator();
87             while (it.hasNext()) {
88                 Namespace pN = (Namespace) it.next();
89
90                 ListInfo[] list = store.list("", pN.getPrefix() + '%');
91                 result.addAll(Arrays.asList(list));
92             }
93         }
94
95         // Process other users namespaces
96
if (namespaces.getOtherUserNamespaceSize() > 0) {
97             it = namespaces.getOtherUserIterator();
98             while (it.hasNext()) {
99                 Namespace pN = (Namespace) it.next();
100
101                 ListInfo[] list = store.list("", pN.getPrefix() + '%');
102                 result.addAll(Arrays.asList(list));
103             }
104         }
105
106         // Process shared namespaces
107
if (namespaces.getSharedNamespaceSize() > 0) {
108             it = namespaces.getSharedIterator();
109             while (it.hasNext()) {
110                 Namespace pN = (Namespace) it.next();
111
112                 ListInfo[] list = store.list("", pN.getPrefix() + '%');
113                 result.addAll(Arrays.asList(list));
114             }
115         }
116
117         for(int i=0; i<result.size(); i++ ) {
118             ListInfo info = (ListInfo) result.get(i);
119             // Handle special case in which INBOX has a NIL delimiter
120
// -> there might exist a pseudo hierarchy under INBOX+delimiter
121
if (info.getName().equalsIgnoreCase("INBOX") && info.getDelimiter() == null) {
122                     result.addAll(Arrays.asList(store.list("", "INBOX"
123                             + store.getDelimiter() + '%')));
124                 break;
125             }
126             
127             // If this folder has children add them
128
// TODO: In the future we should try to fetch additional children on demand
129
// when the tree of the dialog is opened
130
if( info.getParameter(ListInfo.HASCHILDREN)) {
131                 result.addAll(Arrays.asList(store.list("", info.getName()
132                         + store.getDelimiter() + '%')));
133             }
134         }
135
136         return result;
137     }
138
139     private TreeNode JavaDoc createTreeStructure() throws Exception JavaDoc {
140         ListInfo[] lsub = store.fetchSubscribedFolders();
141
142         // Create list of unsubscribed folders
143
List JavaDoc subscribedFolders = new ArrayList JavaDoc(Arrays.asList(lsub));
144         // INBOX is always subscribed
145
subscribedFolders.add(new ListInfo("INBOX", null, 0));
146
147         List JavaDoc unsubscribedFolders = fetchUnsubscribedFolders("");
148         ListTools.substract(unsubscribedFolders, subscribedFolders);
149
150         // Now we have the subscribed folders in subscribedFolders
151
// and the unsubscribed folders in unsubscribedFolders
152
// Next step: Create a treestructure
153
CheckableItemImpl rootNode = new CheckableItemImpl(root.getName());
154
155         Iterator JavaDoc it = unsubscribedFolders.iterator();
156
157         while (it.hasNext()) {
158             ListInfoTreeNode node = insertTreeNode((ListInfo) it.next(),
159                     rootNode);
160             node.setSelected(false);
161         }
162
163         it = subscribedFolders.iterator();
164
165         while (it.hasNext()) {
166             ListInfoTreeNode node = insertTreeNode((ListInfo) it.next(),
167                     rootNode);
168             node.setSelected(true);
169         }
170         return rootNode;
171     }
172
173     private ListInfoTreeNode insertTreeNode(ListInfo listInfo,
174             DefaultMutableTreeNode JavaDoc parent) {
175         // split the hierarchical name with at the delimiters
176
String JavaDoc[] hierarchy = listInfo.getName().split(
177                 "\\" + listInfo.getDelimiter());
178
179         DefaultMutableTreeNode JavaDoc actParent = parent;
180         StringBuffer JavaDoc mailboxName = new StringBuffer JavaDoc();
181
182         mailboxName.append(hierarchy[0]);
183         actParent = ensureChild(hierarchy[0], mailboxName.toString(), actParent);
184
185         for (int i = 1; i < hierarchy.length; i++) {
186             mailboxName.append(listInfo.getDelimiter());
187             mailboxName.append(hierarchy[i]);
188             actParent = ensureChild(hierarchy[i], mailboxName.toString(),
189                     actParent);
190         }
191
192         return (ListInfoTreeNode) actParent;
193     }
194
195     private DefaultMutableTreeNode JavaDoc ensureChild(String JavaDoc name, String JavaDoc mailbox,
196             DefaultMutableTreeNode JavaDoc parent) {
197         Enumeration JavaDoc children = parent.children();
198         ListInfoTreeNode node;
199
200         while (children.hasMoreElements()) {
201             node = (ListInfoTreeNode) children.nextElement();
202
203             if (node.toString().equals(name)) {
204                 return node;
205             }
206         }
207
208         node = new ListInfoTreeNode(name, mailbox);
209         parent.add(node);
210
211         return node;
212     }
213
214     /*
215      * (non-Javadoc)
216      *
217      * @see org.columba.api.command.Command#updateGUI()
218      */

219     public void updateGUI() throws Exception JavaDoc {
220         SubscribeDialog dialog = ((SubscribeCommandReference) getReference())
221                 .getDialog();
222
223         dialog.syncFolderListDone(new DefaultTreeModel JavaDoc(node));
224     }
225 }
Popular Tags