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 #include <kdebug.h> 00030 00031 class KURLDragPrivate 00032 { 00033 public: 00034 bool m_exportAsText; 00035 }; 00036 00037 KURLDrag::KURLDrag( const KURL::List &urls, QWidget* dragSource, const char * name ) 00038 : QUriDrag(dragSource, name), m_metaData(), d( 0 ) 00039 { 00040 init(urls); 00041 } 00042 00043 KURLDrag::KURLDrag( const KURL::List &urls, const QMap<QString,QString>& metaData, 00044 QWidget* dragSource, const char * name ) 00045 : QUriDrag(dragSource, name), m_metaData(metaData), d( 0 ) 00046 { 00047 init(urls); 00048 } 00049 00050 KURLDrag::~KURLDrag() 00051 { 00052 delete d; 00053 } 00054 00055 void KURLDrag::init(const KURL::List &urls) 00056 { 00057 KURL::List::ConstIterator uit = urls.begin(); 00058 KURL::List::ConstIterator uEnd = urls.end(); 00059 // Get each URL encoded in utf8 - and since we get it in escaped 00060 // form on top of that, .latin1() is fine. 00061 for ( ; uit != uEnd ; ++uit ) 00062 { 00063 m_urls.append( urlToString(*uit).latin1() ); 00064 } 00065 setUris(m_urls); 00066 } 00067 00068 void KURLDrag::setExportAsText( bool exp ) 00069 { 00070 // For now d is only used here, so create it on demand 00071 if ( !d ) 00072 d = new KURLDragPrivate; 00073 d->m_exportAsText = exp; 00074 } 00075 00076 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, QWidget* dragSource, const char * name ) 00077 { 00078 return new KURLDrag( urls, QMap<QString, QString>(), dragSource, name ); 00079 } 00080 00081 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, const QMap<QString, QString>& metaData, 00082 QWidget* dragSource, const char * name ) 00083 { 00084 return new KURLDrag( urls, metaData, dragSource, name ); 00085 } 00086 00087 bool KURLDrag::decode( const QMimeSource *e, KURL::List &uris ) 00088 { 00089 QStrList lst; 00090 QUriDrag::decode( e, lst ); 00091 for (QStrListIterator it(lst); *it; ++it) 00092 { 00093 KURL url = stringToUrl( *it ); 00094 if ( !url.isValid() ) 00095 { 00096 uris.clear(); 00097 break; 00098 } 00099 uris.append( url ); 00100 } 00101 return !uris.isEmpty(); 00102 } 00103 00104 bool KURLDrag::decode( const QMimeSource *e, KURL::List &uris, QMap<QString,QString>& metaData ) 00105 { 00106 if ( decode( e, uris ) ) // first decode the URLs (see above) 00107 { 00108 QByteArray ba = e->encodedData( "application/x-kio-metadata" ); 00109 if ( ba.size() ) 00110 { 00111 QString s = ba.data(); 00112 QStringList l = QStringList::split( "$@@$", s ); 00113 QStringList::ConstIterator it = l.begin(); 00114 bool readingKey = true; // true, then false, then true, etc. 00115 QString key; 00116 for ( ; it != l.end(); ++it ) { 00117 if ( readingKey ) 00118 key = *it; 00119 else 00120 metaData.replace( key, *it ); 00121 readingKey = !readingKey; 00122 } 00123 Q_ASSERT( readingKey ); // an odd number of items would be, well, odd ;-) 00124 } 00125 return true; // Success, even if no metadata was found 00126 } 00127 return false; // Couldn't decode the URLs 00128 } 00129 00130 #ifdef Q_WS_QWS 00131 bool KURLDrag::decode( QStringList const &e, KURL::List &uris ) 00132 { 00133 for(QStringList::ConstIterator it=e.begin(); it!=e.end(); it++) 00134 { 00135 KURL url = KURL( *it, 106 ); // 106 is mib enum for utf8 codec 00136 if ( !url.isValid() ) 00137 { 00138 uris.clear(); 00139 break; 00140 } 00141 uris.append( url ); 00142 } 00143 return !uris.isEmpty(); 00144 } 00145 #endif 00146 00148 00149 const char * KURLDrag::format( int i ) const 00150 { 00151 if ( i == 0 ) 00152 return "text/uri-list"; 00153 else if ( i == 1 ) 00154 return "application/x-kio-metadata"; 00155 if ( d && d->m_exportAsText == false ) 00156 return 0; 00157 if ( i == 2 ) 00158 return "text/plain"; 00159 else if ( i == 3 ) //Support this for apps that use plain XA_STRING clipboard 00160 return "text/plain;charset=ISO-8859-1"; 00161 else if ( i == 4 ) //Support this for apps that use the UTF_STRING clipboard 00162 return "text/plain;charset=UTF-8"; 00163 else return 0; 00164 } 00165 00166 QByteArray KURLDrag::encodedData( const char* mime ) const 00167 { 00168 QByteArray a; 00169 QCString mimetype( mime ); 00170 if ( mimetype == "text/uri-list" ) 00171 return QUriDrag::encodedData( mime ); 00172 else if ( mimetype == "text/plain" ) 00173 { 00174 QStringList uris; 00175 for (QStrListIterator it(m_urls); *it; ++it) 00176 uris.append(stringToUrl(*it).prettyURL()); 00177 00178 QCString s = uris.join( "\n" ).local8Bit(); 00179 if( uris.count() > 1 ) // terminate last line, unless it's the only line 00180 s.append( "\n" ); 00181 a.resize( s.length()); 00182 memcpy( a.data(), s.data(), s.length()); // no trailing zero in clipboard text 00183 } 00184 else if ( mimetype.lower() == "text/plain;charset=iso-8859-1") 00185 { 00186 QStringList uris; 00187 for (QStrListIterator it(m_urls); *it; ++it) 00188 for (QStrListIterator it(m_urls); *it; ++it) 00189 uris.append(stringToUrl(*it).url(0, 4)); // 4 is mib for latin1 00190 00191 QCString s = uris.join( "\n" ).latin1(); 00192 if( uris.count() > 1 ) 00193 s.append( "\n" ); 00194 a.resize( s.length()); 00195 memcpy( a.data(), s.data(), s.length()); 00196 } 00197 else if ( mimetype.lower() == "text/plain;charset=utf-8") 00198 { 00199 QStringList uris; 00200 for (QStrListIterator it(m_urls); *it; ++it) 00201 uris.append(stringToUrl(*it).prettyURL()); 00202 00203 QCString s = uris.join( "\n" ).utf8(); 00204 if( uris.count() > 1 ) 00205 s.append( "\n" ); 00206 a.resize( s.length()); 00207 memcpy( a.data(), s.data(), s.length()); 00208 } 00209 else if ( mimetype == "application/x-kio-metadata" ) 00210 { 00211 if ( !m_metaData.isEmpty() ) 00212 { 00213 QString s; 00214 QMap<QString,QString>::ConstIterator it; 00215 for( it = m_metaData.begin(); it != m_metaData.end(); ++it ) 00216 { 00217 s += it.key(); 00218 s += "$@@$"; 00219 s += it.data(); 00220 s += "$@@$"; 00221 } 00222 a.resize( s.length() + 1 ); 00223 memcpy( a.data(), s.latin1(), a.size() ); 00224 } 00225 } 00226 return a; 00227 } 00228 00229 KURL KURLDrag::stringToUrl(const QCString &s) 00230 { 00231 if (strncmp(s.data(), "file:", 5) == 0) 00232 return KURL(s, KGlobal::locale()->fileEncodingMib()); 00233 00234 return KURL(s, 106); // 106 is mib enum for utf8 codec; 00235 } 00236 00237 QString KURLDrag::urlToString(const KURL &url) 00238 { 00239 if (url.isLocalFile()) 00240 { 00241 #if 1 00242 return url.url(0, KGlobal::locale()->fileEncodingMib()); 00243 #else 00244 // According to the XDND spec, file:/ URLs for DND must have 00245 // the hostname part. But in really it just breaks many apps, 00246 // so it's disabled for now. 00247 QString s = url.url(0, KGlobal::locale()->fileEncodingMib()); 00248 if( !s.startsWith( "file://" )) 00249 { 00250 char hostname[257]; 00251 if ( gethostname( hostname, 255 ) == 0 ) 00252 { 00253 hostname[256] = '\0'; 00254 return QString( "file://" ) + hostname + s.mid( 5 ); 00255 } 00256 } 00257 #endif 00258 } 00259 00260 if ( url.protocol() == "mailto" ) { 00261 return url.path(); 00262 } 00263 00264 return url.url(0, 106); // 106 is mib enum for utf8 codec 00265 } 00266 00267 // deprecated ctor 00268 KURLDrag::KURLDrag( const QStrList & urls, const QMap<QString,QString>& metaData, 00269 QWidget * dragSource, const char* name ) : 00270 QUriDrag( urls, dragSource, name ), m_urls( urls ), m_metaData( metaData ), d( 0 ) {}
KDE Logo
This file is part of the documentation for kdecore Library Version 3.4.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 14 00:03:34 2005 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003