kaddressbook

ldif_xxport.cpp
1 /*
2  This file is part of KAddressbook.
3  Copyright (c) 2000 - 2002 Oliver Strutynski <olistrut@gmx.de>
4  Copyright (c) 2002 - 2003 Helge Deller <deller@kde.org>
5  Tobias Koenig <tokoe@kde.org>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 /*
27  Description:
28  The LDAP Data Interchange Format (LDIF) is a common ASCII-text based
29  Internet interchange format. Most programs allow you to export data in
30  LDIF format and e.g. Netscape and Mozilla store by default their
31  Personal Address Book files in this format.
32  This import and export filter reads and writes any LDIF version 1 files
33  into your KDE Addressbook.
34 */
35 
36 #include <tqfile.h>
37 
38 #include <tdefiledialog.h>
39 #include <tdeio/netaccess.h>
40 #include <tdelocale.h>
41 #include <kmdcodec.h>
42 #include <tdemessagebox.h>
43 #include <tdetempfile.h>
44 #include <kurl.h>
45 #include <tdeabc/ldifconverter.h>
46 
47 #include <kdebug.h>
48 
49 #include "ldif_xxport.h"
50 
51 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_ldif_xxport, LDIFXXPort )
52 
53 LDIFXXPort::LDIFXXPort( TDEABC::AddressBook *ab, TQWidget *parent, const char *name )
54  : KAB::XXPort( ab, parent, name )
55 {
56  createImportAction( i18n( "Import LDIF Addressbook..." ) );
57  createExportAction( i18n( "Export LDIF Addressbook..." ) );
58 }
59 
60 /* import */
61 
62 TDEABC::AddresseeList LDIFXXPort::importContacts( const TQString& ) const
63 {
64  TDEABC::AddresseeList addrList;
65 
66  TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath(),
67  "text/x-ldif", 0 );
68  if ( fileName.isEmpty() )
69  return addrList;
70 
71  TQFile file( fileName );
72  if ( !file.open( IO_ReadOnly ) ) {
73  TQString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
74  KMessageBox::error( parentWidget(), msg.arg( fileName ) );
75  return addrList;
76  }
77 
78  TQTextStream t( &file );
79  t.setEncoding( TQTextStream::Latin1 );
80  TQString wholeFile = t.read();
81  TQDateTime dtDefault = TQFileInfo(file).lastModified();
82  file.close();
83 
84  TDEABC::LDIFConverter::LDIFToAddressee( wholeFile, addrList, dtDefault );
85 
86  return addrList;
87 }
88 
89 
90 /* export */
91 
92 bool LDIFXXPort::exportContacts( const TDEABC::AddresseeList &list, const TQString& )
93 {
94  KURL url = KFileDialog::getSaveURL( TQDir::homeDirPath() + "/addressbook.ldif",
95  "text/x-ldif" );
96  if ( url.isEmpty() )
97  return true;
98 
99  if( TQFileInfo(url.path()).exists() ) {
100  if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").arg( url.path()) ) == KMessageBox::No)
101  return false;
102  }
103 
104 
105  if ( !url.isLocalFile() ) {
106  KTempFile tmpFile;
107  if ( tmpFile.status() != 0 ) {
108  TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
109  KMessageBox::error( parentWidget(), txt.arg( url.url() )
110  .arg( strerror( tmpFile.status() ) ) );
111  return false;
112  }
113 
114  doExport( tmpFile.file(), list );
115  tmpFile.close();
116 
117  return TDEIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
118  } else {
119  TQString filename = url.path();
120  TQFile file( filename );
121 
122  if ( !file.open( IO_WriteOnly ) ) {
123  TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
124  KMessageBox::error( parentWidget(), txt.arg( filename ) );
125  return false;
126  }
127 
128  doExport( &file, list );
129  file.close();
130 
131  return true;
132  }
133 }
134 
135 void LDIFXXPort::doExport( TQFile *fp, const TDEABC::AddresseeList &list )
136 {
137  TQString str;
138  TDEABC::LDIFConverter::addresseeToLDIF( list, str );
139 
140  TQTextStream t( fp );
141  t.setEncoding( TQTextStream::UnicodeUTF8 );
142  t << str;
143 }
144 
145 #include "ldif_xxport.moc"
146