Diffstat (limited to 'kaddressbook/xxport/csv_xxport.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | kaddressbook/xxport/csv_xxport.cpp | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/kaddressbook/xxport/csv_xxport.cpp b/kaddressbook/xxport/csv_xxport.cpp new file mode 100644 index 0000000..ce35053 --- a/dev/null +++ b/kaddressbook/xxport/csv_xxport.cpp | |||
@@ -0,0 +1,187 @@ | |||
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 <qregexp.h> | ||
33 | #include <qtextstream.h> | ||
34 | |||
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 "csvimportdialog.h" | ||
46 | |||
47 | #include "csv_xxport.h" | ||
48 | |||
49 | |||
50 | #ifndef KAB_EMBEDDED | ||
51 | |||
52 | class CSVXXPortFactory : public XXPortFactory | ||
53 | { | ||
54 | public: | ||
55 | XXPortObject *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name ) | ||
56 | { | ||
57 | return new CSVXXPort( ab, parent, name ); | ||
58 | } | ||
59 | }; | ||
60 | |||
61 | |||
62 | extern "C" | ||
63 | { | ||
64 | void *init_libkaddrbk_csv_xxport() | ||
65 | { | ||
66 | return ( new CSVXXPortFactory() ); | ||
67 | } | ||
68 | } | ||
69 | #endif //KAB_EMBEDDED | ||
70 | |||
71 | |||
72 | CSVXXPort::CSVXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name ) | ||
73 | : XXPortObject( ab, parent, name ) | ||
74 | { | ||
75 | createImportAction( i18n( "Import CSV List..." ) ); | ||
76 | createExportAction( i18n( "Export CSV List..." ) ); | ||
77 | } | ||
78 | |||
79 | bool CSVXXPort::exportContacts( const KABC::AddresseeList &list, const QString& ) | ||
80 | { | ||
81 | #ifndef KAB_EMBEDDED | ||
82 | KURL url = KFileDialog::getSaveURL( "addressbook.csv" ); | ||
83 | if ( url.isEmpty() ) | ||
84 | return true; | ||
85 | |||
86 | if ( !url.isLocalFile() ) { | ||
87 | KTempFile tmpFile; | ||
88 | if ( tmpFile.status() != 0 ) { | ||
89 | QString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" ); | ||
90 | KMessageBox::error( parentWidget(), txt.arg( url.url() ) | ||
91 | .arg( strerror( tmpFile.status() ) ) ); | ||
92 | return false; | ||
93 | } | ||
94 | |||
95 | doExport( tmpFile.file(), list ); | ||
96 | tmpFile.close(); | ||
97 | |||
98 | return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() ); | ||
99 | } else { | ||
100 | QFile file( url.path() ); | ||
101 | if ( !file.open( IO_WriteOnly ) ) { | ||
102 | QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ); | ||
103 | KMessageBox::error( parentWidget(), txt.arg( url.path() ) ); | ||
104 | return false; | ||
105 | } | ||
106 | |||
107 | doExport( &file, list ); | ||
108 | file.close(); | ||
109 | |||
110 | return true; | ||
111 | } | ||
112 | |||
113 | #else //KAB_EMBEDDED | ||
114 | |||
115 | QString fileName = KFileDialog::getSaveFileName( "addressbook.csv", i18n("Save file"), parentWidget() ); | ||
116 | |||
117 | if ( fileName.isEmpty() ) | ||
118 | return true; | ||
119 | |||
120 | QFile file( fileName ); | ||
121 | if ( !file.open( IO_WriteOnly ) ) { | ||
122 | QString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" ); | ||
123 | KMessageBox::error( parentWidget(), txt.arg( fileName ) ); | ||
124 | return false; | ||
125 | } | ||
126 | |||
127 | doExport( &file, list ); | ||
128 | file.close(); | ||
129 | |||
130 | return true; | ||
131 | |||
132 | |||
133 | #endif //KAB_EMBEDDED | ||
134 | |||
135 | } | ||
136 | |||
137 | KABC::AddresseeList CSVXXPort::importContacts( const QString& ) const | ||
138 | { | ||
139 | CSVImportDialog dlg( addressBook(), parentWidget() ); | ||
140 | if ( dlg.exec() ) | ||
141 | return dlg.contacts(); | ||
142 | else | ||
143 | return KABC::AddresseeList(); | ||
144 | } | ||
145 | |||
146 | void CSVXXPort::doExport( QFile *fp, const KABC::AddresseeList &list ) | ||
147 | { | ||
148 | QTextStream t( fp ); | ||
149 | |||
150 | KABC::AddresseeList::ConstIterator iter; | ||
151 | KABC::Field::List fields = addressBook()->fields(); | ||
152 | KABC::Field::List::Iterator fieldIter; | ||
153 | bool first = true; | ||
154 | |||
155 | // First output the column headings | ||
156 | for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) { | ||
157 | if ( !first ) | ||
158 | t << ","; | ||
159 | |||
160 | t << "\"" << (*fieldIter)->label() << "\""; | ||
161 | first = false; | ||
162 | } | ||
163 | t << "\n"; | ||
164 | |||
165 | // Then all the addressee objects | ||
166 | KABC::Addressee addr; | ||
167 | for ( iter = list.begin(); iter != list.end(); ++iter ) { | ||
168 | addr = *iter; | ||
169 | first = true; | ||
170 | |||
171 | for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) { | ||
172 | if ( !first ) | ||
173 | t << ","; | ||
174 | |||
175 | t << "\"" << (*fieldIter)->value( addr ).replace( QRegExp("\n"), "\\n" ) << "\""; | ||
176 | first = false; | ||
177 | } | ||
178 | |||
179 | t << "\n"; | ||
180 | } | ||
181 | } | ||
182 | |||
183 | #ifndef KAB_EMBEDDED | ||
184 | #include "csv_xxport.moc" | ||
185 | #endif //KAB_EMBEDDED | ||
186 | |||
187 | |||