KDE3Support
k3bookmarkdrag.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "k3bookmarkdrag.h"
00022 #include <k3urldrag.h>
00023 #include <kdebug.h>
00024 #include <Qt3Support/Q3CString>
00025
00026 K3BookmarkDrag * K3BookmarkDrag::newDrag( const Q3ValueList<KBookmark> & bookmarks, QWidget * dragSource, const char * name )
00027 {
00028 KUrl::List urls;
00029
00030 for ( Q3ValueListConstIterator<KBookmark> it = bookmarks.constBegin(); it != bookmarks.constEnd(); ++it ) {
00031 urls.append( (*it).url() );
00032 }
00033
00034
00035 Q3StrList uris;
00036 KUrl::List::ConstIterator uit = urls.constBegin();
00037 KUrl::List::ConstIterator uEnd = urls.constEnd();
00038
00039
00040 for ( ; uit != uEnd ; ++uit )
00041 uris.append( K3URLDrag::urlToString(*uit).toLatin1() );
00042
00043 return new K3BookmarkDrag( bookmarks, uris, dragSource, name );
00044 }
00045
00046 K3BookmarkDrag * K3BookmarkDrag::newDrag( const KBookmark & bookmark, QWidget * dragSource, const char * name )
00047 {
00048 Q3ValueList<KBookmark> bookmarks;
00049 bookmarks.append( KBookmark(bookmark) );
00050 return newDrag(bookmarks, dragSource, name);
00051 }
00052
00053 K3BookmarkDrag::K3BookmarkDrag( const Q3ValueList<KBookmark> & bookmarks, const Q3StrList & urls,
00054 QWidget * dragSource, const char * name )
00055 : Q3UriDrag( urls, dragSource, name ), m_bookmarks( bookmarks ), m_doc("xbel")
00056 {
00057
00058
00059
00060
00061 QDomElement elem = m_doc.createElement("xbel");
00062 m_doc.appendChild( elem );
00063 for ( Q3ValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
00064 elem.appendChild( (*it).internalElement().cloneNode( true ) );
00065 }
00066
00067 }
00068
00069 const char* K3BookmarkDrag::format( int i ) const
00070 {
00071 if ( i == 0 )
00072 return "application/x-xbel";
00073 else if ( i == 1 )
00074 return "text/uri-list";
00075 else if ( i == 2 )
00076 return "text/plain";
00077 else return 0;
00078 }
00079
00080 QByteArray K3BookmarkDrag::encodedData( const char* mime ) const
00081 {
00082 QByteArray a;
00083 Q3CString mimetype( mime );
00084 if ( mimetype == "text/uri-list" )
00085 return Q3UriDrag::encodedData( mime );
00086 else if ( mimetype == "application/x-xbel" )
00087 {
00088 a = m_doc.toByteArray();
00089
00090 }
00091 else if ( mimetype == "text/plain" )
00092 {
00093 KUrl::List m_lstDragURLs;
00094 if ( K3URLDrag::decode( this, m_lstDragURLs ) )
00095 {
00096 QStringList uris;
00097 KUrl::List::ConstIterator uit = m_lstDragURLs.constBegin();
00098 KUrl::List::ConstIterator uEnd = m_lstDragURLs.constEnd();
00099 for ( ; uit != uEnd ; ++uit )
00100 uris.append( (*uit).prettyUrl() );
00101
00102 Q3CString s = uris.join( "\n" ).toLocal8Bit();
00103 a.resize( s.length() + 1 );
00104 memcpy( a.data(), s.data(), s.length() + 1 );
00105 }
00106 }
00107 return a;
00108 }
00109
00110 bool K3BookmarkDrag::canDecode( const QMimeSource * e )
00111 {
00112 return e->provides("text/uri-list") || e->provides("application/x-xbel") ||
00113 e->provides("text/plain");
00114 }
00115
00116 Q3ValueList<KBookmark> K3BookmarkDrag::decode( const QMimeSource * e )
00117 {
00118 Q3ValueList<KBookmark> bookmarks;
00119 if ( e->provides("application/x-xbel") )
00120 {
00121 QByteArray s( e->encodedData("application/x-xbel") );
00122
00123 QDomDocument doc;
00124 doc.setContent( s );
00125 QDomElement elem = doc.documentElement();
00126 QDomNodeList children = elem.childNodes();
00127 for ( int childno = 0; childno < children.count(); childno++)
00128 {
00129 bookmarks.append( KBookmark( children.item(childno).cloneNode(true).toElement() ));
00130 }
00131 return bookmarks;
00132 }
00133 if ( e->provides("text/uri-list") )
00134 {
00135 KUrl::List m_lstDragURLs;
00136
00137 if ( K3URLDrag::decode( e, m_lstDragURLs ) )
00138 {
00139 KUrl::List::ConstIterator uit = m_lstDragURLs.constBegin();
00140 KUrl::List::ConstIterator uEnd = m_lstDragURLs.constEnd();
00141 for ( ; uit != uEnd ; ++uit )
00142 {
00143
00144 bookmarks.append( KBookmark::standaloneBookmark(
00145 (*uit).prettyUrl(), (*uit) ));
00146 }
00147 return bookmarks;
00148 }
00149 }
00150 if( e->provides("text/plain") )
00151 {
00152
00153 QString s;
00154 if(Q3TextDrag::decode( e, s ))
00155 {
00156
00157 QStringList listDragURLs = s.split(QChar('\n'), QString::SkipEmptyParts);
00158 QStringList::ConstIterator it = listDragURLs.constBegin();
00159 QStringList::ConstIterator end = listDragURLs.constEnd();
00160 for( ; it!=end; ++it)
00161 {
00162
00163 bookmarks.append( KBookmark::standaloneBookmark( KUrl(*it).prettyUrl(), KUrl(*it)));
00164 }
00165 return bookmarks;
00166 }
00167 }
00168 bookmarks.append( KBookmark() );
00169 return bookmarks;
00170 }