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