2
This file is part of kdepim.
3
Copyright (c) 2009 Kevin Krammer <kevin.krammer@gmx.at>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
21
#include "resourceconfigbase.h"
23
#include "sharedresourceiface.h"
24
#include "storecollectionmodel.h"
26
#include <akonadi/collection.h>
27
#include <akonadi/collectionfilterproxymodel.h>
28
#include <akonadi/collectionview.h>
29
#include <akonadi/standardactionmanager.h>
32
#include <kactioncollection.h>
36
#include <kcmoduleloader.h>
37
#include <ktabwidget.h>
40
#include <QDialogButtonBox>
41
#include <QHeaderView>
44
#include <QPushButton>
45
#include <akonadi/control.h>
47
class AkonadiResourceDialog : public KDialog
50
AkonadiResourceDialog( const QStringList &mimeList, QWidget *parent )
53
QWidget *widget = KCModuleLoader::loadModule( "kcm_akonadi_resources",
54
KCModuleLoader::Inline, this, mimeList );
55
setMainWidget( widget );
58
setDefaultButton( Close );
62
ResourceConfigBase::ResourceConfigBase( const QStringList &mimeList, QWidget *parent )
63
: KRES::ConfigWidget( parent ),
72
Akonadi::Control::start( this );
73
QVBoxLayout *mainLayout = new QVBoxLayout( this );
74
mainLayout->setMargin( KDialog::marginHint() );
75
mainLayout->setSpacing( KDialog::spacingHint() );
77
mCollectionModel = new Akonadi::StoreCollectionModel( this );
79
QWidget *widget = new QWidget( this );
81
QHBoxLayout *collectionLayout = new QHBoxLayout( widget );
82
collectionLayout->setMargin( KDialog::marginHint() );
83
collectionLayout->setSpacing( KDialog::spacingHint() );
85
Akonadi::CollectionFilterProxyModel *filterModel =
86
new Akonadi::CollectionFilterProxyModel( this );
87
filterModel->addMimeTypeFilters( mimeList );
88
filterModel->setSourceModel( mCollectionModel );
90
mCollectionView = new Akonadi::CollectionView( widget );
91
mCollectionView->setSelectionMode( QAbstractItemView::SingleSelection );
92
mCollectionView->setModel( filterModel );
93
mCollectionView->header()->setResizeMode( QHeaderView::ResizeToContents );
95
connect( mCollectionView, SIGNAL( currentChanged( const Akonadi::Collection& ) ),
96
this, SLOT( collectionChanged( const Akonadi::Collection& ) ) );
98
collectionLayout->addWidget( mCollectionView );
100
KActionCollection *actionCollection = new KActionCollection( this );
102
Akonadi::StandardActionManager *actionManager =
103
new Akonadi::StandardActionManager( actionCollection, this );
104
actionManager->setCollectionSelectionModel( mCollectionView->selectionModel() );
106
mSyncAction = actionManager->createAction( Akonadi::StandardActionManager::SynchronizeCollections );
108
mButtonBox = new QDialogButtonBox( Qt::Vertical, widget );
109
collectionLayout->addWidget( mButtonBox );
111
mSyncButton = new QPushButton( mSyncAction->text() );
112
mSyncButton->setIcon( mSyncAction->icon() );
113
mButtonBox->addButton( mSyncButton, QDialogButtonBox::ActionRole );
114
connect( mSyncButton, SIGNAL( clicked() ), mSyncAction, SLOT( trigger() ) );
115
mSourcesDialog = new AkonadiResourceDialog( mimeList, this );
117
mSourcesButton = new QPushButton( this );
118
mButtonBox->addButton( mSourcesButton, QDialogButtonBox::ActionRole );
120
connect( mSourcesButton, SIGNAL( clicked() ), mSourcesDialog, SLOT( show() ) );
122
mInfoTextLabel = new QLabel( this );
123
mInfoTextLabel->setWordWrap( true );
125
mainLayout->addWidget( mInfoTextLabel );
126
mainLayout->addWidget( widget );
128
updateCollectionButtonState();
130
connect( actionManager, SIGNAL( actionStateUpdated() ),
131
this, SLOT( updateCollectionButtonState() ) );
134
ResourceConfigBase::~ResourceConfigBase()
138
void ResourceConfigBase::loadSettings( KRES::Resource *resource )
140
SharedResourceIface* akonadiResource = dynamic_cast<SharedResourceIface*>( resource );
141
if ( akonadiResource == 0 ) {
142
kError( 5650 ) << "Given resource is not an Akonadi bridge";
146
Akonadi::StoreCollectionModel::StoreItemsByCollection storeMapping;
148
mStoreCollections = akonadiResource->storeConfig().storeCollectionsByMimeType();
149
StoreConfigIface::CollectionsByMimeType::const_iterator it = mStoreCollections.constBegin();
150
StoreConfigIface::CollectionsByMimeType::const_iterator endIt = mStoreCollections.constEnd();
151
for ( ; it != endIt; ++it ) {
152
storeMapping[ it.value().id() ] << mItemTypes[ it.key() ];
155
mCollectionModel->setStoreMapping( storeMapping );
158
void ResourceConfigBase::saveSettings( KRES::Resource *resource )
160
SharedResourceIface* akonadiResource = dynamic_cast<SharedResourceIface*>( resource );
161
if ( akonadiResource == 0 ) {
162
kError( 5650 ) << "Given resource is not an Akonadi bridge";
166
akonadiResource->storeConfig().setStoreCollectionsByMimeType( mStoreCollections );
169
void ResourceConfigBase::connectMimeCheckBoxes()
171
foreach ( const QCheckBox *checkBox, mMimeCheckBoxes ) {
172
connect( checkBox, SIGNAL( toggled( bool ) ), SLOT( mimeCheckBoxToggled( bool ) ) );
176
void ResourceConfigBase::updateCollectionButtonState()
178
mSyncButton->setEnabled( mSyncAction->isEnabled() );
181
void ResourceConfigBase::collectionChanged( const Akonadi::Collection &collection )
183
mCollection = collection;
185
MimeCheckBoxes::const_iterator it = mMimeCheckBoxes.constBegin();
186
MimeCheckBoxes::const_iterator endIt = mMimeCheckBoxes.constEnd();
187
for ( ; it != endIt; ++it ) {
188
const QString mimeType = it.key();
189
QCheckBox *checkBox = it.value();
191
checkBox->blockSignals( true );
192
checkBox->setChecked( mStoreCollections.value( mimeType, Akonadi::Collection() ) == mCollection );
193
checkBox->blockSignals( false );
195
checkBox->setEnabled( mCollection.isValid() );
199
void ResourceConfigBase::mimeCheckBoxToggled( bool checked )
202
QCheckBox *checkBox = 0;
203
MimeCheckBoxes::const_iterator it = mMimeCheckBoxes.constBegin();
204
MimeCheckBoxes::const_iterator endIt = mMimeCheckBoxes.constEnd();
205
for ( ; it != endIt; ++it ) {
206
if ( it.value() == QObject::sender() ) {
208
checkBox = it.value();
213
Q_ASSERT( !mimeType.isEmpty() && checkBox != 0 );
215
const QString itemType = mItemTypes.value( mimeType, QString() );
217
Akonadi::StoreCollectionModel::StoreItemsByCollection storeMapping = mCollectionModel->storeMapping();
220
Akonadi::StoreCollectionModel::StoreItemsByCollection::iterator it =
221
storeMapping.begin();
222
Akonadi::StoreCollectionModel::StoreItemsByCollection::iterator endIt =
224
for ( ; it != endIt; ++it ) {
225
it.value().removeAll( itemType );
228
storeMapping[ mCollection.id() ] << itemType;
229
mStoreCollections[ mimeType ] = mCollection;
231
storeMapping[ mCollection.id() ].removeAll( itemType );
232
mStoreCollections.remove( mimeType );
235
mCollectionModel->setStoreMapping( storeMapping );
238
// kate: space-indent on; indent-width 2; replace-tabs on;