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