20 #include "kbookmark.h"
21 #include <tqvaluestack.h>
23 #include <kmimetype.h>
24 #include <kstringhandler.h>
25 #include <kinputdialog.h>
26 #include <tdeglobal.h>
27 #include <tdelocale.h>
29 #include <tdeapplication.h>
30 #include <dcopclient.h>
31 #include <kbookmarkmanager.h>
34 : KBookmark( TQDomElement() )
45 if (m_address.isEmpty())
46 m_address = address();
52 return element.attribute(
"folded") ==
"no";
56 static TQDomElement firstElement(TQDomNode n)
58 while(!n.isNull() && !n.isElement())
64 static TQDomElement lastElement(TQDomNode n)
66 while(!n.isNull() && !n.isElement())
67 n = n.previousSibling();
73 return KBookmark( nextKnownTag( firstElement(element.firstChild()),
true ) );
78 return KBookmark( nextKnownTag( lastElement(current.element.previousSibling()),
false ) );
83 return KBookmark( nextKnownTag( firstElement(current.element.nextSibling()),
true ) );
88 TQDomElement KBookmarkGroup::nextKnownTag( TQDomElement start,
bool goNext )
const
90 static const TQString & bookmark = TDEGlobal::staticQString(
"bookmark");
91 static const TQString & folder = TDEGlobal::staticQString(
"folder");
92 static const TQString & separator = TDEGlobal::staticQString(
"separator");
94 for( TQDomNode n = start; !n.isNull(); )
96 TQDomElement elem = n.toElement();
97 TQString tag = elem.tagName();
98 if (tag == folder || tag == bookmark || tag == separator)
103 n = n.previousSibling();
105 return TQDomElement();
110 TQString txt( text );
111 if ( text.isEmpty() )
114 TQString caption = parentGroup().fullText().isEmpty() ?
115 i18n(
"Create New Bookmark Folder" ) :
116 i18n(
"Create New Bookmark Folder in %1" )
117 .arg( parentGroup().text() );
118 txt = KInputDialog::getText( caption, i18n(
"New folder:" ),
119 TQString::null, &ok );
124 Q_ASSERT(!element.isNull());
125 TQDomDocument doc = element.ownerDocument();
126 TQDomElement groupElem = doc.createElement(
"folder" );
127 element.appendChild( groupElem );
128 TQDomElement textElem = doc.createElement(
"title" );
129 groupElem.appendChild( textElem );
130 textElem.appendChild( doc.createTextNode( txt ) );
135 emit mgr->
notifier().createdNewFolder(
136 mgr->
path(), grp.fullText(),
145 Q_ASSERT(!element.isNull());
146 TQDomDocument doc = element.ownerDocument();
147 Q_ASSERT(!doc.isNull());
148 TQDomElement sepElem = doc.createElement(
"separator" );
149 element.appendChild( sepElem );
150 return KBookmark(sepElem);
156 if ( !after.isNull() )
157 n = element.insertAfter( item.element, after.element );
160 if ( element.firstChild().isNull() )
161 n = element.insertBefore( item.element, TQDomElement() );
164 TQDomElement firstChild = nextKnownTag(element.firstChild().toElement(),
true);
165 if ( !firstChild.isNull() )
166 n = element.insertBefore( item.element, firstChild );
170 n = element.appendChild( item.element );
173 return (!n.isNull());
178 element.appendChild( bm.internalElement() );
181 if ( bm.hasMetaData() ) {
185 mgr->
path(), bm.url().url(),
186 bm.fullText(), bm.address(), bm.icon() );
196 TQDomDocument doc = element.ownerDocument();
197 TQDomElement elem = doc.createElement(
"bookmark" );
198 elem.setAttribute(
"href", url.url( 0, 106 ) );
199 TQString _icon = icon;
200 if ( _icon.isEmpty() )
201 _icon = KMimeType::iconForURL( url );
202 elem.setAttribute(
"icon", _icon );
204 TQDomElement textElem = doc.createElement(
"title" );
205 elem.appendChild( textElem );
206 textElem.appendChild( doc.createTextNode( text ) );
208 return addBookmark( mgr, KBookmark( elem ), emitSignal );
213 element.removeChild( bk.element );
218 return ( element.attribute(
"toolbar") ==
"yes" );
221 TQDomElement KBookmarkGroup::findToolbar()
const
223 if ( element.attribute(
"toolbar") ==
"yes" )
225 for (TQDomNode n = element.firstChild(); !n.isNull() ; n = n.nextSibling() )
227 TQDomElement e = n.toElement();
229 if ( e.tagName() ==
"folder" )
231 if ( e.attribute(
"toolbar") ==
"yes" )
236 if (!result.isNull())
241 return TQDomElement();
246 TQValueList<KURL> urlList;
247 for ( KBookmark bm =
first(); !bm.isNull(); bm =
next(bm) )
249 if ( bm.isSeparator() || bm.isGroup() )
258 bool KBookmark::isGroup()
const
260 TQString tag = element.tagName();
261 return ( tag ==
"folder"
265 bool KBookmark::isSeparator()
const
267 return (element.tagName() ==
"separator");
270 bool KBookmark::hasParent()
const
272 TQDomElement parent = element.parentNode().toElement();
273 return !parent.isNull();
276 TQString KBookmark::text()
const
278 return KStringHandler::csqueeze( fullText() );
281 TQString KBookmark::fullText()
const
284 return i18n(
"--- separator ---");
286 return element.namedItem(
"title").toElement().text();
289 KURL KBookmark::url()
const
291 return KURL(element.attribute(
"href"), 106);
294 TQString KBookmark::icon()
const
296 TQString icon = element.attribute(
"icon");
297 if ( icon.isEmpty() ) {
301 icon =
"bookmark_folder";
304 if ( isSeparator() ) {
308 icon = KMimeType::iconForURL( url() );
322 Q_ASSERT( isGroup() );
326 TQString KBookmark::address()
const
328 if ( element.tagName() ==
"xbel" )
335 Q_ASSERT(hasParent());
339 TQString parentAddress = group.address();
343 for ( KBookmark bk = group.
first() ; !bk.isNull() ; bk = group.
next(bk), ++counter )
345 if ( bk.element == element )
346 return parentAddress +
"/" + TQString::number(counter);
348 kdWarning() <<
"KBookmark::address : this can't happen! " << parentAddress << endl;
353 KBookmark KBookmark::standaloneBookmark(
const TQString & text,
const KURL & url,
const TQString & icon )
355 TQDomDocument doc(
"xbel");
356 TQDomElement elem = doc.createElement(
"xbel");
357 doc.appendChild( elem );
359 grp.addBookmark( 0L, text, url, icon,
false );
365 TQString KBookmark::left(
const TQString & str, uint len)
371 return str.left(len);
374 TQString KBookmark::commonParent(TQString A, TQString B)
376 TQString error(
"ERROR");
377 if(A == error || B == error)
383 uint lastCommonSlash = 0;
384 uint lastPos = A.length() < B.length() ? A.length() : B.length();
385 for(uint i=0; i < lastPos; ++i)
388 return left(A, lastCommonSlash);
392 return left(A, lastCommonSlash);
395 static TQDomNode cd_or_create(TQDomNode node, TQString name)
397 TQDomNode subnode = node.namedItem(name);
398 if (subnode.isNull())
400 subnode = node.ownerDocument().createElement(name);
401 node.appendChild(subnode);
406 static TQDomText get_or_create_text(TQDomNode node)
408 TQDomNode subnode = node.firstChild();
409 if (subnode.isNull())
411 subnode = node.ownerDocument().createTextNode(
"");
412 node.appendChild(subnode);
414 return subnode.toText();
418 static TQDomNode findOrCreateMetadata( TQDomNode& parent )
420 static const char kdeOwner[] =
"http://www.kde.org";
421 TQDomElement metadataElement;
422 for ( TQDomNode _node = parent.firstChild(); !_node.isNull(); _node = _node.nextSibling() ) {
423 TQDomElement elem = _node.toElement();
424 if ( !elem.isNull() && elem.tagName() ==
"metadata" ) {
425 const TQString owner = elem.attribute(
"owner" );
426 if ( owner == kdeOwner )
428 if ( owner.isEmpty() )
429 metadataElement = elem;
432 if ( metadataElement.isNull() ) {
433 metadataElement = parent.ownerDocument().createElement(
"metadata" );
434 parent.appendChild(metadataElement);
436 metadataElement.setAttribute(
"owner", kdeOwner );
437 return metadataElement;
440 bool KBookmark::hasMetaData()
const
445 TQDomNode n = cd_or_create( internalElement(),
"info" );
446 return findOrCreateMetadata( n ).hasChildNodes();
449 void KBookmark::updateAccessMetadata()
451 kdDebug(7043) <<
"KBookmark::updateAccessMetadata " << address() <<
" " << url().prettyURL() << endl;
453 const uint timet = TQDateTime::currentDateTime().toTime_t();
454 setMetaDataItem(
"time_added", TQString::number( timet ), DontOverwriteMetaData );
455 setMetaDataItem(
"time_visited", TQString::number( timet ) );
457 TQString countStr = metaDataItem(
"visit_count" );
459 int currentCount = countStr.toInt(&ok);
463 setMetaDataItem(
"visit_count", TQString::number( currentCount ) );
468 TQString KBookmark::metaDataItem(
const TQString &key )
const
470 TQDomNode infoNode = cd_or_create( internalElement(),
"info" );
471 infoNode = findOrCreateMetadata( infoNode );
472 for ( TQDomNode n = infoNode.firstChild(); !n.isNull(); n = n.nextSibling() ) {
473 if ( !n.isElement() ) {
476 const TQDomElement e = n.toElement();
477 if ( e.tagName() == key ) {
481 return TQString::null;
484 void KBookmark::setMetaDataItem(
const TQString &key,
const TQString &value, MetaDataOverwriteMode mode )
486 TQDomNode infoNode = cd_or_create( internalElement(),
"info" );
487 infoNode = findOrCreateMetadata( infoNode );
489 TQDomNode item = cd_or_create( infoNode, key );
490 TQDomText text = get_or_create_text( item );
491 if ( mode == DontOverwriteMetaData && !text.data().isEmpty() ) {
495 text.setData( value );
498 void KBookmarkGroupTraverser::traverse(
const KBookmarkGroup &root)
501 TQValueStack<KBookmarkGroup> stack;
503 KBookmark bk = stack.top().first();
509 if (stack.count() > 1)
510 visitLeave(stack.top());
512 bk = stack.top().next(bk);
521 if (!gp.
first().isNull())
533 bk = stack.top().next(bk);
bool isToolbarGroup() const
KBookmark next(const KBookmark ¤t) const
Return the next sibling of a child bookmark of this group.
TQString groupAddress() const
Much like KBookmark::address, but caches the address into m_address.
KBookmark first() const
Return the first child bookmark of this group.
KBookmarkGroup()
Create an invalid group.
KBookmark addBookmark(KBookmarkManager *mgr, const KBookmark &bm, bool emitSignal=true)
Create a new bookmark, as the last child of this group Don't forget to use KBookmarkManager::self()->...
void deleteBookmark(KBookmark bk)
Delete a bookmark - it has to be one of our children ! Don't forget to use KBookmarkManager::self()->...
KBookmark previous(const KBookmark ¤t) const
Return the prevous sibling of a child bookmark of this group.
KBookmarkGroup createNewFolder(KBookmarkManager *mgr, const TQString &text=TQString::null, bool emitSignal=true)
Create a new bookmark folder, as the last child of this group.
KBookmark createNewSeparator()
Create a new bookmark separator Don't forget to use KBookmarkManager::self()->emitChanged( parentBook...
bool moveItem(const KBookmark &item, const KBookmark &after)
Moves item after after (which should be a child of ours).
TQValueList< KURL > groupUrlList() const
This class implements the reading/writing of bookmarks in XML.
TQString path()
This will return the path that this manager is using to read the bookmarks.
KBookmarkNotifier & notifier()
Access to bookmark notifier, for emitting signals.
ASYNC notifyCompleteChange(TQString caller)
Reparse the whole bookmarks file and notify about the change (Called by the bookmark editor)