001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.mail.folder.command;
019:
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.StringTokenizer;
024: import java.util.Vector;
025:
026: import org.columba.addressbook.facade.IContactFacade;
027: import org.columba.addressbook.facade.IContactItem;
028: import org.columba.addressbook.facade.IModelFacade;
029: import org.columba.api.command.ICommandReference;
030: import org.columba.api.command.IWorkerStatusController;
031: import org.columba.api.exception.ServiceNotFoundException;
032: import org.columba.api.exception.StoreException;
033: import org.columba.core.command.Command;
034: import org.columba.core.command.StatusObservableImpl;
035: import org.columba.core.folder.api.IFolderCommandReference;
036: import org.columba.mail.connector.FacadeUtil;
037: import org.columba.mail.connector.ServiceConnector;
038: import org.columba.mail.folder.IMailbox;
039: import org.columba.ristretto.message.Address;
040: import org.columba.ristretto.message.Header;
041: import org.columba.ristretto.parser.ParserException;
042:
043: /**
044: * Add all senders contained in the selected messages to the addressbook.
045: * <p>
046: * A dialog asks the user to choose the destination addressbook.
047: *
048: * @author fdietz
049: */
050: public class AddAllSendersToAddressbookCommand extends Command {
051:
052: /**
053: * Constructor for AddAllSendersToAddressbookCommand.
054: *
055: * @param references
056: */
057: public AddAllSendersToAddressbookCommand(ICommandReference reference) {
058: super (reference);
059: }
060:
061: /**
062: * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
063: */
064: public void execute(IWorkerStatusController worker)
065: throws Exception {
066: // get reference
067: IFolderCommandReference r = (IFolderCommandReference) getReference();
068:
069: // selected messages
070: Object[] uids = r.getUids();
071:
072: // selected folder
073: IMailbox folder = (IMailbox) r.getSourceFolder();
074:
075: // register for status events
076: ((StatusObservableImpl) folder.getObservable())
077: .setWorker(worker);
078:
079: IContactFacade contactFacade = null;
080: IModelFacade modelFacade = null;
081: try {
082: contactFacade = ServiceConnector.getContactFacade();
083: modelFacade = ServiceConnector.getModelFacade();
084: } catch (ServiceNotFoundException e) {
085: e.printStackTrace();
086: return;
087: }
088:
089: List<String> addresses = new ArrayList<String>();
090:
091: // for every message
092: for (int i = 0; i < uids.length; i++) {
093: // get header of message
094: Header header = folder.getHeaderFields(uids[i],
095: new String[] { "From", "To", "Cc", "Bcc" });
096:
097: String addrStr = (String) header.get("From");
098: addresses.addAll(parseAddrStr(addrStr));
099:
100: addrStr = (String) header.get("To");
101: addresses.addAll(parseAddrStr(addrStr));
102:
103: addrStr = (String) header.get("Cc");
104: addresses.addAll(parseAddrStr(addrStr));
105:
106: addrStr = (String) header.get("Bcc");
107: addresses.addAll(parseAddrStr(addrStr));
108:
109: }
110:
111: // add sender to addressbook
112: Iterator<String> it = addresses.listIterator();
113: List<IContactItem> contactItems = new ArrayList<IContactItem>();
114: while (it.hasNext()) {
115: try {
116: String addrStr = it.next();
117: if (addrStr == null)
118: continue;
119: Address address = Address.parse(addrStr);
120:
121: // add contact to addressbook
122: IContactItem contactItem = modelFacade
123: .createContactItem();
124: FacadeUtil.getInstance().initContactItem(contactItem,
125: address.getDisplayName(),
126: address.getMailAddress());
127: contactItems.add(contactItem);
128: } catch (ParserException e) {
129: e.printStackTrace();
130: } catch (StoreException e) {
131: e.printStackTrace();
132: }
133: }
134: contactFacade.addContacts(contactItems
135: .toArray(new IContactItem[contactItems.size()]));
136: }
137:
138: /**
139: * Parse an address string containing multiple comma-separated mail addresses
140: * @param addrStr The comma-separated address string.
141: * @return List containing individual address strings
142: */
143: private List<String> parseAddrStr(String addrStr) {
144: List<String> addresses = new ArrayList<String>();
145: if (addrStr == null)
146: return addresses;
147: StringTokenizer st = new StringTokenizer(addrStr, ",");
148: while (st.hasMoreTokens()) {
149: String addr = st.nextToken();
150: addresses.add(addr);
151: }
152: return addresses;
153: }
154:
155: }
|