kaddressbook

distributionlistpicker.cpp
1 /*
2  This file is part of KAddressBook.
3  Copyright (c) 2007 Klaralvdalens Datakonsult AB <frank@kdab.net>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  As a special exception, permission is given to link this program
19  with any edition of TQt, and distribute the resulting executable,
20  without including the source code for TQt in the source distribution.
21 */
22 
23 #include "distributionlistpicker.h"
24 #include "config.h"
25 
26 #ifdef TDEPIM_NEW_DISTRLISTS
27 #include <libtdepim/distributionlist.h>
28 #endif
29 
30 #include <tdeabc/addressbook.h>
31 
32 #include <tdeapplication.h>
33 #include <kinputdialog.h>
34 #include <tdelistbox.h>
35 #include <tdelocale.h>
36 #include <tdemessagebox.h>
37 
38 #include <tqlabel.h>
39 #include <tqlayout.h>
40 #include <tqpushbutton.h>
41 #include <tqregexp.h>
42 #include <tqvalidator.h>
43 
44 KPIM::DistributionListPickerDialog::DistributionListPickerDialog( TDEABC::AddressBook* book, TQWidget* parent ) : KDialogBase( parent, 0, true, TQString(), Ok | Cancel | User1 ), m_book( book )
45 {
46  Q_ASSERT( m_book );
47  setModal( true );
48  enableButton( Ok, false );
49  setButtonText( User1, i18n( "Add New Distribution List" ) );
50  TQWidget* main = new TQWidget( this );
51  TQGridLayout* layout = new TQGridLayout( main );
52  layout->setSpacing( KDialog::spacingHint() );
53  m_label = new TQLabel( main );
54  layout->addWidget( m_label, 0, 0 );
55  m_listBox = new TDEListBox( main );
56  layout->addWidget( m_listBox, 1, 0 );
57  connect( m_listBox, TQ_SIGNAL( highlighted( const TQString& ) ),
58  this, TQ_SLOT( entrySelected( const TQString& ) ) );
59  connect( m_listBox, TQ_SIGNAL( selected( const TQString& ) ),
60  this, TQ_SLOT( entrySelected( const TQString& ) ) );
61  setMainWidget( main );
62 #ifdef TDEPIM_NEW_DISTRLISTS
63  typedef TQValueList<KPIM::DistributionList> DistListList;
64  const DistListList lists = KPIM::DistributionList::allDistributionLists( m_book );
65  for ( DistListList::ConstIterator it = lists.begin(); it != lists.end(); ++it )
66  {
67  m_listBox->insertItem ( (*it).name() );
68  }
69 #endif
70 }
71 
72 void KPIM::DistributionListPickerDialog::entrySelected( const TQString& name )
73 {
74  actionButton( Ok )->setEnabled( !name.isNull() );
75 }
76 
77 void KPIM::DistributionListPickerDialog::setLabelText( const TQString& text )
78 {
79  m_label->setText( text );
80 }
81 
82 void KPIM::DistributionListPickerDialog::slotUser1()
83 {
84 #ifdef TDEPIM_NEW_DISTRLISTS
85  const TQValueList<KPIM::DistributionList> lists = KPIM::DistributionList::allDistributionLists( m_book );
86  TQStringList listNames;
87  for ( TQValueList<KPIM::DistributionList>::ConstIterator it = lists.begin(); it != lists.end(); ++it )
88  {
89  listNames += (*it).name();
90  }
91 
92  bool validName = false;
93  do
94  {
95  TQRegExpValidator validator( TQRegExp( "\\S+.*" ), 0 );
96  const TQString name = KInputDialog::getText( i18n( "Enter Name" ), i18n( "Enter a name for the new distribution list:" ), TQString(), 0, this, 0, &validator ).stripWhiteSpace();
97  if ( name.isEmpty() )
98  return;
99 
100  validName = !listNames.contains( name );
101 
102  if ( validName )
103  {
104  KPIM::DistributionList list;
105  list.setName( name );
106  list.setUid( TDEApplication::randomString( 10 ) );
107  m_book->insertAddressee( list );
108 
109  m_listBox->insertItem( name );
110  TQListBoxItem* item = m_listBox->findItem( name );
111  m_listBox->setSelected( item, true );
112  }
113  else
114  {
115  KMessageBox::error( this, i18n( "A distribution list with the the name %1 already exists. Please choose another name" ).arg( name ), i18n( "Name Exists" ) );
116  }
117  }
118  while ( !validName );
119 #endif
120 }
121 
122 
123 void KPIM::DistributionListPickerDialog::slotOk()
124 {
125  TQListBoxItem* item = m_listBox->selectedItem();
126  m_selectedDistributionList = item ? item->text() : TQString();
127  KDialogBase::slotOk();
128 }
129 
130 void KPIM::DistributionListPickerDialog::slotCancel()
131 {
132  m_selectedDistributionList = TQString();
133  KDialogBase::slotCancel();
134 }
135 
136 TQString KPIM::DistributionListPickerDialog::selectedDistributionList() const
137 {
138  return m_selectedDistributionList;
139 }
140 
141 #include "distributionlistpicker.moc"
142