summaryrefslogtreecommitdiffabout
path: root/kabc/addresseedialog.cpp
Unidiff
Diffstat (limited to 'kabc/addresseedialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/addresseedialog.cpp264
1 files changed, 264 insertions, 0 deletions
diff --git a/kabc/addresseedialog.cpp b/kabc/addresseedialog.cpp
new file mode 100644
index 0000000..033e857
--- a/dev/null
+++ b/kabc/addresseedialog.cpp
@@ -0,0 +1,264 @@
1/*
2 This file is part of libkabc.
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21#include <qlayout.h>
22#include <qpushbutton.h>
23#include <qgroupbox.h>
24#include <qapplication.h>
25#include <qregexp.h>
26
27#include <klocale.h>
28#include <kdebug.h>
29#include <kglobalsettings.h>
30
31#include "stdaddressbook.h"
32
33#include "addresseedialog.h"
34//#include "addresseedialog.moc"
35
36using namespace KABC;
37
38AddresseeItem::AddresseeItem( QListView *parent, const Addressee &addressee ) :
39 QListViewItem( parent ),
40 mAddressee( addressee )
41{
42 setText( Name, addressee.realName() );
43 setText( Email, addressee.preferredEmail() );
44}
45
46QString AddresseeItem::key( int column, bool ) const
47{
48 /* LR
49 if (column == Email) {
50 QString value = text(Email);
51 QRegExp emailRe("<\\S*>");
52 int match = emailRe.search(value);
53 if (match > -1)
54 value = value.mid(match + 1, emailRe.matchedLength() - 2);
55
56 return value.lower();
57 }
58 */
59 return text(column).lower();
60}
61
62AddresseeDialog::AddresseeDialog( QWidget *parent, bool multiple ) :
63 KDialogBase( KDialogBase::Plain, i18n("Select Addressee"),
64 Ok|Cancel, Ok, parent ), mMultiple( multiple )
65{
66 QWidget *topWidget = plainPage();
67
68 QBoxLayout *topLayout = new QHBoxLayout( topWidget );
69 QBoxLayout *listLayout = new QVBoxLayout;
70 topLayout->addLayout( listLayout );
71
72 mAddresseeList = new KListView( topWidget );
73 mAddresseeList->addColumn( i18n("Name") );
74 mAddresseeList->addColumn( i18n("Email") );
75 mAddresseeList->setAllColumnsShowFocus( true );
76 mAddresseeList->setFullWidth( true );
77 listLayout->addWidget( mAddresseeList );
78 connect( mAddresseeList, SIGNAL( doubleClicked( QListViewItem * ) ),
79 SLOT( slotOk() ) );
80 connect( mAddresseeList, SIGNAL( selectionChanged( QListViewItem * ) ),
81 SLOT( updateEdit( QListViewItem * ) ) );
82
83 mAddresseeEdit = new KLineEdit( topWidget );
84 //mAddresseeEdit->setCompletionMode( KGlobalSettings::CompletionAuto );
85 // connect( mAddresseeEdit->completionObject(), SIGNAL( match( const QString & ) ),
86 // SLOT( selectItem( const QString & ) ) );
87 mAddresseeEdit->setFocus();
88 //mAddresseeEdit->completionObject()->setIgnoreCase( true );
89 listLayout->addWidget( mAddresseeEdit );
90
91 //setInitialSize( QSize( 450, 300 ) );
92
93 if ( mMultiple ) {
94 QBoxLayout *selectedLayout = new QVBoxLayout;
95 topLayout->addLayout( selectedLayout );
96 topLayout->setSpacing( spacingHint() );
97
98 QGroupBox *selectedGroup = new QGroupBox( 1, Horizontal, i18n("Selected"),
99 topWidget );
100 selectedLayout->addWidget( selectedGroup );
101
102 mSelectedList = new KListView( selectedGroup );
103 mSelectedList->addColumn( i18n("Name") );
104 mSelectedList->addColumn( i18n("Email") );
105 mSelectedList->setAllColumnsShowFocus( true );
106 mSelectedList->setFullWidth( true );
107 connect( mSelectedList, SIGNAL( doubleClicked( QListViewItem * ) ),
108 SLOT( removeSelected() ) );
109
110 QPushButton *unselectButton = new QPushButton( i18n("Unselect"), selectedGroup );
111 connect ( unselectButton, SIGNAL( clicked() ), SLOT( removeSelected() ) );
112
113 connect( mAddresseeList, SIGNAL( clicked( QListViewItem * ) ),
114 SLOT( addSelected( QListViewItem * ) ) );
115
116 // setInitialSize( QSize( 650, 350 ) );
117 }
118
119 mAddressBook = StdAddressBook::self( true );
120 connect( mAddressBook, SIGNAL( addressBookChanged( AddressBook* ) ),
121 SLOT( addressBookChanged() ) );
122 connect( mAddressBook, SIGNAL( loadingFinished( Resource* ) ),
123 SLOT( addressBookChanged() ) );
124
125 loadAddressBook();
126}
127
128AddresseeDialog::~AddresseeDialog()
129{
130}
131
132void AddresseeDialog::loadAddressBook()
133{
134 mAddresseeList->clear();
135 mItemDict.clear();
136 //mAddresseeEdit->completionObject()->clear();
137
138 AddressBook::Iterator it;
139 for( it = mAddressBook->begin(); it != mAddressBook->end(); ++it ) {
140 AddresseeItem *item = new AddresseeItem( mAddresseeList, (*it) );
141 addCompletionItem( (*it).realName(), item );
142 addCompletionItem( (*it).preferredEmail(), item );
143 }
144}
145
146void AddresseeDialog::addCompletionItem( const QString &str, QListViewItem *item )
147{
148 if ( str.isEmpty() ) return;
149
150 mItemDict.insert( str, item );
151 //mAddresseeEdit->completionObject()->addItem( str );
152}
153
154void AddresseeDialog::selectItem( const QString &str )
155{
156 if ( str.isEmpty() ) return;
157
158 QListViewItem *item = mItemDict.find( str );
159 if ( item ) {
160 mAddresseeList->blockSignals( true );
161 mAddresseeList->setSelected( item, true );
162 mAddresseeList->ensureItemVisible( item );
163 mAddresseeList->blockSignals( false );
164 }
165}
166
167void AddresseeDialog::updateEdit( QListViewItem *item )
168{
169 mAddresseeEdit->setText( item->text( 0 ) );
170 mAddresseeEdit->setSelection( 0, item->text( 0 ).length() );
171}
172
173void AddresseeDialog::addSelected( QListViewItem *item )
174{
175 AddresseeItem *addrItem = (AddresseeItem *)( item );
176 if ( !addrItem ) return;
177
178 Addressee a = addrItem->addressee();
179
180 QListViewItem *selectedItem = mSelectedDict.find( a.uid() );
181 if ( !selectedItem ) {
182 selectedItem = new AddresseeItem( mSelectedList, a );
183 mSelectedDict.insert( a.uid(), selectedItem );
184 }
185}
186
187void AddresseeDialog::removeSelected()
188{
189 QListViewItem *item = mSelectedList->selectedItem();
190 AddresseeItem *addrItem = (AddresseeItem *)( item );
191 if ( !addrItem ) return;
192
193 mSelectedDict.remove( addrItem->addressee().uid() );
194 delete addrItem;
195}
196
197Addressee AddresseeDialog::addressee()
198{
199 AddresseeItem *aItem = 0;
200
201 if ( mMultiple )
202 aItem = (AddresseeItem *)( mSelectedList->firstChild() );
203 else
204 aItem = (AddresseeItem *)( mAddresseeList->selectedItem() );
205
206 if (aItem) return aItem->addressee();
207 return Addressee();
208}
209
210Addressee::List AddresseeDialog::addressees()
211{
212 Addressee::List al;
213 AddresseeItem *aItem = 0;
214
215 if ( mMultiple ) {
216 QListViewItem *item = mSelectedList->firstChild();
217 while( item ) {
218 aItem = (AddresseeItem *)( item );
219 if ( aItem ) al.append( aItem->addressee() );
220 item = item->nextSibling();
221 }
222 }
223 else
224 {
225 aItem = (AddresseeItem *)( mAddresseeList->selectedItem() );
226 if (aItem) al.append( aItem->addressee() );
227 }
228
229 return al;
230}
231
232Addressee AddresseeDialog::getAddressee( QWidget *parent )
233{
234 AddresseeDialog *dlg = new AddresseeDialog( parent );
235 Addressee addressee;
236 int result = dlg->exec();
237
238 if ( result == QDialog::Accepted ) {
239 addressee = dlg->addressee();
240 }
241
242 delete dlg;
243 return addressee;
244}
245
246Addressee::List AddresseeDialog::getAddressees( QWidget *parent )
247{
248 AddresseeDialog *dlg = new AddresseeDialog( parent, true );
249 Addressee::List addressees;
250 if ( QApplication::desktop()->width() <= 640 )
251 dlg->showMaximized();
252 int result = dlg->exec();
253 if ( result == QDialog::Accepted ) {
254 addressees = dlg->addressees();
255 }
256
257 delete dlg;
258 return addressees;
259}
260
261void AddresseeDialog::addressBookChanged()
262{
263 loadAddressBook();
264}