26 #include "resourceview.h"
27 #include "koviewmanager.h"
28 #include "multiagendaview.h"
31 #include <kcolordialog.h>
33 #include <tdelistview.h>
34 #include <tdelocale.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>
49 #include <tqpainter.h>
50 #include <tqpushbutton.h>
51 #include <tqpopupmenu.h>
53 #include <tqtooltip.h>
54 #include <tqwhatsthis.h>
60 static TQString labelFromSubResName(
ResourceCalendar *resource,
const TQString &subRes )
63 DCOPRef ref(
"kmail",
"KMailICalIface" );
64 DCOPReply reply = ref.call(
"dimapAccounts" );
65 if ( !reply.isValid() ) {
66 kdDebug() <<
"DCOP Call dimapAccounts() failed " << endl;
71 if ( (
int)reply > 1 ) {
72 if( resource && !resource->resourceName().isEmpty() ) {
73 label = i18n(
"My %1 (%2)" ).arg( subRes, resource->resourceName() );
75 label = i18n(
"My %1" ).arg( subRes );
78 label = i18n(
"My %1" ).arg( subRes );
83 static TQString labelFromIdentifier(
ResourceCalendar *resource,
const TQString &identifier )
86 if ( identifier.contains(
"/.INBOX.directory/" ) ) {
87 TQString subResName = identifier;
88 subResName.remove( TQRegExp(
"^.*/\\.INBOX\\.directory/" ) );
89 subResLabel = labelFromSubResName( resource, subResName );
95 : mCalendar( calendar ), mCalendarView(
view ), mResourceView( 0 )
99 CalendarViewExtension *ResourceViewFactory::create( TQWidget *parent )
101 mResourceView =
new ResourceView( mCalendar, mCalendarView, parent );
103 TQObject::connect( mResourceView, TQ_SIGNAL( resourcesChanged() ),
104 mCalendarView, TQ_SLOT( resourcesChanged() ) );
105 TQObject::connect( mResourceView, TQ_SIGNAL( resourcesChanged() ),
106 mCalendarView, TQ_SLOT( updateCategories() ) );
108 TQObject::connect( mCalendar,
112 TQObject::connect( mCalendar,
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() ) );
121 return mResourceView;
126 return mResourceView;
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 )
136 mResourceColor = TQColor();
139 if ( mResource->isActive() ) {
140 createSubresourceItems();
144 void ResourceItem::createSubresourceItems()
146 const TQStringList subresources = mResource->subresources();
147 if ( !subresources.isEmpty() ) {
149 setExpandable(
true );
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 );
157 ResourceItem *item =
new ResourceItem( mResource, *it, text, mResourceView,
this );
158 TQColor resourceColor = *KOPrefs::instance()->resourceColor( *it );
159 item->setResourceColor( resourceColor );
163 mSubItemsCreated =
true;
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 )
172 mResourceColor = TQColor();
173 mResourceIdentifier = identifier;
177 void ResourceItem::setGuiState()
179 mBlockStateChange =
true;
180 if ( mIsSubresource )
181 setOn( mResource->subresourceActive( mResourceIdentifier ) );
183 setOn( mResource->isActive() );
184 mBlockStateChange =
false;
187 void ResourceItem::stateChange(
bool active )
189 if ( mBlockStateChange )
return;
191 if ( mIsSubresource ) {
192 mResource->setSubresourceActive( mResourceIdentifier, active );
195 if ( mResource->load() ) {
196 mResource->setActive(
true );
197 if ( !mSubItemsCreated )
198 createSubresourceItems();
203 mResourceView->requestClose( mResource );
204 if ( mResource->save() ) {
205 mResource->setActive(
false );
209 setOpen( mResource->isActive() && childCount() > 0 );
214 mResourceView->emitResourcesChanged();
217 void ResourceItem::update()
222 void ResourceItem::setResourceColor(TQColor& color)
224 if ( color.isValid() ) {
225 if ( mResourceColor != color ) {
226 TQPixmap px(height()-4,height()-4);
227 mResourceColor = color;
232 mResourceColor = color ;
237 void ResourceItem::setStandardResource(
bool std )
239 if ( mIsStandardResource != std ) {
240 mIsStandardResource = std;
245 void ResourceItem::paintCell(TQPainter *p,
const TQColorGroup &cg,
246 int column,
int width,
int alignment)
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 );
261 CalendarView *view, TQWidget *parent,
const char *name )
262 : CalendarViewExtension( parent, name ), mCalendar( calendar ), mCalendarView(
view )
264 TQBoxLayout *topLayout =
new TQVBoxLayout(
this, 0, KDialog::spacingHint() );
266 TQHBoxLayout *buttonBox =
new TQHBoxLayout();
267 buttonBox->setSpacing( KDialog::spacingHint() );
268 topLayout->addLayout( buttonBox );
270 TQLabel *calLabel =
new TQLabel( i18n(
"Calendar" ),
this );
271 buttonBox->addWidget( calLabel );
272 buttonBox->addStretch( 1 );
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 "
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 );
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 );
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 &,
332 TQ_SLOT( editResource() ) );
333 connect( mListView, TQ_SIGNAL( contextMenuRequested ( TQListViewItem *,
334 const TQPoint &,
int ) ),
335 TQ_SLOT( contextMenuRequested( TQListViewItem *,
const TQPoint &,
341 ResourceView::~ResourceView()
345 void ResourceView::updateView()
351 CalendarResourceManager::Iterator it;
352 for( it = manager->begin(); it != manager->end(); ++it ) {
353 addResourceItem( *it );
357 void ResourceView::emitResourcesChanged()
360 emit resourcesChanged();
363 void ResourceView::addResource()
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(),
375 const TQString parentId = item->isSubresource() ? item->resourceIdentifier() : TQString:: null;
376 if ( !item->resource()->addSubresource( folderName, parentId ) ) {
379 i18n(
"<qt>Unable to create subresource <b>%1</b>.</qt>" ).arg( folderName ) );
384 TQStringList types = manager->resourceTypeNames();
385 TQStringList descs = manager->resourceTypeDescriptions();
387 KInputDialog::getItem( i18n(
"Resource Configuration" ),
388 i18n(
"Please select type of the new resource:" ),
389 descs, 0,
false, &ok,
this );
394 TQString type = types[ descs.findIndex( desc ) ];
401 i18n(
"<qt>Unable to create resource of type <b>%1</b>.</qt>" ).arg( type ) );
405 KRES::ConfigDialog *dlg =
406 new KRES::ConfigDialog(
this, TQString(
"calendar" ), resource,
"KRES::ConfigDialog" );
409 if ( !dlg || !dlg->exec() )
414 if ( resource->isActive() && ( !resource->open() || !resource->
load() ) ) {
417 KMessageBox::error(
this, i18n(
"Unable to create the resource.").arg( type ) );
423 manager->add( resource );
436 emitResourcesChanged();
442 ResourceItem *item =
new ResourceItem( resource,
this, mListView );
447 TQColor resourceColor = *KOPrefs::instance()->resourceColor(resource->identifier());
448 item->setResourceColor(resourceColor);
455 const TQString & ) ),
457 const TQString &,
const TQString & ) ) );
461 const TQString & ) ),
463 const TQString & ) ) );
468 updateResourceList();
469 emit resourcesChanged();
474 const TQString &type,
475 const TQString &identifier,
476 const TQString &label )
480 TQListViewItem *lvitem = mListView->findItem( resource->resourceName(), 0 );
485 if ( findItemByIdentifier( identifier ) )
return;
487 TQString text = labelFromIdentifier( resource, identifier );
488 if ( text.isEmpty() ) {
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 );
499 const TQString &type,
500 const TQString &identifier )
502 Q_UNUSED( resource );
505 delete findItemByIdentifier( identifier );
506 emit resourcesChanged();
511 if ( mResourcesToClose.find( resource ) >= 0 ) {
513 mResourcesToClose.remove( resource );
519 ResourceItem *item = findItem( resource );
525 ResourceItem *ResourceView::currentItem()
527 TQListViewItem *item = mListView->currentItem();
528 ResourceItem *rItem =
static_cast<ResourceItem *
>( item );
532 void ResourceView::removeResource()
534 ResourceItem *item = currentItem();
538 if ( !item->isSubresource() ) {
539 if ( item->resource() == mCalendar->
resourceManager()->standardResource() ) {
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>" ) );
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." );
555 moreInfo = i18n(
"The contents will not be removed so you can always re-add this calendar "
556 "later as you desire." );
560 KMessageBox::warningContinueCancel(
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 ) {
569 if ( item->isSubresource() ) {
570 if ( !item->resource()->removeSubresource( item->resourceIdentifier() ) )
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() ) );
581 mListView->takeItem( item );
584 updateResourceList();
585 emit resourcesChanged();
588 void ResourceView::editResource()
591 ResourceItem *item = currentItem();
595 if ( item->isSubresource() ) {
596 if ( resource->type() ==
"imap" || resource->type() ==
"scalix" ) {
597 TQString identifier = item->resourceIdentifier();
598 if ( !identifier.contains(
"/.INBOX.directory/" ) ) {
601 i18n(
"Cannot rename someone else's calendar folder." ) );
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 );
617 DCOPRef ref(
"kmail",
"KMailICalIface" );
618 DCOPReply reply = ref.call(
"changeResourceUIName", identifier, newSubResourceName );
619 if ( !reply.isValid() ) {
622 i18n(
"Communication with KMail failed when attempting to change the folder name." ) );
626 item->setText( 0, labelFromSubResName( resource, newSubResourceName ) );
629 if ( cV && cV == mCalendarView->viewManager()->multiAgendaView() ) {
630 mCalendarView->viewManager()->multiAgendaView()->deSelectAgendaView();
635 i18n (
"<qt>Cannot edit the subresource <b>%1</b>.</qt>").arg( item->resource()->name() ) );
638 KRES::ConfigDialog dlg(
this, TQString(
"calendar"), resource,
"KRES::ConfigDialog" );
641 item->setText( 0, resource->resourceName() );
645 emitResourcesChanged();
648 void ResourceView::currentChanged( TQListViewItem *lvitem )
650 ResourceItem *item = currentItem();
651 if ( !lvitem || item->isSubresource() ) {
652 mDeleteButton->setEnabled(
false );
653 mEditButton->setEnabled(
false );
655 mDeleteButton->setEnabled(
true );
656 mEditButton->setEnabled(
true );
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;
671 ResourceItem *ResourceView::findItemByIdentifier(
const TQString &identifier )
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 )
683 void ResourceView::contextMenuRequested ( TQListViewItem *lvitem,
const TQPoint &pos,
int )
686 ResourceItem *item =
static_cast<ResourceItem *
>( lvitem );
688 TQPopupMenu *menu =
new TQPopupMenu(
this );
689 connect( menu, TQ_SIGNAL( aboutToHide() ), menu, TQ_SLOT( deleteLater() ) );
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();
699 menu->insertItem( i18n(
"Show &Info"),
this, TQ_SLOT( showInfo() ) );
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 );
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() ) );
715 menu->insertItem( i18n(
"&Edit..."),
this, TQ_SLOT( editResource() ) );
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() ) );
724 menu->insertSeparator();
726 menu->insertItem( i18n(
"&Add..."),
this, TQ_SLOT( addResource() ) );
731 void ResourceView::assignColor()
733 ResourceItem *item = currentItem();
740 TQString identifier = cal->identifier();
741 if ( item->isSubresource() )
742 identifier = item->resourceIdentifier();
744 TQColor defaultColor =*KOPrefs::instance()->resourceColor( identifier );
746 int result = KColorDialog::getColor( myColor,defaultColor);
748 if ( result == KColorDialog::Accepted ) {
749 KOPrefs::instance()->setResourceColor( identifier, myColor );
750 item->setResourceColor( myColor );
752 emitResourcesChanged();
756 void ResourceView::disableColor()
758 ResourceItem *item = currentItem();
763 TQColor colorInvalid;
765 TQString identifier = cal->identifier();
766 if ( item->isSubresource() ) {
767 identifier = item->resourceIdentifier();
769 KOPrefs::instance()->setResourceColor( identifier, colorInvalid );
770 item->setResourceColor( colorInvalid );
772 emitResourcesChanged();
774 void ResourceView::showInfo()
776 ResourceItem *item = currentItem();
780 if ( item->isSubresource() ) {
781 identifier =
"<p>" + item->resourceIdentifier();
784 TQString txt =
"<qt>" + item->resource()->infoText() + identifier +
"</qt>";
785 KMessageBox::information(
this, txt );
788 void ResourceView::reloadResource()
790 ResourceItem *item = currentItem();
797 void ResourceView::saveResource()
799 ResourceItem *item = currentItem();
806 void ResourceView::setStandard()
808 ResourceItem *item = currentItem();
813 manager->setStandardResource( resource );
814 updateResourceList();
817 void ResourceView::updateResourceList()
819 TQListViewItemIterator it( mListView );
821 while ( it.current() ) {
822 ResourceItem *item =
static_cast<ResourceItem *
>( it.current() );
823 item->setStandardResource( item->resource() == stdRes );
828 void ResourceView::showButtons(
bool visible )
832 mDeleteButton->show();
836 mDeleteButton->hide();
843 mResourcesToClose.append( r );
846 #include "resourceview.moc"
This is the main calendar widget.
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.
This class provides a view of calendar resources.
bool view(TQWidget *parent, Attachment *attachment)