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");
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");
70 m_columns = general.readEntry(
"Columns", 0);
71 if (m_columns < 0) m_columns = 0;
73 switch (general.readEntry(
"DelimiterType", 0))
88 m_delimiter = general.readEntry(
"DelimiterOther");
96 switch (general.readEntry(
"QuoteType", 0))
115 if (column < 0 || addressee.isEmpty())
return QString();
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;
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)
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;
478 for (
int i = 0; ok && i < pattern.
length(); ++i)
483 if ((pos + 3) >= text.
length())
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())
544 if ((pos + 1) < text.
length() && text[pos+1].isDigit())
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()
576 for (
int i = 0; i < datePattern.
length(); ++i)
578 switch (datePattern[i])
QString toString(Qt::DateFormat format) const
QString & append(QChar ch)
QString number(int n, int base)
void setFieldText(int column, KABC::Addressee &addressee, const QString &text) const
Sets an addressee field using the data of a given text.
int toInt(bool *ok, int base) const
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.
QByteArray toLatin1() const
QString mid(int position, int n) const
CSVTemplate(KConfigBase *config)
Creates a template handler for the given description.
iterator insert(const Key &key, const T &value)
iterator find(const Key &key)
const T value(const Key &key) const