81 #include "pimcommon/widgets/renamefiledialog.h" 
   84 #include <KFileDialog> 
   85 #include <KIO/NetAccess> 
   86 #include <KLocalizedString> 
   87 #include <KMessageBox> 
   88 #include <KTemporaryFile> 
   91 #include <QtCore/QFile> 
   92 #include <QtCore/QMap> 
   93 #include <QtCore/QList> 
   94 #include <QtCore/QTextStream> 
   96 #define GMX_FILESELECTION_STRING QLatin1String("*.gmxa|") + i18n( "GMX address book file (*.gmxa)" ) 
  134     QFile file( fileName );
 
  135     if ( !file.
open( QIODevice::ReadOnly ) ) {
 
  136         QString msg = i18n( 
"<qt>Unable to open <b>%1</b> for reading.</qt>", fileName );
 
  151                     i18n( 
"%1 is not a GMX address book file.", fileName ) );
 
  158     AddresseeMap addresseeMap;
 
  167             if ( itemList.
count() >= 11 ) {
 
  175         KABC::Addressee *addressee = 
new KABC::Addressee;
 
  176         addressee->setNickName( itemList.
at(1) );
 
  177         addressee->setGivenName( itemList.
at(2) );
 
  178         addressee->setFamilyName( itemList.
at(3) );
 
  179         addressee->setFormattedName( itemList.
at(3) + 
QLatin1String(
", ") + itemList.
at(2) );
 
  180         addressee->setPrefix( itemList.
at(4) );
 
  182             addressee->setBirthday( dt );
 
  184         addressee->setNote( itemList.
at(6) );
 
  186             addressee->setRevision( dt );
 
  190         categoriesOfAddressee[ itemList[0] ] = itemList[10];
 
  191         addresseeMap[ itemList[0] ] = addressee;
 
  199         kWarning() << 
"Could not find address records!";
 
  212             if ( itemList.
count() >= 21 ) {
 
  219         KABC::Addressee *addressee = addresseeMap[ itemList[0] ];
 
  222             int recordTypeId = itemList[14].
toInt();
 
  223             KABC::Address::Type addressType;
 
  224             KABC::PhoneNumber::Type phoneType;
 
  225             switch ( recordTypeId ) {
 
  227                 addressType = KABC::Address::Home;
 
  228                 phoneType = KABC::PhoneNumber::Home;
 
  231                 addressType = KABC::Address::Work;
 
  232                 phoneType = KABC::PhoneNumber::Work;
 
  236                 addressType = KABC::Address::Intl;
 
  237                 phoneType = KABC::PhoneNumber::Voice;
 
  240             KABC::Address address = addressee->address( addressType );
 
  241             address.setStreet( itemList[2] );
 
  242             address.setCountry( itemList[3] );
 
  243             address.setPostalCode( itemList[4] );
 
  244             address.setLocality( itemList[5] );
 
  245             if ( !itemList[6].isEmpty() ) {
 
  246                 addressee->insertPhoneNumber(
 
  247                             KABC::PhoneNumber( itemList[6], phoneType ) );
 
  249             if ( !itemList[7].isEmpty() ) {
 
  250                 addressee->insertPhoneNumber(
 
  251                             KABC::PhoneNumber( itemList[7], KABC::PhoneNumber::Fax ) );
 
  253             KABC::PhoneNumber::Type cellType = KABC::PhoneNumber::Cell;
 
  257             if ( !itemList[8].isEmpty() ) {
 
  258                 addressee->insertPhoneNumber( KABC::PhoneNumber( itemList[8], cellType ) );
 
  260             bool preferred = 
false;
 
  261             if ( itemList[19].toInt() & 1 ) {
 
  264             addressee->insertEmail( itemList[10], preferred );
 
  265             if ( !itemList[11].isEmpty() ) {
 
  266                 addressee->setUrl( itemList[11] );
 
  268             if ( !itemList[12].isEmpty() ) {
 
  269                 addressee->setRole( itemList[12] );
 
  274             if ( !itemList[16].isEmpty() ) {
 
  275                 addressee->setOrganization( itemList[16] ); 
 
  277             if ( !itemList[17].isEmpty() ) {
 
  281                 addressee->setRevision( dt ); 
 
  285             addressee->insertAddress( address );
 
  287             kWarning() << 
"unresolved line:" << line;
 
  298         kWarning() << 
"Could not find category records!";
 
  301                 !gmxStream.
atEnd() ) {
 
  305                 if ( itemList.
count() >= 3 ) {
 
  311             usedCategoryList.
append( itemList[1] );
 
  315     KABC::Addressee::List addresseeList;
 
  318     for ( AddresseeMap::Iterator addresseeIt = addresseeMap.begin();
 
  319           addresseeIt != addresseeMap.end(); ++addresseeIt ) {
 
  320         KABC::Addressee *addressee = addresseeIt.value();
 
  323         int categories = categoriesOfAddressee[ addresseeIt.key() ].toInt();
 
  324         for ( 
int i=32; i >= 0; --i ) {
 
  327             if ( catBit > categories ) {
 
  330             if ( catBit & categories  && usedCategoryList.
count() > i ) {
 
  331                 addressee->insertCategory( usedCategoryList[i] );
 
  334         addresseeList.append( *addressee );
 
  347     KUrl url = KFileDialog::getSaveUrl(
 
  349     if ( url.isEmpty() ) {
 
  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() );
 
  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 );
 
  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" );
 
  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";
 
  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();
 
  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 );
 
  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() :
 
  634                            addressee->organization() :
 
  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;
 
void setCodec(QTextCodec *codec)
QString toString(Qt::DateFormat format) const
QString & append(QChar ch)
static const QStringList assignedCategoriesSorted(const KABC::AddresseeList &list)
QString readLine(qint64 maxlen)
ContactList importContacts() const 
Imports a list of contacts. 
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
const T & at(int i) const
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QWidget * parentWidget() const 
Returns the parent widget that can be used as parent for GUI components. 
#define GMX_FILESELECTION_STRING
int indexOf(const T &value, int from) const
int count(const T &value) const
void append(const T &value)
static const QString dateString(const QDateTime &dt)
int toInt(bool *ok, int base) const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
static bool checkDateTime(const QString &dateStr, QDateTime &dt)
void setDate(const QDate &date)
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
The base class for all import/export modules. 
bool exportContacts(const ContactList &list, VCardExportSelectionWidget::ExportFields) const 
Exports the list of contacts. 
QDateTime fromString(const QString &string, Qt::DateFormat format)
bool contains(const T &value) const
QString fromLatin1(const char *str, int size)
GMXXXPort(QWidget *parent=0)