kaddressbook

csv_xxport.cpp
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 
19  As a special exception, permission is given to link this program
20  with any edition of TQt, and distribute the resulting executable,
21  without including the source code for TQt in the source distribution.
22 */
23 
24 #include <tqfile.h>
25 
26 #include <tdefiledialog.h>
27 #include <tdeio/netaccess.h>
28 #include <tdelocale.h>
29 #include <tdemessagebox.h>
30 #include <tdetempfile.h>
31 #include <kurl.h>
32 
33 #include "csvimportdialog.h"
34 
35 #include "csv_xxport.h"
36 
37 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_csv_xxport, CSVXXPort )
38 
39 CSVXXPort::CSVXXPort( TDEABC::AddressBook *ab, TQWidget *parent, const char *name )
40  : KAB::XXPort( ab, parent, name )
41 {
42  createImportAction( i18n( "Import CSV List..." ) );
43  createExportAction( i18n( "Export CSV List..." ) );
44 }
45 
46 bool CSVXXPort::exportContacts( const TDEABC::AddresseeList &list, const TQString& )
47 {
48  KURL url = KFileDialog::getSaveURL( "addressbook.csv" );
49  if ( url.isEmpty() )
50  return true;
51 
52  if( TQFileInfo(url.path()).exists() ) {
53  if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
54  return false;
55  }
56 
57  if ( !url.isLocalFile() ) {
58  KTempFile tmpFile;
59  if ( tmpFile.status() != 0 ) {
60  TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
61  KMessageBox::error( parentWidget(), txt.arg( url.url() )
62  .arg( strerror( tmpFile.status() ) ) );
63  return false;
64  }
65 
66  doExport( tmpFile.file(), list );
67  tmpFile.close();
68 
69  return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
70  } else {
71  TQFile file( url.path() );
72  if ( !file.open( IO_WriteOnly ) ) {
73  TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
74  KMessageBox::error( parentWidget(), txt.arg( url.path() ) );
75  return false;
76  }
77 
78  doExport( &file, list );
79  file.close();
80 
81  KMessageBox::information( parentWidget(), i18n( "The contacts have been exported successfully." ) );
82 
83  return true;
84  }
85 }
86 
87 TDEABC::AddresseeList CSVXXPort::importContacts( const TQString& ) const
88 {
89  CSVImportDialog dlg( addressBook(), parentWidget() );
90  if ( dlg.exec() )
91  return dlg.contacts();
92  else
93  return TDEABC::AddresseeList();
94 }
95 
96 void CSVXXPort::doExport( TQFile *fp, const TDEABC::AddresseeList &list )
97 {
98  TQTextStream t( fp );
99  t.setEncoding( TQTextStream::Locale );
100 
101  TDEABC::AddresseeList::ConstIterator iter;
102  TDEABC::Field::List fields = addressBook()->fields();
103  TDEABC::Field::List::Iterator fieldIter;
104  bool first = true;
105 
106  // First output the column headings
107  for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) {
108  if ( !first )
109  t << ",";
110 
111  t << "\"" << (*fieldIter)->label() << "\"";
112  first = false;
113  }
114  t << "\n";
115 
116  // Then all the addressee objects
117  TDEABC::Addressee addr;
118  for ( iter = list.begin(); iter != list.end(); ++iter ) {
119  addr = *iter;
120  first = true;
121 
122  for ( fieldIter = fields.begin(); fieldIter != fields.end(); ++fieldIter ) {
123  if ( !first )
124  t << ",";
125 
126  t << "\"" << (*fieldIter)->value( addr ).replace( "\n", "\\n" ) << "\"";
127  first = false;
128  }
129 
130  t << "\n";
131  }
132 }
133 
134 #include "csv_xxport.moc"