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.

306 lines
11KB

  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 "distributionlisteditor.h"
  20. #include "distributionlisteditor_p.h"
  21. #include <libtdepim/addresseelineedit.h>
  22. #include <libtdepim/distributionlist.h>
  23. #include <libemailfunctions/email.h>
  24. #include <kabc/addressbook.h>
  25. #include <kabc/resource.h>
  26. #include <kapplication.h>
  27. #include <kdialogbase.h>
  28. #include <kglobal.h>
  29. #include <kiconloader.h>
  30. #include <klineedit.h>
  31. #include <klocale.h>
  32. #include <kmessagebox.h>
  33. #include <tqlabel.h>
  34. #include <tqlayout.h>
  35. #include <tqsignalmapper.h>
  36. #include <tqtoolbutton.h>
  37. #include <tqguardedptr.h>
  38. class KPIM::DistributionListEditor::EditorWidgetPrivate
  39. {
  40. public:
  41. TQScrollView* scrollView;
  42. TQSignalMapper* mapper;
  43. KABC::AddressBook* addressBook;
  44. TQString distListUid;
  45. TQLabel* nameLabel;
  46. TQLabel* memberListLabel;
  47. KLineEdit* nameLineEdit;
  48. TQWidget* memberListWidget;
  49. TQVBoxLayout* addresseeLayout;
  50. TQValueList<KPIM::DistributionListEditor::Line*> addressees;
  51. TQGuardedPtr<KABC::Resource> resource;
  52. KPIM::DistributionList distributionList;
  53. KPIM::DistributionListEditor::Line* addLineForEntry( const KPIM::DistributionList::Entry& entry );
  54. int lastLineId;
  55. };
  56. KPIM::DistributionListEditor::Line::Line( KABC::AddressBook* book, TQWidget* parent ) : TQWidget( parent ), m_addressBook( book )
  57. {
  58. Q_ASSERT( m_addressBook );
  59. TQBoxLayout* tqlayout = new TQHBoxLayout( this );
  60. tqlayout->setSpacing( KDialog::spacingHint() );
  61. m_lineEdit = new KPIM::DistributionListEditor::LineEdit( this );
  62. connect( m_lineEdit, TQT_SIGNAL( textChanged( const TQString& ) ),
  63. this, TQT_SLOT( textChanged( const TQString& ) ) );
  64. tqlayout->addWidget( m_lineEdit );
  65. m_clearButton = new TQToolButton( this );
  66. m_clearButton->setIconSet( KApplication::reverseLayout() ? SmallIconSet("locationbar_erase") : SmallIconSet( "clear_left" ) );
  67. m_clearButton->setEnabled( false );
  68. tqlayout->addWidget( m_clearButton );
  69. connect( m_clearButton, TQT_SIGNAL( clicked() ), m_lineEdit, TQT_SLOT( clear() ) );
  70. }
  71. void KPIM::DistributionListEditor::Line::textChanged( const TQString& text )
  72. {
  73. m_clearButton->setEnabled( !text.isEmpty() );
  74. if ( text.isEmpty() )
  75. emit cleared();
  76. emit textChanged();
  77. }
  78. void KPIM::DistributionListEditor::Line::setFocusToLineEdit()
  79. {
  80. m_lineEdit->setFocus();
  81. }
  82. void KPIM::DistributionListEditor::Line::setEntry( const KPIM::DistributionList::Entry& entry )
  83. {
  84. m_uid = entry.addressee.uid();
  85. m_initialText = entry.addressee.fullEmail( entry.email );
  86. m_lineEdit->setText( m_initialText );
  87. }
  88. KABC::Addressee KPIM::DistributionListEditor::Line::findAddressee( const TQString& name, const TQString& email ) const
  89. {
  90. if ( name.isEmpty() && email.isEmpty() )
  91. return KABC::Addressee();
  92. typedef KABC::Addressee::List List;
  93. const List byEmail = m_addressBook->findByEmail( email );
  94. if ( !byEmail.isEmpty() )
  95. {
  96. const List::ConstIterator end = byEmail.end();
  97. for ( List::ConstIterator it = byEmail.begin(); it != end; ++it )
  98. {
  99. if ( (*it).formattedName() == name )
  100. return *it;
  101. }
  102. return byEmail.first();
  103. }
  104. // no entry found, create new addressee:
  105. KABC::Addressee addressee;
  106. addressee.setUid( KApplication::randomString( 10 ) );
  107. addressee.setFormattedName( name );
  108. addressee.setEmails( email );
  109. m_addressBook->insertAddressee( addressee );
  110. return addressee;
  111. }
  112. KPIM::DistributionList::Entry KPIM::DistributionListEditor::Line::entry() const
  113. {
  114. const TQString text = m_lineEdit->text();
  115. TQString name;
  116. TQString email;
  117. KPIM::getNameAndMail(m_lineEdit->text(), name, email );
  118. KPIM::DistributionList::Entry res;
  119. if ( !m_uid.isNull() )
  120. {
  121. const KABC::Addressee addr = m_addressBook->findByUid( m_uid );
  122. if ( m_initialText == text || addr.formattedName() == name )
  123. res.addressee = addr;
  124. }
  125. if ( res.addressee.isEmpty() )
  126. res.addressee = findAddressee( name, email );
  127. res.email = res.addressee.preferredEmail() != email ? email : TQString();
  128. return res;
  129. }
  130. KPIM::DistributionListEditor::LineEdit::LineEdit( TQWidget* parent ) : KPIM::AddresseeLineEdit( parent )
  131. {
  132. allowDistributionLists( false );
  133. }
  134. KPIM::DistributionListEditor::EditorWidget::EditorWidget( KABC::AddressBook* book, TQWidget* parent )
  135. : KDialogBase( parent, /*name=*/0, /*modal=*/ true, /*caption=*/TQString(), KDialogBase::Ok|KDialogBase::Cancel ), d( new DistributionListEditor::EditorWidgetPrivate )
  136. {
  137. d->addressBook = book;
  138. Q_ASSERT( d->addressBook );
  139. d->lastLineId = 0;
  140. d->mapper = new TQSignalMapper( TQT_TQOBJECT(this) );
  141. connect( d->mapper, TQT_SIGNAL( mapped( int ) ),
  142. this, TQT_SLOT( lineTextChanged( int ) ) );
  143. setCaption( i18n( "Edit Distribution List" ) );
  144. TQWidget* main = new TQWidget( this );
  145. TQVBoxLayout* mainLayout = new TQVBoxLayout( main );
  146. mainLayout->setMargin( KDialog::marginHint() );
  147. mainLayout->setSpacing( KDialog::spacingHint() );
  148. TQHBoxLayout* nameLayout = new TQHBoxLayout;
  149. nameLayout->setSpacing( KDialog::spacingHint() );
  150. d->nameLabel = new TQLabel( main );
  151. d->nameLabel->setText( i18n( "Name:" ) );
  152. nameLayout->addWidget( d->nameLabel );
  153. d->nameLineEdit = new KLineEdit( main );
  154. nameLayout->addWidget( d->nameLineEdit );
  155. mainLayout->addLayout( nameLayout );
  156. mainLayout->addSpacing( 30 );
  157. d->memberListLabel = new TQLabel( main );
  158. d->memberListLabel->setText( i18n( "Distribution list members:" ) );
  159. mainLayout->addWidget( d->memberListLabel );
  160. d->scrollView = new TQScrollView( main );
  161. d->scrollView->setFrameShape( TQFrame::NoFrame );
  162. mainLayout->addWidget( d->scrollView );
  163. d->memberListWidget = new TQWidget( d->scrollView->viewport() );
  164. d->memberListWidget->tqsetSizePolicy( TQSizePolicy::MinimumExpanding, TQSizePolicy::MinimumExpanding );
  165. TQVBoxLayout* memberLayout = new TQVBoxLayout( d->memberListWidget );
  166. d->addresseeLayout = new TQVBoxLayout;
  167. d->addresseeLayout->setSpacing( KDialog::spacingHint() );
  168. memberLayout->addItem( d->addresseeLayout );
  169. memberLayout->addStretch();
  170. d->scrollView->addChild( d->memberListWidget );
  171. d->scrollView->setResizePolicy( TQScrollView::AutoOneFit );
  172. setMainWidget( main );
  173. KPIM::DistributionListEditor::Line* const last = d->addLineForEntry( KPIM::DistributionList::Entry() );
  174. const TQSize hint = tqsizeHint();
  175. resize( hint.width() * 3L/2, hint.height() );
  176. }
  177. KPIM::DistributionListEditor::EditorWidget::~EditorWidget()
  178. {
  179. delete d;
  180. }
  181. void KPIM::DistributionListEditor::EditorWidget::lineTextChanged( int id )
  182. {
  183. if ( id != d->lastLineId )
  184. return;
  185. d->addLineForEntry( KPIM::DistributionList::Entry() );
  186. d->scrollView->updateContents();
  187. }
  188. void KPIM::DistributionListEditor::EditorWidget::setDistributionList( const KPIM::DistributionList& list )
  189. {
  190. d->distListUid = list.uid();
  191. d->nameLineEdit->setText( list.name() );
  192. d->resource = list.resource();
  193. using KPIM::DistributionListEditor::Line;
  194. typedef TQValueList<Line*>::ConstIterator ListIterator;
  195. for ( ListIterator it = d->addressees.begin(), end = d->addressees.end(); it != end; ++it )
  196. {
  197. delete *it;
  198. }
  199. d->addressees.clear();
  200. typedef KPIM::DistributionList::Entry Entry;
  201. const Entry::List entries = list.entries( d->addressBook );
  202. for ( Entry::List::ConstIterator it = entries.begin(), end = entries.end(); it != end; ++it )
  203. {
  204. d->addLineForEntry( *it );
  205. }
  206. KPIM::DistributionListEditor::Line* const last = d->addLineForEntry( Entry() );
  207. last->setFocusToLineEdit();
  208. }
  209. KPIM::DistributionListEditor::Line* KPIM::DistributionListEditor::EditorWidgetPrivate::addLineForEntry( const KPIM::DistributionList::Entry& entry )
  210. {
  211. KPIM::DistributionListEditor::Line* line = new KPIM::DistributionListEditor::Line( addressBook, memberListWidget );
  212. line->setEntry( entry );
  213. addresseeLayout->addWidget( line );
  214. addressees.append( line );
  215. TQObject::connect( line, TQT_SIGNAL( textChanged() ),
  216. mapper, TQT_SLOT( map() ) );
  217. mapper->setMapping( TQT_TQOBJECT(line), ++lastLineId );
  218. line->setShown( true );
  219. return line;
  220. }
  221. void KPIM::DistributionListEditor::EditorWidget::slotOk()
  222. {
  223. const TQString name = d->nameLineEdit->text();
  224. const KPIM::DistributionList existing = KPIM::DistributionList::findByName( d->addressBook, name );
  225. if ( !existing.isEmpty() && existing.uid() != d->distListUid )
  226. {
  227. KMessageBox::error( this, i18n( "A distribution list with the name %1 already exists. Please choose another name." ).tqarg( name ), i18n( "Name in Use" ) );
  228. return;
  229. }
  230. KABC::Ticket *ticket = d->resource->requestSaveTicket();
  231. if ( !ticket ) {
  232. kdWarning(5720) << "Unable to get save ticket!" << endl;
  233. return;
  234. }
  235. KPIM::DistributionList list;
  236. list.setUid( d->distListUid.isNull() ? KApplication::randomString( 10 ) :d->distListUid );
  237. list.setName( name );
  238. list.setResource( d->resource );
  239. typedef TQValueList<KPIM::DistributionListEditor::Line*>::ConstIterator ListIterator;
  240. for ( ListIterator it = d->addressees.begin(), end = d->addressees.end(); it != end; ++it )
  241. {
  242. const KPIM::DistributionList::Entry entry = (*it)->entry();
  243. if ( entry.addressee.isEmpty() )
  244. continue;
  245. list.insertEntry( entry.addressee, entry.email );
  246. }
  247. d->distributionList = list;
  248. d->addressBook->insertAddressee( d->distributionList );
  249. if ( !d->resource->save( ticket ) ) {
  250. kdWarning(5720) << "Unable to save dist list!" << endl;
  251. }
  252. d->resource->releaseSaveTicket( ticket );
  253. if ( !KPIM::DistributionList::findByName( d->addressBook, name ).isEmpty() ) {
  254. accept();
  255. }
  256. }
  257. KPIM::DistributionList KPIM::DistributionListEditor::EditorWidget::distributionList() const
  258. {
  259. return d->distributionList;
  260. }
  261. #include "distributionlisteditor.moc"
  262. #include "distributionlisteditor_p.moc"