kbookmarkdrag.cc
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kbookmarkdrag.h"
00020 #include <kurldrag.h>
00021 #include <kdebug.h>
00022
00023 KBookmarkDrag * KBookmarkDrag::newDrag( const QValueList<KBookmark> & bookmarks, QWidget * dragSource, const char * name )
00024 {
00025 KURL::List urls;
00026
00027 for ( QValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
00028 urls.append( (*it).url() );
00029 }
00030
00031
00032 QStrList uris;
00033 KURL::List::ConstIterator uit = urls.begin();
00034 KURL::List::ConstIterator uEnd = urls.end();
00035
00036
00037 for ( ; uit != uEnd ; ++uit )
00038 uris.append( (*uit).url(0, 106).latin1() );
00039
00040 return new KBookmarkDrag( bookmarks, uris, dragSource, name );
00041 }
00042
00043 KBookmarkDrag * KBookmarkDrag::newDrag( const KBookmark & bookmark, QWidget * dragSource, const char * name )
00044 {
00045 QValueList<KBookmark> bookmarks;
00046 bookmarks.append( KBookmark(bookmark) );
00047 return newDrag(bookmarks, dragSource, name);
00048 }
00049
00050 KBookmarkDrag::KBookmarkDrag( const QValueList<KBookmark> & bookmarks, const QStrList & urls,
00051 QWidget * dragSource, const char * name )
00052 : QUriDrag( urls, dragSource, name ), m_bookmarks( bookmarks ), m_doc("xbel")
00053 {
00054
00055
00056
00057
00058 QDomElement elem = m_doc.createElement("xbel");
00059 m_doc.appendChild( elem );
00060 for ( QValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
00061 elem.appendChild( (*it).internalElement().cloneNode( true ) );
00062 }
00063 kdDebug(1203) << "KBookmarkDrag::KBookmarkDrag " << m_doc.toString() << endl;
00064 }
00065
00066 const char* KBookmarkDrag::format( int i ) const
00067 {
00068 if ( i == 0 )
00069 return "application/x-xbel";
00070 else if ( i == 1 )
00071 return "text/uri-list";
00072 else if ( i == 2 )
00073 return "text/plain";
00074 else return 0;
00075 }
00076
00077 QByteArray KBookmarkDrag::encodedData( const char* mime ) const
00078 {
00079 QByteArray a;
00080 QCString mimetype( mime );
00081 if ( mimetype == "text/uri-list" )
00082 return QUriDrag::encodedData( mime );
00083 else if ( mimetype == "application/x-xbel" )
00084 {
00085 a = m_doc.toCString();
00086 kdDebug() << "KBookmarkDrag::encodedData " << m_doc.toCString() << endl;
00087 }
00088 else if ( mimetype == "text/plain" )
00089 {
00090 KURL::List m_lstDragURLs;
00091 if ( KURLDrag::decode( this, m_lstDragURLs ) )
00092 {
00093 QStringList uris;
00094 KURL::List::ConstIterator uit = m_lstDragURLs.begin();
00095 KURL::List::ConstIterator uEnd = m_lstDragURLs.end();
00096 for ( ; uit != uEnd ; ++uit )
00097 uris.append( (*uit).prettyURL() );
00098
00099 QCString s = uris.join( "\n" ).local8Bit();
00100 a.resize( s.length() + 1 );
00101 memcpy( a.data(), s.data(), s.length() + 1 );
00102 }
00103 }
00104 return a;
00105 }
00106
00107 bool KBookmarkDrag::canDecode( const QMimeSource * e )
00108 {
00109 return e->provides("text/uri-list") || e->provides("application/x-xbel") ||
00110 e->provides("text/plain");
00111 }
00112
00113 QValueList<KBookmark> KBookmarkDrag::decode( const QMimeSource * e )
00114 {
00115 QValueList<KBookmark> bookmarks;
00116 if ( e->provides("application/x-xbel") )
00117 {
00118 QCString s( e->encodedData("application/x-xbel") );
00119 kdDebug(1203) << "KBookmarkDrag::decode s=" << s << endl;
00120 QDomDocument doc;
00121 doc.setContent( s );
00122 QDomElement elem = doc.documentElement();
00123 QDomNodeList children = elem.childNodes();
00124 for ( uint childno = 0; childno < children.count(); childno++)
00125 {
00126 bookmarks.append( KBookmark( children.item(childno).cloneNode(true).toElement() ));
00127 }
00128 return bookmarks;
00129 }
00130 if ( e->provides("text/uri-list") )
00131 {
00132 KURL::List m_lstDragURLs;
00133 if ( KURLDrag::decode( e, m_lstDragURLs ) )
00134 {
00135
00136 if ( m_lstDragURLs.count() > 1 )
00137 kdWarning() << "Only first URL inserted, known limitation" << endl;
00138
00139 bookmarks.append( KBookmark::standaloneBookmark( m_lstDragURLs.first().fileName(), m_lstDragURLs.first() ));
00140 return bookmarks;
00141 }
00142 }
00143 bookmarks.append( KBookmark() );
00144 return bookmarks;
00145 }
This file is part of the documentation for kdelibs Version 3.1.0.