KIO
dataslave.cpp
Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 **/ 00020 00021 #include "dataslave.h" 00022 #include "dataprotocol.h" 00023 00024 #include <config.h> 00025 00026 #include <klocale.h> 00027 #include <kdebug.h> 00028 00029 #include <QtCore/QTimer> 00030 00031 using namespace KIO; 00032 00033 #define KIO_DATA_POLL_INTERVAL 0 00034 00035 // don't forget to sync DISPATCH_DECL in dataslave.h 00036 #define DISPATCH_IMPL(type) \ 00037 void DataSlave::dispatch_##type() { \ 00038 if (_suspended) { \ 00039 QueueStruct q(Queue_##type); \ 00040 dispatchQueue.push_back(q); \ 00041 if (!timer->isActive()) timer->start(KIO_DATA_POLL_INTERVAL); \ 00042 } else \ 00043 type(); \ 00044 } 00045 00046 // don't forget to sync DISPATCH_DECL1 in dataslave.h 00047 #define DISPATCH_IMPL1(type, paramtype, paramname) \ 00048 void DataSlave::dispatch_##type(paramtype paramname) { \ 00049 if (_suspended) { \ 00050 QueueStruct q(Queue_##type); \ 00051 q.paramname = paramname; \ 00052 dispatchQueue.push_back(q); \ 00053 if (!timer->isActive()) timer->start(KIO_DATA_POLL_INTERVAL); \ 00054 } else \ 00055 type(paramname); \ 00056 } 00057 00058 00059 DataSlave::DataSlave() : 00060 Slave("data") 00061 { 00062 //kDebug() << this; 00063 _suspended = false; 00064 timer = new QTimer(this); 00065 connect(timer, SIGNAL(timeout()), SLOT(dispatchNext())); 00066 } 00067 00068 DataSlave::~DataSlave() { 00069 //kDebug() << this; 00070 } 00071 00072 void DataSlave::hold(const KUrl &/*url*/) { 00073 // ignored 00074 } 00075 00076 void DataSlave::suspend() { 00077 _suspended = true; 00078 //kDebug() << this; 00079 timer->stop(); 00080 } 00081 00082 void DataSlave::resume() { 00083 _suspended = false; 00084 //kDebug() << this; 00085 // aarrrgh! This makes the once hyper fast and efficient data protocol 00086 // implementation slow as molasses. But it wouldn't work otherwise, 00087 // and I don't want to start messing around with threads 00088 timer->start(KIO_DATA_POLL_INTERVAL); 00089 } 00090 00091 // finished is a special case. If we emit it right away, then 00092 // TransferJob::start can delete the job even before the end of the method 00093 void DataSlave::dispatch_finished() { 00094 QueueStruct q(Queue_finished); 00095 dispatchQueue.push_back(q); 00096 if (!timer->isActive()) timer->start(KIO_DATA_POLL_INTERVAL); 00097 } 00098 00099 void DataSlave::dispatchNext() { 00100 if (dispatchQueue.empty()) { 00101 timer->stop(); 00102 return; 00103 } 00104 00105 const QueueStruct &q = dispatchQueue.front(); 00106 //kDebug() << this << "dispatching" << q.type << dispatchQueue.size() << "left"; 00107 switch (q.type) { 00108 case Queue_mimeType: mimeType(q.s); break; 00109 case Queue_totalSize: totalSize(q.size); break; 00110 case Queue_sendMetaData: sendMetaData(); break; 00111 case Queue_data: data(q.ba); break; 00112 case Queue_finished: finished(); break; 00113 }/*end switch*/ 00114 00115 dispatchQueue.pop_front(); 00116 } 00117 00118 void DataSlave::send(int cmd, const QByteArray &arr) { 00119 QDataStream stream(arr); 00120 00121 KUrl url; 00122 00123 switch (cmd) { 00124 case CMD_GET: { 00125 stream >> url; 00126 get(url); 00127 break; 00128 } 00129 case CMD_MIMETYPE: { 00130 stream >> url; 00131 mimetype(url); 00132 break; 00133 } 00134 // ignore these (must not emit error, otherwise SIGSEGV occurs) 00135 case CMD_META_DATA: 00136 case CMD_SUBURL: 00137 break; 00138 default: 00139 error(ERR_UNSUPPORTED_ACTION, 00140 unsupportedActionErrorString(QLatin1String("data"),cmd)); 00141 }/*end switch*/ 00142 } 00143 00144 bool DataSlave::suspended() { 00145 return _suspended; 00146 } 00147 00148 void DataSlave::setHost(const QString &/*host*/, quint16 /*port*/, 00149 const QString &/*user*/, const QString &/*passwd*/) { 00150 // irrelevant -> will be ignored 00151 } 00152 00153 void DataSlave::setConfig(const MetaData &/*config*/) { 00154 // FIXME: decide to handle this directly or not at all 00155 #if 0 00156 QByteArray data; 00157 QDataStream stream( data, QIODevice::WriteOnly ); 00158 stream << config; 00159 slaveconn.send( CMD_CONFIG, data ); 00160 #endif 00161 } 00162 00163 void DataSlave::setAllMetaData(const MetaData &md) { 00164 meta_data = md; 00165 } 00166 00167 void DataSlave::sendMetaData() { 00168 emit metaData(meta_data); 00169 } 00170 00171 DISPATCH_IMPL1(mimeType, const QString &, s) 00172 DISPATCH_IMPL1(totalSize, KIO::filesize_t, size) 00173 DISPATCH_IMPL(sendMetaData) 00174 DISPATCH_IMPL1(data, const QByteArray &, ba) 00175 00176 #undef DISPATCH_IMPL 00177 #undef DISPATCH_IMPL1 00178 00179 #include "dataslave.moc"
KDE 4.6 API Reference