1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
|
/* This file is part of the KDE libraries
Copyright (C) 2000 David Faure <faure@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "paste.h"
#include "pastedialog.h"
#include "kio/job.h"
#include "kio/global.h"
#include "kio/netaccess.h"
#include "kio/observer.h"
#include "kio/renamedlg.h"
#include "kio/kprotocolmanager.h"
#include <kurl.h>
#include <kurldrag.h>
#include <kdebug.h>
#include <klocale.h>
#include <kinputdialog.h>
#include <kmessagebox.h>
#include <kmimetype.h>
#include <ktempfile.h>
#include <tqapplication.h>
#include <clipboard.h>
#include <tqdragobject.h>
#include <textstream.h>
#include <tqvaluevector.h>
static KURL getNewFileName( const KURL &u, const TQString& text )
{
bool ok;
TQString dialogText( text );
if ( dialogText.isEmpty() )
dialogText = i18n( "Filename for clipboard content:" );
TQString file = KInputDialog::getText( TQString::null, dialogText, TQString::null, &ok );
if ( !ok )
return KURL();
KURL myurl(u);
myurl.addPath( file );
if (KIO::NetAccess::exists(myurl, false, 0))
{
kdDebug(7007) << "Paste will overwrite file. Prompting..." << endl;
KIO::RenameDlg_Result res = KIO::R_OVERWRITE;
TQString newPath;
// Ask confirmation about resuming previous transfer
res = Observer::self()->open_RenameDlg(
0L, i18n("File Already Exists"),
u.pathOrURL(),
myurl.pathOrURL(),
(KIO::RenameDlg_Mode) (KIO::M_OVERWRITE | KIO::M_SINGLE), newPath);
if ( res == KIO::R_RENAME )
{
myurl = newPath;
}
else if ( res == KIO::R_CANCEL )
{
return KURL();
}
}
return myurl;
}
// The finaly step: write _data to tempfile and move it to neW_url
static KIO::CopyJob* pasteDataAsyncTo( const KURL& new_url, const TQByteArray& _data )
{
KTempFile tempFile;
tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
tempFile.close();
KURL orig_url;
orig_url.setPath(tempFile.name());
return KIO::move( orig_url, new_url );
}
#ifndef QT_NO_MIMECLIPBOARD
static KIO::CopyJob* chooseAndPaste( const KURL& u, TQMimeSource* data,
const TQValueVector<TQCString>& formats,
const TQString& text,
TQWidget* widget,
bool clipboard )
{
TQStringList formatLabels;
for ( uint i = 0; i < formats.size(); ++i ) {
const TQCString& fmt = formats[i];
KMimeType::Ptr mime = KMimeType::mimeType( fmt );
if ( mime != KMimeType::defaultMimeTypePtr() )
formatLabels.append( i18n( "%1 (%2)" ).arg( mime->comment() ).arg( fmt.data() ) );
else
formatLabels.append( fmt );
}
TQString dialogText( text );
if ( dialogText.isEmpty() )
dialogText = i18n( "Filename for clipboard content:" );
KIO::PasteDialog dlg( TQString::null, dialogText, TQString::null, formatLabels, widget, clipboard );
if ( dlg.exec() != KDialogBase::Accepted )
return 0;
if ( clipboard && dlg.clipboardChanged() ) {
KMessageBox::sorry( widget,
i18n( "The clipboard has changed since you used 'paste': "
"the chosen data format is no longer applicable. "
"Please copy again what you wanted to paste." ) );
return 0;
}
const TQString result = dlg.lineEditText();
const TQCString chosenFormat = formats[ dlg.comboItem() ];
kdDebug() << " result=" << result << " chosenFormat=" << chosenFormat << endl;
KURL new_url( u );
new_url.addPath( result );
// if "data" came from TQClipboard, then it was deleted already - by a nice 0-seconds timer
// In that case, get it again. Let's hope the user didn't copy something else meanwhile :/
if ( clipboard ) {
data = TQApplication::clipboard()->data();
}
const TQByteArray ba = data->encodedData( chosenFormat );
return pasteDataAsyncTo( new_url, ba );
}
#endif
// KDE4: remove
KIO_EXPORT bool KIO::isClipboardEmpty()
{
#ifndef QT_NO_MIMECLIPBOARD
TQMimeSource *data = TQApplication::clipboard()->data();
if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
return false;
#else
// Happens with some versions of Qt Embedded... :/
// Guess.
TQString data = TQApplication::clipboard()->text();
if(data.contains("://"))
return false;
#endif
return true;
}
#ifndef QT_NO_MIMECLIPBOARD
// The main method for dropping
KIO::CopyJob* KIO::pasteMimeSource( TQMimeSource* data, const KURL& dest_url,
const TQString& dialogText, TQWidget* widget, bool clipboard )
{
TQByteArray ba;
// Now check for plain text
// We don't want to display a mimetype choice for a TQTextDrag, those mimetypes look ugly.
TQString text;
if ( TQTextDrag::canDecode( data ) && TQTextDrag::decode( data, text ) )
{
TQTextStream txtStream( ba, IO_WriteOnly );
txtStream << text;
}
else
{
TQValueVector<TQCString> formats;
const char* fmt;
for ( int i = 0; ( fmt = data->format( i ) ); ++i ) {
if ( qstrcmp( fmt, "application/x-qiconlist" ) == 0 ) // see QIconDrag
continue;
if ( qstrcmp( fmt, "application/x-kde-cutselection" ) == 0 ) // see KonqDrag
continue;
if ( strchr( fmt, '/' ) == 0 ) // e.g. TARGETS, MULTIPLE, TIMESTAMP
continue;
formats.append( fmt );
}
if ( formats.size() == 0 )
return 0;
if ( formats.size() > 1 ) {
return chooseAndPaste( dest_url, data, formats, dialogText, widget, clipboard );
}
ba = data->encodedData( formats.first() );
}
if ( ba.size() == 0 )
{
KMessageBox::sorry(0, i18n("The clipboard is empty"));
return 0;
}
return pasteDataAsync( dest_url, ba, dialogText );
}
#endif
// The main method for pasting
KIO_EXPORT KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
{
if ( !dest_url.isValid() ) {
KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
return 0;
}
#ifndef QT_NO_MIMECLIPBOARD
TQMimeSource *data = TQApplication::clipboard()->data();
// First check for URLs.
KURL::List urls;
if ( KURLDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
if ( urls.count() == 0 ) {
KMessageBox::error( 0L, i18n("The clipboard is empty"));
return 0;
}
KIO::Job *res = 0;
if ( move )
res = KIO::move( urls, dest_url );
else
res = KIO::copy( urls, dest_url );
// If moving, erase the clipboard contents, the original files don't exist anymore
if ( move )
TQApplication::clipboard()->clear();
return res;
}
return pasteMimeSource( data, dest_url, TQString::null, 0 /*TODO parent widget*/, true /*clipboard*/ );
#else
TQByteArray ba;
TQTextStream txtStream( ba, IO_WriteOnly );
TQStringList data = TQStringList::split("\n", TQApplication::clipboard()->text());
KURL::List urls;
KURLDrag::decode(data, urls);
TQStringList::Iterator end(data.end());
for(TQStringList::Iterator it=data.begin(); it!=end; ++it)
txtStream << *it;
if ( ba.size() == 0 )
{
KMessageBox::sorry(0, i18n("The clipboard is empty"));
return 0;
}
return pasteDataAsync( dest_url, ba );
#endif
}
KIO_EXPORT void KIO::pasteData( const KURL& u, const TQByteArray& _data )
{
KURL new_url = getNewFileName( u, TQString::null );
// We could use KIO::put here, but that would require a class
// for the slotData call. With NetAccess, we can do a synchronous call.
if (new_url.isEmpty())
return;
KTempFile tempFile;
tempFile.setAutoDelete( true );
tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
tempFile.close();
(void) KIO::NetAccess::upload( tempFile.name(), new_url, 0 );
}
KIO_EXPORT KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const TQByteArray& _data )
{
return pasteDataAsync( u, _data, TQString::null );
}
KIO_EXPORT KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const TQByteArray& _data, const TQString& text )
{
KURL new_url = getNewFileName( u, text );
if (new_url.isEmpty())
return 0;
return pasteDataAsyncTo( new_url, _data );
}
KIO_EXPORT TQString KIO::pasteActionText()
{
TQMimeSource *data = TQApplication::clipboard()->data();
KURL::List urls;
if ( KURLDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
if ( urls.isEmpty() )
return TQString::null; // nothing to paste
else if ( urls.first().isLocalFile() )
return i18n( "&Paste File", "&Paste %n Files", urls.count() );
else
return i18n( "&Paste URL", "&Paste %n URLs", urls.count() );
} else if ( data->format(0) != 0 ) {
return i18n( "&Paste Clipboard Contents" );
} else {
return TQString::null;
}
}
|