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 #include <kurldrag.h>
00023 
00024 KonqIconDrag::KonqIconDrag( QWidget * dragSource, const char* name )
00025   : QIconDrag( dragSource, name ),
00026     m_bCutSelection( false )
00027 {
00028 }
00029 
00030 const char* KonqIconDrag::format( int i ) const
00031 {
00032     if ( i == 0 )
00033     return "application/x-qiconlist";
00034     else if ( i == 1 )
00035     return "text/uri-list";
00036     else if ( i == 2 )
00037         return "application/x-kde-cutselection";
00038     else if ( i == 3 )
00039         return "text/plain";
00040     else if ( i == 4 ) //These two are imporant because they may end up being format 0,
00041                        //which is what KonqDirPart::updatePasteAction() checks
00042         return "text/plain;charset=ISO-8859-1";
00043     else if ( i == 5 ) //..as well as potentially for interoperability
00044         return "text/plain;charset=UTF-8";
00045 
00046     else return 0;
00047 }
00048 
00049 QByteArray KonqIconDrag::encodedData( const char* mime ) const
00050 {
00051     QByteArray a;
00052     QCString mimetype( mime );
00053     if ( mimetype == "application/x-qiconlist" )
00054         a = QIconDrag::encodedData( mime );
00055     else if ( mimetype == "text/uri-list" ) {
00056         QCString s = urls.join( "\r\n" ).latin1();
00057         if( urls.count() > 0 )
00058             s.append( "\r\n" );
00059         a.resize( s.length() + 1 ); // trailing zero
00060         memcpy( a.data(), s.data(), s.length() + 1 );
00061     }
00062     else if ( mimetype == "application/x-kde-cutselection" ) {
00063         QCString s ( m_bCutSelection ? "1" : "0" );
00064         a.resize( s.length() + 1 ); // trailing zero
00065         memcpy( a.data(), s.data(), s.length() + 1 );
00066     }
00067     else if ( mimetype == "text/plain" ) {
00068         if (!urls.isEmpty())
00069         {
00070             QStringList uris;
00071             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it)
00072                 uris.append(KURLDrag::stringToUrl((*it).latin1()).prettyURL());
00073             QCString s = uris.join( "\n" ).local8Bit();
00074             if( uris.count() > 1 )
00075                 s.append( "\n" );
00076             a.resize( s.length()); // no trailing zero in clipboard text
00077             memcpy( a.data(), s.data(), s.length());
00078         }
00079     }
00080     else if ( mimetype.lower() == "text/plain;charset=iso-8859-1")
00081     {
00082         if (!urls.isEmpty())
00083         {
00084             QStringList uris;
00085 
00086             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it) 
00087                uris.append(KURLDrag::stringToUrl((*it).latin1()).url(0, 4)); // 4 for latin1
00088 
00089             QCString s = uris.join( "\n" ).latin1();
00090             if( uris.count() > 1 )
00091                 s.append( "\n" );
00092             a.resize( s.length());
00093             memcpy( a.data(), s.data(), s.length());
00094         }
00095     }
00096     else if ( mimetype.lower() == "text/plain;charset=utf-8")
00097     {
00098         if (!urls.isEmpty())
00099         {
00100             QStringList uris;
00101             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it) 
00102                 uris.append(KURLDrag::stringToUrl((*it).latin1()).prettyURL());
00103             QCString s = uris.join( "\n" ).utf8();
00104             if( uris.count() > 1 )
00105                 s.append( "\n" );
00106             a.resize( s.length());
00107             memcpy( a.data(), s.data(), s.length());
00108         }
00109     }
00110     return a;
00111 }
00112 
00113 bool KonqIconDrag::canDecode( const QMimeSource* e )
00114 {
00115     return  e->provides( "application/x-qiconlist" ) ||
00116       e->provides( "text/uri-list" ) ||
00117       e->provides( "application/x-kde-cutselection" );
00118 }
00119 
00120 void KonqIconDrag::append( const QIconDragItem &item, const QRect &pr,
00121                              const QRect &tr, const QString &url )
00122 {
00123     QIconDrag::append( item, pr, tr );
00124     urls.append( url );
00125 }
00126 
00127 //
00128 
00129 KonqDrag * KonqDrag::newDrag( const KURL::List & urls, bool move, QWidget * dragSource, const char* name )
00130 {
00131     // See KURLDrag::newDrag
00132     QStrList uris;
00133     KURL::List::ConstIterator uit = urls.begin();
00134     KURL::List::ConstIterator uEnd = urls.end();
00135     // Get each URL encoded in utf8 - and since we get it in escaped
00136     // form on top of that, .latin1() is fine.
00137     for ( ; uit != uEnd ; ++uit )
00138         uris.append( KURLDrag::urlToString( *uit ).latin1() );
00139     return new KonqDrag( uris, move, dragSource, name );
00140 }
00141 
00142 // urls must be already checked to have hostname in file URLs
00143 KonqDrag::KonqDrag( const QStrList & urls, bool move, QWidget * dragSource, const char* name )
00144   : QUriDrag( urls, dragSource, name ),
00145     m_bCutSelection( move ), m_urls( urls )
00146 {}
00147 
00148 const char* KonqDrag::format( int i ) const
00149 {
00150     if ( i == 0 )
00151     return "text/uri-list";
00152     else if ( i == 1 )
00153         return "application/x-kde-cutselection";
00154     else if ( i == 2 )
00155         return "text/plain";
00156     else return 0;
00157 }
00158 
00159 QByteArray KonqDrag::encodedData( const char* mime ) const
00160 {
00161     QByteArray a;
00162     QCString mimetype( mime );
00163     if ( mimetype == "text/uri-list" )
00164         return QUriDrag::encodedData( mime );
00165     else if ( mimetype == "application/x-kde-cutselection" ) {
00166         QCString s ( m_bCutSelection ? "1" : "0" );
00167     a.resize( s.length() + 1 ); // trailing zero
00168     memcpy( a.data(), s.data(), s.length() + 1 );
00169     }
00170     else if ( mimetype == "text/plain" )
00171     {
00172         QStringList uris;
00173         for (QStrListIterator it(m_urls); *it; ++it)
00174             uris.append(KURLDrag::stringToUrl(*it).prettyURL());
00175         QCString s = uris.join( "\n" ).local8Bit();
00176         if( uris.count() > 1 )
00177             s.append( "\n" );
00178         a.resize( s.length() + 1 ); // trailing zero
00179         memcpy( a.data(), s.data(), s.length() + 1 );
00180     }
00181     return a;
00182 }
00183 
00184 //
00185 
00186 // Used for KonqIconDrag too
00187 
00188 bool KonqDrag::decodeIsCutSelection( const QMimeSource *e )
00189 {
00190   QByteArray a = e->encodedData( "application/x-kde-cutselection" );
00191   if ( a.isEmpty() )
00192     return false;
00193   else
00194   {
00195     kdDebug(1203) << "KonqDrag::decodeIsCutSelection : a=" << QCString(a.data(), a.size() + 1) << endl;
00196     return (a.at(0) == '1'); // true if 1
00197   }
00198 }
00199 
00200 #include "konq_drag.moc"
KDE Logo
This file is part of the documentation for libkonq Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 6 02:40:56 2005 by doxygen 1.4.0 written by Dimitri van Heesch, © 1997-2003