00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "dbusoperationqueuehandler.h"
00025
00026 #include <QMetaMethod>
00027 #include <QDebug>
00028 #include <QMetaType>
00029
00030 #include "libsignoncommon.h"
00031 #include "identityinfo.h"
00032
00033
00034
00035
00036 namespace SignOn {
00037
00038
00039
00040 DBusOperationQueueHandler::Operation::Operation(const char *name,
00041 QList<QGenericArgument *> args)
00042 {
00043 copy(name, args);
00044 qDeleteAll(args);
00045 }
00046
00047 void DBusOperationQueueHandler::Operation::copy(const char *name,
00048 const QList<QGenericArgument *> &args)
00049 {
00050 Q_ASSERT(name != NULL);
00051
00052 m_name = new char[qstrlen(name) + 1];
00053 qstrcpy(m_name, name);
00054
00055 QListIterator<QGenericArgument *> it(args);
00056 while (it.hasNext()) {
00057 QGenericArgument *arg = it.next();
00058 int type = QMetaType::type(arg->name());
00059 if (!QMetaType::isRegistered(type)) {
00060 qCritical()
00061 << Q_FUNC_INFO
00062 << QString(QLatin1String("Type %1 not registered."))
00063 .arg(QLatin1String(arg->name()));
00064 } else {
00065 Q_ASSERT(arg->name() != NULL);
00066
00067 char *localName = new char[qstrlen(arg->name()) + 1];
00068 qstrcpy(localName, arg->name());
00069 void *localData = QMetaType::construct(type, arg->data());
00070
00071 m_args << (new QGenericArgument(localName, localData));
00072 }
00073 }
00074 }
00075
00076 DBusOperationQueueHandler::Operation::~Operation()
00077 {
00078 if (m_name)
00079 delete [] m_name;
00080
00081 foreach (QGenericArgument *arg, m_args) {
00082 QMetaType::destroy(QMetaType::type(arg->name()), arg->data());
00083 if (arg->name())
00084 delete [] arg->name();
00085 delete arg;
00086 }
00087 }
00088
00089
00090
00091 DBusOperationQueueHandler::DBusOperationQueueHandler(QObject *clientObject)
00092 : m_clientObject(clientObject),
00093 m_maxNumberOfOperationParameters(6),
00094 m_operationsStopped(false)
00095 {
00096 }
00097
00098 DBusOperationQueueHandler::~DBusOperationQueueHandler()
00099 {
00100 }
00101
00102 void DBusOperationQueueHandler::enqueueOperation(Operation *operation)
00103 {
00104 m_operationsQueue.enqueue(operation);
00105 }
00106
00107 void DBusOperationQueueHandler::enqueueOperation(const char *name,
00108 QList<QGenericArgument *> args)
00109 {
00110 m_operationsQueue.enqueue(new Operation(name, args));
00111 }
00112
00113 void DBusOperationQueueHandler::execQueuedOperations()
00114 {
00115 m_operationsStopped = false;
00116
00117 while (m_operationsStopped == false && !m_operationsQueue.empty()) {
00118 Operation *op = m_operationsQueue.dequeue();
00119
00120 if (op->m_args.size() > m_maxNumberOfOperationParameters) {
00121 qWarning() << "DBusOperationQueueHandler::execQueuedOperations(): "
00122 "Maximum number of operation parameters exceeded(6).";
00123 continue;
00124 }
00125
00126 int indexOfMethod = m_clientObject->metaObject()->indexOfMethod(
00127 QMetaObject::normalizedSignature(op->m_name));
00128
00129 QMetaMethod method = m_clientObject->metaObject()->method(indexOfMethod);
00130
00131 TRACE() << "Executing cached oparation: SIGNATURE:" << method.signature();
00132
00133 switch (op->m_args.count()) {
00134 case 0: TRACE(); method.invoke(m_clientObject); break;
00135 case 1: TRACE(); method.invoke(
00136 m_clientObject,
00137 *(op->m_args.at(0))); break;
00138 case 2: TRACE(); method.invoke(
00139 m_clientObject,
00140 *(op->m_args.at(0)),
00141 *(op->m_args.at(1))); break;
00142 case 3: TRACE(); method.invoke(
00143 m_clientObject,
00144 *(op->m_args.at(0)),
00145 *(op->m_args.at(1)),
00146 *(op->m_args.at(2))); break;
00147 case 4: TRACE(); method.invoke(
00148 m_clientObject,
00149 *(op->m_args.at(0)),
00150 *(op->m_args.at(1)),
00151 *(op->m_args.at(2)),
00152 *(op->m_args.at(3))); break;
00153 case 5: TRACE(); method.invoke(
00154 m_clientObject,
00155 *(op->m_args.at(0)),
00156 *(op->m_args.at(1)),
00157 *(op->m_args.at(2)),
00158 *(op->m_args.at(3)),
00159 *(op->m_args.at(4))); break;
00160 case 6: TRACE(); method.invoke(
00161 m_clientObject,
00162 *(op->m_args.at(0)),
00163 *(op->m_args.at(1)),
00164 *(op->m_args.at(2)),
00165 *(op->m_args.at(3)),
00166 *(op->m_args.at(4)),
00167 *(op->m_args.at(5))); break;
00168 default: TRACE(); method.invoke(m_clientObject); break;
00169 }
00170 delete op;
00171 }
00172 }
00173
00174 void DBusOperationQueueHandler::removeOperation(const char *name, bool removeAll)
00175 {
00176 foreach (Operation *operation, m_operationsQueue) {
00177 if (operation != NULL && qstrcmp(operation->m_name, name) == 0) {
00178 m_operationsQueue.removeOne(operation);
00179 if (!removeAll)
00180 break;
00181 }
00182 }
00183 }
00184
00185 bool DBusOperationQueueHandler::queueContainsOperation(const char *name)
00186 {
00187 foreach (Operation *operation, m_operationsQueue)
00188 if (operation != NULL && qstrcmp(operation->m_name, name) == 0)
00189 return true;
00190
00191 return false;
00192 }
00193 }
00194
00195
00196