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.

distributionlistpicker.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. This file is part of KAddressBook.
  3. Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.net>
  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 "distributionlistpicker.h"
  20. #include "config.h"
  21. #ifdef KDEPIM_NEW_DISTRLISTS
  22. #include <libtdepim/distributionlist.h>
  23. #endif
  24. #include <kabc/addressbook.h>
  25. #include <kapplication.h>
  26. #include <kinputdialog.h>
  27. #include <klistbox.h>
  28. #include <klocale.h>
  29. #include <kmessagebox.h>
  30. #include <tqlabel.h>
  31. #include <tqlayout.h>
  32. #include <tqpushbutton.h>
  33. #include <tqregexp.h>
  34. #include <tqvalidator.h>
  35. KPIM::DistributionListPickerDialog::DistributionListPickerDialog( KABC::AddressBook* book, TQWidget* parent ) : KDialogBase( parent, 0, true, TQString(), Ok | Cancel | User1 ), m_book( book )
  36. {
  37. Q_ASSERT( m_book );
  38. setModal( true );
  39. enableButton( Ok, false );
  40. setButtonText( User1, i18n( "Add New Distribution List" ) );
  41. TQWidget* main = new TQWidget( this );
  42. TQGridLayout* tqlayout = new TQGridLayout( main );
  43. tqlayout->setSpacing( KDialog::spacingHint() );
  44. m_label = new TQLabel( main );
  45. tqlayout->addWidget( m_label, 0, 0 );
  46. m_listBox = new KListBox( main );
  47. tqlayout->addWidget( m_listBox, 1, 0 );
  48. connect( m_listBox, TQT_SIGNAL( highlighted( const TQString& ) ),
  49. this, TQT_SLOT( entrySelected( const TQString& ) ) );
  50. connect( m_listBox, TQT_SIGNAL( selected( const TQString& ) ),
  51. this, TQT_SLOT( entrySelected( const TQString& ) ) );
  52. setMainWidget( main );
  53. #ifdef KDEPIM_NEW_DISTRLISTS
  54. typedef TQValueList<KPIM::DistributionList> DistListList;
  55. const DistListList lists = KPIM::DistributionList::allDistributionLists( m_book );
  56. for ( DistListList::ConstIterator it = lists.begin(); it != lists.end(); ++it )
  57. {
  58. m_listBox->insertItem ( (*it).name() );
  59. }
  60. #endif
  61. }
  62. void KPIM::DistributionListPickerDialog::entrySelected( const TQString& name )
  63. {
  64. actionButton( Ok )->setEnabled( !name.isNull() );
  65. }
  66. void KPIM::DistributionListPickerDialog::setLabelText( const TQString& text )
  67. {
  68. m_label->setText( text );
  69. }
  70. void KPIM::DistributionListPickerDialog::slotUser1()
  71. {
  72. #ifdef KDEPIM_NEW_DISTRLISTS
  73. const TQValueList<KPIM::DistributionList> lists = KPIM::DistributionList::allDistributionLists( m_book );
  74. TQStringList listNames;
  75. for ( TQValueList<KPIM::DistributionList>::ConstIterator it = lists.begin(); it != lists.end(); ++it )
  76. {
  77. listNames += (*it).name();
  78. }
  79. bool validName = false;
  80. do
  81. {
  82. TQRegExpValidator validator( TQRegExp( "\\S+.*" ), 0 );
  83. const TQString name = KInputDialog::getText( i18n( "Enter Name" ), i18n( "Enter a name for the new distribution list:" ), TQString(), 0, this, 0, &validator ).stripWhiteSpace();
  84. if ( name.isEmpty() )
  85. return;
  86. validName = !listNames.contains( name );
  87. if ( validName )
  88. {
  89. KPIM::DistributionList list;
  90. list.setName( name );
  91. list.setUid( KApplication::randomString( 10 ) );
  92. m_book->insertAddressee( list );
  93. m_listBox->insertItem( name );
  94. TQListBoxItem* item = m_listBox->findItem( name );
  95. m_listBox->setSelected( item, true );
  96. }
  97. else
  98. {
  99. KMessageBox::error( this, i18n( "A distribution list with the the name %1 already exists. Please choose another name" ).tqarg( name ), i18n( "Name Exists" ) );
  100. }
  101. }
  102. while ( !validName );
  103. #endif
  104. }
  105. void KPIM::DistributionListPickerDialog::slotOk()
  106. {
  107. TQListBoxItem* item = m_listBox->selectedItem();
  108. m_selectedDistributionList = item ? item->text() : TQString();
  109. KDialogBase::slotOk();
  110. }
  111. void KPIM::DistributionListPickerDialog::slotCancel()
  112. {
  113. m_selectedDistributionList = TQString();
  114. KDialogBase::slotCancel();
  115. }
  116. TQString KPIM::DistributionListPickerDialog::selectedDistributionList() const
  117. {
  118. return m_selectedDistributionList;
  119. }
  120. #include "distributionlistpicker.moc"