objectnotificationmessage.cpp
00001 /* 00002 Copyright (c) 2011 Stephen Kelly <steveire@gmail.com> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "objectnotificationmessage.h" 00021 00022 using namespace Akonadi; 00023 00024 ObjectNotificationMessage::ObjectNotificationMessage(const Akonadi::NotificationMessage &message) 00025 : m_parentCollection(message.parentCollection()), 00026 m_parentDestCollection(message.parentDestCollection()), 00027 m_message(message) 00028 { 00029 if (message.type() == NotificationMessage::Collection) { 00030 Collection col( message.uid() ); 00031 col.setResource( QString::fromUtf8( message.resource() ) ); 00032 col.setRemoteId( message.remoteId() ); 00033 col.setParentCollection( message.operation() == NotificationMessage::Move ? m_parentDestCollection : m_parentCollection ); 00034 00035 if ( message.operation() == NotificationMessage::Remove ) { 00036 const QString remoteRevision = QString::fromUtf8( message.itemParts().toList().first() ); 00037 col.setRemoteRevision( remoteRevision ); 00038 } 00039 m_collections.append( col ); 00040 } else if (message.type() == NotificationMessage::Item) { 00041 Item item( message.uid() ); 00042 item.setRemoteId( message.remoteId() ); 00043 item.setMimeType( message.mimeType() ); 00044 item.setParentCollection( message.operation() == NotificationMessage::Move ? m_parentDestCollection : m_parentCollection ); 00045 00046 // HACK: We have the remoteRevision stored in the itemParts set 00047 // for delete operations to avoid protocol breakage 00048 if ( message.operation() == NotificationMessage::Remove ) { 00049 const QString remoteRevision = QString::fromUtf8( message.itemParts().toList().first() ); 00050 item.setRemoteRevision( remoteRevision ); 00051 } 00052 00053 m_items.append( item ); 00054 } 00055 00056 // HACK: destination resource is delivered in the parts field... 00057 if ( message.operation() == NotificationMessage::Move && !message.itemParts().isEmpty() ) 00058 m_parentDestCollection.setResource( QString::fromLatin1( *(message.itemParts().begin()) ) ); 00059 00060 m_parentCollection.setResource( QString::fromUtf8( message.resource() ) ); 00061 } 00062 00063 Item::List ObjectNotificationMessage::items() const 00064 { 00065 return m_items; 00066 } 00067 00068 Collection::List ObjectNotificationMessage::collections() const 00069 { 00070 return m_collections; 00071 } 00072 00073 NotificationMessage ObjectNotificationMessage::message() const 00074 { 00075 return m_message; 00076 } 00077 00078 NotificationMessage::Operation ObjectNotificationMessage::operation() const 00079 { 00080 return m_message.operation(); 00081 } 00082 00083 NotificationMessage::Type ObjectNotificationMessage::type() const 00084 { 00085 return m_message.type(); 00086 } 00087 00088 QByteArray ObjectNotificationMessage::resource() const 00089 { 00090 return m_message.resource(); 00091 } 00092 00093 QString ObjectNotificationMessage::remoteId() const 00094 { 00095 return m_message.remoteId(); 00096 } 00097 00098 QSet<QByteArray> ObjectNotificationMessage::itemParts() const 00099 { 00100 return m_message.itemParts(); 00101 } 00102 00103 QString ObjectNotificationMessage::mimeType() const 00104 { 00105 return m_message.mimeType(); 00106 } 00107 00108 Collection ObjectNotificationMessage::parentCollection() const 00109 { 00110 return m_parentCollection; 00111 } 00112 00113 Collection ObjectNotificationMessage::parentDestCollection() const 00114 { 00115 return m_parentDestCollection; 00116 } 00117 00118 void ObjectNotificationMessage::appendCollections(const Akonadi::Collection::List& list) 00119 { 00120 m_collections.append(list); 00121 } 00122 00123 void ObjectNotificationMessage::appendItems(const Akonadi::Item::List& list) 00124 { 00125 m_items.append(list); 00126 } 00127 00128 template<typename Container> 00129 bool do_appendAndCompress(Container &container, const Akonadi::ObjectNotificationMessage& message ) 00130 { 00131 if ( container.isEmpty() ) { 00132 container.push_back(message); 00133 return true; 00134 } 00135 const ObjectNotificationMessage lastMessage = container.last().message(); 00136 if (lastMessage.message().type() == message.message().type() 00137 && lastMessage.message().operation() == message.message().operation() 00138 && lastMessage.parentCollection() == message.parentCollection() 00139 && lastMessage.parentDestCollection() == message.parentDestCollection() ) { 00140 container.last().appendCollections(message.collections()); 00141 container.last().appendItems(message.items()); 00142 } else { 00143 container.push_back(message); 00144 return true; 00145 } 00146 return false; 00147 } 00148 00149 bool ObjectNotificationMessage::appendAndCompress(QVector<ObjectNotificationMessage> &vector, const Akonadi::ObjectNotificationMessage& message ) 00150 { 00151 return do_appendAndCompress(vector, message); 00152 } 00153 00154 bool ObjectNotificationMessage::appendAndCompress(QList<ObjectNotificationMessage> &list, const Akonadi::ObjectNotificationMessage& message ) 00155 { 00156 return do_appendAndCompress(list, message); 00157 }