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 #include "koviewmanager.h"
00028 #include "multiagendaview.h"
00029 
00030 #include <dcopref.h>
00031 #include <kcolordialog.h>
00032 #include <kdialog.h>
00033 #include <klistview.h>
00034 #include <klocale.h>
00035 #include <kdebug.h>
00036 #include <kglobal.h>
00037 #include <kmessagebox.h>
00038 #include <kinputdialog.h>
00039 #include <kiconloader.h>
00040 #include <kresources/resource.h>
00041 #include <kresources/configdialog.h>
00042 #include <libkcal/calendarresources.h>
00043 #include <kconfig.h>
00044 
00045 #include <qhbox.h>
00046 #include <qheader.h>
00047 #include <qlayout.h>
00048 #include <qlabel.h>
00049 #include <qpainter.h>
00050 #include <qpushbutton.h>
00051 #include <qpopupmenu.h>
00052 #include <qregexp.h>
00053 #include <qtooltip.h>
00054 #include <qwhatsthis.h>
00055 
00056 #include "koprefs.h"
00057 
00058 using namespace KCal;
00059 
00060 static QString scrubDirectory( const QString &subRes )
00061 {
00062   QString nsubRes = subRes;
00063 
00064   // first, replace leading ".foo.directory" with "foo"
00065   if ( subRes.contains( QRegExp( "^\\..*\\.directory" ) ) ) {
00066     nsubRes.remove( ".directory" );
00067     nsubRes.remove( 0, 1 ); // remove leading '.'
00068   }
00069 
00070   // now replace "/." with "/"  (needed for sub-sublevel-directories)
00071   nsubRes.replace( QRegExp( "/\\." ),  "/" );
00072 
00073   // cleaning complete
00074   return nsubRes;
00075 }
00076 
00077 static QString labelFromSubResName( const QString &identifier, const QString &subRes )
00078 {
00079   DCOPRef ref( "kmail", "KMailICalIface" );
00080   DCOPReply reply = ref.call( "dimapAccounts" );
00081   if ( !reply.isValid() ) {
00082     kdDebug() << "DCOP Call dimapAccounts() failed " << endl;
00083     return QString();
00084   }
00085 
00086   QString label, account;
00087   if ( (int)reply > 1 ) {
00088     reply = ref.call( "dimapFolderAccountName", identifier );
00089     if ( reply.isValid() ) {
00090       const QString a = reply;
00091       account = a;
00092     }
00093   }
00094 
00095   if ( account.isEmpty() ) {
00096     label = i18n( "My %1" ).arg( scrubDirectory( subRes ) );
00097   } else {
00098     label = i18n( "My %1 (%2)" ).arg( subRes, account );
00099   }
00100 
00101   return label;
00102 }
00103 
00104 static QString labelFromIdentifier( const QString &identifier )
00105 {
00106   QString subResLabel;
00107   if ( identifier.contains( "/.INBOX.directory/" ) ) { // my subresource
00108     QString subResName = identifier;
00109     subResName.remove( QRegExp( "^.*/\\.INBOX\\.directory/" ) );
00110     subResLabel = labelFromSubResName( identifier, subResName );
00111   }
00112   return subResLabel;
00113 }
00114 
00115 ResourceViewFactory::ResourceViewFactory( CalendarResources *calendar, CalendarView *view )
00116   : mCalendar( calendar ), mCalendarView( view ), mResourceView( 0 )
00117 {
00118 }
00119 
00120 CalendarViewExtension *ResourceViewFactory::create( QWidget *parent )
00121 {
00122   mResourceView = new ResourceView( mCalendar, mCalendarView, parent );
00123 
00124   QObject::connect( mResourceView, SIGNAL( resourcesChanged() ),
00125                     mCalendarView, SLOT( resourcesChanged() ) );
00126   QObject::connect( mResourceView, SIGNAL( resourcesChanged() ),
00127                     mCalendarView, SLOT( updateCategories() ) );
00128 
00129   QObject::connect( mCalendar,
00130                     SIGNAL( signalResourceAdded( ResourceCalendar * ) ),
00131                     mResourceView,
00132                     SLOT( addResourceItem( ResourceCalendar * ) ) );
00133   QObject::connect( mCalendar,
00134                     SIGNAL( signalResourceModified( ResourceCalendar * ) ),
00135                     mResourceView,
00136                     SLOT( updateResourceItem( ResourceCalendar * ) ) );
00137   QObject::connect( mCalendar, SIGNAL( signalResourceAdded( ResourceCalendar * ) ),
00138                     mCalendarView, SLOT( updateCategories() ) );
00139   QObject::connect( mCalendar, SIGNAL( signalResourceModified( ResourceCalendar * ) ),
00140                     mCalendarView, SLOT( updateCategories() ) );
00141 
00142   return mResourceView;
00143 }
00144 
00145 ResourceView *ResourceViewFactory::resourceView() const
00146 {
00147   return mResourceView;
00148 }
00149 
00150 ResourceItem::ResourceItem( ResourceCalendar *resource, ResourceView *view,
00151                             KListView *parent )
00152   : QCheckListItem( parent, resource->resourceName(), CheckBox ),
00153     mResource( resource ), mResourceView( view ), mBlockStateChange( false ),
00154     mIsSubresource( false ), mResourceIdentifier( QString::null ),
00155     mSubItemsCreated( false ), mIsStandardResource( false )
00156 {
00157   mResourceColor = QColor();
00158   setGuiState();
00159 
00160   if ( mResource->isActive() ) {
00161     createSubresourceItems();
00162   }
00163 }
00164 
00165 void ResourceItem::createSubresourceItems()
00166 {
00167   const QStringList subresources = mResource->subresources();
00168   if ( !subresources.isEmpty() ) {
00169     setOpen( true );
00170     setExpandable( true );
00171     // This resource has subresources
00172     QStringList::ConstIterator it;
00173     for ( it = subresources.begin(); it != subresources.end(); ++it ) {
00174       QString text = labelFromIdentifier( *it );
00175       if ( text.isEmpty() ) {
00176         text = mResource->labelForSubresource( *it );
00177       }
00178       ResourceItem *item = new ResourceItem( mResource, *it, text, mResourceView, this );
00179       QColor resourceColor = *KOPrefs::instance()->resourceColor( *it );
00180       item->setResourceColor( resourceColor );
00181       item->update();
00182     }
00183   }
00184   mSubItemsCreated = true;
00185 }
00186 
00187 ResourceItem::ResourceItem( ResourceCalendar *resource, const QString &identifier,
00188                             const QString &label, ResourceView *view, ResourceItem *parent )
00189   : QCheckListItem( parent, label, CheckBox ), mResource( resource ),
00190     mResourceView( view ), mBlockStateChange( false ), mIsSubresource( true ),
00191     mSubItemsCreated( false ), mIsStandardResource( false )
00192 {
00193   mResourceColor = QColor();
00194   mResourceIdentifier = identifier;
00195   setGuiState();
00196 }
00197 
00198 void ResourceItem::setGuiState()
00199 {
00200   mBlockStateChange = true;
00201   if ( mIsSubresource )
00202     setOn( mResource->subresourceActive( mResourceIdentifier ) );
00203   else
00204     setOn( mResource->isActive() );
00205   mBlockStateChange = false;
00206 }
00207 
00208 void ResourceItem::stateChange( bool active )
00209 {
00210   if ( mBlockStateChange ) return;
00211 
00212   if ( mIsSubresource ) {
00213     mResource->setSubresourceActive( mResourceIdentifier, active );
00214   } else {
00215     if ( active ) {
00216       if ( mResource->load() ) {
00217         mResource->setActive( true );
00218         if ( !mSubItemsCreated )
00219           createSubresourceItems();
00220       }
00221     } else {
00222       // mResourceView->requestClose must be called before mResource->save() because
00223       // save causes closeResource do be called.
00224       mResourceView->requestClose( mResource );
00225       if ( mResource->save() ) {
00226         mResource->setActive( false );
00227       }
00228     }
00229 
00230     setOpen( mResource->isActive() && childCount() > 0 );
00231 
00232     setGuiState();
00233   }
00234 
00235   mResourceView->emitResourcesChanged();
00236 }
00237 
00238 void ResourceItem::update()
00239 {
00240   setGuiState();
00241 }
00242 
00243 void ResourceItem::setResourceColor(QColor& color)
00244 {
00245   if ( color.isValid() ) {
00246     if ( mResourceColor != color ) {
00247       QPixmap px(height()-4,height()-4);
00248       mResourceColor = color;
00249       px.fill(color);
00250       setPixmap(0,px);
00251     }
00252   } else {
00253     mResourceColor = color ;
00254     setPixmap(0,0);
00255   }
00256 }
00257 
00258 void ResourceItem::setStandardResource( bool std )
00259 {
00260   if ( mIsStandardResource != std ) {
00261     mIsStandardResource = std;
00262     repaint();
00263   }
00264 }
00265 
00266 void ResourceItem::paintCell(QPainter *p, const QColorGroup &cg,
00267       int column, int width, int alignment)
00268 {
00269   QFont oldFont = p->font();
00270   QFont newFont = oldFont;
00271   newFont.setBold( mIsStandardResource && !mIsSubresource );
00272   p->setFont( newFont );
00273   QCheckListItem::paintCell( p, cg, column, width, alignment );
00274   p->setFont( oldFont );
00275 /*  QColorGroup _cg = cg;
00276   if(!mResource) return;
00277   _cg.setColor(QColorGroup::Base, getTextColor(mResourceColor));*/
00278 }
00279 
00280 
00281 ResourceView::ResourceView( CalendarResources *calendar,
00282                             CalendarView *view, QWidget *parent, const char *name )
00283   : CalendarViewExtension( parent, name ), mCalendar( calendar ), mCalendarView( view )
00284 {
00285   QBoxLayout *topLayout = new QVBoxLayout( this, 0, KDialog::spacingHint() );
00286 
00287   QHBoxLayout *buttonBox = new QHBoxLayout();
00288   buttonBox->setSpacing( KDialog::spacingHint() );
00289   topLayout->addLayout( buttonBox );
00290 
00291   QLabel *calLabel = new QLabel( i18n( "Calendar" ), this );
00292   buttonBox->addWidget( calLabel );
00293   buttonBox->addStretch( 1 );
00294 
00295   mAddButton = new QPushButton( this, "add" );
00296   mAddButton->setIconSet( SmallIconSet( "add" ) );
00297   buttonBox->addWidget( mAddButton );
00298   QToolTip::add( mAddButton, i18n( "Add calendar" ) );
00299   QWhatsThis::add( mAddButton,
00300                    i18n( "<qt><p>Press this button to add a resource to "
00301                          "KOrganizer.</p>"
00302                          "<p>Events, journal entries and to-dos are retrieved "
00303                          "and stored on resources. Available "
00304                          "resources include groupware servers, local files, "
00305                          "journal entries as blogs on a server, etc... </p>"
00306                          "<p>If you have more than one active resource, "
00307                          "when creating incidents you will either automatically "
00308                          "use the default resource or be prompted "
00309                          "to select the resource to use.</p></qt>" ) );
00310   mEditButton = new QPushButton( this, "edit" );
00311   mEditButton->setIconSet( SmallIconSet( "edit" ) );
00312   buttonBox->addWidget( mEditButton );
00313   QToolTip::add( mEditButton, i18n( "Edit calendar settings" ) );
00314   QWhatsThis::add( mEditButton,
00315                    i18n( "Press this button to edit the resource currently "
00316                          "selected on the KOrganizer resources list above." ) );
00317   mDeleteButton = new QPushButton( this, "del" );
00318   mDeleteButton->setIconSet( SmallIconSet( "remove" ) );
00319   buttonBox->addWidget( mDeleteButton );
00320   QToolTip::add( mDeleteButton, i18n( "Remove calendar" ) );
00321   QWhatsThis::add( mDeleteButton,
00322                    i18n( "Press this button to delete the resource currently "
00323                          "selected on the KOrganizer resources list above." ) );
00324   mDeleteButton->setDisabled( true );
00325   mEditButton->setDisabled( true );
00326 
00327   mListView = new KListView( this );
00328   mListView->header()->hide();
00329   QWhatsThis::add( mListView,
00330                    i18n( "<qt><p>Select on this list the active KOrganizer "
00331                          "resources. Check the resource box to make it "
00332                          "active. Press the \"Add...\" button below to add new "
00333                          "resources to the list.</p>"
00334                          "<p>Events, journal entries and to-dos are retrieved "
00335                          "and stored on resources. Available "
00336                          "resources include groupware servers, local files, "
00337                          "journal entries as blogs on a server, etc...</p>"
00338                          "<p>If you have more than one active resource, "
00339                          "when creating incidents you will either automatically "
00340                          "use the default resource or be prompted "
00341                          "to select the resource to use.</p></qt>" ) );
00342   mListView->addColumn( i18n("Calendar") );
00343   mListView->setResizeMode( QListView::LastColumn );
00344   topLayout->addWidget( mListView );
00345 
00346   connect( mListView, SIGNAL(clicked(QListViewItem *)),
00347            SLOT(currentChanged(QListViewItem *)) );
00348   connect( mAddButton, SIGNAL(clicked()), SLOT(addResource()) );
00349   connect( mDeleteButton, SIGNAL(clicked()), SLOT(removeResource()) );
00350   connect( mEditButton, SIGNAL(clicked()), SLOT(editResource()) );
00351   connect( mListView, SIGNAL(doubleClicked(QListViewItem *,const QPoint &,int)),
00352            SLOT(editResource()) );
00353   connect( mListView, SIGNAL(contextMenuRequested(QListViewItem *,const QPoint &,int)),
00354            SLOT(contextMenuRequested(QListViewItem *,const QPoint &,int)) );
00355 
00356   updateView();
00357 }
00358 
00359 ResourceView::~ResourceView()
00360 {
00361 }
00362 
00363 void ResourceView::updateView()
00364 {
00365   mListView->clear();
00366 
00367   CalendarResourceManager *manager = mCalendar->resourceManager();
00368 
00369   CalendarResourceManager::Iterator it;
00370   for( it = manager->begin(); it != manager->end(); ++it ) {
00371     addResourceItem( *it );
00372   }
00373 }
00374 
00375 void ResourceView::emitResourcesChanged()
00376 {
00377   mCalendar->resourceManager()->writeConfig();
00378   emit resourcesChanged();
00379 }
00380 
00381 void ResourceView::addResource()
00382 {
00383   bool ok = false;
00384   CalendarResourceManager *manager = mCalendar->resourceManager();
00385   ResourceItem *item = static_cast<ResourceItem*>( mListView->selectedItem() );
00386   if ( item && ( item->isSubresource() || item->resource()->canHaveSubresources() ) ) {
00387     const QString folderName =
00388       KInputDialog::getText( i18n( "Add Subresource" ),
00389                              i18n( "Please enter a name for the new subresource" ), QString::null,
00390                              &ok, this );
00391     if ( !ok )
00392       return;
00393     const QString parentId = item->isSubresource() ? item->resourceIdentifier() : QString:: null;
00394     if ( !item->resource()->addSubresource( folderName, parentId ) ) {
00395       KMessageBox::error(
00396         this,
00397         i18n( "<qt>Unable to create subresource <b>%1</b>.</qt>" ).arg( folderName ) );
00398     }
00399     return;
00400   }
00401 
00402   QStringList types = manager->resourceTypeNames();
00403   QStringList descs = manager->resourceTypeDescriptions();
00404   QString desc =
00405     KInputDialog::getItem( i18n( "Resource Configuration" ),
00406                            i18n( "Please select type of the new resource:" ),
00407                            descs, 0, false, &ok, this );
00408   if ( !ok ) {
00409     return;
00410   }
00411 
00412   QString type = types[ descs.findIndex( desc ) ];
00413 
00414   // Create new resource
00415   ResourceCalendar *resource = manager->createResource( type );
00416   if( !resource ) {
00417     KMessageBox::error(
00418       this,
00419       i18n( "<qt>Unable to create resource of type <b>%1</b>.</qt>" ).arg( type ) );
00420     return;
00421   }
00422 
00423   KRES::ConfigDialog *dlg =
00424     new KRES::ConfigDialog( this, QString( "calendar" ), resource, "KRES::ConfigDialog" );
00425 
00426   bool success = true;
00427   if ( !dlg || !dlg->exec() )
00428     success = false;
00429 
00430   if ( success ) {
00431     resource->setTimeZoneId( KOPrefs::instance()->mTimeZoneId );
00432     if ( resource->isActive() && ( !resource->open() || !resource->load() ) ) {
00433       // ### There is a resourceLoadError() signal declared in ResourceCalendar
00434       //     but no subclass seems to make use of it. We could do better.
00435       KMessageBox::error( this, i18n("Unable to create the resource.").arg( type ) );
00436       success = false;
00437     }
00438   }
00439 
00440   if ( success ) {
00441     manager->add( resource );
00442     // we have to call resourceAdded manually, because for in-process changes
00443     // the dcop signals are not connected, so the resource's signals would not
00444     // be connected otherwise
00445     mCalendar->resourceAdded( resource );
00446   }
00447 
00448   if ( !success )
00449     delete resource;
00450 
00451   delete dlg;
00452 
00453   //### maybe only do this if ( success )
00454   emitResourcesChanged();
00455 }
00456 
00457 void ResourceView::addResourceItem( ResourceCalendar *resource )
00458 {
00459 
00460   ResourceItem *item = new ResourceItem( resource, this, mListView );
00461 
00462   // assign a color, but only if this is a resource that actually
00463   // hold items at top level
00464   if ( !resource->canHaveSubresources() || resource->subresources().isEmpty() ) {
00465       QColor resourceColor = *KOPrefs::instance()->resourceColor(resource->identifier());
00466       item->setResourceColor(resourceColor);
00467       item->update();
00468   }
00469 
00470   connect( resource, SIGNAL( signalSubresourceAdded( ResourceCalendar *,
00471                                                      const QString &,
00472                                                      const QString &,
00473                                                      const QString & ) ),
00474            SLOT( slotSubresourceAdded( ResourceCalendar *, const QString &,
00475                                        const QString &, const QString & ) ) );
00476 
00477   connect( resource, SIGNAL( signalSubresourceRemoved( ResourceCalendar *,
00478                                                        const QString &,
00479                                                        const QString & ) ),
00480            SLOT( slotSubresourceRemoved( ResourceCalendar *, const QString &,
00481                                          const QString & ) ) );
00482 
00483   connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ),
00484            SLOT( closeResource( ResourceCalendar * ) ) );
00485 
00486   updateResourceList();
00487   emit resourcesChanged();
00488 }
00489 
00490 // Add a new entry
00491 void ResourceView::slotSubresourceAdded( ResourceCalendar *resource,
00492                                          const QString &type,
00493                                          const QString &identifier,
00494                                          const QString &label )
00495 {
00496   Q_UNUSED( type );
00497 
00498   QListViewItem *lvitem = mListView->findItem( resource->resourceName(), 0 );
00499   if ( !lvitem )
00500     // Not found
00501     return;
00502 
00503   if ( findItemByIdentifier( identifier ) ) return;
00504 
00505   QString text = labelFromIdentifier( identifier );
00506   if ( text.isEmpty() ) {
00507     text = label;
00508   }
00509   ResourceItem *item = static_cast<ResourceItem *>( lvitem );
00510   ResourceItem *newItem = new ResourceItem( resource, identifier, text, this, item );
00511   QColor resourceColor = *KOPrefs::instance()->resourceColor( identifier );
00512   newItem->setResourceColor( resourceColor );
00513 }
00514 
00515 // Remove an entry
00516 void ResourceView::slotSubresourceRemoved( ResourceCalendar *resource,
00517                                            const QString &type,
00518                                            const QString &identifier )
00519 {
00520   Q_UNUSED( resource );
00521   Q_UNUSED( type );
00522 
00523   delete findItemByIdentifier( identifier );
00524   emit resourcesChanged();
00525 }
00526 
00527 void ResourceView::closeResource( ResourceCalendar *resource )
00528 {
00529   if ( mResourcesToClose.find( resource ) >= 0 ) {
00530     resource->close();
00531     mResourcesToClose.remove( resource );
00532   }
00533 }
00534 
00535 void ResourceView::updateResourceItem( ResourceCalendar *resource )
00536 {
00537   ResourceItem *item = findItem( resource );
00538   if ( item ) {
00539     item->update();
00540   }
00541 }
00542 
00543 ResourceItem *ResourceView::currentItem()
00544 {
00545   QListViewItem *item = mListView->currentItem();
00546   ResourceItem *rItem = static_cast<ResourceItem *>( item );
00547   return rItem;
00548 }
00549 
00550 void ResourceView::removeResource()
00551 {
00552   ResourceItem *item = currentItem();
00553   if ( !item ) return;
00554 
00555   // Do not allow a non-subresource folder to be removed if it is the standard resource.
00556   if ( !item->isSubresource() ) {
00557     if ( item->resource() == mCalendar->resourceManager()->standardResource() ) {
00558       KMessageBox::sorry(
00559         this,
00560         i18n( "<qt>You may not delete your standard calendar resource.<p>"
00561               "You can change the standard calendar resource in the "
00562               "KDE Control Center using the KDE Resource settings under the "
00563               "KDE Components area.</qt>" ) );
00564       return;
00565     }
00566   }
00567 
00568   QString moreInfo;
00569   if ( item->resource()->type() == "imap" || item->resource()->type() == "scalix" ) {
00570     moreInfo = i18n( "This is a groupware folder so you can always re-subscribe to the folder "
00571                      "later as you desire." );
00572   } else {
00573     moreInfo = i18n( "The contents will not be removed so you can always re-add this calendar "
00574                      "later as you desire." );
00575   }
00576 
00577   int km =
00578     KMessageBox::warningContinueCancel(
00579       this,
00580       i18n( "<qt>Do you really want to remove the calendar <b>%1</b>?<p><b>Note:</b> %2</qt>" ).
00581       arg( item->text( 0 ), moreInfo ),
00582       "", KGuiItem( i18n( "&Remove" ) ) );
00583   if ( km == KMessageBox::Cancel ) {
00584     return;
00585   }
00586 
00587   if ( item->isSubresource() ) {
00588     if ( !item->resource()->removeSubresource( item->resourceIdentifier() ) )
00589       KMessageBox::sorry(
00590         this,
00591         i18n ("<qt>Failed to remove the subresource <b>%1</b>. The "
00592               "reason could be that it is a built-in one which cannot "
00593               "be removed, or that the removal of the underlying storage "
00594               "folder failed.</qt>").arg( item->resource()->name() ) );
00595       return;
00596   } else {
00597     mCalendar->resourceManager()->remove( item->resource() );
00598   }
00599     mListView->takeItem( item );
00600     delete item;
00601 
00602   updateResourceList();
00603   emit resourcesChanged();
00604 }
00605 
00606 void ResourceView::editResource()
00607 {
00608   bool ok = false;
00609   ResourceItem *item = currentItem();
00610   if (!item) return;
00611   ResourceCalendar *resource = item->resource();
00612 
00613   if ( item->isSubresource() ) {
00614     if ( resource->type() == "imap" || resource->type() == "scalix" ) {
00615       QString identifier = item->resourceIdentifier();
00616       if ( !identifier.contains( "/.INBOX.directory/" ) ) {
00617         KMessageBox::sorry(
00618           this,
00619           i18n( "Cannot rename someone else's calendar folder." ) );
00620         return;
00621       }
00622 
00623       QString oldSubResourceName = identifier;
00624       oldSubResourceName.remove( QRegExp( "^.*/\\.INBOX\\.directory/" ) );
00625       QString newSubResourceName =
00626         KInputDialog::getText(
00627           i18n( "Rename Subresource" ),
00628           i18n( "<qt>Enter a new name for the subresource<p>"
00629                 "<b>Note:</b> the new name will take affect after the next sync.</qt>" ),
00630           oldSubResourceName, &ok, this );
00631       if ( !ok ) {
00632         return;
00633       }
00634 
00635       DCOPRef ref( "kmail", "KMailICalIface" );
00636       DCOPReply reply = ref.call( "changeResourceUIName", identifier, newSubResourceName );
00637       if ( !reply.isValid() ) {
00638         KMessageBox::sorry(
00639           this,
00640           i18n( "Communication with KMail failed when attempting to change the folder name." ) );
00641         return;
00642       }
00643 
00644       item->setText( 0, labelFromSubResName( identifier, newSubResourceName ) );
00645 
00646       KOrg::BaseView *cV = mCalendarView->viewManager()->currentView();
00647       if ( cV && cV == mCalendarView->viewManager()->multiAgendaView() ) {
00648         mCalendarView->viewManager()->multiAgendaView()->deSelectAgendaView();
00649       }
00650     } else {
00651       KMessageBox::sorry(
00652         this,
00653         i18n ("<qt>Cannot edit the subresource <b>%1</b>.</qt>").arg( item->resource()->name() ) );
00654     }
00655   } else {
00656     KRES::ConfigDialog dlg( this, QString("calendar"), resource, "KRES::ConfigDialog" );
00657 
00658     if ( dlg.exec() ) {
00659       item->setText( 0, resource->resourceName() );
00660       mCalendar->resourceManager()->change( resource );
00661     }
00662   }
00663   emitResourcesChanged();
00664 }
00665 
00666 void ResourceView::currentChanged( QListViewItem *lvitem )
00667 {
00668   ResourceItem *item = currentItem();
00669   if ( !lvitem || item->isSubresource() ) {
00670     mDeleteButton->setEnabled( false );
00671     mEditButton->setEnabled( false );
00672   } else {
00673     mDeleteButton->setEnabled( true );
00674     mEditButton->setEnabled( true );
00675   }
00676 }
00677 
00678 ResourceItem *ResourceView::findItem( ResourceCalendar *resource )
00679 {
00680   QListViewItem *lvitem;
00681   ResourceItem *item = 0;
00682   for( lvitem = mListView->firstChild(); lvitem; lvitem = lvitem->nextSibling() ) {
00683     item = static_cast<ResourceItem *>( lvitem );
00684     if ( item->resource() == resource ) break;
00685   }
00686   return item;
00687 }
00688 
00689 ResourceItem *ResourceView::findItemByIdentifier( const QString &identifier )
00690 {
00691   QListViewItem *lvitem;
00692   ResourceItem *item = 0;
00693   for ( lvitem = mListView->firstChild(); lvitem; lvitem = lvitem->itemBelow() ) {
00694     item = static_cast<ResourceItem *>( lvitem );
00695     if ( item->resourceIdentifier() == identifier )
00696        return item;
00697   }
00698   return 0;
00699 }
00700 
00701 void ResourceView::contextMenuRequested ( QListViewItem *lvitem, const QPoint &pos, int )
00702 {
00703   CalendarResourceManager *manager = mCalendar->resourceManager();
00704   ResourceItem *item = static_cast<ResourceItem *>( lvitem );
00705 
00706   QPopupMenu *menu = new QPopupMenu( this );
00707   connect( menu, SIGNAL( aboutToHide() ), menu, SLOT( deleteLater() ) );
00708   if ( item ) {
00709     int reloadId = menu->insertItem( i18n("Re&load"), this,
00710                                      SLOT( reloadResource() ) );
00711     menu->setItemEnabled( reloadId, item->resource()->isActive() );
00712     int saveId = menu->insertItem( i18n("&Save"), this,
00713                                    SLOT( saveResource() ) );
00714     menu->setItemEnabled( saveId, item->resource()->isActive() );
00715     menu->insertSeparator();
00716 
00717     menu->insertItem( i18n("Show &Info"), this, SLOT( showInfo() ) );
00718     //FIXME: This is better on the resource dialog
00719     if ( KOPrefs::instance()->agendaViewColors() != KOPrefs::CategoryOnly ) {
00720       QPopupMenu *assignMenu= new QPopupMenu( menu );
00721       assignMenu->insertItem( i18n( "&Assign Color" ), this, SLOT( assignColor() ) );
00722       if ( item->resourceColor().isValid() )
00723         assignMenu->insertItem( i18n( "&Disable Color" ), this, SLOT( disableColor() ) );
00724       menu->insertItem( i18n( "Resources Colors" ), assignMenu );
00725     }
00726 
00727     if ( item->isSubresource() &&
00728          ( item->resource()->type() == "imap" || item->resource()->type() == "scalix" ) ) {
00729       if ( item->resourceIdentifier().contains( "/.INBOX.directory/" ) ) {
00730         menu->insertItem( i18n("&Rename..."), this, SLOT( editResource() ) );
00731       }
00732     } else {
00733       menu->insertItem( i18n("&Edit..."), this, SLOT( editResource() ) );
00734     }
00735     menu->insertItem( i18n("&Remove"), this, SLOT( removeResource() ) );
00736     if ( item->resource() != manager->standardResource() ) {
00737       menu->insertSeparator();
00738       menu->insertItem( i18n("Use as &Default Calendar"), this,
00739                         SLOT( setStandard() ) );
00740     }
00741 
00742     menu->insertSeparator();
00743  }
00744   menu->insertItem( i18n("&Add..."), this, SLOT( addResource() ) );
00745 
00746   menu->popup( pos );
00747 }
00748 
00749 void ResourceView::assignColor()
00750 {
00751   ResourceItem *item = currentItem();
00752   if ( !item )
00753     return;
00754   // A color without initialized is a color invalid
00755   QColor myColor;
00756   ResourceCalendar *cal = item->resource();
00757 
00758   QString identifier = cal->identifier();
00759   if ( item->isSubresource() )
00760     identifier = item->resourceIdentifier();
00761 
00762   QColor defaultColor =*KOPrefs::instance()->resourceColor( identifier );
00763 
00764   int result = KColorDialog::getColor( myColor,defaultColor);
00765 
00766   if ( result == KColorDialog::Accepted ) {
00767     KOPrefs::instance()->setResourceColor( identifier, myColor );
00768     item->setResourceColor( myColor );
00769     item->update();
00770     emitResourcesChanged();
00771   }
00772 }
00773 
00774 void ResourceView::disableColor()
00775 {
00776   ResourceItem *item = currentItem();
00777   if ( !item ) {
00778     return;
00779   }
00780 
00781   QColor colorInvalid;
00782   ResourceCalendar *cal = item->resource();
00783   QString identifier = cal->identifier();
00784   if ( item->isSubresource() ) {
00785     identifier = item->resourceIdentifier();
00786   }
00787   KOPrefs::instance()->setResourceColor( identifier, colorInvalid );
00788   item->setResourceColor( colorInvalid );
00789   item->update();
00790   emitResourcesChanged();
00791 }
00792 void ResourceView::showInfo()
00793 {
00794   ResourceItem *item = currentItem();
00795   if ( !item ) return;
00796 
00797   QString identifier;
00798   if ( item->isSubresource() ) {
00799     identifier = "<p>" + item->resourceIdentifier();
00800   }
00801 
00802   QString txt = "<qt>" + item->resource()->infoText() + identifier + "</qt>";
00803   KMessageBox::information( this, txt );
00804 }
00805 
00806 void ResourceView::reloadResource()
00807 {
00808   ResourceItem *item = currentItem();
00809   if ( !item ) return;
00810 
00811   ResourceCalendar *resource = item->resource();
00812   resource->load();
00813 }
00814 
00815 void ResourceView::saveResource()
00816 {
00817   ResourceItem *item = currentItem();
00818   if ( !item ) return;
00819 
00820   ResourceCalendar *resource = item->resource();
00821   resource->save();
00822 }
00823 
00824 void ResourceView::setStandard()
00825 {
00826   ResourceItem *item = currentItem();
00827   if ( !item ) return;
00828 
00829   ResourceCalendar *resource = item->resource();
00830   CalendarResourceManager *manager = mCalendar->resourceManager();
00831   manager->setStandardResource( resource );
00832   updateResourceList();
00833 }
00834 
00835 void ResourceView::updateResourceList()
00836 {
00837   QListViewItemIterator it( mListView );
00838   ResourceCalendar* stdRes = mCalendar->resourceManager()->standardResource();
00839   while ( it.current() ) {
00840     ResourceItem *item = static_cast<ResourceItem *>( it.current() );
00841     item->setStandardResource( item->resource() == stdRes );
00842     ++it;
00843   }
00844 }
00845 
00846 void ResourceView::showButtons( bool visible )
00847 {
00848   if ( visible ) {
00849     mAddButton->show();
00850     mDeleteButton->show();
00851     mEditButton->show();
00852   } else {
00853     mAddButton->hide();
00854     mDeleteButton->hide();
00855     mEditButton->hide();
00856   }
00857 }
00858 
00859 void ResourceView::requestClose( ResourceCalendar *r )
00860 {
00861   mResourcesToClose.append( r );
00862 }
00863 
00864 #include "resourceview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys