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