31 #include <kpimutils/email.h>
34 #include <KConfigGroup>
39 #include <KPushButton>
41 #include <QCoreApplication>
43 #include <QVBoxLayout>
44 #include <QListWidget>
71 RecentAddresses::RecentAddresses( KConfig *config )
74 load( KGlobal::config().data() );
92 m_addresseeList.clear();
93 KConfigGroup cg( config,
"General" );
94 m_maxCount = cg.readEntry(
"Maximum Recent Addresses", 40 );
95 addresses = cg.readEntry(
"Recent Addresses", QStringList() );
96 QStringList::ConstIterator end( addresses.constEnd() );
97 for ( QStringList::ConstIterator it = addresses.constBegin(); it != end; ++it ) {
98 KABC::Addressee::parseEmailAddress( *it, name, email );
99 if ( !email.isEmpty() ) {
100 KABC::Addressee addr;
101 addr.setNameFromString( name );
102 addr.insertEmail( email,
true );
103 m_addresseeList.append( addr );
112 KConfigGroup cg( config,
"General" );
113 cg.writeEntry(
"Recent Addresses",
addresses() );
118 if ( !entry.isEmpty() && m_maxCount > 0 ) {
119 const QStringList list = KPIMUtils::splitAddressList( entry );
120 QStringList::const_iterator e_itEnd( list.constEnd() );
121 for ( QStringList::const_iterator e_it = list.constBegin(); e_it != e_itEnd; ++e_it ) {
122 KPIMUtils::EmailParseResult errorCode = KPIMUtils::isValidAddress( *e_it );
123 if ( errorCode != KPIMUtils::AddressOk ) {
128 KABC::Addressee addr;
130 KABC::Addressee::parseEmailAddress( *e_it, fullName, email );
132 KABC::Addressee::List::Iterator end( m_addresseeList.end() );
133 for ( KABC::Addressee::List::Iterator it = m_addresseeList.begin();
135 if ( email == (*it).preferredEmail() ) {
137 m_addresseeList.erase( it );
141 addr.setNameFromString( fullName );
142 addr.insertEmail( email,
true );
143 m_addresseeList.prepend( addr );
151 if (count != m_maxCount) {
157 void RecentAddresses::adjustSize()
159 while ( m_addresseeList.count() > m_maxCount ) {
160 m_addresseeList.takeLast();
166 m_addresseeList.clear();
173 KABC::Addressee::List::ConstIterator end = m_addresseeList.constEnd();
174 for ( KABC::Addressee::List::ConstIterator it = m_addresseeList.constBegin();
176 addresses.append( (*it).fullEmail() );
184 setCaption( i18n(
"Edit Recent Addresses" ) );
185 setButtons( Ok|Cancel );
186 setDefaultButton( Ok );
189 setMainWidget( page );
191 QVBoxLayout *layout =
new QVBoxLayout( page );
192 layout->setSpacing( spacingHint() );
193 layout->setMargin( 0 );
196 layout->addWidget(mLineEdit);
198 mLineEdit->setTrapReturnKey(
true);
199 mLineEdit->installEventFilter(
this);
201 connect(mLineEdit,SIGNAL(textChanged(QString)),SLOT(slotTypedSomething(QString)));
202 connect(mLineEdit,SIGNAL(returnPressed()),SLOT(slotAddItem()));
205 QHBoxLayout* hboxLayout =
new QHBoxLayout;
207 QVBoxLayout* btnsLayout =
new QVBoxLayout;
208 btnsLayout->addStretch();
209 mNewButton =
new KPushButton(KIcon(QLatin1String(
"list-add")), i18n(
"&Add"),
this);
210 connect(mNewButton, SIGNAL(clicked()), SLOT(slotAddItem()));
211 btnsLayout->insertWidget(0 ,mNewButton);
213 mRemoveButton =
new KPushButton(KIcon(QLatin1String(
"list-remove")), i18n(
"&Remove"),
this);
214 mRemoveButton->setEnabled(
false);
215 connect(mRemoveButton, SIGNAL(clicked()), SLOT(slotRemoveItem()));
216 btnsLayout->insertWidget(1, mRemoveButton);
220 mListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
221 mListView->setSortingEnabled(
true);
222 hboxLayout->addWidget(mListView);
223 hboxLayout->addLayout(btnsLayout);
224 layout->addLayout(hboxLayout);
225 connect(mListView, SIGNAL(itemSelectionChanged()),
226 SLOT(slotSelectionChanged()));
228 slotTypedSomething( mLineEdit->text() );
237 void RecentAddressDialog::slotTypedSomething(
const QString& text)
239 if (mListView->currentItem()) {
240 if (mListView->currentItem()->text() != mLineEdit->text() && !mLineEdit->text().isEmpty()) {
244 bool block = mListView->signalsBlocked();
245 mListView->blockSignals(
true );
246 QListWidgetItem *currentIndex = mListView->currentItem();
247 if ( currentIndex ) {
248 currentIndex->setText(text);
250 mListView->blockSignals( block );
255 void RecentAddressDialog::slotAddItem()
258 mListView->blockSignals(
true);
260 newList << QString() << lst;
262 mListView->blockSignals(
false);
263 mListView->setCurrentRow(0);
264 mLineEdit->setFocus();
268 void RecentAddressDialog::slotRemoveItem()
271 if (selectedItems.isEmpty())
273 Q_FOREACH(QListWidgetItem *item, selectedItems) {
274 delete mListView->takeItem(mListView->row(item));
282 const int numberOfElementSelected(selectedItems.count());
283 mRemoveButton->setEnabled(numberOfElementSelected);
284 mNewButton->setEnabled(numberOfElementSelected <= 1);
285 mLineEdit->setEnabled(numberOfElementSelected <= 1);
287 if (numberOfElementSelected == 1) {
288 const QString text = mListView->currentItem()->text();
289 if (text != mLineEdit->text())
290 mLineEdit->setText(text);
296 void RecentAddressDialog::slotSelectionChanged()
304 mListView->addItems( addrs );
310 const int numberOfItem(mListView->count());
311 for(
int i = 0; i < numberOfItem; ++i) {
312 lst<<mListView->item(i)->text();
319 if (o == mLineEdit && e->type() == QEvent::KeyPress ) {
320 QKeyEvent* keyEvent = (QKeyEvent*)e;
321 if (keyEvent->key() == Qt::Key_Down ||
322 keyEvent->key() == Qt::Key_Up) {
323 return ((
QObject*)mListView)->event(e);
332 const int numberOfItem(mListView->count());
333 for (
int i = 0; i < numberOfItem; ++i) {
338 void RecentAddressDialog::readConfig()
340 KConfigGroup group( KGlobal::config(),
"RecentAddressDialog" );
341 const QSize size = group.readEntry(
"Size", QSize(600, 400) );
342 if ( size.isValid() ) {
347 void RecentAddressDialog::writeConfig()
349 KConfigGroup group( KGlobal::config(),
"RecentAddressDialog" );
350 group.writeEntry(
"Size", size() );
356 #include "recentaddresses.moc"
void add(const QString &entry)
Adds an entry to the list.
static RecentAddresses * self(KConfig *config=0)
void deleteGlobalRecentAddresses()
bool eventFilter(QObject *o, QEvent *e)
void save(KConfig *)
Saves the list of recently used addresses to the configfile.
void clear()
Removes all entries from the history.
RecentAddressDialog(QWidget *parent)
void addAddresses(KConfig *config)
void setMaxCount(int count)
Sets the maximum number, the list can hold.
QStringList addresses() const
void load(KConfig *)
Loads the list of recently used addresses from the configfile.
Handles a list of "recent email-addresses".
void setAddresses(const QStringList &addrs)
QStringList addresses() const