distributionlistpicker.cpp
00001 /* 00002 This file is part of KAddressBook. 00003 Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.net> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 As a special exception, permission is given to link this program 00019 with any edition of TQt, and distribute the resulting executable, 00020 without including the source code for TQt in the source distribution. 00021 */ 00022 00023 #include "distributionlistpicker.h" 00024 #include "config.h" 00025 00026 #ifdef KDEPIM_NEW_DISTRLISTS 00027 #include <libkdepim/distributionlist.h> 00028 #endif 00029 00030 #include <kabc/addressbook.h> 00031 00032 #include <kapplication.h> 00033 #include <kinputdialog.h> 00034 #include <klistbox.h> 00035 #include <klocale.h> 00036 #include <kmessagebox.h> 00037 00038 #include <tqlabel.h> 00039 #include <tqlayout.h> 00040 #include <tqpushbutton.h> 00041 #include <tqregexp.h> 00042 #include <tqvalidator.h> 00043 00044 KPIM::DistributionListPickerDialog::DistributionListPickerDialog( KABC::AddressBook* book, TQWidget* parent ) : KDialogBase( parent, 0, true, TQString(), Ok | Cancel | User1 ), m_book( book ) 00045 { 00046 Q_ASSERT( m_book ); 00047 setModal( true ); 00048 enableButton( Ok, false ); 00049 setButtonText( User1, i18n( "Add New Distribution List" ) ); 00050 TQWidget* main = new TQWidget( this ); 00051 TQGridLayout* layout = new TQGridLayout( main ); 00052 layout->setSpacing( KDialog::spacingHint() ); 00053 m_label = new TQLabel( main ); 00054 layout->addWidget( m_label, 0, 0 ); 00055 m_listBox = new KListBox( main ); 00056 layout->addWidget( m_listBox, 1, 0 ); 00057 connect( m_listBox, TQT_SIGNAL( highlighted( const TQString& ) ), 00058 this, TQT_SLOT( entrySelected( const TQString& ) ) ); 00059 connect( m_listBox, TQT_SIGNAL( selected( const TQString& ) ), 00060 this, TQT_SLOT( entrySelected( const TQString& ) ) ); 00061 setMainWidget( main ); 00062 #ifdef KDEPIM_NEW_DISTRLISTS 00063 typedef TQValueList<KPIM::DistributionList> DistListList; 00064 const DistListList lists = KPIM::DistributionList::allDistributionLists( m_book ); 00065 for ( DistListList::ConstIterator it = lists.begin(); it != lists.end(); ++it ) 00066 { 00067 m_listBox->insertItem ( (*it).name() ); 00068 } 00069 #endif 00070 } 00071 00072 void KPIM::DistributionListPickerDialog::entrySelected( const TQString& name ) 00073 { 00074 actionButton( Ok )->setEnabled( !name.isNull() ); 00075 } 00076 00077 void KPIM::DistributionListPickerDialog::setLabelText( const TQString& text ) 00078 { 00079 m_label->setText( text ); 00080 } 00081 00082 void KPIM::DistributionListPickerDialog::slotUser1() 00083 { 00084 #ifdef KDEPIM_NEW_DISTRLISTS 00085 const TQValueList<KPIM::DistributionList> lists = KPIM::DistributionList::allDistributionLists( m_book ); 00086 TQStringList listNames; 00087 for ( TQValueList<KPIM::DistributionList>::ConstIterator it = lists.begin(); it != lists.end(); ++it ) 00088 { 00089 listNames += (*it).name(); 00090 } 00091 00092 bool validName = false; 00093 do 00094 { 00095 TQRegExpValidator validator( TQRegExp( "\\S+.*" ), 0 ); 00096 const TQString name = KInputDialog::getText( i18n( "Enter Name" ), i18n( "Enter a name for the new distribution list:" ), TQString(), 0, this, 0, &validator ).stripWhiteSpace(); 00097 if ( name.isEmpty() ) 00098 return; 00099 00100 validName = !listNames.contains( name ); 00101 00102 if ( validName ) 00103 { 00104 KPIM::DistributionList list; 00105 list.setName( name ); 00106 list.setUid( KApplication::randomString( 10 ) ); 00107 m_book->insertAddressee( list ); 00108 00109 m_listBox->insertItem( name ); 00110 TQListBoxItem* item = m_listBox->findItem( name ); 00111 m_listBox->setSelected( item, true ); 00112 } 00113 else 00114 { 00115 KMessageBox::error( this, i18n( "A distribution list with the the name %1 already exists. Please choose another name" ).arg( name ), i18n( "Name Exists" ) ); 00116 } 00117 } 00118 while ( !validName ); 00119 #endif 00120 } 00121 00122 00123 void KPIM::DistributionListPickerDialog::slotOk() 00124 { 00125 TQListBoxItem* item = m_listBox->selectedItem(); 00126 m_selectedDistributionList = item ? item->text() : TQString(); 00127 KDialogBase::slotOk(); 00128 } 00129 00130 void KPIM::DistributionListPickerDialog::slotCancel() 00131 { 00132 m_selectedDistributionList = TQString(); 00133 KDialogBase::slotCancel(); 00134 } 00135 00136 TQString KPIM::DistributionListPickerDialog::selectedDistributionList() const 00137 { 00138 return m_selectedDistributionList; 00139 } 00140 00141 #include "distributionlistpicker.moc" 00142