• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/tdeio
 

tdeio/tdeio

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

tdeio/tdeio

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

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • 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 tdeio/tdeio by doxygen 1.9.1
This website is maintained by Timothy Pearson.