81 #include "pimcommon/widgets/renamefiledialog.h"
85 #include <KFileDialog>
86 #include <KIO/NetAccess>
88 #include <KMessageBox>
89 #include <KTemporaryFile>
92 #include <QtCore/QFile>
93 #include <QtCore/QMap>
94 #include <QtCore/QList>
95 #include <QtCore/QTextStream>
97 #define GMX_FILESELECTION_STRING QLatin1String("*.gmxa|") + i18n( "GMX address book file (*.gmxa)" )
110 if ( dateStr.isEmpty() ) {
114 dt = QDateTime::fromString( dateStr, Qt::ISODate );
115 if ( dt.isValid() && dt.date().year() > 1901 ) {
118 dt.setDate( QDate() );
127 KABC::Addressee::List addresseeList;
132 if ( fileName.isEmpty() ) {
133 return addresseeList;
136 QFile file( fileName );
137 if ( !file.open( QIODevice::ReadOnly ) ) {
138 QString msg = i18n(
"<qt>Unable to open <b>%1</b> for reading.</qt>", fileName );
140 return addresseeList;
144 QTextStream gmxStream( &file );
145 gmxStream.setCodec(
"ISO 8859-1" );
147 line = gmxStream.readLine();
148 line2 = gmxStream.readLine();
149 if ( !line.startsWith( QLatin1String(
"AB_ADDRESSES:" ) ) ||
150 !line2.startsWith( QLatin1String(
"Address_id" ) ) ) {
153 i18n(
"%1 is not a GMX address book file.", fileName ) );
154 return addresseeList;
157 QStringList itemList;
158 QMap<QString, QString> categoriesOfAddressee;
159 typedef QMap<QString, KABC::Addressee *> AddresseeMap;
160 AddresseeMap addresseeMap;
164 line = gmxStream.readLine();
165 while ( ( line != QLatin1String(
"####" ) ) && !gmxStream.atEnd() ) {
168 itemList = line.split( QLatin1Char(
'#'), QString::KeepEmptyParts );
169 if ( itemList.count() >= 11 ) {
172 line.append( QLatin1Char(
'\n') );
173 line.append( gmxStream.readLine() );
177 KABC::Addressee *addressee =
new KABC::Addressee;
178 addressee->setNickName( itemList.at(1) );
179 addressee->setGivenName( itemList.at(2) );
180 addressee->setFamilyName( itemList.at(3) );
181 addressee->setFormattedName( itemList.at(3) + QLatin1String(
", ") + itemList.at(2) );
182 addressee->setPrefix( itemList.at(4) );
184 addressee->setBirthday( dt );
186 addressee->setNote( itemList.at(6) );
188 addressee->setRevision( dt );
192 categoriesOfAddressee[ itemList[0] ] = itemList[10];
193 addresseeMap[ itemList[0] ] = addressee;
195 line = gmxStream.readLine();
199 line = gmxStream.readLine();
200 if ( !line.startsWith( QLatin1String(
"AB_ADDRESS_RECORDS:" ) ) ) {
201 kWarning() <<
"Could not find address records!";
202 return addresseeList;
207 line = gmxStream.readLine();
208 line = gmxStream.readLine();
210 while ( !line.startsWith( QLatin1String(
"####" ) ) && !gmxStream.atEnd() ) {
213 itemList = line.split( QLatin1Char(
'#'), QString::KeepEmptyParts );
214 if ( itemList.count() >= 21 ) {
217 line.append( QLatin1Char(
'\n') );
218 line.append( gmxStream.readLine() );
221 KABC::Addressee *addressee = addresseeMap[ itemList[0] ];
224 int recordTypeId = itemList[14].toInt();
225 KABC::Address::Type addressType;
226 KABC::PhoneNumber::Type phoneType;
227 switch ( recordTypeId ) {
229 addressType = KABC::Address::Home;
230 phoneType = KABC::PhoneNumber::Home;
233 addressType = KABC::Address::Work;
234 phoneType = KABC::PhoneNumber::Work;
238 addressType = KABC::Address::Intl;
239 phoneType = KABC::PhoneNumber::Voice;
242 KABC::Address address = addressee->address( addressType );
243 address.setStreet( itemList[2] );
244 address.setCountry( itemList[3] );
245 address.setPostalCode( itemList[4] );
246 address.setLocality( itemList[5] );
247 if ( !itemList[6].isEmpty() ) {
248 addressee->insertPhoneNumber(
249 KABC::PhoneNumber( itemList[6], phoneType ) );
251 if ( !itemList[7].isEmpty() ) {
252 addressee->insertPhoneNumber(
253 KABC::PhoneNumber( itemList[7], KABC::PhoneNumber::Fax ) );
255 KABC::PhoneNumber::Type cellType = KABC::PhoneNumber::Cell;
259 if ( !itemList[8].isEmpty() ) {
260 addressee->insertPhoneNumber( KABC::PhoneNumber( itemList[8], cellType ) );
262 bool preferred =
false;
263 if ( itemList[19].toInt() & 1 ) {
266 addressee->insertEmail( itemList[10], preferred );
267 if ( !itemList[11].isEmpty() ) {
268 addressee->setUrl( itemList[11] );
270 if ( !itemList[12].isEmpty() ) {
271 addressee->setRole( itemList[12] );
276 if ( !itemList[16].isEmpty() ) {
277 addressee->setOrganization( itemList[16] );
279 if ( !itemList[17].isEmpty() ) {
280 addressee->insertCustom( QLatin1String(
"KADDRESSBOOK"), QLatin1String(
"X-Department"), itemList[17] );
283 addressee->setRevision( dt );
287 addressee->insertAddress( address );
289 kWarning() <<
"unresolved line:" << line;
291 line = gmxStream.readLine();
295 QStringList usedCategoryList;
296 line = gmxStream.readLine();
297 line2 = gmxStream.readLine();
298 if ( !line.startsWith( QLatin1String(
"AB_CATEGORIES:" ) ) ||
299 !line2.startsWith( QLatin1String(
"Category_id" ) ) ) {
300 kWarning() <<
"Could not find category records!";
302 while ( !line.startsWith( QLatin1String(
"####" ) ) &&
303 !gmxStream.atEnd() ) {
306 itemList = line.split( QLatin1Char(
'#'), QString::KeepEmptyParts );
307 if ( itemList.count() >= 3 ) {
310 line.append( QLatin1Char(
'\n') );
311 line.append( gmxStream.readLine() );
313 usedCategoryList.append( itemList[1] );
314 line = gmxStream.readLine();
319 for ( AddresseeMap::Iterator addresseeIt = addresseeMap.begin();
320 addresseeIt != addresseeMap.end(); ++addresseeIt ) {
321 KABC::Addressee *addressee = addresseeIt.value();
324 int categories = categoriesOfAddressee[ addresseeIt.key() ].toInt();
325 for (
int i=32; i >= 0; --i ) {
328 if ( catBit > categories ) {
331 if ( catBit & categories && usedCategoryList.count() > i ) {
332 addressee->insertCategory( usedCategoryList[i] );
335 addresseeList.append( *addressee );
340 return addresseeList;
347 KUrl url = KFileDialog::getSaveUrl(
349 if ( url.isEmpty() ) {
353 if ( QFileInfo( url.isLocalFile() ?
354 url.toLocalFile() : url.path() ).exists() ) {
355 if ( url.isLocalFile() && QFileInfo( url.toLocalFile() ).exists() ) {
356 PimCommon::RenameFileDialog::RenameFileDialogResult result = PimCommon::RenameFileDialog::RENAMEFILE_IGNORE;
357 PimCommon::RenameFileDialog *dialog =
new PimCommon::RenameFileDialog(url,
false,
parentWidget());
358 result =
static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult
>(dialog->exec());
359 if ( result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME ) {
360 url = dialog->newName();
361 }
else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
369 if ( !url.isLocalFile() ) {
370 KTemporaryFile tmpFile;
371 if ( !tmpFile.open() ) {
372 QString txt = i18n(
"<qt>Unable to open file <b>%1</b></qt>", url.url() );
377 doExport( &tmpFile, list );
380 return KIO::NetAccess::upload( tmpFile.fileName(), url,
parentWidget() );
382 QString fileName = url.toLocalFile();
383 QFile file( fileName );
385 if ( !file.open( QIODevice::WriteOnly ) ) {
386 QString txt = i18n(
"<qt>Unable to open file <b>%1</b>.</qt>", fileName );
391 doExport( &file, list );
400 if ( !dt.isValid() ) {
401 return QString::fromLatin1(
"1000-01-01 00:00:00" );
403 QString d( dt.toString( Qt::ISODate ) );
412 QStringList categoryList;
413 const KABC::Addressee *addressee;
414 for ( KABC::AddresseeList::ConstIterator addresseeIt = list.begin();
415 addresseeIt != list.end() && categoryList.count() < 32; ++addresseeIt ) {
416 addressee = &( *addresseeIt );
417 if ( addressee->isEmpty() )
continue;
418 const QStringList categories = addressee->categories();
419 for (
int i=0; i < categories.count() && categoryList.count() < 32; ++i ) {
420 if ( !categoryList.contains( categories[i]) ) {
421 categoryList.append( categories[i] );
429 void GMXXXPort::doExport( QFile *fp,
const KABC::AddresseeList &list )
const
431 if ( !fp || !list.count() ) {
436 t.setCodec(
"ISO 8859-1" );
438 typedef QMap<int, const KABC::Addressee *> AddresseeMap;
439 AddresseeMap addresseeMap;
440 const KABC::Addressee *addressee;
442 t <<
"AB_ADDRESSES:\n";
443 t <<
"Address_id,Nickname,Firstname,Lastname,Title,Birthday,Comments,"
444 "Change_date,Status,Address_link_id,Categories\n";
446 QList<QString> categoryMap;
450 const QChar DELIM( QLatin1Char(
'#') );
451 for ( KABC::AddresseeList::ConstIterator it = list.begin();
452 it != list.end(); ++it ) {
454 if ( addressee->isEmpty() ) {
457 addresseeMap[ ++addresseeId ] = addressee;
460 const QStringList categories = addressee->categories();
461 long int category = 0;
462 if ( categories.count() > 0 ) {
463 for (
int i=0; i < categories.count(); ++i ) {
464 if ( categoryMap.contains( categories[i] ) ) {
465 category |= 1 << categoryMap.indexOf( categories[i], 0 ) ;
471 QString nickName = addressee->nickName();
472 if ( nickName.isEmpty() ) {
473 nickName = addressee->formattedName();
476 t << addresseeId << DELIM
478 << addressee->givenName() << DELIM
479 << addressee->familyName() << DELIM
480 << addressee->prefix() << DELIM
482 <<
dateString( addressee->birthday() ) << DELIM
483 << addressee->note() << DELIM
484 <<
dateString( addressee->revision() ) << DELIM
491 t <<
"AB_ADDRESS_RECORDS:\n";
492 t <<
"Address_id,Record_id,Street,Country,Zipcode,City,Phone,Fax,Mobile,"
493 "Mobile_type,Email,Homepage,Position,Comments,Record_type_id,Record_type,"
494 "Company,Department,Change_date,Preferred,Status\n";
497 while ( ( addressee = addresseeMap[ addresseeId ] ) != 0 ) {
499 const KABC::PhoneNumber::List cellPhones =
500 addressee->phoneNumbers( KABC::PhoneNumber::Cell );
502 const QStringList emails = addressee->emails();
504 for (
int recId=0; recId<3; ++recId ) {
505 KABC::Address address;
506 KABC::PhoneNumber phone, fax, cell;
516 address = addressee->address( KABC::Address::Home );
517 phone = addressee->phoneNumber( KABC::PhoneNumber::Home );
518 if ( cellPhones.count() > 0 ) {
519 cell = cellPhones.at( 0 );
520 if ( !cell.isEmpty() ) {
526 address = addressee->address( KABC::Address::Work );
527 phone = addressee->phoneNumber( KABC::PhoneNumber::Work );
528 if ( cellPhones.count() >= 2 ) {
529 cell = cellPhones.at( 1 );
531 fax = addressee->phoneNumber( KABC::PhoneNumber::Fax );
535 if ( addressee->addresses( KABC::Address::Home ).count() > 1 ) {
536 address = addressee->addresses( KABC::Address::Home ).at( 1 );
538 if ( ( address.isEmpty() ) &&
539 ( addressee->addresses( KABC::Address::Work ).count() > 1 ) ) {
540 address = addressee->addresses( KABC::Address::Work ).at( 1 );
542 if ( address.isEmpty() ) {
543 address = addressee->address( KABC::Address::Dom );
545 if ( address.isEmpty() ) {
546 address = addressee->address( KABC::Address::Intl );
548 if ( address.isEmpty() ) {
549 address = addressee->address( KABC::Address::Postal );
551 if ( address.isEmpty() ) {
552 address = addressee->address( KABC::Address::Parcel );
555 if ( addressee->phoneNumbers( KABC::PhoneNumber::Home ).count() > 1 ) {
556 phone = addressee->phoneNumbers( KABC::PhoneNumber::Home ).at( 1 );
558 if ( ( phone.isEmpty() ) && ( addressee->phoneNumbers(
559 KABC::PhoneNumber::Work ).count() > 1 ) )
560 phone = addressee->phoneNumbers( KABC::PhoneNumber::Work ).at( 1 );
561 if ( phone.isEmpty() ) {
562 phone = addressee->phoneNumber( KABC::PhoneNumber::Voice );
564 if ( phone.isEmpty() ) {
565 phone = addressee->phoneNumber( KABC::PhoneNumber::Msg );
567 if ( phone.isEmpty() ) {
568 phone = addressee->phoneNumber( KABC::PhoneNumber::Isdn );
570 if ( phone.isEmpty() ) {
571 phone = addressee->phoneNumber( KABC::PhoneNumber::Car );
573 if ( phone.isEmpty() ) {
574 phone = addressee->phoneNumber( KABC::PhoneNumber::Pager );
577 switch ( cellPhones.count() ) {
582 if ( !address.isEmpty() ) {
583 cell = cellPhones.at( 0 );
587 cell = cellPhones.at( 2 );
594 if ( emails.count()>recId ) {
595 email = emails[ recId ];
596 if ( email == addressee->preferredEmail() ) {
601 if ( !address.isEmpty() || !phone.isEmpty() ||
602 !cell.isEmpty() || !email.isEmpty() ) {
603 t << addresseeId << DELIM
605 << address.street() << DELIM
606 << address.country() << DELIM
607 << address.postalCode() << DELIM
608 << address.locality() << DELIM
609 << phone.number() << DELIM
610 << fax.number() << DELIM
611 << cell.number() << DELIM
613 << ( ( recId ==
typeWork ) ? 0 : 1 ) << DELIM
618 addressee->url().url() :
626 addressee->custom( QLatin1String(
"KADDRESSBOOK"), QLatin1String(
"X-SpousesName") ) :
634 addressee->organization() :
638 addressee->custom( QLatin1String(
"KADDRESSBOOK"), QLatin1String(
"X-Department") ) :
641 <<
dateString( addressee->revision() ) << DELIM
654 t <<
"AB_CATEGORIES:" << endl;
655 t <<
"Category_id,Name,Icon_id" << endl;
660 for (
int i = 0; i < categoryMap.size(); ++i ) {
661 t << ( i + 1 ) << DELIM << categoryMap.at( i ) << DELIM << 0 << endl;
static const QStringList assignedCategoriesSorted(const KABC::AddresseeList &list)
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
#define GMX_FILESELECTION_STRING
static const QString dateString(const QDateTime &dt)
static bool checkDateTime(const QString &dateStr, QDateTime &dt)
bool exportContacts(const KABC::AddresseeList &list) const
The base class for all import/export modules.
GMXXXPort(QWidget *parent=0)
KABC::Addressee::List importContacts() const
Imports a list of contacts.