TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.2KB

  1. /*
  2. This file is part of KAddressbook.
  3. Copyright (c) 2003 - 2003 Daniel Molkentin <molkentin@kde.org>
  4. Tobias Koenig <tokoe@kde.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. As a special exception, permission is given to link this program
  17. with any edition of TQt, and distribute the resulting executable,
  18. without including the source code for TQt in the source distribution.
  19. */
  20. #include <tqfile.h>
  21. #include <tqregexp.h>
  22. #include <kfiledialog.h>
  23. #include <kio/netaccess.h>
  24. #include <klocale.h>
  25. #include <kmessagebox.h>
  26. #include <ktempfile.h>
  27. #include <kurl.h>
  28. #include <kdebug.h>
  29. #include "opera_xxport.h"
  30. K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_opera_xxport, OperaXXPort )
  31. OperaXXPort::OperaXXPort( KABC::AddressBook *ab, TQWidget *parent, const char *name )
  32. : KAB::XXPort( ab, parent, name )
  33. {
  34. createImportAction( i18n( "Import Opera Addressbook..." ) );
  35. }
  36. KABC::AddresseeList OperaXXPort::importContacts( const TQString& ) const
  37. {
  38. KABC::AddresseeList addrList;
  39. TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath() + TQString::tqfromLatin1( "/.opera/contacts.adr" ) );
  40. if ( fileName.isEmpty() )
  41. return addrList;
  42. TQFile file( fileName );
  43. if ( !file.open( IO_ReadOnly ) ) {
  44. TQString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
  45. KMessageBox::error( parentWidget(), msg.tqarg( fileName ) );
  46. return addrList;
  47. }
  48. TQTextStream stream( &file );
  49. stream.setEncoding( TQTextStream::UnicodeUTF8 );
  50. TQString line, key, value;
  51. bool parseContact = false;
  52. KABC::Addressee addr;
  53. TQRegExp separator( "\x02\x02" );
  54. while ( !stream.atEnd() ) {
  55. line = stream.readLine();
  56. line = line.stripWhiteSpace();
  57. if ( line == TQString::tqfromLatin1( "#CONTACT" ) ) {
  58. parseContact = true;
  59. addr = KABC::Addressee();
  60. continue;
  61. } else if ( line.isEmpty() ) {
  62. parseContact = false;
  63. if ( !addr.isEmpty() ) {
  64. addrList.append( addr );
  65. addr = KABC::Addressee();
  66. }
  67. continue;
  68. }
  69. if ( parseContact == true ) {
  70. int sep = line.find( '=' );
  71. key = line.left( sep ).lower();
  72. value = line.mid( sep + 1 );
  73. if ( key == TQString::tqfromLatin1( "name" ) )
  74. addr.setNameFromString( value );
  75. else if ( key == TQString::tqfromLatin1( "mail" ) ) {
  76. TQStringList emails = TQStringList::split( separator, value );
  77. TQStringList::Iterator it = emails.begin();
  78. bool preferred = true;
  79. for ( ; it != emails.end(); ++it ) {
  80. addr.insertEmail( *it, preferred );
  81. preferred = false;
  82. }
  83. } else if ( key == TQString::tqfromLatin1( "phone" ) )
  84. addr.insertPhoneNumber( KABC::PhoneNumber( value ) );
  85. else if ( key == TQString::tqfromLatin1( "fax" ) )
  86. addr.insertPhoneNumber( KABC::PhoneNumber( value,
  87. KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home ) );
  88. else if ( key == TQString::tqfromLatin1( "postaladdress" ) ) {
  89. KABC::Address address( KABC::Address::Home );
  90. address.setLabel( value.replace( separator, "\n" ) );
  91. addr.insertAddress( address );
  92. } else if ( key == TQString::tqfromLatin1( "description" ) )
  93. addr.setNote( value.replace( separator, "\n" ) );
  94. else if ( key == TQString::tqfromLatin1( "url" ) )
  95. addr.setUrl( KURL( value ) );
  96. else if ( key == TQString::tqfromLatin1( "pictureurl" ) ) {
  97. KABC::Picture pic( value );
  98. addr.setPhoto( pic );
  99. }
  100. }
  101. }
  102. file.close();
  103. return addrList;
  104. }
  105. #include "opera_xxport.moc"