kio Library API Documentation

dataslave.cpp

00001 /*
00002  *  This file is part of the KDE libraries
00003  *  Copyright (c) 2003 Leo Savernik <l.savernik@aon.at>
00004  *  Derived from slave.cpp
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Library General Public
00008  *  License version 2 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  **/
00020 
00021 #include <config.h>
00022 
00023 #include "dataslave.h"
00024 #include "dataprotocol.h"
00025 
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 
00029 #include <qtimer.h>
00030 
00031 using namespace KIO;
00032 
00033 #define KIO_DATA_POLL_INTERVAL 0
00034 
00035 DataSlave::DataSlave() :
00036     Slave(true, 0, "data", QString::null)
00037 {
00038   _suspended = false;
00039   timer = new QTimer(this);
00040   connect(timer, SIGNAL(timeout()), SLOT(dispatchNext()));
00041   timer->start(KIO_DATA_POLL_INTERVAL);
00042 }
00043 
00044 DataSlave::~DataSlave() {
00045 }
00046 
00047 void DataSlave::hold(const KURL &/*url*/) {
00048   // ignored
00049 }
00050 
00051 void DataSlave::suspend() {
00052   _suspended = true;
00053   //kdDebug() << this << k_funcinfo << endl;
00054   timer->stop();
00055 }
00056 
00057 void DataSlave::resume() {
00058   _suspended = false;
00059   //kdDebug() << this << k_funcinfo << endl;
00060   // aarrrgh! This makes the once hyper fast and efficient data protocol
00061   // implementation slow as molasses. But it wouldn't work otherwise,
00062   // and I don't want to start messing around with threads
00063   timer->start(KIO_DATA_POLL_INTERVAL);
00064 }
00065 
00066 void DataSlave::dispatchNext() {
00067   if (dispatchQueue.empty()) return;
00068 
00069   const QueueStruct &q = dispatchQueue.front();
00070   //kdDebug() << this << k_funcinfo << "dispatching " << q.type << " " << dispatchQueue.size() << " left" << endl;
00071   switch (q.type) {
00072     case QueueMimeType:     mimeType(q.s); break;
00073     case QueueTotalSize:    totalSize(q.size); break;
00074     case QueueSendMetaData: sendMetaData(); break;
00075     case QueueData:     data(q.ba); break;
00076     case QueueFinished:     finished(); break;
00077   }/*end switch*/
00078 
00079   dispatchQueue.pop_front();
00080 }
00081 
00082 void DataSlave::send(int cmd, const QByteArray &arr) {
00083   QDataStream stream(arr, IO_ReadOnly);
00084 
00085   KURL url;
00086 
00087   switch (cmd) {
00088     case CMD_GET: {
00089       stream >> url;
00090       get(url);
00091       break;
00092     }
00093     case CMD_MIMETYPE: {
00094       stream >> url;
00095       mimetype(url);
00096       break;
00097     }
00098     // ignore these (must not emit error, otherwise SIGSEGV occurs)
00099     case CMD_META_DATA:
00100     case CMD_SUBURL:
00101       break;
00102     default:
00103       error(ERR_UNSUPPORTED_ACTION,
00104         unsupportedActionErrorString(QString::fromLatin1("data"),cmd));
00105   }/*end switch*/
00106 }
00107 
00108 bool DataSlave::suspended() {
00109   return _suspended;
00110 }
00111 
00112 void DataSlave::setHost(const QString &/*host*/, int /*port*/,
00113                      const QString &/*user*/, const QString &/*passwd*/) {
00114   // irrelevant -> will be ignored
00115 }
00116 
00117 void DataSlave::setConfig(const MetaData &/*config*/) {
00118   // FIXME: decide to handle this directly or not at all
00119 #if 0
00120     QByteArray data;
00121     QDataStream stream( data, IO_WriteOnly );
00122     stream << config;
00123     slaveconn.send( CMD_CONFIG, data );
00124 #endif
00125 }
00126 
00127 void DataSlave::setAllMetaData(const MetaData &md) {
00128   meta_data = md;
00129 }
00130 
00131 void DataSlave::sendMetaData() {
00132   emit metaData(meta_data);
00133 }
00134 
00135 void DataSlave::virtual_hook( int id, void* data ) {
00136   switch (id) {
00137     case VIRTUAL_SUSPEND: suspend(); return;
00138     case VIRTUAL_RESUME: resume(); return;
00139     case VIRTUAL_SEND: {
00140       SendParams *params = reinterpret_cast<SendParams *>(data);
00141       send(params->cmd, *params->arr);
00142       return;
00143     }
00144     case VIRTUAL_HOLD: {
00145       HoldParams *params = reinterpret_cast<HoldParams *>(data);
00146       hold(*params->url);
00147       return;
00148     }
00149     case VIRTUAL_SUSPENDED: {
00150       SuspendedParams *params = reinterpret_cast<SuspendedParams *>(data);
00151       params->retval = suspended();
00152       return;
00153     }
00154     case VIRTUAL_SET_HOST: {
00155       SetHostParams *params = reinterpret_cast<SetHostParams *>(data);
00156       setHost(*params->host,params->port,*params->user,*params->passwd);
00157       return;
00158     }
00159     case VIRTUAL_SET_CONFIG: {
00160       SetConfigParams *params = reinterpret_cast<SetConfigParams *>(data);
00161       setConfig(*params->config);
00162       return;
00163     }
00164     default:
00165       KIO::Slave::virtual_hook( id, data );
00166   }
00167 }
00168 
00169 #include "dataslave.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Jul 22 10:17:12 2005 by doxygen 1.3.6 written by Dimitri van Heesch, © 1997-2003