kitchensync

bookmarksyncee.cpp

00001 /*
00002     This file is part of libksync.
00003 
00004     Copyright (c) 2001,2004 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 
00022 #include <kdebug.h>
00023 
00024 #include <kbookmarkmanager.h>
00025 
00026 #include "bookmarksyncee.h"
00027 
00028 using namespace KSync;
00029 
00030 BookmarkSyncEntry::BookmarkSyncEntry( KBookmark bm, Syncee *parent )
00031   : SyncEntry( parent ), mBookmark( bm )
00032 {
00033   setType( QString::fromLatin1("BookmarkSyncEntry") );
00034 }
00035 
00036 BookmarkSyncEntry::BookmarkSyncEntry( Syncee *parent )
00037   : SyncEntry( parent )
00038 {
00039   setType( QString::fromLatin1("BookmarkSyncEntry") );
00040 }
00041 
00042 QString BookmarkSyncEntry::name()
00043 {
00044   return mBookmark.text();
00045 }
00046 
00047 QString BookmarkSyncEntry::id()
00048 {
00049   return mBookmark.url().url();
00050 }
00051 
00052 QString BookmarkSyncEntry::timestamp()
00053 {
00054   return mBookmark.text() + mBookmark.url().url();
00055 }
00056 
00057 bool BookmarkSyncEntry::equals( SyncEntry *entry )
00058 {
00059   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00060   if (!bmEntry) {
00061     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00062     return false;
00063   }
00064 
00065   KBookmark bm = bmEntry->bookmark();
00066 
00067   kdDebug() << "equals: '" << mBookmark.fullText() << "' <-> '"
00068             << bm.fullText() << "'" << endl;
00069 
00070   if ( mBookmark.fullText() != bmEntry->bookmark().fullText() ) return false;
00071   if ( mBookmark.url() != bmEntry->bookmark().url() ) return false;
00072   // TODO: Compare grouping
00073 
00074   return true;
00075 }
00076 
00077 BookmarkSyncEntry *BookmarkSyncEntry::clone()
00078 {
00079   return new BookmarkSyncEntry( *this );
00080 }
00081 
00082 void BookmarkSyncEntry::setBookmark( const KBookmark& bk )
00083 {
00084     mBookmark = bk;
00085 }
00086 
00087 BookmarkSyncee::BookmarkSyncee( Merger* m)
00088   : Syncee(m)
00089 {
00090   setType( QString::fromLatin1("BookmarkSyncee") );
00091   mBookmarkManager = 0;
00092   mOwnBookmarkManager = true;
00093 
00094   init();
00095 }
00096 
00097 BookmarkSyncee::BookmarkSyncee( KBookmarkManager *bmm ,  Merger* m)
00098   : Syncee( m )
00099 {
00100   setType( QString::fromLatin1("BookmarkSyncee") );
00101   mBookmarkManager = bmm;
00102   mOwnBookmarkManager = false;
00103 
00104   init();
00105 }
00106 
00107 BookmarkSyncee::~BookmarkSyncee()
00108 {
00109   if ( mOwnBookmarkManager ) delete mBookmarkManager;
00110 
00111   /* clear the created and owned sync entries */
00112   for(QMap<QString,BookmarkSyncEntry*>::Iterator it = mEntries.begin(); it != mEntries.end(); ++it)
00113     delete it.data();
00114 
00115 }
00116 
00117 void BookmarkSyncee::init()
00118 {
00119   mBookmarks.clear();
00120 
00121   listGroup( mBookmarkManager->root() );
00122 
00123   mBookmarkIterator = mBookmarks.begin();
00124 }
00125 
00126 void BookmarkSyncee::listGroup( KBookmarkGroup group )
00127 {
00128   for( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) ) {
00129     if ( bm.isGroup() ) {
00130       listGroup( bm.toGroup() );
00131     } else if ( bm.isSeparator() ) {
00132       // Skip separators for now, but these should be synced, too.
00133     } else {
00134       kdDebug() << "appending '" << bm.text() << "' ("
00135                 << bm.parentGroup().fullText() << ")" << endl;
00136       mBookmarks.append( bm.internalElement() );
00137     }
00138   }
00139 }
00140 
00141 BookmarkSyncEntry *BookmarkSyncee::firstEntry()
00142 {
00143   mBookmarkIterator = mBookmarks.begin();
00144   return createEntry( KBookmark( *mBookmarkIterator ) );
00145 }
00146 
00147 BookmarkSyncEntry *BookmarkSyncee::nextEntry()
00148 {
00149   return createEntry( KBookmark( *( ++mBookmarkIterator ) ) );
00150 }
00151 
00152 void BookmarkSyncee::addEntry( SyncEntry *entry )
00153 {
00154   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>( entry );
00155   if ( !bmEntry ) {
00156     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00157   } else {
00158     KBookmark bm = bmEntry->bookmark();
00159     KBookmarkGroup bmGroup = findGroup( bm.parentGroup() );
00160     KBookmark newBookmark = bmGroup.addBookmark( mBookmarkManager,
00161                                                  bm.fullText(), bm.url() );
00162 
00163     bmEntry->setBookmark( newBookmark );
00164     bmEntry->setSyncee( this );
00165     mBookmarks.append( newBookmark.internalElement() );
00166     mEntries.insert(bmEntry->id(), bmEntry );
00167   }
00168 }
00169 
00170 void BookmarkSyncee::removeEntry( SyncEntry *entry )
00171 {
00172   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>( entry );
00173   if ( !bmEntry ) {
00174     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00175   } else {
00176     KBookmark bm = bmEntry->bookmark();
00177     kdDebug() << "Remove " << bm.text() << endl;
00178 
00179     bmEntry->setSyncee( 0 );
00180     mEntries.remove(bmEntry->id() );
00181     /* don't delete bmEntry here */
00182 
00183     // TODO: implement
00184 /*
00185     KBookmarkGroup bmGroup = findGroup(bm.parentGroup());
00186     KBookmark newBookmark = bmGroup.addBookmark(bm.fullText(),bm.url());
00187     mBookmarks.append(newBookmark.internalElement());
00188 */
00189   }
00190 }
00191 
00192 KBookmarkGroup BookmarkSyncee::findGroup( KBookmarkGroup group )
00193 {
00194   if ( group.fullText().isEmpty() ) return mBookmarkManager->root();
00195 
00196   QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00197   while ( bmIt != mBookmarks.end() ) {
00198     KBookmark bm( *bmIt );
00199     if ( bm.isGroup() && ( bm.fullText() == group.fullText() ) ) {
00200       return bm.toGroup();
00201     }
00202     ++bmIt;
00203   }
00204   KBookmarkGroup newGroup =
00205       mBookmarkManager->root().createNewFolder( mBookmarkManager,
00206                                                 group.fullText() );
00207   mBookmarks.append( newGroup.internalElement() );
00208 
00209   return newGroup;
00210 }
00211 
00212 BookmarkSyncEntry *BookmarkSyncee::createEntry( KBookmark bm )
00213 {
00214   if ( !bm.isNull() ) {
00215     if( !mEntries.contains( bm.url().url() ) ){
00216         BookmarkSyncEntry *entry = new BookmarkSyncEntry( bm, this );
00217     mEntries.insert( entry->id(), entry );
00218     }
00219     return mEntries[bm.url().url()];
00220   } else {
00221     return 0;
00222   }
00223 }
00224 
00225 bool BookmarkSyncee::writeBackup( const QString & )
00226 {
00227   return false;
00228 }
00229 
00230 bool BookmarkSyncee::restoreBackup( const QString & )
00231 {
00232   return false;
00233 }
KDE Home | KDE Accessibility Home | Description of Access Keys