paste.cpp

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 2000 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016    Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "paste.h"
00020 #include "pastedialog.h"
00021 
00022 #include "kio/job.h"
00023 #include "kio/global.h"
00024 #include "kio/netaccess.h"
00025 #include "kio/observer.h"
00026 #include "kio/renamedlg.h"
00027 #include "kio/kprotocolmanager.h"
00028 
00029 #include <kurl.h>
00030 #include <kurldrag.h>
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033 #include <kinputdialog.h>
00034 #include <kmessagebox.h>
00035 #include <kmimetype.h>
00036 #include <ktempfile.h>
00037 
00038 #include <qapplication.h>
00039 #include <qclipboard.h>
00040 #include <qdragobject.h>
00041 #include <qtextstream.h>
00042 #include <qvaluevector.h>
00043 
00044 static KURL getNewFileName( const KURL &u, const QString& text, const QString& suggestedFileName = QString::null )
00045 {
00046   bool ok;
00047   QString dialogText( text );
00048   if ( dialogText.isEmpty() )
00049     dialogText = i18n( "Filename for clipboard content:" );
00050   QString file = KInputDialog::getText( QString::null, dialogText, suggestedFileName, &ok );
00051   if ( !ok )
00052      return KURL();
00053 
00054   KURL myurl(u);
00055   myurl.addPath( file );
00056 
00057   if (KIO::NetAccess::exists(myurl, false, 0))
00058   {
00059       kdDebug(7007) << "Paste will overwrite file.  Prompting..." << endl;
00060       KIO::RenameDlg_Result res = KIO::R_OVERWRITE;
00061 
00062       QString newPath;
00063       // Ask confirmation about resuming previous transfer
00064       res = Observer::self()->open_RenameDlg(
00065                           0L, i18n("File Already Exists"),
00066                           u.pathOrURL(),
00067                           myurl.pathOrURL(),
00068                           (KIO::RenameDlg_Mode) (KIO::M_OVERWRITE | KIO::M_SINGLE), newPath);
00069 
00070       if ( res == KIO::R_RENAME )
00071       {
00072           myurl = newPath;
00073       }
00074       else if ( res == KIO::R_CANCEL )
00075       {
00076           return KURL();
00077       }
00078   }
00079 
00080   return myurl;
00081 }
00082 
00083 // The finaly step: write _data to tempfile and move it to neW_url
00084 static KIO::CopyJob* pasteDataAsyncTo( const KURL& new_url, const QByteArray& _data )
00085 {
00086      KTempFile tempFile;
00087      tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00088      tempFile.close();
00089 
00090      KURL orig_url;
00091      orig_url.setPath(tempFile.name());
00092 
00093      return KIO::move( orig_url, new_url );
00094 }
00095 
00096 #ifndef QT_NO_MIMECLIPBOARD
00097 static KIO::CopyJob* chooseAndPaste( const KURL& u, QMimeSource* data,
00098                                      const QValueVector<QCString>& formats,
00099                                      const QString& text,
00100                                      const QString& suggestedFileName,
00101                                      QWidget* widget,
00102                                      bool clipboard )
00103 {
00104     QStringList formatLabels;
00105     for ( uint i = 0; i < formats.size(); ++i ) {
00106         const QCString& fmt = formats[i];
00107         KMimeType::Ptr mime = KMimeType::mimeType( fmt );
00108         if ( mime != KMimeType::defaultMimeTypePtr() )
00109             formatLabels.append( i18n( "%1 (%2)" ).arg( mime->comment() ).arg( fmt ) );
00110         else
00111             formatLabels.append( fmt );
00112     }
00113 
00114     QString dialogText( text );
00115     if ( dialogText.isEmpty() )
00116         dialogText = i18n( "Filename for clipboard content:" );
00117     KIO::PasteDialog dlg( QString::null, dialogText, suggestedFileName, formatLabels, widget, clipboard );
00118 
00119     if ( dlg.exec() != KDialogBase::Accepted )
00120         return 0;
00121 
00122     if ( clipboard && dlg.clipboardChanged() ) {
00123         KMessageBox::sorry( widget,
00124                             i18n( "The clipboard has changed since you used 'paste': "
00125                                   "the chosen data format is no longer applicable. "
00126                                   "Please copy again what you wanted to paste." ) );
00127         return 0;
00128     }
00129 
00130     const QString result = dlg.lineEditText();
00131     const QCString chosenFormat = formats[ dlg.comboItem() ];
00132 
00133     kdDebug() << " result=" << result << " chosenFormat=" << chosenFormat << endl;
00134     KURL new_url( u );
00135     new_url.addPath( result );
00136     // if "data" came from QClipboard, then it was deleted already - by a nice 0-seconds timer
00137     // In that case, get it again. Let's hope the user didn't copy something else meanwhile :/
00138     if ( clipboard ) {
00139         data = QApplication::clipboard()->data();
00140     }
00141     const QByteArray ba = data->encodedData( chosenFormat );
00142     return pasteDataAsyncTo( new_url, ba );
00143 }
00144 #endif
00145 
00146 // KDE4: remove
00147 KIO_EXPORT bool KIO::isClipboardEmpty()
00148 {
00149 #ifndef QT_NO_MIMECLIPBOARD
00150   QMimeSource *data = QApplication::clipboard()->data();
00151   if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
00152     return false;
00153 #else
00154   // Happens with some versions of Qt Embedded... :/
00155   // Guess.
00156   QString data = QApplication::clipboard()->text();
00157   if(data.contains("://"))
00158       return false;
00159 #endif
00160   return true;
00161 }
00162 
00163 #ifndef QT_NO_MIMECLIPBOARD
00164 // The main method for dropping
00165 KIO::CopyJob* KIO::pasteMimeSource( QMimeSource* data, const KURL& dest_url,
00166                                     const QString& dialogText, QWidget* widget, bool clipboard )
00167 {
00168   QByteArray ba;
00169 
00170   const QString suggestedFileName = QString::fromUtf8(data->encodedData("application/x-kde-suggestedfilename"));
00171 
00172   // Now check for plain text
00173   // We don't want to display a mimetype choice for a QTextDrag, those mimetypes look ugly.
00174   QString text;
00175   if ( QTextDrag::canDecode( data ) && QTextDrag::decode( data, text ) )
00176   {
00177       QTextStream txtStream( ba, IO_WriteOnly );
00178       txtStream << text;
00179   }
00180   else
00181   {
00182       QValueVector<QCString> formats;
00183       const char* fmt;
00184       for ( int i = 0; ( fmt = data->format( i ) ); ++i ) {
00185           if ( qstrcmp( fmt, "application/x-qiconlist" ) == 0 ) // see QIconDrag
00186               continue;
00187           if ( qstrcmp( fmt, "application/x-kde-cutselection" ) == 0 ) // see KonqDrag
00188               continue;
00189           if ( qstrcmp( fmt, "application/x-kde-suggestedfilename" ) == 0 )
00190               continue;
00191           if ( qstrncmp( fmt, "x-kmail-drag/", 13 ) == 0 ) // "startsWith"
00192               continue;
00193           if ( strchr( fmt, '/' ) == 0 ) // e.g. TARGETS, MULTIPLE, TIMESTAMP
00194               continue;
00195           formats.append( fmt );
00196       }
00197 
00198       if ( formats.size() == 0 )
00199           return 0;
00200 
00201       if ( formats.size() > 1 ) {
00202           return chooseAndPaste( dest_url, data, formats, dialogText, suggestedFileName, widget, clipboard );
00203       }
00204       ba = data->encodedData( formats.first() );
00205   }
00206   if ( ba.size() == 0 )
00207   {
00208     KMessageBox::sorry(0, i18n("The clipboard is empty"));
00209     return 0;
00210   }
00211 
00212   KURL new_url = getNewFileName( dest_url, dialogText, suggestedFileName );
00213   if (new_url.isEmpty())
00214       return 0;
00215   return pasteDataAsyncTo( new_url, ba );
00216 }
00217 #endif
00218 
00219 // The main method for pasting
00220 KIO_EXPORT KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
00221 {
00222   if ( !dest_url.isValid() ) {
00223     KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
00224     return 0;
00225   }
00226 
00227 #ifndef QT_NO_MIMECLIPBOARD
00228   QMimeSource *data = QApplication::clipboard()->data();
00229 
00230   // First check for URLs.
00231   KURL::List urls;
00232   if ( KURLDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00233     if ( urls.count() == 0 ) {
00234       KMessageBox::error( 0L, i18n("The clipboard is empty"));
00235       return 0;
00236     }
00237 
00238     KIO::Job *res = 0;
00239     if ( move )
00240       res = KIO::move( urls, dest_url );
00241     else
00242       res = KIO::copy( urls, dest_url );
00243 
00244     // If moving, erase the clipboard contents, the original files don't exist anymore
00245     if ( move )
00246       QApplication::clipboard()->clear();
00247     return res;
00248   }
00249   return pasteMimeSource( data, dest_url, QString::null, 0 /*TODO parent widget*/, true /*clipboard*/ );
00250 #else
00251   QByteArray ba;
00252   QTextStream txtStream( ba, IO_WriteOnly );
00253   QStringList data = QStringList::split("\n", QApplication::clipboard()->text());
00254   KURL::List urls;
00255   KURLDrag::decode(data, urls);
00256   QStringList::Iterator end(data.end());
00257   for(QStringList::Iterator it=data.begin(); it!=end; ++it)
00258       txtStream << *it;
00259   if ( ba.size() == 0 )
00260   {
00261     KMessageBox::sorry(0, i18n("The clipboard is empty"));
00262     return 0;
00263   }
00264   return pasteDataAsync( dest_url, ba );
00265 #endif
00266 }
00267 
00268 
00269 KIO_EXPORT void KIO::pasteData( const KURL& u, const QByteArray& _data )
00270 {
00271     KURL new_url = getNewFileName( u, QString::null );
00272     // We could use KIO::put here, but that would require a class
00273     // for the slotData call. With NetAccess, we can do a synchronous call.
00274 
00275     if (new_url.isEmpty())
00276        return;
00277 
00278     KTempFile tempFile;
00279     tempFile.setAutoDelete( true );
00280     tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00281     tempFile.close();
00282 
00283     (void) KIO::NetAccess::upload( tempFile.name(), new_url, 0 );
00284 }
00285 
00286 KIO_EXPORT KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const QByteArray& _data )
00287 {
00288     return pasteDataAsync( u, _data, QString::null );
00289 }
00290 
00291 KIO_EXPORT KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const QByteArray& _data, const QString& text )
00292 {
00293     KURL new_url = getNewFileName( u, text );
00294 
00295     if (new_url.isEmpty())
00296        return 0;
00297 
00298     return pasteDataAsyncTo( new_url, _data );
00299 }
00300 
00301 KIO_EXPORT QString KIO::pasteActionText()
00302 {
00303     QMimeSource *data = QApplication::clipboard()->data();
00304     KURL::List urls;
00305     if ( KURLDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00306         if ( urls.isEmpty() )
00307             return QString::null; // nothing to paste
00308         else if ( urls.first().isLocalFile() )
00309             return i18n( "&Paste File", "&Paste %n Files", urls.count() );
00310         else
00311             return i18n( "&Paste URL", "&Paste %n URLs", urls.count() );
00312     } else if ( data->format(0) != 0 ) {
00313         return i18n( "&Paste Clipboard Contents" );
00314     } else {
00315         return QString::null;
00316     }
00317 }
00318 
KDE Home | KDE Accessibility Home | Description of Access Keys