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

console/kabcclient

  • sources
  • kde-4.14
  • kdepim
  • console
  • kabcclient
  • src
inputformatimpls.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005 - 2006 Kevin Krammer <kevin.krammer@gmx.at>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 //
18 
19 // local includes
20 #include "inputformatimpls.h"
21 #include "csvtemplate.h"
22 #include "csvtemplatefactory.h"
23 
24 // standard includes
25 #include <iostream>
26 #include <string>
27 
28 // Qt includes
29 #include <QtCore/QTextCodec>
30 
31 // KDE includes
32 #include <klocale.h>
33 
34 // KABC includes
35 #include <kabc/addressee.h>
36 #include <kabc/addresseedialog.h>
37 #include <kabc/addresseelist.h>
38 #include <kabc/vcardconverter.h>
39 
40 using namespace KABC;
41 
43 
44 QString UIDInput::description() const
45 {
46  return i18n("Interprets input as a unique KABC contact identifier");
47 }
48 
50 
51 bool UIDInput::setOptions(const QByteArray& options)
52 {
53  Q_UNUSED(options)
54  return false;
55 }
56 
58 
59 bool UIDInput::setCodec(QTextCodec* codec)
60 {
61  if (codec == 0) return false;
62 
63  m_codec = codec;
64 
65  return true;
66 }
67 
69 
70 KABC::Addressee UIDInput::readAddressee(std::istream& stream)
71 {
72  if (stream.bad()) return KABC::Addressee();
73 
74  KABC::Addressee addressee;
75 
76  std::string line;
77  getline(stream, line);
78  while (line.empty() && !stream.eof())
79  {
80  getline(stream, line);
81  }
82 
83  if (stream.eof()) return addressee;
84 
85  QString uid = m_codec->toUnicode(line.c_str(), line.size());
86 
87  addressee.setUid(uid);
88 
89  return addressee;
90 }
91 
94 
95 VCardInput::VCardInput() : m_converter(0)
96 {
97  m_converter = new VCardConverter();
98 }
99 
101 
102 QString VCardInput::description() const
103 {
104  return i18n("Interprets input as vCard data");
105 }
106 
108 
109 VCardInput::~VCardInput()
110 {
111  delete m_converter;
112 }
113 
115 
116 bool VCardInput::setOptions(const QByteArray& options)
117 {
118  Q_UNUSED(options)
119  return false;
120 }
121 
123 
124 bool VCardInput::setCodec(QTextCodec* codec)
125 {
126  if (codec == 0) return false;
127 
128  m_codec = codec;
129 
130  QString codecName = QString::fromLatin1(m_codec->name());
131 
132  if (codecName != QString::fromUtf8("UTF-8"))
133  {
134  QString warning = i18n("Warning: using codec '%1' with input format vcard, "
135  "but vCards are usually expected to be in UTF-8.",
136  codecName);
137 
138  std::cerr << warning.toLocal8Bit().data() << std::endl;
139  }
140 
141  return true;
142 }
143 
145 
146 KABC::Addressee VCardInput::readAddressee(std::istream& stream)
147 {
148  if (stream.bad()) return KABC::Addressee();
149 
150  std::string line;
151  // read any empty lines
152  while (line.empty() && !stream.eof())
153  {
154  getline(stream, line);
155  }
156  if (line.empty()) return KABC::Addressee();
157 
158  QString input = m_codec->toUnicode(line.c_str(), line.size());
159  input += QLatin1Char('\n');
160 
161  QString inputLine;
162  while (!inputLine.isEmpty() || !stream.eof())
163  {
164  getline(stream, line);
165  inputLine = m_codec->toUnicode(line.c_str(), line.size());
166 
167  input += inputLine;
168  input += QLatin1Char('\n');
169  if (inputLine.startsWith(QLatin1String("END:VCARD"))) break;
170  }
171 
172  return m_converter->parseVCard(input.toUtf8());
173 }
174 
177 
178 QString EmailInput::description() const
179 {
180  return i18n("Interprets input as email and optional name");
181 }
182 
184 
185 bool EmailInput::setOptions(const QByteArray& options)
186 {
187  Q_UNUSED(options)
188  return false;
189 }
190 
192 
193 bool EmailInput::setCodec(QTextCodec* codec)
194 {
195  if (codec == 0) return false;
196 
197  m_codec = codec;
198 
199  return true;
200 }
201 
203 
204 KABC::Addressee EmailInput::readAddressee(std::istream& stream)
205 {
206  if (stream.bad()) return KABC::Addressee();
207 
208  KABC::Addressee addressee;
209 
210  std::string line;
211  getline(stream, line);
212  while (line.empty() && !stream.eof())
213  {
214  getline(stream, line);
215  }
216 
217  if (stream.eof()) return addressee;
218 
219  QString rawEmail = m_codec->toUnicode(line.c_str(), line.size());
220  QString name;
221  QString email;
222  KABC::Addressee::parseEmailAddress(rawEmail, name, email);
223 
224  if (!email.isEmpty() && email.indexOf(QLatin1String("@")) != -1)
225  {
226  addressee.insertEmail(email, true);
227 
228  if (!name.isEmpty())
229  {
230  addressee.setNameFromString(name);
231  }
232 
233  addressee.setUid(QString());
234  }
235 
236  return addressee;
237 }
238 
241 
242 QString SearchInput::description() const
243 {
244  return i18n("Tries to get email and name from input,\n\t\t"
245  "otherwise sets input text for both");
246 }
247 
249 
250 bool SearchInput::setOptions(const QByteArray& options)
251 {
252  Q_UNUSED(options)
253  return false;
254 }
255 
257 
258 bool SearchInput::setCodec(QTextCodec* codec)
259 {
260  if (codec == 0) return false;
261 
262  m_codec = codec;
263 
264  return true;
265 }
266 
268 
269 KABC::Addressee SearchInput::readAddressee(std::istream& stream)
270 {
271  if (stream.bad()) return KABC::Addressee();
272 
273  KABC::Addressee addressee;
274 
275  std::string line;
276  getline(stream, line);
277  while (line.empty() && !stream.eof())
278  {
279  getline(stream, line);
280  }
281 
282  if (stream.eof()) return addressee;
283 
284  QString rawEmail = m_codec->toUnicode(line.c_str(), line.size());
285  QString name;
286  QString email;
287  KABC::Addressee::parseEmailAddress(rawEmail, name, email);
288 
289  if (email.isEmpty() || email.indexOf(QLatin1String("@")) == -1)
290  {
291  addressee.insertEmail(rawEmail, true);
292  }
293  else
294  {
295  addressee.insertEmail(email, true);
296  }
297 
298  if (!name.isEmpty())
299  {
300  addressee.setNameFromString(name);
301  }
302  else
303  {
304  addressee.setNameFromString(rawEmail);
305  }
306 
307  addressee.setUid(QString());
308 
309  return addressee;
310 }
311 
314 
315 QString NameInput::description() const
316 {
317  return i18n("Interprets the input as a name.\n\t\t"
318  "Recommended format is 'lastname, firstname middlename'");
319 }
320 
322 
323 bool NameInput::setOptions(const QByteArray& options)
324 {
325  Q_UNUSED(options)
326  return false;
327 }
328 
330 
331 bool NameInput::setCodec(QTextCodec* codec)
332 {
333  if (codec == 0) return false;
334 
335  m_codec = codec;
336 
337  return true;
338 }
339 
341 
342 KABC::Addressee NameInput::readAddressee(std::istream& stream)
343 {
344  if (stream.bad()) return KABC::Addressee();
345 
346  KABC::Addressee addressee;
347 
348  std::string line;
349  getline(stream, line);
350  while (line.empty() && !stream.eof())
351  {
352  getline(stream, line);
353  }
354 
355  if (stream.eof()) return addressee;
356 
357  QString name = m_codec->toUnicode(line.c_str(), line.size());
358 
359  if (!name.isEmpty())
360  {
361  addressee.setNameFromString(name);
362  }
363 
364  addressee.setUid(QString());
365 
366  return addressee;
367 }
368 
371 
372 CSVInput::CSVInput(CSVTemplateFactory* templateFactory)
373  : m_codec(0), m_template(0), m_templateFactory(templateFactory)
374 {
375 }
376 
378 
379 QString CSVInput::description() const
380 {
381  return i18n("Interprets the input as a delimiter separated list of fields.");
382 }
383 
385 
386 bool CSVInput::setOptions(const QByteArray& options)
387 {
388  QString name = QString::fromLocal8Bit(options);
389  if (name.isEmpty()) return false;
390 
391  m_template = m_templateFactory->createCachedTemplate(name);
392 
393  return m_template != 0;
394 }
395 
397 
398 QString CSVInput::optionUsage() const
399 {
400  QString usage =
401  i18n("Specify one of the following CSV templates:");
402 
403  usage += QLatin1Char('\n');
404 
405  const QMap<QString, QString> templateNames = m_templateFactory->templateNames();
406 
407  QMap<QString, QString>::const_iterator it = templateNames.constBegin();
408  QMap<QString, QString>::const_iterator endIt = templateNames.constEnd();
409  for (; it != endIt; ++it)
410  {
411  QString name = it.key();
412  QString templateName = it.value();
413 
414  usage += name;
415 
416  usage += name.length() < 8 ? QLatin1String("\t\t") : QLatin1String("\t");
417 
418  usage += templateName;
419 
420  usage += QLatin1Char('\n');
421  }
422 
423  return usage;
424 }
425 
427 
428 bool CSVInput::setCodec(QTextCodec* codec)
429 {
430  if (codec == 0) return false;
431 
432  m_codec = codec;
433 
434  return true;
435 }
436 
438 
439 KABC::Addressee CSVInput::readAddressee(std::istream& stream)
440 {
441  if (stream.bad()) return KABC::Addressee();
442 
443  if (m_template == 0) m_template = CSVTemplate::defaultTemplate();
444 
445  KABC::Addressee addressee;
446 
447  std::string line;
448  getline(stream, line);
449  while (line.empty() && !stream.eof())
450  {
451  getline(stream, line);
452  }
453 
454  if (stream.eof()) return addressee;
455 
456  QString values = m_codec->toUnicode(line.c_str(), line.size());
457 
458  if (!values.isEmpty())
459  {
460  QStringList list = split(values);
461 
462  QStringList::const_iterator it = list.constBegin();
463  QStringList::const_iterator endIt = list.constEnd();
464  for (int i = 0; it != endIt; ++it, ++i)
465  {
466  m_template->setFieldText(i, addressee, *it);
467  }
468  }
469 
470  return addressee;
471 }
472 
474 
475 QStringList CSVInput::split(const QString& values) const
476 {
477  const QString quote = m_template->quote();
478  const QString delimiter = m_template->delimiter();
479 
480  if (quote.isEmpty()) return values.split(delimiter, QString::KeepEmptyParts);
481 
482  QString remaining = values;
483 
484  QStringList list;
485  bool quoted = false;
486  while (!remaining.isEmpty())
487  {
488  if (quoted)
489  {
490  int quoteIndex = remaining.indexOf(quote);
491  if (quoteIndex >= 0)
492  {
493  list.append(remaining.left(quoteIndex));
494  remaining = remaining.mid(quoteIndex + quote.length());
495 
496  if (remaining.indexOf(delimiter) == 0)
497  {
498  remaining = remaining.mid(delimiter.length());
499  }
500  }
501  else
502  {
503  list.append(remaining);
504  remaining.clear();
505  }
506 
507  quoted = false;
508  }
509  else
510  {
511  int quoteIndex = remaining.indexOf(quote);
512 
513  if (quoteIndex == 0)
514  {
515  quoted = true;
516 
517  remaining = remaining.mid(quote.length());
518  }
519  else
520  {
521  int delimiterIndex = remaining.indexOf(delimiter);
522  if (delimiterIndex >= 0)
523  {
524  list.append(remaining.left(delimiterIndex));
525  remaining = remaining.mid(delimiterIndex + delimiter.length());
526  }
527  else
528  {
529  list.append(remaining);
530  remaining.clear();
531  }
532  }
533  }
534  }
535 
536  return list;
537 }
538 
541 
542 class DialogInputPrivate
543 {
544 public:
545  DialogInputPrivate() : showDialog(true) {}
546 
547 public:
548  bool showDialog;
549  KABC::AddresseeList addresseeList;
550 };
551 
553 
554 DialogInput::DialogInput() : m_private(new DialogInputPrivate())
555 {
556 }
557 
559 
560 DialogInput::~DialogInput()
561 {
562  delete m_private;
563 }
564 
566 
567 QString DialogInput::description() const
568 {
569  return i18n("Select contacts in a dialog instead of reading input text");
570 }
571 
573 
574 bool DialogInput::setOptions(const QByteArray& options)
575 {
576  Q_UNUSED(options)
577  return false;
578 }
579 
581 
582 bool DialogInput::setCodec(QTextCodec* codec)
583 {
584  if (codec == 0) return false;
585 
586  return true;
587 }
588 
590 
591 KABC::Addressee DialogInput::readAddressee(std::istream& stream)
592 {
593  KABC::Addressee addressee;
594 
595  // on first call show dialog for addressee selection
596  if (m_private->showDialog)
597  {
598  m_private->addresseeList = KABC::AddresseeDialog::getAddressees(0);
599  m_private->showDialog = false;
600  }
601 
602  // As long as there are addressees left in the selection list take the first
603  // one and use its UID as the output of this InputFormat.
604  // When all selected addressees have been processed, mark stream as empty
605  if (!m_private->addresseeList.isEmpty())
606  {
607  KABC::Addressee nextAddressee = m_private->addresseeList.front();
608 
609  // using the UID avoid multiple matches by name or email of the addressee
610  if (!nextAddressee.isEmpty()) addressee.setUid(nextAddressee.uid());
611 
612  m_private->addresseeList.pop_front();
613  }
614  else
615  stream.setstate(std::ios_base::eofbit);
616 
617  return addressee;
618 }
619 
620 // End of file
EmailInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:178
DialogInput::DialogInput
DialogInput()
Definition: inputformatimpls.cpp:554
CSVInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:386
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
VCardInput::VCardInput
VCardInput()
Definition: inputformatimpls.cpp:95
VCardInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:124
QTextCodec::name
virtual QByteArray name() const =0
inputformatimpls.h
CSVInput::CSVInput
CSVInput(CSVTemplateFactory *templateFactory)
Definition: inputformatimpls.cpp:372
QByteArray
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
EmailInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:193
QMap::constBegin
const_iterator constBegin() const
QMap< QString, QString >
NameInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:315
CSVTemplateFactory::templateNames
QMap< QString, QString > templateNames()
Returns a set of available templates.
Definition: csvtemplatefactory.cpp:96
DialogInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads one addressee from the input stream.
Definition: inputformatimpls.cpp:591
DialogInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:574
VCardInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads a single contact from the input stream.
Definition: inputformatimpls.cpp:146
NameInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:331
QList::const_iterator
UIDInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:44
DialogInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:567
DialogInput::~DialogInput
virtual ~DialogInput()
Definition: inputformatimpls.cpp:560
UIDInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:59
NameInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads one addressee from the input stream.
Definition: inputformatimpls.cpp:342
QString::clear
void clear()
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
EmailInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:185
QList::append
void append(const T &value)
QString::fromUtf8
QString fromUtf8(const char *str, int size)
DialogInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:582
CSVTemplate::setFieldText
void setFieldText(int column, KABC::Addressee &addressee, const QString &text) const
Sets an addressee field using the data of a given text.
Definition: csvtemplate.cpp:255
SearchInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:242
QString::isEmpty
bool isEmpty() const
QMap::constEnd
const_iterator constEnd() const
csvtemplatefactory.h
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
CSVInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads one addressee from the input stream.
Definition: inputformatimpls.cpp:439
VCardInput::~VCardInput
virtual ~VCardInput()
Definition: inputformatimpls.cpp:109
QString
QTextCodec
CSVTemplate::delimiter
const QString & delimiter() const
Returns the CSV delimiter string.
Definition: csvtemplate.h:135
QStringList
CSVInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:428
CSVTemplate::quote
const QString & quote() const
Returns the CSV quoting string.
Definition: csvtemplate.h:160
QString::toLocal8Bit
QByteArray toLocal8Bit() const
CSVInput::optionUsage
virtual QString optionUsage() const
Returns a translate message about the available format options.
Definition: inputformatimpls.cpp:398
CSVTemplateFactory::createCachedTemplate
CSVTemplate * createCachedTemplate(const QString &name)
Creates a template handler for a given name and caches the instance.
Definition: csvtemplatefactory.cpp:74
QLatin1Char
CSVTemplate::defaultTemplate
static CSVTemplate * defaultTemplate()
Returns the template with default setup.
Definition: csvtemplate.cpp:429
SearchInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads one addressee from the input stream.
Definition: inputformatimpls.cpp:269
QMap::key
const Key key(const T &value) const
QString::mid
QString mid(int position, int n) const
VCardInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:116
QLatin1String
SearchInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:250
UIDInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads one addressee from the input stream.
Definition: inputformatimpls.cpp:70
QString::length
int length() const
QByteArray::data
char * data()
QString::left
QString left(int n) const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
VCardInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:102
csvtemplate.h
QList::constEnd
const_iterator constEnd() const
QList::constBegin
const_iterator constBegin() const
EmailInput::readAddressee
virtual KABC::Addressee readAddressee(std::istream &stream)
Reads one addressee from the input stream.
Definition: inputformatimpls.cpp:204
CSVTemplateFactory
Factory for creation CSV template handlers.
Definition: csvtemplatefactory.h:44
QTextCodec::toUnicode
QString toUnicode(const QByteArray &a) const
SearchInput::setCodec
virtual bool setCodec(QTextCodec *codec)
Sets the text codec to use.
Definition: inputformatimpls.cpp:258
NameInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:323
UIDInput::setOptions
virtual bool setOptions(const QByteArray &options)
Configures the input format.
Definition: inputformatimpls.cpp:51
QMap::value
const T value(const Key &key) const
CSVInput::description
virtual QString description() const
Returns a translate description of the input format.
Definition: inputformatimpls.cpp:379
QString::toUtf8
QByteArray toUtf8() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:31:23 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

console/kabcclient

Skip menu "console/kabcclient"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

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
  • pimprint

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