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

KFile

  • kfile
kfileplacessharedbookmarks.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 2008 Norbert Frese <nf2@scheinwelt.at>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17 
18 */
19 
20 #include "kfileplacessharedbookmarks_p.h"
21 
22 #include <QtCore/QObject>
23 #include <QtCore/QTextStream>
24 #include <QtCore/QFile>
25 #include <kstandarddirs.h>
26 #include <kbookmarkmanager.h>
27 #include <kbookmark.h>
28 #include <kdebug.h>
29 
31 
32 static bool compareBookmarks(const KBookmark & bookmark1, const KBookmark & bookmark2)
33 {
34  return (bookmark1.url() == bookmark2.url() || bookmark1.text() == bookmark2.text());
35 }
36 
37 static bool deepCompareDomNodes(const QDomNode & node1, const QDomNode & node2)
38 {
39 
40  // compare name and value
41  if (node1.nodeName() != node2.nodeName() || node1.nodeValue() != node2.nodeValue())
42  return false;
43 
44  // recursively compare children
45  const QDomNodeList node1Children = node1.childNodes();
46  const QDomNodeList node2Children = node2.childNodes();
47 
48  if (node1Children.count () != node2Children.count ())
49  return false;
50 
51  for (int i=0; i<node1Children.count ();i++) {
52  if (!deepCompareDomNodes(node1Children.at(i), node2Children.at(i) ))
53  return false;
54  }
55  return true;
56 }
57 
58 /*
59 static QString nodeAsString(const QDomNode & node1)
60 {
61  QString str;
62  QTextStream ts( &str, QIODevice::WriteOnly );
63  ts << node1;
64  return str;
65 }
66 */
67 
68 static bool exactCompareBookmarks(const KBookmark & bookmark1, const KBookmark & bookmark2)
69 {
70  //kDebug() << "excat comparing:\n" << nodeAsString(bookmark1.internalElement()) << "\nwith:\n" << nodeAsString(bookmark2.internalElement());
71  return deepCompareDomNodes(bookmark1.internalElement(), bookmark2.internalElement());
72 }
73 
74 static void cloneBookmarkContents(const KBookmark & target, const KBookmark & source)
75 {
76  const QDomElement targetEl = target.internalElement();
77  QDomNode parent = targetEl.parentNode ();
78  QDomNode clonedNode = source.internalElement().cloneNode(true);
79  parent.replaceChild (clonedNode , targetEl );
80 }
81 
82 static KBookmark cloneBookmark(const KBookmark & toClone)
83 {
84  const QDomNode cloned = toClone.internalElement().cloneNode(true);
85  return KBookmark(cloned.toElement ());
86 }
87 
88 
89 static void emptyBookmarkGroup(KBookmarkGroup & root)
90 {
91  KBookmark bookmark = root.first();
92  while (!bookmark.isNull()) {
93  KBookmark bookmarkToRemove = bookmark;
94  bookmark = root.next(bookmark);
95  root.deleteBookmark(bookmarkToRemove);
96  }
97 }
98 
99 static int bookmarkGroupSize(KBookmarkGroup & root)
100 {
101  int count=0;
102  KBookmark bookmark = root.first();
103  while (!bookmark.isNull()) {
104  count++;
105  bookmark = root.next(bookmark);
106  }
107  return count;
108 }
109 
111 
112 KFilePlacesSharedBookmarks::KFilePlacesSharedBookmarks(KBookmarkManager * mgr)
113 {
114  m_placesBookmarkManager = mgr;
115 
116  // we check later if the directory exists
117  KStandardDirs::makeDir(KStandardDirs().localxdgdatadir());
118  const QString file = KStandardDirs().localxdgdatadir() + "user-places.xbel";
119  m_sharedBookmarkManager = KBookmarkManager::managerForExternalFile(file);
120 
121  connect(m_sharedBookmarkManager, SIGNAL(changed(QString,QString)),
122  this, SLOT(slotSharedBookmarksChanged()));
123  connect(m_sharedBookmarkManager, SIGNAL(bookmarksChanged(QString)),
124  this, SLOT(slotSharedBookmarksChanged()));
125 
126  integrateSharedBookmarks();
127 }
128 
129 bool KFilePlacesSharedBookmarks::integrateSharedBookmarks()
130 {
131  KBookmarkGroup root = m_placesBookmarkManager->root();
132  KBookmark bookmark = root.first();
133 
134  KBookmarkGroup sharedRoot = m_sharedBookmarkManager->root();
135  KBookmark sharedBookmark = sharedRoot.first();
136 
137  bool dirty = false;
138 
139  while (!bookmark.isNull()) {
140  //kDebug() << "importing" << bookmark.text();
141 
142  // skip over system items
143  if (bookmark.metaDataItem("isSystemItem") == "true") {
144  bookmark = root.next(bookmark);
145  continue;
146  }
147 
148  // do the bookmarks match?
149  if (!sharedBookmark.isNull() && compareBookmarks(bookmark, sharedBookmark)) {
150  //kDebug() << "excat comparing: targetbk:\n" << nodeAsString(bookmark.internalElement()) << "\nsourcbk:\n" << nodeAsString(sharedBookmark.internalElement());
151 
152  if (!exactCompareBookmarks(bookmark, sharedBookmark)) {
153  KBookmark cloneTarget=bookmark;
154  KBookmark cloneSource = sharedBookmark;
155 
156  sharedBookmark = sharedRoot.next(sharedBookmark);
157  bookmark = root.next(bookmark);
158 
159  //kDebug() << "cloning" << cloneSource.text();
160  //kDebug() << "cloning: target=\n" << nodeAsString(cloneTarget.internalElement()) << "\n source:\n" << nodeAsString(cloneSource.internalElement());
161 
162  cloneBookmarkContents(cloneTarget, cloneSource);
163  dirty = true;
164  continue;
165  } else {
166  //kDebug() << "keeping" << bookmark.text();
167  }
168  sharedBookmark = sharedRoot.next(sharedBookmark);
169  bookmark = root.next(bookmark);
170  continue;
171  }
172 
173  // they don't match -> remove
174  //kDebug() << "removing" << bookmark.text();
175  KBookmark bookmarkToRemove = bookmark;
176  bookmark = root.next(bookmark);
177  root.deleteBookmark(bookmarkToRemove);
178 
179  dirty = true;
180  }
181 
182  // append the remaining shared bookmarks
183  while(!sharedBookmark.isNull()) {
184  root.addBookmark(cloneBookmark(sharedBookmark));
185  sharedBookmark = sharedRoot.next(sharedBookmark);
186  dirty = true;
187  }
188 
189  return dirty;
190 }
191 
192 bool KFilePlacesSharedBookmarks::exportSharedBookmarks()
193 {
194  KBookmarkGroup root = m_placesBookmarkManager->root();
195  KBookmark bookmark = root.first();
196 
197  KBookmarkGroup sharedRoot = m_sharedBookmarkManager->root();
198  KBookmark sharedBookmark = sharedRoot.first();
199 
200  bool dirty = false;
201 
202  // first check if they are the same
203  int count=0;
204  while (!bookmark.isNull()) {
205  //kDebug() << "exporting..." << bookmark.text();
206 
207  // skip over system items
208  if (bookmark.metaDataItem("isSystemItem") == "true") {
209  bookmark = root.next(bookmark);
210  continue;
211  }
212  count++;
213 
214  // end of sharedBookmarks?
215  if (sharedBookmark.isNull()) {
216  dirty=true;
217  break;
218  }
219 
220  // do the bookmarks match?
221  if (compareBookmarks(bookmark, sharedBookmark)) {
222  if (!exactCompareBookmarks(bookmark, sharedBookmark)) {
223  dirty = true;
224  break;
225  }
226  } else {
227  dirty=true;
228  break;
229  }
230  sharedBookmark = sharedRoot.next(sharedBookmark);
231  bookmark = root.next(bookmark);
232  }
233 
234  //kDebug() << "dirty=" << dirty << " oldsize=" << bookmarkGroupSize(sharedRoot) << " count=" << count;
235 
236  if (bookmarkGroupSize(sharedRoot) != count)
237  dirty=true;
238 
239  if (dirty) {
240  emptyBookmarkGroup(sharedRoot);
241 
242  // append all bookmarks
243  KBookmark bookmark = root.first();
244 
245  while(!bookmark.isNull()) {
246 
247  if (bookmark.metaDataItem("isSystemItem") == "true") {
248  bookmark = root.next(bookmark);
249  continue;
250  }
251 
252  sharedRoot.addBookmark(cloneBookmark(bookmark));
253  bookmark = root.next(bookmark);
254  dirty = true;
255  }
256  }
257 
258  return dirty;
259 
260 }
261 
262 void KFilePlacesSharedBookmarks::slotSharedBookmarksChanged()
263 {
264  //kDebug() << "shared bookmarks changed";
265  bool dirty = integrateSharedBookmarks();
266  if (dirty) m_placesBookmarkManager->emitChanged();
267 }
268 
269 void KFilePlacesSharedBookmarks::updateSharedBookmarks()
270 {
271  //kDebug() << "places bookmarks changed";
272  bool dirty = exportSharedBookmarks();
273  if (dirty) m_sharedBookmarkManager->emitChanged();
274 }
275 
276 #include "kfileplacessharedbookmarks_p.moc"
KFilePlacesSharedBookmarks::KFilePlacesSharedBookmarks
KFilePlacesSharedBookmarks(KBookmarkManager *mgr)
Definition: kfileplacessharedbookmarks.cpp:112
KFilePlacesSharedBookmarks::updateSharedBookmarks
void updateSharedBookmarks()
Definition: kfileplacessharedbookmarks.cpp:269
deepCompareDomNodes
static bool deepCompareDomNodes(const QDomNode &node1, const QDomNode &node2)
Definition: kfileplacessharedbookmarks.cpp:37
kbookmarkmanager.h
kdebug.h
KBookmark::internalElement
QDomElement internalElement() const
KBookmarkGroup::deleteBookmark
void deleteBookmark(const KBookmark &bk)
cloneBookmarkContents
static void cloneBookmarkContents(const KBookmark &target, const KBookmark &source)
Definition: kfileplacessharedbookmarks.cpp:74
KBookmarkManager
QString
KBookmark
KStandardDirs::makeDir
static bool makeDir(const QString &dir, int mode=0755)
KBookmark::text
QString text() const
exactCompareBookmarks
static bool exactCompareBookmarks(const KBookmark &bookmark1, const KBookmark &bookmark2)
Definition: kfileplacessharedbookmarks.cpp:68
KBookmarkGroup::addBookmark
KBookmark addBookmark(const KBookmark &bm)
KStandardDirs
KStandardDirs::localxdgdatadir
QString localxdgdatadir() const
KBookmarkManager::managerForExternalFile
static KBookmarkManager * managerForExternalFile(const QString &bookmarksFile)
KBookmarkGroup
cloneBookmark
static KBookmark cloneBookmark(const KBookmark &toClone)
Definition: kfileplacessharedbookmarks.cpp:82
KBookmark::metaDataItem
QString metaDataItem(const QString &key) const
bookmarkGroupSize
static int bookmarkGroupSize(KBookmarkGroup &root)
Definition: kfileplacessharedbookmarks.cpp:99
emptyBookmarkGroup
static void emptyBookmarkGroup(KBookmarkGroup &root)
Definition: kfileplacessharedbookmarks.cpp:89
KBookmarkManager::emitChanged
void emitChanged()
kbookmark.h
KBookmarkManager::root
KBookmarkGroup root() const
kstandarddirs.h
compareBookmarks
static bool compareBookmarks(const KBookmark &bookmark1, const KBookmark &bookmark2)
Definition: kfileplacessharedbookmarks.cpp:32
KBookmark::isNull
bool isNull() const
KBookmarkGroup::next
KBookmark next(const KBookmark &current) const
KBookmarkGroup::first
KBookmark first() const
kfileplacessharedbookmarks_p.h
KBookmark::url
KUrl url() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Wed Apr 22 2015 15:47:18 by doxygen 1.8.6 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.14.7 API Reference

Skip menu "kdelibs-4.14.7 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