void XXPortManager::slotImport(const QString &identifier, const QString &data) { KAB::XXPort *obj = mXXPortObjects[ identifier ]; if(!obj) { KMessageBox::error(mCore->widget(), i18n("<qt>No import plugin available for <b>%1</b>.</qt>").arg(identifier)); return; } KABC::Resource *resource = mCore->requestResource(mCore->widget()); if(!resource) return; KABC::AddresseeList list = obj->importContacts(data); KABC::AddresseeList::Iterator it; for(it = list.begin(); it != list.end(); ++it) (*it).setResource(resource); if(!list.isEmpty()) { NewCommand *command = new NewCommand(mCore->addressBook(), list); mCore->commandHistory()->addCommand(command); emit modified(); } }
void XXPortManager::slotExport( const QString &identifier ) { if ( !mSelectionModel ) { return; } QPointer<ContactSelectionDialog> dlg = new ContactSelectionDialog( mSelectionModel, mParentWidget ); dlg->setMessageText( i18n( "Which contact do you want to export?" ) ); dlg->setDefaultAddressBook( mDefaultAddressBook ); if ( !dlg->exec() || !dlg ) { delete dlg; return; } const KABC::AddresseeList contacts = dlg->selectedContacts(); delete dlg; if ( contacts.isEmpty() ) { KMessageBox::sorry( 0, i18n( "You have not selected any contacts to export." ) ); return; } const XXPort *xxport = mFactory.createXXPort( identifier, mParentWidget ); if ( !xxport ) { return; } xxport->exportContacts( contacts ); delete xxport; }
KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const { QString fileName; KABC::AddresseeList addrList; KURL::List urls; if ( !XXPortManager::importData.isEmpty() ) addrList = parseVCard( XXPortManager::importData ); else { if ( XXPortManager::importURL.isEmpty() ) urls = KFileDialog::getOpenURLs( QString::null, "*.vcf|vCards", parentWidget(), i18n( "Select vCard to Import" ) ); else urls.append( XXPortManager::importURL ); if ( urls.count() == 0 ) return addrList; QString caption( i18n( "vCard Import Failed" ) ); bool anyFailures = false; KURL::List::Iterator it; for ( it = urls.begin(); it != urls.end(); ++it ) { if ( KIO::NetAccess::download( *it, fileName, parentWidget() ) ) { QFile file( fileName ); if ( file.open( IO_ReadOnly ) ) { QByteArray rawData = file.readAll(); file.close(); if ( rawData.size() > 0 ) addrList += parseVCard( rawData ); KIO::NetAccess::removeTempFile( fileName ); } else { QString text = i18n( "<qt>When trying to read the vCard, there was an error opening the file '%1': %2</qt>" ); text = text.arg( (*it).url() ); text = text.arg( kapp->translate( "QFile", file.errorString().latin1() ) ); KMessageBox::error( parentWidget(), text, caption ); anyFailures = true; } } else { QString text = i18n( "<qt>Unable to access vCard: %1</qt>" ); text = text.arg( KIO::NetAccess::lastErrorString() ); KMessageBox::error( parentWidget(), text, caption ); anyFailures = true; } } if ( !XXPortManager::importURL.isEmpty() ) { // a vcard was passed via cmd if ( addrList.isEmpty() ) { if ( anyFailures && urls.count() > 1 ) KMessageBox::information( parentWidget(), i18n( "No contacts were imported, due to errors with the vCards." ) ); else if ( !anyFailures ) KMessageBox::information( parentWidget(), i18n( "The vCard does not contain any contacts." ) ); } else { VCardViewerDialog dlg( addrList, parentWidget() ); dlg.exec(); addrList = dlg.contacts(); } } } return addrList; }
KABC::AddresseeList VCardXXPort::filterContacts( const KABC::AddresseeList &addrList ) { KABC::AddresseeList list; if ( addrList.isEmpty() ) return addrList; VCardExportSelectionDialog dlg( parentWidget() ); if ( !dlg.exec() ) return list; KABC::AddresseeList::ConstIterator it; for ( it = addrList.begin(); it != addrList.end(); ++it ) { KABC::Addressee addr; addr.setUid( (*it).uid() ); addr.setFormattedName( (*it).formattedName() ); addr.setPrefix( (*it).prefix() ); addr.setGivenName( (*it).givenName() ); addr.setAdditionalName( (*it).additionalName() ); addr.setFamilyName( (*it).familyName() ); addr.setSuffix( (*it).suffix() ); addr.setNickName( (*it).nickName() ); addr.setMailer( (*it).mailer() ); addr.setTimeZone( (*it).timeZone() ); addr.setGeo( (*it).geo() ); addr.setProductId( (*it).productId() ); addr.setSortString( (*it).sortString() ); addr.setUrl( (*it).url() ); addr.setSecrecy( (*it).secrecy() ); addr.setSound( (*it).sound() ); addr.setEmails( (*it).emails() ); addr.setCategories( (*it).categories() ); if ( dlg.exportPrivateFields() ) { addr.setBirthday( (*it).birthday() ); addr.setNote( (*it).note() ); addr.setPhoto( (*it).photo() ); } if ( dlg.exportBusinessFields() ) { addr.setTitle( (*it).title() ); addr.setRole( (*it).role() ); addr.setOrganization( (*it).organization() ); addr.setLogo( (*it).logo() ); KABC::PhoneNumber::List phones = (*it).phoneNumbers( KABC::PhoneNumber::Work ); KABC::PhoneNumber::List::Iterator phoneIt; for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) addr.insertPhoneNumber( *phoneIt ); KABC::Address::List addresses = (*it).addresses( KABC::Address::Work ); KABC::Address::List::Iterator addrIt; for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) addr.insertAddress( *addrIt ); } KABC::PhoneNumber::List phones = (*it).phoneNumbers(); KABC::PhoneNumber::List::Iterator phoneIt; for ( phoneIt = phones.begin(); phoneIt != phones.end(); ++phoneIt ) { int type = (*phoneIt).type(); if ( type & KABC::PhoneNumber::Home && dlg.exportPrivateFields() ) addr.insertPhoneNumber( *phoneIt ); else if ( type & KABC::PhoneNumber::Work && dlg.exportBusinessFields() ) addr.insertPhoneNumber( *phoneIt ); else if ( dlg.exportOtherFields() ) addr.insertPhoneNumber( *phoneIt ); } KABC::Address::List addresses = (*it).addresses(); KABC::Address::List::Iterator addrIt; for ( addrIt = addresses.begin(); addrIt != addresses.end(); ++addrIt ) { int type = (*addrIt).type(); if ( type & KABC::Address::Home && dlg.exportPrivateFields() ) addr.insertAddress( *addrIt ); else if ( type & KABC::Address::Work && dlg.exportBusinessFields() ) addr.insertAddress( *addrIt ); else if ( dlg.exportOtherFields() ) addr.insertAddress( *addrIt ); } if ( dlg.exportOtherFields() ) addr.setCustoms( (*it).customs() ); if ( dlg.exportEncryptionKeys() ) { addKey( addr, KABC::Key::PGP ); addKey( addr, KABC::Key::X509 ); } list.append( addr ); } return list; }
bool VCardXXPort::exportContacts( const KABC::AddresseeList &addrList, const QString &data ) { KABC::VCardConverter converter; KURL url; KABC::AddresseeList list; list = filterContacts( addrList ); bool ok = true; if ( list.isEmpty() ) { return ok; } else if ( list.count() == 1 ) { url = KFileDialog::getSaveURL( list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf" ); if ( url.isEmpty() ) return true; if ( data == "v21" ) ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) ); else ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) ); } else { QString msg = i18n( "You have selected a list of contacts, shall they be " "exported to several files?" ); switch ( KMessageBox::questionYesNo( parentWidget(), msg, QString::null, i18n("Export to Several Files"), i18n("Export to One File") ) ) { case KMessageBox::Yes: { KURL baseUrl = KFileDialog::getExistingURL(); if ( baseUrl.isEmpty() ) return true; KABC::AddresseeList::ConstIterator it; uint counter = 0; for ( it = list.begin(); it != list.end(); ++it ) { QString testUrl; if ( (*it).givenName().isEmpty() && (*it).familyName().isEmpty() ) testUrl = baseUrl.url() + "/" + (*it).organization(); else testUrl = baseUrl.url() + "/" + (*it).givenName() + "_" + (*it).familyName(); if ( KIO::NetAccess::exists( testUrl + (counter == 0 ? "" : QString::number( counter )) + ".vcf", false, parentWidget() ) ) { counter++; url = testUrl + QString::number( counter ) + ".vcf"; } else url = testUrl + ".vcf"; bool tmpOk; KABC::AddresseeList tmpList; tmpList.append( *it ); if ( data == "v21" ) tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v2_1 ) ); else tmpOk = doExport( url, converter.createVCards( tmpList, KABC::VCardConverter::v3_0 ) ); ok = ok && tmpOk; } break; } case KMessageBox::No: default: { url = KFileDialog::getSaveURL( "addressbook.vcf" ); if ( url.isEmpty() ) return true; if ( data == "v21" ) ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v2_1 ) ); else ok = doExport( url, converter.createVCards( list, KABC::VCardConverter::v3_0 ) ); } } } return ok; }