akonadi
agentactionmanager.cpp
00001 /* 00002 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org> 00003 00004 This library is free software; you can redistribute it and/or modify it 00005 under the terms of the GNU Library General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or (at your 00007 option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, but WITHOUT 00010 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00011 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 00012 License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to the 00016 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00017 02110-1301, USA. 00018 */ 00019 00020 #include "agentactionmanager.h" 00021 00022 #include "agentfilterproxymodel.h" 00023 #include "agentinstancecreatejob.h" 00024 #include "agentinstancemodel.h" 00025 #include "agentmanager.h" 00026 #include "agenttypedialog.h" 00027 #include "metatypes.h" 00028 00029 #include <KAction> 00030 #include <KActionCollection> 00031 #include <KDebug> 00032 #include <KInputDialog> 00033 #include <KLocale> 00034 #include <KMessageBox> 00035 00036 #include <QtGui/QItemSelectionModel> 00037 00038 #include <boost/static_assert.hpp> 00039 00040 using namespace Akonadi; 00041 00042 //@cond PRIVATE 00043 00044 static const struct { 00045 const char *name; 00046 const char *label; 00047 const char *icon; 00048 int shortcut; 00049 const char *slot; 00050 } agentActionData[] = { 00051 { "akonadi_agentinstance_create", I18N_NOOP( "&New Agent Instance..." ), 00052 "folder-new", 0, SLOT(slotCreateAgentInstance()) 00053 }, 00054 { "akonadi_agentinstance_delete", I18N_NOOP( "&Delete Agent Instance" ), 00055 "edit-delete", 0, SLOT(slotDeleteAgentInstance()) 00056 }, 00057 { "akonadi_agentinstance_configure", I18N_NOOP( "&Configure Agent Instance" ), 00058 "configure", 0, SLOT(slotConfigureAgentInstance()) 00059 } 00060 }; 00061 static const int numAgentActionData = sizeof agentActionData / sizeof *agentActionData; 00062 00063 BOOST_STATIC_ASSERT( numAgentActionData == AgentActionManager::LastType ); 00064 00068 class AgentActionManager::Private 00069 { 00070 public: 00071 Private( AgentActionManager *parent ) : 00072 q( parent ), 00073 mSelectionModel( 0 ) 00074 { 00075 mActions.fill( 0, AgentActionManager::LastType ); 00076 00077 setContextText( AgentActionManager::CreateAgentInstance, 00078 AgentActionManager::DialogTitle, 00079 i18nc( "@title:window", "New Agent Instance" ) ); 00080 00081 setContextText( AgentActionManager::CreateAgentInstance, 00082 AgentActionManager::ErrorMessageText, 00083 ki18n( "Could not create agent instance: %1" ) ); 00084 00085 setContextText( AgentActionManager::CreateAgentInstance, 00086 AgentActionManager::ErrorMessageTitle, 00087 i18n( "Agent instance creation failed" ) ); 00088 00089 setContextText( AgentActionManager::DeleteAgentInstance, 00090 AgentActionManager::MessageBoxTitle, 00091 i18nc( "@title:window", "Delete Agent Instance?" ) ); 00092 00093 setContextText( AgentActionManager::DeleteAgentInstance, 00094 AgentActionManager::MessageBoxText, 00095 i18n( "Do you really want to delete the selected agent instance?" ) ); 00096 } 00097 00098 void enableAction( AgentActionManager::Type type, bool enable ) 00099 { 00100 Q_ASSERT( type >= 0 && type < AgentActionManager::LastType ); 00101 if ( mActions[ type ] ) { 00102 mActions[ type ]->setEnabled( enable ); 00103 } 00104 } 00105 00106 void updateActions() 00107 { 00108 const AgentInstance::List instances = selectedAgentInstances(); 00109 00110 const bool createActionEnabled = true; 00111 bool deleteActionEnabled = true; 00112 bool configureActionEnabled = true; 00113 00114 if ( instances.isEmpty() ) { 00115 deleteActionEnabled = false; 00116 configureActionEnabled = false; 00117 } 00118 00119 if ( instances.count() == 1 ) { 00120 const AgentInstance instance = instances.first(); 00121 if ( instance.type().capabilities().contains( QLatin1String( "NoConfig" ) ) ) { 00122 configureActionEnabled = false; 00123 } 00124 } 00125 00126 enableAction( CreateAgentInstance, createActionEnabled ); 00127 enableAction( DeleteAgentInstance, deleteActionEnabled ); 00128 enableAction( ConfigureAgentInstance, configureActionEnabled ); 00129 00130 emit q->actionStateUpdated(); 00131 } 00132 00133 AgentInstance::List selectedAgentInstances() const 00134 { 00135 AgentInstance::List instances; 00136 00137 if ( !mSelectionModel ) { 00138 return instances; 00139 } 00140 00141 foreach ( const QModelIndex &index, mSelectionModel->selectedRows() ) { 00142 const AgentInstance instance = 00143 index.data( AgentInstanceModel::InstanceRole ).value<AgentInstance>(); 00144 if ( instance.isValid() ) { 00145 instances << instance; 00146 } 00147 } 00148 00149 return instances; 00150 } 00151 00152 void slotCreateAgentInstance() 00153 { 00154 Akonadi::AgentTypeDialog dlg( mParentWidget ); 00155 dlg.setCaption( contextText( AgentActionManager::CreateAgentInstance, 00156 AgentActionManager::DialogTitle ) ); 00157 00158 foreach ( const QString &mimeType, mMimeTypeFilter ) { 00159 dlg.agentFilterProxyModel()->addMimeTypeFilter( mimeType ); 00160 } 00161 00162 foreach ( const QString &capability, mCapabilityFilter ) { 00163 dlg.agentFilterProxyModel()->addCapabilityFilter( capability ); 00164 } 00165 00166 if ( dlg.exec() ) { 00167 const AgentType agentType = dlg.agentType(); 00168 00169 if ( agentType.isValid() ) { 00170 AgentInstanceCreateJob *job = new AgentInstanceCreateJob( agentType, q ); 00171 q->connect( job, SIGNAL(result(KJob*)), SLOT(slotAgentInstanceCreationResult(KJob*)) ); 00172 job->configure( mParentWidget ); 00173 job->start(); 00174 } 00175 } 00176 } 00177 00178 void slotDeleteAgentInstance() 00179 { 00180 const AgentInstance::List instances = selectedAgentInstances(); 00181 if ( !instances.isEmpty() ) { 00182 if ( KMessageBox::questionYesNo( 00183 mParentWidget, 00184 contextText( AgentActionManager::DeleteAgentInstance, 00185 AgentActionManager::MessageBoxText ), 00186 contextText( AgentActionManager::DeleteAgentInstance, 00187 AgentActionManager::MessageBoxTitle ), 00188 KStandardGuiItem::del(), 00189 KStandardGuiItem::cancel(), 00190 QString(), 00191 KMessageBox::Dangerous ) == KMessageBox::Yes ) { 00192 00193 foreach ( const AgentInstance &instance, instances ) { 00194 AgentManager::self()->removeInstance( instance ); 00195 } 00196 } 00197 } 00198 } 00199 00200 void slotConfigureAgentInstance() 00201 { 00202 AgentInstance::List instances = selectedAgentInstances(); 00203 if ( instances.isEmpty() ) { 00204 return; 00205 } 00206 00207 instances.first().configure( mParentWidget ); 00208 } 00209 00210 void slotAgentInstanceCreationResult( KJob *job ) 00211 { 00212 if ( job->error() ) { 00213 KMessageBox::error( 00214 mParentWidget, 00215 contextText( AgentActionManager::CreateAgentInstance, 00216 AgentActionManager::ErrorMessageText ).arg( job->errorString() ), 00217 contextText( AgentActionManager::CreateAgentInstance, 00218 AgentActionManager::ErrorMessageTitle ) ); 00219 } 00220 } 00221 00222 void setContextText( AgentActionManager::Type type, 00223 AgentActionManager::TextContext context, const QString &data ) 00224 { 00225 mContextTexts[ type ].insert( context, data ); 00226 } 00227 00228 void setContextText( AgentActionManager::Type type, 00229 AgentActionManager::TextContext context, const KLocalizedString &data ) 00230 { 00231 00232 mContextTexts[ type ].insert( context, data.toString() ); 00233 } 00234 00235 QString contextText( AgentActionManager::Type type, 00236 AgentActionManager::TextContext context ) const 00237 { 00238 return mContextTexts[ type ].value( context ); 00239 } 00240 00241 AgentActionManager *q; 00242 KActionCollection *mActionCollection; 00243 QWidget *mParentWidget; 00244 QItemSelectionModel *mSelectionModel; 00245 QVector<KAction*> mActions; 00246 QStringList mMimeTypeFilter; 00247 QStringList mCapabilityFilter; 00248 00249 typedef QHash<AgentActionManager::TextContext, QString> ContextTexts; 00250 QHash<AgentActionManager::Type, ContextTexts> mContextTexts; 00251 }; 00252 00253 //@endcond 00254 00255 AgentActionManager::AgentActionManager( KActionCollection *actionCollection, QWidget *parent ) 00256 : QObject( parent ), 00257 d( new Private( this ) ) 00258 { 00259 d->mParentWidget = parent; 00260 d->mActionCollection = actionCollection; 00261 } 00262 00263 AgentActionManager::~AgentActionManager() 00264 { 00265 delete d; 00266 } 00267 00268 void AgentActionManager::setSelectionModel( QItemSelectionModel *selectionModel ) 00269 { 00270 d->mSelectionModel = selectionModel; 00271 connect( selectionModel, SIGNAL(selectionChanged(QItemSelection,QItemSelection)), 00272 SLOT(updateActions()) ); 00273 } 00274 00275 void AgentActionManager::setMimeTypeFilter( const QStringList &mimeTypes ) 00276 { 00277 d->mMimeTypeFilter = mimeTypes; 00278 } 00279 00280 void AgentActionManager::setCapabilityFilter( const QStringList &capabilities ) 00281 { 00282 d->mCapabilityFilter = capabilities; 00283 } 00284 00285 KAction *AgentActionManager::createAction( Type type ) 00286 { 00287 Q_ASSERT( type >= 0 && type < LastType ); 00288 Q_ASSERT( agentActionData[ type ].name ); 00289 if ( d->mActions[ type ] ) { 00290 return d->mActions[ type ]; 00291 } 00292 00293 KAction *action = new KAction( d->mParentWidget ); 00294 action->setText( i18n( agentActionData[ type ].label ) ); 00295 00296 if ( agentActionData[ type ].icon ) { 00297 action->setIcon( KIcon( QString::fromLatin1( agentActionData[ type ].icon ) ) ); 00298 } 00299 00300 action->setShortcut( agentActionData[ type ].shortcut ); 00301 00302 if ( agentActionData[ type ].slot ) { 00303 connect( action, SIGNAL(triggered()), agentActionData[ type ].slot ); 00304 } 00305 00306 d->mActionCollection->addAction( QString::fromLatin1( agentActionData[ type ].name ), action ); 00307 d->mActions[ type ] = action; 00308 d->updateActions(); 00309 00310 return action; 00311 } 00312 00313 void AgentActionManager::createAllActions() 00314 { 00315 for ( int type = 0; type < LastType; ++type ) { 00316 createAction( (Type)type ); 00317 } 00318 } 00319 00320 KAction * AgentActionManager::action( Type type ) const 00321 { 00322 Q_ASSERT( type >= 0 && type < LastType ); 00323 return d->mActions[ type ]; 00324 } 00325 00326 void AgentActionManager::interceptAction( Type type, bool intercept ) 00327 { 00328 Q_ASSERT( type >= 0 && type < LastType ); 00329 00330 const KAction *action = d->mActions[ type ]; 00331 00332 if ( !action ) { 00333 return; 00334 } 00335 00336 if ( intercept ) { 00337 disconnect( action, SIGNAL(triggered()), this, agentActionData[ type ].slot ); 00338 } else { 00339 connect( action, SIGNAL(triggered()), agentActionData[ type ].slot ); 00340 } 00341 } 00342 00343 AgentInstance::List AgentActionManager::selectedAgentInstances() const 00344 { 00345 return d->selectedAgentInstances(); 00346 } 00347 00348 void AgentActionManager::setContextText( Type type, TextContext context, const QString &text ) 00349 { 00350 d->setContextText( type, context, text ); 00351 } 00352 00353 void AgentActionManager::setContextText( Type type, TextContext context, 00354 const KLocalizedString &text ) 00355 { 00356 d->setContextText( type, context, text ); 00357 } 00358 00359 #include "agentactionmanager.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:51 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 04:52:51 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.