KIO
downloader.cpp
Go to the documentation of this file.
00001 /* 00002 Copyright (c) 2003 Malte Starostik <malte@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 as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "downloader.h" 00021 00022 #include <cstdlib> 00023 #include <cstring> 00024 00025 #include <QtCore/QTextCodec> 00026 00027 #include <kcharsets.h> 00028 #include <kglobal.h> 00029 #include <klocale.h> 00030 #include <kio/job.h> 00031 00032 namespace KPAC 00033 { 00034 Downloader::Downloader( QObject* parent ) 00035 : QObject( parent ) 00036 { 00037 } 00038 00039 void Downloader::download( const KUrl& url ) 00040 { 00041 m_data.resize( 0 ); 00042 m_script.clear(); 00043 m_scriptURL = url; 00044 00045 KIO::TransferJob* job = KIO::get( url, KIO::NoReload, KIO::HideProgressInfo ); 00046 connect( job, SIGNAL(data(KIO::Job*,QByteArray)), 00047 SLOT(data(KIO::Job*,QByteArray)) ); 00048 connect( job, SIGNAL (redirection(KIO::Job*,KUrl)), 00049 SLOT(redirection(KIO::Job*,KUrl)) ); 00050 connect( job, SIGNAL(result(KJob*)), SLOT(result(KJob*)) ); 00051 } 00052 00053 void Downloader::failed() 00054 { 00055 emit result( false ); 00056 } 00057 00058 void Downloader::setError( const QString& error ) 00059 { 00060 m_error = error; 00061 } 00062 00063 void Downloader::redirection( KIO::Job* , const KUrl& url ) 00064 { 00065 m_scriptURL = url; 00066 } 00067 00068 void Downloader::data( KIO::Job*, const QByteArray& data ) 00069 { 00070 unsigned offset = m_data.size(); 00071 m_data.resize( offset + data.size() ); 00072 std::memcpy( m_data.data() + offset, data.data(), data.size() ); 00073 } 00074 00075 static bool hasErrorPage(KJob* job) 00076 { 00077 KIO::TransferJob* tJob = qobject_cast<KIO::TransferJob*>(job); 00078 return (tJob && tJob->isErrorPage()); 00079 } 00080 00081 void Downloader::result( KJob* job ) 00082 { 00083 if ( !job->error() && !hasErrorPage(job) ) 00084 { 00085 bool dummy; 00086 m_script = KGlobal::charsets()->codecForName( 00087 static_cast<KIO::Job*>( job )->queryMetaData( "charset" ), dummy )->toUnicode( m_data ); 00088 emit result( true ); 00089 } 00090 else 00091 { 00092 if ( job->error() ) 00093 setError( i18n( "Could not download the proxy configuration script:\n%1" , 00094 job->errorString() ) ); 00095 else setError( i18n( "Could not download the proxy configuration script" ) ); // error page 00096 failed(); 00097 } 00098 } 00099 } 00100 00101 // vim: ts=4 sw=4 et 00102 #include "downloader.moc" 00103
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:55:18 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Thu May 10 2012 20:55:18 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.