bookmarksyncee.cpp
00001
00002
00003 #include <kdebug.h>
00004
00005 #include <kbookmarkmanager.h>
00006
00007 #include "bookmarksyncee.h"
00008
00009 BookmarkSyncEntry::BookmarkSyncEntry(KBookmark bm) :
00010 mBookmark(bm)
00011 {
00012 }
00013
00014 QString BookmarkSyncEntry::name()
00015 {
00016 return mBookmark.text();
00017 }
00018
00019 QString BookmarkSyncEntry::id()
00020 {
00021 return mBookmark.url().url();
00022 }
00023
00024 QString BookmarkSyncEntry::timestamp()
00025 {
00026 return mBookmark.text() + mBookmark.url().url();
00027 }
00028
00029 bool BookmarkSyncEntry::equals(KSyncEntry *entry)
00030 {
00031 BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00032 if (!bmEntry) {
00033 kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00034 return false;
00035 }
00036
00037 KBookmark bm = bmEntry->bookmark();
00038
00039 kdDebug() << "equals: '" << mBookmark.fullText() << "' <-> '"
00040 << bm.fullText() << "'" << endl;
00041
00042 if (mBookmark.fullText() != bmEntry->bookmark().fullText()) return false;
00043 if (mBookmark.url() != bmEntry->bookmark().url()) return false;
00044
00045
00046 return true;
00047 }
00048
00049
00050 BookmarkSyncee::BookmarkSyncee()
00051 {
00052 mBookmarkManager = 0;
00053
00054 mEntries.setAutoDelete(true);
00055 }
00056
00057 BookmarkSyncee::~BookmarkSyncee()
00058 {
00059 delete mBookmarkManager;
00060 }
00061
00062 bool BookmarkSyncee::read()
00063 {
00064 delete mBookmarkManager;
00065 mBookmarkManager = KBookmarkManager::managerForFile( filename() );
00066
00067 mBookmarks.clear();
00068
00069 listGroup(mBookmarkManager->root());
00070
00071 mBookmarkIterator = mBookmarks.begin();
00072
00073 return true;
00074 }
00075
00076 void BookmarkSyncee::listGroup(KBookmarkGroup group)
00077 {
00078 for(KBookmark bm = group.first(); !bm.isNull(); bm = group.next(bm)) {
00079 if (bm.isGroup()) {
00080 listGroup(bm.toGroup());
00081 } else if (bm.isSeparator()) {
00082
00083 } else {
00084 kdDebug() << "appending '" << bm.text() << "' ("
00085 << bm.parentGroup().fullText() << ")" << endl;
00086 mBookmarks.append(bm.internalElement());
00087 }
00088 }
00089 }
00090
00091 bool BookmarkSyncee::write()
00092 {
00093 mBookmarkManager->save();
00094
00095 return true;
00096 }
00097
00098
00099 BookmarkSyncEntry *BookmarkSyncee::firstEntry()
00100 {
00101 mBookmarkIterator = mBookmarks.begin();
00102 return createEntry(KBookmark(*mBookmarkIterator));
00103 }
00104
00105 BookmarkSyncEntry *BookmarkSyncee::nextEntry()
00106 {
00107 return createEntry(KBookmark(*(++mBookmarkIterator)));
00108 }
00109
00110 #if 0
00111 BookmarkSyncEntry *BookmarkSyncee::findEntry(const QString &id)
00112 {
00113 QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00114 while (bmIt != mBookmarks.end()) {
00115 if (KBookmark(*bmIt).url().url() == id) {
00116 return createEntry(KBookmark(*bmIt));
00117 }
00118 ++bmIt;
00119 }
00120
00121 return 0;
00122 }
00123 #endif
00124
00125 void BookmarkSyncee::addEntry(KSyncEntry *entry)
00126 {
00127 BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00128 if (!bmEntry) {
00129 kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00130 } else {
00131 KBookmark bm = bmEntry->bookmark();
00132 KBookmarkGroup bmGroup = findGroup(bm.parentGroup());
00133 KBookmark newBookmark = bmGroup.addBookmark( mBookmarkManager,
00134 bm.fullText(), bm.url() );
00135 mBookmarks.append(newBookmark.internalElement());
00136 }
00137 }
00138
00139 void BookmarkSyncee::removeEntry(KSyncEntry *entry)
00140 {
00141 BookmarkSyncEntry *bmEntry = dynamic_cast<BookmarkSyncEntry *>(entry);
00142 if (!bmEntry) {
00143 kdDebug() << "BookmarkSyncee::addEntry(): Wrong type." << endl;
00144 } else {
00145 KBookmark bm = bmEntry->bookmark();
00146 kdDebug() << "Remove " << bm.text() << endl;
00147
00148
00149
00150
00151
00152
00153 }
00154 }
00155
00156 KBookmarkGroup BookmarkSyncee::findGroup(KBookmarkGroup group)
00157 {
00158 if (group.fullText().isEmpty()) return mBookmarkManager->root();
00159
00160 QValueList<QDomElement>::Iterator bmIt = mBookmarks.begin();
00161 while (bmIt != mBookmarks.end()) {
00162 KBookmark bm(*bmIt);
00163 if (bm.isGroup() && (bm.fullText() == group.fullText())) {
00164 return bm.toGroup();
00165 }
00166 ++bmIt;
00167 }
00168 KBookmarkGroup newGroup =
00169 mBookmarkManager->root().createNewFolder( mBookmarkManager,
00170 group.fullText() );
00171 mBookmarks.append(newGroup.internalElement());
00172
00173 return newGroup;
00174 }
00175
00176 BookmarkSyncEntry *BookmarkSyncee::createEntry(KBookmark bm)
00177 {
00178 if (!bm.isNull()) {
00179 BookmarkSyncEntry *entry = new BookmarkSyncEntry(bm);
00180 mEntries.append(entry);
00181 return entry;
00182 } else {
00183 return 0;
00184 }
00185 }
This file is part of the documentation for kdelibs Version 3.1.5.