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