KABC::AddresseeList XXPortSelectDialog::contacts() { const QStringList selection = mCore->selectedUIDs(); KABC::AddresseeList list; if ( mUseSelection->isChecked() ) { QStringList::ConstIterator it; for ( it = selection.constBegin(); it != selection.constEnd(); ++it ) { KABC::Addressee addr = mCore->addressBook()->findByUid( *it ); if ( !addr.isEmpty() ) list.append( addr ); } } else if ( mUseFilters->isChecked() ) { // find contacts that can pass selected filter Filter::List::ConstIterator filterIt; for ( filterIt = mFilters.constBegin(); filterIt != mFilters.constEnd(); ++filterIt ) if ( (*filterIt).name() == mFiltersCombo->currentText() ) break; KABC::AddressBook::Iterator it; for ( it = mCore->addressBook()->begin(); it != mCore->addressBook()->end(); ++it ) { if ( (*filterIt).filterAddressee( *it ) ) list.append( *it ); } } else if ( mUseCategories->isChecked() ) { const QStringList categorieList = categories(); KABC::AddressBook::ConstIterator it; KABC::AddressBook::ConstIterator addressBookEnd( mCore->addressBook()->constEnd() ); for ( it = mCore->addressBook()->constBegin(); it != addressBookEnd; ++it ) { const QStringList tmp( (*it).categories() ); QStringList::ConstIterator tmpIt; for ( tmpIt = tmp.constBegin(); tmpIt != tmp.constEnd(); ++tmpIt ) if ( categorieList.contains( *tmpIt ) ) { list.append( *it ); break; } } } else { // create a string list of all entries: KABC::AddressBook::ConstIterator it; for ( it = mCore->addressBook()->constBegin(); it != mCore->addressBook()->constEnd(); ++it ) list.append( *it ); } if ( mUseSorting ) { list.setReverseSorting( mSortTypeCombo->currentIndex() == 1 ); int pos = mFieldCombo->currentIndex(); if ( pos < mFields.count() ) list.sortByField( mFields[ pos ] ); } return list; }
KABC::AddresseeList GMXXXPort::importContacts( const QString& ) const { KABC::AddresseeList addrList; QString fileName = KFileDialog::getOpenFileName( ":xxport_gmx", GMX_FILESELECTION_STRING, 0 ); if ( fileName.isEmpty() ) return addrList; QFile file( fileName ); if ( !file.open( IO_ReadOnly ) ) { QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" ); KMessageBox::error( parentWidget(), msg.arg( fileName ) ); return addrList; } QDateTime dt; QTextStream gmxStream( &file ); gmxStream.setEncoding( QTextStream::Latin1 ); QString line, line2; line = gmxStream.readLine(); line2 = gmxStream.readLine(); if (!line.startsWith("AB_ADDRESSES:") || !line2.startsWith("Address_id")) { KMessageBox::error( parentWidget(), i18n("%1 is not a GMX address book file.").arg(fileName) ); return addrList; } QStringList strList; typedef QMap<QString, KABC::Addressee *> AddressMap; AddressMap addrMap; // "Address_id,Nickname,Firstname,Lastname,Title,Birthday,Comments,Change_date,Status,Address_link_id,Categories" line = gmxStream.readLine(); while (!line.startsWith("####") && !gmxStream.atEnd()) { while (1) { strList = QStringList::split('#', line, true); if (strList.count() >= 11) break; line.append('\n'); line.append(gmxStream.readLine()); }; KABC::Addressee *addr = new KABC::Addressee; addr->setNickName(strList[1]); addr->setGivenName(strList[2]); addr->setFamilyName(strList[3]); addr->setTitle(strList[4]); if (addr->formattedName().isEmpty()) addr->setFormattedName(addr->realName()); if (checkDateTime(strList[5],dt)) addr->setBirthday(dt); addr->setNote(strList[6]); if (checkDateTime(strList[7],dt)) addr->setRevision(dt); // addr->setStatus(strList[8]); Status // addr->xxx(strList[9]); Address_link_id // addr->setCategory(strList[10]); Categories addrMap[strList[0]] = addr; line = gmxStream.readLine(); } // now read the address records line = gmxStream.readLine(); if (!line.startsWith("AB_ADDRESS_RECORDS:")) { kdWarning() << "Could not find address records!\n"; return addrList; } // Address_id,Record_id,Street,Country,Zipcode,City,Phone,Fax,Mobile,Mobile_type,Email, // Homepage,Position,Comments,Record_type_id,Record_type,Company,Department,Change_date,Preferred,Status line = gmxStream.readLine(); line = gmxStream.readLine(); while (!line.startsWith("####") && !gmxStream.atEnd()) { while (1) { strList = QStringList::split('#', line, true); if (strList.count() >= 21) break; line.append('\n'); line.append(gmxStream.readLine()); }; KABC::Addressee *addr = addrMap[strList[0]]; if (addr) { for ( QStringList::Iterator it = strList.begin(); it != strList.end(); ++it ) *it = (*it).simplifyWhiteSpace(); // strList[1] = Record_id (numbered item, ignore here) int id = strList[14].toInt(); // Record_type_id (0=work,1=home,2=other) int type = (id==0) ? KABC::Address::Work : KABC::Address::Home; if (!strList[19].isEmpty() && strList[19].toInt()!=0) type |= KABC::Address::Pref; // Preferred address (seems to be bitfield for telephone Prefs) KABC::Address adr = addr->address(type); adr.setStreet(strList[2]); adr.setCountry(strList[3]); adr.setPostalCode(strList[4]); adr.setLocality(strList[5]); addr->insertPhoneNumber( KABC::PhoneNumber(strList[6], KABC::PhoneNumber::Home) ); addr->insertPhoneNumber( KABC::PhoneNumber(strList[7], KABC::PhoneNumber::Fax) ); int celltype = KABC::PhoneNumber::Cell; // strList[9]=Mobile_type // always 0 or -1(default phone). if (strList[9].toInt()) celltype |= KABC::PhoneNumber::Pref; addr->insertPhoneNumber( KABC::PhoneNumber(strList[8], celltype) ); addr->insertEmail(strList[10]); if (!strList[11].isEmpty()) addr->setUrl(strList[11]); if (!strList[12].isEmpty()) addr->setRole(strList[12]); // strList[13]=Comments // strList[14]=Record_type_id (0,1,2) - see above // strList[15]=Record_type (name of this additional record entry) if (!strList[16].isEmpty()) addr->setOrganization(strList[16]); // Company if (!strList[17].isEmpty()) addr->insertCustom( "KADDRESSBOOK", "X-Department", strList[17]); // Department if (checkDateTime(strList[18],dt)) addr->setRevision(dt); // Change_date // strList[19]=Preferred (see above) // strList[20]=Status (should always be "1") addr->insertAddress(adr); } else { kdWarning() << "unresolved line: " << line << endl; } line = gmxStream.readLine(); } // now add the addresses to to addrList for ( AddressMap::Iterator it = addrMap.begin(); it != addrMap.end(); ++it ) { KABC::Addressee *addr = it.data(); addrList.append(*addr); delete addr; } file.close(); return addrList; }
KABC::AddresseeList OperaXXPort::importContacts(const QString &) const { KABC::AddresseeList addrList; QString fileName = KFileDialog::getOpenFileName(QDir::homeDirPath() + QString::fromLatin1("/.opera/contacts.adr")); if(fileName.isEmpty()) return addrList; QFile file(fileName); if(!file.open(IO_ReadOnly)) { QString msg = i18n("<qt>Unable to open <b>%1</b> for reading.</qt>"); KMessageBox::error(parentWidget(), msg.arg(fileName)); return addrList; } QTextStream stream(&file); stream.setEncoding(QTextStream::UnicodeUTF8); QString line, key, value; bool parseContact = false; KABC::Addressee addr; QRegExp separator("\x02\x02"); while(!stream.atEnd()) { line = stream.readLine(); line = line.stripWhiteSpace(); if(line == QString::fromLatin1("#CONTACT")) { parseContact = true; addr = KABC::Addressee(); continue; } else if(line.isEmpty()) { parseContact = false; if(!addr.isEmpty()) { addrList.append(addr); addr = KABC::Addressee(); } continue; } if(parseContact == true) { int sep = line.find('='); key = line.left(sep).lower(); value = line.mid(sep + 1); if(key == QString::fromLatin1("name")) addr.setNameFromString(value); else if(key == QString::fromLatin1("mail")) { QStringList emails = QStringList::split(separator, value); QStringList::Iterator it = emails.begin(); bool preferred = true; for(; it != emails.end(); ++it) { addr.insertEmail(*it, preferred); preferred = false; } } else if(key == QString::fromLatin1("phone")) addr.insertPhoneNumber(KABC::PhoneNumber(value)); else if(key == QString::fromLatin1("fax")) addr.insertPhoneNumber(KABC::PhoneNumber(value, KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home)); else if(key == QString::fromLatin1("postaladdress")) { KABC::Address address(KABC::Address::Home); address.setLabel(value.replace(separator, "\n")); addr.insertAddress(address); } else if(key == QString::fromLatin1("description")) addr.setNote(value.replace(separator, "\n")); else if(key == QString::fromLatin1("url")) addr.setUrl(KURL(value)); else if(key == QString::fromLatin1("pictureurl")) { KABC::Picture pic(value); addr.setPhoto(pic); } } } file.close(); 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; }