-rw-r--r-- | kabc/addressbook.cpp | 70 |
1 files changed, 64 insertions, 6 deletions
diff --git a/kabc/addressbook.cpp b/kabc/addressbook.cpp index 5fb49eb..295cf03 100644 --- a/kabc/addressbook.cpp +++ b/kabc/addressbook.cpp @@ -9,71 +9,76 @@ This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ /* Enhanced Version of the file for platform independent KDE tools. Copyright (c) 2004 Ulf Schenk $Id$ */ /*US #include <qfile.h> #include <qregexp.h> #include <qtimer.h> #include <kapplication.h> #include <kinstance.h> #include <kstandarddirs.h> #include "errorhandler.h" */ #include <qptrlist.h> +#include <qtextstream.h> +#include <qfile.h> #include <kglobal.h> -#include <klocale.h> +#include <klocale.h>> +#include <kmessagebox.h> #include <kdebug.h> #include <libkcal/syncdefines.h> #include "addressbook.h" #include "resource.h" +#include "vcardconverter.h" +#include "vcardparser/vcardtool.h" //US #include "addressbook.moc" using namespace KABC; struct AddressBook::AddressBookData { Addressee::List mAddressees; Addressee::List mRemovedAddressees; Field::List mAllFields; KConfig *mConfig; KRES::Manager<Resource> *mManager; //US ErrorHandler *mErrorHandler; }; struct AddressBook::Iterator::IteratorData { Addressee::List::Iterator mIt; }; struct AddressBook::ConstIterator::ConstIteratorData { Addressee::List::ConstIterator mIt; }; AddressBook::Iterator::Iterator() { d = new IteratorData; } AddressBook::Iterator::Iterator( const AddressBook::Iterator &i ) { @@ -341,64 +346,112 @@ bool AddressBook::load() bool ok = true; for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) if ( !(*it)->load() ) { error( i18n("Unable to load resource '%1'").arg( (*it)->resourceName() ) ); ok = false; } // mark all addressees as unchanged Addressee::List::Iterator addrIt; for ( addrIt = d->mAddressees.begin(); addrIt != d->mAddressees.end(); ++addrIt ) { (*addrIt).setChanged( false ); QString id = (*addrIt).custom( "KADDRESSBOOK", "X-ExternalID" ); if ( !id.isEmpty() ) { //qDebug("setId aa %s ", id.latin1()); (*addrIt).setIDStr(id ); } } blockLSEchange = true; return ok; } bool AddressBook::save( Ticket *ticket ) { kdDebug(5700) << "AddressBook::save()"<< endl; if ( ticket->resource() ) { deleteRemovedAddressees(); return ticket->resource()->save( ticket ); } return false; } +void AddressBook::export2File( QString fileName ) +{ + + QFile outFile( fileName ); + if ( !outFile.open( IO_WriteOnly ) ) { + QString text = i18n( "<qt>Unable to open file <b>%1</b> for export.</qt>" ); + KMessageBox::error( 0, text.arg( fileName ) ); + return ; + } + QTextStream t( &outFile ); + t.setEncoding( QTextStream::UnicodeUTF8 ); + Iterator it; + KABC::VCardConverter::Version version; + version = KABC::VCardConverter::v3_0; + for ( it = begin(); it != end(); ++it ) { + if ( !(*it).IDStr().isEmpty() ) { + (*it).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*it).IDStr() ); + } + KABC::VCardConverter converter; + QString vcard; + //Resource *resource() const; + converter.addresseeToVCard( *it, vcard, version ); + t << vcard << "\r\n"; + } + outFile.close(); +} +void AddressBook::importFromFile( QString fileName ) +{ + + KABC::Addressee::List list; + QFile file( fileName ); + + file.open( IO_ReadOnly ); + QByteArray rawData = file.readAll(); + file.close(); + + QString data = QString::fromUtf8( rawData.data(), rawData.size() + 1 ); + KABC::VCardTool tool; + list = tool.parseVCards( data ); + + KABC::Addressee::List::Iterator it; + for ( it = list.begin(); it != list.end(); ++it ) { + (*it).setResource( 0 ); + insertAddressee( (*it), false, true ); + } + +} + bool AddressBook::saveAB() { bool ok = true; deleteRemovedAddressees(); Iterator ait; for ( ait = begin(); ait != end(); ++ait ) { if ( !(*ait).IDStr().isEmpty() ) { (*ait).insertCustom( "KADDRESSBOOK", "X-ExternalID", (*ait).IDStr() ); } } KRES::Manager<Resource>::ActiveIterator it; KRES::Manager<Resource> *manager = d->mManager; for ( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) { if ( !(*it)->readOnly() && (*it)->isOpen() ) { Ticket *ticket = requestSaveTicket( *it ); // qDebug("StdAddressBook::save '%s'", (*it)->resourceName().latin1() ); if ( !ticket ) { error( i18n( "Unable to save to resource '%1'. It is locked." ) .arg( (*it)->resourceName() ) ); return false; } //if ( !save( ticket ) ) if ( ticket->resource() ) { if ( ! ticket->resource()->save( ticket ) ) ok = false; } else ok = false; } } @@ -432,85 +485,90 @@ AddressBook::ConstIterator AddressBook::end() const it.d->mIt = d->mAddressees.end(); return it; } void AddressBook::clear() { d->mAddressees.clear(); } Ticket *AddressBook::requestSaveTicket( Resource *resource ) { kdDebug(5700) << "AddressBook::requestSaveTicket()" << endl; if ( !resource ) { qDebug("AddressBook::requestSaveTicket no resource" ); resource = standardResource(); } KRES::Manager<Resource>::ActiveIterator it; for ( it = d->mManager->activeBegin(); it != d->mManager->activeEnd(); ++it ) { if ( (*it) == resource ) { if ( (*it)->readOnly() || !(*it)->isOpen() ) return 0; else return (*it)->requestSaveTicket(); } } return 0; } -void AddressBook::insertAddressee( const Addressee &a, bool setRev ) +void AddressBook::insertAddressee( const Addressee &a, bool setRev, bool takeResource ) { if ( blockLSEchange && setRev && a.uid().left( 19 ) == QString("last-syncAddressee-") ) { //qDebug("block insert "); return; } //qDebug("inserting.... %s ",a.uid().latin1() ); bool found = false; Addressee::List::Iterator it; for ( it = d->mAddressees.begin(); it != d->mAddressees.end(); ++it ) { if ( a.uid() == (*it).uid() ) { bool changed = false; Addressee addr = a; if ( addr != (*it) ) changed = true; - (*it) = a; - if ( (*it).resource() == 0 ) - (*it).setResource( standardResource() ); - + if ( takeResource ) { + Resource * res = (*it).resource(); + (*it) = a; + (*it).setResource( res ); + } else { + (*it) = a; + if ( (*it).resource() == 0 ) + (*it).setResource( standardResource() ); + } if ( changed ) { if ( setRev ) { // get rid of micro seconds QDateTime dt = QDateTime::currentDateTime(); QTime t = dt.time(); dt.setTime( QTime (t.hour (), t.minute (), t.second () ) ); (*it).setRevision( dt ); } (*it).setChanged( true ); } found = true; } else { if ( (*it).uid().left( 19 ) == QString("last-syncAddressee-") ) { QString name = (*it).uid().mid( 19 ); Addressee b = a; QString id = b.getID( name ); if ( ! id.isEmpty() ) { QString des = (*it).note(); int startN; if( (startN = des.find( id ) ) >= 0 ) { int endN = des.find( ",", startN+1 ); des = des.left( startN ) + des.mid( endN+1 ); (*it).setNote( des ); } } } } } if ( found ) return; |