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