26 #include <tqwidgetstack.h>
28 #include <libtdepim/kvcarddrag.h>
29 #include <tdeabc/addressbook.h>
30 #include <tdeabc/vcardconverter.h>
31 #include <tdeactionclasses.h>
32 #include <tdeconfig.h>
34 #include <tdeversion.h>
35 #include <kiconloader.h>
36 #include <tdelocale.h>
37 #include <tdemessagebox.h>
38 #include <tdemultipledrag.h>
43 #include "addviewdialog.h"
44 #include "addresseeutil.h"
46 #include "filtereditdialog.h"
47 #include "filterselectionwidget.h"
50 #include "viewmanager.h"
52 ViewManager::ViewManager( KAB::Core *core, TQWidget *parent,
const char *name )
53 : TQWidget( parent, name ), mCore( core ), mActiveView( 0 ),
54 mFilterSelectionWidget( 0 )
59 mViewDict.setAutoDelete(
true );
61 createViewFactories();
64 ViewManager::~ViewManager()
67 mViewFactoryDict.clear();
70 void ViewManager::restoreSettings()
72 mViewNameList = KABPrefs::instance()->viewNames();
73 TQString activeViewName = KABPrefs::instance()->currentView();
75 mActionSelectView->setItems( mViewNameList );
79 mFilterSelectionWidget->setItems( filterNames() );
80 mFilterSelectionWidget->setCurrentItem( KABPrefs::instance()->currentFilter() );
84 TQDictIterator<KAddressBookView> it( mViewDict );
85 for ( it.toFirst(); it.current(); ++it ) {
86 TDEConfigGroupSaver saver( mCore->config(), it.currentKey() );
87 it.current()->readConfig( mCore->config() );
90 setActiveView( activeViewName );
92 mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
95 void ViewManager::saveSettings()
97 TQDictIterator<KAddressBookView> it( mViewDict );
98 for ( it.toFirst(); it.current(); ++it ) {
99 TDEConfigGroupSaver saver( mCore->config(), it.currentKey() );
100 (*it)->writeConfig( mCore->config() );
104 KABPrefs::instance()->setCurrentFilter( mFilterSelectionWidget->currentItem() );
107 KABPrefs::instance()->setViewNames( mViewNameList );
110 KABPrefs::instance()->setCurrentView( mActiveView->caption() );
113 TQStringList ViewManager::selectedUids()
const
118 return TQStringList();
121 TQStringList ViewManager::selectedEmails()
const
126 return TQStringList();
129 TDEABC::Addressee::List ViewManager::selectedAddressees()
const
131 TDEABC::Addressee::List list;
133 const TQStringList uids = selectedUids();
134 TQStringList::ConstIterator it;
135 for ( it = uids.begin(); it != uids.end(); ++it ) {
136 TDEABC::Addressee addr = mCore->addressBook()->findByUid( *it );
137 if ( !addr.isEmpty() )
146 mFilterSelectionWidget = wdg;
149 TDEABC::Field *ViewManager::currentSortField()
const
157 TDEABC::Field::List ViewManager::viewFields()
const
164 return TDEABC::Field::List();
167 void ViewManager::setSelected(
const TQString &uid,
bool selected )
173 void ViewManager::setFirstSelected(
bool selected )
179 void ViewManager::unloadViews()
185 void ViewManager::setActiveView(
const TQString &name )
190 if ( mActiveView && ( mActiveView->caption() == name ) )
198 view = mViewDict.find( name );
202 TDEConfig *config = mCore->config();
203 TDEConfigGroupSaver saver( config, name );
204 TQString type = config->readEntry(
"Type",
"Table" );
206 kdDebug(5720) <<
"ViewManager::setActiveView: creating view - " << name << endl;
208 ViewFactory *factory = mViewFactoryDict.find( type );
210 view = factory->view( mCore, mViewWidgetStack );
213 view->setCaption( name );
214 mViewDict.insert( name, view );
215 mViewWidgetStack->addWidget( view );
219 connect( view, TQ_SIGNAL(
selected(
const TQString& ) ),
220 TQ_SIGNAL(
selected(
const TQString & ) ) );
221 connect( view, TQ_SIGNAL(
executed(
const TQString& ) ),
222 TQ_SIGNAL(
executed(
const TQString& ) ) );
224 connect( view, TQ_SIGNAL(
dropped( TQDropEvent* ) ),
225 TQ_SLOT(
dropped( TQDropEvent* ) ) );
234 mViewWidgetStack->raiseWidget( view );
239 mFilterSelectionWidget->setCurrentItem( 0 );
240 setActiveFilter( 0 );
242 setActiveFilter( mFilterSelectionWidget->currentItem() );
245 mFilterSelectionWidget->setCurrentItem( pos );
246 setActiveFilter( pos );
254 kdDebug(5720) <<
"ViewManager::setActiveView: unable to find view\n";
257 void ViewManager::refreshView(
const TQString &uid )
263 void ViewManager::editView()
268 ViewFactory *factory = mViewFactoryDict.find( mActiveView->
type() );
275 wdg = factory->configureWidget( mCore->addressBook(), 0 );
279 ViewConfigureDialog dlg( wdg, mActiveView->caption(),
this );
281 TDEConfigGroupSaver saver( mCore->config(), mActiveView->caption() );
282 dlg.restoreSettings( mCore->config() );
285 dlg.saveSettings( mCore->config() );
291 mFilterSelectionWidget->setCurrentItem( 0 );
292 setActiveFilter( 0 );
294 setActiveFilter( mFilterSelectionWidget->currentItem() );
297 mFilterSelectionWidget->setCurrentItem( pos );
298 setActiveFilter( pos );
307 void ViewManager::deleteView()
309 TQString text = i18n(
"<qt>Are you sure that you want to delete the view <b>%1</b>?</qt>" )
310 .arg( mActiveView->caption() );
311 TQString caption = i18n(
"Confirm Delete" );
313 if ( KMessageBox::warningContinueCancel(
this, text, caption, KGuiItem( i18n(
"&Delete"),
"edit-delete") ) == KMessageBox::Continue ) {
314 mViewNameList.remove( mActiveView->caption() );
317 TDEConfig *config = mCore->config();
318 config->deleteGroup( mActiveView->caption() );
320 mViewDict.remove( mActiveView->caption() );
325 mActionSelectView->setItems( mViewNameList );
326 if ( mViewNameList.count() > 0 ) {
327 mActionSelectView->setCurrentItem( 0 );
328 setActiveView( mViewNameList[ 0 ] );
330 mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
334 void ViewManager::addView()
338 if ( dialog.exec() ) {
339 TQString newName = dialog.viewName();
340 TQString type = dialog.viewType();
343 bool firstConflict =
true;
345 while ( mViewNameList.contains( newName ) > 0 ) {
346 if ( !firstConflict ) {
347 newName = newName.left( newName.length() - 4 );
348 firstConflict =
false;
351 newName = TQString(
"%1 <%2>" ).arg( newName ).arg( numTries );
356 mViewNameList.append( newName );
359 TDEConfig *config = mCore->config();
360 config->deleteGroup( newName );
361 TDEConfigGroupSaver saver( config, newName );
362 config->writeEntry(
"Type", type );
365 mActionSelectView->setItems( mViewNameList );
366 mActionSelectView->setCurrentItem( mViewNameList.findIndex( newName ) );
367 setActiveView( newName );
371 mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
375 void ViewManager::scrollUp()
378 mActiveView->scrollUp();
381 void ViewManager::scrollDown()
384 mActiveView->scrollDown();
387 void ViewManager::createViewFactories()
389 const TDETrader::OfferList plugins = TDETrader::self()->query(
"KAddressBook/View",
390 TQString(
"[X-TDE-KAddressBook-ViewPluginVersion] == %1" ).arg( KAB_VIEW_PLUGIN_VERSION ) );
391 TDETrader::OfferList::ConstIterator it;
392 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
393 if ( !(*it)->hasServiceType(
"KAddressBook/View" ) )
396 KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
399 kdDebug(5720) <<
"ViewManager::createViewFactories(): Factory creation failed" << endl;
403 ViewFactory *viewFactory =
static_cast<ViewFactory*
>( factory );
405 if ( !viewFactory ) {
406 kdDebug(5720) <<
"ViewManager::createViewFactories(): Cast failed" << endl;
410 mViewFactoryDict.insert( viewFactory->type(), viewFactory );
416 kdDebug(5720) <<
"ViewManager::dropped: got a drop event" << endl;
419 if ( e->source() ==
this )
422 TDEABC::Addressee::List list;
425 if ( KURLDrag::decode( e, urls) ) {
426 KURL::List::ConstIterator it = urls.begin();
427 int c = urls.count();
429 TQString questionString = i18n(
"Import one contact into your addressbook?",
"Import %n contacts into your addressbook?", c );
430 if ( KMessageBox::questionYesNo(
this, questionString, i18n(
"Import Contacts?" ), i18n(
"Import"), i18n(
"Do Not Import") ) == KMessageBox::Yes ) {
431 for ( ; it != urls.end(); ++it )
436 }
else if ( KVCardDrag::decode( e, list ) ) {
437 TDEABC::Addressee::List::ConstIterator it;
438 for ( it = list.begin(); it != list.end(); ++it ) {
439 TDEABC::Addressee a = mCore->addressBook()->findByUid( (*it).uid() );
441 mCore->addressBook()->insertAddressee( *it );
453 TDEABC::Addressee::List addrList;
454 const TQStringList uidList = selectedUids();
455 if ( uidList.isEmpty() )
458 kdDebug(5720) <<
"ViewManager::startDrag: starting to drag" << endl;
460 TQStringList::ConstIterator it;
461 for ( it = uidList.begin(); it != uidList.end(); ++it )
462 addrList.append( mCore->addressBook()->findByUid( *it ) );
464 KMultipleDrag *drag =
new KMultipleDrag(
this );
466 TDEABC::VCardConverter converter;
467 #if defined(KABC_VCARD_ENCODING_FIX)
468 TQCString vcards = converter.createVCardsRaw( addrList );
470 TQString vcards = converter.createVCards( addrList );
475 drag->addDragObject(
new KVCardDrag( vcards,
this ) );
479 if ( tempDir.status() == 0 ) {
481 if ( addrList.count() == 1 )
482 fileName = addrList[ 0 ].givenName() +
"_" + addrList[ 0 ].familyName() +
".vcf";
484 fileName =
"contacts.vcf";
486 TQFile tempFile( tempDir.name() +
"/" + fileName );
487 if ( tempFile.open( IO_WriteOnly ) ) {
488 #if defined(KABC_VCARD_ENCODING_FIX)
489 tempFile.writeBlock( vcards, vcards.length() );
491 tempFile.writeBlock( vcards.utf8() );
495 KURLDrag *urlDrag =
new KURLDrag( KURL( tempFile.name() ),
this );
496 drag->addDragObject( urlDrag );
500 drag->setPixmap( TDEGlobal::iconLoader()->loadIcon(
"x-office-address-book", TDEIcon::Desktop ) );
504 void ViewManager::setActiveFilter(
int index )
508 if ( ( index - 1 ) < 0 )
510 else if ( ( index - 1 ) < 1 ) {
515 currentFilter = mFilterList[ index - 2 ];
526 void ViewManager::configureFilters()
528 FilterDialog dlg(
this );
530 dlg.setFilters( mFilterList );
533 mFilterList = dlg.filters();
535 uint pos = mFilterSelectionWidget->currentItem();
536 mFilterSelectionWidget->setItems( filterNames() );
537 mFilterSelectionWidget->setCurrentItem( pos );
538 setActiveFilter( pos );
541 TQStringList ViewManager::filterNames()
const
543 TQStringList names( i18n(
"None" ) );
544 names.append( i18n(
"Unfiled" ) );
546 Filter::List::ConstIterator it;
547 for ( it = mFilterList.begin(); it != mFilterList.end(); ++it )
548 names.append( (*it).name() );
553 int ViewManager::filterPosition(
const TQString &name )
const
557 Filter::List::ConstIterator it;
558 for ( it = mFilterList.begin(); it != mFilterList.end(); ++it, ++pos )
559 if ( name == (*it).name() )
565 void ViewManager::initActions()
567 mActionSelectView =
new TDESelectAction( i18n(
"Select View" ), 0, mCore->actionCollection(),
"select_view" );
568 #if TDE_VERSION >= 309
569 mActionSelectView->setMenuAccelsEnabled(
false );
571 connect( mActionSelectView, TQ_SIGNAL( activated(
const TQString& ) ),
572 TQ_SLOT( setActiveView(
const TQString& ) ) );
576 action =
new TDEAction( i18n(
"Modify View..." ),
"configure", 0,
this,
577 TQ_SLOT( editView() ), mCore->actionCollection(),
579 action->setWhatsThis( i18n(
"By pressing this button a dialog opens that allows you to modify the view of the addressbook. There you can add or remove fields that you want to be shown or hidden in the addressbook like the name for example." ) );
581 action =
new TDEAction( i18n(
"Add View..." ),
"window-new", 0,
this,
582 TQ_SLOT( addView() ), mCore->actionCollection(),
584 action->setWhatsThis( i18n(
"You can add a new view by choosing one from the dialog that appears after pressing the button. You have to give the view a name, so that you can distinguish between the different views." ) );
586 mActionDeleteView =
new TDEAction( i18n(
"Delete View" ),
"view_remove", 0,
587 this, TQ_SLOT( deleteView() ),
588 mCore->actionCollection(),
"view_delete" );
589 mActionDeleteView->setWhatsThis( i18n(
"By pressing this button you can delete the actual view, which you have added before." ) );
591 action =
new TDEAction( i18n(
"Refresh View" ),
"reload", 0,
this,
592 TQ_SLOT( refreshView() ), mCore->actionCollection(),
594 action->setWhatsThis( i18n(
"The view will be refreshed by pressing this button." ) );
596 action =
new TDEAction( i18n(
"Edit &Filters..." ),
"filter", 0,
this,
597 TQ_SLOT( configureFilters() ), mCore->actionCollection(),
598 "options_edit_filters" );
599 action->setWhatsThis( i18n(
"Edit the contact filters<p>You will be presented with a dialog, where you can add, remove and edit filters." ) );
602 void ViewManager::initGUI()
604 TQHBoxLayout *layout =
new TQHBoxLayout(
this );
605 mViewWidgetStack =
new TQWidgetStack(
this );
606 layout->addWidget( mViewWidgetStack );
609 #include "viewmanager.moc"
Modal dialog used for adding a new view.
static TQString addresseesToEmails(const TDEABC::Addressee::List &addrList)
Converts the list of addressee objects into a list of email addresses.
Filter for AddressBook related objects (Addressees)
void restore(TDEConfig *config)
Loads the filter from the config file.
void save(TDEConfig *config)
Saves the filter to the config file.
void setMatchRule(MatchRule rule)
Sets the filter rule.
Base class for all views in kaddressbook.
virtual TQStringList selectedUids()=0
Must be overloaded in subclasses.
virtual TQString type() const =0
Return the type of the view: Icon, Table, etc.
const TQString & defaultFilterName() const
void setFilter(const Filter &)
Sets the active filter.
DefaultFilterType defaultFilterType() const
virtual void refresh(const TQString &uid=TQString())=0
Must be overloaded in subclasses to refresh the view.
virtual TQString selectedEmails()
Returns a TQString with all the selected email addresses concatenated together with a ',...
virtual void setSelected(const TQString &uid=TQString(), bool selected=true)=0
This method must be overloaded in subclasses.
virtual void setFirstSelected(bool selected=true)=0
Selects the first contact in the view.
virtual void readConfig(TDEConfig *config)
Called whenever this view should read the config.
virtual TDEABC::Field * sortField() const =0
void viewFieldsChanged()
Emitted whenever the view fields changed.
void startDrag()
Called whenever the user attempts to start a drag in the view.
void urlDropped(const KURL &)
Emitted whenever a url is dragged on a view.
void dropped(TQDropEvent *)
Called whenever the user drops something in the active view.
void modified()
Emitted whenever the address book is modified in some way.
void selected(const TQString &uid)
Emitted whenever the user selects an entry in the view.
void sortFieldChanged()
Emitted whenever the sort field of a view has changed.
void executed(const TQString &uid)
Emitted whenever the user activates an entry in the view.