kbookmarkdrag.cc
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
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     
00034     QStrList uris;
00035     KURL::List::ConstIterator uit = urls.begin();
00036     KURL::List::ConstIterator uEnd = urls.end();
00037     
00038     
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     
00057     
00058     
00059     
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  ) );
00064     }
00065     
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         
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 ); 
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         
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         
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                 
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.2.0.