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., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, 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     // warning, don't add anything here without checking KonqIconDrag2
00046 
00047     else return 0;
00048 }
00049 
00050 QByteArray KonqIconDrag::encodedData( const char* mime ) const
00051 {
00052     QByteArray a;
00053     QCString mimetype( mime );
00054     if ( mimetype == "application/x-qiconlist" )
00055         a = QIconDrag::encodedData( mime );
00056     else if ( mimetype == "text/uri-list" ) {
00057         QCString s = urls.join( "\r\n" ).latin1();
00058         if( urls.count() > 0 )
00059             s.append( "\r\n" );
00060         a.resize( s.length() + 1 ); // trailing zero
00061         memcpy( a.data(), s.data(), s.length() + 1 );
00062     }
00063     else if ( mimetype == "application/x-kde-cutselection" ) {
00064         QCString s ( m_bCutSelection ? "1" : "0" );
00065         a.resize( s.length() + 1 ); // trailing zero
00066         memcpy( a.data(), s.data(), s.length() + 1 );
00067     }
00068     else if ( mimetype == "text/plain" ) {
00069         if (!urls.isEmpty())
00070         {
00071             QStringList uris;
00072             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it)
00073                 uris.append(KURLDrag::stringToUrl((*it).latin1()).prettyURL());
00074             QCString s = uris.join( "\n" ).local8Bit();
00075             if( uris.count() > 1 )
00076                 s.append( "\n" );
00077             a.resize( s.length()); // no trailing zero in clipboard text
00078             memcpy( a.data(), s.data(), s.length());
00079         }
00080     }
00081     else if ( mimetype.lower() == "text/plain;charset=iso-8859-1")
00082     {
00083         if (!urls.isEmpty())
00084         {
00085             QStringList uris;
00086 
00087             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it) 
00088                uris.append(KURLDrag::stringToUrl((*it).latin1()).url(0, 4)); // 4 for latin1
00089 
00090             QCString s = uris.join( "\n" ).latin1();
00091             if( uris.count() > 1 )
00092                 s.append( "\n" );
00093             a.resize( s.length());
00094             memcpy( a.data(), s.data(), s.length());
00095         }
00096     }
00097     else if ( mimetype.lower() == "text/plain;charset=utf-8")
00098     {
00099         if (!urls.isEmpty())
00100         {
00101             QStringList uris;
00102             for (QStringList::ConstIterator it = urls.begin(); it != urls.end(); ++it) 
00103                 uris.append(KURLDrag::stringToUrl((*it).latin1()).prettyURL());
00104             QCString s = uris.join( "\n" ).utf8();
00105             if( uris.count() > 1 )
00106                 s.append( "\n" );
00107             a.resize( s.length());
00108             memcpy( a.data(), s.data(), s.length());
00109         }
00110     }
00111     return a;
00112 }
00113 
00114 bool KonqIconDrag::canDecode( const QMimeSource* e )
00115 {
00116     return  e->provides( "application/x-qiconlist" ) ||
00117       e->provides( "text/uri-list" ) ||
00118       e->provides( "application/x-kde-cutselection" );
00119 }
00120 
00121 void KonqIconDrag::append( const QIconDragItem &item, const QRect &pr,
00122                              const QRect &tr, const QString &url )
00123 {
00124     QIconDrag::append( item, pr, tr );
00125     urls.append( url );
00126 }
00127 
00128 KonqIconDrag2::KonqIconDrag2( QWidget * dragSource )
00129     : KonqIconDrag( dragSource )
00130 {
00131 }
00132 
00133 void KonqIconDrag2::append( const QIconDragItem &item, const QRect &pr,
00134                             const QRect &tr, const QString& url, const KURL &mostLocalURL )
00135 {
00136     QString mostLocalURLStr = KURLDrag::urlToString(mostLocalURL);
00137     m_kdeURLs.append( url );
00138     KonqIconDrag::append( item, pr, tr, mostLocalURLStr );
00139 }
00140 
00141 const char* KonqIconDrag2::format( int i ) const
00142 {
00143     if ( i == 6 )
00144         return "application/x-kde-urilist";
00145     return KonqIconDrag::format( i );
00146 }
00147 
00148 QByteArray KonqIconDrag2::encodedData( const char* mime ) const
00149 {
00150     QCString mimetype( mime );
00151     if ( mimetype == "application/x-kde-urilist" )
00152     {
00153         QByteArray a;
00154         int c=0;
00155         for (QStringList::ConstIterator it = m_kdeURLs.begin(); it != m_kdeURLs.end(); ++it) {
00156             QCString url = (*it).utf8();
00157             int l = url.length();
00158             a.resize(c+l+2);
00159             memcpy(a.data()+c, url.data(), l);
00160             memcpy(a.data()+c+l,"\r\n",2);
00161             c += l+2;
00162         }
00163         a.resize(c+1);
00164         a[c] = 0;
00165         return a;
00166     }
00167     return KonqIconDrag::encodedData( mime );
00168 }
00169 
00170 //
00171 
00172 KonqDrag * KonqDrag::newDrag( const KURL::List & urls, bool cut, QWidget * dragSource, const char* name )
00173 {
00174     // See KURLDrag::newDrag
00175     QStrList uris;
00176     KURL::List::ConstIterator uit = urls.begin();
00177     KURL::List::ConstIterator uEnd = urls.end();
00178     // Get each URL encoded in utf8 - and since we get it in escaped
00179     // form on top of that, .latin1() is fine.
00180     for ( ; uit != uEnd ; ++uit )
00181         uris.append( KURLDrag::urlToString( *uit ).latin1() );
00182     return new KonqDrag( uris, cut, dragSource, name );
00183 }
00184 
00185 // urls must be already checked to have hostname in file URLs
00186 KonqDrag::KonqDrag( const QStrList & urls, bool cut, QWidget * dragSource, const char* name )
00187   : QUriDrag( urls, dragSource, name ),
00188     m_bCutSelection( cut ), m_urls( urls )
00189 {}
00190 
00191 // urls must be already checked to have hostname in file URLs
00192 KonqDrag::KonqDrag( const KURL::List & urls, const KURL::List& mostLocalUrls,
00193                     bool cut, QWidget * dragSource )
00194     : QUriDrag( dragSource ),
00195       m_bCutSelection( cut )
00196 {
00197     QStrList uris;
00198     KURL::List::ConstIterator uit = urls.begin();
00199     KURL::List::ConstIterator uEnd = urls.end();
00200     // Get each URL encoded in utf8 - and since we get it in escaped
00201     // form on top of that, .latin1() is fine.
00202     for ( ; uit != uEnd ; ++uit )
00203         uris.append( KURLDrag::urlToString( *uit ).latin1() );
00204     setUris( uris ); // we give the KDE uris to QUriDrag. TODO: do the opposite in KDE4 and add a m_mostLocalUris member.
00205 
00206     uit = mostLocalUrls.begin();
00207     uEnd = mostLocalUrls.end();
00208     for ( ; uit != uEnd ; ++uit )
00209         m_urls.append( KURLDrag::urlToString( *uit ).latin1() );
00210     // we keep the most-local-uris in m_urls for exporting those as text/plain (for xmms)
00211 }
00212 
00213 const char* KonqDrag::format( int i ) const
00214 {
00215     if ( i == 0 )
00216     return "text/uri-list";
00217     else if ( i == 1 )
00218         return "application/x-kde-cutselection";
00219     else if ( i == 2 )
00220         return "text/plain";
00221     else if ( i == 3 )
00222         return "application/x-kde-urilist";
00223     else return 0;
00224 }
00225 
00226 QByteArray KonqDrag::encodedData( const char* mime ) const
00227 {
00228     QByteArray a;
00229     QCString mimetype( mime );
00230     if ( mimetype == "text/uri-list" )
00231     {
00232         // Code taken from QUriDrag::setUris
00233         int c=0;
00234         for (QStrListIterator it(m_urls); *it; ++it) {
00235              int l = qstrlen(*it);
00236              a.resize(c+l+2);
00237              memcpy(a.data()+c,*it,l);
00238              memcpy(a.data()+c+l,"\r\n",2);
00239              c+=l+2;
00240         }
00241         a.resize(c+1);
00242         a[c] = 0;
00243     }
00244     else if ( mimetype == "application/x-kde-urilist" )
00245     {
00246         return QUriDrag::encodedData( "text/uri-list" );
00247     }
00248     else if ( mimetype == "application/x-kde-cutselection" )
00249     {
00250         QCString s ( m_bCutSelection ? "1" : "0" );
00251     a.resize( s.length() + 1 ); // trailing zero
00252     memcpy( a.data(), s.data(), s.length() + 1 );
00253     }
00254     else if ( mimetype == "text/plain" )
00255     {
00256         QStringList uris;
00257         for (QStrListIterator it(m_urls); *it; ++it)
00258             uris.append(KURLDrag::stringToUrl(*it).prettyURL());
00259         QCString s = uris.join( "\n" ).local8Bit();
00260         if( uris.count() > 1 )
00261             s.append( "\n" );
00262         a.resize( s.length() + 1 ); // trailing zero
00263         memcpy( a.data(), s.data(), s.length() + 1 );
00264     }
00265     return a;
00266 }
00267 
00268 //
00269 
00270 // Used for KonqIconDrag too
00271 
00272 bool KonqDrag::decodeIsCutSelection( const QMimeSource *e )
00273 {
00274   QByteArray a = e->encodedData( "application/x-kde-cutselection" );
00275   if ( a.isEmpty() )
00276     return false;
00277   else
00278   {
00279     kdDebug(1203) << "KonqDrag::decodeIsCutSelection : a=" << QCString(a.data(), a.size() + 1) << endl;
00280     return (a.at(0) == '1'); // true if 1
00281   }
00282 }
00283 
00284 #include "konq_drag.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys