• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

libkdepim

  • sources
  • kde-4.12
  • kdepim
  • libkdepim
  • addressline
recentaddresses.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2  *
3  * Copyright (c) 2001-2003 Carsten Pfeiffer <pfeiffer@kde.org>
4  * Copyright (c) 2003 Zack Rusin <zack@kde.org>
5  *
6  * KMail is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License, version 2, as
8  * published by the Free Software Foundation.
9  *
10  * KMail is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * In addition, as a special exception, the copyright holders give
20  * permission to link the code of this program with any edition of
21  * the Qt library by Trolltech AS, Norway (or with modified versions
22  * of Qt that use the same license as Qt), and distribute linked
23  * combinations including the two. You must obey the GNU General
24  * Public License in all respects for all of the code used other than
25  * Qt. If you modify this file, you may extend this exception to
26  * your version of the file, but you are not obligated to do so. If
27  * you do not wish to do so, delete this exception statement from
28  * your version.
29  */
30 #include "recentaddresses.h"
31 #include <kpimutils/email.h>
32 
33 #include <KConfig>
34 #include <KConfigGroup>
35 #include <KDebug>
36 #include <KGlobal>
37 #include <KLocale>
38 #include <KLineEdit>
39 #include <KPushButton>
40 
41 #include <QCoreApplication>
42 #include <QLayout>
43 #include <QVBoxLayout>
44 #include <QListWidget>
45 #include <QKeyEvent>
46 
47 using namespace KPIM;
48 
49 RecentAddresses *s_self = 0;
50 
51 void deleteGlobalRecentAddresses()
52 {
53  delete s_self;
54  s_self = 0;
55 }
56 
57 RecentAddresses *RecentAddresses::self( KConfig *config )
58 {
59  if ( !s_self ) {
60  s_self = new RecentAddresses( config );
61  qAddPostRoutine( deleteGlobalRecentAddresses );
62  }
63  return s_self;
64 }
65 
66 bool RecentAddresses::exists()
67 {
68  return s_self != 0;
69 }
70 
71 RecentAddresses::RecentAddresses( KConfig *config )
72 {
73  if ( !config ) {
74  load( KGlobal::config().data() );
75  } else {
76  load( config );
77  }
78 }
79 
80 RecentAddresses::~RecentAddresses()
81 {
82  // if you want this destructor to get called, use K_GLOBAL_STATIC
83  // on s_self
84 }
85 
86 void RecentAddresses::load( KConfig *config )
87 {
88  QStringList addresses;
89  QString name;
90  QString email;
91 
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 );
104  }
105  }
106 
107  adjustSize();
108 }
109 
110 void RecentAddresses::save( KConfig *config )
111 {
112  KConfigGroup cg( config, "General" );
113  cg.writeEntry( "Recent Addresses", addresses() );
114 }
115 
116 void RecentAddresses::add( const QString &entry )
117 {
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 ) {
124  continue;
125  }
126  QString email;
127  QString fullName;
128  KABC::Addressee addr;
129 
130  KABC::Addressee::parseEmailAddress( *e_it, fullName, email );
131 
132  KABC::Addressee::List::Iterator end( m_addresseeList.end() );
133  for ( KABC::Addressee::List::Iterator it = m_addresseeList.begin();
134  it != end; ++it ) {
135  if ( email == (*it).preferredEmail() ) {
136  //already inside, remove it here and add it later at pos==1
137  m_addresseeList.erase( it );
138  break;
139  }
140  }
141  addr.setNameFromString( fullName );
142  addr.insertEmail( email, true );
143  m_addresseeList.prepend( addr );
144  adjustSize();
145  }
146  }
147 }
148 
149 void RecentAddresses::setMaxCount( int count )
150 {
151  if (count != m_maxCount) {
152  m_maxCount = count;
153  adjustSize();
154  }
155 }
156 
157 void RecentAddresses::adjustSize()
158 {
159  while ( m_addresseeList.count() > m_maxCount ) {
160  m_addresseeList.takeLast();
161  }
162 }
163 
164 void RecentAddresses::clear()
165 {
166  m_addresseeList.clear();
167  adjustSize();
168 }
169 
170 QStringList RecentAddresses::addresses() const
171 {
172  QStringList addresses;
173  KABC::Addressee::List::ConstIterator end = m_addresseeList.constEnd();
174  for ( KABC::Addressee::List::ConstIterator it = m_addresseeList.constBegin();
175  it != end; ++it ) {
176  addresses.append( (*it).fullEmail() );
177  }
178  return addresses;
179 }
180 
181 RecentAddressDialog::RecentAddressDialog( QWidget *parent )
182  : KDialog( parent )
183 {
184  setCaption( i18n( "Edit Recent Addresses" ) );
185  setButtons( Ok|Cancel );
186  setDefaultButton( Ok );
187  setModal( true );
188  QWidget *page = new QWidget( this );
189  setMainWidget( page );
190 
191  QVBoxLayout *layout = new QVBoxLayout( page );
192  layout->setSpacing( spacingHint() );
193  layout->setMargin( 0 );
194 
195  mLineEdit = new KLineEdit(this);
196  layout->addWidget(mLineEdit);
197 
198  mLineEdit->setTrapReturnKey(true);
199  mLineEdit->installEventFilter(this);
200 
201  connect(mLineEdit,SIGNAL(textChanged(QString)),SLOT(slotTypedSomething(QString)));
202  connect(mLineEdit,SIGNAL(returnPressed()),SLOT(slotAddItem()));
203 
204 
205  QHBoxLayout* hboxLayout = new QHBoxLayout;
206 
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);
212 
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);
217 
218 
219  mListView = new QListWidget(this);
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()));
227  // maybe supplied lineedit has some text already
228  slotTypedSomething( mLineEdit->text() );
229  readConfig();
230 }
231 
232 RecentAddressDialog::~RecentAddressDialog()
233 {
234  writeConfig();
235 }
236 
237 void RecentAddressDialog::slotTypedSomething(const QString& text)
238 {
239  if (mListView->currentItem()) {
240  if (mListView->currentItem()->text() != mLineEdit->text() && !mLineEdit->text().isEmpty()) {
241  // IMHO changeItem() shouldn't do anything with the value
242  // of currentItem() ... like changing it or emitting signals ...
243  // but TT disagree with me on this one (it's been that way since ages ... grrr)
244  bool block = mListView->signalsBlocked();
245  mListView->blockSignals( true );
246  QListWidgetItem *currentIndex = mListView->currentItem();
247  if ( currentIndex ) {
248  currentIndex->setText(text);
249  }
250  mListView->blockSignals( block );
251  }
252  }
253 }
254 
255 void RecentAddressDialog::slotAddItem()
256 {
257  QStringList lst = addresses();
258  mListView->blockSignals(true);
259  QStringList newList;
260  newList << QString() << lst;
261  setAddresses(newList);
262  mListView->blockSignals(false);
263  mListView->setCurrentRow(0);
264  mLineEdit->setFocus();
265  updateButtonState();
266 }
267 
268 void RecentAddressDialog::slotRemoveItem()
269 {
270  QList<QListWidgetItem *> selectedItems = mListView->selectedItems();
271  if (selectedItems.isEmpty())
272  return;
273  Q_FOREACH(QListWidgetItem *item, selectedItems) {
274  delete mListView->takeItem(mListView->row(item));
275  }
276  updateButtonState();
277 }
278 
279 void RecentAddressDialog::updateButtonState()
280 {
281  QList<QListWidgetItem *> selectedItems = mListView->selectedItems();
282  const int numberOfElementSelected(selectedItems.count());
283  mRemoveButton->setEnabled(numberOfElementSelected);
284  mNewButton->setEnabled(numberOfElementSelected <= 1);
285  mLineEdit->setEnabled(numberOfElementSelected <= 1);
286 
287  if (numberOfElementSelected == 1) {
288  const QString text = mListView->currentItem()->text();
289  if (text != mLineEdit->text())
290  mLineEdit->setText(text);
291  } else {
292  mLineEdit->clear();
293  }
294 }
295 
296 void RecentAddressDialog::slotSelectionChanged()
297 {
298  updateButtonState();
299 }
300 
301 void RecentAddressDialog::setAddresses( const QStringList &addrs )
302 {
303  mListView->clear();
304  mListView->addItems( addrs );
305 }
306 
307 QStringList RecentAddressDialog::addresses() const
308 {
309  QStringList lst;
310  const int numberOfItem(mListView->count());
311  for(int i = 0; i < numberOfItem; ++i) {
312  lst<<mListView->item(i)->text();
313  }
314  return lst;
315 }
316 
317 bool RecentAddressDialog::eventFilter( QObject* o, QEvent* e )
318 {
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);
324  }
325  }
326 
327  return false;
328 }
329 
330 void RecentAddressDialog::addAddresses(KConfig *config)
331 {
332  const int numberOfItem(mListView->count());
333  for (int i = 0; i < numberOfItem; ++i) {
334  KPIM::RecentAddresses::self( config )->add( mListView->item(i)->text() );
335  }
336 }
337 
338 void RecentAddressDialog::readConfig()
339 {
340  KConfigGroup group( KGlobal::config(), "RecentAddressDialog" );
341  const QSize size = group.readEntry( "Size", QSize(600, 400) );
342  if ( size.isValid() ) {
343  resize( size );
344  }
345 }
346 
347 void RecentAddressDialog::writeConfig()
348 {
349  KConfigGroup group( KGlobal::config(), "RecentAddressDialog" );
350  group.writeEntry( "Size", size() );
351  group.sync();
352 }
353 
354 
355 
356 #include "recentaddresses.moc"
KPIM::RecentAddressDialog::updateButtonState
void updateButtonState()
Definition: recentaddresses.cpp:279
KPIM::RecentAddresses::add
void add(const QString &entry)
Adds an entry to the list.
Definition: recentaddresses.cpp:116
KPIM::RecentAddresses::self
static RecentAddresses * self(KConfig *config=0)
Definition: recentaddresses.cpp:57
QWidget
deleteGlobalRecentAddresses
void deleteGlobalRecentAddresses()
Definition: recentaddresses.cpp:51
QListWidget
KPIM::RecentAddresses::~RecentAddresses
~RecentAddresses()
Definition: recentaddresses.cpp:80
KPIM::RecentAddressDialog::~RecentAddressDialog
~RecentAddressDialog()
Definition: recentaddresses.cpp:232
KDialog
KPIM::RecentAddresses::exists
static bool exists()
Definition: recentaddresses.cpp:66
KPIM::RecentAddressDialog::eventFilter
bool eventFilter(QObject *o, QEvent *e)
Definition: recentaddresses.cpp:317
QObject
KPIM::RecentAddresses::save
void save(KConfig *)
Saves the list of recently used addresses to the configfile.
Definition: recentaddresses.cpp:110
KPIM::RecentAddresses::clear
void clear()
Removes all entries from the history.
Definition: recentaddresses.cpp:164
KPIM::RecentAddressDialog::RecentAddressDialog
RecentAddressDialog(QWidget *parent)
Definition: recentaddresses.cpp:181
KPIM::RecentAddressDialog::addAddresses
void addAddresses(KConfig *config)
Definition: recentaddresses.cpp:330
KPIM::RecentAddresses::setMaxCount
void setMaxCount(int count)
Sets the maximum number, the list can hold.
Definition: recentaddresses.cpp:149
KPIM::RecentAddresses::addresses
QStringList addresses() const
Definition: recentaddresses.cpp:170
KPIM::RecentAddresses::load
void load(KConfig *)
Loads the list of recently used addresses from the configfile.
Definition: recentaddresses.cpp:86
KLineEdit
s_self
RecentAddresses * s_self
Definition: recentaddresses.cpp:49
KPIM::RecentAddresses
Handles a list of "recent email-addresses".
Definition: recentaddresses.h:80
KPIM::RecentAddressDialog::setAddresses
void setAddresses(const QStringList &addrs)
Definition: recentaddresses.cpp:301
KPIM::RecentAddressDialog::addresses
QStringList addresses() const
Definition: recentaddresses.cpp:307
QList
recentaddresses.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:03 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdepim

Skip menu "libkdepim"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal