• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

akonadi/kcm

resourcesmanagementwidget.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
00003     Copyright (c) 2008 Omat Holding B.V. <tomalbers@kde.nl>
00004 
00005     Based on KMail code by:
00006     Copyright (C) 2001-2003 Marc Mutz <mutz@kde.org>
00007 
00008     This library is free software; you can redistribute it and/or modify it
00009     under the terms of the GNU Library General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or (at your
00011     option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful, but WITHOUT
00014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00016     License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to the
00020     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00021     02110-1301, USA.
00022 */
00023 
00024 #include "resourcesmanagementwidget.h"
00025 #include "ui_resourcesmanagementwidget.h"
00026 
00027 #include <akonadi/agentmanager.h>
00028 #include <akonadi/agentinstancecreatejob.h>
00029 #include <akonadi/agentfilterproxymodel.h>
00030 #include <akonadi/control.h>
00031 
00032 #include <KDebug>
00033 #include <KMenu>
00034 #include <KMessageBox>
00035 #include <KMimeType>
00036 
00037 static bool isWantedResource( const QStringList &wantedMimeTypes, const QStringList &resourceMimeTypes )
00038 {
00039   foreach ( const QString &resourceMimeType, resourceMimeTypes ) {
00040     KMimeType::Ptr mimeType = KMimeType::mimeType( resourceMimeType, KMimeType::ResolveAliases );
00041     if ( mimeType.isNull() )
00042       continue;
00043 
00044     // check if the resource MIME type is or inherits from the
00045     // wanted one, e.g.
00046     // if the resource offers message/news and wanted types includes
00047     // message/rfc822 this will be true, because message/news is a
00048     // "subclass" of message/rfc822
00049     foreach ( const QString &wantedMimeType, wantedMimeTypes ) {
00050       if ( mimeType->is( wantedMimeType ) )
00051         return true;
00052     }
00053   }
00054 
00055   return false;
00056 }
00057 
00058 class ResourcesManagementWidget::Private
00059 {
00060 public:
00061     Ui::ResourcesManagementWidget   ui;
00062     QHash<QAction*, Akonadi::AgentType>        menuOptions;
00063     QStringList                     wantedMimeTypes;
00064 };
00065 
00066 ResourcesManagementWidget::ResourcesManagementWidget( QWidget *parent,  const QStringList &args ) :
00067         QWidget( parent ),
00068         d( new Private )
00069 {
00070     d->wantedMimeTypes = args;
00071     d->ui.setupUi( this );
00072 
00073     KMenu *addMenu = new KMenu( this );
00074     d->ui.addButton->setMenu( addMenu );
00075     updateMenu();
00076     connect( addMenu, SIGNAL( triggered( QAction* ) ), SLOT( addClicked( QAction* ) ) );
00077 
00078     d->ui.resourcesList->agentFilterProxyModel()->addCapabilityFilter( "Resource" );
00079     foreach( const QString& type, d->wantedMimeTypes )
00080         d->ui.resourcesList->agentFilterProxyModel()->addMimeTypeFilter( type );
00081     connect( d->ui.resourcesList, SIGNAL( currentChanged( const Akonadi::AgentInstance&, const Akonadi::AgentInstance& ) ),
00082              SLOT( updateButtonState( const Akonadi::AgentInstance& ) ) );
00083     connect( d->ui.resourcesList, SIGNAL( doubleClicked( const Akonadi::AgentInstance& ) ),
00084              SLOT( editClicked() ) );
00085 
00086     connect( d->ui.editButton, SIGNAL( clicked() ), SLOT( editClicked() ) );
00087     connect( d->ui.removeButton, SIGNAL( clicked() ), SLOT( removeClicked() ) );
00088 
00089     connect( Akonadi::AgentManager::self(), SIGNAL(typeAdded(Akonadi::AgentType)), SLOT(updateMenu()) );
00090     connect( Akonadi::AgentManager::self(), SIGNAL(typeRemoved(Akonadi::AgentType)), SLOT(updateMenu()) );
00091 
00092     updateButtonState( d->ui.resourcesList->currentAgentInstance() );
00093 
00094     Akonadi::Control::widgetNeedsAkonadi( this );
00095 }
00096 
00097 ResourcesManagementWidget::~ResourcesManagementWidget()
00098 {
00099     delete d;
00100 }
00101 
00102 void ResourcesManagementWidget::updateButtonState( const Akonadi::AgentInstance& current)
00103 {
00104     if ( !current.isValid() ) {
00105         d->ui.editButton->setEnabled( false );
00106         d->ui.removeButton->setEnabled( false );
00107     } else {
00108         d->ui.editButton->setEnabled( true );
00109         d->ui.removeButton->setEnabled( true );
00110     }
00111 }
00112 
00113 void ResourcesManagementWidget::addClicked( QAction *action )
00114 {
00115     Q_ASSERT( d->menuOptions.contains( action ) );
00116     Akonadi::AgentInstanceCreateJob *job = new Akonadi::AgentInstanceCreateJob( d->menuOptions.value( action ) , this );
00117     job->configure( this );
00118     job->start();
00119 }
00120 
00121 void ResourcesManagementWidget::editClicked()
00122 {
00123     Akonadi::AgentInstance instance = d->ui.resourcesList->currentAgentInstance();
00124     if ( instance.isValid() )
00125         instance.configure( this );
00126 }
00127 
00128 void ResourcesManagementWidget::removeClicked()
00129 {
00130     const Akonadi::AgentInstance instance = d->ui.resourcesList->currentAgentInstance();
00131     if ( instance.isValid() )
00132         Akonadi::AgentManager::self()->removeInstance( instance );
00133 
00134     updateButtonState( d->ui.resourcesList->currentAgentInstance() );
00135 }
00136 
00137 void ResourcesManagementWidget::updateMenu()
00138 {
00139     QMenu *addMenu = d->ui.addButton->menu();
00140     addMenu->clear();
00141     d->menuOptions.clear();
00142     Akonadi::AgentType::List agentTypes = Akonadi::AgentManager::self()->types();
00143     foreach( const Akonadi::AgentType &agentType, agentTypes ) {
00144 
00145         if ( !agentType.capabilities().contains( "Resource" ) )
00146             continue;
00147 
00148         const QStringList mimeTypes = agentType.mimeTypes();
00149         bool wanted = isWantedResource( d->wantedMimeTypes, mimeTypes );
00150 
00151         if ( !wanted && !d->wantedMimeTypes.isEmpty() )
00152             continue;
00153 
00154         QAction *action = addMenu->addAction( agentType.name() );
00155         action->setIcon( agentType.icon() );
00156         d->menuOptions.insert( action, agentType );
00157     }
00158 }
00159 
00160 #include "resourcesmanagementwidget.moc"

akonadi/kcm

Skip menu "akonadi/kcm"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal