2
Copyright (C) 2009 Omat Holding B.V. <info@omat.nl>
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.
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.
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
20
#include "microblog.h"
21
#include "configdialog.h"
22
#include "communication.h"
23
#include "settingsadaptor.h"
24
#include "idattribute.h"
28
#include <kwindowsystem.h>
30
#include <akonadi/collection.h>
31
#include <akonadi/collectionmodifyjob.h>
32
#include <akonadi/attributefactory.h>
33
#include <akonadi/cachepolicy.h>
34
#include <akonadi/item.h>
35
#include <microblog/statusitem.h>
38
using namespace Akonadi;
39
using namespace Microblog;
41
MicroblogResource::MicroblogResource( const QString &id )
42
: ResourceBase( id ), m_comm( 0 )
44
new SettingsAdaptor( Settings::self() );
45
QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
46
Settings::self(), QDBusConnection::ExportAdaptors );
47
AttributeFactory::registerAttribute<IdAttribute>();
51
MicroblogResource::~MicroblogResource()
56
void MicroblogResource::initComm()
61
if ( Settings::self()->userName().isEmpty() || Settings::self()->password().isEmpty() )
64
m_comm = new Communication( this );
65
m_comm->setService( Settings::self()->service() );
66
m_comm->setCredentials( Settings::self()->userName(), Settings::self()->password() );
67
connect( m_comm, SIGNAL( statusList( const QList<QByteArray> ) ),
68
SLOT( slotStatusList( const QList<QByteArray> ) ) );
70
synchronizeCollectionTree();
73
void MicroblogResource::retrieveCollections()
75
QHash<QString,Collection> collections;
77
// if there is no connection, don't continue.
79
kDebug() << "Tried to retrieve collection, but there is no connection";
80
collectionsRetrieved( collections.values() );
85
root.setName( i18n( "%1's microblog", Settings::self()->name() ) );
86
root.setRemoteId( "microblog" );
87
root.setContentMimeTypes( QStringList( Collection::mimeType() ) );
88
Collection::Rights rights = Collection::ReadOnly;
89
root.setRights( rights );
92
policy.setInheritFromParent( false );
93
policy.setSyncOnDemand( true );
94
policy.setIntervalCheckTime( -1 );
95
root.setCachePolicy( policy );
97
collections[ "rootfolderunique" ] = root;
99
// for all the folders, inherit it from the parent.
100
policy.setInheritFromParent( true );
103
folders << "home" << "replies" << "favorites" << "inbox" << "outbox";
104
QStringList foldersI18n;
105
foldersI18n << i18n( "Home" ) << i18n( "Replies" )
106
<< i18n( "Favorites" ) << i18n( "Inbox" ) << i18n( "Outbox" );
107
QStringList contentTypes;
108
contentTypes << "application/x-vnd.kde.microblog";
110
for ( int i=0; i<5; ++i ) {
112
c.setRemoteId( folders.at( i ) );
113
c.setContentMimeTypes( contentTypes );
114
c.setName( foldersI18n.at( i ) );
115
c.setParentRemoteId( "microblog" );
116
c.setRights( Collection::ReadOnly );
119
policy.setInheritFromParent( false );
120
policy.setSyncOnDemand( true );
121
policy.setIntervalCheckTime( 5 );
122
c.setCachePolicy( policy );
124
collections[ folders.at( i )] = c;
127
collectionsRetrieved( collections.values() );
130
void MicroblogResource::retrieveItems( const Akonadi::Collection &collection )
133
itemsRetrievalDone();
137
m_collection = collection;
139
// get only newer items, except for favorites, which does not allow that.
141
if ( m_collection.hasAttribute( "id" ) && collection.remoteId() != "favorites" ) {
142
IdAttribute* currentid = static_cast<IdAttribute*>( m_collection.attribute( "id" ) );
143
id = currentid->id();
145
kDebug() << "Getting everything for " << collection.name() << "and id >" << id;
147
m_comm->retrieveFolder( collection.remoteId(), id );
150
void MicroblogResource::slotStatusList( const QList<QByteArray> list )
152
kDebug() << list.count() << "received for" << m_collection.name();
153
if ( list.count() == 0 ) {
154
itemsRetrievalDone();
158
IdAttribute* currentid;
159
if ( m_collection.hasAttribute( "id" ) ) {
160
currentid = static_cast<IdAttribute*>( m_collection.attribute( "id" ) );
162
currentid = new IdAttribute( 0 );
163
m_collection.addAttribute( currentid );
167
foreach( const QByteArray& status, list ) {
168
Akonadi::Item item( -1 );
169
StatusItem stat( status );
170
item.setRemoteId( QString::number( stat.id() ) );
171
if ( stat.id() > currentid->id() )
172
currentid->setId( stat.id() );
173
item.setMimeType( "application/x-vnd.kde.microblog" );
174
item.setPayload<StatusItem>( stat );
175
item.setSize( status.length() );
176
messages.append( item );
179
new Akonadi::CollectionModifyJob( m_collection );
180
itemsRetrievedIncremental( messages, Item::List() );
183
bool MicroblogResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray>& )
185
itemRetrieved( item );
189
void MicroblogResource::configure( WId windowId )
193
KWindowSystem::setMainWindow( &dlg, windowId );
195
if ( !Settings::self()->name().isEmpty() )
196
setName( i18n( "%1's microblog", Settings::self()->name() ) );
200
AKONADI_RESOURCE_MAIN( MicroblogResource )
202
#include "microblog.moc"