doc update
[kdepim.git] / korganizer / akonadicollectionview.cpp
blobda2216d8c53f1556e5e3379d732aba2b21fd8545
1 /*
2 This file is part of KOrganizer.
4 Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
5 Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 Copyright (C) 2009 Sebastian Sauer <sebsauer@kdab.net>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 As a special exception, permission is given to link this program
23 with any edition of Qt, and distribute the resulting executable,
24 without including the source code for Qt in the source distribution.
27 #include "akonadicollectionview.h"
28 #include "kocore.h"
29 #include "kohelper.h"
30 #include "koprefs.h"
32 #include <KDebug>
33 #include <KDialog>
34 #include <KAction>
35 #include <KActionCollection>
36 #include <kjob.h>
37 #include <QVBoxLayout>
38 #include <QHeaderView>
39 #include <QItemSelectionModel>
41 #include <akonadi/kcal/calendarmodel.h>
42 #include <akonadi/kcal/collectionselectionproxymodel.h>
43 #include <akonadi/kcal/entitymodelstatesaver.h>
44 #include <akonadi/kcal/collectionselection.h>
45 #include <akonadi/kcal/utils.h>
47 #include <akonadi/collection.h>
48 #include <akonadi/collectionview.h>
49 #include <akonadi/collectionfilterproxymodel.h>
50 #include <akonadi/collectiondeletejob.h>
51 #include <akonadi/entitytreemodel.h>
52 #include <akonadi/entitytreeview.h>
53 #include <akonadi/standardactionmanager.h>
54 #include <akonadi/agenttypedialog.h>
55 #include <akonadi/agentinstancewidget.h>
56 #include <akonadi/agentmanager.h>
57 #include <akonadi/agentinstancecreatejob.h>
58 #include <akonadi/agentfilterproxymodel.h>
59 #include <akonadi/control.h>
60 #include <akonadi/session.h>
61 #include <akonadi/changerecorder.h>
63 #include <QHash>
65 using namespace Akonadi;
67 AkonadiCollectionViewFactory::AkonadiCollectionViewFactory( CalendarView *view )
68 : mView( view ), mAkonadiCollectionView( 0 )
72 namespace {
73 class ColorProxyModel : public QSortFilterProxyModel
75 public:
76 explicit ColorProxyModel( QObject* parent=0 ) : QSortFilterProxyModel( parent ) {}
78 /* reimp */ QVariant data( const QModelIndex &index, int role ) const
80 if ( !index.isValid() )
81 return QVariant();
82 if ( role == Qt::DecorationRole ) {
83 const Akonadi::Collection collection = Akonadi::collectionFromIndex( index );
84 if ( !collection.contentMimeTypes().isEmpty() )
85 return KOHelper::resourceColor( collection );
87 return QSortFilterProxyModel::data( index, role );
92 CalendarViewExtension *AkonadiCollectionViewFactory::create( QWidget *parent )
94 mAkonadiCollectionView = new AkonadiCollectionView( view(), parent );
95 QObject::connect( mAkonadiCollectionView, SIGNAL(resourcesChanged(bool)), mView, SLOT(resourcesChanged()) );
96 QObject::connect( mAkonadiCollectionView, SIGNAL(resourcesChanged(bool)), mView, SLOT(updateCategories()) );
97 return mAkonadiCollectionView;
100 CalendarView* AkonadiCollectionViewFactory::view() const
102 return mView;
105 AkonadiCollectionView* AkonadiCollectionViewFactory::collectionView() const
107 return mAkonadiCollectionView;
110 AkonadiCollectionView::AkonadiCollectionView( CalendarView* view, QWidget *parent )
111 : CalendarViewExtension( parent ), mActionManager(0), mCollectionview(0), mBaseModel( 0 ), mSelectionProxyModel( 0 ), mDeleteAction( 0 )
113 QVBoxLayout *topLayout = new QVBoxLayout( this );
114 topLayout->setSpacing( KDialog::spacingHint() );
116 Akonadi::CollectionFilterProxyModel *collectionproxymodel = new Akonadi::CollectionFilterProxyModel( this );
117 collectionproxymodel->setDynamicSortFilter( true );
118 collectionproxymodel->addMimeTypeFilter( QString::fromLatin1( "text/calendar" ) );
120 ColorProxyModel* colorProxy = new ColorProxyModel( this );
121 colorProxy->setDynamicSortFilter( true );
122 colorProxy->setSourceModel( collectionproxymodel );
123 mBaseModel = collectionproxymodel;
125 mCollectionview = new Akonadi::EntityTreeView( this );
126 topLayout->addWidget( mCollectionview );
127 mCollectionview->header()->hide();
128 mCollectionview->setRootIsDecorated( true );
129 mCollectionview->setModel( colorProxy );
131 //mCollectionview->setSelectionMode( QAbstractItemView::NoSelection );
132 KXMLGUIClient *xmlclient = KOCore::self()->xmlguiClient( view );
133 if( xmlclient ) {
134 mCollectionview->setXmlGuiClient( xmlclient );
136 mActionManager = new Akonadi::StandardActionManager( xmlclient->actionCollection(), mCollectionview );
137 mActionManager->createAllActions();
138 mActionManager->action( Akonadi::StandardActionManager::CreateCollection )->setText( i18n( "Add Calendar..." ) );
139 mActionManager->setActionText( Akonadi::StandardActionManager::CopyCollections, ki18np( "Copy Calendar", "Copy %1 Calendars" ) );
140 mActionManager->action( Akonadi::StandardActionManager::DeleteCollections )->setText( i18n( "Delete Calendar" ) );
141 mActionManager->action( Akonadi::StandardActionManager::SynchronizeCollections )->setText( i18n( "Reload" ) );
142 mActionManager->action( Akonadi::StandardActionManager::CollectionProperties )->setText( i18n( "Properties..." ) );
143 mActionManager->setCollectionSelectionModel( mCollectionview->selectionModel() );
145 mCreateAction = new KAction( mCollectionview );
146 mCreateAction->setIcon( KIcon( "appointment-new" ) );
147 mCreateAction->setText( i18n( "New Calendar..." ) );
148 //mCreateAction->setWhatsThis( i18n( "Create a new contact<p>You will be presented with a dialog where you can add all data about a person, including addresses and phone numbers.</p>" ) );
149 xmlclient->actionCollection()->addAction( QString::fromLatin1( "akonadi_calendar_create" ), mCreateAction );
150 connect( mCreateAction, SIGNAL( triggered( bool ) ), this, SLOT( newCalendar() ) );
152 mDeleteAction = new KAction( mCollectionview );
153 mDeleteAction->setIcon( KIcon( "edit-delete" ) );
154 mDeleteAction->setText( i18n( "Delete Calendar" ) );
155 mDeleteAction->setEnabled( false );
156 //mDeleteAction->setWhatsThis( i18n( "Create a new contact<p>You will be presented with a dialog where you can add all data about a person, including addresses and phone numbers.</p>" ) );
157 xmlclient->actionCollection()->addAction( QString::fromLatin1( "akonadi_calendar_delete" ), mDeleteAction );
158 connect( mDeleteAction, SIGNAL( triggered( bool ) ), this, SLOT( deleteCalendar() ) );
162 AkonadiCollectionView::~AkonadiCollectionView()
166 void AkonadiCollectionView::setCollectionSelectionProxyModel( CollectionSelectionProxyModel* m )
168 if ( mSelectionProxyModel == m )
169 return;
170 mSelectionProxyModel = m;
171 if ( !mSelectionProxyModel )
172 return;
173 mBaseModel->setSourceModel( mSelectionProxyModel );
174 connect( mSelectionProxyModel->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), this, SLOT(selectionChanged()) );
177 Akonadi::EntityTreeView* AkonadiCollectionView::view() const
179 return mCollectionview;
182 void AkonadiCollectionView::updateView()
184 emit resourcesChanged( mSelectionProxyModel ? mSelectionProxyModel->selectionModel()->hasSelection() : false );
187 void AkonadiCollectionView::selectionChanged()
189 kDebug();
190 if ( mDeleteAction )
191 mDeleteAction->setEnabled( mCollectionview->selectionModel()->hasSelection() );
192 updateView();
195 void AkonadiCollectionView::newCalendar()
197 kDebug();
198 Akonadi::AgentTypeDialog dlg( this );
199 dlg.setWindowTitle( i18n( "Add Calendar" ) );
200 dlg.agentFilterProxyModel()->addMimeTypeFilter( QString::fromLatin1( "text/calendar" ) );
201 dlg.agentFilterProxyModel()->addCapabilityFilter( "Resource" ); // show only resources, no agents
202 if ( dlg.exec() ) {
203 const Akonadi::AgentType agentType = dlg.agentType();
204 if ( agentType.isValid() ) {
205 Akonadi::AgentInstanceCreateJob *job = new Akonadi::AgentInstanceCreateJob( agentType, this );
206 job->configure( this );
207 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( newCalendarDone( KJob* ) ) );
208 job->start();
213 void AkonadiCollectionView::newCalendarDone( KJob *job )
215 kDebug();
216 Akonadi::AgentInstanceCreateJob *createjob = static_cast<Akonadi::AgentInstanceCreateJob*>( job );
217 if ( createjob->error() ) {
218 //TODO(AKONADI_PORT) this should show an error dialog and should be merged with the identical code in ActionManager
219 kWarning( 5250 ) << "Create calendar failed:" << createjob->errorString();
220 return;
222 //TODO
225 void AkonadiCollectionView::deleteCalendar()
227 kDebug();
229 QModelIndex index = mCollectionview->selectionModel()->currentIndex(); //selectedRows()
230 Q_ASSERT( index.isValid() );
231 const Akonadi::Collection collection = collectionFromIndex( index );
232 Q_ASSERT( collection.isValid() );
233 //Q_ASSERT( mCollectionview->selectionModel()->isSelected(index) );
235 const QString displayname = index.model()->data( index, Qt::DisplayRole ).toString();
236 Q_ASSERT( ! displayname.isEmpty() );
238 if( KMessageBox::questionYesNo( this,
239 i18n( "Do you really want to delete calendar %1?", displayname ),
240 i18n( "Delete Calendar" ),
241 KStandardGuiItem::del(),
242 KStandardGuiItem::cancel(),
243 QString(),
244 KMessageBox::Dangerous )
245 == KMessageBox::Yes )
247 Akonadi::CollectionDeleteJob *job = new Akonadi::CollectionDeleteJob( collection /* , m_session */ );
248 connect( job, SIGNAL( result( KJob* ) ), this, SLOT( deleteCalendarDone( KJob* ) ) );
252 void AkonadiCollectionView::deleteCalendarDone( KJob *job )
254 kDebug();
255 Akonadi::CollectionDeleteJob *createjob = static_cast<Akonadi::CollectionDeleteJob*>( job );
256 if ( createjob->error() ) {
257 kWarning( 5250 ) << "Delete calendar failed:" << createjob->errorString();
258 return;
260 //TODO
263 #include "akonadicollectionview.moc" // for EntityModelStateSaver Q_PRIVATE_SLOT