Microblog Library
statusitem.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "statusitem.h"
00021 #include <kdebug.h>
00022
00023 #include <QDateTime>
00024 #include <QDomElement>
00025 #include <QHash>
00026 #include <QStringList>
00027
00028 #include <kpimutils/linklocator.h>
00029
00030 using namespace Microblog;
00031
00032 class StatusItem::Private : public QSharedData
00033 {
00034 public:
00035 Private() {}
00036 Private( const Private& other ) : QSharedData( other ) {
00037 data = other.data;
00038 status = other.status;
00039 dateTime = other.dateTime;
00040 }
00041
00042 public:
00043 void init();
00044 QByteArray data;
00045 QHash<QString,QString> status;
00046 QDateTime dateTime;
00047 };
00048
00049 void StatusItem::Private::init()
00050 {
00051 QDomDocument document;
00052 document.setContent( data );
00053 QDomElement root = document.documentElement();
00054 QDomNode node = root.firstChild();
00055 while ( !node.isNull() ) {
00056 const QString key = node.toElement().tagName();
00057 if ( key == "user" || key == "sender" || key == "recipient" ) {
00058 QDomNode node2 = node.firstChild();
00059 while ( !node2.isNull() ) {
00060 const QString key2 = node2.toElement().tagName();
00061 const QString val2 = node2.toElement().text();
00062 status[ key + "_-_" + key2 ] = val2;
00063 node2 = node2.nextSibling();
00064 }
00065 } else {
00066 const QString value = node.toElement().text();
00067 status[key] = value;
00068 }
00069 node = node.nextSibling();
00070 }
00071
00072
00073 dateTime = QDateTime::fromString( status.value( "created_at" ).toLower().mid( 4 ),
00074 "MMM dd H:mm:ss +0000 yyyy" );
00075 dateTime.setTimeSpec( Qt::UTC );
00076 dateTime = dateTime.toLocalTime();
00077
00078 if ( !dateTime.isValid() )
00079 kDebug() << "Unable to parse" << status.value( "created_at" ).toLower().mid( 4 );
00080
00081 }
00082
00083 StatusItem::StatusItem() : d( new Private )
00084 {
00085 }
00086
00087 StatusItem::StatusItem( const QByteArray &data ) : d( new Private )
00088 {
00089 d->data = data;
00090 d->init();
00091 }
00092
00093 StatusItem::StatusItem( const StatusItem& other ) : d( other.d )
00094 {
00095 }
00096
00097 StatusItem::~StatusItem()
00098 {
00099 }
00100
00101 StatusItem StatusItem::operator=( const StatusItem &other )
00102 {
00103 if ( &other != this )
00104 d = other.d;
00105
00106 return *this;
00107 }
00108
00109 void StatusItem::setData( const QByteArray &data )
00110 {
00111 d->data = data;
00112 d->init();
00113 }
00114
00115
00116 qlonglong StatusItem::id() const
00117 {
00118 return d->status.value( "id" ).toLongLong();
00119 }
00120
00121 QByteArray StatusItem::data() const
00122 {
00123 return d->data;
00124 }
00125
00126 QString StatusItem::value( const QString& value ) const
00127 {
00128 return d->status.value( value );
00129 }
00130
00131 QStringList StatusItem::keys() const
00132 {
00133 return d->status.keys();
00134 }
00135
00136 QString StatusItem::text() const
00137 {
00138 using KPIMUtils::LinkLocator;
00139 int flags = LinkLocator::PreserveSpaces | LinkLocator::HighlightText | LinkLocator::ReplaceSmileys;
00140 return KPIMUtils::LinkLocator::convertToHtml( d->status.value( "text" ), flags );
00141 }
00142
00143 QDateTime StatusItem::date() const
00144 {
00145 return d->dateTime;
00146 }