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 
00025 KURLDrag::KURLDrag( const KURL::List &urls, QWidget* dragSource, const char * name )
00026     : QUriDrag(dragSource, name), m_metaData()
00027 {
00028     init(urls);
00029 }
00030 
00031 KURLDrag::KURLDrag( const KURL::List &urls, const QMap<QString,QString>& metaData,
00032                     QWidget* dragSource, const char * name )
00033     : QUriDrag(dragSource, name), m_metaData(metaData)
00034 {
00035     init(urls);
00036 }
00037 
00038 KURLDrag::~KURLDrag()
00039 {
00040 }
00041 
00042 void KURLDrag::init(const KURL::List &urls)
00043 {
00044     KURL::List::ConstIterator uit = urls.begin();
00045     KURL::List::ConstIterator uEnd = urls.end();
00046     // Get each URL encoded in utf8 - and since we get it in escaped
00047     // form on top of that, .latin1() is fine.
00048     for ( ; uit != uEnd ; ++uit )
00049         m_urls.append( (*uit).url(0, 106).latin1() ); // 106 is mib enum for utf8 codec
00050     setUris(m_urls);
00051 }
00052 
00053 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, QWidget* dragSource, const char * name )
00054 {
00055     return new KURLDrag( urls, QMap<QString, QString>(), dragSource, name );
00056 }
00057 
00058 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, const QMap<QString, QString>& metaData,
00059                               QWidget* dragSource, const char * name )
00060 {
00061     return new KURLDrag( urls, metaData, dragSource, name );
00062 }
00063 
00064 bool KURLDrag::decode( const QMimeSource *e, KURL::List &uris )
00065 {
00066     QStrList lst;
00067     bool ret = QUriDrag::decode( e, lst );
00068     for (QStrListIterator it(lst); *it; ++it)
00069       uris.append(KURL(*it, 106)); // 106 is mib enum for utf8 codec
00070     return ret;
00071 }
00072 
00073 bool KURLDrag::decode( const QMimeSource *e, KURL::List &uris, QMap<QString,QString>& metaData )
00074 {
00075     if ( decode( e, uris ) ) // first decode the URLs (see above)
00076     {
00077         QByteArray ba = e->encodedData( "application/x-kio-metadata" );
00078         if ( ba.size() )
00079         {
00080             QString s = ba.data();
00081             QStringList l = QStringList::split( "$@@$", s );
00082             QStringList::ConstIterator it = l.begin();
00083             bool readingKey = true; // true, then false, then true, etc.
00084             QString key;
00085             for ( ; it != l.end(); ++it ) {
00086                 if ( readingKey )
00087                     key = *it;
00088                 else
00089                     metaData.replace( key, *it );
00090                 readingKey = !readingKey;
00091             }
00092             Q_ASSERT( readingKey ); // an odd number of items would be, well, odd ;-)
00093         }
00094         return true; // Success, even if no metadata was found
00095     }
00096     return false; // Couldn't decode the URLs
00097 }
00098 
00099 #ifdef Q_WS_QWS
00100 bool KURLDrag::decode( QStringList const &e, KURL::List &uris )
00101 {
00102     for(QStringList::ConstIterator it=e.begin(); it!=e.end(); it++)
00103       uris.append(KURL(*it, 106)); // 106 is mib enum for utf8 codec
00104     return true;
00105 }
00106 #endif
00107 
00109 
00110 const char * KURLDrag::format( int i ) const
00111 {
00112     if ( i == 0 )
00113         return "text/uri-list";
00114     else if ( i == 1 )
00115         return "text/plain";
00116     else if ( i == 2 )
00117         return "application/x-kio-metadata";
00118     else if ( i == 3 ) //Support this for apps that use plain XA_STRING clipboard
00119         return "text/plain;charset=ISO-8859-1";
00120     else if ( i == 4 ) //Support this for apps that use the UTF_STRING clipboard
00121         return "text/plain;charset=UTF-8"; 
00122 
00123     else return 0;
00124 }
00125 
00126 QByteArray KURLDrag::encodedData( const char* mime ) const
00127 {
00128     QByteArray a;
00129     QCString mimetype( mime );
00130     if ( mimetype == "text/uri-list" )
00131         return QUriDrag::encodedData( mime );
00132     else if ( mimetype == "text/plain" )
00133     {
00134     QStringList uris;
00135         for (QStrListIterator it(m_urls); *it; ++it)
00136          uris.append(KURL(*it, 106).prettyURL()); // 106 is mib enum for utf8 codec
00137         QCString s = uris.join( "\n" ).local8Bit();
00138         a.resize( s.length());
00139         memcpy( a.data(), s.data(), s.length()); // no trailing zero in clipboard text
00140     }
00141     else if ( mimetype.lower() == "text/plain;charset=iso-8859-1")
00142     {
00143         QStringList uris;
00144         for (QStrListIterator it(m_urls); *it; ++it)
00145          uris.append(KURL(*it, 106).url(0, 4)); // 106 is mib enum for utf8 codec; 4 for latin1
00146 
00147         QCString s = uris.join( "\n" ).latin1();
00148         a.resize( s.length());
00149         memcpy( a.data(), s.data(), s.length());    
00150     }
00151     else if ( mimetype.lower() == "text/plain;charset=utf-8")
00152     {
00153         QStringList uris;
00154         for (QStrListIterator it(m_urls); *it; ++it)
00155          uris.append(KURL(*it, 106).prettyURL()); // 106 is mib enum for utf8 codec
00156         QCString s = uris.join( "\n" ).utf8();
00157         a.resize( s.length());
00158         memcpy( a.data(), s.data(), s.length());    
00159     }    
00160     else if ( mimetype == "application/x-kio-metadata" )
00161     {
00162         if ( !m_metaData.isEmpty() )
00163         {
00164             QString s;
00165             QMap<QString,QString>::ConstIterator it;
00166             for( it = m_metaData.begin(); it != m_metaData.end(); ++it )
00167             {
00168                 s += it.key();
00169                 s += "$@@$";
00170                 s += it.data();
00171                 s += "$@@$";
00172             }
00173         a.resize( s.length() + 1 );
00174         memcpy( a.data(), s.latin1(), a.size() );
00175         }
00176     }
00177     return a;
00178 }
00179 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.4.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 27 22:14:48 2005 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001