KDE3Support
k3bookmarkdrag.cpp
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 // See KURLDrag::newDrag 00035 Q3StrList uris; 00036 KUrl::List::ConstIterator uit = urls.constBegin(); 00037 KUrl::List::ConstIterator uEnd = urls.constEnd(); 00038 // Get each URL encoded in utf8 - and since we get it in escaped 00039 // form on top of that, .toLatin1().constData() is fine. 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 // We need to create the XML for this drag right now and not 00058 // in encodedData because when cutting a folder, the children 00059 // wouldn't be part of the bookmarks anymore, when encodedData 00060 // is requested. 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 /* deep */ ) ); 00065 } 00066 //kDebug(7043) << "K3BookmarkDrag::K3BookmarkDrag " << m_doc.toString(); 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 //kDebug(7043) << "K3BookmarkDrag::encodedData " << m_doc.toCString(); 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 ); // trailing zero 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 //kDebug(7043) << "K3BookmarkDrag::decode s=" << QCString(s); 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 //kDebug(7043) << "K3BookmarkDrag::decode uri-list"; 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 //kDebug(7043) << "K3BookmarkDrag::decode url=" << (*uit).url(); 00144 bookmarks.append( KBookmark::standaloneBookmark( 00145 (*uit).prettyUrl(), (*uit) )); 00146 } 00147 return bookmarks; 00148 } 00149 } 00150 if( e->provides("text/plain") ) 00151 { 00152 //kDebug(7043) << "K3BookmarkDrag::decode text/plain"; 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 //kDebug(7043)<<"K3BookmarkDrag::decode string"<<(*it); 00163 bookmarks.append( KBookmark::standaloneBookmark( KUrl(*it).prettyUrl(), KUrl(*it))); 00164 } 00165 return bookmarks; 00166 } 00167 } 00168 bookmarks.append( KBookmark() ); 00169 return bookmarks; 00170 }
KDE 4.6 API Reference