korganizer

resourceview.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This program is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     This program is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015     GNU General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020 
00021     As a special exception, permission is given to link this program
00022     with any edition of Qt, and distribute the resulting executable,
00023     without including the source code for Qt in the source distribution.
00024 */
00025 
00026 #include "resourceview.h"
00027 
00028 #include <dcopref.h>
00029 #include <kcolordialog.h>
00030 #include <kdialog.h>
00031 #include <klistview.h>
00032 #include <klocale.h>
00033 #include <kdebug.h>
00034 #include <kglobal.h>
00035 #include <kmessagebox.h>
00036 #include <kinputdialog.h>
00037 #include <kiconloader.h>
00038 #include <kresources/resource.h>
00039 #include <kresources/configdialog.h>
00040 #include <libkcal/calendarresources.h>
00041 #include <kconfig.h>
00042 
00043 #include <qhbox.h>
00044 #include <qheader.h>
00045 #include <qlayout.h>
00046 #include <qlabel.h>
00047 #include <qpainter.h>
00048 #include <qpushbutton.h>
00049 #include <qpopupmenu.h>
00050 #include <qtooltip.h>
00051 #include <qwhatsthis.h>
00052 
00053 #include "koprefs.h"
00054 
00055 using namespace KCal;
00056 
00057 ResourceViewFactory::ResourceViewFactory( KCal::CalendarResources *calendar,
00058                                           CalendarView *view )
00059   : mCalendar( calendar ), mView( view ), mResourceView( 0 )
00060 {
00061 }
00062 
00063 CalendarViewExtension *ResourceViewFactory::create( QWidget *parent )
00064 {
00065   mResourceView = new ResourceView( mCalendar, parent );
00066 
00067   QObject::connect( mResourceView, SIGNAL( resourcesChanged() ),
00068                     mView, SLOT( resourcesChanged() ) );
00069   QObject::connect( mResourceView, SIGNAL( resourcesChanged() ),
00070                     mView, SLOT( updateCategories() ) );
00071 
00072   QObject::connect( mCalendar,
00073                     SIGNAL( signalResourceAdded( ResourceCalendar * ) ),
00074                     mResourceView,
00075                     SLOT( addResourceItem( ResourceCalendar * ) ) );
00076   QObject::connect( mCalendar,
00077                     SIGNAL( signalResourceModified( ResourceCalendar * ) ),
00078                     mResourceView,
00079                     SLOT( updateResourceItem( ResourceCalendar * ) ) );
00080   QObject::connect( mCalendar, SIGNAL( signalResourceAdded( ResourceCalendar * ) ),
00081                     mView, SLOT( updateCategories() ) );
00082   QObject::connect( mCalendar, SIGNAL( signalResourceModified( ResourceCalendar * ) ),
00083                     mView, SLOT( updateCategories() ) );
00084 
00085   return mResourceView;
00086 }
00087 
00088 ResourceView *ResourceViewFactory::resourceView() const
00089 {
00090   return mResourceView;
00091 }
00092 
00093 ResourceItem::ResourceItem( ResourceCalendar *resource, ResourceView *view,
00094                             KListView *parent )
00095   : QCheckListItem( parent, resource->resourceName(), CheckBox ),
00096     mResource( resource ), mView( view ), mBlockStateChange( false ),
00097     mIsSubresource( false ), mResourceIdentifier( QString::null ),
00098     mSubItemsCreated( false ), mIsStandardResource( false )
00099 {
00100   mResourceColor = QColor();
00101   setGuiState();
00102 
00103   if ( mResource->isActive() ) {
00104     createSubresourceItems();
00105   }
00106 }
00107 
00108 void ResourceItem::createSubresourceItems()
00109 {
00110   const QStringList subresources = mResource->subresources();
00111   if ( !subresources.isEmpty() ) {
00112     setOpen( true );
00113     setExpandable( true );
00114     // This resource has subresources
00115     QStringList::ConstIterator it;
00116     for ( it=subresources.begin(); it!=subresources.end(); ++it ) {
00117       ResourceItem *item = new ResourceItem( mResource, *it, mResource->labelForSubresource( *it ),
00118                                              mView, this );
00119       QColor resourceColor = *KOPrefs::instance()->resourceColor( *it );
00120       item->setResourceColor( resourceColor );
00121       item->update();
00122     }
00123   }
00124   mSubItemsCreated = true;
00125 }
00126 
00127 ResourceItem::ResourceItem( KCal::ResourceCalendar *resource,
00128                             const QString& sub, const QString& label,
00129                             ResourceView *view, ResourceItem* parent )
00130 
00131   : QCheckListItem( parent, label, CheckBox ), mResource( resource ),
00132     mView( view ), mBlockStateChange( false ), mIsSubresource( true ),
00133     mSubItemsCreated( false ), mIsStandardResource( false )
00134 {
00135   mResourceColor = QColor();
00136   mResourceIdentifier = sub;
00137   setGuiState();
00138 }
00139 
00140 void ResourceItem::setGuiState()
00141 {
00142   mBlockStateChange = true;
00143   if ( mIsSubresource )
00144     setOn( mResource->subresourceActive( mResourceIdentifier ) );
00145   else
00146     setOn( mResource->isActive() );
00147   mBlockStateChange = false;
00148 }
00149 
00150 void ResourceItem::stateChange( bool active )
00151 {
00152   if ( mBlockStateChange ) return;
00153 
00154   if ( mIsSubresource ) {
00155     mResource->setSubresourceActive( mResourceIdentifier, active );
00156   } else {
00157     if ( active ) {
00158       if ( mResource->load() ) {
00159         mResource->setActive( true );
00160         if ( !mSubItemsCreated )
00161           createSubresourceItems();
00162       }
00163     } else {
00164       if ( mResource->save() ) mResource->setActive( false );
00165       mView->requestClose( mResource );
00166     }
00167 
00168     setOpen( mResource->isActive() && childCount() > 0 );
00169 
00170     setGuiState();
00171   }
00172 
00173   mView->emitResourcesChanged();
00174 }
00175 
00176 void ResourceItem::update()
00177 {
00178   setGuiState();
00179 }
00180 
00181 void ResourceItem::setResourceColor(QColor& color)
00182 {
00183   if ( color.isValid() ) {
00184     if ( mResourceColor != color ) {
00185       QPixmap px(height()-4,height()-4);
00186       mResourceColor = color;
00187       px.fill(color);
00188       setPixmap(0,px);
00189     }
00190   } else {
00191     mResourceColor = color ;
00192     setPixmap(0,0);
00193   }
00194 }
00195 
00196 void ResourceItem::setStandardResource( bool std )
00197 {
00198   if ( mIsStandardResource != std ) {
00199     mIsStandardResource = std;
00200     repaint();
00201   }
00202 }
00203 
00204 void ResourceItem::paintCell(QPainter *p, const QColorGroup &cg,
00205       int column, int width, int alignment)
00206 {
00207   QFont oldFont = p->font();
00208   QFont newFont = oldFont;
00209   newFont.setBold( mIsStandardResource && !mIsSubresource );
00210   p->setFont( newFont );
00211   QCheckListItem::paintCell( p, cg, column, width, alignment );
00212   p->setFont( oldFont );
00213 /*  QColorGroup _cg = cg;
00214   if(!mResource) return;
00215   _cg.setColor(QColorGroup::Base, getTextColor(mResourceColor));*/
00216 }
00217 
00218 
00219 ResourceView::ResourceView( KCal::CalendarResources *calendar,
00220                             QWidget *parent, const char *name )
00221   : CalendarViewExtension( parent, name ), mCalendar( calendar )
00222 {
00223   QBoxLayout *topLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00224 
00225   QHBoxLayout *buttonBox = new QHBoxLayout();
00226   buttonBox->setSpacing( KDialog::spacingHint() );
00227   topLayout->addLayout( buttonBox );
00228 
00229   QLabel *calLabel = new QLabel( i18n( "Calendar" ), this );
00230   buttonBox->addWidget( calLabel );
00231   buttonBox->addStretch( 1 );
00232 
00233   mAddButton = new QPushButton( this, "add" );
00234   mAddButton->setIconSet( SmallIconSet( "add" ) );
00235   buttonBox->addWidget( mAddButton );
00236   QToolTip::add( mAddButton, i18n( "Add calendar" ) );
00237   QWhatsThis::add( mAddButton,
00238                    i18n( "<qt><p>Press this button to add a resource to "
00239                          "KOrganizer.</p>"
00240                          "<p>Events, journal entries and to-dos are retrieved "
00241                          "and stored on resources. Available "
00242                          "resources include groupware servers, local files, "
00243                          "journal entries as blogs on a server, etc... </p>"
00244                          "<p>If you have more than one active resource, "
00245                          "when creating incidents you will either automatically "
00246                          "use the default resource or be prompted "
00247                          "to select the resource to use.</p></qt>" ) );
00248   mEditButton = new QPushButton( this, "edit" );
00249   mEditButton->setIconSet( SmallIconSet( "edit" ) );
00250   buttonBox->addWidget( mEditButton );
00251   QToolTip::add( mEditButton, i18n( "Edit calendar settings" ) );
00252   QWhatsThis::add( mEditButton,
00253                    i18n( "Press this button to edit the resource currently "
00254                          "selected on the KOrganizer resources list above." ) );
00255   mDeleteButton = new QPushButton( this, "del" );
00256   mDeleteButton->setIconSet( SmallIconSet( "remove" ) );
00257   buttonBox->addWidget( mDeleteButton );
00258   QToolTip::add( mDeleteButton, i18n( "Remove calendar" ) );
00259   QWhatsThis::add( mDeleteButton,
00260                    i18n( "Press this button to delete the resource currently "
00261                          "selected on the KOrganizer resources list above." ) );
00262   mDeleteButton->setDisabled( true );
00263   mEditButton->setDisabled( true );
00264 
00265   mListView = new KListView( this );
00266   mListView->header()->hide();
00267   QWhatsThis::add( mListView,
00268                    i18n( "<qt><p>Select on this list the active KOrganizer "
00269                          "resources. Check the resource box to make it "
00270                          "active. Press the \"Add...\" button below to add new "
00271                          "resources to the list.</p>"
00272                          "<p>Events, journal entries and to-dos are retrieved "
00273                          "and stored on resources. Available "
00274                          "resources include groupware servers, local files, "
00275                          "journal entries as blogs on a server, etc...</p>"
00276                          "<p>If you have more than one active resource, "
00277                          "when creating incidents you will either automatically "
00278                          "use the default resource or be prompted "
00279                          "to select the resource to use.</p></qt>" ) );
00280   mListView->addColumn( i18n("Calendar") );
00281   mListView->setResizeMode( QListView::LastColumn );
00282   topLayout->addWidget( mListView );
00283 
00284   connect( mListView, SIGNAL( clicked( QListViewItem * ) ),
00285            SLOT( currentChanged( QListViewItem * ) ) );
00286   connect( mAddButton, SIGNAL( clicked() ), SLOT( addResource() ) );
00287   connect( mDeleteButton, SIGNAL( clicked() ), SLOT( removeResource() ) );
00288   connect( mEditButton, SIGNAL( clicked() ), SLOT( editResource() ) );
00289   connect( mListView, SIGNAL( doubleClicked ( QListViewItem *, const QPoint &,
00290                                               int ) ),
00291            SLOT( editResource() ) );
00292   connect( mListView, SIGNAL( contextMenuRequested ( QListViewItem *,
00293                                                      const QPoint &, int ) ),
00294            SLOT( contextMenuRequested( QListViewItem *, const QPoint &,
00295                                        int ) ) );
00296 
00297   updateView();
00298 }
00299 
00300 ResourceView::~ResourceView()
00301 {
00302 }
00303 
00304 void ResourceView::updateView()
00305 {
00306   mListView->clear();
00307 
00308   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00309 
00310   KCal::CalendarResourceManager::Iterator it;
00311   for( it = manager->begin(); it != manager->end(); ++it ) {
00312     addResourceItem( *it );
00313   }
00314 }
00315 
00316 void ResourceView::emitResourcesChanged()
00317 {
00318   mCalendar->resourceManager()->writeConfig();
00319   emit resourcesChanged();
00320 }
00321 
00322 void ResourceView::addResource()
00323 {
00324   bool ok = false;
00325   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00326   ResourceItem *i = static_cast<ResourceItem*>( mListView->selectedItem() );
00327   if ( i && ( i->isSubresource() || i->resource()->canHaveSubresources() ) ) {
00328     const QString folderName = KInputDialog::getText( i18n( "Add Subresource" ),
00329             i18n( "Please enter a name for the new subresource" ), QString::null,
00330             &ok, this );
00331     if ( !ok )
00332       return;
00333     const QString parentId = i->isSubresource() ? i->resourceIdentifier() : QString:: null;
00334     if ( !i->resource()->addSubresource( folderName, parentId ) ) {
00335       KMessageBox::error( this, i18n("<qt>Unable to create subresource <b>%1</b>.</qt>")
00336                                 .arg( folderName ) );
00337     }
00338     return;
00339   }
00340 
00341   QStringList types = manager->resourceTypeNames();
00342   QStringList descs = manager->resourceTypeDescriptions();
00343   QString desc = KInputDialog::getItem( i18n( "Resource Configuration" ),
00344       i18n( "Please select type of the new resource:" ), descs, 0, false, &ok,
00345             this );
00346   if ( !ok )
00347     return;
00348 
00349   QString type = types[ descs.findIndex( desc ) ];
00350 
00351   // Create new resource
00352   ResourceCalendar *resource = manager->createResource( type );
00353   if( !resource ) {
00354     KMessageBox::error( this, i18n("<qt>Unable to create resource of type <b>%1</b>.</qt>")
00355                               .arg( type ) );
00356     return;
00357   }
00358 
00359   resource->setResourceName( i18n("%1 resource").arg( type ) );
00360 
00361   KRES::ConfigDialog *dlg = new KRES::ConfigDialog( this, QString("calendar"), resource,
00362                           "KRES::ConfigDialog" );
00363 
00364   bool success = true;
00365   if ( !dlg || !dlg->exec() )
00366     success = false;
00367 
00368   if ( success ) {
00369     resource->setTimeZoneId( KOPrefs::instance()->mTimeZoneId );
00370     if ( resource->isActive() && ( !resource->open() || !resource->load() ) ) {
00371       // ### There is a resourceLoadError() signal declared in ResourceCalendar
00372       //     but no subclass seems to make use of it. We could do better.
00373       KMessageBox::error( this, i18n("Unable to create the resource.")
00374                                 .arg( type ) );
00375       success = false;
00376     }
00377   }
00378 
00379   if ( success ) {
00380     manager->add( resource );
00381     // we have to call resourceAdded manually, because for in-process changes
00382     // the dcop signals are not connected, so the resource's signals would not
00383     // be connected otherwise
00384     mCalendar->resourceAdded( resource );
00385   }
00386 
00387   if ( !success )
00388     delete resource;
00389 
00390   delete dlg;
00391 
00392   //### maybe only do this if ( success )
00393   emitResourcesChanged();
00394 }
00395 
00396 void ResourceView::addResourceItem( ResourceCalendar *resource )
00397 {
00398 
00399   ResourceItem *item=new ResourceItem( resource, this, mListView );
00400 
00401   // assign a color, but only if this is a resource that actually
00402   // hold items at top level
00403   if ( !resource->canHaveSubresources() || resource->subresources().isEmpty() ) {
00404       QColor resourceColor = *KOPrefs::instance()->resourceColor(resource->identifier());
00405       item->setResourceColor(resourceColor);
00406       item->update();
00407   }
00408 
00409   connect( resource, SIGNAL( signalSubresourceAdded( ResourceCalendar *,
00410                                                      const QString &,
00411                                                      const QString &,
00412                                                      const QString & ) ),
00413            SLOT( slotSubresourceAdded( ResourceCalendar *, const QString &,
00414                                        const QString &, const QString & ) ) );
00415 
00416   connect( resource, SIGNAL( signalSubresourceRemoved( ResourceCalendar *,
00417                                                        const QString &,
00418                                                        const QString & ) ),
00419            SLOT( slotSubresourceRemoved( ResourceCalendar *, const QString &,
00420                                          const QString & ) ) );
00421 
00422   connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ),
00423            SLOT( closeResource( ResourceCalendar * ) ) );
00424 
00425   updateResourceList();
00426   emit resourcesChanged();
00427 }
00428 
00429 // Add a new entry
00430 void ResourceView::slotSubresourceAdded( ResourceCalendar *calendar,
00431                                          const QString& /*type*/,
00432                                          const QString& resource,
00433                                          const QString& label)
00434 {
00435   QListViewItem *i = mListView->findItem( calendar->resourceName(), 0 );
00436   if ( !i )
00437     // Not found
00438     return;
00439 
00440   if ( findItemByIdentifier( resource ) ) return;
00441 
00442   ResourceItem *item = static_cast<ResourceItem *>( i );
00443   ResourceItem *newItem = new ResourceItem( calendar, resource, label, this, item );
00444   QColor resourceColor = *KOPrefs::instance()->resourceColor( resource );
00445   newItem->setResourceColor( resourceColor );
00446 }
00447 
00448 // Remove an entry
00449 void ResourceView::slotSubresourceRemoved( ResourceCalendar * /*calendar*/,
00450                                            const QString &/*type*/,
00451                                            const QString &resource )
00452 {
00453   delete findItemByIdentifier( resource );
00454   emit resourcesChanged();
00455 }
00456 
00457 void ResourceView::closeResource( ResourceCalendar *r )
00458 {
00459   if ( mResourcesToClose.find( r ) >= 0 ) {
00460     r->close();
00461     mResourcesToClose.remove( r );
00462   }
00463 }
00464 
00465 void ResourceView::updateResourceItem( ResourceCalendar *resource )
00466 {
00467   ResourceItem *item = findItem( resource );
00468   if ( item ) {
00469     item->update();
00470   }
00471 }
00472 
00473 ResourceItem *ResourceView::currentItem()
00474 {
00475   QListViewItem *item = mListView->currentItem();
00476   ResourceItem *rItem = static_cast<ResourceItem *>( item );
00477   return rItem;
00478 }
00479 
00480 void ResourceView::removeResource()
00481 {
00482   ResourceItem *item = currentItem();
00483   if ( !item ) return;
00484 
00485   const QString warningMsg = item->isSubresource() ?
00486         i18n("<qt>Do you really want to remove the subresource <b>%1</b>? "
00487               "Note that its contents will be completely deleted. This "
00488               "operation cannot be undone. </qt>").arg( item->text( 0 ) ) :
00489         i18n("<qt>Do you really want to remove the resource <b>%1</b>?</qt>").arg( item->text( 0 ) );
00490 
00491   int km = KMessageBox::warningContinueCancel( this, warningMsg, "",
00492         KGuiItem( i18n("&Remove" ), "editdelete") );
00493   if ( km == KMessageBox::Cancel ) return;
00494 
00495 // Don't be so restricitve
00496 #if 0
00497   if ( item->resource() == mCalendar->resourceManager()->standardResource() ) {
00498     KMessageBox::sorry( this,
00499                         i18n( "You cannot delete your standard resource." ) );
00500     return;
00501   }
00502 #endif
00503   if ( item->isSubresource() ) {
00504     if ( !item->resource()->removeSubresource( item->resourceIdentifier() ) )
00505       KMessageBox::sorry( this,
00506               i18n ("<qt>Failed to remove the subresource <b>%1</b>. The "
00507                   "reason could be that it is a built-in one which cannot "
00508                   "be removed, or that the removal of the underlying storage "
00509                   "folder failed.</qt>").arg( item->resource()->name() ) );
00510       return;
00511   } else {
00512     mCalendar->resourceManager()->remove( item->resource() );
00513   }
00514     mListView->takeItem( item );
00515     delete item;
00516 
00517   updateResourceList();
00518   emit resourcesChanged();
00519 }
00520 
00521 void ResourceView::editResource()
00522 {
00523   bool ok = false;
00524   ResourceItem *item = currentItem();
00525   if (!item) return;
00526   ResourceCalendar *resource = item->resource();
00527 
00528    if ( item->isSubresource() ) {
00529      if ( resource->type() == "imap" || resource->type() == "scalix" ) {
00530         QString identifier = item->resourceIdentifier();
00531         const QString newResourceName = KInputDialog::getText( i18n( "Rename Subresource" ),
00532                i18n( "Please enter a new name for the subresource" ), item->text(),
00533                     &ok, this );
00534         if ( !ok )
00535           return;
00536 
00537         DCOPRef ref( "kmail", "KMailICalIface" );
00538         DCOPReply reply = ref.call( "changeResourceUIName", identifier, newResourceName );
00539         if ( !reply.isValid() ) {
00540            kdDebug() << "DCOP Call changeResourceUIName() failed " << endl;
00541         }
00542      } else {
00543            KMessageBox::sorry( this,
00544                                i18n ("<qt>Cannot edit the subresource <b>%1</b>.</qt>").arg( item->resource()->name() ) );
00545        }
00546    } else {
00547      KRES::ConfigDialog dlg( this, QString("calendar"), resource,
00548                           "KRES::ConfigDialog" );
00549 
00550      if ( dlg.exec() ) {
00551        item->setText( 0, resource->resourceName() );
00552 
00553       mCalendar->resourceManager()->change( resource );
00554      }
00555    }
00556    emitResourcesChanged();
00557 
00558 }
00559 
00560 void ResourceView::currentChanged( QListViewItem *item )
00561 {
00562    ResourceItem *i = currentItem();
00563    if ( !item || i->isSubresource() ) {
00564      mDeleteButton->setEnabled( false );
00565      mEditButton->setEnabled( false );
00566    } else {
00567      mDeleteButton->setEnabled( true );
00568      mEditButton->setEnabled( true );
00569    }
00570 }
00571 
00572 ResourceItem *ResourceView::findItem( ResourceCalendar *r )
00573 {
00574   QListViewItem *item;
00575   ResourceItem *i = 0;
00576   for( item = mListView->firstChild(); item; item = item->nextSibling() ) {
00577     i = static_cast<ResourceItem *>( item );
00578     if ( i->resource() == r ) break;
00579   }
00580   return i;
00581 }
00582 
00583 ResourceItem *ResourceView::findItemByIdentifier( const QString& id )
00584 {
00585   QListViewItem *item;
00586   ResourceItem *i = 0;
00587   for( item = mListView->firstChild(); item; item = item->itemBelow() ) {
00588     i = static_cast<ResourceItem *>( item );
00589     if ( i->resourceIdentifier() == id )
00590        return i;
00591   }
00592   return 0;
00593 }
00594 
00595 
00596 void ResourceView::contextMenuRequested ( QListViewItem *i,
00597                                           const QPoint &pos, int )
00598 {
00599   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00600   ResourceItem *item = static_cast<ResourceItem *>( i );
00601 
00602   QPopupMenu *menu = new QPopupMenu( this );
00603   connect( menu, SIGNAL( aboutToHide() ), menu, SLOT( deleteLater() ) );
00604   if ( item ) {
00605     int reloadId = menu->insertItem( i18n("Re&load"), this,
00606                                      SLOT( reloadResource() ) );
00607     menu->setItemEnabled( reloadId, item->resource()->isActive() );
00608     int saveId = menu->insertItem( i18n("&Save"), this,
00609                                    SLOT( saveResource() ) );
00610     menu->setItemEnabled( saveId, item->resource()->isActive() );
00611     menu->insertSeparator();
00612 
00613     menu->insertItem( i18n("Show &Info"), this, SLOT( showInfo() ) );
00614     //FIXME: This is better on the resource dialog
00615     if ( KOPrefs::instance()->agendaViewColors() != KOPrefs::CategoryOnly ) {
00616       QPopupMenu *assignMenu= new QPopupMenu( menu );
00617       assignMenu->insertItem( i18n( "&Assign Color" ), this, SLOT( assignColor() ) );
00618       if ( item->resourceColor().isValid() )
00619         assignMenu->insertItem( i18n( "&Disable Color" ), this, SLOT( disableColor() ) );
00620       menu->insertItem( i18n( "Resources Colors" ), assignMenu );
00621     }
00622 
00623     menu->insertItem( i18n("&Edit..."), this, SLOT( editResource() ) );
00624     menu->insertItem( i18n("&Remove"), this, SLOT( removeResource() ) );
00625     if ( item->resource() != manager->standardResource() ) {
00626       menu->insertSeparator();
00627       menu->insertItem( i18n("Use as &Default Calendar"), this,
00628                         SLOT( setStandard() ) );
00629     }
00630 
00631     menu->insertSeparator();
00632  }
00633   menu->insertItem( i18n("&Add..."), this, SLOT( addResource() ) );
00634 
00635   menu->popup( pos );
00636 }
00637 
00638 void ResourceView::assignColor()
00639 {
00640   ResourceItem *item = currentItem();
00641   if ( !item )
00642     return;
00643   // A color without initialized is a color invalid
00644   QColor myColor;
00645   KCal::ResourceCalendar *cal = item->resource();
00646 
00647   QString identifier = cal->identifier();
00648   if ( item->isSubresource() )
00649     identifier = item->resourceIdentifier();
00650 
00651   QColor defaultColor =*KOPrefs::instance()->resourceColor( identifier );
00652 
00653   int result = KColorDialog::getColor( myColor,defaultColor);
00654 
00655   if ( result == KColorDialog::Accepted ) {
00656     KOPrefs::instance()->setResourceColor( identifier, myColor );
00657     item->setResourceColor( myColor );
00658     item->update();
00659     emitResourcesChanged();
00660   }
00661 }
00662 
00663 void ResourceView::disableColor()
00664 {
00665   ResourceItem *item = currentItem();
00666   if ( !item )
00667     return;
00668   QColor colorInvalid;
00669   KCal::ResourceCalendar *cal = item->resource();
00670   QString identifier = cal->identifier();
00671   if ( item->isSubresource() )
00672     identifier = item->resourceIdentifier();
00673   KOPrefs::instance()->setResourceColor( identifier, colorInvalid );
00674   item->setResourceColor( colorInvalid );
00675   item->update();
00676   emitResourcesChanged();
00677 }
00678 void ResourceView::showInfo()
00679 {
00680   ResourceItem *item = currentItem();
00681   if ( !item ) return;
00682 
00683   QString txt = "<qt>" + item->resource()->infoText() + "</qt>";
00684   KMessageBox::information( this, txt );
00685 }
00686 
00687 void ResourceView::reloadResource()
00688 {
00689   ResourceItem *item = currentItem();
00690   if ( !item ) return;
00691 
00692   ResourceCalendar *r = item->resource();
00693   r->load();
00694 }
00695 
00696 void ResourceView::saveResource()
00697 {
00698   ResourceItem *item = currentItem();
00699   if ( !item ) return;
00700 
00701   ResourceCalendar *r = item->resource();
00702   r->save();
00703 }
00704 
00705 void ResourceView::setStandard()
00706 {
00707   ResourceItem *item = currentItem();
00708   if ( !item ) return;
00709 
00710   ResourceCalendar *r = item->resource();
00711   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00712   manager->setStandardResource( r );
00713   updateResourceList();
00714 }
00715 
00716 void ResourceView::updateResourceList()
00717 {
00718   QListViewItemIterator it( mListView );
00719   ResourceCalendar* stdRes = mCalendar->resourceManager()->standardResource();
00720   while ( it.current() ) {
00721     ResourceItem *item = static_cast<ResourceItem *>( it.current() );
00722     item->setStandardResource( item->resource() == stdRes );
00723     ++it;
00724   }
00725 }
00726 
00727 void ResourceView::showButtons( bool visible )
00728 {
00729   if ( visible ) {
00730     mAddButton->show();
00731     mDeleteButton->show();
00732     mEditButton->show();
00733   } else {
00734     mAddButton->hide();
00735     mDeleteButton->hide();
00736     mEditButton->hide();
00737   }
00738 }
00739 
00740 void ResourceView::requestClose( ResourceCalendar *r )
00741 {
00742   mResourcesToClose.append( r );
00743 }
00744 
00745 #include "resourceview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys