kio Library API Documentation

kbookmark.cc

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
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., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "kbookmark.h"
00020 #include <kdebug.h>
00021 #include <kmimetype.h>
00022 #include <kstringhandler.h>
00023 #include <klineeditdlg.h>
00024 #include <kglobal.h>
00025 #include <klocale.h>
00026 #include <assert.h>
00027 #include <kapplication.h>
00028 #include <dcopclient.h>
00029 #include <kbookmarkmanager.h>
00030 
00031 KBookmarkGroup::KBookmarkGroup()
00032  : KBookmark( QDomElement() )
00033 {
00034 }
00035 
00036 KBookmarkGroup::KBookmarkGroup( QDomElement elem )
00037  : KBookmark(elem)
00038 {
00039 }
00040 
00041 QString KBookmarkGroup::groupAddress() const
00042 {
00043     if (m_address.isEmpty())
00044         m_address = address();
00045     return m_address;
00046 }
00047 
00048 bool KBookmarkGroup::isOpen() const
00049 {
00050     return element.attribute("folded") == "no"; // default is: folded
00051 }
00052 
00053 KBookmark KBookmarkGroup::first() const
00054 {
00055     return KBookmark( nextKnownTag( element.firstChild().toElement(), true ) );
00056 }
00057 
00058 KBookmark KBookmarkGroup::previous( const KBookmark & current ) const
00059 {
00060     return KBookmark( nextKnownTag( current.element.previousSibling().toElement(), false ) );
00061 }
00062 
00063 KBookmark KBookmarkGroup::next( const KBookmark & current ) const
00064 {
00065     return KBookmark( nextKnownTag( current.element.nextSibling().toElement(), true ) );
00066 }
00067 
00068 QDomElement KBookmarkGroup::nextKnownTag( QDomElement start, bool goNext ) const
00069 {
00070     static const QString & bookmark = KGlobal::staticQString("bookmark");
00071     static const QString & folder = KGlobal::staticQString("folder");
00072     static const QString & separator = KGlobal::staticQString("separator");
00073     QDomElement elem = start;
00074     while ( !elem.isNull() )
00075     {
00076         QString tag = elem.tagName();
00077         if (tag == folder || tag == bookmark || tag == separator)
00078             break;
00079         if (goNext)
00080             elem = elem.nextSibling().toElement();
00081         else
00082             elem = elem.previousSibling().toElement();
00083     }
00084     return elem;
00085 }
00086 
00087 KBookmarkGroup KBookmarkGroup::createNewFolder( KBookmarkManager* mgr, const QString & text, bool emitSignal )
00088 {
00089     QString txt( text );
00090     if ( text.isEmpty() )
00091     {
00092         KLineEditDlg l( i18n("New folder:"), "", 0L );
00093         l.setCaption( parentGroup().text().isEmpty() ?
00094                       i18n("Create New Bookmark Folder") :
00095                       i18n("Create New Bookmark Folder in %1").arg( parentGroup().text() ) );
00096         //text is empty so disable ok button.
00097         l.enableButtonOK( false );
00098         if ( l.exec() )
00099             txt = l.text();
00100         else
00101             return KBookmarkGroup();
00102     }
00103 
00104     Q_ASSERT(!element.isNull());
00105     QDomDocument doc = element.ownerDocument();
00106     QDomElement groupElem = doc.createElement( "folder" );
00107     element.appendChild( groupElem );
00108     QDomElement textElem = doc.createElement( "title" );
00109     groupElem.appendChild( textElem );
00110     textElem.appendChild( doc.createTextNode( txt ) );
00111 
00112     KBookmarkGroup grp(groupElem);
00113 
00114     if (emitSignal) emit mgr->notifier().createdNewFolder(
00115         mgr->path(),
00116         grp.fullText(), grp.address() );
00117 
00118     return grp;
00119 
00120 }
00121 
00122 KBookmark KBookmarkGroup::createNewSeparator()
00123 {
00124     Q_ASSERT(!element.isNull());
00125     QDomDocument doc = element.ownerDocument();
00126     Q_ASSERT(!doc.isNull());
00127     QDomElement sepElem = doc.createElement( "separator" );
00128     element.appendChild( sepElem );
00129     return KBookmark(sepElem);
00130 }
00131 
00132 bool KBookmarkGroup::moveItem( const KBookmark & item, const KBookmark & after )
00133 {
00134     QDomNode n;
00135     if ( !after.isNull() )
00136         n = element.insertAfter( item.element, after.element );
00137     else // first child
00138     {
00139         if ( element.firstChild().isNull() ) // Empty element -> set as real first child
00140             n = element.insertBefore( item.element, QDomElement() );
00141 
00142         // we have to skip everything up to the first valid child
00143         QDomElement firstChild = nextKnownTag(element.firstChild().toElement(), true);
00144         if ( !firstChild.isNull() )
00145             n = element.insertBefore( item.element, firstChild );
00146         else
00147         {
00148             // No real first child -> append after the <title> etc.
00149             n = element.appendChild( item.element );
00150         }
00151     }
00152     return (!n.isNull());
00153 }
00154 
00155 KBookmark KBookmarkGroup::addBookmark( KBookmarkManager* mgr, const QString & text, const KURL & url, const QString & icon, bool emitSignal )
00156 {
00157     //kdDebug(1203) << "KBookmarkGroup::addBookmark " << text << " into " << m_address << endl;
00158     QDomDocument doc = element.ownerDocument();
00159     QDomElement elem = doc.createElement( "bookmark" );
00160     element.appendChild( elem );
00161     elem.setAttribute( "href", url.url( 0, 106 ) ); // write utf8 URL (106 is mib enum for utf8)
00162     QString _icon = icon;
00163     if ( _icon.isEmpty() )
00164         _icon = KMimeType::iconForURL( url );
00165     elem.setAttribute( "icon", _icon );
00166 
00167     QDomElement textElem = doc.createElement( "title" );
00168     elem.appendChild( textElem );
00169     textElem.appendChild( doc.createTextNode( text ) );
00170 
00171     KBookmark bk(elem);
00172 
00173     if (emitSignal) emit mgr->notifier().addedBookmark(
00174         mgr->path(),
00175         url.url(), text, bk.address(), icon );
00176     
00177     return bk;
00178 }
00179 
00180 void KBookmarkGroup::deleteBookmark( KBookmark bk )
00181 {
00182     element.removeChild( bk.element );
00183 }
00184 
00185 bool KBookmarkGroup::isToolbarGroup() const
00186 {
00187     return ( element.attribute("toolbar") == "yes" );
00188 }
00189 
00190 QDomElement KBookmarkGroup::findToolbar() const
00191 {
00192     if ( element.attribute("toolbar") == "yes" )
00193         return element;
00194     QDomElement e = element.firstChild().toElement();
00195     for ( ; !e.isNull() ; e = e.nextSibling().toElement() )
00196     {
00197         // Search among the "folder" children only
00198         if ( e.tagName() == "folder" )
00199         {
00200             if ( e.attribute("toolbar") == "yes" )
00201                 return e;
00202             else
00203             {
00204                 QDomElement result = KBookmarkGroup(e).findToolbar();
00205                 if (!result.isNull())
00206                     return result;
00207             }
00208         }
00209     }
00210     return QDomElement();
00211 }
00212 
00214 
00215 bool KBookmark::isGroup() const
00216 {
00217     QString tag = element.tagName();
00218     return ( tag == "folder"
00219              || tag == "xbel" ); // don't forget the toplevel group
00220 }
00221 
00222 bool KBookmark::isSeparator() const
00223 {
00224     return (element.tagName() == "separator");
00225 }
00226 
00227 QString KBookmark::text() const
00228 {
00229     return KStringHandler::csqueeze( fullText() );
00230 }
00231 
00232 QString KBookmark::fullText() const
00233 {
00234     if (isSeparator())
00235         return i18n("--- separator ---");
00236 
00237     return element.namedItem("title").toElement().text();
00238 }
00239 
00240 KURL KBookmark::url() const
00241 {
00242     return KURL(element.attribute("href"), 106); // Decode it from utf8 (106 is mib enum for utf8)
00243 }
00244 
00245 QString KBookmark::icon() const
00246 {
00247     QString icon = element.attribute("icon");
00248     if ( icon.isEmpty() )
00249         // Default icon depends on URL for bookmarks, and is default directory
00250         // icon for groups.
00251         if ( isGroup() )
00252             icon = "bookmark_folder";
00253         else
00254             if ( isSeparator() )
00255                 icon = "eraser"; // whatever
00256             else
00257                 icon = KMimeType::iconForURL( url() );
00258     return icon;
00259 }
00260 
00261 KBookmarkGroup KBookmark::parentGroup() const
00262 {
00263     return KBookmarkGroup( element.parentNode().toElement() );
00264 }
00265 
00266 KBookmarkGroup KBookmark::toGroup() const
00267 {
00268     Q_ASSERT( isGroup() );
00269     return KBookmarkGroup(element);
00270 }
00271 
00272 QString KBookmark::address() const
00273 {
00274     if ( element.tagName() == "xbel" )
00275         return ""; // not QString::null !
00276     else
00277     {
00278         // Use keditbookmarks's DEBUG_ADDRESSES flag to debug this code :)
00279         QDomElement parent = element.parentNode().toElement();
00280         if(parent.isNull())
00281         {
00282           Q_ASSERT(!parent.isNull());
00283           return "ERROR"; // Avoid an infinite loop
00284         }
00285         KBookmarkGroup group( parent );
00286         QString parentAddress = group.address();
00287         uint counter = 0;
00288         // Implementation note: we don't use QDomNode's childNode list because we
00289         // would have to skip "TEXT", which KBookmarkGroup already does for us.
00290         for ( KBookmark bk = group.first() ; !bk.isNull() ; bk = group.next(bk), ++counter )
00291         {
00292             if ( bk.element == element )
00293                 return parentAddress + "/" + QString::number(counter);
00294         }
00295         kdWarning() << "KBookmark::address : this can't happen!  " << parentAddress << endl;
00296         return "ERROR";
00297     }
00298 }
00299 
00300 KBookmark KBookmark::standaloneBookmark( const QString & text, const KURL & url, const QString & icon )
00301 {
00302     QDomDocument doc("xbel");
00303     QDomElement elem = doc.createElement("xbel");
00304     doc.appendChild( elem );
00305     KBookmarkGroup grp( elem );
00306     grp.addBookmark( 0L, text, url, icon, false );
00307     return grp.first();
00308 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:10 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001