libkonq Library API Documentation

konq_drag.cc

00001 /* This file is part of the KDE projects
00002    Copyright (C) 1998, 1999 Torben Weis <weis@kde.org>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "konq_drag.h"
00021 #include <kdebug.h>
00022 
00023 KonqIconDrag::KonqIconDrag( QWidget * dragSource, const char* name )
00024   : QIconDrag( dragSource, name ),
00025     m_bCutSelection( false )
00026 {
00027 }
00028 
00029 const char* KonqIconDrag::format( int i ) const
00030 {
00031     if ( i == 0 )
00032         return "application/x-qiconlist";
00033     else if ( i == 1 )
00034         return "text/uri-list";
00035     else if ( i == 2 )
00036         return "application/x-kde-cutselection";
00037     else if ( i == 3 )
00038         return "text/plain";
00039     else if ( i == 4 ) //These two are imporant because they may end up being format 0,
00040                        //which is what KonqDirPart::updatePasteAction() checks
00041         return "text/plain;charset=ISO-8859-1";
00042     else if ( i == 5 ) //..as well as potentially for interoperability
00043         return "text/plain;charset=UTF-8";
00044 
00045     else return 0;
00046 }
00047 
00048 QByteArray KonqIconDrag::encodedData( const char* mime ) const
00049 {
00050     QByteArray a;
00051     QCString mimetype( mime );
00052     if ( mimetype == "application/x-qiconlist" )
00053         a = QIconDrag::encodedData( mime );
00054     else if ( mimetype == "text/uri-list" ) {
00055         QCString s = urls.join( "\r\n" ).latin1();
00056         if( urls.count() > 0 )
00057             s.append( "\r\n" );
00058         a.resize( s.length() + 1 ); // trailing zero
00059         memcpy( a.data(), s.data(), s.length() + 1 );
00060     }
00061     else if ( mimetype == "application/x-kde-cutselection" ) {
00062         QCString s ( m_bCutSelection ? "1" : "0" );
00063         a.resize( s.length() + 1 ); // trailing zero
00064         memcpy( a.data(), s.data(), s.length() + 1 );
00065     }
00066     else if ( mimetype == "text/plain" ) {
00067         if (!urls.isEmpty())
00068         {
00069             QStringList uris;
00070             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it)
00071                 uris.append(KURL((*it).latin1(), 106).prettyURL()); // 106 is mib enum for utf8 codec
00072             QCString s = uris.join( "\n" ).local8Bit();
00073             a.resize( s.length()); // no trailing zero in clipboard text
00074             memcpy( a.data(), s.data(), s.length());
00075         }
00076     }
00077     else if ( mimetype.lower() == "text/plain;charset=iso-8859-1")
00078     {
00079         if (!urls.isEmpty())
00080         {
00081             QStringList uris;
00082 
00083             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it) 
00084                uris.append(KURL(*it, 106).url(0, 4)); // 106 is mib enum for utf8 codec; 4 for latin1
00085 
00086             QCString s = uris.join( "\n" ).latin1();
00087             a.resize( s.length());
00088             memcpy( a.data(), s.data(), s.length());
00089         }
00090     }
00091     else if ( mimetype.lower() == "text/plain;charset=utf-8")
00092     {
00093         if (!urls.isEmpty())
00094         {
00095             QStringList uris;
00096             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it) 
00097                 uris.append(KURL(*it, 106).prettyURL()); // 106 is mib enum for utf8 codec
00098             QCString s = uris.join( "\n" ).utf8();
00099             a.resize( s.length());
00100             memcpy( a.data(), s.data(), s.length());
00101         }
00102     }
00103     return a;
00104 }
00105 
00106 bool KonqIconDrag::canDecode( const QMimeSource* e )
00107 {
00108     return  e->provides( "application/x-qiconlist" ) ||
00109       e->provides( "text/uri-list" ) ||
00110       e->provides( "application/x-kde-cutselection" );
00111 }
00112 
00113 void KonqIconDrag::append( const QIconDragItem &item, const QRect &pr,
00114                              const QRect &tr, const QString &url )
00115 {
00116     QIconDrag::append( item, pr, tr );
00117     urls.append(url);
00118 }
00119 
00120 //
00121 
00122 KonqDrag * KonqDrag::newDrag( const KURL::List & urls, bool move, QWidget * dragSource, const char* name )
00123 {
00124     // See KURLDrag::newDrag
00125     QStrList uris;
00126     KURL::List::ConstIterator uit = urls.begin();
00127     KURL::List::ConstIterator uEnd = urls.end();
00128     // Get each URL encoded in utf8 - and since we get it in escaped
00129     // form on top of that, .latin1() is fine.
00130     for ( ; uit != uEnd ; ++uit )
00131         uris.append( (*uit).url(0, 106).latin1() ); // 106 is mib enum for utf8 codec
00132     return new KonqDrag( uris, move, dragSource, name );
00133 }
00134 
00135 KonqDrag::KonqDrag( const QStrList & urls, bool move, QWidget * dragSource, const char* name )
00136   : QUriDrag( urls, dragSource, name ),
00137     m_bCutSelection( move ), m_urls( urls )
00138 {}
00139 
00140 const char* KonqDrag::format( int i ) const
00141 {
00142     if ( i == 0 )
00143         return "text/uri-list";
00144     else if ( i == 1 )
00145         return "application/x-kde-cutselection";
00146     else if ( i == 2 )
00147         return "text/plain";
00148     else return 0;
00149 }
00150 
00151 QByteArray KonqDrag::encodedData( const char* mime ) const
00152 {
00153     QByteArray a;
00154     QCString mimetype( mime );
00155     if ( mimetype == "text/uri-list" )
00156         return QUriDrag::encodedData( mime );
00157     else if ( mimetype == "application/x-kde-cutselection" ) {
00158         QCString s ( m_bCutSelection ? "1" : "0" );
00159         a.resize( s.length() + 1 ); // trailing zero
00160         memcpy( a.data(), s.data(), s.length() + 1 );
00161     }
00162     else if ( mimetype == "text/plain" )
00163     {
00164         QStringList uris;
00165         for (QStrListIterator it(m_urls); *it; ++it)
00166             uris.append(KURL(*it, 106).prettyURL()); // 106 is mib enum for utf8 codec
00167         QCString s = uris.join( "\n" ).local8Bit();
00168         a.resize( s.length() + 1 ); // trailing zero
00169         memcpy( a.data(), s.data(), s.length() + 1 );
00170     }
00171     return a;
00172 }
00173 
00174 //
00175 
00176 // Used for KonqIconDrag too
00177 
00178 bool KonqDrag::decodeIsCutSelection( const QMimeSource *e )
00179 {
00180   QByteArray a = e->encodedData( "application/x-kde-cutselection" );
00181   if ( a.isEmpty() )
00182     return false;
00183   else
00184   {
00185     return (a.at(0) == '1'); // true if "1", or similar
00186   }
00187 }
00188 
00189 #include "konq_drag.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Thu Jan 29 23:03:28 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001