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 #include "kio/observer.h"
00024 #include "kio/renamedlg.h"
00025 #include "kio/kprotocolmanager.h"
00026
00027 #include <qapplication.h>
00028 #include <qclipboard.h>
00029 #include <qdragobject.h>
00030 #include <qtextstream.h>
00031 #include <kurl.h>
00032 #include <kurldrag.h>
00033 #include <kdebug.h>
00034 #include <klocale.h>
00035 #include <klineeditdlg.h>
00036 #include <kmessagebox.h>
00037 #include <ktempfile.h>
00038
00039 bool KIO::isClipboardEmpty()
00040 {
00041 #ifndef QT_NO_MIMECLIPBOARD
00042 QMimeSource *data = QApplication::clipboard()->data();
00043 if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
00044 return false;
00045 #else
00046
00047
00048 QString data = QApplication::clipboard()->text();
00049 if(data.contains("://"))
00050 return false;
00051 #endif
00052 return true;
00053 }
00054
00055 KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
00056 {
00057 if ( !dest_url.isValid() ) {
00058 KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
00059 return 0;
00060 }
00061
00062 #ifndef QT_NO_MIMECLIPBOARD
00063 QMimeSource *data = QApplication::clipboard()->data();
00064
00065 KURL::List urls;
00066 if ( QUriDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00067 if ( urls.count() == 0 ) {
00068 KMessageBox::error( 0L, i18n("The clipboard is empty"));
00069 return 0;
00070 }
00071
00072 KIO::Job *res = 0;
00073 if ( move )
00074 res = KIO::move( urls, dest_url );
00075 else
00076 res = KIO::copy( urls, dest_url );
00077
00078
00079 if ( move )
00080 QApplication::clipboard()->clear();
00081 return res;
00082 }
00083 #else
00084 QStringList data = QStringList::split("\n", QApplication::clipboard()->text());
00085 KURL::List urls;
00086 KURLDrag::decode(data, urls);
00087 #endif
00088
00089 QByteArray ba;
00090
00091 #ifndef QT_NO_MIMECLIPBOARD
00092 QString text;
00093 if ( QTextDrag::canDecode( data ) && QTextDrag::decode( data, text ) )
00094 {
00095 QTextStream txtStream( ba, IO_WriteOnly );
00096 txtStream << text;
00097 }
00098 else
00099 ba = data->encodedData( data->format() );
00100 #else
00101 QTextStream txtStream( ba, IO_WriteOnly );
00102 for(QStringList::Iterator it=data.begin(); it!=data.end(); it++)
00103 txtStream << *it;
00104 #endif
00105
00106 if ( ba.size() == 0 )
00107 {
00108 KMessageBox::sorry(0, i18n("The clipboard is empty"));
00109 return 0;
00110 }
00111
00112 pasteData( dest_url, ba );
00113 return 0;
00114 }
00115
00116 void KIO::pasteData( const KURL& u, const QByteArray& _data )
00117 {
00118 KLineEditDlg l( i18n("Filename for clipboard content:"), "", 0L );
00119 int x = l.exec();
00120 if ( x ) {
00121 QString url = l.text();
00122 if ( url.isEmpty() ) {
00123 KMessageBox::error( 0L, i18n("You did not enter a filename"));
00124 return;
00125 }
00126
00127 KURL myurl(u);
00128 myurl.addPath( l.text() );
00129
00130 if (KIO::NetAccess::exists(myurl, false))
00131 {
00132 kdDebug(7007) << "Paste will overwrite file. Prompting..." << endl;
00133 RenameDlg_Result res = R_OVERWRITE;
00134
00135 QString newPath;
00136
00137 res = Observer::self()->open_RenameDlg(
00138 0L, i18n("File Already Exists"),
00139 u.prettyURL(0, KURL::StripFileProtocol),
00140 myurl.prettyURL(0, KURL::StripFileProtocol),
00141 (RenameDlg_Mode) (M_OVERWRITE | M_SINGLE), newPath);
00142
00143 if ( res == R_RENAME )
00144 {
00145 myurl = newPath;
00146 }
00147 else if ( res == R_CANCEL )
00148 {
00149 return;
00150 }
00151 }
00152
00153
00154
00155
00156 KTempFile tempFile;
00157 tempFile.setAutoDelete( true );
00158 tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00159 tempFile.close();
00160
00161 (void) KIO::NetAccess::upload( tempFile.name(), myurl );
00162 }
00163 }
This file is part of the documentation for kdelibs Version 3.1.5.