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 
00024 #include <qapplication.h>
00025 #include <qclipboard.h>
00026 #include <qdragobject.h>
00027 #include <qtextstream.h>
00028 #include <kurl.h>
00029 #include <kurldrag.h>
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <klineeditdlg.h>
00033 #include <kmessagebox.h>
00034 #include <ktempfile.h>
00035 
00036 bool KIO::isClipboardEmpty()
00037 {
00038 #ifndef QT_NO_MIMECLIPBOARD
00039   QMimeSource *data = QApplication::clipboard()->data();
00040   if ( data->provides( "text/uri-list" ) && data->encodedData( "text/uri-list" ).size() > 0 )
00041     return false;
00042 #else
00043   // Happens with some versions of Qt Embedded... :/
00044   // Guess.
00045   QString data = QApplication::clipboard()->text();
00046   if(data.contains("://"))
00047       return false;
00048 #endif
00049   return true;
00050 }
00051 
00052 KIO::Job *KIO::pasteClipboard( const KURL& dest_url, bool move )
00053 {
00054   if ( !dest_url.isValid() ) {
00055     KMessageBox::error( 0L, i18n( "Malformed URL\n%1" ).arg( dest_url.url() ) );
00056     return 0;
00057   }
00058 
00059 #ifndef QT_NO_MIMECLIPBOARD
00060   QMimeSource *data = QApplication::clipboard()->data();
00061 
00062   KURL::List urls;
00063   if ( QUriDrag::canDecode( data ) && KURLDrag::decode( data, urls ) ) {
00064     if ( urls.count() == 0 ) {
00065       KMessageBox::error( 0L, i18n("The clipboard is empty"));
00066       return 0;
00067     }
00068 
00069     KIO::Job *res = 0;
00070     if ( move )
00071       res = KIO::move( urls, dest_url );
00072     else
00073       res = KIO::copy( urls, dest_url );
00074 
00075     // If moving, erase the clipboard contents, the original files don't exist anymore
00076     if ( move )
00077       QApplication::clipboard()->clear();
00078     return res;
00079   }
00080 #else
00081   QStringList data = QStringList::split("\n", QApplication::clipboard()->text());
00082   KURL::List urls;
00083   KURLDrag::decode(data, urls);
00084 #endif
00085 
00086   QByteArray ba;
00087 
00088 #ifndef QT_NO_MIMECLIPBOARD
00089   QString text;
00090   if ( QTextDrag::canDecode( data ) && QTextDrag::decode( data, text ) )
00091   {
00092       QTextStream txtStream( ba, IO_WriteOnly );
00093       txtStream << text;
00094   }
00095   else
00096       ba = data->encodedData( data->format() );
00097 #else
00098   QTextStream txtStream( ba, IO_WriteOnly );
00099   for(QStringList::Iterator it=data.begin(); it!=data.end(); it++)
00100       txtStream << *it;
00101 #endif
00102 
00103   if ( ba.size() == 0 )
00104   {
00105     KMessageBox::sorry(0, i18n("The clipboard is empty"));
00106     return 0;
00107   }
00108 
00109   pasteData( dest_url, ba );
00110   return 0;
00111 }
00112 
00113 void KIO::pasteData( const KURL& u, const QByteArray& _data )
00114 {
00115   KLineEditDlg l( i18n("Filename for clipboard content:"), "", 0L );
00116   int x = l.exec();
00117   if ( x ) {
00118     QString url = l.text();
00119     if ( url.isEmpty() ) {
00120       KMessageBox::error( 0L, i18n("You did not enter a filename"));
00121       return;
00122     }
00123 
00124     KURL myurl(u);
00125     myurl.addPath( l.text() );
00126 
00127     // We could use KIO::put here, but that would require a class
00128     // for the slotData call. With NetAcess, we can do a synchronous call.
00129     // NOTE: upload() overwrites the destination if it exists. TODO dialog box.
00130 
00131     KTempFile tempFile;
00132     tempFile.setAutoDelete( true );
00133     tempFile.dataStream()->writeRawBytes( _data.data(), _data.size() );
00134     tempFile.close();
00135 
00136     (void) KIO::NetAccess::upload( tempFile.name(), myurl );
00137   }
00138 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:21:33 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001