• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdecore
 

tdecore

  • tdecore
kurldrag.cpp
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 "kurldrag.h"
21 #include <tqstrlist.h>
22 #include <tqdragobject.h>
23 #include <tqfont.h>
24 #include <unistd.h>
25 
26 #include <tdeglobal.h>
27 #include <tdelocale.h>
28 #include <kdebug.h>
29 
30 class KURLDragPrivate
31 {
32 public:
33  bool m_exportAsText;
34 };
35 
36 KURLDrag::KURLDrag( const KURL::List &urls, TQWidget* dragSource, const char * name )
37  : TQUriDrag(dragSource, name), m_metaData(), d( 0 )
38 {
39  init(urls);
40 }
41 
42 KURLDrag::KURLDrag( const KURL::List &urls, const TQMap<TQString,TQString>& metaData,
43  TQWidget* dragSource, const char * name )
44  : TQUriDrag(dragSource, name), m_metaData(metaData), d( 0 )
45 {
46  init(urls);
47 }
48 
49 KURLDrag::~KURLDrag()
50 {
51  delete d;
52 }
53 
54 void KURLDrag::init(const KURL::List &urls)
55 {
56  KURL::List::ConstIterator uit = urls.begin();
57  KURL::List::ConstIterator uEnd = urls.end();
58  // Get each URL encoded in utf8 - and since we get it in escaped
59  // form on top of that, .latin1() is fine.
60  for ( ; uit != uEnd ; ++uit )
61  {
62  m_urls.append( urlToString(*uit).latin1() );
63  }
64  setUris(m_urls);
65 }
66 
67 void KURLDrag::setExportAsText( bool exp )
68 {
69  // For now d is only used here, so create it on demand
70  if ( !d )
71  d = new KURLDragPrivate;
72  d->m_exportAsText = exp;
73 }
74 
75 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, TQWidget* dragSource, const char * name )
76 {
77  return new KURLDrag( urls, TQMap<TQString, TQString>(), dragSource, name );
78 }
79 
80 KURLDrag * KURLDrag::newDrag( const KURL::List &urls, const TQMap<TQString, TQString>& metaData,
81  TQWidget* dragSource, const char * name )
82 {
83  return new KURLDrag( urls, metaData, dragSource, name );
84 }
85 
86 bool KURLDrag::decode( const TQMimeSource *e, KURL::List &uris )
87 {
88  if ( e->provides( "application/x-tde-urilist" ) ) {
89  TQByteArray payload = e->encodedData( "application/x-tde-urilist" );
90  if ( payload.size() ) {
91  uint c=0;
92  const char* d = payload.data();
93  while (c < payload.size() && d[c]) {
94  uint f = c;
95  // Find line end
96  while (c < payload.size() && d[c] && d[c]!='\r'
97  && d[c] != '\n')
98  c++;
99  TQCString s(d+f,c-f+1);
100  if ( s[0] != '#' ) // non-comment?
101  uris.append(stringToUrl(s));
102  // Skip junk
103  while (c < payload.size() && d[c] &&
104  (d[c]=='\n' || d[c]=='\r'))
105  c++;
106  }
107  return !uris.isEmpty();
108  }
109  }
110 
111  TQStrList lst;
112  TQUriDrag::decode( e, lst );
113  for (TQStrListIterator it(lst); *it; ++it)
114  {
115  KURL url = stringToUrl( *it );
116  if ( !url.isValid() )
117  {
118  uris.clear();
119  break;
120  }
121  uris.append( url );
122  }
123  return !uris.isEmpty();
124 }
125 
126 bool KURLDrag::decode( const TQMimeSource *e, KURL::List &uris, TQMap<TQString,TQString>& metaData )
127 {
128  if ( decode( e, uris ) ) // first decode the URLs (see above)
129  {
130  TQByteArray ba = e->encodedData( "application/x-tdeio-metadata" );
131  if ( ba.size() )
132  {
133  TQString s = ba.data();
134  TQStringList l = TQStringList::split( "$@@$", s );
135  TQStringList::ConstIterator it = l.begin();
136  bool readingKey = true; // true, then false, then true, etc.
137  TQString key;
138  for ( ; it != l.end(); ++it ) {
139  if ( readingKey )
140  key = *it;
141  else
142  metaData.replace( key, *it );
143  readingKey = !readingKey;
144  }
145  Q_ASSERT( readingKey ); // an odd number of items would be, well, odd ;-)
146  }
147  return true; // Success, even if no metadata was found
148  }
149  return false; // Couldn't decode the URLs
150 }
151 
152 #ifdef TQ_WS_QWS
153 bool KURLDrag::decode( TQStringList const &e, KURL::List &uris )
154 {
155  TQStringList::ConstIterator end(e.end());
156  for(TQStringList::ConstIterator it=e.begin(); it!=end; ++it)
157  {
158  KURL url = KURL( *it, 106 ); // 106 is mib enum for utf8 codec
159  if ( !url.isValid() )
160  {
161  uris.clear();
162  break;
163  }
164  uris.append( url );
165  }
166  return !uris.isEmpty();
167 }
168 #endif
169 
171 
172 const char * KURLDrag::format( int i ) const
173 {
174  if ( i == 0 )
175  return "text/uri-list";
176  else if ( i == 1 )
177  return "application/x-tdeio-metadata";
178  if ( d && d->m_exportAsText == false )
179  return 0;
180  if ( i == 2 )
181  return "text/plain";
182  else if ( i == 3 ) //Support this for apps that use plain XA_STRING clipboard
183  return "text/plain;charset=ISO-8859-1";
184  else if ( i == 4 ) //Support this for apps that use the UTF_STRING clipboard
185  return "text/plain;charset=UTF-8";
186  else return 0;
187 }
188 
189 TQByteArray KURLDrag::encodedData( const char* mime ) const
190 {
191  TQByteArray a;
192  TQCString mimetype( mime );
193  if ( mimetype == "text/uri-list" )
194  return TQUriDrag::encodedData( mime );
195  else if ( mimetype == "text/plain" )
196  {
197  TQStringList uris;
198  for (TQStrListIterator it(m_urls); *it; ++it)
199  uris.append(stringToUrl(*it).prettyURL());
200 
201  TQCString s = uris.join( "\n" ).local8Bit();
202  if( uris.count() > 1 ) // terminate last line, unless it's the only line
203  s.append( "\n" );
204  a.resize( s.length());
205  memcpy( a.data(), s.data(), s.length()); // no trailing zero in clipboard text
206  }
207  else if ( mimetype.lower() == "text/plain;charset=iso-8859-1")
208  {
209  TQStringList uris;
210  for (TQStrListIterator it(m_urls); *it; ++it)
211  for (TQStrListIterator it(m_urls); *it; ++it)
212  uris.append(stringToUrl(*it).url(0, 4)); // 4 is mib for latin1
213 
214  TQCString s = uris.join( "\n" ).latin1();
215  if( uris.count() > 1 )
216  s.append( "\n" );
217  a.resize( s.length());
218  memcpy( a.data(), s.data(), s.length());
219  }
220  else if ( mimetype.lower() == "text/plain;charset=utf-8")
221  {
222  TQStringList uris;
223  for (TQStrListIterator it(m_urls); *it; ++it)
224  uris.append(stringToUrl(*it).prettyURL());
225 
226  TQCString s = uris.join( "\n" ).utf8();
227  if( uris.count() > 1 )
228  s.append( "\n" );
229  a.resize( s.length());
230  memcpy( a.data(), s.data(), s.length());
231  }
232  else if ( mimetype == "application/x-tdeio-metadata" )
233  {
234  if ( !m_metaData.isEmpty() )
235  {
236  TQString s;
237  TQMap<TQString,TQString>::ConstIterator it;
238  for( it = m_metaData.begin(); it != m_metaData.end(); ++it )
239  {
240  s += it.key();
241  s += "$@@$";
242  s += it.data();
243  s += "$@@$";
244  }
245  a.resize( s.length() + 1 );
246  memcpy( a.data(), s.latin1(), a.size() );
247  }
248  }
249  return a;
250 }
251 
252 KURL KURLDrag::stringToUrl(const TQCString &s)
253 {
254  if (strncmp(s.data(), "file:", 5) == 0)
255  return KURL(s, TDEGlobal::locale()->fileEncodingMib());
256 
257  return KURL(s, 106); // 106 is mib enum for utf8 codec;
258 }
259 
260 TQString KURLDrag::urlToString(const KURL &url)
261 {
262  if (url.isLocalFile())
263  {
264 #if 1
265  return url.url(0, TDEGlobal::locale()->fileEncodingMib());
266 #else
267  // According to the XDND spec, file:/ URLs for DND must have
268  // the hostname part. But in really it just breaks many apps,
269  // so it's disabled for now.
270  TQString s = url.url(0, TDEGlobal::locale()->fileEncodingMib());
271  if( !s.startsWith( "file://" ))
272  {
273  char hostname[257];
274  if ( gethostname( hostname, 255 ) == 0 )
275  {
276  hostname[256] = '\0';
277  return TQString( "file://" ) + hostname + s.mid( 5 );
278  }
279  }
280 #endif
281  }
282 
283  if ( url.protocol() == "mailto" ) {
284  return url.path();
285  }
286 
287  return url.url(0, 106); // 106 is mib enum for utf8 codec
288 }
289 
290 // deprecated ctor
291 KURLDrag::KURLDrag( const TQStrList & urls, const TQMap<TQString,TQString>& metaData,
292  TQWidget * dragSource, const char* name ) :
293 TQUriDrag( urls, dragSource, name ), m_urls( urls ), m_metaData( metaData ), d( 0 ) {}
KURLDrag
This class is to be used instead of TQUriDrag when using KURL.
Definition: kurldrag.h:45
KURLDrag::stringToUrl
static KURL stringToUrl(const TQCString &s)
Converts a string used for dragging to a URL.
Definition: kurldrag.cpp:252
KURLDrag::setExportAsText
void setExportAsText(bool exp)
By default, KURLDrag also exports the URLs as plain text, for e.g.
Definition: kurldrag.cpp:67
KURLDrag::encodedData
virtual TQByteArray encodedData(const char *mime) const
Definition: kurldrag.cpp:189
KURLDrag::newDrag
static KURLDrag * newDrag(const KURL::List &urls, TQWidget *dragSource=0, const char *name=0) TDE_DEPRECATED
Definition: kurldrag.cpp:75
KURLDrag::format
virtual const char * format(int i) const
Definition: kurldrag.cpp:172
KURLDrag::metaData
TQMap< TQString, TQString > & metaData()
Meta-data to associate with those URLs.
Definition: kurldrag.h:100
KURLDrag::KURLDrag
KURLDrag(const KURL::List &urls, TQWidget *dragSource=0, const char *name=0)
Constructs an object to drag the list of URLs in urls.
Definition: kurldrag.cpp:36
KURLDrag::urlToString
static TQString urlToString(const KURL &url)
Converts a URL to a string representation suitable for dragging.
Definition: kurldrag.cpp:260
KURLDrag::decode
static bool decode(const TQMimeSource *e, KURL::List &urls)
Convenience method that decodes the contents of e into a list of KURLs.
Definition: kurldrag.cpp:86
KURL::List
KURL::List is a TQValueList that contains KURLs with a few convenience methods.
Definition: kurl.h:188
KURL
Represents and parses a URL.
Definition: kurl.h:128
KURL::path
TQString path() const
Returns the current decoded path.
Definition: kurl.h:532
KURL::protocol
TQString protocol() const
Returns the protocol for the URL.
Definition: kurl.h:367
KURL::url
TQString url(int _trailing=0, int encoding_hint=0) const
Returns the URL as string, with all escape sequences intact, encoded in a given charset.
Definition: kurl.cpp:1499
KURL::isLocalFile
bool isLocalFile() const
Tests if the file is local.
Definition: kurl.cpp:1399
KURL::prettyURL
TQString prettyURL(int _trailing=0) const
Returns the URL as string in human-friendly format.
Definition: kurl.cpp:1559
KURL::isValid
bool isValid() const
Tests if the URL is well formed.
Definition: kurl.h:826
TDEGlobal::locale
static TDELocale * locale()
Returns the global locale object.
Definition: tdeglobal.cpp:108
tdelocale.h

tdecore

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

tdecore

Skip menu "tdecore"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdecore by doxygen 1.9.1
This website is maintained by Timothy Pearson.