• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.12
  • kdepim
  • kaddressbook
  • xxport
  • csv
csv_xxport.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2009 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 along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include "csv_xxport.h"
21 #include "contactfields.h"
22 #include "csvimportdialog.h"
23 
24 #include "pimcommon/widgets/renamefiledialog.h"
25 
26 #include <KFileDialog>
27 #include <KLocale>
28 #include <KMessageBox>
29 #include <KTemporaryFile>
30 #include <KUrl>
31 #include <KIO/NetAccess>
32 
33 #include <QtCore/QPointer>
34 #include <QtCore/QTextCodec>
35 #include <QtCore/QTextStream>
36 
37 CsvXXPort::CsvXXPort( QWidget *parent )
38  : XXPort( parent )
39 {
40 }
41 
42 bool CsvXXPort::exportContacts( const KABC::Addressee::List &contacts ) const
43 {
44  KUrl url = KFileDialog::getSaveUrl( KUrl( QLatin1String("addressbook.csv") ) );
45  if ( url.isEmpty() ) {
46  return true;
47  }
48 
49  if ( QFileInfo( url.isLocalFile() ? url.toLocalFile() : url.path() ).exists() ) {
50  if ( url.isLocalFile() && QFileInfo( url.toLocalFile() ).exists() ) {
51  PimCommon::RenameFileDialog::RenameFileDialogResult result = PimCommon::RenameFileDialog::RENAMEFILE_IGNORE;
52  PimCommon::RenameFileDialog *dialog = new PimCommon::RenameFileDialog(url, false, parentWidget());
53  result = static_cast<PimCommon::RenameFileDialog::RenameFileDialogResult>(dialog->exec());
54  if ( result == PimCommon::RenameFileDialog::RENAMEFILE_RENAME ) {
55  url = dialog->newName();
56  } else if (result == PimCommon::RenameFileDialog::RENAMEFILE_IGNORE) {
57  delete dialog;
58  return true;
59  }
60  delete dialog;
61  }
62  }
63 
64  if ( !url.isLocalFile() ) {
65  KTemporaryFile tmpFile;
66  if ( !tmpFile.open() ) {
67  const QString msg = i18n( "<qt>Unable to open file <b>%1</b></qt>", url.url() );
68  KMessageBox::error( parentWidget(), msg );
69  return false;
70  }
71 
72  exportToFile( &tmpFile, contacts );
73  tmpFile.flush();
74 
75  return KIO::NetAccess::upload( tmpFile.fileName(), url, parentWidget() );
76 
77  } else {
78  QFile file( url.toLocalFile() );
79  if ( !file.open( QIODevice::WriteOnly ) ) {
80  const QString msg = i18n( "<qt>Unable to open file <b>%1</b>.</qt>", url.toLocalFile() );
81  KMessageBox::error( parentWidget(), msg );
82  return false;
83  }
84 
85  exportToFile( &file, contacts );
86  file.close();
87 
88  return true;
89  }
90 }
91 
92 void CsvXXPort::exportToFile( QFile *file, const KABC::Addressee::List &contacts ) const
93 {
94  QTextStream stream( file );
95  stream.setCodec( QTextCodec::codecForLocale() );
96 
97  ContactFields::Fields fields = ContactFields::allFields();
98  fields.remove( ContactFields::Undefined );
99 
100  bool first = true;
101 
102  // First output the column headings
103  for ( int i = 0; i < fields.count(); ++i ) {
104  if ( !first ) {
105  stream << ",";
106  }
107 
108  // add quoting as defined in RFC 4180
109  QString label = ContactFields::label( fields.at( i ) );
110  label.replace( QLatin1Char( '"' ), QLatin1String( "\"\"" ) );
111 
112  stream << "\"" << label << "\"";
113  first = false;
114  }
115  stream << "\n";
116 
117  // Then all the contacts
118  for ( int i = 0; i < contacts.count(); ++i ) {
119 
120  const KABC::Addressee contact = contacts.at( i );
121  first = true;
122 
123  for ( int j = 0; j < fields.count(); ++j ) {
124  if ( !first ) {
125  stream << ",";
126  }
127 
128  QString content;
129  if ( fields.at( j ) == ContactFields::Birthday ||
130  fields.at( j ) == ContactFields::Anniversary ) {
131  const QDateTime dateTime =
132  QDateTime::fromString( ContactFields::value( fields.at( j ), contact ), Qt::ISODate );
133  if ( dateTime.isValid() ) {
134  content = dateTime.date().toString( Qt::ISODate );
135  }
136  } else {
137  content = ContactFields::value( fields.at( j ), contact ).replace( QLatin1Char('\n'), QLatin1String("\\n") );
138  }
139 
140  // add quoting as defined in RFC 4180
141  content.replace( QLatin1Char( '"' ), QLatin1String( "\"\"" ) );
142 
143  stream << '\"' << content << '\"';
144  first = false;
145  }
146 
147  stream << "\n";
148  }
149 }
150 
151 KABC::Addressee::List CsvXXPort::importContacts() const
152 {
153  KABC::Addressee::List contacts;
154 
155  QPointer<CSVImportDialog> dlg = new CSVImportDialog( parentWidget() );
156  if ( dlg->exec() && dlg ) {
157  contacts = dlg->contacts();
158  }
159 
160  delete dlg;
161 
162  return contacts;
163 }
ContactFields::Undefined
Definition: contactfields.h:34
CsvXXPort::CsvXXPort
CsvXXPort(QWidget *parent=0)
Definition: csv_xxport.cpp:37
contactfields.h
csv_xxport.h
XXPort::parentWidget
QWidget * parentWidget() const
Returns the parent widget that can be used as parent for GUI components.
Definition: xxport.cpp:51
ContactFields::Birthday
Definition: contactfields.h:44
CsvXXPort::importContacts
KABC::Addressee::List importContacts() const
Imports a list of contacts.
Definition: csv_xxport.cpp:151
ContactFields::Fields
QVector< Field > Fields
Defines a list of Field enums.
Definition: contactfields.h:95
ContactFields::label
static QString label(Field field)
Returns the i18n label for the field.
Definition: contactfields.cpp:25
XXPort
The base class for all import/export modules.
Definition: xxport.h:32
csvimportdialog.h
CSVImportDialog
Definition: csvimportdialog.h:40
ContactFields::value
static QString value(Field field, const KABC::Addressee &contact)
Returns the value for the field of the contact.
Definition: contactfields.cpp:480
ContactFields::Anniversary
Definition: contactfields.h:45
CsvXXPort::exportContacts
bool exportContacts(const KABC::Addressee::List &contacts) const
Exports the list of contacts.
Definition: csv_xxport.cpp:42
ContactFields::allFields
static Fields allFields()
Returns a list of all available fields.
Definition: contactfields.cpp:177
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal