kaddressbook

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