korganizer

resourceview.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
5  Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  As a special exception, permission is given to link this program
22  with any edition of TQt, and distribute the resulting executable,
23  without including the source code for TQt in the source distribution.
24 */
25 
26 #include "resourceview.h"
27 #include "koviewmanager.h"
28 #include "multiagendaview.h"
29 
30 #include <dcopref.h>
31 #include <kcolordialog.h>
32 #include <kdialog.h>
33 #include <tdelistview.h>
34 #include <tdelocale.h>
35 #include <kdebug.h>
36 #include <tdeglobal.h>
37 #include <tdemessagebox.h>
38 #include <kinputdialog.h>
39 #include <kiconloader.h>
40 #include <tderesources/resource.h>
41 #include <tderesources/configdialog.h>
43 #include <tdeconfig.h>
44 
45 #include <tqhbox.h>
46 #include <tqheader.h>
47 #include <tqlayout.h>
48 #include <tqlabel.h>
49 #include <tqpainter.h>
50 #include <tqpushbutton.h>
51 #include <tqpopupmenu.h>
52 #include <tqregexp.h>
53 #include <tqtooltip.h>
54 #include <tqwhatsthis.h>
55 
56 #include "koprefs.h"
57 
58 using namespace KCal;
59 
60 static TQString labelFromSubResName( ResourceCalendar *resource, const TQString &subRes )
61 {
62 
63  DCOPRef ref( "kmail", "KMailICalIface" );
64  DCOPReply reply = ref.call( "dimapAccounts" );
65  if ( !reply.isValid() ) {
66  kdDebug() << "DCOP Call dimapAccounts() failed " << endl;
67  return TQString();
68  }
69 
70  TQString label;
71  if ( (int)reply > 1 ) {
72  if( resource && !resource->resourceName().isEmpty() ) {
73  label = i18n( "My %1 (%2)" ).arg( subRes, resource->resourceName() );
74  } else {
75  label = i18n( "My %1" ).arg( subRes );
76  }
77  } else {
78  label = i18n( "My %1" ).arg( subRes );
79  }
80  return label;
81 }
82 
83 static TQString labelFromIdentifier( ResourceCalendar *resource, const TQString &identifier )
84 {
85  TQString subResLabel;
86  if ( identifier.contains( "/.INBOX.directory/" ) ) { // my subresource
87  TQString subResName = identifier;
88  subResName.remove( TQRegExp( "^.*/\\.INBOX\\.directory/" ) );
89  subResLabel = labelFromSubResName( resource, subResName );
90  }
91  return subResLabel;
92 }
93 
94 ResourceViewFactory::ResourceViewFactory( CalendarResources *calendar, CalendarView *view )
95  : mCalendar( calendar ), mCalendarView( view ), mResourceView( 0 )
96 {
97 }
98 
99 CalendarViewExtension *ResourceViewFactory::create( TQWidget *parent )
100 {
101  mResourceView = new ResourceView( mCalendar, mCalendarView, parent );
102 
103  TQObject::connect( mResourceView, TQ_SIGNAL( resourcesChanged() ),
104  mCalendarView, TQ_SLOT( resourcesChanged() ) );
105  TQObject::connect( mResourceView, TQ_SIGNAL( resourcesChanged() ),
106  mCalendarView, TQ_SLOT( updateCategories() ) );
107 
108  TQObject::connect( mCalendar,
109  TQ_SIGNAL( signalResourceAdded( ResourceCalendar * ) ),
110  mResourceView,
111  TQ_SLOT( addResourceItem( ResourceCalendar * ) ) );
112  TQObject::connect( mCalendar,
113  TQ_SIGNAL( signalResourceModified( ResourceCalendar * ) ),
114  mResourceView,
115  TQ_SLOT( updateResourceItem( ResourceCalendar * ) ) );
116  TQObject::connect( mCalendar, TQ_SIGNAL( signalResourceAdded( ResourceCalendar * ) ),
117  mCalendarView, TQ_SLOT( updateCategories() ) );
118  TQObject::connect( mCalendar, TQ_SIGNAL( signalResourceModified( ResourceCalendar * ) ),
119  mCalendarView, TQ_SLOT( updateCategories() ) );
120 
121  return mResourceView;
122 }
123 
124 ResourceView *ResourceViewFactory::resourceView() const
125 {
126  return mResourceView;
127 }
128 
129 ResourceItem::ResourceItem( ResourceCalendar *resource, ResourceView *view,
130  TDEListView *parent )
131  : TQCheckListItem( parent, resource->resourceName(), CheckBox ),
132  mResource( resource ), mResourceView( view ), mBlockStateChange( false ),
133  mIsSubresource( false ), mResourceIdentifier( TQString() ),
134  mSubItemsCreated( false ), mIsStandardResource( false )
135 {
136  mResourceColor = TQColor();
137  setGuiState();
138 
139  if ( mResource->isActive() ) {
140  createSubresourceItems();
141  }
142 }
143 
144 void ResourceItem::createSubresourceItems()
145 {
146  const TQStringList subresources = mResource->subresources();
147  if ( !subresources.isEmpty() ) {
148  setOpen( true );
149  setExpandable( true );
150  // This resource has subresources
151  TQStringList::ConstIterator it;
152  for ( it=subresources.begin(); it!=subresources.end(); ++it ) {
153  TQString text = labelFromIdentifier( mResource, *it );
154  if ( text.isEmpty() ) {
155  text = mResource->labelForSubresource( *it );
156  }
157  ResourceItem *item = new ResourceItem( mResource, *it, text, mResourceView, this );
158  TQColor resourceColor = *KOPrefs::instance()->resourceColor( *it );
159  item->setResourceColor( resourceColor );
160  item->update();
161  }
162  }
163  mSubItemsCreated = true;
164 }
165 
166 ResourceItem::ResourceItem( ResourceCalendar *resource, const TQString &identifier,
167  const TQString &label, ResourceView *view, ResourceItem *parent )
168  : TQCheckListItem( parent, label, CheckBox ), mResource( resource ),
169  mResourceView( view ), mBlockStateChange( false ), mIsSubresource( true ),
170  mSubItemsCreated( false ), mIsStandardResource( false )
171 {
172  mResourceColor = TQColor();
173  mResourceIdentifier = identifier;
174  setGuiState();
175 }
176 
177 void ResourceItem::setGuiState()
178 {
179  mBlockStateChange = true;
180  if ( mIsSubresource )
181  setOn( mResource->subresourceActive( mResourceIdentifier ) );
182  else
183  setOn( mResource->isActive() );
184  mBlockStateChange = false;
185 }
186 
187 void ResourceItem::stateChange( bool active )
188 {
189  if ( mBlockStateChange ) return;
190 
191  if ( mIsSubresource ) {
192  mResource->setSubresourceActive( mResourceIdentifier, active );
193  } else {
194  if ( active ) {
195  if ( mResource->load() ) {
196  mResource->setActive( true );
197  if ( !mSubItemsCreated )
198  createSubresourceItems();
199  }
200  } else {
201  // mResourceView->requestClose must be called before mResource->save() because
202  // save causes closeResource do be called.
203  mResourceView->requestClose( mResource );
204  if ( mResource->save() ) {
205  mResource->setActive( false );
206  }
207  }
208 
209  setOpen( mResource->isActive() && childCount() > 0 );
210 
211  setGuiState();
212  }
213 
214  mResourceView->emitResourcesChanged();
215 }
216 
217 void ResourceItem::update()
218 {
219  setGuiState();
220 }
221 
222 void ResourceItem::setResourceColor(TQColor& color)
223 {
224  if ( color.isValid() ) {
225  if ( mResourceColor != color ) {
226  TQPixmap px(height()-4,height()-4);
227  mResourceColor = color;
228  px.fill(color);
229  setPixmap(0,px);
230  }
231  } else {
232  mResourceColor = color ;
233  setPixmap(0,0);
234  }
235 }
236 
237 void ResourceItem::setStandardResource( bool std )
238 {
239  if ( mIsStandardResource != std ) {
240  mIsStandardResource = std;
241  repaint();
242  }
243 }
244 
245 void ResourceItem::paintCell(TQPainter *p, const TQColorGroup &cg,
246  int column, int width, int alignment)
247 {
248  TQFont oldFont = p->font();
249  TQFont newFont = oldFont;
250  newFont.setBold( mIsStandardResource && !mIsSubresource );
251  p->setFont( newFont );
252  TQCheckListItem::paintCell( p, cg, column, width, alignment );
253  p->setFont( oldFont );
254 /* TQColorGroup _cg = cg;
255  if(!mResource) return;
256  _cg.setColor(TQColorGroup::Base, getTextColor(mResourceColor));*/
257 }
258 
259 
260 ResourceView::ResourceView( CalendarResources *calendar,
261  CalendarView *view, TQWidget *parent, const char *name )
262  : CalendarViewExtension( parent, name ), mCalendar( calendar ), mCalendarView( view )
263 {
264  TQBoxLayout *topLayout = new TQVBoxLayout( this, 0, KDialog::spacingHint() );
265 
266  TQHBoxLayout *buttonBox = new TQHBoxLayout();
267  buttonBox->setSpacing( KDialog::spacingHint() );
268  topLayout->addLayout( buttonBox );
269 
270  TQLabel *calLabel = new TQLabel( i18n( "Calendar" ), this );
271  buttonBox->addWidget( calLabel );
272  buttonBox->addStretch( 1 );
273 
274  mAddButton = new TQPushButton( this, "add" );
275  mAddButton->setIconSet( SmallIconSet( "add" ) );
276  buttonBox->addWidget( mAddButton );
277  TQToolTip::add( mAddButton, i18n( "Add calendar" ) );
278  TQWhatsThis::add( mAddButton,
279  i18n( "<qt><p>Press this button to add a resource to "
280  "KOrganizer.</p>"
281  "<p>Events, journal entries and to-dos are retrieved "
282  "and stored on resources. Available "
283  "resources include groupware servers, local files, "
284  "journal entries as blogs on a server, etc... </p>"
285  "<p>If you have more than one active resource, "
286  "when creating incidents you will either automatically "
287  "use the default resource or be prompted "
288  "to select the resource to use.</p></qt>" ) );
289  mEditButton = new TQPushButton( this, "edit" );
290  mEditButton->setIconSet( SmallIconSet( "edit" ) );
291  buttonBox->addWidget( mEditButton );
292  TQToolTip::add( mEditButton, i18n( "Edit calendar settings" ) );
293  TQWhatsThis::add( mEditButton,
294  i18n( "Press this button to edit the resource currently "
295  "selected on the KOrganizer resources list above." ) );
296  mDeleteButton = new TQPushButton( this, "del" );
297  mDeleteButton->setIconSet( SmallIconSet( "remove" ) );
298  buttonBox->addWidget( mDeleteButton );
299  TQToolTip::add( mDeleteButton, i18n( "Remove calendar" ) );
300  TQWhatsThis::add( mDeleteButton,
301  i18n( "Press this button to delete the resource currently "
302  "selected on the KOrganizer resources list above." ) );
303  mDeleteButton->setDisabled( true );
304  mEditButton->setDisabled( true );
305 
306  mListView = new TDEListView( this );
307  mListView->header()->hide();
308  TQWhatsThis::add( mListView,
309  i18n( "<qt><p>Select on this list the active KOrganizer "
310  "resources. Check the resource box to make it "
311  "active. Press the \"Add...\" button below to add new "
312  "resources to the list.</p>"
313  "<p>Events, journal entries and to-dos are retrieved "
314  "and stored on resources. Available "
315  "resources include groupware servers, local files, "
316  "journal entries as blogs on a server, etc...</p>"
317  "<p>If you have more than one active resource, "
318  "when creating incidents you will either automatically "
319  "use the default resource or be prompted "
320  "to select the resource to use.</p></qt>" ) );
321  mListView->addColumn( i18n("Calendar") );
322  mListView->setResizeMode( TQListView::LastColumn );
323  topLayout->addWidget( mListView );
324 
325  connect( mListView, TQ_SIGNAL( clicked( TQListViewItem * ) ),
326  TQ_SLOT( currentChanged( TQListViewItem * ) ) );
327  connect( mAddButton, TQ_SIGNAL( clicked() ), TQ_SLOT( addResource() ) );
328  connect( mDeleteButton, TQ_SIGNAL( clicked() ), TQ_SLOT( removeResource() ) );
329  connect( mEditButton, TQ_SIGNAL( clicked() ), TQ_SLOT( editResource() ) );
330  connect( mListView, TQ_SIGNAL( doubleClicked ( TQListViewItem *, const TQPoint &,
331  int ) ),
332  TQ_SLOT( editResource() ) );
333  connect( mListView, TQ_SIGNAL( contextMenuRequested ( TQListViewItem *,
334  const TQPoint &, int ) ),
335  TQ_SLOT( contextMenuRequested( TQListViewItem *, const TQPoint &,
336  int ) ) );
337 
338  updateView();
339 }
340 
341 ResourceView::~ResourceView()
342 {
343 }
344 
345 void ResourceView::updateView()
346 {
347  mListView->clear();
348 
349  CalendarResourceManager *manager = mCalendar->resourceManager();
350 
351  CalendarResourceManager::Iterator it;
352  for( it = manager->begin(); it != manager->end(); ++it ) {
353  addResourceItem( *it );
354  }
355 }
356 
357 void ResourceView::emitResourcesChanged()
358 {
359  mCalendar->resourceManager()->writeConfig();
360  emit resourcesChanged();
361 }
362 
363 void ResourceView::addResource()
364 {
365  bool ok = false;
366  CalendarResourceManager *manager = mCalendar->resourceManager();
367  ResourceItem *item = static_cast<ResourceItem*>( mListView->selectedItem() );
368  if ( item && ( item->isSubresource() || item->resource()->canHaveSubresources() ) ) {
369  const TQString folderName =
370  KInputDialog::getText( i18n( "Add Subresource" ),
371  i18n( "Please enter a name for the new subresource" ), TQString(),
372  &ok, this );
373  if ( !ok )
374  return;
375  const TQString parentId = item->isSubresource() ? item->resourceIdentifier() : TQString:: null;
376  if ( !item->resource()->addSubresource( folderName, parentId ) ) {
377  KMessageBox::error(
378  this,
379  i18n( "<qt>Unable to create subresource <b>%1</b>.</qt>" ).arg( folderName ) );
380  }
381  return;
382  }
383 
384  TQStringList types = manager->resourceTypeNames();
385  TQStringList descs = manager->resourceTypeDescriptions();
386  TQString desc =
387  KInputDialog::getItem( i18n( "Resource Configuration" ),
388  i18n( "Please select type of the new resource:" ),
389  descs, 0, false, &ok, this );
390  if ( !ok ) {
391  return;
392  }
393 
394  TQString type = types[ descs.findIndex( desc ) ];
395 
396  // Create new resource
397  ResourceCalendar *resource = manager->createResource( type );
398  if( !resource ) {
399  KMessageBox::error(
400  this,
401  i18n( "<qt>Unable to create resource of type <b>%1</b>.</qt>" ).arg( type ) );
402  return;
403  }
404 
405  KRES::ConfigDialog *dlg =
406  new KRES::ConfigDialog( this, TQString( "calendar" ), resource, "KRES::ConfigDialog" );
407 
408  bool success = true;
409  if ( !dlg || !dlg->exec() )
410  success = false;
411 
412  if ( success ) {
413  resource->setTimeZoneId( KOPrefs::instance()->mTimeZoneId );
414  if ( resource->isActive() && ( !resource->open() || !resource->load() ) ) {
415  // ### There is a resourceLoadError() signal declared in ResourceCalendar
416  // but no subclass seems to make use of it. We could do better.
417  KMessageBox::error( this, i18n("Unable to create the resource.").arg( type ) );
418  success = false;
419  }
420  }
421 
422  if ( success ) {
423  manager->add( resource );
424  // we have to call resourceAdded manually, because for in-process changes
425  // the dcop signals are not connected, so the resource's signals would not
426  // be connected otherwise
427  mCalendar->resourceAdded( resource );
428  }
429 
430  if ( !success )
431  delete resource;
432 
433  delete dlg;
434 
435  //### maybe only do this if ( success )
436  emitResourcesChanged();
437 }
438 
439 void ResourceView::addResourceItem( ResourceCalendar *resource )
440 {
441 
442  ResourceItem *item = new ResourceItem( resource, this, mListView );
443 
444  // assign a color, but only if this is a resource that actually
445  // hold items at top level
446  if ( !resource->canHaveSubresources() || resource->subresources().isEmpty() ) {
447  TQColor resourceColor = *KOPrefs::instance()->resourceColor(resource->identifier());
448  item->setResourceColor(resourceColor);
449  item->update();
450  }
451 
452  connect( resource, TQ_SIGNAL( signalSubresourceAdded( ResourceCalendar *,
453  const TQString &,
454  const TQString &,
455  const TQString & ) ),
456  TQ_SLOT( slotSubresourceAdded( ResourceCalendar *, const TQString &,
457  const TQString &, const TQString & ) ) );
458 
459  connect( resource, TQ_SIGNAL( signalSubresourceRemoved( ResourceCalendar *,
460  const TQString &,
461  const TQString & ) ),
462  TQ_SLOT( slotSubresourceRemoved( ResourceCalendar *, const TQString &,
463  const TQString & ) ) );
464 
465  connect( resource, TQ_SIGNAL( resourceSaved( ResourceCalendar * ) ),
466  TQ_SLOT( closeResource( ResourceCalendar * ) ) );
467 
468  updateResourceList();
469  emit resourcesChanged();
470 }
471 
472 // Add a new entry
473 void ResourceView::slotSubresourceAdded( ResourceCalendar *resource,
474  const TQString &type,
475  const TQString &identifier,
476  const TQString &label )
477 {
478  Q_UNUSED( type );
479 
480  TQListViewItem *lvitem = mListView->findItem( resource->resourceName(), 0 );
481  if ( !lvitem )
482  // Not found
483  return;
484 
485  if ( findItemByIdentifier( identifier ) ) return;
486 
487  TQString text = labelFromIdentifier( resource, identifier );
488  if ( text.isEmpty() ) {
489  text = label;
490  }
491  ResourceItem *item = static_cast<ResourceItem *>( lvitem );
492  ResourceItem *newItem = new ResourceItem( resource, identifier, text, this, item );
493  TQColor resourceColor = *KOPrefs::instance()->resourceColor( identifier );
494  newItem->setResourceColor( resourceColor );
495 }
496 
497 // Remove an entry
498 void ResourceView::slotSubresourceRemoved( ResourceCalendar *resource,
499  const TQString &type,
500  const TQString &identifier )
501 {
502  Q_UNUSED( resource );
503  Q_UNUSED( type );
504 
505  delete findItemByIdentifier( identifier );
506  emit resourcesChanged();
507 }
508 
509 void ResourceView::closeResource( ResourceCalendar *resource )
510 {
511  if ( mResourcesToClose.find( resource ) >= 0 ) {
512  resource->close();
513  mResourcesToClose.remove( resource );
514  }
515 }
516 
517 void ResourceView::updateResourceItem( ResourceCalendar *resource )
518 {
519  ResourceItem *item = findItem( resource );
520  if ( item ) {
521  item->update();
522  }
523 }
524 
525 ResourceItem *ResourceView::currentItem()
526 {
527  TQListViewItem *item = mListView->currentItem();
528  ResourceItem *rItem = static_cast<ResourceItem *>( item );
529  return rItem;
530 }
531 
532 void ResourceView::removeResource()
533 {
534  ResourceItem *item = currentItem();
535  if ( !item ) return;
536 
537  // Do not allow a non-subresource folder to be removed if it is the standard resource.
538  if ( !item->isSubresource() ) {
539  if ( item->resource() == mCalendar->resourceManager()->standardResource() ) {
540  KMessageBox::sorry(
541  this,
542  i18n( "<qt>You may not delete your standard calendar resource.<p>"
543  "You can change the standard calendar resource in the "
544  "Trinity Control Center using the TDE Resource settings under the "
545  "TDE Components area.</qt>" ) );
546  return;
547  }
548  }
549 
550  TQString moreInfo;
551  if ( item->resource()->type() == "imap" || item->resource()->type() == "scalix" ) {
552  moreInfo = i18n( "This is a groupware folder so you can always re-subscribe to the folder "
553  "later as you desire." );
554  } else {
555  moreInfo = i18n( "The contents will not be removed so you can always re-add this calendar "
556  "later as you desire." );
557  }
558 
559  int km =
560  KMessageBox::warningContinueCancel(
561  this,
562  i18n( "<qt>Do you really want to remove the calendar <b>%1</b>?<p><b>Note:</b> %2</qt>" ).
563  arg( item->text( 0 ), moreInfo ),
564  "", KGuiItem( i18n( "&Remove" ) ) );
565  if ( km == KMessageBox::Cancel ) {
566  return;
567  }
568 
569  if ( item->isSubresource() ) {
570  if ( !item->resource()->removeSubresource( item->resourceIdentifier() ) )
571  KMessageBox::sorry(
572  this,
573  i18n ("<qt>Failed to remove the subresource <b>%1</b>. The "
574  "reason could be that it is a built-in one which cannot "
575  "be removed, or that the removal of the underlying storage "
576  "folder failed.</qt>").arg( item->resource()->name() ) );
577  return;
578  } else {
579  mCalendar->resourceManager()->remove( item->resource() );
580  }
581  mListView->takeItem( item );
582  delete item;
583 
584  updateResourceList();
585  emit resourcesChanged();
586 }
587 
588 void ResourceView::editResource()
589 {
590  bool ok = false;
591  ResourceItem *item = currentItem();
592  if (!item) return;
593  ResourceCalendar *resource = item->resource();
594 
595  if ( item->isSubresource() ) {
596  if ( resource->type() == "imap" || resource->type() == "scalix" ) {
597  TQString identifier = item->resourceIdentifier();
598  if ( !identifier.contains( "/.INBOX.directory/" ) ) {
599  KMessageBox::sorry(
600  this,
601  i18n( "Cannot rename someone else's calendar folder." ) );
602  return;
603  }
604 
605  TQString oldSubResourceName = identifier;
606  oldSubResourceName.remove( TQRegExp( "^.*/\\.INBOX\\.directory/" ) );
607  TQString newSubResourceName =
608  KInputDialog::getText(
609  i18n( "Rename Subresource" ),
610  i18n( "<qt>Enter a new name for the subresource<p>"
611  "<b>Note:</b> the new name will take affect after the next sync.</qt>" ),
612  oldSubResourceName, &ok, this );
613  if ( !ok ) {
614  return;
615  }
616 
617  DCOPRef ref( "kmail", "KMailICalIface" );
618  DCOPReply reply = ref.call( "changeResourceUIName", identifier, newSubResourceName );
619  if ( !reply.isValid() ) {
620  KMessageBox::sorry(
621  this,
622  i18n( "Communication with KMail failed when attempting to change the folder name." ) );
623  return;
624  }
625 
626  item->setText( 0, labelFromSubResName( resource, newSubResourceName ) );
627 
628  KOrg::BaseView *cV = mCalendarView->viewManager()->currentView();
629  if ( cV && cV == mCalendarView->viewManager()->multiAgendaView() ) {
630  mCalendarView->viewManager()->multiAgendaView()->deSelectAgendaView();
631  }
632  } else {
633  KMessageBox::sorry(
634  this,
635  i18n ("<qt>Cannot edit the subresource <b>%1</b>.</qt>").arg( item->resource()->name() ) );
636  }
637  } else {
638  KRES::ConfigDialog dlg( this, TQString("calendar"), resource, "KRES::ConfigDialog" );
639 
640  if ( dlg.exec() ) {
641  item->setText( 0, resource->resourceName() );
642  mCalendar->resourceManager()->change( resource );
643  }
644  }
645  emitResourcesChanged();
646 }
647 
648 void ResourceView::currentChanged( TQListViewItem *lvitem )
649 {
650  ResourceItem *item = currentItem();
651  if ( !lvitem || item->isSubresource() ) {
652  mDeleteButton->setEnabled( false );
653  mEditButton->setEnabled( false );
654  } else {
655  mDeleteButton->setEnabled( true );
656  mEditButton->setEnabled( true );
657  }
658 }
659 
660 ResourceItem *ResourceView::findItem( ResourceCalendar *resource )
661 {
662  TQListViewItem *lvitem;
663  ResourceItem *item = 0;
664  for( lvitem = mListView->firstChild(); lvitem; lvitem = lvitem->nextSibling() ) {
665  item = static_cast<ResourceItem *>( lvitem );
666  if ( item->resource() == resource ) break;
667  }
668  return item;
669 }
670 
671 ResourceItem *ResourceView::findItemByIdentifier( const TQString &identifier )
672 {
673  TQListViewItem *lvitem;
674  ResourceItem *item = 0;
675  for ( lvitem = mListView->firstChild(); lvitem; lvitem = lvitem->itemBelow() ) {
676  item = static_cast<ResourceItem *>( lvitem );
677  if ( item->resourceIdentifier() == identifier )
678  return item;
679  }
680  return 0;
681 }
682 
683 void ResourceView::contextMenuRequested ( TQListViewItem *lvitem, const TQPoint &pos, int )
684 {
685  CalendarResourceManager *manager = mCalendar->resourceManager();
686  ResourceItem *item = static_cast<ResourceItem *>( lvitem );
687 
688  TQPopupMenu *menu = new TQPopupMenu( this );
689  connect( menu, TQ_SIGNAL( aboutToHide() ), menu, TQ_SLOT( deleteLater() ) );
690  if ( item ) {
691  int reloadId = menu->insertItem( i18n("Re&load"), this,
692  TQ_SLOT( reloadResource() ) );
693  menu->setItemEnabled( reloadId, item->resource()->isActive() );
694  int saveId = menu->insertItem( i18n("&Save"), this,
695  TQ_SLOT( saveResource() ) );
696  menu->setItemEnabled( saveId, item->resource()->isActive() );
697  menu->insertSeparator();
698 
699  menu->insertItem( i18n("Show &Info"), this, TQ_SLOT( showInfo() ) );
700  //FIXME: This is better on the resource dialog
701  if ( KOPrefs::instance()->agendaViewColors() != KOPrefs::CategoryOnly ) {
702  TQPopupMenu *assignMenu= new TQPopupMenu( menu );
703  assignMenu->insertItem( i18n( "&Assign Color" ), this, TQ_SLOT( assignColor() ) );
704  if ( item->resourceColor().isValid() )
705  assignMenu->insertItem( i18n( "&Disable Color" ), this, TQ_SLOT( disableColor() ) );
706  menu->insertItem( i18n( "Resources Colors" ), assignMenu );
707  }
708 
709  if ( item->isSubresource() &&
710  ( item->resource()->type() == "imap" || item->resource()->type() == "scalix" ) ) {
711  if ( item->resourceIdentifier().contains( "/.INBOX.directory/" ) ) {
712  menu->insertItem( i18n("&Rename..."), this, TQ_SLOT( editResource() ) );
713  }
714  } else {
715  menu->insertItem( i18n("&Edit..."), this, TQ_SLOT( editResource() ) );
716  }
717  menu->insertItem( i18n("&Remove"), this, TQ_SLOT( removeResource() ) );
718  if ( item->resource() != manager->standardResource() ) {
719  menu->insertSeparator();
720  menu->insertItem( i18n("Use as &Default Calendar"), this,
721  TQ_SLOT( setStandard() ) );
722  }
723 
724  menu->insertSeparator();
725  }
726  menu->insertItem( i18n("&Add..."), this, TQ_SLOT( addResource() ) );
727 
728  menu->popup( pos );
729 }
730 
731 void ResourceView::assignColor()
732 {
733  ResourceItem *item = currentItem();
734  if ( !item )
735  return;
736  // A color without initialized is a color invalid
737  TQColor myColor;
738  ResourceCalendar *cal = item->resource();
739 
740  TQString identifier = cal->identifier();
741  if ( item->isSubresource() )
742  identifier = item->resourceIdentifier();
743 
744  TQColor defaultColor =*KOPrefs::instance()->resourceColor( identifier );
745 
746  int result = KColorDialog::getColor( myColor,defaultColor);
747 
748  if ( result == KColorDialog::Accepted ) {
749  KOPrefs::instance()->setResourceColor( identifier, myColor );
750  item->setResourceColor( myColor );
751  item->update();
752  emitResourcesChanged();
753  }
754 }
755 
756 void ResourceView::disableColor()
757 {
758  ResourceItem *item = currentItem();
759  if ( !item ) {
760  return;
761  }
762 
763  TQColor colorInvalid;
764  ResourceCalendar *cal = item->resource();
765  TQString identifier = cal->identifier();
766  if ( item->isSubresource() ) {
767  identifier = item->resourceIdentifier();
768  }
769  KOPrefs::instance()->setResourceColor( identifier, colorInvalid );
770  item->setResourceColor( colorInvalid );
771  item->update();
772  emitResourcesChanged();
773 }
774 void ResourceView::showInfo()
775 {
776  ResourceItem *item = currentItem();
777  if ( !item ) return;
778 
779  TQString identifier;
780  if ( item->isSubresource() ) {
781  identifier = "<p>" + item->resourceIdentifier();
782  }
783 
784  TQString txt = "<qt>" + item->resource()->infoText() + identifier + "</qt>";
785  KMessageBox::information( this, txt );
786 }
787 
788 void ResourceView::reloadResource()
789 {
790  ResourceItem *item = currentItem();
791  if ( !item ) return;
792 
793  ResourceCalendar *resource = item->resource();
794  resource->load();
795 }
796 
797 void ResourceView::saveResource()
798 {
799  ResourceItem *item = currentItem();
800  if ( !item ) return;
801 
802  ResourceCalendar *resource = item->resource();
803  resource->save();
804 }
805 
806 void ResourceView::setStandard()
807 {
808  ResourceItem *item = currentItem();
809  if ( !item ) return;
810 
811  ResourceCalendar *resource = item->resource();
812  CalendarResourceManager *manager = mCalendar->resourceManager();
813  manager->setStandardResource( resource );
814  updateResourceList();
815 }
816 
817 void ResourceView::updateResourceList()
818 {
819  TQListViewItemIterator it( mListView );
820  ResourceCalendar* stdRes = mCalendar->resourceManager()->standardResource();
821  while ( it.current() ) {
822  ResourceItem *item = static_cast<ResourceItem *>( it.current() );
823  item->setStandardResource( item->resource() == stdRes );
824  ++it;
825  }
826 }
827 
828 void ResourceView::showButtons( bool visible )
829 {
830  if ( visible ) {
831  mAddButton->show();
832  mDeleteButton->show();
833  mEditButton->show();
834  } else {
835  mAddButton->hide();
836  mDeleteButton->hide();
837  mEditButton->hide();
838  }
839 }
840 
841 void ResourceView::requestClose( ResourceCalendar *r )
842 {
843  mResourcesToClose.append( r );
844 }
845 
846 #include "resourceview.moc"
This is the main calendar widget.
Definition: calendarview.h:82
CalendarResourceManager * resourceManager() const
void resourceAdded(ResourceCalendar *resource)
virtual TQStringList subresources() const
bool save(Incidence *incidence=0)
virtual bool canHaveSubresources() const
virtual void setTimeZoneId(const TQString &timeZoneId)=0
This class provides an interface for all views being displayed within the main calendar view.
Definition: baseview.h:60
This class provides a view of calendar resources.
Definition: resourceview.h:96
bool view(TQWidget *parent, Attachment *attachment)