kio Library API Documentation

kbookmarkdrag.cc

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
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     // See KURLDrag::newDrag
00032     QStrList uris;
00033     KURL::List::ConstIterator uit = urls.begin();
00034     KURL::List::ConstIterator uEnd = urls.end();
00035     // Get each URL encoded in utf8 - and since we get it in escaped
00036     // form on top of that, .latin1() is fine.
00037     for ( ; uit != uEnd ; ++uit )
00038         uris.append( (*uit).url(0, 106).latin1() ); // 106 is mib enum for utf8 codec
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     // We need to create the XML for this drag right now and not
00055     // in encodedData because when cutting a folder, the children
00056     // wouldn't be part of the bookmarks anymore, when encodedData
00057     // is requested.
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 /* deep */ ) );
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 ); // trailing zero
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             // FIXME iterate through them!!!
00136             if ( m_lstDragURLs.count() > 1 )
00137                 kdWarning() << "Only first URL inserted, known limitation" << endl;
00138             //kdDebug(1203) << "KBookmarkDrag::decode url=" << m_lstDragURLs.first().url() << endl;
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 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:28 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001