1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtVersitOrganizer module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qversitorganizerexporter.h"
#include "qversitorganizerexporter_p.h"
#include <QtCore/qdebug.h>
#include <QtOrganizer/qorganizer.h>
#include <QtVersit/qversitdocument.h>
#include <QtVersit/qversitproperty.h>
#include <QtVersit/private/qversitutils_p.h>
#include "qversitorganizerdefs_p.h"
#include "qversitorganizerpluginloader_p.h"
QTORGANIZER_USE_NAMESPACE
QTVERSIT_USE_NAMESPACE
QT_BEGIN_NAMESPACE_VERSITORGANIZER
QVersitOrganizerExporterPrivate::QVersitOrganizerExporterPrivate(const QString& profile) :
mDetailHandler(NULL),
mTimeZoneHandler(NULL)
{
int versitPropertyCount =
sizeof(versitOrganizerDetailMappings)/sizeof(VersitOrganizerDetailMapping);
for (int i = 0; i < versitPropertyCount; i++) {
mPropertyMappings.insert(
versitOrganizerDetailMappings[i].detailType,
QPair<int, QString>(
versitOrganizerDetailMappings[i].detailField,
QLatin1String(versitOrganizerDetailMappings[i].versitPropertyName)));
}
mPluginDetailHandlers = QVersitOrganizerPluginLoader::instance()->createOrganizerHandlers(profile);
mTimeZoneHandler = QVersitOrganizerPluginLoader::instance()->timeZoneHandler();
}
QVersitOrganizerExporterPrivate::~QVersitOrganizerExporterPrivate()
{
foreach (QVersitOrganizerHandler* pluginHandler, mPluginDetailHandlers) {
delete pluginHandler;
}
}
bool QVersitOrganizerExporterPrivate::exportItem(
const QOrganizerItem& item,
QVersitDocument* document,
QVersitOrganizerExporter::Error* error)
{
if (item.isEmpty()) {
*error = QVersitOrganizerExporter::EmptyOrganizerError;
return false;
}
if (item.type() == QOrganizerItemType::TypeEvent
|| item.type() == QOrganizerItemType::TypeEventOccurrence) {
document->setComponentType(QStringLiteral("VEVENT"));
} else if (item.type() == QOrganizerItemType::TypeTodo
|| item.type() == QOrganizerItemType::TypeTodoOccurrence) {
document->setComponentType(QStringLiteral("VTODO"));
} else if (item.type() == QOrganizerItemType::TypeJournal) {
document->setComponentType(QStringLiteral("VJOURNAL"));
} else {
*error = QVersitOrganizerExporter::UnknownComponentTypeError;
return false;
}
QList<QOrganizerItemDetail> allDetails = item.details();
if (allDetails.isEmpty()) {
*error = QVersitOrganizerExporter::EmptyOrganizerError;
return false;
}
foreach (const QOrganizerItemDetail& detail, allDetails) {
exportDetail(item, detail, document);
}
// run plugin handlers
foreach (QVersitOrganizerExporterDetailHandler* handler, mPluginDetailHandlers) {
handler->itemProcessed(item, document);
}
// run the handler, if set
if (mDetailHandler) {
mDetailHandler->itemProcessed(item, document);
}
if (item.type() == QOrganizerItemType::TypeEventOccurrence
&& !documentContainsUidAndRecurrenceId(*document)) {
*error = QVersitOrganizerExporter::UnderspecifiedOccurrenceError;
return false;
}
return true;
}
void QVersitOrganizerExporterPrivate::exportDetail(
const QOrganizerItem& item,
const QOrganizerItemDetail& detail,
QVersitDocument* document)
{
QList<QVersitProperty> removedProperties;
QList<QVersitProperty> generatedProperties;
QSet<int> processedFields;
if (detail.type() == QOrganizerItemDetail::TypeEventTime) {
encodeEventTimeRange(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeTodoTime) {
encodeTodoTimeRange(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeJournalTime) {
encodeJournalTimeRange(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeTimestamp) {
encodeTimestamp(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeRecurrence) {
encodeRecurrence(item, detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypePriority) {
encodePriority(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeParent) {
encodeInstanceOrigin(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeTodoProgress) {
encodeTodoProgress(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeComment) {
encodeComment(detail, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeExtendedDetail) {
encodeExtendedDetail(detail, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeVersion) {
encodeVersion(detail, *document, &removedProperties, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeAudibleReminder) {
encodeAudibleReminder(item, detail, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeVisualReminder) {
encodeVisualReminder(item, detail, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeEmailReminder) {
encodeEmailReminder(item, detail, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeLocation) {
encodeLocation(detail, &generatedProperties, &processedFields);
} else if (detail.type() == QOrganizerItemDetail::TypeEventAttendee) {
encodeAttendee(detail, &generatedProperties, &processedFields);
} else if (mPropertyMappings.contains(detail.type())) {
encodeSimpleProperty(detail, *document, &removedProperties, &generatedProperties, &processedFields);
}
// run the plugin handler
foreach (QVersitOrganizerExporterDetailHandler* handler, mPluginDetailHandlers) {
handler->detailProcessed(item, detail, *document,
&processedFields, &removedProperties, &generatedProperties);
}
// run the detail handler, if set
if (mDetailHandler) {
mDetailHandler->detailProcessed(item, detail, *document,
&processedFields, &removedProperties, &generatedProperties);
}
foreach(const QVersitProperty& property, removedProperties) {
document->removeProperty(property);
}
foreach(const QVersitProperty& property, generatedProperties) {
if (property.valueType() == QVersitProperty::VersitDocumentType) {
QVersitDocument subDocument(QVersitDocument::ICalendar20Type);
subDocument = property.value<QVersitDocument>();
document->addSubDocument(subDocument);
} else {
document->addProperty(property);
}
}
}
void QVersitOrganizerExporterPrivate::encodeEventTimeRange(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerEventTime &etr = static_cast<const QOrganizerEventTime &>(detail);
bool isAllDay = etr.isAllDay();
QVersitProperty property = takeProperty(document, QStringLiteral("DTSTART"), removedProperties);
property.setName(QStringLiteral("DTSTART"));
if (isAllDay) {
property.setValue(etr.startDateTime().date().toString(QStringLiteral("yyyyMMdd")));
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("DATE"));
} else {
property.setValue(encodeDateTime(etr.startDateTime()));
}
*generatedProperties << property;
property = takeProperty(document, QStringLiteral("DTEND"), removedProperties);
property.setName(QStringLiteral("DTEND"));
if (isAllDay) {
// In iCalendar, the end date is exclusive while in Qt Organizer, it is inclusive.
property.setValue(etr.endDateTime().date().addDays(1).toString(QStringLiteral("yyyyMMdd")));
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("DATE"));
} else {
property.setValue(encodeDateTime(etr.endDateTime()));
}
*generatedProperties << property;
*processedFields << QOrganizerEventTime::FieldStartDateTime
<< QOrganizerEventTime::FieldEndDateTime;
}
void QVersitOrganizerExporterPrivate::encodeTodoTimeRange(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerTodoTime &ttr = static_cast<const QOrganizerTodoTime &>(detail);
bool isAllDay = ttr.isAllDay();
QVersitProperty property = takeProperty(document, QStringLiteral("DTSTART"), removedProperties);
property.setName(QStringLiteral("DTSTART"));
if (isAllDay) {
property.setValue(ttr.startDateTime().date().toString(QStringLiteral("yyyyMMdd")));
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("DATE"));
} else {
property.setValue(encodeDateTime(ttr.startDateTime()));
}
*generatedProperties << property;
property = takeProperty(document, QStringLiteral("DUE"), removedProperties);
property.setName(QStringLiteral("DUE"));
if (isAllDay) {
property.setValue(ttr.dueDateTime().date().toString(QStringLiteral("yyyyMMdd")));
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("DATE"));
} else {
property.setValue(encodeDateTime(ttr.dueDateTime()));
}
*generatedProperties << property;
*processedFields << QOrganizerTodoTime::FieldStartDateTime
<< QOrganizerTodoTime::FieldDueDateTime;
}
void QVersitOrganizerExporterPrivate::encodeJournalTimeRange(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerJournalTime &jtr = static_cast<const QOrganizerJournalTime &>(detail);
QVersitProperty property = takeProperty(document, QStringLiteral("DTSTART"), removedProperties);
property.setName(QStringLiteral("DTSTART"));
property.setValue(encodeDateTime(jtr.entryDateTime()));
*generatedProperties << property;
*processedFields << QOrganizerJournalTime::FieldEntryDateTime;
}
void QVersitOrganizerExporterPrivate::encodeTimestamp(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerItemTimestamp ×tamp = static_cast<const QOrganizerItemTimestamp &>(detail);
QVersitProperty property = takeProperty(document, QStringLiteral("CREATED"), removedProperties);
property.setName(QStringLiteral("CREATED"));
property.setValue(encodeDateTime(timestamp.created().toUTC()));
*generatedProperties << property;
property = takeProperty(document, QStringLiteral("LAST-MODIFIED"), removedProperties);
property.setName(QStringLiteral("LAST-MODIFIED"));
property.setValue(encodeDateTime(timestamp.lastModified().toUTC()));
*generatedProperties << property;
*processedFields << QOrganizerItemTimestamp::FieldCreated
<< QOrganizerItemTimestamp::FieldLastModified;
}
void QVersitOrganizerExporterPrivate::encodeVersion(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerItemVersion &version = static_cast<const QOrganizerItemVersion &>(detail);
QVersitProperty property = takeProperty(document, QStringLiteral("X-QTPROJECT-VERSION"), removedProperties);
property.setName(QStringLiteral("X-QTPROJECT-VERSION"));
QStringList values(QString::number(version.version()));
values.append(QString::fromLocal8Bit(version.extendedVersion()));
property.setValue(values);
property.setValueType(QVersitProperty::CompoundType);
*generatedProperties << property;
*processedFields << QOrganizerItemVersion::FieldVersion
<< QOrganizerItemVersion::FieldExtendedVersion;
}
void QVersitOrganizerExporterPrivate::encodeRecurrence(
const QOrganizerItem& item,
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerItemRecurrence &recurrence = static_cast<const QOrganizerItemRecurrence &>(detail);
QSet<QOrganizerRecurrenceRule> rrules = recurrence.recurrenceRules();
QSet<QOrganizerRecurrenceRule> exrules = recurrence.exceptionRules();
QSet<QDate> rdates = recurrence.recurrenceDates();
QSet<QDate> exdates = recurrence.exceptionDates();
if (!rrules.isEmpty()) {
foreach (const QOrganizerRecurrenceRule& rrule, rrules) {
encodeRecurRule(QStringLiteral("RRULE"), rrule, generatedProperties);
}
*processedFields << QOrganizerItemRecurrence::FieldRecurrenceRules;
}
if (!exrules.isEmpty()) {
foreach (const QOrganizerRecurrenceRule& exrule, exrules) {
encodeRecurRule(QStringLiteral("EXRULE"), exrule, generatedProperties);
}
*processedFields << QOrganizerItemRecurrence::FieldExceptionRules;
}
if (!rdates.isEmpty()) {
encodeRecurDates(QStringLiteral("RDATE"), item, rdates, document, removedProperties, generatedProperties);
*processedFields << QOrganizerItemRecurrence::FieldRecurrenceDates;
}
if (!exdates.isEmpty()) {
encodeRecurDates(QStringLiteral("EXDATE"), item, exdates, document, removedProperties, generatedProperties);
*processedFields << QOrganizerItemRecurrence::FieldExceptionDates;
}
}
/*! Encodes an iCalendar recurrence specification from \a rule. */
void QVersitOrganizerExporterPrivate::encodeRecurRule(
const QString& propertyName,
const QOrganizerRecurrenceRule& rule,
QList<QVersitProperty>* generatedProperties)
{
QVersitProperty property;
property.setName(propertyName);
QString value = QStringLiteral("FREQ=");
switch (rule.frequency()) {
case QOrganizerRecurrenceRule::Daily:
value.append(QStringLiteral("DAILY"));
break;
case QOrganizerRecurrenceRule::Weekly:
value.append(QStringLiteral("WEEKLY"));
break;
case QOrganizerRecurrenceRule::Monthly:
value.append(QStringLiteral("MONTHLY"));
break;
case QOrganizerRecurrenceRule::Yearly:
value.append(QStringLiteral("YEARLY"));
break;
case QOrganizerRecurrenceRule::Invalid:
default:
return;
}
if (rule.limitType() == QOrganizerRecurrenceRule::CountLimit) {
value.append(QStringLiteral(";COUNT="));
value.append(QString::number(rule.limitCount()));
}
if (rule.limitType() == QOrganizerRecurrenceRule::DateLimit) {
value.append(QStringLiteral(";UNTIL="));
value.append(rule.limitDate().toString(QStringLiteral("yyyyMMdd")));
}
if (rule.interval() > 1) {
value.append(QStringLiteral(";INTERVAL="));
value.append(QString::number(rule.interval()));
}
if (!rule.daysOfWeek().isEmpty()) {
value.append(QStringLiteral(";BYDAY="));
bool first = true;
QList<Qt::DayOfWeek> daysOfWeek(rule.daysOfWeek().values());
std::sort(daysOfWeek.begin(), daysOfWeek.end());
foreach (Qt::DayOfWeek day, daysOfWeek) {
if (!first)
value.append(QStringLiteral(","));
first = false;
value.append(weekString(day));
}
}
if (!rule.daysOfMonth().isEmpty()) {
value.append(QStringLiteral(";BYMONTHDAY="));
appendInts(&value, rule.daysOfMonth());
}
if (!rule.daysOfYear().isEmpty()) {
value.append(QStringLiteral(";BYYEARDAY="));
appendInts(&value, rule.daysOfYear());
}
if (!rule.weeksOfYear().isEmpty()) {
value.append(QStringLiteral(";BYWEEKNO="));
appendInts(&value, rule.weeksOfYear());
}
if (!rule.monthsOfYear().isEmpty()) {
value.append(QStringLiteral(";BYMONTH="));
bool first = true;
QList<QOrganizerRecurrenceRule::Month> months(rule.monthsOfYear().values());
std::sort(months.begin(), months.end());
foreach (QOrganizerRecurrenceRule::Month month, months) {
if (!first)
value.append(QStringLiteral(","));
first = false;
value.append(QString::number(month));
}
}
if (!rule.positions().isEmpty()) {
value.append(QStringLiteral(";BYSETPOS="));
appendInts(&value, rule.positions());
}
if (rule.firstDayOfWeek() != Qt::Monday && rule.firstDayOfWeek() > 0) {
value.append(QStringLiteral(";WKST="));
value.append(weekString(rule.firstDayOfWeek()));
}
property.setValue(value);
property.setValueType(QVersitProperty::PreformattedType);
*generatedProperties << property;
}
/*! Joins \a list together with commas and appends the result to \a str.
*/
void QVersitOrganizerExporterPrivate::appendInts(QString* str, const QSet<int>& ints) {
bool first = true;
QList<int> intList(ints.values());
std::sort(intList.begin(), intList.end());
foreach (int n, intList) {
if (!first)
str->append(QStringLiteral(","));
first = false;
str->append(QString::number(n));
}
}
/*! Converts \a day to a two-character iCalendar string */
QString QVersitOrganizerExporterPrivate::weekString(Qt::DayOfWeek day) {
switch (day) {
case Qt::Monday:
return QStringLiteral("MO");
case Qt::Tuesday:
return QStringLiteral("TU");
case Qt::Wednesday:
return QStringLiteral("WE");
case Qt::Thursday:
return QStringLiteral("TH");
case Qt::Friday:
return QStringLiteral("FR");
case Qt::Saturday:
return QStringLiteral("SA");
case Qt::Sunday:
return QStringLiteral("SU");
default:
return QString();
}
}
void QVersitOrganizerExporterPrivate::encodeRecurDates(
const QString& propertyName,
const QOrganizerItem& item,
const QSet<QDate>& dates,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties)
{
Q_UNUSED(item);
QVersitProperty property;
property = takeProperty(document, propertyName, removedProperties);
property.setName(propertyName);
property.insertParameter(QStringLiteral("VALUE"), QStringLiteral("DATE"));
QString value = property.value();
bool valueIsEmpty = value.isEmpty();
QList<QDate> dateList(dates.values());
std::sort(dateList.begin(), dateList.end());
foreach (const QDate& dt, dateList) {
QString str;
if (dt.isValid()) {
str = dt.toString(QStringLiteral("yyyyMMdd"));
if (!valueIsEmpty)
value += QLatin1Char(',');
value += str;
valueIsEmpty = false;
}
}
property.setValue(value);
*generatedProperties << property;
}
void QVersitOrganizerExporterPrivate::encodePriority(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerItemPriority &priority = static_cast<const QOrganizerItemPriority &>(detail);
QVersitProperty property =
takeProperty(document, QStringLiteral("PRIORITY"), removedProperties);
property.setName(QStringLiteral("PRIORITY"));
property.setValue(QString::number(priority.priority()));
*generatedProperties << property;
*processedFields << QOrganizerItemPriority::FieldPriority;
}
void QVersitOrganizerExporterPrivate::encodeInstanceOrigin(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerItemParent &instanceOrigin = static_cast<const QOrganizerItemParent &>(detail);
QVersitProperty property =
takeProperty(document, QStringLiteral("RECURRENCE-ID"), removedProperties);
property.setName(QStringLiteral("RECURRENCE-ID"));
property.setValue(instanceOrigin.originalDate().toString(QStringLiteral("yyyyMMdd")));
*generatedProperties << property;
*processedFields << QOrganizerItemParent::FieldOriginalDate;
}
void QVersitOrganizerExporterPrivate::encodeTodoProgress(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerTodoProgress &todoProgress = static_cast<const QOrganizerTodoProgress &>(detail);
if (todoProgress.finishedDateTime().isValid()) {
QVersitProperty property =
takeProperty(document, QStringLiteral("COMPLETED"), removedProperties);
property.setName(QStringLiteral("COMPLETED"));
property.setValue(todoProgress.finishedDateTime().toString(QStringLiteral("yyyyMMddTHHmmss")));
*generatedProperties << property;
*processedFields << QOrganizerTodoProgress::FieldFinishedDateTime;
}
if (todoProgress.hasValue(QOrganizerTodoProgress::FieldPercentageComplete)) {
QVersitProperty property =
takeProperty(document, QStringLiteral("PERCENT-COMPLETE"), removedProperties);
property.setName(QStringLiteral("PERCENT-COMPLETE"));
property.setValue(QString::number(todoProgress.percentageComplete()));
*generatedProperties << property;
*processedFields << QOrganizerTodoProgress::FieldPercentageComplete;
}
if (todoProgress.hasValue(QOrganizerTodoProgress::FieldStatus)) {
QVersitProperty property =
takeProperty(document, QStringLiteral("STATUS"), removedProperties);
property.setName(QStringLiteral("STATUS"));
switch (todoProgress.status()) {
case QOrganizerTodoProgress::StatusNotStarted:
property.setValue(QStringLiteral("NEEDS-ACTION"));
break;
case QOrganizerTodoProgress::StatusInProgress:
property.setValue(QStringLiteral("IN-PROCESS"));
break;
case QOrganizerTodoProgress::StatusComplete:
property.setValue(QStringLiteral("COMPLETED"));
break;
default:
return;
}
*generatedProperties << property;
*processedFields << QOrganizerTodoProgress::FieldStatus;
}
}
void QVersitOrganizerExporterPrivate::encodeComment(
const QOrganizerItemDetail& detail,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
const QOrganizerItemComment &comment = static_cast<const QOrganizerItemComment &>(detail);
QVersitProperty property;
property.setName(QStringLiteral("COMMENT"));
property.setValue(comment.comment());
*generatedProperties << property;
*processedFields << QOrganizerItemComment::FieldComment;
}
void QVersitOrganizerExporterPrivate::encodeAudibleReminder(
const QOrganizerItem &item,
const QOrganizerItemDetail &detail,
QList<QVersitProperty> *generatedProperties,
QSet<int> *processedFields)
{
const QOrganizerItemAudibleReminder &audibleReminder = static_cast<const QOrganizerItemAudibleReminder &>(detail);
QVersitProperty property;
QVersitDocument valarmDocument = encodeItemReminderCommonFields(item, audibleReminder, processedFields);
const QUrl attachUrl = audibleReminder.dataUrl();
if (!attachUrl.isEmpty()) {
property.setName(QStringLiteral("ATTACH"));
property.setValue(attachUrl.toString());
valarmDocument.addProperty(property);
*processedFields << QOrganizerItemAudibleReminder::FieldDataUrl;
}
property.setValueType(QVersitProperty::VersitDocumentType);
property.setValue(QVariant::fromValue(valarmDocument));
property.setName(QStringLiteral("VALARM"));
*generatedProperties << property;
}
void QVersitOrganizerExporterPrivate::encodeEmailReminder(
const QOrganizerItem &item,
const QOrganizerItemDetail &detail,
QList<QVersitProperty> *generatedProperties,
QSet<int> *processedFields)
{
const QOrganizerItemEmailReminder &emailReminder = static_cast<const QOrganizerItemEmailReminder &>(detail);
QVersitProperty property;
QVersitDocument valarmDocument = encodeItemReminderCommonFields(item, emailReminder, processedFields);
foreach (const QVariant &attachment, emailReminder.attachments()) {
property.setName(QStringLiteral("ATTACH"));
property.setValue(attachment);
valarmDocument.addProperty(property);
*processedFields << QOrganizerItemEmailReminder::FieldAttachments;
}
if (emailReminder.hasValue(QOrganizerItemEmailReminder::FieldRecipients)) {
property.setName(QStringLiteral("ATTENDEE"));
foreach (const QString &recipient, emailReminder.recipients()) {
property.setValue(recipient);
valarmDocument.addProperty(property);
}
*processedFields << QOrganizerItemEmailReminder::FieldRecipients;
}
// DESCRIPTION and SUMMARY properties are not optional,
// so we add them anyway even when empty
property.setName(QStringLiteral("DESCRIPTION"));
property.setValue(emailReminder.body());
valarmDocument.addProperty(property);
*processedFields << QOrganizerItemEmailReminder::FieldBody;
property.setName(QStringLiteral("SUMMARY"));
property.setValue(emailReminder.subject());
valarmDocument.addProperty(property);
*processedFields << QOrganizerItemEmailReminder::FieldSubject;
property.setValueType(QVersitProperty::VersitDocumentType);
property.setValue(QVariant::fromValue(valarmDocument));
property.setName(QStringLiteral("VALARM"));
*generatedProperties << property;
}
void QVersitOrganizerExporterPrivate::encodeVisualReminder(
const QOrganizerItem &item,
const QOrganizerItemDetail &detail,
QList<QVersitProperty> *generatedProperties,
QSet<int> *processedFields)
{
const QOrganizerItemVisualReminder &visualReminder = static_cast<const QOrganizerItemVisualReminder &>(detail);
QVersitProperty property;
QVersitDocument valarmDocument = encodeItemReminderCommonFields(item, visualReminder, processedFields);
const QUrl attachUrl = visualReminder.dataUrl();
const QString message = visualReminder.message();
if (!attachUrl.isEmpty()) {
//ICAL specs do not include ATTACH property for DISPLAY VALARM components
//Hence, we add it here as an (optional) extended QTPROJECT-specific property
property.setName(QStringLiteral("X-QTPROJECT-ATTACH"));
property.setValue(attachUrl.toString());
valarmDocument.addProperty(property);
*processedFields << QOrganizerItemAudibleReminder::FieldDataUrl;
}
//DESCRIPTION property is not optional, so we add it anyway even when empty
property.setName(QStringLiteral("DESCRIPTION"));
property.setValue(message);
valarmDocument.addProperty(property);
*processedFields << QOrganizerItemVisualReminder::FieldMessage;
property.setValueType(QVersitProperty::VersitDocumentType);
property.setValue(QVariant::fromValue(valarmDocument));
property.setName(QStringLiteral("VALARM"));
*generatedProperties << property;
}
void QVersitOrganizerExporterPrivate::encodeLocation(const QOrganizerItemDetail &detail, QList<QVersitProperty> *generatedProperties, QSet<int> *processedFields)
{
const QOrganizerItemLocation &location = static_cast<const QOrganizerItemLocation &>(detail);
QVersitProperty propertyLoc;
propertyLoc.setName(QStringLiteral("LOCATION"));
propertyLoc.setValue(location.label());
*processedFields << QOrganizerItemLocation::FieldLabel;
*generatedProperties << propertyLoc;
if (location.latitude() != 0 && location.longitude() != 0) {
QVersitProperty propertyGeo;
propertyGeo.setName(QStringLiteral("GEO"));
QString value = QString::number(location.latitude(), 'f', 6);
value.append(QLatin1Char(';'));
value.append(QString::number(location.longitude(), 'f', 6));
propertyGeo.setValue(value);
*processedFields << QOrganizerItemLocation::FieldLatitude << QOrganizerItemLocation::FieldLongitude;
*generatedProperties << propertyGeo;
}
}
void QVersitOrganizerExporterPrivate::encodeAttendee(const QOrganizerItemDetail &detail, QList<QVersitProperty> *generatedProperties, QSet<int> *processedFields)
{
QVersitProperty property;
property.setName(QStringLiteral("ATTENDEE"));
const QOrganizerEventAttendee &attendee = static_cast<const QOrganizerEventAttendee &>(detail);
QOrganizerEventAttendee::ParticipationStatus status = attendee.participationStatus();
QString value = QStringLiteral("PARSTAT=");
if (status == QOrganizerEventAttendee::StatusAccepted) {
value.append(QStringLiteral("ACCEPTED"));
} else if (status == QOrganizerEventAttendee::StatusDeclined) {
value.append(QStringLiteral("DECLINED"));
} else if (status == QOrganizerEventAttendee::StatusTentative) {
value.append(QStringLiteral("TENTATIVE"));
} else if (status == QOrganizerEventAttendee::StatusDelegated) {
value.append(QStringLiteral("DELEGATED"));
} else if (status == QOrganizerEventAttendee::StatusCompleted) {
value.append(QStringLiteral("COMPLETED"));
} else if (status == QOrganizerEventAttendee::StatusInProcess) {
value.append(QStringLiteral("IN-PROCESS"));
} else {
value.append(QStringLiteral("NEEDS-ACTION"));
}
*processedFields << QOrganizerEventAttendee::FieldParticipationStatus;
QOrganizerEventAttendee::ParticipationRole role = attendee.participationRole();
value.append(QStringLiteral(";ROLE="));
if (role == QOrganizerEventAttendee::RoleRequiredParticipant) {
value.append(QStringLiteral("REQ-PARTICIPANT"));
} else if (role == QOrganizerEventAttendee::RoleOptionalParticipant) {
value.append(QStringLiteral("OPT-PARTICIPANT"));
} else if (role == QOrganizerEventAttendee::RoleNonParticipant) {
value.append(QStringLiteral("NON-PARTICIPANT"));
} else if (role == QOrganizerEventAttendee::RoleChairperson) {
value.append(QStringLiteral("CHAIR"));
} else {
value.append(QStringLiteral("REQ-PARTICIPANT"));
}
*processedFields << QOrganizerEventAttendee::FieldParticipationRole;
if (!attendee.emailAddress().isEmpty()) {
value.append(QStringLiteral(":MAILTO:").append(attendee.emailAddress()));
*processedFields << QOrganizerEventAttendee::FieldEmailAddress;
}
if (!attendee.name().isEmpty()) {
value.append((QStringLiteral(";CN=")).append(attendee.name()));
*processedFields << QOrganizerEventAttendee::FieldName;
}
property.setValue(value);
property.setValueType(QVersitProperty::PreformattedType);
*generatedProperties << property;
}
QVersitDocument QVersitOrganizerExporterPrivate::encodeItemReminderCommonFields(
const QOrganizerItem &item,
const QOrganizerItemReminder &reminder,
QSet<int> *processedFields)
{
QVersitProperty property;
QVersitDocument valarmDocument(QVersitDocument::ICalendar20Type);
QList<QVersitProperty> reminderProperties;
valarmDocument.setComponentType(QStringLiteral("VALARM"));
property.setName(QStringLiteral("ACTION"));
switch (reminder.type()) {
case QOrganizerItemDetail::TypeAudibleReminder:
property.setValue(QStringLiteral("AUDIO"));
break;
case QOrganizerItemDetail::TypeVisualReminder:
property.setValue(QStringLiteral("DISPLAY"));
break;
case QOrganizerItemDetail::TypeEmailReminder:
property.setValue(QStringLiteral("EMAIL"));
break;
default:
return QVersitDocument();
}
reminderProperties << property;
QMap<int, QVariant>::const_iterator valueIter = reminder.values().constBegin();
bool triggerFound = false;
while (valueIter != reminder.values().constEnd()) {
switch (valueIter.key()) {
case QOrganizerItemReminder::FieldSecondsBeforeStart: {
property.setName(QStringLiteral("TRIGGER"));
property.setValue(QString(QStringLiteral("-PT%1S")).arg(
QString::number(valueIter.value().toInt())));
switch (item.type()) {
case QOrganizerItemType::TypeEvent:
case QOrganizerItemType::TypeEventOccurrence:
property.insertParameter(QStringLiteral("RELATED"), QStringLiteral("START"));
break;
case QOrganizerItemType::TypeTodo:
case QOrganizerItemType::TypeTodoOccurrence:
property.insertParameter(QStringLiteral("RELATED"), QStringLiteral("END"));
break;
default:
break;
}
triggerFound = true;
break;
}
case QOrganizerItemReminder::FieldRepetitionCount: {
property.setName(QStringLiteral("REPEAT"));
property.setValue(valueIter.value().toString());
break;
}
case QOrganizerItemReminder::FieldRepetitionDelay: {
property.setName(QStringLiteral("DURATION"));
property.setValue(QString(QStringLiteral("PT%1S")).arg(
QString::number(valueIter.value().toInt())));
break;
}
default:
valueIter ++;
property.clear();
continue;
break;
}
reminderProperties << property;
*processedFields << valueIter.key();
property.clear();
valueIter ++;
}
if (!triggerFound) {
property.setName(QStringLiteral("TRIGGER"));
property.setValue(QString(QStringLiteral("-PT%1S")).arg(
QString::number(0)));
reminderProperties << property;
}
valarmDocument.setProperties(reminderProperties);
return valarmDocument;
}
void QVersitOrganizerExporterPrivate::encodeExtendedDetail(
const QOrganizerItemDetail &detail,
QList<QVersitProperty> *generatedProperties,
QSet<int> *processedFields)
{
const QOrganizerItemExtendedDetail &extendedDetail = static_cast<const QOrganizerItemExtendedDetail &>(detail);
QVersitProperty property;
property.setName(QStringLiteral("X-QTPROJECT-EXTENDED-DETAIL"));
QString dataAsJson;
if (VersitUtils::convertToJson(extendedDetail.data(), &dataAsJson)) {
property.setValue(QStringList() << extendedDetail.name() << dataAsJson);
} else {
qWarning() << Q_FUNC_INFO << "Failed to export an extended detail";
return;
}
property.setValueType(QVersitProperty::CompoundType);
*generatedProperties << property;
*processedFields << QOrganizerItemExtendedDetail::FieldName
<< QOrganizerItemExtendedDetail::FieldData;
}
void QVersitOrganizerExporterPrivate::encodeSimpleProperty(
const QOrganizerItemDetail& detail,
const QVersitDocument& document,
QList<QVersitProperty>* removedProperties,
QList<QVersitProperty>* generatedProperties,
QSet<int>* processedFields)
{
QPair<int, QString> fieldPropertyMap = mPropertyMappings[detail.type()];
const int& fieldName = fieldPropertyMap.first;
const QString& propertyName = fieldPropertyMap.second;
QVersitProperty property =
takeProperty(document, propertyName, removedProperties);
property.setName(propertyName);
property.setValue(detail.value(fieldName));
*generatedProperties << property;
*processedFields << fieldName;
}
/*! Formats \a dateTime in ISO 8601 basic format */
QString QVersitOrganizerExporterPrivate::encodeDateTime(const QDateTime& dateTime)
{
if (dateTime.timeSpec() == Qt::UTC)
return dateTime.toString(QStringLiteral("yyyyMMddTHHmmssZ"));
else
return dateTime.toString(QStringLiteral("yyyyMMddTHHmmss"));
}
/*!
* Returns true if and only if \a document has both the UID and the RECURRENCE-ID properties
*/
bool QVersitOrganizerExporterPrivate::documentContainsUidAndRecurrenceId(const QVersitDocument &document)
{
bool hasUid = false;
bool hasRecurId = false;
foreach (const QVersitProperty& property, document.properties()) {
const QString& name = property.name();
if (name == QStringLiteral("UID")) {
if (hasRecurId)
return true;
hasUid = true;
} else if (name == QStringLiteral("RECURRENCE-ID")) {
if (hasUid)
return true;
hasRecurId = true;
}
}
return false;
}
/*!
* Finds a property in the \a document with the given \a propertyName, adds it to \a toBeRemoved,
* and returns it.
*/
QVersitProperty QVersitOrganizerExporterPrivate::takeProperty(
const QVersitDocument& document,
const QString& propertyName,
QList<QVersitProperty>* toBeRemoved) {
foreach (const QVersitProperty& currentProperty, document.properties()) {
if (currentProperty.name() == propertyName) {
*toBeRemoved << currentProperty;
return currentProperty;
}
}
return QVersitProperty();
}
QT_END_NAMESPACE_VERSITORGANIZER
|