1/*
2 Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
3 Copyright (c) 2008 Omat Holding B.V. <tomalbers@kde.nl>
4
5 Based on KMail code by:
6 Copyright (C) 2001-2003 Marc Mutz <mutz@kde.org>
7
8 This library is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 This library is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16 License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
22*/
23
24#include "resourcesmanagementwidget.h"
25#include "ui_resourcesmanagementwidget.h"
26
27#include <akonadi/agentmanager.h>
28#include <akonadi/agentinstancecreatejob.h>
29#include <akonadi/agentfilterproxymodel.h>
30#include <akonadi/agenttypedialog.h>
31#include <akonadi/control.h>
32
33#include <kwindowsystem.h>
34
35#include <KDebug>
36#include <KMessageBox>
37#include <KLineEdit>
38
39class ResourcesManagementWidget::Private
40{
41public:
42 Ui::ResourcesManagementWidget ui;
43 QHash<QAction*, Akonadi::AgentType> menuOptions;
44 QStringList wantedMimeTypes;
45};
46
47ResourcesManagementWidget::ResourcesManagementWidget( QWidget *parent, const QStringList &args ) :
48 QWidget( parent ),
49 d( new Private )
50{
51 d->wantedMimeTypes = args;
52 d->ui.setupUi( this );
53
54 d->ui.resourcesList->agentFilterProxyModel()->addCapabilityFilter( QLatin1String("Resource") );
55 foreach ( const QString& type, d->wantedMimeTypes )
56 d->ui.resourcesList->agentFilterProxyModel()->addMimeTypeFilter( type );
57 connect( d->ui.resourcesList->view()->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
58 SLOT(updateButtonState()) );
59 connect( d->ui.resourcesList, SIGNAL(doubleClicked(Akonadi::AgentInstance)),
60 SLOT(editClicked()) );
61
62 connect( d->ui.addButton, SIGNAL(clicked()), SLOT(addClicked()) );
63 connect( d->ui.editButton, SIGNAL(clicked()), SLOT(editClicked()) );
64 connect( d->ui.removeButton, SIGNAL(clicked()), SLOT(removeClicked()) );
65
66 d->ui.mFilterAccount->setProxy( d->ui.resourcesList->agentFilterProxyModel() );
67 d->ui.mFilterAccount->lineEdit()->setTrapReturnKey( true );
68
69 updateButtonState();
70 Akonadi::Control::widgetNeedsAkonadi( this );
71}
72
73ResourcesManagementWidget::~ResourcesManagementWidget()
74{
75 delete d;
76}
77
78void ResourcesManagementWidget::updateButtonState()
79{
80 const QList<Akonadi::AgentInstance> instanceList = d->ui.resourcesList->selectedAgentInstances();
81 if ( instanceList.isEmpty() ) {
82 d->ui.editButton->setEnabled( false );
83 d->ui.removeButton->setEnabled( false );
84 } else {
85 const Akonadi::AgentInstance current = instanceList.first();
86 d->ui.editButton->setEnabled( !current.type().capabilities().contains( QLatin1String( "NoConfig" ) ) );
87 d->ui.removeButton->setEnabled( true );
88 }
89}
90
91void ResourcesManagementWidget::addClicked()
92{
93 Akonadi::AgentTypeDialog dlg( this );
94 Akonadi::AgentFilterProxyModel* filter = dlg.agentFilterProxyModel();
95 foreach ( const QString& type, d->wantedMimeTypes )
96 filter->addMimeTypeFilter( type );
97
98 if ( dlg.exec() ) {
99 const Akonadi::AgentType agentType = dlg.agentType();
100 if ( agentType.isValid() ) {
101 Akonadi::AgentInstanceCreateJob *job = new Akonadi::AgentInstanceCreateJob( agentType, this );
102 job->configure( this );
103 job->start();
104 }
105 }
106}
107
108void ResourcesManagementWidget::editClicked()
109{
110 const QList<Akonadi::AgentInstance> instanceList = d->ui.resourcesList->selectedAgentInstances();
111 if ( !instanceList.isEmpty() && instanceList.first().isValid() ) {
112 KWindowSystem::allowExternalProcessWindowActivation();
113 Akonadi::AgentInstance instance = instanceList.first();
114 instance.configure( this );
115 }
116}
117
118void ResourcesManagementWidget::removeClicked()
119{
120 const QList<Akonadi::AgentInstance> instanceList = d->ui.resourcesList->selectedAgentInstances();
121 if ( !instanceList.isEmpty() ) {
122 if ( KMessageBox::questionYesNo( this,
123 i18np( "Do you really want to delete the selected agent instance?",
124 "Do you really want to delete these %1 agent instances?",
125 instanceList.size() ),
126 i18n( "Multiple Agent Deletion" ),
127 KStandardGuiItem::del(),
128 KStandardGuiItem::cancel(),
129 QString(),
130 KMessageBox::Dangerous )
131 == KMessageBox::Yes ) {
132 foreach ( const Akonadi::AgentInstance &agent, instanceList )
133 Akonadi::AgentManager::self()->removeInstance( agent );
134 updateButtonState();
135 }
136 }
137}
138
139