You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
6.0 KiB
168 lines
6.0 KiB
15 years ago
|
/* This file is part of the KDE libraries
|
||
|
Copyright (C) 2000 David Faure <faure@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2 as published by the Free Software Foundation.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include "kbookmarkdrag.h"
|
||
|
#include <kurldrag.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
15 years ago
|
KBookmarkDrag * KBookmarkDrag::newDrag( const TQValueList<KBookmark> & bookmarks, TQWidget * dragSource, const char * name )
|
||
15 years ago
|
{
|
||
|
KURL::List urls;
|
||
|
|
||
15 years ago
|
for ( TQValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
|
||
15 years ago
|
urls.append( (*it).url() );
|
||
|
}
|
||
|
|
||
|
// See KURLDrag::newDrag
|
||
15 years ago
|
TQStrList uris;
|
||
15 years ago
|
KURL::List::ConstIterator uit = urls.begin();
|
||
|
KURL::List::ConstIterator uEnd = urls.end();
|
||
|
// Get each URL encoded in utf8 - and since we get it in escaped
|
||
|
// form on top of that, .latin1() is fine.
|
||
|
for ( ; uit != uEnd ; ++uit )
|
||
|
uris.append( KURLDrag::urlToString(*uit).latin1() );
|
||
|
|
||
|
return new KBookmarkDrag( bookmarks, uris, dragSource, name );
|
||
|
}
|
||
|
|
||
15 years ago
|
KBookmarkDrag * KBookmarkDrag::newDrag( const KBookmark & bookmark, TQWidget * dragSource, const char * name )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<KBookmark> bookmarks;
|
||
15 years ago
|
bookmarks.append( KBookmark(bookmark) );
|
||
|
return newDrag(bookmarks, dragSource, name);
|
||
|
}
|
||
|
|
||
15 years ago
|
KBookmarkDrag::KBookmarkDrag( const TQValueList<KBookmark> & bookmarks, const TQStrList & urls,
|
||
|
TQWidget * dragSource, const char * name )
|
||
|
: TQUriDrag( urls, dragSource, name ), m_bookmarks( bookmarks ), m_doc("xbel")
|
||
15 years ago
|
{
|
||
|
// We need to create the XML for this drag right now and not
|
||
13 years ago
|
// in encodedData because when cutting a folder, the children
|
||
|
// wouldn't be part of the bookmarks anymore, when encodedData
|
||
15 years ago
|
// is requested.
|
||
15 years ago
|
TQDomElement elem = m_doc.createElement("xbel");
|
||
15 years ago
|
m_doc.appendChild( elem );
|
||
15 years ago
|
for ( TQValueListConstIterator<KBookmark> it = bookmarks.begin(); it != bookmarks.end(); ++it ) {
|
||
15 years ago
|
elem.appendChild( (*it).internalElement().cloneNode( true /* deep */ ) );
|
||
|
}
|
||
|
//kdDebug(7043) << "KBookmarkDrag::KBookmarkDrag " << m_doc.toString() << endl;
|
||
|
}
|
||
|
|
||
|
const char* KBookmarkDrag::format( int i ) const
|
||
|
{
|
||
|
if ( i == 0 )
|
||
|
return "application/x-xbel";
|
||
|
else if ( i == 1 )
|
||
|
return "text/uri-list";
|
||
|
else if ( i == 2 )
|
||
|
return "text/plain";
|
||
|
else return 0;
|
||
|
}
|
||
|
|
||
13 years ago
|
TQByteArray KBookmarkDrag::encodedData( const char* mime ) const
|
||
15 years ago
|
{
|
||
15 years ago
|
TQByteArray a;
|
||
|
TQCString mimetype( mime );
|
||
15 years ago
|
if ( mimetype == "text/uri-list" )
|
||
13 years ago
|
return TQUriDrag::encodedData( mime );
|
||
15 years ago
|
else if ( mimetype == "application/x-xbel" )
|
||
|
{
|
||
|
a = m_doc.toCString();
|
||
13 years ago
|
//kdDebug(7043) << "KBookmarkDrag::encodedData " << m_doc.toCString() << endl;
|
||
15 years ago
|
}
|
||
|
else if ( mimetype == "text/plain" )
|
||
|
{
|
||
|
KURL::List m_lstDragURLs;
|
||
|
if ( KURLDrag::decode( this, m_lstDragURLs ) )
|
||
|
{
|
||
15 years ago
|
TQStringList uris;
|
||
15 years ago
|
KURL::List::ConstIterator uit = m_lstDragURLs.begin();
|
||
|
KURL::List::ConstIterator uEnd = m_lstDragURLs.end();
|
||
|
for ( ; uit != uEnd ; ++uit )
|
||
|
uris.append( (*uit).prettyURL() );
|
||
|
|
||
15 years ago
|
TQCString s = uris.join( "\n" ).local8Bit();
|
||
15 years ago
|
a.resize( s.length() + 1 ); // trailing zero
|
||
|
memcpy( a.data(), s.data(), s.length() + 1 );
|
||
|
}
|
||
|
}
|
||
|
return a;
|
||
|
}
|
||
|
|
||
15 years ago
|
bool KBookmarkDrag::canDecode( const TQMimeSource * e )
|
||
15 years ago
|
{
|
||
|
return e->provides("text/uri-list") || e->provides("application/x-xbel") ||
|
||
|
e->provides("text/plain");
|
||
|
}
|
||
|
|
||
15 years ago
|
TQValueList<KBookmark> KBookmarkDrag::decode( const TQMimeSource * e )
|
||
15 years ago
|
{
|
||
15 years ago
|
TQValueList<KBookmark> bookmarks;
|
||
15 years ago
|
if ( e->provides("application/x-xbel") )
|
||
|
{
|
||
13 years ago
|
TQByteArray s( e->encodedData("application/x-xbel") );
|
||
15 years ago
|
//kdDebug(7043) << "KBookmarkDrag::decode s=" << TQCString(s) << endl;
|
||
|
TQDomDocument doc;
|
||
15 years ago
|
doc.setContent( s );
|
||
15 years ago
|
TQDomElement elem = doc.documentElement();
|
||
|
TQDomNodeList children = elem.childNodes();
|
||
15 years ago
|
for ( uint childno = 0; childno < children.count(); childno++)
|
||
|
{
|
||
|
bookmarks.append( KBookmark( children.item(childno).cloneNode(true).toElement() ));
|
||
|
}
|
||
|
return bookmarks;
|
||
|
}
|
||
|
if ( e->provides("text/uri-list") )
|
||
|
{
|
||
|
KURL::List m_lstDragURLs;
|
||
|
//kdDebug(7043) << "KBookmarkDrag::decode uri-list" << endl;
|
||
|
if ( KURLDrag::decode( e, m_lstDragURLs ) )
|
||
|
{
|
||
|
KURL::List::ConstIterator uit = m_lstDragURLs.begin();
|
||
|
KURL::List::ConstIterator uEnd = m_lstDragURLs.end();
|
||
|
for ( ; uit != uEnd ; ++uit )
|
||
|
{
|
||
|
//kdDebug(7043) << "KBookmarkDrag::decode url=" << (*uit).url() << endl;
|
||
|
bookmarks.append( KBookmark::standaloneBookmark(
|
||
|
(*uit).prettyURL(), (*uit) ));
|
||
|
}
|
||
|
return bookmarks;
|
||
|
}
|
||
|
}
|
||
|
if( e->provides("text/plain") )
|
||
|
{
|
||
|
//kdDebug(7043) << "KBookmarkDrag::decode text/plain" << endl;
|
||
15 years ago
|
TQString s;
|
||
|
if(TQTextDrag::decode( e, s ))
|
||
15 years ago
|
{
|
||
|
|
||
15 years ago
|
TQStringList listDragURLs = TQStringList::split(TQChar('\n'), s);
|
||
|
TQStringList::ConstIterator it = listDragURLs.begin();
|
||
|
TQStringList::ConstIterator end = listDragURLs.end();
|
||
15 years ago
|
for( ; it!=end; ++it)
|
||
|
{
|
||
|
//kdDebug(7043)<<"KBookmarkDrag::decode string"<<(*it)<<endl;
|
||
|
bookmarks.append( KBookmark::standaloneBookmark( KURL(*it).prettyURL(), KURL(*it)));
|
||
|
}
|
||
|
return bookmarks;
|
||
|
}
|
||
|
}
|
||
|
bookmarks.append( KBookmark() );
|
||
|
return bookmarks;
|
||
|
}
|