~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

« back to all changes in this revision

Viewing changes to akonadi/resources/microblog/microblog.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi, Alessandro Ghersi, Harald Sitter
  • Date: 2009-06-27 04:40:05 UTC
  • mfrom: (1.1.39 upstream)
  • Revision ID: james.westby@ubuntu.com-20090627044005-4y2vm9xz7rvmzi4p
Tags: 4:4.2.95svn20090701-0ubuntu1
[ Alessandro Ghersi ]
* New upstream release
  - Bump build-deps
* Remove akonadi-kde and libmaildir4 packages
  - remove akonadi-kde.install and libmaildir4.install
  - remove libmaildir4 from debian/rules
  - remove akonadi-kde and libmaildir4 from depends
  - remove akonadi-kde and libmaildir4 from installgen
* Update kdepim-dev.install
* Update kpilot.install
* Add akonadi-kde and libmaildir4 transitional packages

[ Harald Sitter ]
* KAddressbook replaces Kontact << 4.2.85 (LP: #378373)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "microblog.h"
 
21
#include "configdialog.h"
 
22
#include "communication.h"
 
23
#include "settingsadaptor.h"
 
24
#include "idattribute.h"
 
25
 
 
26
#include <kdebug.h>
 
27
#include <klocale.h>
 
28
#include <kwindowsystem.h>
 
29
 
 
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>
 
36
 
 
37
 
 
38
using namespace Akonadi;
 
39
using namespace Microblog;
 
40
 
 
41
MicroblogResource::MicroblogResource( const QString &id )
 
42
        : ResourceBase( id ), m_comm( 0 )
 
43
{
 
44
    new SettingsAdaptor( Settings::self() );
 
45
    QDBusConnection::sessionBus().registerObject( QLatin1String( "/Settings" ),
 
46
            Settings::self(), QDBusConnection::ExportAdaptors );
 
47
    AttributeFactory::registerAttribute<IdAttribute>();
 
48
    initComm();
 
49
}
 
50
 
 
51
MicroblogResource::~MicroblogResource()
 
52
{
 
53
    delete m_comm;
 
54
}
 
55
 
 
56
void MicroblogResource::initComm()
 
57
{
 
58
    delete m_comm;
 
59
    m_comm = 0;
 
60
 
 
61
    if ( Settings::self()->userName().isEmpty() || Settings::self()->password().isEmpty() )
 
62
        return;
 
63
 
 
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> ) ) );
 
69
 
 
70
    synchronizeCollectionTree();
 
71
}
 
72
 
 
73
void MicroblogResource::retrieveCollections()
 
74
{
 
75
    QHash<QString,Collection> collections;
 
76
 
 
77
    // if there is no connection, don't continue.
 
78
    if ( !m_comm ) {
 
79
        kDebug() << "Tried to retrieve collection, but there is no connection";
 
80
        collectionsRetrieved( collections.values() );
 
81
        return;
 
82
    }
 
83
 
 
84
    Collection root;
 
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 );
 
90
 
 
91
    CachePolicy policy;
 
92
    policy.setInheritFromParent( false );
 
93
    policy.setSyncOnDemand( true );
 
94
    policy.setIntervalCheckTime( -1 );
 
95
    root.setCachePolicy( policy );
 
96
 
 
97
    collections[ "rootfolderunique" ] = root;
 
98
 
 
99
    // for all the folders, inherit it from the parent.
 
100
    policy.setInheritFromParent( true );
 
101
 
 
102
    QStringList folders;
 
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";
 
109
 
 
110
    for ( int i=0; i<5; ++i ) {
 
111
        Collection c;
 
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 );
 
117
 
 
118
        CachePolicy policy;
 
119
        policy.setInheritFromParent( false );
 
120
        policy.setSyncOnDemand( true );
 
121
        policy.setIntervalCheckTime( 5 );
 
122
        c.setCachePolicy( policy );
 
123
 
 
124
        collections[ folders.at( i )] = c;
 
125
    }
 
126
 
 
127
    collectionsRetrieved( collections.values() );
 
128
}
 
129
 
 
130
void MicroblogResource::retrieveItems( const Akonadi::Collection &collection )
 
131
{
 
132
    if ( !m_comm ) {
 
133
        itemsRetrievalDone();
 
134
        return;
 
135
    }
 
136
 
 
137
    m_collection = collection;
 
138
 
 
139
    // get only newer items, except for favorites, which does not allow that.
 
140
    int id = 0;
 
141
    if ( m_collection.hasAttribute( "id" ) && collection.remoteId() != "favorites" ) {
 
142
        IdAttribute* currentid = static_cast<IdAttribute*>( m_collection.attribute( "id" ) );
 
143
        id = currentid->id();
 
144
    }
 
145
    kDebug() << "Getting everything for " << collection.name() << "and id >" << id;
 
146
 
 
147
    m_comm->retrieveFolder( collection.remoteId(), id );
 
148
}
 
149
 
 
150
void MicroblogResource::slotStatusList( const QList<QByteArray> list )
 
151
{
 
152
    kDebug() << list.count() << "received for" << m_collection.name();
 
153
    if ( list.count() == 0 ) {
 
154
        itemsRetrievalDone();
 
155
        return;
 
156
    }
 
157
 
 
158
    IdAttribute* currentid;
 
159
    if ( m_collection.hasAttribute( "id" ) ) {
 
160
        currentid = static_cast<IdAttribute*>( m_collection.attribute( "id" ) );
 
161
    } else {
 
162
        currentid = new IdAttribute( 0 );
 
163
        m_collection.addAttribute( currentid );
 
164
    }
 
165
 
 
166
    Item::List messages;
 
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 );
 
177
    }
 
178
 
 
179
    new Akonadi::CollectionModifyJob( m_collection );
 
180
    itemsRetrievedIncremental( messages, Item::List() );
 
181
}
 
182
 
 
183
bool MicroblogResource::retrieveItem( const Akonadi::Item &item, const QSet<QByteArray>& )
 
184
{
 
185
    itemRetrieved( item );
 
186
    return true;
 
187
}
 
188
 
 
189
void MicroblogResource::configure( WId windowId )
 
190
{
 
191
    ConfigDialog dlg;
 
192
    if ( windowId )
 
193
        KWindowSystem::setMainWindow( &dlg, windowId );
 
194
    dlg.exec();
 
195
    if ( !Settings::self()->name().isEmpty() )
 
196
        setName( i18n( "%1's microblog", Settings::self()->name() ) );
 
197
    initComm();
 
198
}
 
199
 
 
200
AKONADI_RESOURCE_MAIN( MicroblogResource )
 
201
 
 
202
#include "microblog.moc"
 
203
 
 
204