1 | /* |
2 | Copyright (C) 2009 Omat Holding B.V. <info@omat.nl> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #include "statusitem.h" |
21 | #include <kdebug.h> |
22 | |
23 | #include <QDateTime> |
24 | #include <QDomElement> |
25 | #include <QHash> |
26 | #include <QStringList> |
27 | |
28 | #include <kpimutils/linklocator.h> |
29 | |
30 | using namespace Microblog; |
31 | |
32 | class StatusItem::Private : public QSharedData |
33 | { |
34 | public: |
35 | Private() {} |
36 | Private( const Private& other ) : QSharedData( other ) { |
37 | data = other.data; |
38 | status = other.status; |
39 | dateTime = other.dateTime; |
40 | } |
41 | |
42 | public: |
43 | void init(); |
44 | QByteArray data; |
45 | QHash<QString, QString> status; |
46 | QDateTime dateTime; |
47 | }; |
48 | |
49 | void StatusItem::Private::init() |
50 | { |
51 | QDomDocument document; |
52 | document.setContent( data ); |
53 | QDomElement root = document.documentElement(); |
54 | QDomNode node = root.firstChild(); |
55 | while ( !node.isNull() ) { |
56 | const QString key = node.toElement().tagName(); |
57 | if ( key == QLatin1String("user" ) || key == QLatin1String("sender" ) || key == QLatin1String("recipient" ) ) { |
58 | QDomNode node2 = node.firstChild(); |
59 | while ( !node2.isNull() ) { |
60 | const QString key2 = node2.toElement().tagName(); |
61 | const QString val2 = node2.toElement().text(); |
62 | status[ key + QLatin1String("_-_" ) + key2 ] = val2; |
63 | node2 = node2.nextSibling(); |
64 | } |
65 | } else { |
66 | const QString value = node.toElement().text(); |
67 | status[key] = value; |
68 | } |
69 | node = node.nextSibling(); |
70 | } |
71 | //kDebug() << status; |
72 | |
73 | dateTime = QDateTime::fromString( status.value( QLatin1String("created_at" ) ).toLower().mid( 4 ), |
74 | QLatin1String("MMM dd H:mm:ss +0000 yyyy" ) ); |
75 | dateTime.setTimeSpec( Qt::UTC ); |
76 | dateTime = dateTime.toLocalTime(); |
77 | |
78 | if ( !dateTime.isValid() ) { |
79 | kDebug() << "Unable to parse" << status.value( QLatin1String("created_at" ) ).toLower().mid( 4 ); |
80 | } |
81 | //kDebug() << dateTime; |
82 | } |
83 | |
84 | StatusItem::StatusItem() : d( new Private ) |
85 | { |
86 | } |
87 | |
88 | StatusItem::StatusItem( const QByteArray &data ) : d( new Private ) |
89 | { |
90 | d->data = data; |
91 | d->init(); |
92 | } |
93 | |
94 | StatusItem::StatusItem( const StatusItem& other ) : d( other.d ) |
95 | { |
96 | } |
97 | |
98 | StatusItem::~StatusItem() |
99 | { |
100 | } |
101 | |
102 | StatusItem StatusItem::operator=( const StatusItem &other ) |
103 | { |
104 | if ( &other != this ) { |
105 | d = other.d; |
106 | } |
107 | |
108 | return *this; |
109 | } |
110 | |
111 | void StatusItem::setData( const QByteArray &data ) |
112 | { |
113 | d->data = data; |
114 | d->init(); |
115 | } |
116 | |
117 | |
118 | qlonglong StatusItem::id() const |
119 | { |
120 | return d->status.value( QLatin1String("id" ) ).toLongLong(); |
121 | } |
122 | |
123 | QByteArray StatusItem::data() const |
124 | { |
125 | return d->data; |
126 | } |
127 | |
128 | QString StatusItem::value( const QString& value ) const |
129 | { |
130 | return d->status.value( value ); |
131 | } |
132 | |
133 | QStringList StatusItem::keys() const |
134 | { |
135 | return d->status.keys(); |
136 | } |
137 | |
138 | QString StatusItem::text() const |
139 | { |
140 | using KPIMUtils::LinkLocator; |
141 | int flags = LinkLocator::PreserveSpaces | LinkLocator::HighlightText | LinkLocator::ReplaceSmileys; |
142 | return KPIMUtils::LinkLocator::convertToHtml( d->status.value( QLatin1String("text" ) ), flags ); |
143 | } |
144 | |
145 | QDateTime StatusItem::date() const |
146 | { |
147 | return d->dateTime; |
148 | } |
149 | |