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