-rw-r--r-- | kaddressbook/xxport/vcard_xxport.cpp | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/kaddressbook/xxport/vcard_xxport.cpp b/kaddressbook/xxport/vcard_xxport.cpp index 91df96d..9a8fa68 100644 --- a/kaddressbook/xxport/vcard_xxport.cpp +++ b/kaddressbook/xxport/vcard_xxport.cpp | |||
@@ -1,266 +1,272 @@ | |||
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 | /* |
25 | Enhanced Version of the file for platform independent KDE tools. | 25 | Enhanced Version of the file for platform independent KDE tools. |
26 | Copyright (c) 2004 Ulf Schenk | 26 | Copyright (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 | #include <qfileinfo.h> | 33 | #include <qfileinfo.h> |
34 | 34 | ||
35 | #include <kabc/vcardconverter.h> | 35 | #include <kabc/vcardconverter.h> |
36 | #include <kabc/vcardparser/vcardtool.h> | 36 | #include <kabc/vcardparser/vcardtool.h> |
37 | #include <kfiledialog.h> | 37 | #include <kfiledialog.h> |
38 | #ifndef KAB_EMBEDDED | 38 | #ifndef KAB_EMBEDDED |
39 | #include <kio/netaccess.h> | 39 | #include <kio/netaccess.h> |
40 | #endif //KAB_EMBEDDED | 40 | #endif //KAB_EMBEDDED |
41 | 41 | ||
42 | #include <klocale.h> | 42 | #include <klocale.h> |
43 | #include <kmessagebox.h> | 43 | #include <kmessagebox.h> |
44 | #include <ktempfile.h> | 44 | #include <ktempfile.h> |
45 | #include <kurl.h> | 45 | #include <kurl.h> |
46 | 46 | ||
47 | #include "xxportmanager.h" | 47 | #include "xxportmanager.h" |
48 | 48 | ||
49 | #include "vcard_xxport.h" | 49 | #include "vcard_xxport.h" |
50 | 50 | ||
51 | #ifndef KAB_EMBEDDED | 51 | #ifndef KAB_EMBEDDED |
52 | 52 | ||
53 | class VCardXXPortFactory : public XXPortFactory | 53 | class VCardXXPortFactory : public XXPortFactory |
54 | { | 54 | { |
55 | public: | 55 | public: |
56 | XXPortObject *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name ) | 56 | XXPortObject *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name ) |
57 | { | 57 | { |
58 | return new VCardXXPort( ab, parent, name ); | 58 | return new VCardXXPort( ab, parent, name ); |
59 | } | 59 | } |
60 | }; | 60 | }; |
61 | #endif //KAB_EMBEDDED | 61 | #endif //KAB_EMBEDDED |
62 | 62 | ||
63 | 63 | ||
64 | extern "C" | 64 | extern "C" |
65 | { | 65 | { |
66 | #ifndef KAB_EMBEDDED | 66 | #ifndef KAB_EMBEDDED |
67 | void *init_libkaddrbk_vcard_xxport() | 67 | void *init_libkaddrbk_vcard_xxport() |
68 | #else //KAB_EMBEDDED | 68 | #else //KAB_EMBEDDED |
69 | void *init_microkaddrbk_vcard_xxport() | 69 | void *init_microkaddrbk_vcard_xxport() |
70 | #endif //KAB_EMBEDDED | 70 | #endif //KAB_EMBEDDED |
71 | { | 71 | { |
72 | return ( new VCardXXPortFactory() ); | 72 | return ( new VCardXXPortFactory() ); |
73 | } | 73 | } |
74 | } | 74 | } |
75 | 75 | ||
76 | 76 | ||
77 | VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name ) | 77 | VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name ) |
78 | : XXPortObject( ab, parent, name ) | 78 | : XXPortObject( ab, parent, name ) |
79 | { | 79 | { |
80 | createImportAction( i18n( "Import vCard..." ) ); | 80 | createImportAction( i18n( "Import vCard..." ) ); |
81 | //US KABC::VCardConverter does not support the export of 2.1 addressbooks. | 81 | //US KABC::VCardConverter does not support the export of 2.1 addressbooks. |
82 | //US createExportAction( i18n( "Export vCard 2.1..." ), "v21" ); | 82 | //US createExportAction( i18n( "Export vCard 2.1..." ), "v21" ); |
83 | createExportAction( i18n( "Export vCard 3.0..." ), "v30" ); | 83 | createExportAction( i18n( "Export vCard 3.0..." ), "v30" ); |
84 | } | 84 | } |
85 | 85 | ||
86 | bool VCardXXPort::exportContacts( const KABC::AddresseeList &list, const QString &data ) | 86 | bool VCardXXPort::exportContacts( const KABC::AddresseeList &list, const QString &data ) |
87 | { | 87 | { |
88 | QString name; | 88 | QString name; |
89 | 89 | ||
90 | if ( list.count() == 1 ) | 90 | if ( list.count() == 1 ) |
91 | name = list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf"; | 91 | name = list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf"; |
92 | else | 92 | else |
93 | name = "addressbook.vcf"; | 93 | name = "addressbook.vcf"; |
94 | 94 | ||
95 | #ifndef KAB_EMBEDDED | 95 | #ifndef KAB_EMBEDDED |
96 | QString fileName = KFileDialog::getSaveFileName( name ); | 96 | QString fileName = KFileDialog::getSaveFileName( name ); |
97 | #else //KAB_EMBEDDED | 97 | #else //KAB_EMBEDDED |
98 | QString fileName = KFileDialog::getSaveFileName( name, i18n("Save file"), parentWidget() ); | 98 | QString fileName = KFileDialog::getSaveFileName( name, i18n("Save file"), parentWidget() ); |
99 | #endif //KAB_EMBEDDED | 99 | #endif //KAB_EMBEDDED |
100 | 100 | ||
101 | if ( fileName.isEmpty() ) | 101 | if ( fileName.isEmpty() ) |
102 | return false; | 102 | return false; |
103 | 103 | ||
104 | QFile outFile( fileName ); | 104 | QFile outFile( fileName ); |
105 | if ( !outFile.open( IO_WriteOnly ) ) { | 105 | if ( !outFile.open( IO_WriteOnly ) ) { |
106 | QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); | 106 | QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); |
107 | KMessageBox::error( parentWidget(), text.arg( fileName ) ); | 107 | KMessageBox::error( parentWidget(), text.arg( fileName ) ); |
108 | return false; | 108 | return false; |
109 | } | 109 | } |
110 | 110 | ||
111 | QTextStream t( &outFile ); | 111 | QTextStream t( &outFile ); |
112 | t.setEncoding( QTextStream::UnicodeUTF8 ); | 112 | t.setEncoding( QTextStream::UnicodeUTF8 ); |
113 | 113 | ||
114 | KABC::Addressee::List::ConstIterator it; | 114 | KABC::Addressee::List::ConstIterator it; |
115 | for ( it = list.begin(); it != list.end(); ++it ) { | 115 | for ( it = list.begin(); it != list.end(); ++it ) { |
116 | KABC::VCardConverter converter; | 116 | KABC::VCardConverter converter; |
117 | QString vcard; | 117 | QString vcard; |
118 | 118 | ||
119 | KABC::VCardConverter::Version version; | 119 | KABC::VCardConverter::Version version; |
120 | if ( data == "v21" ) | 120 | if ( data == "v21" ) |
121 | version = KABC::VCardConverter::v2_1; | 121 | version = KABC::VCardConverter::v2_1; |
122 | else | 122 | else |
123 | version = KABC::VCardConverter::v3_0; | 123 | version = KABC::VCardConverter::v3_0; |
124 | 124 | ||
125 | converter.addresseeToVCard( *it, vcard, version ); | 125 | converter.addresseeToVCard( *it, vcard, version ); |
126 | t << vcard << "\r\n\r\n"; | 126 | t << vcard << "\r\n\r\n"; |
127 | } | 127 | } |
128 | 128 | ||
129 | outFile.close(); | 129 | outFile.close(); |
130 | 130 | ||
131 | return true; | 131 | return true; |
132 | } | 132 | } |
133 | 133 | ||
134 | KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const | 134 | KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const |
135 | { | 135 | { |
136 | QString fileName; | 136 | QString fileName; |
137 | KABC::AddresseeList addrList; | 137 | KABC::AddresseeList addrList; |
138 | KURL url; | 138 | KURL url; |
139 | 139 | ||
140 | #ifndef KAB_EMBEDDED | 140 | #ifndef KAB_EMBEDDED |
141 | if ( !XXPortManager::importData.isEmpty() ) | 141 | if ( !XXPortManager::importData.isEmpty() ) |
142 | addrList = parseVCard( XXPortManager::importData ); | 142 | addrList = parseVCard( XXPortManager::importData ); |
143 | else { | 143 | else { |
144 | if ( XXPortManager::importURL.isEmpty() ) | 144 | if ( XXPortManager::importURL.isEmpty() ) |
145 | { | 145 | { |
146 | url = KFileDialog::getLoadFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); | 146 | url = KFileDialog::getLoadFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); |
147 | } | 147 | } |
148 | else | 148 | else |
149 | url = XXPortManager::importURL; | 149 | url = XXPortManager::importURL; |
150 | if ( url.isEmpty() ) | 150 | if ( url.isEmpty() ) |
151 | return addrList; | 151 | return addrList; |
152 | QString caption( i18n( "vCard Import Failed" ) ); | 152 | QString caption( i18n( "vCard Import Failed" ) ); |
153 | if ( KIO::NetAccess::download( url, fileName ) ) { | 153 | if ( KIO::NetAccess::download( url, fileName ) ) { |
154 | QFile file( fileName ); | 154 | QFile file( fileName ); |
155 | 155 | ||
156 | file.open( IO_ReadOnly ); | 156 | file.open( IO_ReadOnly ); |
157 | QByteArray rawData = file.readAll(); | 157 | QByteArray rawData = file.readAll(); |
158 | file.close(); | 158 | file.close(); |
159 | 159 | ||
160 | QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | 160 | QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); |
161 | addrList = parseVCard( data ); | 161 | addrList = parseVCard( data ); |
162 | 162 | ||
163 | if ( !url.isLocalFile() ) | 163 | if ( !url.isLocalFile() ) |
164 | KIO::NetAccess::removeTempFile( fileName ); | 164 | KIO::NetAccess::removeTempFile( fileName ); |
165 | 165 | ||
166 | } else { | 166 | } else { |
167 | QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" ); | 167 | QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" ); |
168 | KMessageBox::error( parentWidget(), text.arg( url.url() ), caption ); | 168 | KMessageBox::error( parentWidget(), text.arg( url.url() ), caption ); |
169 | } | 169 | } |
170 | 170 | ||
171 | } | 171 | } |
172 | 172 | ||
173 | 173 | ||
174 | #else //KAB_EMBEDDED | 174 | #else //KAB_EMBEDDED |
175 | 175 | ||
176 | 176 | ||
177 | if ( !XXPortManager::importData.isEmpty() ) | 177 | if ( !XXPortManager::importData.isEmpty() ) |
178 | addrList = parseVCard( XXPortManager::importData ); | 178 | addrList = parseVCard( XXPortManager::importData ); |
179 | else { | 179 | else { |
180 | if ( XXPortManager::importURL.isEmpty() ) | 180 | if ( XXPortManager::importURL.isEmpty() ) |
181 | { | 181 | { |
182 | fileName = KFileDialog::getOpenFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); | 182 | fileName = KFileDialog::getOpenFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); |
183 | 183 | ||
184 | if ( fileName.isEmpty() ) | 184 | if ( fileName.isEmpty() ) |
185 | return addrList; | 185 | return addrList; |
186 | QFileInfo fi ( fileName ); | 186 | QFileInfo fi ( fileName ); |
187 | if ( !fi.isFile() ) | 187 | if ( !fi.isFile() ) |
188 | return addrList; | 188 | return addrList; |
189 | } | 189 | } |
190 | else | 190 | else |
191 | { | 191 | { |
192 | //US url = XXPortManager::importURL; | 192 | //US url = XXPortManager::importURL; |
193 | qDebug("VCardXXPort::importContacts Urls at the moment not supported"); | 193 | qDebug("VCardXXPort::importContacts Urls at the moment not supported"); |
194 | if ( url.isEmpty() ) | 194 | if ( url.isEmpty() ) |
195 | return addrList; | 195 | return addrList; |
196 | 196 | ||
197 | } | 197 | } |
198 | QFile file( fileName ); | 198 | QFile file( fileName ); |
199 | if ( file.open( IO_ReadOnly ) ) { | 199 | if ( file.open( IO_ReadOnly ) ) { |
200 | QCString rawData ( file.readAll().data(),file.size()+1); | 200 | QCString rawData ( file.readAll().data(),file.size()+1); |
201 | file.close(); | 201 | file.close(); |
202 | int start = 0; | 202 | int start = 0; |
203 | #ifndef DESKTOP_VERSION | 203 | #ifndef DESKTOP_VERSION |
204 | while ( start < rawData.size()-2 ) { | 204 | while ( start < rawData.size()-2 ) { |
205 | if ( rawData.at( start ) == '\r' ) | 205 | if ( rawData.at( start ) == '\r' ) |
206 | if ( rawData.at( start+1 ) == '\n' ) | 206 | if ( rawData.at( start+1 ) == '\n' ) |
207 | if ( rawData.at( start+2 ) == ' ' ) { | 207 | if ( rawData.at( start+2 ) == ' ' ) { |
208 | rawData.remove(start,3); | 208 | rawData.remove(start,3); |
209 | --start; | 209 | --start; |
210 | } | 210 | } |
211 | ++start; | 211 | ++start; |
212 | } | 212 | } |
213 | #endif | 213 | #endif |
214 | addrList = parseVCard( QString::fromUtf8( rawData.data() ) ); | 214 | int ret = KMessageBox::warningYesNoCancel( 0, i18n("Select import format!\nDefault and standard is Utf8.\nLatin1 may be the right\nfor some West Europian languages."), i18n("Import Format"), i18n("Utf8"), i18n("Latin1") ); |
215 | if ( ret == KMessageBox::Cancel ) | ||
216 | return addrList; | ||
217 | if ( ret == KMessageBox::Yes ) | ||
218 | addrList = parseVCard( QString::fromUtf8( rawData.data() ) ); | ||
219 | else | ||
220 | addrList = parseVCard( QString::fromLatin1( rawData.data() ) ); | ||
215 | } | 221 | } |
216 | 222 | ||
217 | } | 223 | } |
218 | #endif //KAB_EMBEDDED | 224 | #endif //KAB_EMBEDDED |
219 | 225 | ||
220 | return addrList; | 226 | return addrList; |
221 | } | 227 | } |
222 | 228 | ||
223 | KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const | 229 | KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const |
224 | { | 230 | { |
225 | 231 | ||
226 | KABC::VCardTool tool; | 232 | KABC::VCardTool tool; |
227 | KABC::AddresseeList addrList; | 233 | KABC::AddresseeList addrList; |
228 | addrList = tool.parseVCards( data ); | 234 | addrList = tool.parseVCards( data ); |
229 | // LR : I switched to the code, which is in current cvs HEAD | 235 | // LR : I switched to the code, which is in current cvs HEAD |
230 | /* | 236 | /* |
231 | uint numVCards = data.contains( "BEGIN:VCARD", false ); | 237 | uint numVCards = data.contains( "BEGIN:VCARD", false ); |
232 | QStringList dataList = QStringList::split( "\r\n\r\n", data ); | 238 | QStringList dataList = QStringList::split( "\r\n\r\n", data ); |
233 | 239 | ||
234 | for ( uint i = 0; i < numVCards && i < dataList.count(); ++i ) { | 240 | for ( uint i = 0; i < numVCards && i < dataList.count(); ++i ) { |
235 | KABC::Addressee addr; | 241 | KABC::Addressee addr; |
236 | bool ok = false; | 242 | bool ok = false; |
237 | 243 | ||
238 | if ( dataList[ i ].contains( "VERSION:3.0" ) ) | 244 | if ( dataList[ i ].contains( "VERSION:3.0" ) ) |
239 | ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v3_0 ); | 245 | ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v3_0 ); |
240 | else if ( dataList[ i ].contains( "VERSION:2.1" ) ) | 246 | else if ( dataList[ i ].contains( "VERSION:2.1" ) ) |
241 | ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v2_1 ); | 247 | ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v2_1 ); |
242 | else { | 248 | else { |
243 | KMessageBox::sorry( parentWidget(), i18n( "Not supported vCard version." ) ); | 249 | KMessageBox::sorry( parentWidget(), i18n( "Not supported vCard version." ) ); |
244 | continue; | 250 | continue; |
245 | } | 251 | } |
246 | 252 | ||
247 | if ( !addr.isEmpty() && ok ) | 253 | if ( !addr.isEmpty() && ok ) |
248 | addrList.append( addr ); | 254 | addrList.append( addr ); |
249 | else { | 255 | else { |
250 | QString text = i18n( "The selected file does not include a valid vCard. " | 256 | QString text = i18n( "The selected file does not include a valid vCard. " |
251 | "Please check the file and try again." ); | 257 | "Please check the file and try again." ); |
252 | KMessageBox::sorry( parentWidget(), text ); | 258 | KMessageBox::sorry( parentWidget(), text ); |
253 | } | 259 | } |
254 | } | 260 | } |
255 | */ | 261 | */ |
256 | if ( addrList.isEmpty() ) { | 262 | if ( addrList.isEmpty() ) { |
257 | QString text = i18n( "The selected file does not\ninclude a valid vCard.\nPlease check the file and try again.\n" ); | 263 | QString text = i18n( "The selected file does not\ninclude a valid vCard.\nPlease check the file and try again.\n" ); |
258 | KMessageBox::sorry( parentWidget(), text ); | 264 | KMessageBox::sorry( parentWidget(), text ); |
259 | } | 265 | } |
260 | return addrList; | 266 | return addrList; |
261 | } | 267 | } |
262 | 268 | ||
263 | 269 | ||
264 | #ifndef KAB_EMBEDDED | 270 | #ifndef KAB_EMBEDDED |
265 | #include "vcard_xxport.moc" | 271 | #include "vcard_xxport.moc" |
266 | #endif //KAB_EMBEDDED | 272 | #endif //KAB_EMBEDDED |