• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.3 API Reference
  • KDE Home
  • Contact Us
 

KFile

kfileplacessharedbookmarks.cpp
Go to the documentation of this file.
00001 /*  This file is part of the KDE project
00002     Copyright (C) 2008 Norbert Frese <nf2@scheinwelt.at>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License version 2 as published by the Free Software Foundation.
00007 
00008     This library is distributed in the hope that it will be useful,
00009     but WITHOUT ANY WARRANTY; without even the implied warranty of
00010     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011     Library General Public License for more details.
00012 
00013     You should have received a copy of the GNU Library General Public License
00014     along with this library; see the file COPYING.LIB.  If not, write to
00015     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016     Boston, MA 02110-1301, USA.
00017 
00018 */
00019 
00020 #include "kfileplacessharedbookmarks_p.h"
00021 
00022 #include <QtCore/QObject>
00023 #include <QtCore/QTextStream>
00024 #include <QtCore/QFile>
00025 #include <kstandarddirs.h>
00026 #include <kbookmarkmanager.h>
00027 #include <kbookmark.h>
00028 #include <kdebug.h>
00029 
00031 
00032 static bool compareBookmarks(const KBookmark & bookmark1, const KBookmark & bookmark2)
00033 {
00034     return (bookmark1.url() == bookmark2.url() || bookmark1.text() == bookmark2.text());
00035 }
00036 
00037 static bool deepCompareDomNodes(const QDomNode & node1, const QDomNode & node2)
00038 {
00039     
00040     // compare name and value
00041     if (node1.nodeName() != node2.nodeName() || node1.nodeValue() != node2.nodeValue())
00042         return false;
00043     
00044     // recursively compare children
00045     const QDomNodeList node1Children  = node1.childNodes();
00046     const QDomNodeList node2Children  = node2.childNodes();
00047     
00048     if (node1Children.count () != node2Children.count ())
00049         return false;
00050     
00051     for (int i=0; i<node1Children.count ();i++) {
00052         if (!deepCompareDomNodes(node1Children.at(i), node2Children.at(i) ))
00053             return false;
00054     }
00055     return true;
00056 }
00057 
00058 /*
00059 static QString nodeAsString(const QDomNode & node1)
00060 {
00061     QString str;
00062     QTextStream ts( &str, QIODevice::WriteOnly );
00063     ts << node1; 
00064     return str;    
00065 }
00066 */
00067 
00068 static bool exactCompareBookmarks(const KBookmark & bookmark1, const KBookmark & bookmark2)
00069 {
00070     //kDebug() << "excat comparing:\n" << nodeAsString(bookmark1.internalElement()) << "\nwith:\n" << nodeAsString(bookmark2.internalElement()); 
00071     return deepCompareDomNodes(bookmark1.internalElement(), bookmark2.internalElement());
00072 }
00073 
00074 static void cloneBookmarkContents(const KBookmark & target, const KBookmark & source)
00075 {
00076     const QDomElement targetEl = target.internalElement();
00077     QDomNode parent = targetEl.parentNode ();
00078     QDomNode clonedNode = source.internalElement().cloneNode(true);
00079     parent.replaceChild (clonedNode , targetEl );
00080 }
00081 
00082 static KBookmark cloneBookmark(const KBookmark & toClone)
00083 {
00084     const QDomNode cloned = toClone.internalElement().cloneNode(true);
00085     return KBookmark(cloned.toElement ()); 
00086 }
00087 
00088 
00089 static void emptyBookmarkGroup(KBookmarkGroup & root)
00090 {
00091     KBookmark bookmark = root.first();
00092     while (!bookmark.isNull()) {
00093         KBookmark bookmarkToRemove = bookmark; 
00094         bookmark = root.next(bookmark);
00095         root.deleteBookmark(bookmarkToRemove);
00096     }
00097 }
00098 
00099 static int bookmarkGroupSize(KBookmarkGroup & root)
00100 {
00101     int count=0;
00102     KBookmark bookmark = root.first();
00103     while (!bookmark.isNull()) {
00104         count++;
00105         bookmark = root.next(bookmark);
00106     }
00107     return count;
00108 }
00109 
00111 
00112 KFilePlacesSharedBookmarks::KFilePlacesSharedBookmarks(KBookmarkManager * mgr)
00113 {
00114     m_placesBookmarkManager = mgr;
00115     
00116     // we check later if the directory exists 
00117     KStandardDirs::makeDir(KStandardDirs().localxdgdatadir());
00118     const QString file = KStandardDirs().localxdgdatadir() + "user-places.xbel";
00119     m_sharedBookmarkManager = KBookmarkManager::managerForExternalFile(file); 
00120     
00121     connect(m_sharedBookmarkManager, SIGNAL(changed(QString,QString)),
00122               this, SLOT(slotSharedBookmarksChanged()));
00123     connect(m_sharedBookmarkManager, SIGNAL(bookmarksChanged(QString)),
00124               this, SLOT(slotSharedBookmarksChanged()));
00125 
00126     connect(m_placesBookmarkManager, SIGNAL(changed(QString,QString)),
00127               this, SLOT(slotBookmarksChanged()));
00128     connect(m_placesBookmarkManager, SIGNAL(bookmarksChanged(QString)),
00129               this, SLOT(slotBookmarksChanged()));
00130     
00131     integrateSharedBookmarks();
00132 }
00133 
00134 bool KFilePlacesSharedBookmarks::integrateSharedBookmarks()
00135 {
00136     KBookmarkGroup root = m_placesBookmarkManager->root();
00137     KBookmark bookmark = root.first();
00138     
00139     KBookmarkGroup sharedRoot = m_sharedBookmarkManager->root();
00140     KBookmark sharedBookmark = sharedRoot.first();
00141   
00142     bool dirty = false;
00143     
00144     while (!bookmark.isNull()) {
00145         //kDebug() << "importing" << bookmark.text();
00146       
00147         // skip over system items
00148         if (bookmark.metaDataItem("isSystemItem") == "true") {
00149             bookmark = root.next(bookmark);
00150             continue;
00151         }
00152 
00153         // do the bookmarks match?
00154         if (!sharedBookmark.isNull() && compareBookmarks(bookmark, sharedBookmark)) {
00155             //kDebug() << "excat comparing: targetbk:\n" << nodeAsString(bookmark.internalElement()) << "\nsourcbk:\n" << nodeAsString(sharedBookmark.internalElement());
00156           
00157             if (!exactCompareBookmarks(bookmark, sharedBookmark)) {
00158                 KBookmark cloneTarget=bookmark;
00159                 KBookmark cloneSource = sharedBookmark;
00160               
00161                 sharedBookmark = sharedRoot.next(sharedBookmark);
00162                 bookmark = root.next(bookmark);
00163 
00164                 //kDebug() << "cloning" << cloneSource.text();
00165                 //kDebug() << "cloning: target=\n" << nodeAsString(cloneTarget.internalElement()) << "\n source:\n" << nodeAsString(cloneSource.internalElement());
00166 
00167                 cloneBookmarkContents(cloneTarget, cloneSource);
00168                 dirty = true;
00169                 continue;
00170             } else {
00171                 //kDebug() << "keeping" << bookmark.text();
00172             }
00173             sharedBookmark = sharedRoot.next(sharedBookmark);
00174             bookmark = root.next(bookmark);
00175             continue;
00176         }
00177         
00178         // they don't match -> remove
00179         //kDebug() << "removing" << bookmark.text();
00180         KBookmark bookmarkToRemove = bookmark; 
00181         bookmark = root.next(bookmark);
00182         root.deleteBookmark(bookmarkToRemove);
00183         
00184         dirty = true;
00185     }
00186 
00187     // append the remaining shared bookmarks
00188     while(!sharedBookmark.isNull()) {
00189         root.addBookmark(cloneBookmark(sharedBookmark));
00190         sharedBookmark = sharedRoot.next(sharedBookmark);
00191         dirty = true;
00192     }
00193   
00194     return dirty;
00195 }
00196 
00197 bool KFilePlacesSharedBookmarks::exportSharedBookmarks()
00198 {
00199     KBookmarkGroup root = m_placesBookmarkManager->root();
00200     KBookmark bookmark = root.first();
00201     
00202     KBookmarkGroup sharedRoot = m_sharedBookmarkManager->root();
00203     KBookmark sharedBookmark = sharedRoot.first();
00204   
00205     bool dirty = false;
00206     
00207     // first check if they are the same
00208     int count=0;
00209     while (!bookmark.isNull()) {
00210         //kDebug() << "exporting..." << bookmark.text();
00211       
00212         // skip over system items
00213         if (bookmark.metaDataItem("isSystemItem") == "true") {
00214           bookmark = root.next(bookmark);
00215           continue;
00216         }
00217         count++;
00218         
00219         // end of sharedBookmarks?
00220         if (sharedBookmark.isNull()) {
00221             dirty=true;
00222             break;
00223         }
00224         
00225         // do the bookmarks match?
00226         if (compareBookmarks(bookmark, sharedBookmark)) {
00227             if (!exactCompareBookmarks(bookmark, sharedBookmark)) {
00228                 dirty = true;
00229                 break;
00230             }
00231         } else {
00232             dirty=true;
00233             break;
00234         }
00235         sharedBookmark = sharedRoot.next(sharedBookmark);
00236         bookmark = root.next(bookmark);
00237     }
00238   
00239     //kDebug() << "dirty=" << dirty << " oldsize=" << bookmarkGroupSize(sharedRoot) << " count=" << count;
00240     
00241     if (bookmarkGroupSize(sharedRoot) != count)
00242         dirty=true;
00243     
00244     if (dirty) {
00245         emptyBookmarkGroup(sharedRoot);
00246 
00247         // append all bookmarks
00248         KBookmark bookmark = root.first();
00249       
00250         while(!bookmark.isNull()) {
00251           
00252             if (bookmark.metaDataItem("isSystemItem") == "true") {
00253               bookmark = root.next(bookmark);
00254               continue;
00255             }
00256           
00257             sharedRoot.addBookmark(cloneBookmark(bookmark));
00258             bookmark = root.next(bookmark);
00259             dirty = true;
00260         }
00261     }
00262     
00263     return dirty;    
00264   
00265 }
00266 
00267 void KFilePlacesSharedBookmarks::slotSharedBookmarksChanged()
00268 {
00269     //kDebug() << "shared bookmarks changed";
00270     bool dirty = integrateSharedBookmarks();
00271     if (dirty) m_placesBookmarkManager->emitChanged();
00272 }
00273 
00274 void KFilePlacesSharedBookmarks::slotBookmarksChanged()
00275 {
00276     //kDebug() << "places bookmarks changed";
00277     bool dirty = exportSharedBookmarks();
00278     if (dirty) m_sharedBookmarkManager->emitChanged();
00279 }
00280 
00281 #include "kfileplacessharedbookmarks_p.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 21:01:45 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KFile

Skip menu "KFile"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.8.3 API Reference

Skip menu "kdelibs-4.8.3 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal