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       // mView->requestClose must be called before mResource->save() because
00165       // save causes closeResource do be called.
00166       mView->requestClose( mResource );
00167       if ( mResource->save() ) {
00168         mResource->setActive( false );
00169       }
00170     }
00171 
00172     setOpen( mResource->isActive() && childCount() > 0 );
00173 
00174     setGuiState();
00175   }
00176 
00177   mView->emitResourcesChanged();
00178 }
00179 
00180 void ResourceItem::update()
00181 {
00182   setGuiState();
00183 }
00184 
00185 void ResourceItem::setResourceColor(QColor& color)
00186 {
00187   if ( color.isValid() ) {
00188     if ( mResourceColor != color ) {
00189       QPixmap px(height()-4,height()-4);
00190       mResourceColor = color;
00191       px.fill(color);
00192       setPixmap(0,px);
00193     }
00194   } else {
00195     mResourceColor = color ;
00196     setPixmap(0,0);
00197   }
00198 }
00199 
00200 void ResourceItem::setStandardResource( bool std )
00201 {
00202   if ( mIsStandardResource != std ) {
00203     mIsStandardResource = std;
00204     repaint();
00205   }
00206 }
00207 
00208 void ResourceItem::paintCell(QPainter *p, const QColorGroup &cg,
00209       int column, int width, int alignment)
00210 {
00211   QFont oldFont = p->font();
00212   QFont newFont = oldFont;
00213   newFont.setBold( mIsStandardResource && !mIsSubresource );
00214   p->setFont( newFont );
00215   QCheckListItem::paintCell( p, cg, column, width, alignment );
00216   p->setFont( oldFont );
00217 /*  QColorGroup _cg = cg;
00218   if(!mResource) return;
00219   _cg.setColor(QColorGroup::Base, getTextColor(mResourceColor));*/
00220 }
00221 
00222 
00223 ResourceView::ResourceView( KCal::CalendarResources *calendar,
00224                             QWidget *parent, const char *name )
00225   : CalendarViewExtension( parent, name ), mCalendar( calendar )
00226 {
00227   QBoxLayout *topLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00228 
00229   QHBoxLayout *buttonBox = new QHBoxLayout();
00230   buttonBox->setSpacing( KDialog::spacingHint() );
00231   topLayout->addLayout( buttonBox );
00232 
00233   QLabel *calLabel = new QLabel( i18n( "Calendar" ), this );
00234   buttonBox->addWidget( calLabel );
00235   buttonBox->addStretch( 1 );
00236 
00237   mAddButton = new QPushButton( this, "add" );
00238   mAddButton->setIconSet( SmallIconSet( "add" ) );
00239   buttonBox->addWidget( mAddButton );
00240   QToolTip::add( mAddButton, i18n( "Add calendar" ) );
00241   QWhatsThis::add( mAddButton,
00242                    i18n( "<qt><p>Press this button to add a resource to "
00243                          "KOrganizer.</p>"
00244                          "<p>Events, journal entries and to-dos are retrieved "
00245                          "and stored on resources. Available "
00246                          "resources include groupware servers, local files, "
00247                          "journal entries as blogs on a server, etc... </p>"
00248                          "<p>If you have more than one active resource, "
00249                          "when creating incidents you will either automatically "
00250                          "use the default resource or be prompted "
00251                          "to select the resource to use.</p></qt>" ) );
00252   mEditButton = new QPushButton( this, "edit" );
00253   mEditButton->setIconSet( SmallIconSet( "edit" ) );
00254   buttonBox->addWidget( mEditButton );
00255   QToolTip::add( mEditButton, i18n( "Edit calendar settings" ) );
00256   QWhatsThis::add( mEditButton,
00257                    i18n( "Press this button to edit the resource currently "
00258                          "selected on the KOrganizer resources list above." ) );
00259   mDeleteButton = new QPushButton( this, "del" );
00260   mDeleteButton->setIconSet( SmallIconSet( "remove" ) );
00261   buttonBox->addWidget( mDeleteButton );
00262   QToolTip::add( mDeleteButton, i18n( "Remove calendar" ) );
00263   QWhatsThis::add( mDeleteButton,
00264                    i18n( "Press this button to delete the resource currently "
00265                          "selected on the KOrganizer resources list above." ) );
00266   mDeleteButton->setDisabled( true );
00267   mEditButton->setDisabled( true );
00268 
00269   mListView = new KListView( this );
00270   mListView->header()->hide();
00271   QWhatsThis::add( mListView,
00272                    i18n( "<qt><p>Select on this list the active KOrganizer "
00273                          "resources. Check the resource box to make it "
00274                          "active. Press the \"Add...\" button below to add new "
00275                          "resources to the list.</p>"
00276                          "<p>Events, journal entries and to-dos are retrieved "
00277                          "and stored on resources. Available "
00278                          "resources include groupware servers, local files, "
00279                          "journal entries as blogs on a server, etc...</p>"
00280                          "<p>If you have more than one active resource, "
00281                          "when creating incidents you will either automatically "
00282                          "use the default resource or be prompted "
00283                          "to select the resource to use.</p></qt>" ) );
00284   mListView->addColumn( i18n("Calendar") );
00285   mListView->setResizeMode( QListView::LastColumn );
00286   topLayout->addWidget( mListView );
00287 
00288   connect( mListView, SIGNAL( clicked( QListViewItem * ) ),
00289            SLOT( currentChanged( QListViewItem * ) ) );
00290   connect( mAddButton, SIGNAL( clicked() ), SLOT( addResource() ) );
00291   connect( mDeleteButton, SIGNAL( clicked() ), SLOT( removeResource() ) );
00292   connect( mEditButton, SIGNAL( clicked() ), SLOT( editResource() ) );
00293   connect( mListView, SIGNAL( doubleClicked ( QListViewItem *, const QPoint &,
00294                                               int ) ),
00295            SLOT( editResource() ) );
00296   connect( mListView, SIGNAL( contextMenuRequested ( QListViewItem *,
00297                                                      const QPoint &, int ) ),
00298            SLOT( contextMenuRequested( QListViewItem *, const QPoint &,
00299                                        int ) ) );
00300 
00301   updateView();
00302 }
00303 
00304 ResourceView::~ResourceView()
00305 {
00306 }
00307 
00308 void ResourceView::updateView()
00309 {
00310   mListView->clear();
00311 
00312   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00313 
00314   KCal::CalendarResourceManager::Iterator it;
00315   for( it = manager->begin(); it != manager->end(); ++it ) {
00316     addResourceItem( *it );
00317   }
00318 }
00319 
00320 void ResourceView::emitResourcesChanged()
00321 {
00322   mCalendar->resourceManager()->writeConfig();
00323   emit resourcesChanged();
00324 }
00325 
00326 void ResourceView::addResource()
00327 {
00328   bool ok = false;
00329   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00330   ResourceItem *i = static_cast<ResourceItem*>( mListView->selectedItem() );
00331   if ( i && ( i->isSubresource() || i->resource()->canHaveSubresources() ) ) {
00332     const QString folderName = KInputDialog::getText( i18n( "Add Subresource" ),
00333             i18n( "Please enter a name for the new subresource" ), QString::null,
00334             &ok, this );
00335     if ( !ok )
00336       return;
00337     const QString parentId = i->isSubresource() ? i->resourceIdentifier() : QString:: null;
00338     if ( !i->resource()->addSubresource( folderName, parentId ) ) {
00339       KMessageBox::error( this, i18n("<qt>Unable to create subresource <b>%1</b>.</qt>")
00340                                 .arg( folderName ) );
00341     }
00342     return;
00343   }
00344 
00345   QStringList types = manager->resourceTypeNames();
00346   QStringList descs = manager->resourceTypeDescriptions();
00347   QString desc = KInputDialog::getItem( i18n( "Resource Configuration" ),
00348       i18n( "Please select type of the new resource:" ), descs, 0, false, &ok,
00349             this );
00350   if ( !ok )
00351     return;
00352 
00353   QString type = types[ descs.findIndex( desc ) ];
00354 
00355   // Create new resource
00356   ResourceCalendar *resource = manager->createResource( type );
00357   if( !resource ) {
00358     KMessageBox::error( this, i18n("<qt>Unable to create resource of type <b>%1</b>.</qt>")
00359                               .arg( type ) );
00360     return;
00361   }
00362 
00363   KRES::ConfigDialog *dlg = new KRES::ConfigDialog( this, QString("calendar"), resource,
00364                           "KRES::ConfigDialog" );
00365 
00366   bool success = true;
00367   if ( !dlg || !dlg->exec() )
00368     success = false;
00369 
00370   if ( success ) {
00371     resource->setTimeZoneId( KOPrefs::instance()->mTimeZoneId );
00372     if ( resource->isActive() && ( !resource->open() || !resource->load() ) ) {
00373       // ### There is a resourceLoadError() signal declared in ResourceCalendar
00374       //     but no subclass seems to make use of it. We could do better.
00375       KMessageBox::error( this, i18n("Unable to create the resource.")
00376                                 .arg( type ) );
00377       success = false;
00378     }
00379   }
00380 
00381   if ( success ) {
00382     manager->add( resource );
00383     // we have to call resourceAdded manually, because for in-process changes
00384     // the dcop signals are not connected, so the resource's signals would not
00385     // be connected otherwise
00386     mCalendar->resourceAdded( resource );
00387   }
00388 
00389   if ( !success )
00390     delete resource;
00391 
00392   delete dlg;
00393 
00394   //### maybe only do this if ( success )
00395   emitResourcesChanged();
00396 }
00397 
00398 void ResourceView::addResourceItem( ResourceCalendar *resource )
00399 {
00400 
00401   ResourceItem *item=new ResourceItem( resource, this, mListView );
00402 
00403   // assign a color, but only if this is a resource that actually
00404   // hold items at top level
00405   if ( !resource->canHaveSubresources() || resource->subresources().isEmpty() ) {
00406       QColor resourceColor = *KOPrefs::instance()->resourceColor(resource->identifier());
00407       item->setResourceColor(resourceColor);
00408       item->update();
00409   }
00410 
00411   connect( resource, SIGNAL( signalSubresourceAdded( ResourceCalendar *,
00412                                                      const QString &,
00413                                                      const QString &,
00414                                                      const QString & ) ),
00415            SLOT( slotSubresourceAdded( ResourceCalendar *, const QString &,
00416                                        const QString &, const QString & ) ) );
00417 
00418   connect( resource, SIGNAL( signalSubresourceRemoved( ResourceCalendar *,
00419                                                        const QString &,
00420                                                        const QString & ) ),
00421            SLOT( slotSubresourceRemoved( ResourceCalendar *, const QString &,
00422                                          const QString & ) ) );
00423 
00424   connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ),
00425            SLOT( closeResource( ResourceCalendar * ) ) );
00426 
00427   updateResourceList();
00428   emit resourcesChanged();
00429 }
00430 
00431 // Add a new entry
00432 void ResourceView::slotSubresourceAdded( ResourceCalendar *calendar,
00433                                          const QString& /*type*/,
00434                                          const QString& resource,
00435                                          const QString& label)
00436 {
00437   QListViewItem *i = mListView->findItem( calendar->resourceName(), 0 );
00438   if ( !i )
00439     // Not found
00440     return;
00441 
00442   if ( findItemByIdentifier( resource ) ) return;
00443 
00444   ResourceItem *item = static_cast<ResourceItem *>( i );
00445   ResourceItem *newItem = new ResourceItem( calendar, resource, label, this, item );
00446   QColor resourceColor = *KOPrefs::instance()->resourceColor( resource );
00447   newItem->setResourceColor( resourceColor );
00448 }
00449 
00450 // Remove an entry
00451 void ResourceView::slotSubresourceRemoved( ResourceCalendar * /*calendar*/,
00452                                            const QString &/*type*/,
00453                                            const QString &resource )
00454 {
00455   delete findItemByIdentifier( resource );
00456   emit resourcesChanged();
00457 }
00458 
00459 void ResourceView::closeResource( ResourceCalendar *r )
00460 {
00461   if ( mResourcesToClose.find( r ) >= 0 ) {
00462     r->close();
00463     mResourcesToClose.remove( r );
00464   }
00465 }
00466 
00467 void ResourceView::updateResourceItem( ResourceCalendar *resource )
00468 {
00469   ResourceItem *item = findItem( resource );
00470   if ( item ) {
00471     item->update();
00472   }
00473 }
00474 
00475 ResourceItem *ResourceView::currentItem()
00476 {
00477   QListViewItem *item = mListView->currentItem();
00478   ResourceItem *rItem = static_cast<ResourceItem *>( item );
00479   return rItem;
00480 }
00481 
00482 void ResourceView::removeResource()
00483 {
00484   ResourceItem *item = currentItem();
00485   if ( !item ) return;
00486 
00487   const QString warningMsg = item->isSubresource() ?
00488         i18n("<qt>Do you really want to remove the subresource <b>%1</b>? "
00489               "Note that its contents will be completely deleted. This "
00490               "operation cannot be undone. </qt>").arg( item->text( 0 ) ) :
00491         i18n("<qt>Do you really want to remove the resource <b>%1</b>?</qt>").arg( item->text( 0 ) );
00492 
00493   int km = KMessageBox::warningContinueCancel( this, warningMsg, "",
00494         KGuiItem( i18n("&Remove" ), "editdelete") );
00495   if ( km == KMessageBox::Cancel ) return;
00496 
00497 // Don't be so restricitve
00498 #if 1
00499   if ( item->resource() == mCalendar->resourceManager()->standardResource() ) {
00500     KMessageBox::sorry( this,
00501                         i18n( "You cannot delete your standard resource." ) );
00502     return;
00503   }
00504 #endif
00505   if ( item->isSubresource() ) {
00506     if ( !item->resource()->removeSubresource( item->resourceIdentifier() ) )
00507       KMessageBox::sorry( this,
00508               i18n ("<qt>Failed to remove the subresource <b>%1</b>. The "
00509                   "reason could be that it is a built-in one which cannot "
00510                   "be removed, or that the removal of the underlying storage "
00511                   "folder failed.</qt>").arg( item->resource()->name() ) );
00512       return;
00513   } else {
00514     mCalendar->resourceManager()->remove( item->resource() );
00515   }
00516     mListView->takeItem( item );
00517     delete item;
00518 
00519   updateResourceList();
00520   emit resourcesChanged();
00521 }
00522 
00523 void ResourceView::editResource()
00524 {
00525   bool ok = false;
00526   ResourceItem *item = currentItem();
00527   if (!item) return;
00528   ResourceCalendar *resource = item->resource();
00529 
00530    if ( item->isSubresource() ) {
00531      if ( resource->type() == "imap" || resource->type() == "scalix" ) {
00532         QString identifier = item->resourceIdentifier();
00533         const QString newResourceName = KInputDialog::getText( i18n( "Rename Subresource" ),
00534                i18n( "Please enter a new name for the subresource" ), item->text(),
00535                     &ok, this );
00536         if ( !ok )
00537           return;
00538 
00539         DCOPRef ref( "kmail", "KMailICalIface" );
00540         DCOPReply reply = ref.call( "changeResourceUIName", identifier, newResourceName );
00541         if ( !reply.isValid() ) {
00542            kdDebug() << "DCOP Call changeResourceUIName() failed " << endl;
00543         }
00544      } else {
00545            KMessageBox::sorry( this,
00546                                i18n ("<qt>Cannot edit the subresource <b>%1</b>.</qt>").arg( item->resource()->name() ) );
00547        }
00548    } else {
00549      KRES::ConfigDialog dlg( this, QString("calendar"), resource,
00550                           "KRES::ConfigDialog" );
00551 
00552      if ( dlg.exec() ) {
00553        item->setText( 0, resource->resourceName() );
00554 
00555       mCalendar->resourceManager()->change( resource );
00556      }
00557    }
00558    emitResourcesChanged();
00559 
00560 }
00561 
00562 void ResourceView::currentChanged( QListViewItem *item )
00563 {
00564    ResourceItem *i = currentItem();
00565    if ( !item || i->isSubresource() ) {
00566      mDeleteButton->setEnabled( false );
00567      mEditButton->setEnabled( false );
00568    } else {
00569      mDeleteButton->setEnabled( true );
00570      mEditButton->setEnabled( true );
00571    }
00572 }
00573 
00574 ResourceItem *ResourceView::findItem( ResourceCalendar *r )
00575 {
00576   QListViewItem *item;
00577   ResourceItem *i = 0;
00578   for( item = mListView->firstChild(); item; item = item->nextSibling() ) {
00579     i = static_cast<ResourceItem *>( item );
00580     if ( i->resource() == r ) break;
00581   }
00582   return i;
00583 }
00584 
00585 ResourceItem *ResourceView::findItemByIdentifier( const QString& id )
00586 {
00587   QListViewItem *item;
00588   ResourceItem *i = 0;
00589   for( item = mListView->firstChild(); item; item = item->itemBelow() ) {
00590     i = static_cast<ResourceItem *>( item );
00591     if ( i->resourceIdentifier() == id )
00592        return i;
00593   }
00594   return 0;
00595 }
00596 
00597 
00598 void ResourceView::contextMenuRequested ( QListViewItem *i,
00599                                           const QPoint &pos, int )
00600 {
00601   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00602   ResourceItem *item = static_cast<ResourceItem *>( i );
00603 
00604   QPopupMenu *menu = new QPopupMenu( this );
00605   connect( menu, SIGNAL( aboutToHide() ), menu, SLOT( deleteLater() ) );
00606   if ( item ) {
00607     int reloadId = menu->insertItem( i18n("Re&load"), this,
00608                                      SLOT( reloadResource() ) );
00609     menu->setItemEnabled( reloadId, item->resource()->isActive() );
00610     int saveId = menu->insertItem( i18n("&Save"), this,
00611                                    SLOT( saveResource() ) );
00612     menu->setItemEnabled( saveId, item->resource()->isActive() );
00613     menu->insertSeparator();
00614 
00615     menu->insertItem( i18n("Show &Info"), this, SLOT( showInfo() ) );
00616     //FIXME: This is better on the resource dialog
00617     if ( KOPrefs::instance()->agendaViewColors() != KOPrefs::CategoryOnly ) {
00618       QPopupMenu *assignMenu= new QPopupMenu( menu );
00619       assignMenu->insertItem( i18n( "&Assign Color" ), this, SLOT( assignColor() ) );
00620       if ( item->resourceColor().isValid() )
00621         assignMenu->insertItem( i18n( "&Disable Color" ), this, SLOT( disableColor() ) );
00622       menu->insertItem( i18n( "Resources Colors" ), assignMenu );
00623     }
00624 
00625     menu->insertItem( i18n("&Edit..."), this, SLOT( editResource() ) );
00626     menu->insertItem( i18n("&Remove"), this, SLOT( removeResource() ) );
00627     if ( item->resource() != manager->standardResource() ) {
00628       menu->insertSeparator();
00629       menu->insertItem( i18n("Use as &Default Calendar"), this,
00630                         SLOT( setStandard() ) );
00631     }
00632 
00633     menu->insertSeparator();
00634  }
00635   menu->insertItem( i18n("&Add..."), this, SLOT( addResource() ) );
00636 
00637   menu->popup( pos );
00638 }
00639 
00640 void ResourceView::assignColor()
00641 {
00642   ResourceItem *item = currentItem();
00643   if ( !item )
00644     return;
00645   // A color without initialized is a color invalid
00646   QColor myColor;
00647   KCal::ResourceCalendar *cal = item->resource();
00648 
00649   QString identifier = cal->identifier();
00650   if ( item->isSubresource() )
00651     identifier = item->resourceIdentifier();
00652 
00653   QColor defaultColor =*KOPrefs::instance()->resourceColor( identifier );
00654 
00655   int result = KColorDialog::getColor( myColor,defaultColor);
00656 
00657   if ( result == KColorDialog::Accepted ) {
00658     KOPrefs::instance()->setResourceColor( identifier, myColor );
00659     item->setResourceColor( myColor );
00660     item->update();
00661     emitResourcesChanged();
00662   }
00663 }
00664 
00665 void ResourceView::disableColor()
00666 {
00667   ResourceItem *item = currentItem();
00668   if ( !item )
00669     return;
00670   QColor colorInvalid;
00671   KCal::ResourceCalendar *cal = item->resource();
00672   QString identifier = cal->identifier();
00673   if ( item->isSubresource() )
00674     identifier = item->resourceIdentifier();
00675   KOPrefs::instance()->setResourceColor( identifier, colorInvalid );
00676   item->setResourceColor( colorInvalid );
00677   item->update();
00678   emitResourcesChanged();
00679 }
00680 void ResourceView::showInfo()
00681 {
00682   ResourceItem *item = currentItem();
00683   if ( !item ) return;
00684 
00685   QString txt = "<qt>" + item->resource()->infoText() + "</qt>";
00686   KMessageBox::information( this, txt );
00687 }
00688 
00689 void ResourceView::reloadResource()
00690 {
00691   ResourceItem *item = currentItem();
00692   if ( !item ) return;
00693 
00694   ResourceCalendar *r = item->resource();
00695   r->load();
00696 }
00697 
00698 void ResourceView::saveResource()
00699 {
00700   ResourceItem *item = currentItem();
00701   if ( !item ) return;
00702 
00703   ResourceCalendar *r = item->resource();
00704   r->save();
00705 }
00706 
00707 void ResourceView::setStandard()
00708 {
00709   ResourceItem *item = currentItem();
00710   if ( !item ) return;
00711 
00712   ResourceCalendar *r = item->resource();
00713   KCal::CalendarResourceManager *manager = mCalendar->resourceManager();
00714   manager->setStandardResource( r );
00715   updateResourceList();
00716 }
00717 
00718 void ResourceView::updateResourceList()
00719 {
00720   QListViewItemIterator it( mListView );
00721   ResourceCalendar* stdRes = mCalendar->resourceManager()->standardResource();
00722   while ( it.current() ) {
00723     ResourceItem *item = static_cast<ResourceItem *>( it.current() );
00724     item->setStandardResource( item->resource() == stdRes );
00725     ++it;
00726   }
00727 }
00728 
00729 void ResourceView::showButtons( bool visible )
00730 {
00731   if ( visible ) {
00732     mAddButton->show();
00733     mDeleteButton->show();
00734     mEditButton->show();
00735   } else {
00736     mAddButton->hide();
00737     mDeleteButton->hide();
00738     mEditButton->hide();
00739   }
00740 }
00741 
00742 void ResourceView::requestClose( ResourceCalendar *r )
00743 {
00744   mResourcesToClose.append( r );
00745 }
00746 
00747 #include "resourceview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys