kopete/protocols/messenger/libpapillon
networkmessage.cpp
Go to the documentation of this file.00001 /* 00002 networkmessage.cpp - Represent a network message between the Messenger server. 00003 00004 Copyright (c) 2006-2007 by Michaƫl Larouche <larouche@kde.org> 00005 00006 ************************************************************************* 00007 * * 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2 of the License, or (at your option) any later version. * 00012 * * 00013 ************************************************************************* 00014 */ 00015 #include "Papillon/NetworkMessage" 00016 00017 // Qt includes 00018 #include <QtCore/QStringList> 00019 #include <QtCore/QByteArray> 00020 00021 namespace Papillon { 00022 00023 class NetworkMessage::Private 00024 { 00025 public: 00026 NetworkMessage::NetworkMessageType type; 00027 QString command; 00028 QString transactionId; 00029 QStringList arguments; 00030 QByteArray payloadData; 00031 }; 00032 00033 NetworkMessage::NetworkMessage(const NetworkMessageType &type) 00034 : d(new Private) 00035 { 00036 d->type = type; 00037 } 00038 00039 NetworkMessage::~NetworkMessage() 00040 { 00041 delete d; 00042 } 00043 00044 NetworkMessage::NetworkMessageType NetworkMessage::type() const 00045 { 00046 return d->type; 00047 } 00048 00049 QString NetworkMessage::command() const 00050 { 00051 return d->command; 00052 } 00053 00054 void NetworkMessage::setCommand(const QString &command) 00055 { 00056 d->command = command; 00057 } 00058 00059 QString NetworkMessage::transactionId() const 00060 { 00061 Q_ASSERT(type() & NetworkMessage::TransactionMessage); 00062 00063 return d->transactionId; 00064 } 00065 00066 void NetworkMessage::setTransactionId(const QString &transactionId) 00067 { 00068 Q_ASSERT(type() & NetworkMessage::TransactionMessage); 00069 00070 d->transactionId = transactionId; 00071 } 00072 00073 QStringList NetworkMessage::arguments() const 00074 { 00075 return d->arguments; 00076 } 00077 00078 void NetworkMessage::setArguments(const QStringList &arguments) 00079 { 00080 d->arguments = arguments; 00081 } 00082 00083 void NetworkMessage::setArguments(const QString &argumentString) 00084 { 00085 d->arguments = argumentString.split(" "); 00086 } 00087 00088 int NetworkMessage::payloadLength() const 00089 { 00090 Q_ASSERT(type() & NetworkMessage::PayloadMessage); 00091 00092 return d->payloadData.size(); 00093 } 00094 00095 QByteArray NetworkMessage::payloadData() const 00096 { 00097 Q_ASSERT(type() & NetworkMessage::PayloadMessage); 00098 00099 return d->payloadData; 00100 } 00101 00102 void NetworkMessage::setPayloadData(const QByteArray &data) 00103 { 00104 Q_ASSERT(type() & NetworkMessage::PayloadMessage); 00105 00106 d->payloadData = data; 00107 } 00108 00109 QString NetworkMessage::toString() const 00110 { 00111 QString result; 00112 00113 result += command(); 00114 00115 if( type() & NetworkMessage::TransactionMessage && !transactionId().isEmpty() ) 00116 { 00117 result += ' ' + transactionId(); 00118 } 00119 00120 if( !d->arguments.empty() ) 00121 { 00122 result += ' ' + arguments().join(" "); 00123 } 00124 00125 if( type() & NetworkMessage::PayloadMessage ) 00126 { 00127 result += ' ' + QString::number(payloadLength()); 00128 } 00129 00130 result += "\r\n"; 00131 00132 return result; 00133 } 00134 00135 QByteArray NetworkMessage::toRawCommand() const 00136 { 00137 QByteArray result; 00138 00139 result += toString().toUtf8(); 00140 00141 if( !d->payloadData.isEmpty() ) 00142 result += d->payloadData; 00143 00144 return result; 00145 } 00146 00147 }
KDE 4.2 API Reference