23 #include <QtCore/QDateTime>
26 #include <kconfigbase.h>
27 #include <kconfiggroup.h>
30 #include <kabc/address.h>
31 #include <kabc/addressee.h>
43 if (config == 0)
return;
45 KConfigGroup columnMap = config->group(
"csv column map");
46 const QStringList keyList = columnMap.keyList();
48 QStringList::const_iterator it = keyList.begin();
49 QStringList::const_iterator endIt = keyList.end();
50 for (; it != endIt; ++it)
52 if ((*it).isEmpty())
continue;
55 int column = (*it).toInt(&ok);
58 int field = columnMap.readEntry(*it, -1);
59 if (field < 0)
continue;
61 m_columnToField.insert(column, field);
64 KConfigGroup general = config->group(
"General");
66 m_datePattern = general.readEntry(
"DatePattern");
67 if (m_datePattern.isEmpty()) m_datePattern = QLatin1String(
"Y-M-D");
70 m_columns = general.readEntry(
"Columns", 0);
71 if (m_columns < 0) m_columns = 0;
73 switch (general.readEntry(
"DelimiterType", 0))
76 m_delimiter = QLatin1String(
";");
80 m_delimiter = QLatin1String(
"\t");
84 m_delimiter = QLatin1String(
" ");
88 m_delimiter = general.readEntry(
"DelimiterOther");
92 m_delimiter = QLatin1String(
",");
94 if (m_delimiter.isEmpty()) m_delimiter = QLatin1String(
",");
96 switch (general.readEntry(
"QuoteType", 0))
99 m_quote = QLatin1String(
"'");
106 m_quote = QLatin1String(
"\"");
115 if (column < 0 || addressee.isEmpty())
return QString();
116 if (m_columnToField.isEmpty())
return QString();
118 QMap<int, int>::const_iterator it = m_columnToField.find(column);
119 if (it == m_columnToField.end())
return QString();
125 text = addressee.formattedName();
128 text = addressee.familyName();
131 text = addressee.givenName();
134 text = addressee.additionalName();
137 text = addressee.prefix();
140 text = addressee.suffix();
143 text = addressee.nickName();
146 text = formatDate(addressee.birthday());
149 text = addressee.address(Address::Home).street();
152 text = addressee.address(Address::Home).locality();
155 text = addressee.address(Address::Home).region();
158 text = addressee.address(Address::Home).postalCode();
161 text = addressee.address(Address::Home).country();
164 text = addressee.address(Address::Home).label();
167 text = addressee.address(Address::Work).street();
170 text = addressee.address(Address::Work).locality();
173 text = addressee.address(Address::Work).region();
176 text = addressee.address(Address::Work).postalCode();
179 text = addressee.address(Address::Work).country();
182 text = addressee.address(Address::Work).label();
185 text = addressee.phoneNumber(PhoneNumber::Home).number();
188 text = addressee.phoneNumber(PhoneNumber::Work).number();
191 text = addressee.phoneNumber(PhoneNumber::Cell).number();
194 text = addressee.phoneNumber(PhoneNumber::Fax | PhoneNumber::Home).number();
197 text = addressee.phoneNumber(PhoneNumber::Fax | PhoneNumber::Work).number();
200 text = addressee.phoneNumber(PhoneNumber::Car).number();
203 text = addressee.phoneNumber(PhoneNumber::Isdn).number();
206 text = addressee.phoneNumber(PhoneNumber::Pager).number();
209 text = addressee.preferredEmail();
212 text = addressee.mailer();
215 text = addressee.title();
218 text = addressee.role();
221 text = addressee.organization();
224 text = addressee.note();
227 text = addressee.url().prettyUrl();
257 if (column < 0 || text.isEmpty())
return;
258 if (m_columnToField.isEmpty())
return;
260 QMap<int, int>::const_iterator it = m_columnToField.find(column);
261 if (it == m_columnToField.end())
return;
269 addressee.setFormattedName(text);
272 addressee.setFamilyName(text);
275 addressee.setGivenName(text);
278 addressee.setAdditionalName(text);
281 addressee.setPrefix(text);
284 addressee.setSuffix(text);
287 addressee.setNickName(text);
290 addressee.setBirthday(parseDate(text));
293 address = addressee.address(Address::Home);
294 address.setStreet(text);
295 addressee.insertAddress(address);
298 address = addressee.address(Address::Home);
299 address.setLocality(text);
300 addressee.insertAddress(address);
303 address = addressee.address(Address::Home);
304 address.setRegion(text);
305 addressee.insertAddress(address);
308 address = addressee.address(Address::Home);
309 address.setPostalCode(text);
310 addressee.insertAddress(address);
313 address = addressee.address(Address::Home);
314 address.setCountry(text);
315 addressee.insertAddress(address);
318 address = addressee.address(Address::Home);
319 address.setLabel(text);
320 addressee.insertAddress(address);
323 address = addressee.address(Address::Work);
324 address.setStreet(text);
325 addressee.insertAddress(address);
328 address = addressee.address(Address::Work);
329 address.setLocality(text);
330 addressee.insertAddress(address);
333 address = addressee.address(Address::Work);
334 address.setRegion(text);
335 addressee.insertAddress(address);
338 address = addressee.address(Address::Work);
339 address.setPostalCode(text);
340 addressee.insertAddress(address);
343 address = addressee.address(Address::Work);
344 address.setCountry(text);
345 addressee.insertAddress(address);
348 address = addressee.address(Address::Work);
349 address.setLabel(text);
350 addressee.insertAddress(address);
353 phone = PhoneNumber(text, PhoneNumber::Home);
354 addressee.insertPhoneNumber(phone);
357 phone = PhoneNumber(text, PhoneNumber::Work);
358 addressee.insertPhoneNumber(phone);
361 phone = PhoneNumber(text, PhoneNumber::Cell);
362 addressee.insertPhoneNumber(phone);
365 phone = PhoneNumber(text, PhoneNumber::Fax | PhoneNumber::Home);
366 addressee.insertPhoneNumber(phone);
369 phone = PhoneNumber(text, PhoneNumber::Fax | PhoneNumber::Work);
370 addressee.insertPhoneNumber(phone);
373 phone = PhoneNumber(text, PhoneNumber::Car);
374 addressee.insertPhoneNumber(phone);
377 phone = PhoneNumber(text, PhoneNumber::Isdn);
378 addressee.insertPhoneNumber(phone);
381 phone = PhoneNumber(text, PhoneNumber::Pager);
382 addressee.insertPhoneNumber(phone);
385 addressee.insertEmail(text);
388 addressee.setMailer(text);
391 addressee.setTitle(text);
394 addressee.setRole(text);
397 addressee.setOrganization(text);
400 addressee.setNote(text);
403 addressee.setUrl(text);
431 if (m_defaultTemplate == 0)
433 m_defaultTemplate =
new CSVTemplate(QLatin1String(
"Y-M-D"));
435 m_defaultTemplate->m_quote = QLatin1String(
"\"");
436 m_defaultTemplate->m_delimiter = QLatin1String(
",");
438 m_defaultTemplate->m_columns = 42;
439 for (
int i = 0; i < m_defaultTemplate->m_columns; ++i)
441 m_defaultTemplate->m_columnToField[i] = i + 1;
445 return m_defaultTemplate;
452 m_datePattern = datePattern;
458 QString CSVTemplate::formatDate(
const QDateTime& date)
const
460 if (!date.isValid())
return QString();
462 return date.toString(m_dateFormat);
467 QDateTime CSVTemplate::parseDate(
const QString& text)
const
469 if (text.isEmpty())
return QDateTime();
475 QByteArray pattern = m_datePattern.toLatin1();
478 for (
int i = 0; ok && i < pattern.length(); ++i)
483 if ((pos + 3) >= text.length())
489 year = text.mid(pos, 4).toInt(&ok);
495 if ((pos + 1) >= text.length())
501 year = 1900 + text.mid(pos, 2).toInt(&ok);
507 if ((pos + 1) >= text.length())
513 month = text.mid(pos, 2).toInt(&ok);
519 if ((pos + 1) < text.length() && text[pos+1].isDigit())
521 month = text.mid(pos, 2).toInt(&ok);
526 month = text.mid(pos, 1).toInt(&ok);
532 if ((pos + 1) >= text.length())
538 day = text.mid(pos, 2).toInt(&ok);
544 if ((pos + 1) < text.length() && text[pos+1].isDigit())
546 day = text.mid(pos, 2).toInt(&ok);
551 day = text.mid(pos, 1).toInt(&ok);
557 ok = QLatin1Char(pattern[i]) == text[pos];
563 ok = ok && pos >= text.length();
565 if (ok && year > 0 && month > 0 && day > 0)
return QDateTime(QDate(year, month, day));
572 void CSVTemplate::createDateFormat()
574 QByteArray datePattern = m_datePattern.toLatin1();
576 for (
int i = 0; i < datePattern.length(); ++i)
578 switch (datePattern[i])
581 m_dateFormat.append(QLatin1String(
"yyyy"));
585 m_dateFormat.append(QLatin1String(
"yy"));
589 m_dateFormat.append(QLatin1String(
"MM"));
593 m_dateFormat.append(QLatin1String(
"m"));
597 m_dateFormat.append(QLatin1String(
"dd"));
601 m_dateFormat.append(QLatin1String(
"d"));
605 m_dateFormat.append(QChar::fromLatin1(datePattern[i]));
void setFieldText(int column, KABC::Addressee &addressee, const QString &text) const
Sets an addressee field using the data of a given text.
Class for handling KAddressBook's CSV templates.
QString fieldText(int column, const KABC::Addressee &addressee) const
Returns the specified field of the given addressee formatted as a string.
static CSVTemplate * defaultTemplate()
Returns the template with default setup.
CSVTemplate(KConfigBase *config)
Creates a template handler for the given description.