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   timer->stop();
00054 }
00055 
00056 void DataSlave::resume() {
00057   _suspended = false;
00058   kdDebug() << this << k_funcinfo << endl;
00059   // aarrrgh! This makes the once hyper fast and efficient data protocol
00060   // implementation slow as molasses. But it wouldn't work otherwise,
00061   // and I don't want to start messing around with threads
00062   timer->start(KIO_DATA_POLL_INTERVAL);
00063 }
00064 
00065 void DataSlave::dispatchNext() {
00066   if (dispatchQueue.empty()) return;
00067 
00068   const QueueStruct &q = dispatchQueue.front();
00069   //kdDebug() << this << k_funcinfo << "dispatching " << q.type << " " << dispatchQueue.size() << " left" << endl;
00070   switch (q.type) {
00071     case QueueMimeType:     mimeType(q.s); break;
00072     case QueueTotalSize:    totalSize(q.size); break;
00073     case QueueSendMetaData: sendMetaData(); break;
00074     case QueueData:     data(q.ba); break;
00075     case QueueFinished:
00076       finished();
00077       kill();           // commit suicide, we don't want to be reused
00078       emit slaveDied(this);
00079       //delete this;
00080       return;
00081   }/*end switch*/
00082 
00083   dispatchQueue.pop_front();
00084 }
00085 
00086 void DataSlave::send(int cmd, const QByteArray &arr) {
00087   QDataStream stream(arr, IO_ReadOnly);
00088 
00089   KURL url;
00090 
00091   switch (cmd) {
00092     case CMD_GET: {
00093       stream >> url;
00094       get(url);
00095       break;
00096     }
00097     case CMD_MIMETYPE: {
00098       stream >> url;
00099       mimetype(url);
00100       break;
00101     }
00102     // ignore these (must not emit error, otherwise SIGSEGV occurs)
00103     case CMD_META_DATA:
00104     case CMD_SUBURL:
00105       break;
00106     default:
00107       error(ERR_UNSUPPORTED_ACTION,
00108         unsupportedActionErrorString(QString::fromLatin1("data"),cmd));
00109   }/*end switch*/
00110 }
00111 
00112 bool DataSlave::suspended() {
00113   return _suspended;
00114 }
00115 
00116 void DataSlave::setHost(const QString &/*host*/, int /*port*/,
00117                      const QString &/*user*/, const QString &/*passwd*/) {
00118   // irrelevant -> will be ignored
00119 }
00120 
00121 void DataSlave::setConfig(const MetaData &/*config*/) {
00122   // FIXME: decide to handle this directly or not at all
00123 #if 0
00124     QByteArray data;
00125     QDataStream stream( data, IO_WriteOnly );
00126     stream << config;
00127     slaveconn.send( CMD_CONFIG, data );
00128 #endif
00129 }
00130 
00131 void DataSlave::setAllMetaData(const MetaData &md) {
00132   meta_data = md;
00133 }
00134 
00135 void DataSlave::sendMetaData() {
00136   emit metaData(meta_data);
00137 }
00138 
00139 void DataSlave::virtual_hook( int id, void* data ) {
00140   switch (id) {
00141     case VIRTUAL_SUSPEND: suspend(); return;
00142     case VIRTUAL_RESUME: resume(); return;
00143     case VIRTUAL_SEND: {
00144       SendParams *params = reinterpret_cast<SendParams *>(data);
00145       send(params->cmd, *params->arr);
00146       return;
00147     }
00148     case VIRTUAL_HOLD: {
00149       HoldParams *params = reinterpret_cast<HoldParams *>(data);
00150       hold(*params->url);
00151       return;
00152     }
00153     case VIRTUAL_SUSPENDED: {
00154       SuspendedParams *params = reinterpret_cast<SuspendedParams *>(data);
00155       params->retval = suspended();
00156       return;
00157     }
00158     case VIRTUAL_SET_HOST: {
00159       SetHostParams *params = reinterpret_cast<SetHostParams *>(data);
00160       setHost(*params->host,params->port,*params->user,*params->passwd);
00161       return;
00162     }
00163     case VIRTUAL_SET_CONFIG: {
00164       SetConfigParams *params = reinterpret_cast<SetConfigParams *>(data);
00165       setConfig(*params->config);
00166       return;
00167     }
00168     default:
00169       KIO::Slave::virtual_hook( id, data );
00170   }
00171 }
00172 
00173 #include "dataslave.moc"
KDE Logo
This file is part of the documentation for kio Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Aug 4 05:25:29 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2003