kdecore Library API Documentation

kurldrag.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License.
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    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library 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 "kurldrag.h"
00021 #include <qstrlist.h>
00022 #include <qdragobject.h>
00023 #include <qfont.h>
00024 #include <unistd.h>
00025 
00026 #include <kdeversion.h>
00027 #include <kglobal.h>
00028 #include <klocale.h>
00029 
00030 KURLDrag::KURLDrag( const KURL::List &urls, QWidget* dragSource, const char * name )
00031     : QUriDrag(dragSource, name), m_metaData()
00032 {
00033     init(urls);
00034 }
00035 
00036 KURLDrag::KURLDrag( const KURL::List &urls, const QMap<QString,QString>& metaData,
00037                     QWidget* dragSource, const char * name )
00038     : QUriDrag(dragSource, name), m_metaData(metaData)
00039 {
00040     init(urls);
00041 }
00042 
00043 KURLDrag::~KURLDrag()
00044 {
00045 }
00046 
00047 void KURLDrag::init(const KURL::List &urls)
00048 {
00049     KURL::List::ConstIterator uit = urls.begin();
00050     KURL::List::ConstIterator uEnd = urls.end();
00051     // Get each URL encoded in utf8 - and since we get it in escaped
00052     // form on top of that, .latin1() is fine.
00053     for ( ; uit != uEnd ; ++uit )
00054     {
00055         m_urls.append( urlToString(*uit).latin1() ); 
00056     }
00057     setUris(m_urls);
00058 }
00059 
00060 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, QWidget* dragSource, const char * name )
00061 {
00062     return new KURLDrag( urls, QMap<QString, QString>(), dragSource, name );
00063 }
00064 
00065 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, const QMap<QString, QString>& metaData,
00066                               QWidget* dragSource, const char * name )
00067 {
00068     return new KURLDrag( urls, metaData, dragSource, name );
00069 }
00070 
00071 bool KURLDrag::decode( const QMimeSource *e, KURL::List &uris )
00072 {
00073     QStrList lst;
00074     QUriDrag::decode( e, lst );
00075     for (QStrListIterator it(lst); *it; ++it)
00076     {
00077       KURL url = stringToUrl( *it );
00078       if ( !url.isValid() )
00079       {
00080         uris.clear();
00081         break;
00082       }
00083       uris.append( url );
00084     }
00085     return !uris.isEmpty();
00086 }
00087 
00088 bool KURLDrag::decode( const QMimeSource *e, KURL::List &uris, QMap<QString,QString>& metaData )
00089 {
00090     if ( decode( e, uris ) ) // first decode the URLs (see above)
00091     {
00092         QByteArray ba = e->encodedData( "application/x-kio-metadata" );
00093         if ( ba.size() )
00094         {
00095             QString s = ba.data();
00096             QStringList l = QStringList::split( "$@@$", s );
00097             QStringList::ConstIterator it = l.begin();
00098             bool readingKey = true; // true, then false, then true, etc.
00099             QString key;
00100             for ( ; it != l.end(); ++it ) {
00101                 if ( readingKey )
00102                     key = *it;
00103                 else
00104                     metaData.replace( key, *it );
00105                 readingKey = !readingKey;
00106             }
00107             Q_ASSERT( readingKey ); // an odd number of items would be, well, odd ;-)
00108         }
00109         return true; // Success, even if no metadata was found
00110     }
00111     return false; // Couldn't decode the URLs
00112 }
00113 
00114 #ifdef Q_WS_QWS
00115 bool KURLDrag::decode( QStringList const &e, KURL::List &uris )
00116 {
00117     for(QStringList::ConstIterator it=e.begin(); it!=e.end(); it++)
00118     {
00119       KURL url = KURL( *it, 106 ); // 106 is mib enum for utf8 codec
00120       if ( !url.isValid() )
00121       {
00122         uris.clear();
00123         break;
00124       }
00125       uris.append( url );
00126     }
00127     return !uris.isEmpty();
00128 }
00129 #endif
00130 
00132 
00133 const char * KURLDrag::format( int i ) const
00134 {
00135     if ( i == 0 )
00136         return "text/uri-list";
00137     else if ( i == 1 )
00138         return "text/plain";
00139     else if ( i == 2 )
00140         return "application/x-kio-metadata";
00141     else if ( i == 3 ) //Support this for apps that use plain XA_STRING clipboard
00142         return "text/plain;charset=ISO-8859-1";
00143     else if ( i == 4 ) //Support this for apps that use the UTF_STRING clipboard
00144         return "text/plain;charset=UTF-8"; 
00145     else return 0;
00146 }
00147 
00148 QByteArray KURLDrag::encodedData( const char* mime ) const
00149 {
00150     QByteArray a;
00151     QCString mimetype( mime );
00152     if ( mimetype == "text/uri-list" )
00153         return QUriDrag::encodedData( mime );
00154     else if ( mimetype == "text/plain" )
00155     {
00156     QStringList uris;
00157         for (QStrListIterator it(m_urls); *it; ++it)
00158            uris.append(stringToUrl(*it).prettyURL());
00159 
00160         QCString s = uris.join( "\n" ).local8Bit();
00161         if( uris.count() > 1 ) // terminate last line, unless it's the only line
00162             s.append( "\n" );
00163         a.resize( s.length());
00164         memcpy( a.data(), s.data(), s.length()); // no trailing zero in clipboard text
00165     }
00166     else if ( mimetype.lower() == "text/plain;charset=iso-8859-1")
00167     {
00168         QStringList uris;
00169         for (QStrListIterator it(m_urls); *it; ++it)
00170         for (QStrListIterator it(m_urls); *it; ++it)
00171            uris.append(stringToUrl(*it).url(0, 4)); // 4 is mib for latin1
00172 
00173         QCString s = uris.join( "\n" ).latin1();
00174         if( uris.count() > 1 )
00175             s.append( "\n" );
00176         a.resize( s.length());
00177         memcpy( a.data(), s.data(), s.length());    
00178     }
00179     else if ( mimetype.lower() == "text/plain;charset=utf-8")
00180     {
00181         QStringList uris;
00182         for (QStrListIterator it(m_urls); *it; ++it)
00183            uris.append(stringToUrl(*it).prettyURL());
00184 
00185         QCString s = uris.join( "\n" ).utf8();
00186         if( uris.count() > 1 )
00187             s.append( "\n" );
00188         a.resize( s.length());
00189         memcpy( a.data(), s.data(), s.length());    
00190     }    
00191     else if ( mimetype == "application/x-kio-metadata" )
00192     {
00193         if ( !m_metaData.isEmpty() )
00194         {
00195             QString s;
00196             QMap<QString,QString>::ConstIterator it;
00197             for( it = m_metaData.begin(); it != m_metaData.end(); ++it )
00198             {
00199                 s += it.key();
00200                 s += "$@@$";
00201                 s += it.data();
00202                 s += "$@@$";
00203             }
00204         a.resize( s.length() + 1 );
00205         memcpy( a.data(), s.latin1(), a.size() );
00206         }
00207     }
00208     return a;
00209 }
00210 
00211 KURL KURLDrag::stringToUrl(const QCString &s)
00212 {
00213     if (strncmp(s.data(), "file:", 5) == 0)
00214        return KURL(s, KGlobal::locale()->fileEncodingMib());
00215     
00216     return KURL(s, 106); // 106 is mib enum for utf8 codec;
00217 }
00218 
00219 QString KURLDrag::urlToString(const KURL &url)
00220 {
00221     if (url.isLocalFile())
00222     {
00223 #if 1
00224         return url.url(0, KGlobal::locale()->fileEncodingMib());
00225 #else
00226         // According to the XDND spec, file:/ URLs for DND must have
00227         // the hostname part. But in really it just breaks many apps,
00228         // so it's disabled for now.
00229         QString s = url.url(0, KGlobal::locale()->fileEncodingMib());
00230         if( !s.startsWith( "file://" ))
00231         {
00232             char hostname[257];
00233             if ( gethostname( hostname, 255 ) == 0 )
00234             {
00235             hostname[256] = '\0';
00236                 return QString( "file://" ) + hostname + s.mid( 5 );
00237             }
00238         }
00239 #endif
00240     }
00241 
00242     return url.url(0, 106); // 106 is mib enum for utf8 codec
00243 }
00244 
00245 // deprecated ctor
00246 KURLDrag::KURLDrag( const QStrList & urls, const QMap<QString,QString>& metaData,
00247                     QWidget * dragSource, const char* name ) :
00248 QUriDrag( urls, dragSource, name ), m_urls( urls ), m_metaData( metaData ) {}
00249 
KDE Logo
This file is part of the documentation for kdecore Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Aug 4 05:23:04 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003