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

« back to all changes in this revision

Viewing changes to akonadi/kresources/shared/resourceconfigbase.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christian Mangold
  • Date: 2009-07-10 06:34:50 UTC
  • mfrom: (1.1.40 upstream)
  • Revision ID: james.westby@ubuntu.com-20090710063450-neojgew2fh0n3y0u
Tags: 4:4.2.96-0ubuntu1
* New upstream release
* Bump kde build-deps to 4.2.96

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of kdepim.
3
 
    Copyright (c) 2009 Kevin Krammer <kevin.krammer@gmx.at>
4
 
 
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.
9
 
 
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.
14
 
 
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.
19
 
*/
20
 
 
21
 
#include "resourceconfigbase.h"
22
 
 
23
 
#include "sharedresourceiface.h"
24
 
#include "storecollectionmodel.h"
25
 
 
26
 
#include <akonadi/collection.h>
27
 
#include <akonadi/collectionfilterproxymodel.h>
28
 
#include <akonadi/collectionview.h>
29
 
#include <akonadi/standardactionmanager.h>
30
 
 
31
 
#include <kaction.h>
32
 
#include <kactioncollection.h>
33
 
#include <kdebug.h>
34
 
#include <kdialog.h>
35
 
#include <kconfig.h>
36
 
#include <kcmoduleloader.h>
37
 
#include <ktabwidget.h>
38
 
 
39
 
#include <QCheckBox>
40
 
#include <QDialogButtonBox>
41
 
#include <QHeaderView>
42
 
#include <QLabel>
43
 
#include <QLayout>
44
 
#include <QPushButton>
45
 
#include <akonadi/control.h>
46
 
 
47
 
class AkonadiResourceDialog : public KDialog
48
 
{
49
 
  public:
50
 
    AkonadiResourceDialog( const QStringList &mimeList, QWidget *parent )
51
 
      : KDialog( parent )
52
 
    {
53
 
      QWidget *widget = KCModuleLoader::loadModule( "kcm_akonadi_resources",
54
 
                                                    KCModuleLoader::Inline, this, mimeList );
55
 
      setMainWidget( widget );
56
 
 
57
 
      setButtons( Close );
58
 
      setDefaultButton( Close );
59
 
    }
60
 
};
61
 
 
62
 
ResourceConfigBase::ResourceConfigBase( const QStringList &mimeList, QWidget *parent )
63
 
  : KRES::ConfigWidget( parent ),
64
 
    mCollectionView( 0 ),
65
 
    mButtonBox( 0 ),
66
 
    mSyncAction( 0 ),
67
 
    mSyncButton( 0 ),
68
 
    mInfoTextLabel( 0 ),
69
 
    mSourcesDialog( 0 ),
70
 
    mSourcesButton( 0 )
71
 
{
72
 
  Akonadi::Control::start( this );
73
 
  QVBoxLayout *mainLayout = new QVBoxLayout( this );
74
 
  mainLayout->setMargin( KDialog::marginHint() );
75
 
  mainLayout->setSpacing( KDialog::spacingHint() );
76
 
 
77
 
  mCollectionModel = new Akonadi::StoreCollectionModel( this );
78
 
 
79
 
  QWidget *widget = new QWidget( this );
80
 
 
81
 
  QHBoxLayout *collectionLayout = new QHBoxLayout( widget );
82
 
  collectionLayout->setMargin( KDialog::marginHint() );
83
 
  collectionLayout->setSpacing( KDialog::spacingHint() );
84
 
 
85
 
  Akonadi::CollectionFilterProxyModel *filterModel =
86
 
    new Akonadi::CollectionFilterProxyModel( this );
87
 
  filterModel->addMimeTypeFilters( mimeList );
88
 
  filterModel->setSourceModel( mCollectionModel );
89
 
 
90
 
  mCollectionView = new Akonadi::CollectionView( widget );
91
 
  mCollectionView->setSelectionMode( QAbstractItemView::SingleSelection );
92
 
  mCollectionView->setModel( filterModel );
93
 
  mCollectionView->header()->setResizeMode( QHeaderView::ResizeToContents );
94
 
 
95
 
  connect( mCollectionView, SIGNAL( currentChanged( const Akonadi::Collection& ) ),
96
 
           this, SLOT( collectionChanged( const Akonadi::Collection& ) ) );
97
 
 
98
 
  collectionLayout->addWidget( mCollectionView );
99
 
 
100
 
  KActionCollection *actionCollection = new KActionCollection( this );
101
 
 
102
 
  Akonadi::StandardActionManager *actionManager =
103
 
    new Akonadi::StandardActionManager( actionCollection, this );
104
 
  actionManager->setCollectionSelectionModel( mCollectionView->selectionModel() );
105
 
 
106
 
  mSyncAction = actionManager->createAction( Akonadi::StandardActionManager::SynchronizeCollections );
107
 
 
108
 
  mButtonBox = new QDialogButtonBox( Qt::Vertical, widget );
109
 
  collectionLayout->addWidget( mButtonBox );
110
 
 
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 );
116
 
 
117
 
  mSourcesButton = new QPushButton( this );
118
 
  mButtonBox->addButton( mSourcesButton, QDialogButtonBox::ActionRole );
119
 
 
120
 
  connect( mSourcesButton, SIGNAL( clicked() ), mSourcesDialog, SLOT( show() ) );
121
 
 
122
 
  mInfoTextLabel = new QLabel( this );
123
 
  mInfoTextLabel->setWordWrap( true );
124
 
 
125
 
  mainLayout->addWidget( mInfoTextLabel );
126
 
  mainLayout->addWidget( widget );
127
 
 
128
 
  updateCollectionButtonState();
129
 
 
130
 
  connect( actionManager, SIGNAL( actionStateUpdated() ),
131
 
           this, SLOT( updateCollectionButtonState() ) );
132
 
}
133
 
 
134
 
ResourceConfigBase::~ResourceConfigBase()
135
 
{
136
 
}
137
 
 
138
 
void ResourceConfigBase::loadSettings( KRES::Resource *resource )
139
 
{
140
 
  SharedResourceIface* akonadiResource = dynamic_cast<SharedResourceIface*>( resource );
141
 
  if ( akonadiResource == 0 ) {
142
 
    kError( 5650 ) << "Given resource is not an Akonadi bridge";
143
 
    return;
144
 
  }
145
 
 
146
 
  Akonadi::StoreCollectionModel::StoreItemsByCollection storeMapping;
147
 
 
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() ];
153
 
  }
154
 
 
155
 
  mCollectionModel->setStoreMapping( storeMapping );
156
 
}
157
 
 
158
 
void ResourceConfigBase::saveSettings( KRES::Resource *resource )
159
 
{
160
 
  SharedResourceIface* akonadiResource = dynamic_cast<SharedResourceIface*>( resource );
161
 
  if ( akonadiResource == 0 ) {
162
 
    kError( 5650 ) << "Given resource is not an Akonadi bridge";
163
 
    return;
164
 
  }
165
 
 
166
 
  akonadiResource->storeConfig().setStoreCollectionsByMimeType( mStoreCollections );
167
 
}
168
 
 
169
 
void ResourceConfigBase::connectMimeCheckBoxes()
170
 
{
171
 
  foreach ( const QCheckBox *checkBox, mMimeCheckBoxes ) {
172
 
    connect( checkBox, SIGNAL( toggled( bool ) ), SLOT( mimeCheckBoxToggled( bool ) ) );
173
 
  }
174
 
}
175
 
 
176
 
void ResourceConfigBase::updateCollectionButtonState()
177
 
{
178
 
  mSyncButton->setEnabled( mSyncAction->isEnabled() );
179
 
}
180
 
 
181
 
void ResourceConfigBase::collectionChanged( const Akonadi::Collection &collection )
182
 
{
183
 
  mCollection = collection;
184
 
 
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();
190
 
 
191
 
    checkBox->blockSignals( true );
192
 
    checkBox->setChecked( mStoreCollections.value( mimeType, Akonadi::Collection() ) == mCollection );
193
 
    checkBox->blockSignals( false );
194
 
 
195
 
    checkBox->setEnabled( mCollection.isValid() );
196
 
  }
197
 
}
198
 
 
199
 
void ResourceConfigBase::mimeCheckBoxToggled( bool checked )
200
 
{
201
 
  QString mimeType;
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() ) {
207
 
      mimeType = it.key();
208
 
      checkBox = it.value();
209
 
      break;
210
 
    }
211
 
  }
212
 
 
213
 
  Q_ASSERT( !mimeType.isEmpty() && checkBox != 0 );
214
 
 
215
 
  const QString itemType = mItemTypes.value( mimeType, QString() );
216
 
 
217
 
  Akonadi::StoreCollectionModel::StoreItemsByCollection storeMapping = mCollectionModel->storeMapping();
218
 
 
219
 
  if ( checked ) {
220
 
    Akonadi::StoreCollectionModel::StoreItemsByCollection::iterator it =
221
 
      storeMapping.begin();
222
 
    Akonadi::StoreCollectionModel::StoreItemsByCollection::iterator endIt =
223
 
      storeMapping.end();
224
 
    for ( ; it != endIt; ++it ) {
225
 
      it.value().removeAll( itemType );
226
 
    }
227
 
 
228
 
    storeMapping[ mCollection.id() ] << itemType;
229
 
    mStoreCollections[ mimeType ] = mCollection;
230
 
  } else {
231
 
    storeMapping[ mCollection.id() ].removeAll( itemType );
232
 
    mStoreCollections.remove( mimeType );
233
 
  }
234
 
 
235
 
  mCollectionModel->setStoreMapping( storeMapping );
236
 
}
237
 
 
238
 
// kate: space-indent on; indent-width 2; replace-tabs on;