paste.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "kio/job.h"
00020 #include "kio/paste.h"
00021 #include "kio/global.h"
00022 #include "kio/netaccess.h"
00023
00024 #include <qapplication.h>
00025 #include <qclipboard.h>
00026 #include <qdragobject.h>
00027 #include <qtextstream.h>
00028 #include <kurl.h>
00029 #include <kurldrag.h>
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <klineeditdlg.h>
00033 #include <kmessagebox.h>
00034 #include <ktempfile.h>
00035
00036 bool KIO::isClipboardEmpty()
00037 {
00038 #ifndef QT_NO_MIMECLIPBOARD
00039 QMimeSource *data = QApplication::clipboard()->data();
00040 if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
00041 return false;
00042 #else
00043
00044
00045 QString data = QApplication::clipboard()->text();
00046 if(data.contains("://"))
00047 return false;
00048 #endif
00049 return true;
00050 }
00051
00052 KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
00053 {
00054 if ( !dest_url.isValid() ) {
00055 KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
00056 return 0;
00057 }
00058
00059 #ifndef QT_NO_MIMECLIPBOARD
00060 QMimeSource *data = QApplication::clipboard()->data();
00061
00062 KURL::List urls;
00063 if ( QUriDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00064 if ( urls.count() == 0 ) {
00065 KMessageBox::error( 0L, i18n("The clipboard is empty"));
00066 return 0;
00067 }
00068
00069 KIO::Job *res = 0;
00070 if ( move )
00071 res = KIO::move( urls, dest_url );
00072 else
00073 res = KIO::copy( urls, dest_url );
00074
00075
00076 if ( move )
00077 QApplication::clipboard()->clear();
00078 return res;
00079 }
00080 #else
00081 QStringList data = QStringList::split("\n", QApplication::clipboard()->text());
00082 KURL::List urls;
00083 KURLDrag::decode(data, urls);
00084 #endif
00085
00086 QByteArray ba;
00087
00088 #ifndef QT_NO_MIMECLIPBOARD
00089 QString text;
00090 if ( QTextDrag::canDecode( data ) && QTextDrag::decode( data, text ) )
00091 {
00092 QTextStream txtStream( ba, IO_WriteOnly );
00093 txtStream << text;
00094 }
00095 else
00096 ba = data->encodedData( data->format() );
00097 #else
00098 QTextStream txtStream( ba, IO_WriteOnly );
00099 for(QStringList::Iterator it=data.begin(); it!=data.end(); it++)
00100 txtStream << *it;
00101 #endif
00102
00103 if ( ba.size() == 0 )
00104 {
00105 KMessageBox::sorry(0, i18n("The clipboard is empty"));
00106 return 0;
00107 }
00108
00109 pasteData( dest_url, ba );
00110 return 0;
00111 }
00112
00113 void KIO::pasteData( const KURL& u, const QByteArray& _data )
00114 {
00115 KLineEditDlg l( i18n("Filename for clipboard content:"), "", 0L );
00116 int x = l.exec();
00117 if ( x ) {
00118 QString url = l.text();
00119 if ( url.isEmpty() ) {
00120 KMessageBox::error( 0L, i18n("You did not enter a filename"));
00121 return;
00122 }
00123
00124 KURL myurl(u);
00125 myurl.addPath( l.text() );
00126
00127
00128
00129
00130
00131 KTempFile tempFile;
00132 tempFile.setAutoDelete( true );
00133 tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00134 tempFile.close();
00135
00136 (void) KIO::NetAccess::upload( tempFile.name(), myurl );
00137 }
00138 }
This file is part of the documentation for kdelibs Version 3.1.0.