ksync

bookmarksyncee.cpp

00001 /*
00002     This file is part of ksync.
00003 
00004     Copyright (c) 2001 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 BookmarkSyncEntry::BookmarkSyncEntry(KBookmark bm) :
00029   mBookmark(bm)
00030 {
00031 }
00032 
00033 QString BookmarkSyncEntry::name()
00034 {
00035   return mBookmark.text();
00036 }
00037 
00038 QString BookmarkSyncEntry::id()
00039 {
00040   return mBookmark.url().url();
00041 }
00042 
00043 QString BookmarkSyncEntry::timestamp()
00044 {
00045   return mBookmark.text() + mBookmark.url().url();
00046 }
00047 
00048 bool BookmarkSyncEntry::equals(KSyncEntry *entry)
00049 {
00050   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00051   if (!bmEntry) {
00052     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00053     return false;
00054   }
00055 
00056   KBookmark bm = bmEntry->bookmark();
00057 
00058   kdDebug() << "equals: '" << mBookmark.fullText() << "' <-> '"
00059             << bm.fullText() << "'" << endl;
00060 
00061   if (mBookmark.fullText() != bmEntry->bookmark().fullText()) return false;
00062   if (mBookmark.url() != bmEntry->bookmark().url()) return false;
00063   // TODO: Compare grouping
00064   
00065   return true;
00066 }
00067 
00068 
00069 BookmarkSyncee::BookmarkSyncee()
00070 {
00071   mBookmarkManager = 0;
00072 
00073   mEntries.setAutoDelete(true);
00074 }
00075 
00076 BookmarkSyncee::~BookmarkSyncee()
00077 {
00078   delete mBookmarkManager;
00079 }
00080 
00081 bool BookmarkSyncee::read()
00082 {
00083   delete mBookmarkManager;
00084   mBookmarkManager = KBookmarkManager::managerForFile( filename() );
00085   
00086   mBookmarks.clear();
00087   
00088   listGroup(mBookmarkManager->root());
00089 
00090   mBookmarkIterator = mBookmarks.begin();
00091 
00092   return true;
00093 }
00094 
00095 void BookmarkSyncee::listGroup(KBookmarkGroup group)
00096 {
00097   for(KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) {
00098     if (bm.isGroup()) {
00099       listGroup(bm.toGroup());
00100     } else if (bm.isSeparator()) {
00101       // Skip separators for now, but these should be synced, too.
00102     } else {
00103       kdDebug() << "appending '" << bm.text() << "' ("
00104                 << bm.parentGroup().fullText() << ")" << endl;
00105       mBookmarks.append(bm.internalElement());
00106     }
00107   }
00108 }
00109 
00110 bool BookmarkSyncee::write()
00111 {
00112   mBookmarkManager->save();
00113 
00114   return true;
00115 }
00116 
00117 
00118 BookmarkSyncEntry *BookmarkSyncee::firstEntry()
00119 {
00120   mBookmarkIterator = mBookmarks.begin();
00121   return createEntry(KBookmark(*mBookmarkIterator));
00122 }
00123 
00124 BookmarkSyncEntry *BookmarkSyncee::nextEntry()
00125 {
00126   return createEntry(KBookmark(*(++mBookmarkIterator)));
00127 }
00128 
00129 #if 0
00130 BookmarkSyncEntry *BookmarkSyncee::findEntry(const QString &id)
00131 {
00132   QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00133   while (bmIt != mBookmarks.end()) {
00134     if (KBookmark(*bmIt).url().url() == id) {
00135       return createEntry(KBookmark(*bmIt));
00136     }
00137     ++bmIt;
00138   }
00139 
00140   return 0;
00141 }
00142 #endif
00143 
00144 void BookmarkSyncee::addEntry(KSyncEntry *entry)
00145 {
00146   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00147   if (!bmEntry) {
00148     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00149   } else {
00150     KBookmark bm = bmEntry->bookmark();
00151     KBookmarkGroup bmGroup = findGroup(bm.parentGroup());
00152     KBookmark newBookmark = bmGroup.addBookmark( mBookmarkManager,
00153                                                  bm.fullText(), bm.url() );
00154     mBookmarks.append(newBookmark.internalElement());
00155   }
00156 }
00157 
00158 void BookmarkSyncee::removeEntry(KSyncEntry *entry)
00159 {
00160   BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00161   if (!bmEntry) {
00162     kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00163   } else {
00164     KBookmark bm = bmEntry->bookmark();
00165     kdDebug() << "Remove " << bm.text() << endl;
00166     // TODO: implement
00167 /*
00168     KBookmarkGroup bmGroup = findGroup(bm.parentGroup());
00169     KBookmark newBookmark = bmGroup.addBookmark(bm.fullText(),bm.url());
00170     mBookmarks.append(newBookmark.internalElement());
00171 */
00172   }
00173 }
00174 
00175 KBookmarkGroup BookmarkSyncee::findGroup(KBookmarkGroup group)
00176 {
00177   if (group.fullText().isEmpty()) return mBookmarkManager->root();
00178 
00179   QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00180   while (bmIt != mBookmarks.end()) {
00181     KBookmark bm(*bmIt);
00182     if (bm.isGroup() && (bm.fullText() == group.fullText())) {
00183       return bm.toGroup();
00184     }
00185     ++bmIt;
00186   }
00187   KBookmarkGroup newGroup =
00188       mBookmarkManager->root().createNewFolder( mBookmarkManager, 
00189                                                 group.fullText() );
00190   mBookmarks.append(newGroup.internalElement());
00191 
00192   return newGroup;
00193 }
00194 
00195 BookmarkSyncEntry *BookmarkSyncee::createEntry(KBookmark bm)
00196 {
00197   if (!bm.isNull()) {
00198     BookmarkSyncEntry *entry = new BookmarkSyncEntry(bm);
00199     mEntries.append(entry);
00200     return entry;    
00201   } else {
00202     return 0;
00203   }
00204 }
KDE Home | KDE Accessibility Home | Description of Access Keys