26#include <tqwidgetstack.h>
28#include <libtdepim/kvcarddrag.h>
29#include <tdeabc/addressbook.h>
30#include <tdeabc/vcardconverter.h>
31#include <tdeactionclasses.h>
34#include <kiconloader.h>
36#include <tdemessagebox.h>
37#include <tdemultipledrag.h>
42#include "addviewdialog.h"
43#include "addresseeutil.h"
45#include "filtereditdialog.h"
46#include "filterselectionwidget.h"
49#include "viewmanager.h"
51ViewManager::ViewManager( KAB::Core *core, TQWidget *parent,
const char *name )
52 : TQWidget( parent, name ), mCore( core ), mActiveView( 0 ),
53 mFilterSelectionWidget( 0 )
58 mViewDict.setAutoDelete(
true );
60 createViewFactories();
63ViewManager::~ViewManager()
66 mViewFactoryDict.clear();
69void ViewManager::restoreSettings()
71 mViewNameList = KABPrefs::instance()->viewNames();
72 TQString activeViewName = KABPrefs::instance()->currentView();
74 mActionSelectView->setItems( mViewNameList );
78 mFilterSelectionWidget->setItems( filterNames() );
79 mFilterSelectionWidget->setCurrentItem( KABPrefs::instance()->currentFilter() );
83 TQDictIterator<KAddressBookView> it( mViewDict );
84 for ( it.toFirst(); it.current(); ++it ) {
85 TDEConfigGroupSaver saver( mCore->config(), it.currentKey() );
86 it.current()->readConfig( mCore->config() );
89 setActiveView( activeViewName );
91 mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
94void ViewManager::saveSettings()
96 TQDictIterator<KAddressBookView> it( mViewDict );
97 for ( it.toFirst(); it.current(); ++it ) {
98 TDEConfigGroupSaver saver( mCore->config(), it.currentKey() );
99 (*it)->writeConfig( mCore->config() );
103 KABPrefs::instance()->setCurrentFilter( mFilterSelectionWidget->currentItem() );
106 KABPrefs::instance()->setViewNames( mViewNameList );
109 KABPrefs::instance()->setCurrentView( mActiveView->caption() );
112TQStringList ViewManager::selectedUids()
const
117 return TQStringList();
120TQStringList ViewManager::selectedEmails()
const
125 return TQStringList();
128TDEABC::Addressee::List ViewManager::selectedAddressees()
const
130 TDEABC::Addressee::List list;
132 const TQStringList uids = selectedUids();
133 TQStringList::ConstIterator it;
134 for ( it = uids.begin(); it != uids.end(); ++it ) {
135 TDEABC::Addressee addr = mCore->addressBook()->findByUid( *it );
136 if ( !addr.isEmpty() )
145 mFilterSelectionWidget = wdg;
148TDEABC::Field *ViewManager::currentSortField()
const
156TDEABC::Field::List ViewManager::viewFields()
const
163 return TDEABC::Field::List();
166void ViewManager::setSelected(
const TQString &uid,
bool selected )
172void ViewManager::setFirstSelected(
bool selected )
178void ViewManager::unloadViews()
184void ViewManager::setActiveView(
const TQString &name )
189 if ( mActiveView && ( mActiveView->caption() == name ) )
197 view = mViewDict.find( name );
201 TDEConfig *config = mCore->config();
202 TDEConfigGroupSaver saver( config, name );
203 TQString type = config->readEntry(
"Type",
"Table" );
205 kdDebug(5720) <<
"ViewManager::setActiveView: creating view - " << name << endl;
207 ViewFactory *factory = mViewFactoryDict.find( type );
209 view = factory->view( mCore, mViewWidgetStack );
212 view->setCaption( name );
213 mViewDict.insert( name, view );
214 mViewWidgetStack->addWidget( view );
218 connect( view, TQ_SIGNAL(
selected(
const TQString& ) ),
219 TQ_SIGNAL(
selected(
const TQString & ) ) );
220 connect( view, TQ_SIGNAL(
executed(
const TQString& ) ),
221 TQ_SIGNAL(
executed(
const TQString& ) ) );
223 connect( view, TQ_SIGNAL(
dropped( TQDropEvent* ) ),
224 TQ_SLOT(
dropped( TQDropEvent* ) ) );
233 mViewWidgetStack->raiseWidget( view );
238 mFilterSelectionWidget->setCurrentItem( 0 );
239 setActiveFilter( 0 );
241 setActiveFilter( mFilterSelectionWidget->currentItem() );
244 mFilterSelectionWidget->setCurrentItem( pos );
245 setActiveFilter( pos );
253 kdDebug(5720) <<
"ViewManager::setActiveView: unable to find view\n";
256void ViewManager::refreshView(
const TQString &uid )
262void ViewManager::editView()
267 ViewFactory *factory = mViewFactoryDict.find( mActiveView->
type() );
274 wdg = factory->configureWidget( mCore->addressBook(), 0 );
278 ViewConfigureDialog dlg( wdg, mActiveView->caption(),
this );
280 TDEConfigGroupSaver saver( mCore->config(), mActiveView->caption() );
281 dlg.restoreSettings( mCore->config() );
284 dlg.saveSettings( mCore->config() );
290 mFilterSelectionWidget->setCurrentItem( 0 );
291 setActiveFilter( 0 );
293 setActiveFilter( mFilterSelectionWidget->currentItem() );
296 mFilterSelectionWidget->setCurrentItem( pos );
297 setActiveFilter( pos );
306void ViewManager::deleteView()
308 TQString text = i18n(
"<qt>Are you sure that you want to delete the view <b>%1</b>?</qt>" )
309 .arg( mActiveView->caption() );
310 TQString caption = i18n(
"Confirm Delete" );
312 if ( KMessageBox::warningContinueCancel(
this, text, caption, KGuiItem( i18n(
"&Delete"),
"edit-delete") ) == KMessageBox::Continue ) {
313 mViewNameList.remove( mActiveView->caption() );
316 TDEConfig *config = mCore->config();
317 config->deleteGroup( mActiveView->caption() );
319 mViewDict.remove( mActiveView->caption() );
324 mActionSelectView->setItems( mViewNameList );
325 if ( mViewNameList.count() > 0 ) {
326 mActionSelectView->setCurrentItem( 0 );
327 setActiveView( mViewNameList[ 0 ] );
329 mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
333void ViewManager::addView()
337 if ( dialog.exec() ) {
338 TQString newName = dialog.viewName();
339 TQString type = dialog.viewType();
342 bool firstConflict =
true;
344 while ( mViewNameList.contains( newName ) > 0 ) {
345 if ( !firstConflict ) {
346 newName = newName.left( newName.length() - 4 );
347 firstConflict =
false;
350 newName = TQString(
"%1 <%2>" ).arg( newName ).arg( numTries );
355 mViewNameList.append( newName );
358 TDEConfig *config = mCore->config();
359 config->deleteGroup( newName );
360 TDEConfigGroupSaver saver( config, newName );
361 config->writeEntry(
"Type", type );
364 mActionSelectView->setItems( mViewNameList );
365 mActionSelectView->setCurrentItem( mViewNameList.findIndex( newName ) );
366 setActiveView( newName );
370 mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
374void ViewManager::scrollUp()
377 mActiveView->scrollUp();
380void ViewManager::scrollDown()
383 mActiveView->scrollDown();
386void ViewManager::createViewFactories()
388 const TDETrader::OfferList plugins = TDETrader::self()->query(
"KAddressBook/View",
389 TQString(
"[X-TDE-KAddressBook-ViewPluginVersion] == %1" ).arg( KAB_VIEW_PLUGIN_VERSION ) );
390 TDETrader::OfferList::ConstIterator it;
391 for ( it = plugins.begin(); it != plugins.end(); ++it ) {
392 if ( !(*it)->hasServiceType(
"KAddressBook/View" ) )
395 KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
398 kdDebug(5720) <<
"ViewManager::createViewFactories(): Factory creation failed" << endl;
402 ViewFactory *viewFactory =
static_cast<ViewFactory*
>( factory );
404 if ( !viewFactory ) {
405 kdDebug(5720) <<
"ViewManager::createViewFactories(): Cast failed" << endl;
409 mViewFactoryDict.insert( viewFactory->type(), viewFactory );
415 kdDebug(5720) <<
"ViewManager::dropped: got a drop event" << endl;
418 if ( e->source() ==
this )
421 TDEABC::Addressee::List list;
424 if ( KURLDrag::decode( e, urls) ) {
425 KURL::List::ConstIterator it = urls.begin();
426 int c = urls.count();
428 TQString questionString = i18n(
"Import one contact into your addressbook?",
"Import %n contacts into your addressbook?", c );
429 if ( KMessageBox::questionYesNo(
this, questionString, i18n(
"Import Contacts?" ), i18n(
"Import"), i18n(
"Do Not Import") ) == KMessageBox::Yes ) {
430 for ( ; it != urls.end(); ++it )
435 }
else if ( KVCardDrag::decode( e, list ) ) {
436 TDEABC::Addressee::List::ConstIterator it;
437 for ( it = list.begin(); it != list.end(); ++it ) {
438 TDEABC::Addressee a = mCore->addressBook()->findByUid( (*it).uid() );
440 mCore->addressBook()->insertAddressee( *it );
452 TDEABC::Addressee::List addrList;
453 const TQStringList uidList = selectedUids();
454 if ( uidList.isEmpty() )
457 kdDebug(5720) <<
"ViewManager::startDrag: starting to drag" << endl;
459 TQStringList::ConstIterator it;
460 for ( it = uidList.begin(); it != uidList.end(); ++it )
461 addrList.append( mCore->addressBook()->findByUid( *it ) );
463 KMultipleDrag *drag =
new KMultipleDrag(
this );
465 TDEABC::VCardConverter converter;
466#if defined(KABC_VCARD_ENCODING_FIX)
467 TQCString vcards = converter.createVCardsRaw( addrList );
469 TQString vcards = converter.createVCards( addrList );
474 drag->addDragObject(
new KVCardDrag( vcards,
this ) );
478 if ( tempDir.status() == 0 ) {
480 if ( addrList.count() == 1 )
481 fileName = addrList[ 0 ].givenName() +
"_" + addrList[ 0 ].familyName() +
".vcf";
483 fileName =
"contacts.vcf";
485 TQFile tempFile( tempDir.name() +
"/" + fileName );
486 if ( tempFile.open( IO_WriteOnly ) ) {
487#if defined(KABC_VCARD_ENCODING_FIX)
488 tempFile.writeBlock( vcards, vcards.length() );
490 tempFile.writeBlock( vcards.utf8() );
494 KURLDrag *urlDrag =
new KURLDrag( KURL( tempFile.name() ),
this );
495 drag->addDragObject( urlDrag );
499 drag->setPixmap( TDEGlobal::iconLoader()->loadIcon(
"x-office-address-book", TDEIcon::Desktop ) );
503void ViewManager::setActiveFilter(
int index )
507 if ( ( index - 1 ) < 0 )
509 else if ( ( index - 1 ) < 1 ) {
514 currentFilter = mFilterList[ index - 2 ];
525void ViewManager::configureFilters()
527 FilterDialog dlg(
this );
529 dlg.setFilters( mFilterList );
532 mFilterList = dlg.filters();
534 uint pos = mFilterSelectionWidget->currentItem();
535 mFilterSelectionWidget->setItems( filterNames() );
536 mFilterSelectionWidget->setCurrentItem( pos );
537 setActiveFilter( pos );
540TQStringList ViewManager::filterNames()
const
542 TQStringList names( i18n(
"None" ) );
543 names.append( i18n(
"Unfiled" ) );
545 Filter::List::ConstIterator it;
546 for ( it = mFilterList.begin(); it != mFilterList.end(); ++it )
547 names.append( (*it).name() );
552int ViewManager::filterPosition(
const TQString &name )
const
556 Filter::List::ConstIterator it;
557 for ( it = mFilterList.begin(); it != mFilterList.end(); ++it, ++pos )
558 if ( name == (*it).name() )
564void ViewManager::initActions()
566 mActionSelectView =
new TDESelectAction( i18n(
"Select View" ), 0, mCore->actionCollection(),
"select_view" );
567 mActionSelectView->setMenuAccelsEnabled(
false );
568 connect( mActionSelectView, TQ_SIGNAL( activated(
const TQString& ) ),
569 TQ_SLOT( setActiveView(
const TQString& ) ) );
573 action =
new TDEAction( i18n(
"Modify View..." ),
"configure", 0,
this,
574 TQ_SLOT( editView() ), mCore->actionCollection(),
576 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." ) );
578 action =
new TDEAction( i18n(
"Add View..." ),
"window-new", 0,
this,
579 TQ_SLOT( addView() ), mCore->actionCollection(),
581 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." ) );
583 mActionDeleteView =
new TDEAction( i18n(
"Delete View" ),
"view_remove", 0,
584 this, TQ_SLOT( deleteView() ),
585 mCore->actionCollection(),
"view_delete" );
586 mActionDeleteView->setWhatsThis( i18n(
"By pressing this button you can delete the actual view, which you have added before." ) );
588 action =
new TDEAction( i18n(
"Refresh View" ),
"reload", 0,
this,
589 TQ_SLOT( refreshView() ), mCore->actionCollection(),
591 action->setWhatsThis( i18n(
"The view will be refreshed by pressing this button." ) );
593 action =
new TDEAction( i18n(
"Edit &Filters..." ),
"filter", 0,
this,
594 TQ_SLOT( configureFilters() ), mCore->actionCollection(),
595 "options_edit_filters" );
596 action->setWhatsThis( i18n(
"Edit the contact filters<p>You will be presented with a dialog, where you can add, remove and edit filters." ) );
599void ViewManager::initGUI()
601 TQHBoxLayout *layout =
new TQHBoxLayout(
this );
602 mViewWidgetStack =
new TQWidgetStack(
this );
603 layout->addWidget( mViewWidgetStack );
606#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 TDEABC::Field * sortField() const =0
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.
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.