paste.cpp Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# 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 <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   // Happens with some versions of Qt Embedded... :/
    00047   // Guess.
    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     // If moving, erase the clipboard contents, the original files don't exist anymore
    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         // Ask confirmation about resuming previous transfer
    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     // We could use KIO::put here, but that would require a class
    00154     // for the slotData call. With NetAcess, we can do a synchronous call.
    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.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:14:36 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001