# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# kbookmarkdrag.cc

    00001 //  -*- c-basic-offset:4; indent-tabs-mode:nil -*-
    00002 // vim: set ts=4 sts=4 sw=4 et:
    00003 /* This file is part of the KDE libraries
    00004    Copyright (C) 2000 David Faure <faure@kde.org>
    00005
    00006    This library is free software; you can redistribute it and/or
    00007    modify it under the terms of the GNU Library General Public
    00008    License version 2 as published by the Free Software Foundation.
    00009
    00010    This library is distributed in the hope that it will be useful,
    00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013    Library General Public License for more details.
    00014
    00015    You should have received a copy of the GNU Library General Public License
    00016    along with this library; see the file COPYING.LIB.  If not, write to
    00017    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018    Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include "kbookmarkdrag.h"
    00022 #include <kurldrag.h>
    00023 #include <kdebug.h>
    00024
    00025 KBookmarkDrag * KBookmarkDrag::newDrag( const QValueList<KBookmark> & bookmarks, QWidget * dragSource, const char * name )
    00026 {
    00027     KURL::List urls;
    00028
    00029     for ( QValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
    00030        urls.append( (*it).url() );
    00031     }
    00032
    00033     // See KURLDrag::newDrag
    00034     QStrList uris;
    00035     KURL::List::ConstIterator uit = urls.begin();
    00036     KURL::List::ConstIterator uEnd = urls.end();
    00037     // Get each URL encoded in utf8 - and since we get it in escaped
    00038     // form on top of that, .latin1() is fine.
    00039     for ( ; uit != uEnd ; ++uit )
    00040         uris.append( KURLDrag::urlToString(*uit).latin1() );
    00041
    00042     return new KBookmarkDrag( bookmarks, uris, dragSource, name );
    00043 }
    00044
    00045 KBookmarkDrag * KBookmarkDrag::newDrag( const KBookmark & bookmark, QWidget * dragSource, const char * name )
    00046 {
    00047     QValueList<KBookmark> bookmarks;
    00048     bookmarks.append( KBookmark(bookmark) );
    00049     return newDrag(bookmarks, dragSource, name);
    00050 }
    00051
    00052 KBookmarkDrag::KBookmarkDrag( const QValueList<KBookmark> & bookmarks, const QStrList & urls,
    00053                   QWidget * dragSource, const char * name )
    00054     : QUriDrag( urls, dragSource, name ), m_bookmarks( bookmarks ), m_doc("xbel")
    00055 {
    00056     // We need to create the XML for this drag right now and not
    00057     // in encodedData because when cutting a folder, the children
    00058     // wouldn't be part of the bookmarks anymore, when encodedData
    00059     // is requested.
    00060     QDomElement elem = m_doc.createElement("xbel");
    00061     m_doc.appendChild( elem );
    00062     for ( QValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
    00063        elem.appendChild( (*it).internalElement().cloneNode( true /* deep */ ) );
    00064     }
    00065     //kdDebug(7043) << "KBookmarkDrag::KBookmarkDrag " << m_doc.toString() << endl;
    00066 }
    00067
    00068 const char* KBookmarkDrag::format( int i ) const
    00069 {
    00070     if ( i == 0 )
    00071         return "application/x-xbel";
    00072     else if ( i == 1 )
    00073     return "text/uri-list";
    00074     else if ( i == 2 )
    00075     return "text/plain";
    00076     else return 0;
    00077 }
    00078
    00079 QByteArray KBookmarkDrag::encodedData( const char* mime ) const
    00080 {
    00081     QByteArray a;
    00082     QCString mimetype( mime );
    00083     if ( mimetype == "text/uri-list" )
    00084         return QUriDrag::encodedData( mime );
    00085     else if ( mimetype == "application/x-xbel" )
    00086     {
    00087         a = m_doc.toCString();
    00088         //kdDebug(7043) << "KBookmarkDrag::encodedData " << m_doc.toCString() << endl;
    00089     }
    00090     else if ( mimetype == "text/plain" )
    00091     {
    00092         KURL::List m_lstDragURLs;
    00093         if ( KURLDrag::decode( this, m_lstDragURLs ) )
    00094         {
    00095             QStringList uris;
    00096             KURL::List::ConstIterator uit = m_lstDragURLs.begin();
    00097             KURL::List::ConstIterator uEnd = m_lstDragURLs.end();
    00098             for ( ; uit != uEnd ; ++uit )
    00099                 uris.append( (*uit).prettyURL() );
    00100
    00101             QCString s = uris.join( "\n" ).local8Bit();
    00102             a.resize( s.length() + 1 ); // trailing zero
    00103             memcpy( a.data(), s.data(), s.length() + 1 );
    00104         }
    00105     }
    00106     return a;
    00107 }
    00108
    00109 bool KBookmarkDrag::canDecode( const QMimeSource * e )
    00110 {
    00111     return e->provides("text/uri-list") || e->provides("application/x-xbel") ||
    00112            e->provides("text/plain");
    00113 }
    00114
    00115 QValueList<KBookmark> KBookmarkDrag::decode( const QMimeSource * e )
    00116 {
    00117     QValueList<KBookmark> bookmarks;
    00118     if ( e->provides("application/x-xbel") )
    00119     {
    00120         QByteArray s( e->encodedData("application/x-xbel") );
    00121         //kdDebug(7043) << "KBookmarkDrag::decode s=" << QCString(s) << endl;
    00122         QDomDocument doc;
    00123         doc.setContent( s );
    00124         QDomElement elem = doc.documentElement();
    00125         QDomNodeList children = elem.childNodes();
    00126         for ( uint childno = 0; childno < children.count(); childno++)
    00127         {
    00128            bookmarks.append( KBookmark( children.item(childno).cloneNode(true).toElement() ));
    00129         }
    00130         return bookmarks;
    00131     }
    00132     if ( e->provides("text/uri-list") )
    00133     {
    00134         KURL::List m_lstDragURLs;
    00135         //kdDebug(7043) << "KBookmarkDrag::decode uri-list" << endl;
    00136         if ( KURLDrag::decode( e, m_lstDragURLs ) )
    00137         {
    00138             KURL::List::ConstIterator uit = m_lstDragURLs.begin();
    00139             KURL::List::ConstIterator uEnd = m_lstDragURLs.end();
    00140             for ( ; uit != uEnd ; ++uit )
    00141             {
    00142                 //kdDebug(7043) << "KBookmarkDrag::decode url=" << (*uit).url() << endl;
    00143                 bookmarks.append( KBookmark::standaloneBookmark(
    00144                                         (*uit).prettyURL(), (*uit) ));
    00145             }
    00146             return bookmarks;
    00147         }
    00148     }
    00149     bookmarks.append( KBookmark() );
    00150     return bookmarks;
    00151 }

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:55 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003