• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDE3Support

  • sources
  • kde-4.14
  • kdelibs
  • kde3support
  • kdecore
k3urldrag.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE project
2  Copyright (C) 2000 David Faure <faure@kde.org>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this program; see the file COPYING. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "k3urldrag.h"
21 #include <Qt3Support/Q3CString>
22 #include <Qt3Support/Q3StrIList>
23 #include <Qt3Support/Q3ColorDrag>
24 #include <QtGui/QFont>
25 #include <unistd.h>
26 
27 #include <kglobal.h>
28 #include <klocale.h>
29 #include <kdebug.h>
30 
31 class K3URLDragPrivate
32 {
33 public:
34  bool m_exportAsText;
35 };
36 
37 K3URLDrag::K3URLDrag( const KUrl::List &urls, QWidget* dragSource )
38  : Q3UriDrag(dragSource), m_metaData(), d( 0 )
39 {
40  init(urls);
41 }
42 
43 K3URLDrag::K3URLDrag( const KUrl::List &urls,
44  const QMap<QString,QString>& metaData,
45  QWidget* dragSource )
46  : Q3UriDrag(dragSource), m_metaData(metaData), d( 0 )
47 {
48  init(urls);
49 }
50 
51 K3URLDrag::~K3URLDrag()
52 {
53  delete d;
54 }
55 
56 void K3URLDrag::init(const KUrl::List &urls)
57 {
58  KUrl::List::ConstIterator uit = urls.begin();
59  KUrl::List::ConstIterator uEnd = urls.end();
60  // Get each URL encoded in utf8 - and since we get it in escaped
61  // form on top of that, .toLatin1().constData() is fine.
62  for ( ; uit != uEnd ; ++uit )
63  {
64  m_urls.append( urlToString(*uit).toLatin1().constData() );
65  }
66  setUris(m_urls);
67 }
68 
69 void K3URLDrag::setExportAsText( bool exp )
70 {
71  // For now d is only used here, so create it on demand
72  if ( !d )
73  d = new K3URLDragPrivate;
74  d->m_exportAsText = exp;
75 }
76 
77 K3URLDrag * K3URLDrag::newDrag( const KUrl::List &urls, QWidget* dragSource )
78 {
79  return new K3URLDrag( urls, QMap<QString, QString>(), dragSource );
80 }
81 
82 K3URLDrag * K3URLDrag::newDrag( const KUrl::List &urls, const QMap<QString, QString>& metaData,
83  QWidget* dragSource )
84 {
85  return new K3URLDrag( urls, metaData, dragSource );
86 }
87 
88 QMap<QString, QString> &K3URLDrag::metaData()
89 {
90  return m_metaData;
91 }
92 
93 bool K3URLDrag::decode( const QMimeSource *e, KUrl::List &uris )
94 {
95  // x-kde4-urilist is the same format as text/uri-list, but contains
96  // KDE-aware urls, like media:/ and system:/, whereas text/uri-list is resolved to
97  // local files.
98  if ( e->provides( "application/x-kde4-urilist" ) ) {
99  QByteArray payload = e->encodedData( "application/x-kde4-urilist" );
100  if ( payload.size() ) {
101  int c=0;
102  const char* d = payload.data();
103  while (c < payload.size() && d[c]) {
104  int f = c;
105  // Find line end
106  while (c < payload.size() && d[c] && d[c]!='\r'
107  && d[c] != '\n')
108  c++;
109  Q3CString s(d+f,c-f+1);
110  if ( s[0] != '#' ) // non-comment?
111  uris.append(stringToUrl(s));
112  // Skip junk
113  while (c < payload.size() && d[c] &&
114  (d[c]=='\n' || d[c]=='\r'))
115  c++;
116  }
117  return !uris.isEmpty();
118  }
119  }
120 
121  Q3StrList lst;
122  Q3UriDrag::decode( e, lst );
123  for (Q3StrListIterator it(lst); *it; ++it)
124  {
125  KUrl url = stringToUrl( *it );
126  if ( !url.isValid() )
127  {
128  uris.clear();
129  break;
130  }
131  uris.append( url );
132  }
133  return !uris.isEmpty();
134 }
135 
136 bool K3URLDrag::decode( const QMimeSource *e, KUrl::List &uris, QMap<QString,QString>& metaData )
137 {
138  if ( decode( e, uris ) ) // first decode the URLs (see above)
139  {
140  QByteArray ba = e->encodedData( "application/x-kio-metadata" );
141  if ( ba.size() )
142  {
143  QString s = ba.data();
144  const QStringList l = s.split( "$@@$", QString::SkipEmptyParts );
145  QStringList::ConstIterator it = l.begin();
146  bool readingKey = true; // true, then false, then true, etc.
147  QString key;
148  for ( ; it != l.end(); ++it ) {
149  if ( readingKey )
150  key = *it;
151  else
152  metaData.replace( key, *it );
153  readingKey = !readingKey;
154  }
155  Q_ASSERT( readingKey ); // an odd number of items would be, well, odd ;-)
156  }
157  return true; // Success, even if no metadata was found
158  }
159  return false; // Couldn't decode the URLs
160 }
161 
163 
164 const char * K3URLDrag::format( int i ) const
165 {
166  if ( i == 0 )
167  return "text/uri-list";
168  else if ( i == 1 )
169  return "application/x-kio-metadata";
170  if ( d && d->m_exportAsText == false )
171  return 0;
172  if ( i == 2 )
173  return "text/plain";
174  else if ( i == 3 ) //Support this for apps that use plain XA_STRING clipboard
175  return "text/plain;charset=ISO-8859-1";
176  else if ( i == 4 ) //Support this for apps that use the UTF_STRING clipboard
177  return "text/plain;charset=UTF-8";
178  else return 0;
179 }
180 
181 QByteArray K3URLDrag::encodedData( const char* mime ) const
182 {
183  QByteArray a;
184  QByteArray mimetype( mime );
185  if ( mimetype == "text/uri-list" )
186  return Q3UriDrag::encodedData( mime );
187  else if ( mimetype == "text/plain" )
188  {
189  QStringList uris;
190  for (Q3StrListIterator it(m_urls); *it; ++it)
191  uris.append(stringToUrl(*it).prettyUrl());
192 
193  QByteArray s = uris.join( "\n" ).toLocal8Bit();
194  if( uris.count() > 1 ) // terminate last line, unless it's the only line
195  s.append( "\n" );
196  a.resize( s.length());
197  memcpy( a.data(), s.data(), s.length()); // no trailing zero in clipboard text
198  }
199  else if ( mimetype.toLower() == "text/plain;charset=iso-8859-1")
200  {
201  QStringList uris;
202  for (Q3StrListIterator it(m_urls); *it; ++it)
203  uris.append(stringToUrl(*it).url()); // was using ",4" - the mib for latin1
204 
205  QByteArray s = uris.join( "\n" ).toLatin1();
206  if( uris.count() > 1 )
207  s.append( "\n" );
208  a.resize( s.length());
209  memcpy( a.data(), s.data(), s.length());
210  }
211  else if ( mimetype.toLower() == "text/plain;charset=utf-8")
212  {
213  QStringList uris;
214  for (Q3StrListIterator it(m_urls); *it; ++it)
215  uris.append(stringToUrl(*it).prettyUrl());
216 
217  QByteArray s = uris.join( "\n" ).toUtf8();
218  if( uris.count() > 1 )
219  s.append( "\n" );
220  a.resize( s.length());
221  memcpy( a.data(), s.data(), s.length());
222  }
223  else if ( mimetype == "application/x-kio-metadata" )
224  {
225  if ( !m_metaData.isEmpty() )
226  {
227  QString s;
228  QMap<QString,QString>::ConstIterator it;
229  for( it = m_metaData.begin(); it != m_metaData.end(); ++it )
230  {
231  s += it.key();
232  s += "$@@$";
233  s += it.data();
234  s += "$@@$";
235  }
236  a.resize( s.length() + 1 );
237  memcpy( a.data(), s.toLatin1().constData(), a.size() );
238  }
239  }
240  return a;
241 }
242 
243 KUrl K3URLDrag::stringToUrl(const QByteArray &s)
244 {
245  if (strncmp(s.data(), "file:", 5) == 0)
246  return KUrl(s /*, KGlobal::locale()->fileEncodingMib()*/);
247 
248  return KUrl(s /*, 106*/); // 106 is mib enum for utf8 codec;
249 }
250 
251 QString K3URLDrag::urlToString(const KUrl &url)
252 {
253  if (url.isLocalFile())
254  {
255 #if 1
256  return url.url(/*0 , KGlobal::locale()->fileEncodingMib()*/);
257 #else
258  // According to the XDND spec, file:/ URLs for DND must have
259  // the hostname part. But in really it just breaks many apps,
260  // so it's disabled for now.
261  QString s = url.url(0, KGlobal::locale()->fileEncodingMib());
262  if( !s.startsWith( "file://" ))
263  {
264  char hostname[257];
265  if ( gethostname( hostname, 255 ) == 0 )
266  {
267  hostname[256] = '\0';
268  return QString( "file://" ) + hostname + s.mid( 5 );
269  }
270  }
271 #endif
272  }
273 
274  if ( url.protocol() == "mailto" ) {
275  return url.path();
276  }
277 
278  return url.url(/*0 , 106*/); // 106 is mib enum for utf8 codec
279 }
280 
281 // deprecated ctor
282 K3URLDrag::K3URLDrag( const Q3StrList & urls, const QMap<QString,QString>& metaData,
283  QWidget * dragSource ) :
284 Q3UriDrag( urls, dragSource ), m_urls( urls ), m_metaData( metaData ), d( 0 ) {}
QList::clear
void clear()
QWidget
QMimeSource::encodedData
virtual QByteArray encodedData(const char *format) const =0
kdebug.h
K3URLDrag::urlToString
static QString urlToString(const KUrl &url)
Converts a URL to a string representation suitable for dragging.
Definition: k3urldrag.cpp:251
K3URLDrag::K3URLDrag
K3URLDrag(const KUrl::List &urls, QWidget *dragSource=0)
Constructs an object to drag the list of URLs in urls.
Definition: k3urldrag.cpp:37
QByteArray::toLower
QByteArray toLower() const
QByteArray
k3urldrag.h
Q3StoredDrag::encodedData
virtual QByteArray encodedData(const char *format) const
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
mimetype
MimetypeJob * mimetype(const KUrl &url, JobFlags flags=DefaultFlags)
QMap< QString, QString >
Q3UriDrag::setUris
virtual void setUris(const QList< QByteArray > &list)
Q3UriDrag
QStringList::join
QString join(const QString &separator) const
QByteArray::length
int length() const
klocale.h
K3URLDrag::decode
static bool decode(const QMimeSource *e, KUrl::List &urls)
Convenience method that decodes the contents of e into a list of KUrls.
Definition: k3urldrag.cpp:93
KUrl
K3URLDrag::stringToUrl
static KUrl stringToUrl(const QByteArray &s)
Converts a string used for dragging to a URL.
Definition: k3urldrag.cpp:243
Q3PtrList::append
void append(const type *item)
QByteArray::resize
void resize(int size)
K3URLDrag::setExportAsText
void setExportAsText(bool exp)
By default, K3URLDrag also exports the URLs as plain text, for e.g.
Definition: k3urldrag.cpp:69
kglobal.h
K3URLDrag
This class is to be used instead of Q3UriDrag when using KUrl.
Definition: k3urldrag.h:46
QList::count
int count(const T &value) const
QList::append
void append(const T &value)
KUrl::protocol
QString protocol() const
QList::isEmpty
bool isEmpty() const
QByteArray::constData
const char * constData() const
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
K3URLDrag::newDrag
static K3URLDrag * newDrag(const KUrl::List &urls, QWidget *dragSource=0)
Definition: k3urldrag.cpp:77
K3URLDrag::encodedData
virtual QByteArray encodedData(const char *mime) const
Definition: k3urldrag.cpp:181
QString
QMap::end
iterator end()
KUrl::path
QString path(AdjustPathOption trailing=LeaveTrailingSlash) const
QMap::replace
iterator replace(const Key &key, const T &value)
QMap::begin
iterator begin()
QStringList
K3URLDrag::metaData
QMap< QString, QString > & metaData()
Meta-data to associate with those URLs.
Definition: k3urldrag.cpp:88
QByteArray::append
QByteArray & append(char ch)
QList::end
iterator end()
QString::toLocal8Bit
QByteArray toLocal8Bit() const
Q3StrListIterator
Q3StrList
K3URLDrag::format
virtual const char * format(int i) const
Definition: k3urldrag.cpp:164
KGlobal::locale
KLocale * locale()
Q3CString
QMap::key
const Key key(const T &value) const
K3URLDrag::~K3URLDrag
virtual ~K3URLDrag()
Definition: k3urldrag.cpp:51
KUrl::List
Q3UriDrag::decode
bool decode(const QMimeSource *source, Q3StrList &list)
QUrl::isValid
bool isValid() const
QString::toLatin1
QByteArray toLatin1() const
QString::mid
QString mid(int position, int n) const
QList< KUrl >::ConstIterator
typedef ConstIterator
QString::length
int length() const
QByteArray::data
char * data()
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
QMap::isEmpty
bool isEmpty() const
KUrl::isLocalFile
bool isLocalFile() const
QByteArray::size
int size() const
QMimeSource::provides
virtual bool provides(const char *mimeType) const
QList::begin
iterator begin()
KUrl::prettyUrl
QString prettyUrl(AdjustPathOption trailing=LeaveTrailingSlash) const
QMimeSource
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:26:48 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDE3Support

Skip menu "KDE3Support"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal