1/*
2 This file is part of the kabc library.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "emailselectdialog.h"
22#include "addresseedialog.h"
23
24#include <klocalizedstring.h>
25
26#include <QtCore/QPointer>
27#include <QButtonGroup>
28#include <QTreeWidget>
29#include <QGroupBox>
30#include <QLayout>
31#include <QRadioButton>
32
33
34using namespace KABC;
35
36class EmailSelectDialog::Private
37{
38 public:
39 QButtonGroup *mButtonGroup;
40};
41
42EmailSelectDialog::EmailSelectDialog( const QStringList &emails,
43 const QString &current,
44 QWidget *parent )
45 : KDialog( parent ), d( new Private )
46{
47 setCaption( i18n( "Select Email Address" ) );
48 setButtons( Ok );
49 setDefaultButton( Ok );
50
51 QFrame *topFrame = new QFrame( this );
52 setMainWidget( topFrame );
53
54 QBoxLayout *topLayout = new QVBoxLayout( topFrame );
55 QGroupBox *box = new QGroupBox( i18n( "Email Addresses" ) );
56 d->mButtonGroup = new QButtonGroup( box );
57 d->mButtonGroup->setExclusive( true );
58 topLayout->addWidget( box );
59 QVBoxLayout *layout = new QVBoxLayout;
60
61 QStringList::ConstIterator it;
62 QStringList::ConstIterator end( emails.end() );
63 for ( it = emails.begin(); it != end; ++it ) {
64 QRadioButton *button = new QRadioButton( *it, box );
65 d->mButtonGroup->addButton( button );
66 layout->addWidget( button );
67 if ( ( *it ) == current ) {
68 button->setChecked( true );
69 }
70 }
71 layout->addStretch( 1 );
72 box->setLayout( layout );
73}
74
75EmailSelectDialog::~EmailSelectDialog()
76{
77 delete d;
78}
79
80QString EmailSelectDialog::selected()
81{
82 QAbstractButton *button = d->mButtonGroup->checkedButton();
83 if ( button ) {
84 return button->text();
85 }
86 return QString();
87}
88
89QString EmailSelectDialog::getEmail( const QStringList &emails, const QString &current,
90 QWidget *parent )
91{
92 QPointer<EmailSelectDialog> dlg = new EmailSelectDialog( emails, current, parent );
93
94 QString result;
95 if ( dlg->exec() && dlg ) {
96 result = dlg->selected();
97 }
98
99 delete dlg;
100
101 return result;
102}
103
104class EditEntryItem : public QTreeWidgetItem
105{
106 public:
107 EditEntryItem( QTreeWidget *parent, const Addressee &addressee,
108 const QString &email=QString() ) :
109 QTreeWidgetItem( parent ),
110 mAddressee( addressee ),
111 mEmail( email )
112 {
113 setText( 0, addressee.realName() );
114 if ( email.isEmpty() ) {
115 setText( 1, addressee.preferredEmail() );
116 setText( 2, i18nc( "this the preferred email address", "Yes" ) );
117 } else {
118 setText( 1, email );
119 setText( 2, i18nc( "this is not preferred email address", "No" ) );
120 }
121 }
122
123 Addressee addressee() const
124 {
125 return mAddressee;
126 }
127
128 QString email() const
129 {
130 return mEmail;
131 }
132
133 private:
134 Addressee mAddressee;
135 QString mEmail;
136};
137