TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

viewmanager.cpp 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. This file is part of KAddressBook.
  3. Copyright (c) 2002 Mike Pilone <mpilone@slac.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  15. As a special exception, permission is given to link this program
  16. with any edition of TQt, and distribute the resulting executable,
  17. without including the source code for TQt in the source distribution.
  18. */
  19. #include <tqfile.h>
  20. #include <tqlayout.h>
  21. #include <tqwidgetstack.h>
  22. #include <libtdepim/kvcarddrag.h>
  23. #include <kabc/addressbook.h>
  24. #include <kabc/vcardconverter.h>
  25. #include <kactionclasses.h>
  26. #include <kconfig.h>
  27. #include <kdebug.h>
  28. #include <kdeversion.h>
  29. #include <kiconloader.h>
  30. #include <klocale.h>
  31. #include <kmessagebox.h>
  32. #include <kmultipledrag.h>
  33. #include <ktempdir.h>
  34. #include <ktrader.h>
  35. #include <kurldrag.h>
  36. #include "addviewdialog.h"
  37. #include "addresseeutil.h"
  38. #include "core.h"
  39. #include "filtereditdialog.h"
  40. #include "filterselectionwidget.h"
  41. #include "kabprefs.h"
  42. #include "viewmanager.h"
  43. ViewManager::ViewManager( KAB::Core *core, TQWidget *parent, const char *name )
  44. : TQWidget( parent, name ), mCore( core ), mActiveView( 0 ),
  45. mFilterSelectionWidget( 0 )
  46. {
  47. initGUI();
  48. initActions();
  49. mViewDict.setAutoDelete( true );
  50. createViewFactories();
  51. }
  52. ViewManager::~ViewManager()
  53. {
  54. unloadViews();
  55. mViewFactoryDict.clear();
  56. }
  57. void ViewManager::restoreSettings()
  58. {
  59. mViewNameList = KABPrefs::instance()->viewNames();
  60. TQString activeViewName = KABPrefs::instance()->currentView();
  61. mActionSelectView->setItems( mViewNameList );
  62. // Filter
  63. mFilterList = Filter::restore( mCore->config(), "Filter" );
  64. mFilterSelectionWidget->setItems( filterNames() );
  65. mFilterSelectionWidget->setCurrentItem( KABPrefs::instance()->currentFilter() );
  66. // Tell the views to reread their config, since they may have
  67. // been modified by global settings
  68. TQDictIterator<KAddressBookView> it( mViewDict );
  69. for ( it.toFirst(); it.current(); ++it ) {
  70. KConfigGroupSaver saver( mCore->config(), it.currentKey() );
  71. it.current()->readConfig( mCore->config() );
  72. }
  73. setActiveView( activeViewName );
  74. mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
  75. }
  76. void ViewManager::saveSettings()
  77. {
  78. TQDictIterator<KAddressBookView> it( mViewDict );
  79. for ( it.toFirst(); it.current(); ++it ) {
  80. KConfigGroupSaver saver( mCore->config(), it.currentKey() );
  81. (*it)->writeConfig( mCore->config() );
  82. }
  83. Filter::save( mCore->config(), "Filter", mFilterList );
  84. KABPrefs::instance()->setCurrentFilter( mFilterSelectionWidget->currentItem() );
  85. // write the view name list
  86. KABPrefs::instance()->setViewNames( mViewNameList );
  87. if ( mActiveView )
  88. KABPrefs::instance()->setCurrentView( mActiveView->caption() );
  89. }
  90. TQStringList ViewManager::selectedUids() const
  91. {
  92. if ( mActiveView ) {
  93. return mActiveView->selectedUids();
  94. } else
  95. return TQStringList();
  96. }
  97. TQStringList ViewManager::selectedEmails() const
  98. {
  99. if ( mActiveView )
  100. return mActiveView->selectedEmails();
  101. else
  102. return TQStringList();
  103. }
  104. KABC::Addressee::List ViewManager::selectedAddressees() const
  105. {
  106. KABC::Addressee::List list;
  107. const TQStringList uids = selectedUids();
  108. TQStringList::ConstIterator it;
  109. for ( it = uids.begin(); it != uids.end(); ++it ) {
  110. KABC::Addressee addr = mCore->addressBook()->findByUid( *it );
  111. if ( !addr.isEmpty() )
  112. list.append( addr );
  113. }
  114. return list;
  115. }
  116. void ViewManager::setFilterSelectionWidget( FilterSelectionWidget *wdg )
  117. {
  118. mFilterSelectionWidget = wdg;
  119. }
  120. KABC::Field *ViewManager::currentSortField() const
  121. {
  122. if ( mActiveView )
  123. return mActiveView->sortField();
  124. else
  125. return 0;
  126. }
  127. KABC::Field::List ViewManager::viewFields() const
  128. {
  129. /*
  130. if ( mActiveView )
  131. return mActiveView->fields();
  132. else
  133. */
  134. return KABC::Field::List();
  135. }
  136. void ViewManager::setSelected( const TQString &uid, bool selected )
  137. {
  138. if ( mActiveView )
  139. mActiveView->setSelected( uid, selected );
  140. }
  141. void ViewManager::setFirstSelected( bool selected )
  142. {
  143. if ( mActiveView )
  144. mActiveView->setFirstSelected( selected );
  145. }
  146. void ViewManager::unloadViews()
  147. {
  148. mViewDict.clear();
  149. mActiveView = 0;
  150. }
  151. void ViewManager::setActiveView( const TQString &name )
  152. {
  153. KAddressBookView *view = 0;
  154. // Check that this isn't the same as the current active view
  155. if ( mActiveView && ( mActiveView->caption() == name ) )
  156. return;
  157. // At this point we know the view that should be active is not
  158. // currently active. We will try to find the new on in the list. If
  159. // we can't find it, it means it hasn't been instantiated, so we will
  160. // create it on demand.
  161. view = mViewDict.find( name );
  162. // Check if we found the view. If we didn't, then we need to create it
  163. if ( view == 0 ) {
  164. KConfig *config = mCore->config();
  165. KConfigGroupSaver saver( config, name );
  166. TQString type = config->readEntry( "Type", "Table" );
  167. kdDebug(5720) << "ViewManager::setActiveView: creating view - " << name << endl;
  168. ViewFactory *factory = mViewFactoryDict.find( type );
  169. if ( factory )
  170. view = factory->view( mCore, mViewWidgetStack );
  171. if ( view ) {
  172. view->setCaption( name );
  173. mViewDict.insert( name, view );
  174. mViewWidgetStack->addWidget( view );
  175. view->readConfig( config );
  176. // The manager just relays the signals
  177. connect( view, TQT_SIGNAL( selected( const TQString& ) ),
  178. TQT_SIGNAL( selected( const TQString & ) ) );
  179. connect( view, TQT_SIGNAL( executed( const TQString& ) ),
  180. TQT_SIGNAL( executed( const TQString& ) ) );
  181. connect( view, TQT_SIGNAL( modified() ), TQT_SIGNAL( modified() ) );
  182. connect( view, TQT_SIGNAL( dropped( TQDropEvent* ) ),
  183. TQT_SLOT( dropped( TQDropEvent* ) ) );
  184. connect( view, TQT_SIGNAL( startDrag() ), TQT_SLOT( startDrag() ) );
  185. connect( view, TQT_SIGNAL( sortFieldChanged() ), TQT_SIGNAL( sortFieldChanged() ) );
  186. }
  187. }
  188. // If we found or created the view, raise it and refresh it
  189. if ( view ) {
  190. mActiveView = view;
  191. mViewWidgetStack->raiseWidget( view );
  192. // Set the proper filter in the view. By setting the combo
  193. // box, the activated slot will be called, which will push
  194. // the filter to the view and refresh it.
  195. if ( view->defaultFilterType() == KAddressBookView::None ) {
  196. mFilterSelectionWidget->setCurrentItem( 0 );
  197. setActiveFilter( 0 );
  198. } else if ( view->defaultFilterType() == KAddressBookView::Active ) {
  199. setActiveFilter( mFilterSelectionWidget->currentItem() );
  200. } else {
  201. uint pos = filterPosition( view->defaultFilterName() );
  202. mFilterSelectionWidget->setCurrentItem( pos );
  203. setActiveFilter( pos );
  204. }
  205. // Update the inc search widget to show the fields in the new active
  206. // view.
  207. mActiveView->refresh();
  208. } else
  209. kdDebug(5720) << "ViewManager::setActiveView: unable to find view\n";
  210. }
  211. void ViewManager::refreshView( const TQString &uid )
  212. {
  213. if ( mActiveView )
  214. mActiveView->refresh( uid );
  215. }
  216. void ViewManager::editView()
  217. {
  218. if ( !mActiveView )
  219. return;
  220. ViewFactory *factory = mViewFactoryDict.find( mActiveView->type() );
  221. ViewConfigureWidget *wdg = 0;
  222. if ( factory ) {
  223. // Save the filters so the dialog has the latest set
  224. Filter::save( mCore->config(), "Filter", mFilterList );
  225. wdg = factory->configureWidget( mCore->addressBook(), 0 );
  226. }
  227. if ( wdg ) {
  228. ViewConfigureDialog dlg( wdg, mActiveView->caption(), this );
  229. KConfigGroupSaver saver( mCore->config(), mActiveView->caption() );
  230. dlg.restoreSettings( mCore->config() );
  231. if ( dlg.exec() ) {
  232. dlg.saveSettings( mCore->config() );
  233. mActiveView->readConfig( mCore->config() );
  234. // Set the proper filter in the view. By setting the combo
  235. // box, the activated slot will be called, which will push
  236. // the filter to the view and refresh it.
  237. if ( mActiveView->defaultFilterType() == KAddressBookView::None ) {
  238. mFilterSelectionWidget->setCurrentItem( 0 );
  239. setActiveFilter( 0 );
  240. } else if ( mActiveView->defaultFilterType() == KAddressBookView::Active ) {
  241. setActiveFilter( mFilterSelectionWidget->currentItem() );
  242. } else {
  243. uint pos = filterPosition( mActiveView->defaultFilterName() );
  244. mFilterSelectionWidget->setCurrentItem( pos );
  245. setActiveFilter( pos );
  246. }
  247. mActiveView->refresh();
  248. emit viewFieldsChanged();
  249. }
  250. }
  251. }
  252. void ViewManager::deleteView()
  253. {
  254. TQString text = i18n( "<qt>Are you sure that you want to delete the view <b>%1</b>?</qt>" )
  255. .tqarg( mActiveView->caption() );
  256. TQString caption = i18n( "Confirm Delete" );
  257. if ( KMessageBox::warningContinueCancel( this, text, caption, KGuiItem( i18n("&Delete"), "editdelete") ) == KMessageBox::Continue ) {
  258. mViewNameList.remove( mActiveView->caption() );
  259. // remove the view from the config file
  260. KConfig *config = mCore->config();
  261. config->deleteGroup( mActiveView->caption() );
  262. mViewDict.remove( mActiveView->caption() );
  263. mActiveView = 0;
  264. // we are in an invalid state now, but that should be fixed after
  265. // we emit the signal
  266. mActionSelectView->setItems( mViewNameList );
  267. if ( mViewNameList.count() > 0 ) {
  268. mActionSelectView->setCurrentItem( 0 );
  269. setActiveView( mViewNameList[ 0 ] );
  270. }
  271. mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
  272. }
  273. }
  274. void ViewManager::addView()
  275. {
  276. AddViewDialog dialog( &mViewFactoryDict, this );
  277. if ( dialog.exec() ) {
  278. TQString newName = dialog.viewName();
  279. TQString type = dialog.viewType();
  280. // Check for name conflicts
  281. bool firstConflict = true;
  282. int numTries = 1;
  283. while ( mViewNameList.contains( newName ) > 0 ) {
  284. if ( !firstConflict ) {
  285. newName = newName.left( newName.length() - 4 );
  286. firstConflict = false;
  287. }
  288. newName = TQString( "%1 <%2>" ).tqarg( newName ).tqarg( numTries );
  289. numTries++;
  290. }
  291. // Add the new one to the list
  292. mViewNameList.append( newName );
  293. // write the view to the config file,
  294. KConfig *config = mCore->config();
  295. config->deleteGroup( newName );
  296. KConfigGroupSaver saver( config, newName );
  297. config->writeEntry( "Type", type );
  298. // try to set the active view
  299. mActionSelectView->setItems( mViewNameList );
  300. mActionSelectView->setCurrentItem( mViewNameList.findIndex( newName ) );
  301. setActiveView( newName );
  302. editView();
  303. mActionDeleteView->setEnabled( mViewNameList.count() > 1 );
  304. }
  305. }
  306. void ViewManager::scrollUp()
  307. {
  308. if ( mActiveView )
  309. mActiveView->scrollUp();
  310. }
  311. void ViewManager::scrollDown()
  312. {
  313. if ( mActiveView )
  314. mActiveView->scrollDown();
  315. }
  316. void ViewManager::createViewFactories()
  317. {
  318. const KTrader::OfferList plugins = KTrader::self()->query( "KAddressBook/View",
  319. TQString( "[X-KDE-KAddressBook-ViewPluginVersion] == %1" ).tqarg( KAB_VIEW_PLUGIN_VERSION ) );
  320. KTrader::OfferList::ConstIterator it;
  321. for ( it = plugins.begin(); it != plugins.end(); ++it ) {
  322. if ( !(*it)->hasServiceType( "KAddressBook/View" ) )
  323. continue;
  324. KLibFactory *factory = KLibLoader::self()->factory( (*it)->library().latin1() );
  325. if ( !factory ) {
  326. kdDebug(5720) << "ViewManager::createViewFactories(): Factory creation failed" << endl;
  327. continue;
  328. }
  329. ViewFactory *viewFactory = static_cast<ViewFactory*>( factory );
  330. if ( !viewFactory ) {
  331. kdDebug(5720) << "ViewManager::createViewFactories(): Cast failed" << endl;
  332. continue;
  333. }
  334. mViewFactoryDict.insert( viewFactory->type(), viewFactory );
  335. }
  336. }
  337. void ViewManager::dropped( TQDropEvent *e )
  338. {
  339. kdDebug(5720) << "ViewManager::dropped: got a drop event" << endl;
  340. // don't allow drops from our own drags
  341. if ( e->source() == this )
  342. return;
  343. KABC::Addressee::List list;
  344. KURL::List urls;
  345. if ( KURLDrag::decode( e, urls) ) {
  346. KURL::List::ConstIterator it = urls.begin();
  347. int c = urls.count();
  348. if ( c > 1 ) {
  349. TQString questionString = i18n( "Import one contact into your addressbook?", "Import %n contacts into your addressbook?", c );
  350. if ( KMessageBox::questionYesNo( this, questionString, i18n( "Import Contacts?" ), i18n("Import"), i18n("Do Not Import") ) == KMessageBox::Yes ) {
  351. for ( ; it != urls.end(); ++it )
  352. emit urlDropped( *it );
  353. }
  354. } else if ( c == 1 )
  355. emit urlDropped( *it );
  356. } else if ( KVCardDrag::decode( e, list ) ) {
  357. KABC::Addressee::List::ConstIterator it;
  358. for ( it = list.begin(); it != list.end(); ++it ) {
  359. KABC::Addressee a = mCore->addressBook()->findByUid( (*it).uid() );
  360. if ( a.isEmpty() ) { // not yet in address book
  361. mCore->addressBook()->insertAddressee( *it );
  362. emit modified();
  363. }
  364. }
  365. mActiveView->refresh();
  366. }
  367. }
  368. void ViewManager::startDrag()
  369. {
  370. // Get the list of all the selected addressees
  371. KABC::Addressee::List addrList;
  372. const TQStringList uidList = selectedUids();
  373. if ( uidList.isEmpty() )
  374. return;
  375. kdDebug(5720) << "ViewManager::startDrag: starting to drag" << endl;
  376. TQStringList::ConstIterator it;
  377. for ( it = uidList.begin(); it != uidList.end(); ++it )
  378. addrList.append( mCore->addressBook()->findByUid( *it ) );
  379. KMultipleDrag *drag = new KMultipleDrag( this );
  380. KABC::VCardConverter converter;
  381. #if defined(KABC_VCARD_ENCODING_FIX)
  382. TQCString vcards = converter.createVCardsRaw( addrList );
  383. #else
  384. TQString vcards = converter.createVCards( addrList );
  385. #endif
  386. // Best text representation is given by textdrag, so it must be first
  387. drag->addDragObject( new TQTextDrag( AddresseeUtil::addresseesToEmails( addrList ), this ) );
  388. drag->addDragObject( new KVCardDrag( vcards, this ) );
  389. KTempDir tempDir;
  390. // can't set tempDir to autoDelete, in case of dropping on the desktop, the copy is async...
  391. if ( tempDir.status() == 0 ) {
  392. TQString fileName;
  393. if ( addrList.count() == 1 )
  394. fileName = addrList[ 0 ].givenName() + "_" + addrList[ 0 ].familyName() + ".vcf";
  395. else
  396. fileName = "contacts.vcf";
  397. TQFile tempFile( tempDir.name() + "/" + fileName );
  398. if ( tempFile.open( IO_WriteOnly ) ) {
  399. #if defined(KABC_VCARD_ENCODING_FIX)
  400. tempFile.writeBlock( vcards, vcards.length() );
  401. #else
  402. tempFile.writeBlock( vcards.utf8() );
  403. #endif
  404. tempFile.close();
  405. KURLDrag *urlDrag = new KURLDrag( KURL( tempFile.name() ), this );
  406. drag->addDragObject( urlDrag );
  407. }
  408. }
  409. drag->setPixmap( KGlobal::iconLoader()->loadIcon( "vcard", KIcon::Desktop ) );
  410. drag->dragCopy();
  411. }
  412. void ViewManager::setActiveFilter( int index )
  413. {
  414. Filter currentFilter;
  415. if ( ( index - 1 ) < 0 )
  416. currentFilter = Filter();
  417. else if ( ( index - 1 ) < 1 ) {
  418. currentFilter = Filter();
  419. currentFilter.setMatchRule(Filter::NotMatching);
  420. }
  421. else
  422. currentFilter = mFilterList[ index - 2 ];
  423. // Check if we have a view. Since the filter combo is created before
  424. // the view, this slot could be called before there is a valid view.
  425. if ( mActiveView ) {
  426. mActiveView->setFilter( currentFilter );
  427. mActiveView->refresh();
  428. emit selected( TQString() );
  429. }
  430. }
  431. void ViewManager::configureFilters()
  432. {
  433. FilterDialog dlg( this );
  434. dlg.setFilters( mFilterList );
  435. if ( dlg.exec() )
  436. mFilterList = dlg.filters();
  437. uint pos = mFilterSelectionWidget->currentItem();
  438. mFilterSelectionWidget->setItems( filterNames() );
  439. mFilterSelectionWidget->setCurrentItem( pos );
  440. setActiveFilter( pos );
  441. }
  442. TQStringList ViewManager::filterNames() const
  443. {
  444. TQStringList names( i18n( "None" ) );
  445. names.append( i18n( "Unfiled" ) );
  446. Filter::List::ConstIterator it;
  447. for ( it = mFilterList.begin(); it != mFilterList.end(); ++it )
  448. names.append( (*it).name() );
  449. return names;
  450. }
  451. int ViewManager::filterPosition( const TQString &name ) const
  452. {
  453. int pos = 0;
  454. Filter::List::ConstIterator it;
  455. for ( it = mFilterList.begin(); it != mFilterList.end(); ++it, ++pos )
  456. if ( name == (*it).name() )
  457. return pos + 2;
  458. return 0;
  459. }
  460. void ViewManager::initActions()
  461. {
  462. mActionSelectView = new KSelectAction( i18n( "Select View" ), 0, mCore->actionCollection(), "select_view" );
  463. #if KDE_VERSION >= 309
  464. mActionSelectView->setMenuAccelsEnabled( false );
  465. #endif
  466. connect( mActionSelectView, TQT_SIGNAL( activated( const TQString& ) ),
  467. TQT_SLOT( setActiveView( const TQString& ) ) );
  468. KAction *action;
  469. action = new KAction( i18n( "Modify View..." ), "configure", 0, TQT_TQOBJECT(this),
  470. TQT_SLOT( editView() ), mCore->actionCollection(),
  471. "view_modify" );
  472. 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." ) );
  473. action = new KAction( i18n( "Add View..." ), "window_new", 0, TQT_TQOBJECT(this),
  474. TQT_SLOT( addView() ), mCore->actionCollection(),
  475. "view_add" );
  476. 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." ) );
  477. mActionDeleteView = new KAction( i18n( "Delete View" ), "view_remove", 0,
  478. TQT_TQOBJECT(this), TQT_SLOT( deleteView() ),
  479. mCore->actionCollection(), "view_delete" );
  480. mActionDeleteView->setWhatsThis( i18n( "By pressing this button you can delete the actual view, which you have added before." ) );
  481. action = new KAction( i18n( "Refresh View" ), "reload", 0, TQT_TQOBJECT(this),
  482. TQT_SLOT( refreshView() ), mCore->actionCollection(),
  483. "view_refresh" );
  484. action->setWhatsThis( i18n( "The view will be refreshed by pressing this button." ) );
  485. action = new KAction( i18n( "Edit &Filters..." ), "filter", 0, TQT_TQOBJECT(this),
  486. TQT_SLOT( configureFilters() ), mCore->actionCollection(),
  487. "options_edit_filters" );
  488. action->setWhatsThis( i18n( "Edit the contact filters<p>You will be presented with a dialog, where you can add, remove and edit filters." ) );
  489. }
  490. void ViewManager::initGUI()
  491. {
  492. TQHBoxLayout *tqlayout = new TQHBoxLayout( this );
  493. mViewWidgetStack = new TQWidgetStack( this );
  494. tqlayout->addWidget( mViewWidgetStack );
  495. }
  496. #include "viewmanager.moc"