summaryrefslogtreecommitdiffabout
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--kaddressbook/xxport/vcard_xxport.cpp3
1 files changed, 2 insertions, 1 deletions
diff --git a/kaddressbook/xxport/vcard_xxport.cpp b/kaddressbook/xxport/vcard_xxport.cpp
index b53f873..adf47cd 100644
--- a/kaddressbook/xxport/vcard_xxport.cpp
+++ b/kaddressbook/xxport/vcard_xxport.cpp
@@ -1,254 +1,255 @@
1/* 1/*
2 This file is part of KAddressbook. 2 This file is part of KAddressbook.
3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org> 3 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4 4
5 This program is free software; you can redistribute it and/or modify 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 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 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 13 GNU General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 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 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 18
19 As a special exception, permission is given to link this program 19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable, 20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution. 21 without including the source code for Qt in the source distribution.
22*/ 22*/
23 23
24/* 24/*
25Enhanced Version of the file for platform independent KDE tools. 25Enhanced Version of the file for platform independent KDE tools.
26Copyright (c) 2004 Ulf Schenk 26Copyright (c) 2004 Ulf Schenk
27 27
28$Id$ 28$Id$
29*/ 29*/
30 30
31#include <qfile.h> 31#include <qfile.h>
32#include <qtextstream.h> 32#include <qtextstream.h>
33 33
34#include <kabc/vcardconverter.h> 34#include <kabc/vcardconverter.h>
35#include <kabc/vcardparser/vcardtool.h> 35#include <kabc/vcardparser/vcardtool.h>
36#include <kfiledialog.h> 36#include <kfiledialog.h>
37#ifndef KAB_EMBEDDED 37#ifndef KAB_EMBEDDED
38#include <kio/netaccess.h> 38#include <kio/netaccess.h>
39#endif //KAB_EMBEDDED 39#endif //KAB_EMBEDDED
40 40
41#include <klocale.h> 41#include <klocale.h>
42#include <kmessagebox.h> 42#include <kmessagebox.h>
43#include <ktempfile.h> 43#include <ktempfile.h>
44#include <kurl.h> 44#include <kurl.h>
45 45
46#include "xxportmanager.h" 46#include "xxportmanager.h"
47 47
48#include "vcard_xxport.h" 48#include "vcard_xxport.h"
49 49
50#ifndef KAB_EMBEDDED 50#ifndef KAB_EMBEDDED
51 51
52class VCardXXPortFactory : public XXPortFactory 52class VCardXXPortFactory : public XXPortFactory
53{ 53{
54 public: 54 public:
55 XXPortObject *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name ) 55 XXPortObject *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name )
56 { 56 {
57 return new VCardXXPort( ab, parent, name ); 57 return new VCardXXPort( ab, parent, name );
58 } 58 }
59}; 59};
60#endif //KAB_EMBEDDED 60#endif //KAB_EMBEDDED
61 61
62 62
63extern "C" 63extern "C"
64{ 64{
65#ifndef KAB_EMBEDDED 65#ifndef KAB_EMBEDDED
66 void *init_libkaddrbk_vcard_xxport() 66 void *init_libkaddrbk_vcard_xxport()
67#else //KAB_EMBEDDED 67#else //KAB_EMBEDDED
68 void *init_kaddrbk_vcard_xxport() 68 void *init_kaddrbk_vcard_xxport()
69#endif //KAB_EMBEDDED 69#endif //KAB_EMBEDDED
70 { 70 {
71 return ( new VCardXXPortFactory() ); 71 return ( new VCardXXPortFactory() );
72 } 72 }
73} 73}
74 74
75 75
76VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name ) 76VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
77 : XXPortObject( ab, parent, name ) 77 : XXPortObject( ab, parent, name )
78{ 78{
79 createImportAction( i18n( "Import vCard..." ) ); 79 createImportAction( i18n( "Import vCard..." ) );
80 createExportAction( i18n( "Export vCard 2.1..." ), "v21" ); 80//US KABC::VCardConverter does not support the export of 2.1 addressbooks.
81//US createExportAction( i18n( "Export vCard 2.1..." ), "v21" );
81 createExportAction( i18n( "Export vCard 3.0..." ), "v30" ); 82 createExportAction( i18n( "Export vCard 3.0..." ), "v30" );
82} 83}
83 84
84bool VCardXXPort::exportContacts( const KABC::AddresseeList &list, const QString &data ) 85bool VCardXXPort::exportContacts( const KABC::AddresseeList &list, const QString &data )
85{ 86{
86 QString name; 87 QString name;
87 88
88 if ( list.count() == 1 ) 89 if ( list.count() == 1 )
89 name = list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf"; 90 name = list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf";
90 else 91 else
91 name = "addressbook.vcf"; 92 name = "addressbook.vcf";
92 93
93#ifndef KAB_EMBEDDED 94#ifndef KAB_EMBEDDED
94 QString fileName = KFileDialog::getSaveFileName( name ); 95 QString fileName = KFileDialog::getSaveFileName( name );
95#else //KAB_EMBEDDED 96#else //KAB_EMBEDDED
96 QString fileName = KFileDialog::getSaveFileName( name, i18n("Save file"), parentWidget() ); 97 QString fileName = KFileDialog::getSaveFileName( name, i18n("Save file"), parentWidget() );
97#endif //KAB_EMBEDDED 98#endif //KAB_EMBEDDED
98 99
99 if ( fileName.isEmpty() ) 100 if ( fileName.isEmpty() )
100 return true; 101 return true;
101 102
102 QFile outFile( fileName ); 103 QFile outFile( fileName );
103 if ( !outFile.open( IO_WriteOnly ) ) { 104 if ( !outFile.open( IO_WriteOnly ) ) {
104 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); 105 QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" );
105 KMessageBox::error( parentWidget(), text.arg( fileName ) ); 106 KMessageBox::error( parentWidget(), text.arg( fileName ) );
106 return false; 107 return false;
107 } 108 }
108 109
109 QTextStream t( &outFile ); 110 QTextStream t( &outFile );
110 t.setEncoding( QTextStream::UnicodeUTF8 ); 111 t.setEncoding( QTextStream::UnicodeUTF8 );
111 112
112 KABC::Addressee::List::ConstIterator it; 113 KABC::Addressee::List::ConstIterator it;
113 for ( it = list.begin(); it != list.end(); ++it ) { 114 for ( it = list.begin(); it != list.end(); ++it ) {
114 KABC::VCardConverter converter; 115 KABC::VCardConverter converter;
115 QString vcard; 116 QString vcard;
116 117
117 KABC::VCardConverter::Version version; 118 KABC::VCardConverter::Version version;
118 if ( data == "v21" ) 119 if ( data == "v21" )
119 version = KABC::VCardConverter::v2_1; 120 version = KABC::VCardConverter::v2_1;
120 else 121 else
121 version = KABC::VCardConverter::v3_0; 122 version = KABC::VCardConverter::v3_0;
122 123
123 converter.addresseeToVCard( *it, vcard, version ); 124 converter.addresseeToVCard( *it, vcard, version );
124 t << vcard << "\r\n\r\n"; 125 t << vcard << "\r\n\r\n";
125 } 126 }
126 127
127 outFile.close(); 128 outFile.close();
128 129
129 return true; 130 return true;
130} 131}
131 132
132KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const 133KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const
133{ 134{
134 QString fileName; 135 QString fileName;
135 KABC::AddresseeList addrList; 136 KABC::AddresseeList addrList;
136 KURL url; 137 KURL url;
137 138
138#ifndef KAB_EMBEDDED 139#ifndef KAB_EMBEDDED
139 if ( !XXPortManager::importData.isEmpty() ) 140 if ( !XXPortManager::importData.isEmpty() )
140 addrList = parseVCard( XXPortManager::importData ); 141 addrList = parseVCard( XXPortManager::importData );
141 else { 142 else {
142 if ( XXPortManager::importURL.isEmpty() ) 143 if ( XXPortManager::importURL.isEmpty() )
143 { 144 {
144 url = KFileDialog::getLoadFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); 145 url = KFileDialog::getLoadFileName( QString::null, i18n("Select vCard to Import"), parentWidget() );
145 } 146 }
146 else 147 else
147 url = XXPortManager::importURL; 148 url = XXPortManager::importURL;
148 149
149 if ( url.isEmpty() ) 150 if ( url.isEmpty() )
150 return addrList; 151 return addrList;
151 152
152 QString caption( i18n( "vCard Import Failed" ) ); 153 QString caption( i18n( "vCard Import Failed" ) );
153 if ( KIO::NetAccess::download( url, fileName ) ) { 154 if ( KIO::NetAccess::download( url, fileName ) ) {
154 QFile file( fileName ); 155 QFile file( fileName );
155 156
156 file.open( IO_ReadOnly ); 157 file.open( IO_ReadOnly );
157 QByteArray rawData = file.readAll(); 158 QByteArray rawData = file.readAll();
158 file.close(); 159 file.close();
159 160
160 QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 161 QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
161 addrList = parseVCard( data ); 162 addrList = parseVCard( data );
162 163
163 if ( !url.isLocalFile() ) 164 if ( !url.isLocalFile() )
164 KIO::NetAccess::removeTempFile( fileName ); 165 KIO::NetAccess::removeTempFile( fileName );
165 166
166 } else { 167 } else {
167 QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" ); 168 QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" );
168 KMessageBox::error( parentWidget(), text.arg( url.url() ), caption ); 169 KMessageBox::error( parentWidget(), text.arg( url.url() ), caption );
169 } 170 }
170 171
171 } 172 }
172 173
173 174
174#else //KAB_EMBEDDED 175#else //KAB_EMBEDDED
175 176
176 177
177 if ( !XXPortManager::importData.isEmpty() ) 178 if ( !XXPortManager::importData.isEmpty() )
178 addrList = parseVCard( XXPortManager::importData ); 179 addrList = parseVCard( XXPortManager::importData );
179 else { 180 else {
180 if ( XXPortManager::importURL.isEmpty() ) 181 if ( XXPortManager::importURL.isEmpty() )
181 { 182 {
182 fileName = KFileDialog::getOpenFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); 183 fileName = KFileDialog::getOpenFileName( QString::null, i18n("Select vCard to Import"), parentWidget() );
183 if ( fileName.isEmpty() ) 184 if ( fileName.isEmpty() )
184 return addrList; 185 return addrList;
185 186
186 } 187 }
187 else 188 else
188 { 189 {
189//US url = XXPortManager::importURL; 190//US url = XXPortManager::importURL;
190 qDebug("VCardXXPort::importContacts Urls at the moment not supported"); 191 qDebug("VCardXXPort::importContacts Urls at the moment not supported");
191 if ( url.isEmpty() ) 192 if ( url.isEmpty() )
192 return addrList; 193 return addrList;
193 194
194 } 195 }
195 196
196 197
197 QFile file( fileName ); 198 QFile file( fileName );
198 199
199 file.open( IO_ReadOnly ); 200 file.open( IO_ReadOnly );
200 QByteArray rawData = file.readAll(); 201 QByteArray rawData = file.readAll();
201 file.close(); 202 file.close();
202 203
203 QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); 204 QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 );
204 addrList = parseVCard( data ); 205 addrList = parseVCard( data );
205 206
206 } 207 }
207#endif //KAB_EMBEDDED 208#endif //KAB_EMBEDDED
208 209
209 return addrList; 210 return addrList;
210} 211}
211 212
212KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const 213KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const
213{ 214{
214 215
215 KABC::VCardTool tool; 216 KABC::VCardTool tool;
216 KABC::AddresseeList addrList; 217 KABC::AddresseeList addrList;
217 addrList = tool.parseVCards( data ); 218 addrList = tool.parseVCards( data );
218 // LR : I switched to the code, which is in current cvs HEAD 219 // LR : I switched to the code, which is in current cvs HEAD
219 /* 220 /*
220 uint numVCards = data.contains( "BEGIN:VCARD", false ); 221 uint numVCards = data.contains( "BEGIN:VCARD", false );
221 QStringList dataList = QStringList::split( "\r\n\r\n", data ); 222 QStringList dataList = QStringList::split( "\r\n\r\n", data );
222 223
223 for ( uint i = 0; i < numVCards && i < dataList.count(); ++i ) { 224 for ( uint i = 0; i < numVCards && i < dataList.count(); ++i ) {
224 KABC::Addressee addr; 225 KABC::Addressee addr;
225 bool ok = false; 226 bool ok = false;
226 227
227 if ( dataList[ i ].contains( "VERSION:3.0" ) ) 228 if ( dataList[ i ].contains( "VERSION:3.0" ) )
228 ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v3_0 ); 229 ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v3_0 );
229 else if ( dataList[ i ].contains( "VERSION:2.1" ) ) 230 else if ( dataList[ i ].contains( "VERSION:2.1" ) )
230 ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v2_1 ); 231 ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v2_1 );
231 else { 232 else {
232 KMessageBox::sorry( parentWidget(), i18n( "Not supported vCard version." ) ); 233 KMessageBox::sorry( parentWidget(), i18n( "Not supported vCard version." ) );
233 continue; 234 continue;
234 } 235 }
235 236
236 if ( !addr.isEmpty() && ok ) 237 if ( !addr.isEmpty() && ok )
237 addrList.append( addr ); 238 addrList.append( addr );
238 else { 239 else {
239 QString text = i18n( "The selected file does not include a valid vCard. " 240 QString text = i18n( "The selected file does not include a valid vCard. "
240 "Please check the file and try again." ); 241 "Please check the file and try again." );
241 KMessageBox::sorry( parentWidget(), text ); 242 KMessageBox::sorry( parentWidget(), text );
242 } 243 }
243 } 244 }
244 */ 245 */
245 if ( addrList.isEmpty() ) { 246 if ( addrList.isEmpty() ) {
246 QString text = i18n( "The selected file does not\ninclude a valid vCard.\nPlease check the file and try again.\n" ); 247 QString text = i18n( "The selected file does not\ninclude a valid vCard.\nPlease check the file and try again.\n" );
247 KMessageBox::sorry( parentWidget(), text ); 248 KMessageBox::sorry( parentWidget(), text );
248 } 249 }
249 return addrList; 250 return addrList;
250} 251}
251 252
252#ifndef KAB_EMBEDDED 253#ifndef KAB_EMBEDDED
253#include "vcard_xxport.moc" 254#include "vcard_xxport.moc"
254#endif //KAB_EMBEDDED 255#endif //KAB_EMBEDDED