author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /kaddressbook/xxport/vcard_xxport.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
Diffstat (limited to 'kaddressbook/xxport/vcard_xxport.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/xxport/vcard_xxport.cpp | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/kaddressbook/xxport/vcard_xxport.cpp b/kaddressbook/xxport/vcard_xxport.cpp new file mode 100644 index 0000000..dd49a9c --- a/dev/null +++ b/kaddressbook/xxport/vcard_xxport.cpp | |||
@@ -0,0 +1,238 @@ | |||
1 | /* | ||
2 | This file is part of KAddressbook. | ||
3 | Copyright (c) 2003 Tobias Koenig <tokoe@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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | |||
19 | As a special exception, permission is given to link this program | ||
20 | with any edition of Qt, and distribute the resulting executable, | ||
21 | without including the source code for Qt in the source distribution. | ||
22 | */ | ||
23 | |||
24 | /* | ||
25 | Enhanced Version of the file for platform independent KDE tools. | ||
26 | Copyright (c) 2004 Ulf Schenk | ||
27 | |||
28 | $Id$ | ||
29 | */ | ||
30 | |||
31 | #include <qfile.h> | ||
32 | #include <qtextstream.h> | ||
33 | |||
34 | #include <kabc/vcardconverter.h> | ||
35 | #include <kfiledialog.h> | ||
36 | #ifndef KAB_EMBEDDED | ||
37 | #include <kio/netaccess.h> | ||
38 | #endif //KAB_EMBEDDED | ||
39 | |||
40 | #include <klocale.h> | ||
41 | #include <kmessagebox.h> | ||
42 | #include <ktempfile.h> | ||
43 | #include <kurl.h> | ||
44 | |||
45 | #include "xxportmanager.h" | ||
46 | |||
47 | #include "vcard_xxport.h" | ||
48 | |||
49 | #ifndef KAB_EMBEDDED | ||
50 | |||
51 | class VCardXXPortFactory : public XXPortFactory | ||
52 | { | ||
53 | public: | ||
54 | XXPortObject *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name ) | ||
55 | { | ||
56 | return new VCardXXPort( ab, parent, name ); | ||
57 | } | ||
58 | }; | ||
59 | |||
60 | |||
61 | extern "C" | ||
62 | { | ||
63 | void *init_libkaddrbk_vcard_xxport() | ||
64 | { | ||
65 | return ( new VCardXXPortFactory() ); | ||
66 | } | ||
67 | } | ||
68 | #endif //KAB_EMBEDDED | ||
69 | |||
70 | |||
71 | VCardXXPort::VCardXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name ) | ||
72 | : XXPortObject( ab, parent, name ) | ||
73 | { | ||
74 | createImportAction( i18n( "Import vCard..." ) ); | ||
75 | createExportAction( i18n( "Export vCard 2.1..." ), "v21" ); | ||
76 | createExportAction( i18n( "Export vCard 3.0..." ), "v30" ); | ||
77 | } | ||
78 | |||
79 | bool VCardXXPort::exportContacts( const KABC::AddresseeList &list, const QString &data ) | ||
80 | { | ||
81 | QString name; | ||
82 | |||
83 | if ( list.count() == 1 ) | ||
84 | name = list[ 0 ].givenName() + "_" + list[ 0 ].familyName() + ".vcf"; | ||
85 | else | ||
86 | name = "addressbook.vcf"; | ||
87 | |||
88 | #ifndef KAB_EMBEDDED | ||
89 | QString fileName = KFileDialog::getSaveFileName( name ); | ||
90 | #else //KAB_EMBEDDED | ||
91 | QString fileName = KFileDialog::getSaveFileName( name, i18n("Save file"), parentWidget() ); | ||
92 | #endif //KAB_EMBEDDED | ||
93 | |||
94 | if ( fileName.isEmpty() ) | ||
95 | return true; | ||
96 | |||
97 | QFile outFile( fileName ); | ||
98 | if ( !outFile.open( IO_WriteOnly ) ) { | ||
99 | QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); | ||
100 | KMessageBox::error( parentWidget(), text.arg( fileName ) ); | ||
101 | return false; | ||
102 | } | ||
103 | |||
104 | QTextStream t( &outFile ); | ||
105 | t.setEncoding( QTextStream::UnicodeUTF8 ); | ||
106 | |||
107 | KABC::Addressee::List::ConstIterator it; | ||
108 | for ( it = list.begin(); it != list.end(); ++it ) { | ||
109 | KABC::VCardConverter converter; | ||
110 | QString vcard; | ||
111 | |||
112 | KABC::VCardConverter::Version version; | ||
113 | if ( data == "v21" ) | ||
114 | version = KABC::VCardConverter::v2_1; | ||
115 | else | ||
116 | version = KABC::VCardConverter::v3_0; | ||
117 | |||
118 | converter.addresseeToVCard( *it, vcard, version ); | ||
119 | t << vcard << "\r\n\r\n"; | ||
120 | } | ||
121 | |||
122 | outFile.close(); | ||
123 | |||
124 | return true; | ||
125 | } | ||
126 | |||
127 | KABC::AddresseeList VCardXXPort::importContacts( const QString& ) const | ||
128 | { | ||
129 | QString fileName; | ||
130 | KABC::AddresseeList addrList; | ||
131 | KURL url; | ||
132 | |||
133 | #ifndef KAB_EMBEDDED | ||
134 | if ( !XXPortManager::importData.isEmpty() ) | ||
135 | addrList = parseVCard( XXPortManager::importData ); | ||
136 | else { | ||
137 | if ( XXPortManager::importURL.isEmpty() ) | ||
138 | { | ||
139 | url = KFileDialog::getLoadFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); | ||
140 | } | ||
141 | else | ||
142 | url = XXPortManager::importURL; | ||
143 | |||
144 | if ( url.isEmpty() ) | ||
145 | return addrList; | ||
146 | |||
147 | QString caption( i18n( "vCard Import Failed" ) ); | ||
148 | if ( KIO::NetAccess::download( url, fileName ) ) { | ||
149 | QFile file( fileName ); | ||
150 | |||
151 | file.open( IO_ReadOnly ); | ||
152 | QByteArray rawData = file.readAll(); | ||
153 | file.close(); | ||
154 | |||
155 | QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | ||
156 | addrList = parseVCard( data ); | ||
157 | |||
158 | if ( !url.isLocalFile() ) | ||
159 | KIO::NetAccess::removeTempFile( fileName ); | ||
160 | |||
161 | } else { | ||
162 | QString text = i18n( "<qt>Unable to access <b>%1</b>.</qt>" ); | ||
163 | KMessageBox::error( parentWidget(), text.arg( url.url() ), caption ); | ||
164 | } | ||
165 | |||
166 | } | ||
167 | |||
168 | |||
169 | #else //KAB_EMBEDDED | ||
170 | |||
171 | |||
172 | if ( !XXPortManager::importData.isEmpty() ) | ||
173 | addrList = parseVCard( XXPortManager::importData ); | ||
174 | else { | ||
175 | if ( XXPortManager::importURL.isEmpty() ) | ||
176 | { | ||
177 | fileName = KFileDialog::getOpenFileName( QString::null, i18n("Select vCard to Import"), parentWidget() ); | ||
178 | } | ||
179 | else | ||
180 | { | ||
181 | //US url = XXPortManager::importURL; | ||
182 | qDebug("VCardXXPort::importContacts Urls at the moment not supported"); | ||
183 | } | ||
184 | |||
185 | if ( url.isEmpty() ) | ||
186 | return addrList; | ||
187 | |||
188 | QFile file( fileName ); | ||
189 | |||
190 | file.open( IO_ReadOnly ); | ||
191 | QByteArray rawData = file.readAll(); | ||
192 | file.close(); | ||
193 | |||
194 | QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); | ||
195 | addrList = parseVCard( data ); | ||
196 | |||
197 | } | ||
198 | #endif //KAB_EMBEDDED | ||
199 | |||
200 | return addrList; | ||
201 | } | ||
202 | |||
203 | KABC::AddresseeList VCardXXPort::parseVCard( const QString &data ) const | ||
204 | { | ||
205 | KABC::VCardConverter converter; | ||
206 | KABC::AddresseeList addrList; | ||
207 | |||
208 | uint numVCards = data.contains( "BEGIN:VCARD", false ); | ||
209 | QStringList dataList = QStringList::split( "\r\n\r\n", data ); | ||
210 | |||
211 | for ( uint i = 0; i < numVCards && i < dataList.count(); ++i ) { | ||
212 | KABC::Addressee addr; | ||
213 | bool ok = false; | ||
214 | |||
215 | if ( dataList[ i ].contains( "VERSION:3.0" ) ) | ||
216 | ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v3_0 ); | ||
217 | else if ( dataList[ i ].contains( "VERSION:2.1" ) ) | ||
218 | ok = converter.vCardToAddressee( dataList[ i ], addr, KABC::VCardConverter::v2_1 ); | ||
219 | else { | ||
220 | KMessageBox::sorry( parentWidget(), i18n( "Not supported vCard version." ) ); | ||
221 | continue; | ||
222 | } | ||
223 | |||
224 | if ( !addr.isEmpty() && ok ) | ||
225 | addrList.append( addr ); | ||
226 | else { | ||
227 | QString text = i18n( "The selected file does not include a valid vCard. " | ||
228 | "Please check the file and try again." ); | ||
229 | KMessageBox::sorry( parentWidget(), text ); | ||
230 | } | ||
231 | } | ||
232 | |||
233 | return addrList; | ||
234 | } | ||
235 | |||
236 | #ifndef KAB_EMBEDDED | ||
237 | #include "vcard_xxport.moc" | ||
238 | #endif //KAB_EMBEDDED | ||