kitchensync Library API Documentation

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., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, 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 }
00034 
00035 QString BookmarkSyncEntry::type() const
00036 {
00037   return "BookmarkSyncEntry";
00038 }
00039 
00040 QString BookmarkSyncEntry::name()
00041 {
00042   return mBookmark.text();
00043 }
00044 
00045 QString BookmarkSyncEntry::id()
00046 {
00047   return mBookmark.url().url();
00048 }
00049 
00050 QString BookmarkSyncEntry::timestamp()
00051 {
00052   return mBookmark.text() + mBookmark.url().url();
00053 }
00054 
00055 bool BookmarkSyncEntry::equals( SyncEntry *entry )
00056 {
00057   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00058   if (!bmEntry) {
00059     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00060     return false;
00061   }
00062 
00063   KBookmark bm = bmEntry->bookmark();
00064 
00065   kdDebug() << "equals: '" << mBookmark.fullText() << "' <-> '"
00066             << bm.fullText() << "'" << endl;
00067 
00068   if ( mBookmark.fullText() != bmEntry->bookmark().fullText() ) return false;
00069   if ( mBookmark.url() != bmEntry->bookmark().url() ) return false;
00070   // TODO: Compare grouping
00071   
00072   return true;
00073 }
00074 
00075 BookmarkSyncEntry *BookmarkSyncEntry::clone()
00076 {
00077   return new BookmarkSyncEntry( *this );
00078 }
00079 
00080 
00081 BookmarkSyncee::BookmarkSyncee()
00082 {
00083   mBookmarkManager = 0;
00084   mOwnBookmarkManager = true;
00085 
00086   init();
00087 }
00088 
00089 BookmarkSyncee::BookmarkSyncee( KBookmarkManager *bmm )
00090 {
00091   mBookmarkManager = bmm;
00092   mOwnBookmarkManager = false;
00093 
00094   init();
00095 }
00096 
00097 BookmarkSyncee::~BookmarkSyncee()
00098 {
00099   if ( mOwnBookmarkManager ) delete mBookmarkManager;
00100 }
00101 
00102 void BookmarkSyncee::init()
00103 {
00104   mEntries.setAutoDelete( true );
00105 
00106   mBookmarks.clear();
00107   
00108   listGroup( mBookmarkManager->root() );
00109 
00110   mBookmarkIterator = mBookmarks.begin();
00111 }
00112 
00113 void BookmarkSyncee::listGroup( KBookmarkGroup group )
00114 {
00115   for( KBookmark bm = group.first(); !bm.isNull(); bm = group.next( bm ) ) {
00116     if ( bm.isGroup() ) {
00117       listGroup( bm.toGroup() );
00118     } else if ( bm.isSeparator() ) {
00119       // Skip separators for now, but these should be synced, too.
00120     } else {
00121       kdDebug() << "appending '" << bm.text() << "' ("
00122                 << bm.parentGroup().fullText() << ")" << endl;
00123       mBookmarks.append( bm.internalElement() );
00124     }
00125   }
00126 }
00127 
00128 BookmarkSyncEntry *BookmarkSyncee::firstEntry()
00129 {
00130   mBookmarkIterator = mBookmarks.begin();
00131   return createEntry( KBookmark( *mBookmarkIterator ) );
00132 }
00133 
00134 BookmarkSyncEntry *BookmarkSyncee::nextEntry()
00135 {
00136   return createEntry( KBookmark( *( ++mBookmarkIterator ) ) );
00137 }
00138 
00139 #if 0
00140 BookmarkSyncEntry *BookmarkSyncee::findEntry( const QString &id )
00141 {
00142   QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00143   while ( bmIt != mBookmarks.end() ) {
00144     if ( KBookmark( *bmIt ).url().url() == id ) {
00145       return createEntry( KBookmark( *bmIt ) );
00146     }
00147     ++bmIt;
00148   }
00149 
00150   return 0;
00151 }
00152 #endif
00153 
00154 void BookmarkSyncee::addEntry( SyncEntry *entry )
00155 {
00156   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>( entry );
00157   if ( !bmEntry ) {
00158     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00159   } else {
00160     KBookmark bm = bmEntry->bookmark();
00161     KBookmarkGroup bmGroup = findGroup( bm.parentGroup() );
00162     KBookmark newBookmark = bmGroup.addBookmark( mBookmarkManager,
00163                                                  bm.fullText(), bm.url() );
00164     mBookmarks.append( newBookmark.internalElement() );
00165   }
00166 }
00167 
00168 void BookmarkSyncee::removeEntry( SyncEntry *entry )
00169 {
00170   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>( entry );
00171   if ( !bmEntry ) {
00172     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00173   } else {
00174     KBookmark bm = bmEntry->bookmark();
00175     kdDebug() << "Remove " << bm.text() << endl;
00176     // TODO: implement
00177 /*
00178     KBookmarkGroup bmGroup = findGroup(bm.parentGroup());
00179     KBookmark newBookmark = bmGroup.addBookmark(bm.fullText(),bm.url());
00180     mBookmarks.append(newBookmark.internalElement());
00181 */
00182   }
00183 }
00184 
00185 KBookmarkGroup BookmarkSyncee::findGroup( KBookmarkGroup group )
00186 {
00187   if ( group.fullText().isEmpty() ) return mBookmarkManager->root();
00188 
00189   QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00190   while ( bmIt != mBookmarks.end() ) {
00191     KBookmark bm( *bmIt );
00192     if ( bm.isGroup() && ( bm.fullText() == group.fullText() ) ) {
00193       return bm.toGroup();
00194     }
00195     ++bmIt;
00196   }
00197   KBookmarkGroup newGroup =
00198       mBookmarkManager->root().createNewFolder( mBookmarkManager, 
00199                                                 group.fullText() );
00200   mBookmarks.append( newGroup.internalElement() );
00201 
00202   return newGroup;
00203 }
00204 
00205 BookmarkSyncEntry *BookmarkSyncee::createEntry( KBookmark bm )
00206 {
00207   if ( !bm.isNull() ) {
00208     BookmarkSyncEntry *entry = new BookmarkSyncEntry( bm, this );
00209     mEntries.append( entry );
00210     return entry;    
00211   } else {
00212     return 0;
00213   }
00214 }
00215 
00216 bool BookmarkSyncee::writeBackup( const QString & )
00217 {
00218   return false;
00219 }
00220 
00221 bool BookmarkSyncee::restoreBackup( const QString & )
00222 {
00223   return false;
00224 }
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:23:39 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003