void AutoCollectAddresses(const Message *message, MAction autocollectFlag, bool senderOnly, bool collectNamed, const String& bookName, const String& groupName, wxFrame *frame) { static const MessageAddressType addressTypesToCollect[] = { // In this array, the values corresponding to 'Sender' headers // (e.g. From and ReplyTo) must appear before the others. And if // some other 'sender' headers must be taken into account, the ending // index in the for loop below (named stopAt) must be changed. MAT_REPLYTO, MAT_FROM, MAT_TO, MAT_CC, }; // the email addresses we have already seen wxArrayString addressesSeen; const size_t stopAt = senderOnly ? 2 : WXSIZEOF(addressTypesToCollect); for ( size_t n = 0; n < stopAt; n++ ) { AddressList *addrList = message->GetAddressList(addressTypesToCollect[n]); if ( !addrList ) continue; for ( Address *addr = addrList->GetFirst(); addr; addr = addrList->GetNext(addr) ) { const String email = addr->GetEMail(); if ( addressesSeen.Index(email) == wxNOT_FOUND ) { addressesSeen.Add(email); AutoCollectAddress(email, addr->GetName(), autocollectFlag, collectNamed, bookName, groupName, frame); } } addrList->DecRef(); } }
int InteractivelyCollectAddresses(const wxArrayString& addresses, const String& bookNameOrig, const String& groupNameOrig, wxFrame *parent) { // by default, select all addresses wxArrayInt selections; size_t count = addresses.GetCount(); for ( size_t n = 0; n < count; n++ ) { selections.Add(n); } if ( count > 1 ) { // now ask the user which ones does he really want count = MDialog_GetSelections ( _("Please select the addresses to save"), _("Save addresses"), addresses, &selections, parent, _T("AddrExtract") ); } //else: don't ask the user to choose when there is one address only if ( count > 0 ) { // ask the user for the book and group names to use // // TODO propose something better - ADB tree dialog for example wxString bookName(bookNameOrig), groupName(groupNameOrig); if ( MDialog_GetText2FromUser ( _("Please select the location in the address\n" "book to save the selected entries to:"), _("Save addresses"), _("Address book name:"), &bookName, _("Group name:"), &groupName, parent ) ) { AdbManager_obj manager; CHECK( manager, -1, _T("can't get AdbManager") ); AdbBook_obj autocollectbook(manager->CreateBook(bookName)); if ( !autocollectbook ) { wxLogError(_("Failed to create the address book '%s' " "for autocollected e-mail addresses."), bookName.c_str()); // TODO ask the user for another book name return -1; } AdbEntryGroup_obj group(autocollectbook->CreateGroup(groupName)); if ( !group ) { wxLogError(_("Failed to create group '%s' in the address " "book '%s'."), groupName.c_str(), bookName.c_str()); return -1; } // create all entries in this group size_t saved = 0; for ( size_t n = 0; n < count; n++ ) { AddressList_obj addrList(addresses[selections[n]]); for ( Address *addr = addrList->GetFirst(); addr; addr = addrList->GetNext(addr) ) { String name = addr->GetName(), email = addr->GetEMail(); if ( name.empty() || name == email ) { name = addr->GetMailbox(); } AdbEntry_obj entry(group->CreateEntry(name)); entry->SetField(AdbField_NickName, name); entry->SetField(AdbField_FullName, name); entry->SetField(AdbField_EMail, email); saved++; } } wxLogStatus(parent, _("Saved %u addresses."), saved); } //else: cancelled } else // cancelled by user or nothing selected { wxLogStatus(parent, _("No addresses to save.")); } return count; }