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.Iterator;
021: import java.util.Vector;
022:
023: import org.columba.addressbook.facade.IContactFacade;
024: import org.columba.addressbook.facade.IContactItem;
025: import org.columba.addressbook.facade.IModelFacade;
026: import org.columba.api.command.ICommandReference;
027: import org.columba.api.command.IWorkerStatusController;
028: import org.columba.api.exception.ServiceNotFoundException;
029: import org.columba.api.exception.StoreException;
030: import org.columba.core.command.Command;
031: import org.columba.core.command.StatusObservableImpl;
032: import org.columba.mail.command.IMailFolderCommandReference;
033: import org.columba.mail.connector.FacadeUtil;
034: import org.columba.mail.connector.ServiceConnector;
035: import org.columba.mail.folder.IMailbox;
036: import org.columba.ristretto.message.Address;
037: import org.columba.ristretto.message.Header;
038: import org.columba.ristretto.parser.ParserException;
039:
040: /**
041: * Add sender of the selected messages to addressbook.
042: * <p>
043: * A dialog asks the user the destination addressbook.
044: *
045: * @author fdietz
046: */
047: public class AddSenderToAddressbookCommand extends Command {
048:
049: /**
050: * Constructor for AddSenderToAddressbookCommand.
051: *
052: * @param references
053: */
054: public AddSenderToAddressbookCommand(ICommandReference reference) {
055: super (reference);
056: }
057:
058: /**
059: * @see org.columba.api.command.Command#execute(org.columba.api.command.Worker)
060: */
061: public void execute(IWorkerStatusController worker)
062: throws Exception {
063: // get reference
064: IMailFolderCommandReference r = (IMailFolderCommandReference) getReference();
065:
066: // get array of message UIDs
067: Object[] uids = r.getUids();
068:
069: // get source folder
070: IMailbox folder = (IMailbox) r.getSourceFolder();
071:
072: // register for status events
073: ((StatusObservableImpl) folder.getObservable())
074: .setWorker(worker);
075:
076: IContactFacade contactFacade = null;
077: IModelFacade modelFacade = null;
078: try {
079: contactFacade = ServiceConnector.getContactFacade();
080: modelFacade = ServiceConnector.getModelFacade();
081: } catch (ServiceNotFoundException e) {
082: e.printStackTrace();
083: return;
084: }
085:
086: Vector<String> v = new Vector<String>();
087: // for each message
088: for (int i = 0; i < uids.length; i++) {
089: // get header of message
090: Header header = folder.getHeaderFields(uids[i],
091: new String[] { "From" });
092:
093: // get sender
094: String sender = (String) header.get("From");
095:
096: v.add(sender);
097: }
098:
099: // add sender to addressbook
100: Iterator<String> it = v.listIterator();
101: while (it.hasNext()) {
102: try {
103: Address address = Address.parse(it.next());
104:
105: // add contact to addressbook
106: IContactItem contactItem = modelFacade
107: .createContactItem();
108: FacadeUtil.getInstance().initContactItem(contactItem,
109: address.getDisplayName(),
110: address.getMailAddress());
111: contactFacade.addContact(null);
112: } catch (ParserException e) {
113: e.printStackTrace();
114: } catch (StoreException e) {
115: e.printStackTrace();
116: }
117: }
118: }
119:
120: }
|