TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
4.4KB

  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. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, permission is given to link this program
  18. with any edition of TQt, and distribute the resulting executable,
  19. without including the source code for TQt in the source distribution.
  20. */
  21. /*
  22. Description:
  23. The LDAP Data Interchange Format (LDIF) is a common ASCII-text based
  24. Internet interchange format. Most programs allow you to export data in
  25. LDIF format and e.g. Netscape and Mozilla store by default their
  26. Personal Address Book files in this format.
  27. This import and export filter reads and writes any LDIF version 1 files
  28. into your KDE Addressbook.
  29. */
  30. #include <tqfile.h>
  31. #include <kfiledialog.h>
  32. #include <kio/netaccess.h>
  33. #include <klocale.h>
  34. #include <kmdcodec.h>
  35. #include <kmessagebox.h>
  36. #include <ktempfile.h>
  37. #include <kurl.h>
  38. #include <kabc/ldifconverter.h>
  39. #include <kdebug.h>
  40. #include "ldif_xxport.h"
  41. K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_ldif_xxport, LDIFXXPort )
  42. LDIFXXPort::LDIFXXPort( KABC::AddressBook *ab, TQWidget *parent, const char *name )
  43. : KAB::XXPort( ab, parent, name )
  44. {
  45. createImportAction( i18n( "Import LDIF Addressbook..." ) );
  46. createExportAction( i18n( "Export LDIF Addressbook..." ) );
  47. }
  48. /* import */
  49. KABC::AddresseeList LDIFXXPort::importContacts( const TQString& ) const
  50. {
  51. KABC::AddresseeList addrList;
  52. TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath(),
  53. "text/x-ldif", 0 );
  54. if ( fileName.isEmpty() )
  55. return addrList;
  56. TQFile file( fileName );
  57. if ( !file.open( IO_ReadOnly ) ) {
  58. TQString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
  59. KMessageBox::error( parentWidget(), msg.tqarg( fileName ) );
  60. return addrList;
  61. }
  62. TQTextStream t( &file );
  63. t.setEncoding( TQTextStream::Latin1 );
  64. TQString wholeFile = t.read();
  65. TQDateTime dtDefault = TQFileInfo(file).lastModified();
  66. file.close();
  67. KABC::LDIFConverter::LDIFToAddressee( wholeFile, addrList, dtDefault );
  68. return addrList;
  69. }
  70. /* export */
  71. bool LDIFXXPort::exportContacts( const KABC::AddresseeList &list, const TQString& )
  72. {
  73. KURL url = KFileDialog::getSaveURL( TQDir::homeDirPath() + "/addressbook.ldif",
  74. "text/x-ldif" );
  75. if ( url.isEmpty() )
  76. return true;
  77. if( TQFileInfo(url.path()).exists() ) {
  78. if(KMessageBox::questionYesNo( parentWidget(), i18n("Do you want to overwrite file \"%1\"").tqarg( url.path()) ) == KMessageBox::No)
  79. return false;
  80. }
  81. if ( !url.isLocalFile() ) {
  82. KTempFile tmpFile;
  83. if ( tmpFile.status() != 0 ) {
  84. TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.%2.</qt>" );
  85. KMessageBox::error( parentWidget(), txt.tqarg( url.url() )
  86. .tqarg( strerror( tmpFile.status() ) ) );
  87. return false;
  88. }
  89. doExport( tmpFile.file(), list );
  90. tmpFile.close();
  91. return KIO::NetAccess::upload( tmpFile.name(), url, parentWidget() );
  92. } else {
  93. TQString filename = url.path();
  94. TQFile file( filename );
  95. if ( !file.open( IO_WriteOnly ) ) {
  96. TQString txt = i18n( "<qt>Unable to open file <b>%1</b>.</qt>" );
  97. KMessageBox::error( parentWidget(), txt.tqarg( filename ) );
  98. return false;
  99. }
  100. doExport( &file, list );
  101. file.close();
  102. return true;
  103. }
  104. }
  105. void LDIFXXPort::doExport( TQFile *fp, const KABC::AddresseeList &list )
  106. {
  107. TQString str;
  108. KABC::LDIFConverter::addresseeToLDIF( list, str );
  109. TQTextStream t( fp );
  110. t.setEncoding( TQTextStream::UnicodeUTF8 );
  111. t << str;
  112. }
  113. #include "ldif_xxport.moc"