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.
 
 
 
 
 
 

214 lines
4.9 KiB

  1. /*
  2. This file is part of KAddressbook.
  3. Copyright (c) 2003 Daniel Molkentin <molkentin@kde.org>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, permission is given to link this program
  16. with any edition of TQt, and distribute the resulting executable,
  17. without including the source code for TQt in the source distribution.
  18. */
  19. #include <tqfile.h>
  20. #include <kfiledialog.h>
  21. #include <kio/netaccess.h>
  22. #include <klocale.h>
  23. #include <kmessagebox.h>
  24. #include <ktempfile.h>
  25. #include <kurl.h>
  26. #include <kdebug.h>
  27. #include "eudora_xxport.h"
  28. #define CTRL_C 3
  29. K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_eudora_xxport, EudoraXXPort )
  30. EudoraXXPort::EudoraXXPort( KABC::AddressBook *ab, TQWidget *parent, const char *name )
  31. : KAB::XXPort( ab, parent, name )
  32. {
  33. createImportAction( i18n( "Import Eudora Addressbook..." ) );
  34. }
  35. KABC::AddresseeList EudoraXXPort::importContacts( const TQString& ) const
  36. {
  37. TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath(),
  38. "*.[tT][xX][tT]|" + i18n("Eudora Light Addressbook (*.txt)"), 0 );
  39. if ( fileName.isEmpty() )
  40. return KABC::AddresseeList();
  41. TQFile file( fileName );
  42. if ( !file.open( IO_ReadOnly ) )
  43. return KABC::AddresseeList();
  44. TQString line;
  45. TQTextStream stream( &file );
  46. KABC::Addressee *a = 0;
  47. int bytesRead = 0;
  48. KABC::AddresseeList list;
  49. while( !stream.eof() ) {
  50. line = stream.readLine();
  51. bytesRead += line.length();
  52. TQString tmp;
  53. if ( line.startsWith( "alias" ) ) {
  54. if ( a ) { // Write it out
  55. list << *a;
  56. delete a;
  57. a = 0;
  58. a = new KABC::Addressee();
  59. } else
  60. a = new KABC::Addressee();
  61. tmp = key( line ).stripWhiteSpace();
  62. if ( !tmp.isEmpty() )
  63. a->setFormattedName( tmp );
  64. tmp = email( line ).stripWhiteSpace();
  65. if ( !tmp.isEmpty() )
  66. a->insertEmail( tmp );
  67. } else if ( line.startsWith( "note" ) ) {
  68. if ( !a ) // Must have an alias before a note
  69. break;
  70. tmp = comment( line ).stripWhiteSpace();
  71. if ( !tmp.isEmpty() )
  72. a->setNote( tmp );
  73. tmp = get( line, "name" ).stripWhiteSpace();
  74. if ( !tmp.isEmpty() )
  75. a->setNameFromString( tmp );
  76. tmp = get( line, "address" ).stripWhiteSpace();
  77. if ( !tmp.isEmpty() ) {
  78. KABC::Address addr;
  79. kdDebug(5720) << tmp << endl; // dump complete address
  80. addr.setLabel( tmp );
  81. a->insertAddress( addr );
  82. }
  83. tmp = get( line, "phone" ).stripWhiteSpace();
  84. if ( !tmp.isEmpty() )
  85. a->insertPhoneNumber( KABC::PhoneNumber( tmp, KABC::PhoneNumber::Home ) );
  86. }
  87. }
  88. if ( a ) { // Write out address
  89. list << *a;
  90. delete a;
  91. a = 0;
  92. }
  93. file.close();
  94. return list;
  95. }
  96. TQString EudoraXXPort::key( const TQString& line) const
  97. {
  98. int e;
  99. TQString result;
  100. int b = line.find( '\"', 0 );
  101. if ( b == -1 ) {
  102. b = line.find( ' ' );
  103. if ( b == -1 )
  104. return result;
  105. b++;
  106. e = line.find( ' ', b );
  107. result = line.mid( b, e - b );
  108. return result;
  109. }
  110. b++;
  111. e = line.find( '\"', b );
  112. if ( e == -1 )
  113. return result;
  114. result = line.mid( b, e - b );
  115. return result;
  116. }
  117. TQString EudoraXXPort::email( const TQString& line ) const
  118. {
  119. int b;
  120. TQString result;
  121. b = line.findRev( '\"' );
  122. if ( b == -1 ) {
  123. b = line.findRev( ' ' );
  124. if ( b == -1 )
  125. return result;
  126. }
  127. result = line.mid( b + 1 );
  128. return result;
  129. }
  130. TQString EudoraXXPort::comment( const TQString& line ) const
  131. {
  132. int b;
  133. TQString result;
  134. uint i;
  135. b = line.findRev( '>' );
  136. if ( b == -1 ) {
  137. b = line.findRev( '\"' );
  138. if ( b == -1 )
  139. return result;
  140. }
  141. result = line.mid( b + 1 );
  142. for ( i = 0; i < result.length(); i++ ) {
  143. if ( result[ i ] == CTRL_C )
  144. result[ i ] = '\n';
  145. }
  146. return result;
  147. }
  148. TQString EudoraXXPort::get( const TQString& line, const TQString& key ) const
  149. {
  150. TQString fd = "<" + key + ":";
  151. int b, e;
  152. uint i;
  153. // Find formatted key, return on error
  154. b = line.find( fd );
  155. if ( b == -1 )
  156. return TQString();
  157. b += fd.length();
  158. e = line.find( '>', b );
  159. if ( e == -1 )
  160. return TQString();
  161. e--;
  162. TQString result = line.mid( b, e - b + 1 );
  163. for ( i = 0; i < result.length(); i++ ) {
  164. if ( result[ i ] == CTRL_C )
  165. result[ i ] = '\n';
  166. }
  167. return result;
  168. }
  169. #include "eudora_xxport.moc"