kio Library API Documentation

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., 59 Temple Place - Suite 330,
00016    Boston, MA 02111-1307, USA.
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)
00040 {
00041   bool ok;
00042   QString file = KInputDialog::getText( QString::null,
00043       i18n( "Filename for clipboard content:" ), QString::null, &ok );
00044   if ( !ok ) 
00045      return KURL();
00046 
00047   KURL myurl(u);
00048   myurl.addPath( file );
00049 
00050   if (KIO::NetAccess::exists(myurl, false, 0))
00051   {
00052       kdDebug(7007) << "Paste will overwrite file.  Prompting..." << endl;
00053       KIO::RenameDlg_Result res = KIO::R_OVERWRITE;
00054 
00055       QString newPath;
00056       // Ask confirmation about resuming previous transfer
00057       res = Observer::self()->open_RenameDlg(
00058                           0L, i18n("File Already Exists"),
00059                           u.prettyURL(0, KURL::StripFileProtocol),
00060                           myurl.prettyURL(0, KURL::StripFileProtocol),
00061                           (KIO::RenameDlg_Mode) (KIO::M_OVERWRITE | KIO::M_SINGLE), newPath);
00062 
00063       if ( res == KIO::R_RENAME )
00064       {
00065           myurl = newPath;
00066       }
00067       else if ( res == KIO::R_CANCEL )
00068       {
00069           return KURL();
00070       }
00071   }
00072 
00073   return myurl;
00074 }
00075 
00076 bool KIO::isClipboardEmpty()
00077 {
00078 #ifndef QT_NO_MIMECLIPBOARD
00079   QMimeSource *data = QApplication::clipboard()->data();
00080   if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
00081     return false;
00082 #else
00083   // Happens with some versions of Qt Embedded... :/
00084   // Guess.
00085   QString data = QApplication::clipboard()->text();
00086   if(data.contains("://"))
00087       return false;
00088 #endif
00089   return true;
00090 }
00091 
00092 KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
00093 {
00094   if ( !dest_url.isValid() ) {
00095     KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
00096     return 0;
00097   }
00098 
00099 #ifndef QT_NO_MIMECLIPBOARD
00100   QMimeSource *data = QApplication::clipboard()->data();
00101 
00102   KURL::List urls;
00103   if ( KURLDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00104     if ( urls.count() == 0 ) {
00105       KMessageBox::error( 0L, i18n("The clipboard is empty"));
00106       return 0;
00107     }
00108 
00109     KIO::Job *res = 0;
00110     if ( move )
00111       res = KIO::move( urls, dest_url );
00112     else
00113       res = KIO::copy( urls, dest_url );
00114 
00115     // If moving, erase the clipboard contents, the original files don't exist anymore
00116     if ( move )
00117       QApplication::clipboard()->clear();
00118     return res;
00119   }
00120 #else
00121   QStringList data = QStringList::split("\n", QApplication::clipboard()->text());
00122   KURL::List urls;
00123   KURLDrag::decode(data, urls);
00124 #endif
00125 
00126   QByteArray ba;
00127 
00128 #ifndef QT_NO_MIMECLIPBOARD
00129   QString text;
00130   if ( QTextDrag::canDecode( data ) && QTextDrag::decode( data, text ) )
00131   {
00132       QTextStream txtStream( ba, IO_WriteOnly );
00133       txtStream << text;
00134   }
00135   else
00136       ba = data->encodedData( data->format() );
00137 #else
00138   QTextStream txtStream( ba, IO_WriteOnly );
00139   for(QStringList::Iterator it=data.begin(); it!=data.end(); it++)
00140       txtStream << *it;
00141 #endif
00142 
00143   if ( ba.size() == 0 )
00144   {
00145     KMessageBox::sorry(0, i18n("The clipboard is empty"));
00146     return 0;
00147   }
00148 
00149   return pasteDataAsync(dest_url, ba);
00150 }
00151 
00152 
00153 void KIO::pasteData( const KURL& u, const QByteArray& _data )
00154 {
00155     KURL new_url = getNewFileName(u);
00156     // We could use KIO::put here, but that would require a class
00157     // for the slotData call. With NetAcess, we can do a synchronous call.
00158     
00159     if (new_url.isEmpty())
00160        return;
00161     
00162     KTempFile tempFile;
00163     tempFile.setAutoDelete( true );
00164     tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00165     tempFile.close();
00166 
00167     (void) KIO::NetAccess::upload( tempFile.name(), new_url );
00168 }
00169 
00170 KIO::CopyJob* KIO::pasteDataAsync( const KURL& u, const QByteArray& _data )
00171 {
00172     KURL new_url = getNewFileName(u);
00173     // We could use KIO::put here, but that would require a class
00174     // for the slotData call. With NetAcess, we can do a synchronous call.
00175     
00176     if (new_url.isEmpty())
00177        return 0;
00178     
00179      KTempFile tempFile;
00180      tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00181      tempFile.close();
00182   
00183      KURL orig_url;
00184      orig_url.setPath(tempFile.name());
00185 
00186      return KIO::move( orig_url, new_url );
00187 }
KDE Logo
This file is part of the documentation for kio Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Apr 21 18:43:49 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003