[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-pim
Subject:    [Kde-pim] [Patch] DistributionListEditor
From:       Mike Pilone <mpilone () slac ! com>
Date:       2002-05-13 19:37:36
[Download RAW message or body]

Over the weekend I spent some time rewriting the dist editor. I saw that 
some folks have been making minor fixes to it, but this is a complete 
rewrite that I think makes the dialog a little more usable.

If you like the patch, let me know and I will commit it.

Thanks,
-mike

-- 
Mike Pilone <mpilone@slac.com>        http://www.slac.com/mpilone/personal/
GPG Fingerprint = 856C 8B36 ECF7 9156 4611  7C6B C265 05C4 162F C3B5

See http://www.slac.com/mpilone/personal/mpilone_pub_key.gpg for full key.
See http://www.gnupg.org for GPG information.
["disteditor-mp1.diff" (text/x-diff)]

? diffs
? distributionlisteditor.cpp.org
? distributionlisteditor.h.org
? my.kabc
Index: Makefile.am
===================================================================
RCS file: /home/kde/kdelibs/kabc/Makefile.am,v
retrieving revision 1.18
diff -u -b -r1.18 Makefile.am
--- Makefile.am	2002/04/24 11:55:18	1.18
+++ Makefile.am	2002/05/13 19:35:27
@@ -12,13 +12,13 @@
   stdaddressbook.cpp vcardformat.cpp address.cpp geo.cpp timezone.cpp \
   vcardformatimpl.cpp addresseedialog.cpp distributionlist.cpp \
   distributionlisteditor.cpp resource.cpp resourcefile.cpp \
-  vcard21parser.cpp field.cpp
+  vcard21parser.cpp field.cpp distributionlisteditorwidget.cpp
 
 kabcincludedir = $(includedir)/kabc
 kabcinclude_HEADERS = address.h addressbook.h addressee.h addresseedialog.h \
   distributionlist.h distributionlisteditor.h format.h geo.h phonenumber.h \
   simpleformat.h stdaddressbook.h timezone.h vcardformat.h resource.h \
-  resourcefile.h vcard21parser.h field.h
+  resourcefile.h vcard21parser.h field.h distributionlisteditorwidget.h
 
 METASOURCES = AUTO
 
Index: distributionlisteditor.cpp
===================================================================
RCS file: /home/kde/kdelibs/kabc/distributionlisteditor.cpp,v
retrieving revision 1.9
diff -u -b -r1.9 distributionlisteditor.cpp
--- distributionlisteditor.cpp	2002/05/10 08:28:12	1.9
+++ distributionlisteditor.cpp	2002/05/13 19:35:27
@@ -1,325 +1,285 @@
 /*
-    This file is part of libkabc.
-    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
+    This file is part of KAddressBook.                                  
+    Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
 
-    This library is free software; you can redistribute it and/or
-    modify it under the terms of the GNU Library General Public
-    License as published by the Free Software Foundation; either
-    version 2 of the License, or (at your option) any later version.
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or   
+    (at your option) any later version.                                 
 
-    This library is distributed in the hope that it will be useful,
+    This program is distributed in the hope that it will be useful,     
     but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-    Library General Public License for more details.
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
+    GNU General Public License for more details.                        
 
-    You should have received a copy of the GNU Library General Public License
-    along with this library; see the file COPYING.LIB.  If not, write to
-    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-    Boston, MA 02111-1307, USA.
+    You should have received a copy of the GNU General Public License   
+    along with this program; if not, write to the Free Software         
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.           
+                                                                        
+    As a special exception, permission is given to link this program    
+    with any edition of Qt, and distribute the resulting executable,    
+    without including the source code for Qt in the source distribution.
 */
 
-#include <qlistview.h>
 #include <qlayout.h>
-#include <qpushbutton.h>
-#include <qcombobox.h>
-#include <qinputdialog.h>
-#include <qbuttongroup.h>
-#include <qradiobutton.h>
+#include <qgroupbox.h>
+#include <qtooltip.h>
+#include <qtoolbutton.h>
+#include <qlabel.h>
 
 #include <klocale.h>
-#include <kdebug.h>
+#include <kiconloader.h>
+#include <klistbox.h>
+#include <klineedit.h>
 
 #include "addressbook.h"
-#include "addresseedialog.h"
 #include "distributionlist.h"
-
+#include "distributionlisteditorwidget.h"
 #include "distributionlisteditor.h"
-#include "distributionlisteditor.moc"
 
-using namespace KABC;
+namespace KABC {
 
-EmailSelectDialog::EmailSelectDialog( const QStringList &emails, const QString &current,
-                                      QWidget *parent ) :
-  KDialogBase( KDialogBase::Plain, i18n("Select Email Address"), Ok, Ok,
-               parent )
-{
-  QFrame *topFrame = plainPage();
-  QBoxLayout *topLayout = new QVBoxLayout( topFrame );
-
-  mButtonGroup = new QButtonGroup( 1, Horizontal, i18n("Email Addresses"),
-                                   topFrame );
-  topLayout->addWidget( mButtonGroup );
-
-  QStringList::ConstIterator it;
-  for( it = emails.begin(); it != emails.end(); ++it ) {
-    QRadioButton *button = new QRadioButton( *it, mButtonGroup );
-    if ( (*it) == current ) {
-      button->setDown( true );
-    }
-  }
-}
+/////////////////////////////////
+// NameEditDialog
 
-QString EmailSelectDialog::selected()
+/** Internal class that has a line edit. This should probably
+* be generalized into a simple dialog in kdelibs/kdeui
+*/
+class NameDialog : public KDialogBase
 {
-  QButton *button = mButtonGroup->selected();
-  if ( button ) return button->text();
-  return QString::null;
-}
+  public:
+    NameDialog(QWidget *parent) 
+      : KDialogBase(Plain, i18n("Distribution List Name"), Ok | Cancel, 
+                    Ok, parent)
+      {
+        QWidget *page = plainPage();
+        QVBoxLayout *layout = new QVBoxLayout(page);
+        layout->setMargin(marginHint());
+        layout->setSpacing(spacingHint());
+        layout->setAutoAdd(true);
+        
+        (void) new QLabel(i18n("Please enter a name for the list:"), page);
+        mNameEdit = new KLineEdit(page, "mNameEdit");
+        mNameEdit->setFocus();
+      }
 
-QString EmailSelectDialog::getEmail( const QStringList &emails, const QString &current,
-                                     QWidget *parent )
-{
-  EmailSelectDialog *dlg = new EmailSelectDialog( emails, current, parent );
-  dlg->exec();
+    QString name() const { return mNameEdit->text(); }
+    void setName(const QString &name)
+    {
+      mNameEdit->setText(name);
+      mNameEdit->home(false);
+      mNameEdit->end(true); // select all
+    }
 
-  QString result = dlg->selected();
+  private:
+   KLineEdit *mNameEdit;
+};
 
-  delete dlg;
 
-  return result;
-}
+/////////////////////////////////
+// DistributionListEditor
 
-class EntryItem : public QListViewItem
+class DistributionListEditorPrivate
 {
   public:
-    EntryItem( QListView *parent, const Addressee &addressee,
-               const QString &email=QString::null ) :
-      QListViewItem( parent ),
-      mAddressee( addressee ),
-      mEmail( email )
-    {
-      setText( 0, addressee.realName() );
-      if( email.isEmpty() ) {
-        setText( 1, addressee.preferredEmail() );
-        setText( 2, i18n("Yes") );
-      } else {
-        setText( 1, email );
-        setText( 2, i18n("No") );
-      }
-    }
+    DistributionListEditorPrivate() {}
+    ~DistributionListEditorPrivate() {}
 
-    Addressee addressee() const
-    {
-      return mAddressee;
-    }
+    AddressBook *mAddressBook;
+    DistributionListManager *mManager;
+    int mIndex;
+    
+    // GUI
+    QToolButton *mRemoveButton;
+    QToolButton *mRenameButton;
+    KListBox *mDistListBox;
+    DistributionListEditorWidget *mEditorWidget;
+}; 
 
-    QString email() const
-    {
-      return mEmail;
-    }
+DistributionListEditor::DistributionListEditor(AddressBook *book,
+                                               QWidget *parent,
+                                               const char *name)
+  : KDialogBase(Plain, i18n("Distribution List Editor"),
+                Ok | Apply | Cancel, Ok, parent, name)
+{
+  d = new DistributionListEditorPrivate();
+  d->mAddressBook = book;
+  d->mIndex = -1;
 
-  private:
-    Addressee mAddressee;
-    QString mEmail;
-};
+  initGUI();
 
-DistributionListEditor::DistributionListEditor( AddressBook *addressBook, QWidget *parent) :
-  QWidget( parent ),
-  mAddressBook( addressBook )
-{
-  kdDebug() << "DistributionListEditor()" << endl;
-
-  QBoxLayout *topLayout = new QVBoxLayout( this );
-  topLayout->setMargin( KDialog::marginHint() );
-  topLayout->setSpacing( KDialog::spacingHint() );
-
-  QBoxLayout *nameLayout = new QHBoxLayout( topLayout) ;
-
-  mNameCombo = new QComboBox( this );
-  nameLayout->addWidget( mNameCombo );
-  connect( mNameCombo, SIGNAL( activated( int ) ), SLOT( updateEntryView() ) );
-
-  newButton = new QPushButton( i18n("New List"), this );
-  nameLayout->addWidget( newButton );
-  connect( newButton, SIGNAL( clicked() ), SLOT( newList() ) );
-
-  removeButton = new QPushButton( i18n("Remove List"), this );
-  nameLayout->addWidget( removeButton );
-  connect( removeButton, SIGNAL( clicked() ), SLOT( removeList() ) );
-
-  QGridLayout *gridLayout = new QGridLayout( topLayout, 3, 2 );
-  gridLayout->setColStretch(1, 1);
-
-  mAddresseeView = new QListView( this );
-  mAddresseeView->addColumn( i18n("Name") );
-  mAddresseeView->addColumn( i18n("Preferred Email") );
-  gridLayout->addWidget( mAddresseeView, 0, 0 );
-  connect(mAddresseeView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionAddresseeViewChanged()));
-
-  addEntryButton = new QPushButton( i18n("Add Entry"), this );
-  addEntryButton->setEnabled(false);
-  gridLayout->addWidget( addEntryButton, 1, 0 );
-  connect( addEntryButton, SIGNAL( clicked() ), SLOT( addEntry() ) );
-
-  mEntryView = new QListView( this );
-  mEntryView->addColumn( i18n("Name") );
-  mEntryView->addColumn( i18n("Email") );
-  mEntryView->addColumn( i18n("Use preferred") );
-  mEntryView->setEnabled(false);
-  gridLayout->addMultiCellWidget( mEntryView, 0, 0, 1, 2 );
-  connect(mEntryView,SIGNAL(selectionChanged ()),this, SLOT(slotSelectionEntryViewChanged()));
-
-  changeEmailButton = new QPushButton( i18n("Change Email"), this );
-  gridLayout->addWidget( changeEmailButton, 1, 1 );
-  connect( changeEmailButton, SIGNAL( clicked() ), SLOT( changeEmail() ) );
-
-  removeEntryButton = new QPushButton( i18n("Remove Entry"), this );
-  gridLayout->addWidget( removeEntryButton, 1, 2 );
-  connect( removeEntryButton, SIGNAL( clicked() ), SLOT( removeEntry() ) );
+  // Fill in the values
+  d->mManager = new DistributionListManager(book);
+  d->mManager->load();
 
-  mManager = new DistributionListManager( mAddressBook );
-  mManager->load();
+  d->mDistListBox->insertStringList(d->mManager->listNames());
 
-  updateAddresseeView();
-  updateNameCombo();
+  itemSelected(-1);
+  
+  enableButton(Apply, false);
 }
 
 DistributionListEditor::~DistributionListEditor()
 {
-  kdDebug() << "~DistributionListEditor()" << endl;
-
-  mManager->save();
-  delete mManager;
+  delete d;
 }
 
-void DistributionListEditor::slotSelectionEntryViewChanged()
+void DistributionListEditor::initGUI()
 {
-    EntryItem *entryItem =dynamic_cast<EntryItem *>( mEntryView->selectedItem() );
-    bool state=entryItem;
+  QWidget *page = plainPage();
 
-    changeEmailButton->setEnabled(state);
-    removeEntryButton->setEnabled(state);
-}
+  QBoxLayout *topLayout = new QHBoxLayout(page);
+  topLayout->setSpacing( spacingHint() );
+  topLayout->setMargin( marginHint() );
 
-void DistributionListEditor::newList()
-{
-  bool ok = false;
-  QString name = QInputDialog::getText( i18n("New Distribution List"),
-                                        i18n("Please enter name."),
-                                        QLineEdit::Normal, QString::null, &ok,
-                                        this );
-  if ( !ok || name.isEmpty() ) return;
-
-  new DistributionList( mManager, name );
-
-  mNameCombo->clear();
-  mNameCombo->insertStringList( mManager->listNames() );
-  mNameCombo->setCurrentItem( mNameCombo->count() );
+  QGroupBox *gb = new QGroupBox(i18n("Available Lists"), page);
+  topLayout->addWidget(gb);
+  topLayout->setStretchFactor(gb, 1);
 
-  updateEntryView();
-  slotSelectionAddresseeViewChanged();
-}
+  QBoxLayout *listLayout = new QVBoxLayout(gb);
+  listLayout->setSpacing( spacingHint() );
+  listLayout->setMargin( 15 );
 
-void DistributionListEditor::removeList()
-{
-  delete mManager->list( mNameCombo->currentText() );
-  mNameCombo->removeItem( mNameCombo->currentItem() );
+  d->mDistListBox = new KListBox(gb, "mDistListBox");
+  connect(d->mDistListBox, SIGNAL(highlighted(int)), SLOT(itemSelected(int)));
+  listLayout->addWidget(d->mDistListBox);
 
-  updateEntryView();
-  slotSelectionAddresseeViewChanged();
-}
+  QBoxLayout *buttonLayout = new QHBoxLayout();
+  buttonLayout->setSpacing( spacingHint() );
+  listLayout->addLayout(buttonLayout);
 
-void DistributionListEditor::addEntry()
-{
-  AddresseeItem *addresseeItem =
-      dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
+  QToolButton *addButton = new QToolButton(gb);
+  addButton->setIconSet(SmallIconSet("filenew"));
+  QToolTip::add(addButton, i18n("Add a new distribution list"));
+  connect(addButton, SIGNAL(clicked()), SLOT(add()));
+  buttonLayout->addWidget(addButton);
 
-  if( !addresseeItem ) {
-    kdDebug() << "DLE::addEntry(): No addressee selected." << endl;
-    return;
-  }
+  d->mRemoveButton = new QToolButton(gb, "mRemoveButton");
+  d->mRemoveButton->setIconSet(SmallIconSet("remove"));
+  QToolTip::add(d->mRemoveButton, i18n("Remove the selected distribution list"));
+  connect(d->mRemoveButton, SIGNAL(clicked()), SLOT(remove()));
+  buttonLayout->addWidget(d->mRemoveButton);
 
-  DistributionList *list = mManager->list( mNameCombo->currentText() );
-  if ( !list ) {
-    kdDebug() << "DLE::addEntry(): No dist list '" << mNameCombo->currentText() << "'" << endl;
-    return;
-  }
+  d->mRenameButton = new QToolButton(gb, "mRenameButton");
+  d->mRenameButton->setIconSet(SmallIconSet("edit"));
+  QToolTip::add(d->mRenameButton, i18n("Rename the selected distribution list"));
+  connect(d->mRenameButton, SIGNAL(clicked()), SLOT(rename()));
+  buttonLayout->addWidget(d->mRenameButton);
 
-  list->insertEntry( addresseeItem->addressee() );
-  updateEntryView();
-  slotSelectionAddresseeViewChanged();
+  d->mEditorWidget = new DistributionListEditorWidget(d->mAddressBook,
+                                                      page, "mEditorWidget");
+  d->mEditorWidget->setMinimumWidth(300);
+  connect(d->mEditorWidget, SIGNAL(modified()), SLOT(modified()));
+  topLayout->addWidget(d->mEditorWidget);
+  topLayout->setStretchFactor(d->mEditorWidget, 3);
 }
 
-void DistributionListEditor::removeEntry()
+void DistributionListEditor::add()
 {
-  DistributionList *list = mManager->list( mNameCombo->currentText() );
-  if ( !list ) return;
-
-  EntryItem *entryItem =
-      dynamic_cast<EntryItem *>( mEntryView->selectedItem() );
-  if ( !entryItem ) return;
+  NameDialog dialog(this);
+  if (dialog.exec())
+  {
+    (void) new DistributionList(d->mManager, dialog.name());
+    d->mDistListBox->insertItem(dialog.name());
+    d->mDistListBox->setCurrentItem(d->mDistListBox->count()-1);
+    d->mDistListBox->ensureCurrentVisible();
 
-  list->removeEntry( entryItem->addressee(), entryItem->email() );
-  delete entryItem;
+    modified();
+  }
 }
 
-void DistributionListEditor::changeEmail()
+void DistributionListEditor::remove()
 {
-  DistributionList *list = mManager->list( mNameCombo->currentText() );
-  if ( !list ) return;
+  QString name = d->mDistListBox->currentText();
+  int index = d->mDistListBox->currentItem();
+  DistributionList *list = d->mManager->list(name);
 
-  EntryItem *entryItem =
-      dynamic_cast<EntryItem *>( mEntryView->selectedItem() );
-  if ( !entryItem ) return;
+  // Remove it from the list view so current changes
+  d->mDistListBox->removeItem(index);
 
-  QString email = EmailSelectDialog::getEmail( entryItem->addressee().emails(),
-                                               entryItem->email(), this );
-  list->removeEntry( entryItem->addressee(), entryItem->email() );
-  list->insertEntry( entryItem->addressee(), email );
+  // If we have a valid list, remove it as well
+  if (list)
+  {
+    d->mManager->remove(list);
+    delete list;
 
-  updateEntryView();
+    modified();
+  }
 }
 
-void DistributionListEditor::updateEntryView()
+void DistributionListEditor::rename()
 {
-  mEntryView->clear();
+  NameDialog dialog(this);
+  QString name = d->mDistListBox->currentText();
 
-  DistributionList *list = mManager->list( mNameCombo->currentText() );
-  if ( !list ) {
-    removeButton->setEnabled(false);
-    changeEmailButton->setEnabled(false);
-    removeEntryButton->setEnabled(false);
-    mEntryView->setEnabled(false);
-    return;
-  } else {
-    removeButton->setEnabled(true);
-    mEntryView->setEnabled(true);
+  dialog.setName(name);
+  if (dialog.exec())
+  {
+    DistributionList *list = d->mManager->list(name);
+    if (list)
+    {
+      d->mManager->remove(list);
+      list->setName(dialog.name());
+      d->mManager->insert(list); 
   }
+    
+    d->mDistListBox->changeItem(dialog.name(), d->mDistListBox->currentItem());
 
-  DistributionList::Entry::List entries = list->entries();
-  DistributionList::Entry::List::ConstIterator it;
-  for( it = entries.begin(); it != entries.end(); ++it ) {
-    new EntryItem( mEntryView, (*it).addressee, (*it).email );
+    modified();
   }
+}
+
+void DistributionListEditor::itemSelected(int id)
+{
+  bool e = id >= 0;
+  
+  d->mRemoveButton->setEnabled(e);
+  d->mRenameButton->setEnabled(e);
+  d->mEditorWidget->setEnabled(e);
 
-  EntryItem *entryItem =dynamic_cast<EntryItem *>( mEntryView->selectedItem() );
-  bool state=entryItem;
+  // save the current one if there is one
+  if (d->mIndex >= 0)
+    (void) d->mEditorWidget->distributionList();
 
-  changeEmailButton->setEnabled(state);
-  removeEntryButton->setEnabled(state);
+  // Now update the new one
+  if (e)
+  {
+    d->mIndex = id;
+    QString name = d->mDistListBox->currentText();
+    d->mEditorWidget->setDistributionList(d->mManager->list(name));
+  }
+  else
+  {
+    d->mEditorWidget->clear();
+  }
 }
 
-void DistributionListEditor::updateAddresseeView()
+void DistributionListEditor::slotOk()
 {
-  mAddresseeView->clear();
+  slotApply();
 
-  AddressBook::Iterator it;
-  for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
-    new AddresseeItem( mAddresseeView, *it );
-  }
+  KDialogBase::slotOk();
 }
 
-void DistributionListEditor::updateNameCombo()
+void DistributionListEditor::slotApply()
 {
-  mNameCombo->insertStringList( mManager->listNames() );
+  // Save the current one if there is one
+  if (d->mIndex >= 0)
+    (void) d->mEditorWidget->distributionList();
+    
+  // Save the entire dist list
+  d->mManager->save();  
+  
+  enableButton(Apply, false);
 
-  updateEntryView();
+  KDialogBase::slotApply();
 }
 
-void DistributionListEditor::slotSelectionAddresseeViewChanged()
+void DistributionListEditor::modified()
 {
-    AddresseeItem *addresseeItem =
-        dynamic_cast<AddresseeItem *>( mAddresseeView->selectedItem() );
-    bool state=addresseeItem;
-    addEntryButton->setEnabled( state && !mNameCombo->currentText().isEmpty());
+  enableButton(Apply, true);
 }
+
+} // end namespace KABC
+
+#include "distributionlisteditor.moc"
Index: distributionlisteditor.h
===================================================================
RCS file: /home/kde/kdelibs/kabc/distributionlisteditor.h,v
retrieving revision 1.3
diff -u -b -r1.3 distributionlisteditor.h
--- distributionlisteditor.h	2001/11/22 10:13:57	1.3
+++ distributionlisteditor.h	2002/05/13 19:35:27
@@ -1,82 +1,61 @@
+#ifndef DISTRIBUTIONLISTEDITOR_H
+#define DISTRIBUTIONLISTEDITOR_H
+
 /*
     This file is part of libkabc.
-    Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
+    Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
 
-    This library is free software; you can redistribute it and/or
-    modify it under the terms of the GNU Library General Public
-    License as published by the Free Software Foundation; either
-    version 2 of the License, or (at your option) any later version.
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or   
+    (at your option) any later version.                                 
 
-    This library is distributed in the hope that it will be useful,
+    This program is distributed in the hope that it will be useful,     
     but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-    Library General Public License for more details.
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
+    GNU General Public License for more details.                        
 
-    You should have received a copy of the GNU Library General Public License
-    along with this library; see the file COPYING.LIB.  If not, write to
-    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-    Boston, MA 02111-1307, USA.
+    You should have received a copy of the GNU General Public License   
+    along with this program; if not, write to the Free Software         
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.           
+                                                                        
+    As a special exception, permission is given to link this program    
+    with any edition of Qt, and distribute the resulting executable,    
+    without including the source code for Qt in the source distribution.
 */
-#ifndef KABC_DISTRIBUTIONLISTEDITOR_H
-#define KABC_DISTRIBUTIONLISTEDITOR_H
-
-#include <qwidget.h>
 
 #include <kdialogbase.h>
 
-class QListView;
-class QComboBox;
-class QButtonGroup;
+class QWidget;
 
 namespace KABC {
 
 class AddressBook;
-class DistributionListManager;
+class DistributionListEditorPrivate;
 
-class EmailSelectDialog : public KDialogBase
+class DistributionListEditor : public KDialogBase
 {
-  public:
-    EmailSelectDialog( const QStringList &emails, const QString &current,
-                       QWidget *parent );
-    
-    QString selected();
-
-    static QString getEmail( const QStringList &emails, const QString &current,
-                             QWidget *parent );
-
-  private:
-    QButtonGroup *mButtonGroup;
-};
-
-
-class DistributionListEditor : public QWidget
-{
     Q_OBJECT
+  
   public:
-    DistributionListEditor( AddressBook *, QWidget *parent );
+    DistributionListEditor(AddressBook *book, QWidget *parent, 
+                           const char *name = 0);
     virtual ~DistributionListEditor();
+    
+  protected slots:
+    void add();
+    void remove();
+    void rename();
+    void itemSelected(int id);
+    void modified();
 
-  private slots:
-    void newList();
-    void removeList();
-    void addEntry();
-    void removeEntry();
-    void changeEmail();
-    void updateEntryView();
-    void updateAddresseeView();
-    void updateNameCombo();
-    void slotSelectionEntryViewChanged();
-    void slotSelectionAddresseeViewChanged();
+    virtual void slotApply();
+    virtual void slotOk();
 
   private:
-    QComboBox *mNameCombo;  
-    QListView *mEntryView;
-    QListView *mAddresseeView;
-
-    AddressBook *mAddressBook;
-    DistributionListManager *mManager;
-    QPushButton *newButton, *removeButton;
-    QPushButton *changeEmailButton,*removeEntryButton,*addEntryButton;
+    void initGUI();
+    
+    DistributionListEditorPrivate *d;
 };
 
 }
Index: distributionlisteditorwidget.cpp
===================================================================
RCS file: distributionlisteditorwidget.cpp
diff -N distributionlisteditorwidget.cpp
--- /dev/null	Fri Feb  1 11:53:05 2002
+++ distributionlisteditorwidget.cpp	Mon May 13 21:35:27 2002
@@ -0,0 +1,388 @@
+/*                                                                      
+    This file is part of KAddressBook.                                  
+    Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
+                                                                        
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or   
+    (at your option) any later version.                                 
+                                                                        
+    This program is distributed in the hope that it will be useful,     
+    but WITHOUT ANY WARRANTY; without even the implied warranty of      
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
+    GNU General Public License for more details.                        
+                                                                        
+    You should have received a copy of the GNU General Public License   
+    along with this program; if not, write to the Free Software         
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.           
+                                                                        
+    As a special exception, permission is given to link this program    
+    with any edition of Qt, and distribute the resulting executable,    
+    without including the source code for Qt in the source distribution.
+*/                                                                      
+
+#include <qlayout.h>
+#include <qtoolbutton.h>
+#include <qlabel.h>
+#include <qtooltip.h>
+#include <qheader.h>
+
+#include <klistview.h>
+#include <kiconloader.h>
+#include <kdialog.h>
+#include <klocale.h>
+#include <kdebug.h>
+
+#include "addressbook.h"
+#include "distributionlisteditorwidget.h"
+
+namespace KABC {
+
+/////////////////////////////
+// AddresseeListViewItem
+class AddresseeListViewItem : public KListViewItem
+{
+  public:
+    AddresseeListViewItem(const Addressee &a, KListView *parent, 
+                          const QString &text)
+      : KListViewItem(parent, text)
+    {
+      mAddressee = a;
+    }
+    
+    ~AddresseeListViewItem() {}
+    
+    const Addressee &addressee() const { return mAddressee; }
+  
+  private:
+    Addressee mAddressee;
+                          
+};
+
+//////////////////////////////
+// DistributionListEditorWidget
+
+class DistributionListEditorWidgetPrivate
+{
+  public:
+    DistributionListEditorWidgetPrivate() {}
+    ~DistributionListEditorWidgetPrivate() {}
+    
+    void setEnabled(bool enabled);
+    
+    DistributionList *mList;
+    AddressBook *mAddressBook;
+    
+    // GUI
+    KListView *mAvailableView;
+    KListView *mSelectedView;
+    QToolButton *mAddButton;
+    QToolButton *mRemoveButton;
+};
+  
+DistributionListEditorWidget::DistributionListEditorWidget(AddressBook *book,
+                                                           QWidget *parent, 
+                                                           const char *name)
+  : QWidget(parent, name)
+{
+  d = new DistributionListEditorWidgetPrivate();
+  d->mAddressBook = book;
+  
+  initGUI();
+  
+  availableSelectionChanged();
+  selectedSelectionChanged();
+}
+
+DistributionListEditorWidget::~DistributionListEditorWidget()
+{
+  delete d;
+}
+
+void DistributionListEditorWidget::clear()
+{
+  // Clear all the widgets
+  d->mAvailableView->clear();
+  d->mSelectedView->clear();
+  
+  // Set the list to null
+  d->mList = 0;
+}
+
+void DistributionListEditorWidget::setDistributionList(DistributionList *list)
+{
+  bool b = signalsBlocked();
+  blockSignals(true);
+  
+  clear();
+  
+  d->mList = list;
+  
+  // populate the left and right list boxes
+  QListViewItem *item;
+  QStringList emailList;
+  QStringList::Iterator emailIter;
+  Addressee a;
+  AddressBook::Iterator iter;
+  for (iter = d->mAddressBook->begin(); iter != d->mAddressBook->end(); ++iter)
+  {
+    a = *iter;
+    
+    item = new AddresseeListViewItem(*iter, d->mAvailableView, a.realName());
+    
+    // Now add all the email addresses
+    emailList = a.emails();
+    for (emailIter = emailList.begin(); emailIter != emailList.end();
+          ++emailIter)
+    {
+      (void) new KListViewItem(item, *emailIter);
+    }
+  }
+  
+  // now move all the selected addressees to the other list
+  DistributionList::Entry::List entryList = d->mList->entries();
+  DistributionList::Entry::List::Iterator entryIter;
+  for (entryIter = entryList.begin(); entryIter != entryList.end(); ++entryIter)
+  {
+    if (selectEntry(*entryIter))
+      add();
+  }
+  
+  // now delselect everything
+  d->mAvailableView->selectAll(false);
+  d->mSelectedView->selectAll(false);
+  
+  // close all the items. Only the selected list will be expanded
+  for (item = d->mSelectedView->firstChild(); item; item = item->nextSibling())
+    item->setOpen(false);
+  
+  // scroll to the top
+  d->mAvailableView->ensureItemVisible(d->mAvailableView->firstChild());
+  d->mSelectedView->ensureItemVisible(d->mSelectedView->firstChild());
+  
+  availableSelectionChanged();
+  selectedSelectionChanged();
+  
+  blockSignals(b);
+}
+
+DistributionList *DistributionListEditorWidget::distributionList()
+{
+  // Save the list from the gui and then return it
+  // There is really nothing to save since the distribution list i modified
+  // in real time.
+  return d->mList;
+}
+
+void DistributionListEditorWidget::add()
+{
+  // update the dist list
+  QListViewItem *item = d->mAvailableView->selectedItem();
+  AddresseeListViewItem *aItem = 
+                      dynamic_cast<AddresseeListViewItem*>(item->parent());
+                      
+  d->mList->insertEntry(aItem->addressee(), item->text(0));
+  
+  moveItem(d->mAvailableView, d->mSelectedView);
+  
+  emit modified();
+}
+
+void DistributionListEditorWidget::remove()
+{
+  // update the dist list
+  QListViewItem *item = d->mSelectedView->selectedItem();
+  AddresseeListViewItem *aItem = 
+                      dynamic_cast<AddresseeListViewItem*>(item->parent());
+  d->mList->removeEntry(aItem->addressee(), item->text(0));
+  
+  moveItem(d->mSelectedView, d->mAvailableView);
+  
+  emit modified();
+}
+
+void DistributionListEditorWidget::moveItem(KListView *source, 
+                                            KListView *target)
+{
+  QListViewItem *item;
+  AddresseeListViewItem *aItem;
+  AddresseeListViewItem *targetItem;
+  QString email;
+  
+  item = source->selectedItem();
+  if (!item)
+    return;
+    
+  aItem = dynamic_cast<AddresseeListViewItem*>(item->parent());
+  
+  // Find the root item
+  targetItem = findItem(target, aItem->addressee().uid());
+  
+  if (!targetItem)
+  {
+    // Add a parent item
+    targetItem = new AddresseeListViewItem(aItem->addressee(), target,
+                                           aItem->addressee().realName());
+  }
+  
+  // Now add the child item to the selected list
+  email = item->text(0);
+  aItem->takeItem(item);
+  delete item;
+  
+  // If deleting the item leaves the parent with 0 children, get rid of the
+  // parent
+  if (aItem->childCount() == 0)
+  {
+    source->takeItem(aItem);
+    delete aItem;
+  }
+  
+  item = new KListViewItem(targetItem, email);
+  target->setSelected(item, true);
+  target->ensureItemVisible(item);
+}
+
+AddresseeListViewItem *DistributionListEditorWidget::findItem(KListView *view,
+                                                           const QString &uid)
+{
+  bool found = false;
+  QListViewItem *item;
+  AddresseeListViewItem *aItem = 0;
+  for (item = view->firstChild(); item && !found; )
+  {
+    aItem = dynamic_cast<AddresseeListViewItem*>(item);
+    if (aItem->addressee().uid() == uid)
+      found = true;
+    else
+      item = item->nextSibling();
+  }
+  
+  aItem = dynamic_cast<AddresseeListViewItem*>(item);
+  return aItem;
+}
+                                                     
+bool DistributionListEditorWidget::selectEntry(DistributionList::Entry &e)
+{
+  // find the addressee in the list
+  bool found = false;
+  QListViewItem *item;
+  AddresseeListViewItem *aItem = 0;
+  QString uid = e.addressee.uid();
+  for (item = d->mAvailableView->firstChild();
+       item && !found;)
+  {
+    aItem = dynamic_cast<AddresseeListViewItem*>(item);
+    if (aItem)
+    {
+      if (uid == aItem->addressee().uid())
+        found = true;
+    }
+    
+    if (!found)
+      item = item->nextSibling();
+  }
+  
+  if (found)
+  {
+    // We found the addressee, now try to find the email address
+    found = false;
+    for (item = aItem->firstChild(); item && !found; item = item->nextSibling())
+    {
+      if (item->text(0) == e.email)
+      {
+        found = true;
+        d->mAvailableView->setSelected(item, true);
+      }
+    }
+  }
+  
+  return found;
+}
+
+void DistributionListEditorWidget::availableSelectionChanged()
+{
+  QListViewItem *item = d->mAvailableView->selectedItem();
+  d->mAddButton->setEnabled(item && item->parent());
+}
+
+void DistributionListEditorWidget::selectedSelectionChanged()
+{
+  QListViewItem *item = d->mSelectedView->selectedItem();
+  d->mRemoveButton->setEnabled(item && item->parent());
+}
+    
+void DistributionListEditorWidget::initGUI()
+{
+  QBoxLayout *topLayout = new QHBoxLayout(this);
+  topLayout->setSpacing( KDialog::spacingHint() );
+  
+  // Available list
+  QBoxLayout *listLayout = new QVBoxLayout();
+  listLayout->setSpacing( KDialog::spacingHint() );
+  topLayout->addLayout( listLayout );
+  topLayout->setStretchFactor(listLayout, 1);
+  
+  QLabel *label = new QLabel(i18n("&Available Contacts:"), this);
+  listLayout->addWidget(label);
+  d->mAvailableView = new KListView(this, "mAvailableView");
+  d->mAvailableView->setSelectionModeExt(KListView::Single);
+  d->mAvailableView->addColumn(i18n("Contacts"));
+  d->mAvailableView->header()->hide();
+  d->mAvailableView->setRootIsDecorated(true);
+  connect(d->mAvailableView, SIGNAL(selectionChanged()),
+          SLOT(availableSelectionChanged()));
+  label->setBuddy(d->mAvailableView);
+  listLayout->addWidget(d->mAvailableView);
+  
+  
+  // Buttons
+  listLayout = new QVBoxLayout();
+  listLayout->setSpacing( KDialog::spacingHint() );
+  topLayout->addLayout( listLayout );
+  
+  QWidget *spacer = new QWidget(this);
+  spacer->setMinimumHeight(30);
+  listLayout->addWidget(spacer);
+  listLayout->setStretchFactor(spacer, 1);
+  
+  d->mAddButton = new QToolButton(this, "mAddButton");
+  QToolTip::add(d->mAddButton, i18n("Add selected contact"));
+  d->mAddButton->setIconSet(SmallIconSet("forward"));
+  connect(d->mAddButton, SIGNAL(clicked()), SLOT(add()));
+  listLayout->addWidget(d->mAddButton);
+  
+  d->mRemoveButton = new QToolButton(this, "mRemoveButton");
+  QToolTip::add(d->mRemoveButton, i18n("Remove selected contact"));
+  d->mRemoveButton->setIconSet(SmallIconSet("back"));
+  connect(d->mRemoveButton, SIGNAL(clicked()), SLOT(remove()));
+  listLayout->addWidget(d->mRemoveButton);
+  
+  spacer = new QWidget(this);
+  spacer->setMinimumHeight(30);
+  listLayout->addWidget(spacer);
+  listLayout->setStretchFactor(spacer, 1);
+  
+  // selected list
+  listLayout = new QVBoxLayout();
+  listLayout->setSpacing( KDialog::spacingHint() );
+  topLayout->addLayout( listLayout );
+  topLayout->setStretchFactor(listLayout, 1);
+  
+  label = new QLabel(i18n("&Selected Contacts:"), this);
+  listLayout->addWidget(label);
+  d->mSelectedView = new KListView(this, "mSelectedView");
+  d->mSelectedView->setSelectionModeExt(KListView::Single);
+  d->mSelectedView->addColumn(i18n("Contacts"));
+  d->mSelectedView->header()->hide();
+  d->mSelectedView->setRootIsDecorated(true);
+  connect(d->mSelectedView, SIGNAL(selectionChanged()),
+          SLOT(selectedSelectionChanged()));
+  label->setBuddy(d->mSelectedView);
+  listLayout->addWidget(d->mSelectedView);
+  
+}
+
+}
+
+#include "distributionlisteditorwidget.moc"
Index: distributionlisteditorwidget.h
===================================================================
RCS file: distributionlisteditorwidget.h
diff -N distributionlisteditorwidget.h
--- /dev/null	Fri Feb  1 11:53:05 2002
+++ distributionlisteditorwidget.h	Mon May 13 21:35:27 2002
@@ -0,0 +1,73 @@
+#ifndef DISTRIBUTIONLISTEDITORWIDGET_H
+#define DISTRIBUTIONLISTEDITORWIDGET_H
+
+/*                                                                      
+    This file is part of KAddressBook.                                  
+    Copyright (c) 2002 Mike Pilone <mpilone@slac.com>                   
+                                                                        
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or   
+    (at your option) any later version.                                 
+                                                                        
+    This program is distributed in the hope that it will be useful,     
+    but WITHOUT ANY WARRANTY; without even the implied warranty of      
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the        
+    GNU General Public License for more details.                        
+                                                                        
+    You should have received a copy of the GNU General Public License   
+    along with this program; if not, write to the Free Software         
+    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.           
+                                                                        
+    As a special exception, permission is given to link this program    
+    with any edition of Qt, and distribute the resulting executable,    
+    without including the source code for Qt in the source distribution.
+*/                                                                      
+
+#include <qwidget.h>
+
+#include "distributionlist.h"
+
+class QListViewItem;
+class QString;
+class KListView;
+
+namespace KABC {
+
+class AddressBook;
+class AddresseeListViewItem;
+class DistributionListEditorWidgetPrivate;
+class DistributionListEditorWidget : public QWidget
+{
+  Q_OBJECT
+  
+  public:
+    DistributionListEditorWidget(AddressBook *book, QWidget *parent, 
+                                 const char *name = 0);
+    ~DistributionListEditorWidget();
+    
+    void clear();
+    
+    void setDistributionList(DistributionList *list);
+    DistributionList *distributionList();
+    
+  signals:
+    void modified();
+    
+  protected slots:
+    void add();
+    void remove();
+    void availableSelectionChanged();
+    void selectedSelectionChanged();
+    
+  private:
+    void initGUI();
+    bool selectEntry(DistributionList::Entry &e);
+    AddresseeListViewItem *findItem(KListView *view, const QString &uid);
+    void moveItem(KListView *source, KListView *target);
+    
+    DistributionListEditorWidgetPrivate *d;
+};
+
+}
+#endif
Index: testdistlist.cpp
===================================================================
RCS file: /home/kde/kdelibs/kabc/testdistlist.cpp,v
retrieving revision 1.2
diff -u -b -r1.2 testdistlist.cpp
--- testdistlist.cpp	2001/11/12 23:08:10	1.2
+++ testdistlist.cpp	2002/05/13 19:35:28
@@ -49,6 +49,7 @@
       new DistributionListEditor( StdAddressBook::self(), 0 );
 
   editor->show();
+  app.setMainWidget(editor);
 
   QObject::connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
 

_______________________________________________
kde-pim mailing list
kde-pim@mail.kde.org
http://mail.kde.org/mailman/listinfo/kde-pim
kde-pim home page at http://pim.kde.org/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic