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 <kinputdialog.h>
00036 #include <kmessagebox.h>
00037 #include <ktempfile.h>
00038
00039 static KURL getNewFileName( const KURL &u, const QString& text )
00040 {
00041 bool ok;
00042 QString dialogText( text );
00043 if ( dialogText.isEmpty() )
00044 dialogText = i18n( "Filename for clipboard content:" );
00045 QString file = KInputDialog::getText( QString::null, dialogText, QString::null, &ok );
00046 if ( !ok )
00047 return KURL();
00048
00049 KURL myurl(u);
00050 myurl.addPath( file );
00051
00052 if (KIO::NetAccess::exists(myurl, false, 0))
00053 {
00054 kdDebug(7007) << "Paste will overwrite file. Prompting..." << endl;
00055 KIO::RenameDlg_Result res = KIO::R_OVERWRITE;
00056
00057 QString newPath;
00058
00059 res = Observer::self()->open_RenameDlg(
00060 0L, i18n("File Already Exists"),
00061 u.pathOrURL(),
00062 myurl.pathOrURL(),
00063 (KIO::RenameDlg_Mode) (KIO::M_OVERWRITE | KIO::M_SINGLE), newPath);
00064
00065 if ( res == KIO::R_RENAME )
00066 {
00067 myurl = newPath;
00068 }
00069 else if ( res == KIO::R_CANCEL )
00070 {
00071 return KURL();
00072 }
00073 }
00074
00075 return myurl;
00076 }
00077
00078 KIO_EXPORT bool KIO::isClipboardEmpty()
00079 {
00080 #ifndef QT_NO_MIMECLIPBOARD
00081 QMimeSource *data = QApplication::clipboard()->data();
00082 if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
00083 return false;
00084 #else
00085
00086
00087 QString data = QApplication::clipboard()->text();
00088 if(data.contains("://"))
00089 return false;
00090 #endif
00091 return true;
00092 }
00093
00094 KIO_EXPORT KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
00095 {
00096 if ( !dest_url.isValid() ) {
00097 KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
00098 return 0;
00099 }
00100
00101 #ifndef QT_NO_MIMECLIPBOARD
00102 QMimeSource *data = QApplication::clipboard()->data();
00103
00104 KURL::List urls;
00105 if ( KURLDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00106 if ( urls.count() == 0 ) {
00107 KMessageBox::error( 0L, i18n("The clipboard is empty"));
00108 return 0;
00109 }
00110
00111 KIO::Job *res = 0;
00112 if ( move )
00113 res = KIO::move( urls, dest_url );
00114 else
00115 res = KIO::copy( urls, dest_url );
00116
00117
00118 if ( move )
00119 QApplication::clipboard()->clear();
00120 return res;
00121 }
00122 #else
00123 QStringList data = QStringList::split("\n", QApplication::clipboard()->text());
00124 KURL::List urls;
00125 KURLDrag::decode(data, urls);
00126 #endif
00127
00128 QByteArray ba;
00129
00130 #ifndef QT_NO_MIMECLIPBOARD
00131 QString text;
00132 if ( QTextDrag::canDecode( data ) && QTextDrag::decode( data, text ) )
00133 {
00134 QTextStream txtStream( ba, IO_WriteOnly );
00135 txtStream << text;
00136 }
00137 else
00138 ba = data->encodedData( data->format() );
00139 #else
00140 QTextStream txtStream( ba, IO_WriteOnly );
00141 for(QStringList::Iterator it=data.begin(); it!=data.end(); it++)
00142 txtStream << *it;
00143 #endif
00144
00145 if ( ba.size() == 0 )
00146 {
00147 KMessageBox::sorry(0, i18n("The clipboard is empty"));
00148 return 0;
00149 }
00150
00151 return pasteDataAsync(dest_url, ba);
00152 }
00153
00154
00155 KIO_EXPORT void KIO::pasteData( const KURL& u, const QByteArray& _data )
00156 {
00157 KURL new_url = getNewFileName( u, QString::null );
00158
00159
00160
00161 if (new_url.isEmpty())
00162 return;
00163
00164 KTempFile tempFile;
00165 tempFile.setAutoDelete( true );
00166 tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00167 tempFile.close();
00168
00169 (void) KIO::NetAccess::upload( tempFile.name(), new_url, 0 );
00170 }
00171
00172 KIO_EXPORT KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const QByteArray& _data )
00173 {
00174 return pasteDataAsync( u, _data, QString::null );
00175 }
00176
00177 KIO_EXPORT KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const QByteArray& _data, const QString& text )
00178 {
00179 KURL new_url = getNewFileName( u, text );
00180
00181 if (new_url.isEmpty())
00182 return 0;
00183
00184 KTempFile tempFile;
00185 tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00186 tempFile.close();
00187
00188 KURL orig_url;
00189 orig_url.setPath(tempFile.name());
00190
00191 return KIO::move( orig_url, new_url );
00192 }
This file is part of the documentation for kio Library Version 3.4.0.