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
|
/*
* Copyright (C) 2011 Christian Mollekopf <mollekopf@kolabsys.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "kcalconversion.h"
#include <kcalcore/recurrence.h>
#include <QtCore/QBitArray>
#include <QtCore/QVector>
#include <QtCore/QDebug>
#include <QUrl>
#include <vector>
#include "kolabformat/errorhandler.h"
#include "commonconversion.h"
namespace Kolab {
namespace Conversion {
//The uid of a contact which refers to the uuid of a contact in the addressbook
#define CUSTOM_KOLAB_CONTACT_UUID "X_KOLAB_CONTACT_UUID"
#define CUSTOM_KOLAB_CONTACT_CUTYPE "X_KOLAB_CONTACT_CUTYPE"
#define CUSTOM_KOLAB_URL "X-KOLAB-URL"
KCalCore::Duration toDuration(const Kolab::Duration &d)
{
int value = 0;
if (d.hours() || d.minutes() || d.seconds()) {
value = ((((d.weeks() * 7 + d.days()) * 24 + d.hours()) * 60 + d.minutes()) * 60 + d.seconds());
if (d.isNegative()) {
value = -value;
}
return KCalCore::Duration(value);
}
value = d.weeks() * 7 + d.days();
if (d.isNegative()) {
value = -value;
}
return KCalCore::Duration(value, KCalCore::Duration::Days);
}
Kolab::Duration fromDuration(const KCalCore::Duration &d)
{
int value = d.value();
bool isNegative = false;
if (value < 0) {
isNegative = true;
value = -value;
}
//We don't know how the seconds/days were distributed before, so no point in distributing them (probably)
if (d.isDaily()) {
int days = value;
return Kolab::Duration(days, 0, 0, 0, isNegative);
}
int seconds = value;
// int minutes = seconds / 60;
// seconds = seconds % 60;
// int hours = minutes / 60;
// minutes = minutes % 60;
return Kolab::Duration(0, 0, 0, seconds, isNegative);
}
KCalCore::Incidence::Secrecy toSecrecy(Kolab::Classification c)
{
switch(c) {
case Kolab::ClassPublic:
return KCalCore::Incidence::SecrecyPublic;
case Kolab::ClassPrivate:
return KCalCore::Incidence::SecrecyPrivate;
case Kolab::ClassConfidential:
return KCalCore::Incidence::SecrecyConfidential;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return KCalCore::Incidence::SecrecyPublic;
}
Kolab::Classification fromSecrecy(KCalCore::Incidence::Secrecy c)
{
switch(c) {
case KCalCore::Incidence::SecrecyPublic:
return Kolab::ClassPublic;
case KCalCore::Incidence::SecrecyPrivate:
return Kolab::ClassPrivate;
case KCalCore::Incidence::SecrecyConfidential:
return Kolab::ClassConfidential;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return Kolab::ClassPublic;
}
int toPriority(int priority)
{
//Same mapping
return priority;
}
int fromPriority(int priority)
{
//Same mapping
return priority;
}
KCalCore::Incidence::Status toStatus(Kolab::Status s)
{
switch (s) {
case StatusUndefined:
return KCalCore::Incidence::StatusNone;
case StatusNeedsAction:
return KCalCore::Incidence::StatusNeedsAction;
case StatusCompleted:
return KCalCore::Incidence::StatusCompleted;
case StatusInProcess:
return KCalCore::Incidence::StatusInProcess;
case StatusCancelled:
return KCalCore::Incidence::StatusCanceled;
case StatusTentative:
return KCalCore::Incidence::StatusTentative;
case StatusConfirmed:
return KCalCore::Incidence::StatusConfirmed;
case StatusDraft:
return KCalCore::Incidence::StatusDraft;
case StatusFinal:
return KCalCore::Incidence::StatusFinal;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return KCalCore::Incidence::StatusNone;
}
Kolab::Status fromStatus(KCalCore::Incidence::Status s)
{
switch (s) {
case KCalCore::Incidence::StatusNone:
return StatusUndefined;
case KCalCore::Incidence::StatusNeedsAction:
return StatusNeedsAction;
case KCalCore::Incidence::StatusCompleted:
return StatusCompleted;
case KCalCore::Incidence::StatusInProcess:
return StatusInProcess;
case KCalCore::Incidence::StatusCanceled:
return StatusCancelled;
case KCalCore::Incidence::StatusTentative:
return StatusTentative;
case KCalCore::Incidence::StatusConfirmed:
return StatusConfirmed;
case KCalCore::Incidence::StatusDraft:
return StatusDraft;
case KCalCore::Incidence::StatusFinal:
return StatusFinal;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return StatusUndefined;
}
KCalCore::Attendee::PartStat toPartStat(Kolab::PartStatus p)
{
switch (p) {
case PartNeedsAction:
return KCalCore::Attendee::NeedsAction;
case PartAccepted:
return KCalCore::Attendee::Accepted;
case PartDeclined:
return KCalCore::Attendee::Declined;
case PartTentative:
return KCalCore::Attendee::Tentative;
case PartDelegated:
return KCalCore::Attendee::Delegated;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return KCalCore::Attendee::NeedsAction;
}
Kolab::PartStatus fromPartStat(KCalCore::Attendee::PartStat p)
{
switch (p) {
case KCalCore::Attendee::NeedsAction:
return PartNeedsAction;
case KCalCore::Attendee::Accepted:
return PartAccepted;
case KCalCore::Attendee::Declined:
return PartDeclined;
case KCalCore::Attendee::Tentative:
return PartTentative;
case KCalCore::Attendee::Delegated:
return PartDelegated;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return PartNeedsAction;
}
KCalCore::Attendee::Role toRole(Kolab::Role r)
{
switch (r) {
case Required:
return KCalCore::Attendee::ReqParticipant;
case Chair:
return KCalCore::Attendee::Chair;
case Optional:
return KCalCore::Attendee::OptParticipant;
case NonParticipant:
return KCalCore::Attendee::NonParticipant;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return KCalCore::Attendee::ReqParticipant;
}
Kolab::Role fromRole(KCalCore::Attendee::Role r)
{
switch (r) {
case KCalCore::Attendee::ReqParticipant:
return Required;
case KCalCore::Attendee::Chair:
return Chair;
case KCalCore::Attendee::OptParticipant:
return Optional;
case KCalCore::Attendee::NonParticipant:
return NonParticipant;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return Required;
}
template <typename T>
QString getCustomProperty(const QString &id, const T &e)
{
std::vector<CustomProperty> &props = e.customProperties();
foreach (const CustomProperty &p, props) {
if (fromStdString(p.identifier) == id) {
return fromStdString(p.value);
}
}
}
template <typename T>
void setIncidence(KCalCore::Incidence &i, const T &e)
{
if (!e.uid().empty()) {
i.setUid(fromStdString(e.uid()));
}
i.setCreated(toDate(e.created()));
i.setLastModified(toDate(e.lastModified()));
i.setRevision(e.sequence());
i.setSecrecy(toSecrecy(e.classification()));
i.setCategories(toStringList(e.categories()));
if (e.start().isValid()) {
i.setDtStart(toDate(e.start()));
}
i.setSummary(fromStdString(e.summary())); //TODO detect richtext
i.setDescription(fromStdString(e.description())); //TODO detect richtext
i.setStatus(toStatus(e.status()));
foreach (const Kolab::Attendee a, e.attendees()) {
/*
* KCalCore always sets a UID if empty, but that's just a pointer, and not the uid of a real contact.
* Since that means the semantics of the two are different, we have to store the kolab uid as a custom property.
*/
KCalCore::Attendee::Ptr attendee = KCalCore::Attendee::Ptr(new KCalCore::Attendee(fromStdString(a.contact().name()),
fromStdString(a.contact().email()),
a.rsvp(),
toPartStat(a.partStat()),
toRole(a.role()) ));
if (!a.contact().uid().empty()) { //TODO Identify contact from addressbook based on uid
attendee->customProperties().setNonKDECustomProperty(CUSTOM_KOLAB_CONTACT_UUID, fromStdString(a.contact().uid()));
}
if (!a.delegatedTo().empty()) {
if (a.delegatedTo().size() > 1) {
WARNING("multiple delegatees are not supported");
}
attendee->setDelegate(toMailto(a.delegatedTo().front().email(), a.delegatedTo().front().name()).toString());
}
if (!a.delegatedFrom().empty()) {
if (a.delegatedFrom().size() > 1) {
WARNING("multiple delegators are not supported");
}
attendee->setDelegator(toMailto(a.delegatedFrom().front().email(), a.delegatedFrom().front().name()).toString());
}
if (!a.cutype() != CutypeIndividual) {
attendee->customProperties().setNonKDECustomProperty(CUSTOM_KOLAB_CONTACT_CUTYPE, QString::number(a.cutype()));
}
i.addAttendee(attendee);
}
foreach (const Kolab::Attachment a, e.attachments()) {
KCalCore::Attachment::Ptr ptr;
if (!a.uri().empty()) {
ptr = KCalCore::Attachment::Ptr(new KCalCore::Attachment(fromStdString(a.uri()), fromStdString(a.mimetype())));
} else {
ptr = KCalCore::Attachment::Ptr(new KCalCore::Attachment(QByteArray::fromRawData(a.data().c_str(), a.data().size()), fromStdString(a.mimetype())));
}
if (!a.label().empty()) {
ptr->setLabel(fromStdString(a.label()));
}
i.addAttachment(ptr);
}
QMap<QByteArray, QString> props;
foreach (const Kolab::CustomProperty &prop, e.customProperties()) {
QString key;
if (prop.identifier.compare(0, 5, "X-KDE")) {
key.append(QString::fromLatin1("X-KOLAB-"));
}
key.append(fromStdString(prop.identifier));
props.insert(key.toLatin1(), fromStdString(prop.value));
// i.setCustomProperty("KOLAB", fromStdString(prop.identifier).toLatin1(), fromStdString(prop.value));
}
i.setCustomProperties(props);
}
template <typename T, typename I>
void getIncidence(T &i, const I &e)
{
i.setUid(toStdString(e.uid()));
i.setCreated(fromDate(e.created()));
i.setLastModified(fromDate(e.lastModified()));
i.setSequence(e.revision());
i.setClassification(fromSecrecy(e.secrecy()));
i.setCategories(fromStringList(e.categories()));
i.setStart(fromDate(e.dtStart()));
i.setSummary(toStdString(e.summary()));
i.setDescription(toStdString(e.description()));
i.setStatus(fromStatus(e.status()));
std::vector<Kolab::Attendee> attendees;
foreach (const KCalCore::Attendee::Ptr ptr, e.attendees()) {
const QString &uid = ptr->customProperties().nonKDECustomProperty(CUSTOM_KOLAB_CONTACT_UUID);
Kolab::Attendee a(Kolab::ContactReference(toStdString(ptr->email()), toStdString(ptr->name()), toStdString(uid)));
a.setRSVP(ptr->RSVP());
a.setPartStat(fromPartStat(ptr->status()));
a.setRole(fromRole(ptr->role()));
if (!ptr->delegate().isEmpty()) {
std::string name;
const std::string &email = fromMailto(ptr->delegate(), name);
a.setDelegatedTo(std::vector<Kolab::ContactReference>() << Kolab::ContactReference(email, name));
}
if (!ptr->delegator().isEmpty()) {
std::string name;
const std::string &email = fromMailto(ptr->delegator(), name);
a.setDelegatedFrom(std::vector<Kolab::ContactReference>() << Kolab::ContactReference(email, name));
}
const QString &cutype = ptr->customProperties().nonKDECustomProperty(CUSTOM_KOLAB_CONTACT_CUTYPE);
if (!cutype.isEmpty()) {
a.setCutype(static_cast<Kolab::Cutype>(cutype.toInt()));
}
attendees.push_back(a);
}
i.setAttendees(attendees);
std::vector<Kolab::Attachment> attachments;
foreach (const KCalCore::Attachment::Ptr &ptr, e.attachments()) {
Kolab::Attachment a;
if (ptr->isUri()) {
a.setUri(toStdString(ptr->uri()), toStdString(ptr->mimeType()));
} else {
a.setData(std::string(ptr->decodedData().data(), ptr->decodedData().size()), toStdString(ptr->mimeType()));
}
a.setLabel(toStdString(ptr->label()));
attachments.push_back(a);
}
i.setAttachments(attachments);
std::vector<Kolab::CustomProperty> customProperties;
const QMap<QByteArray, QString> &props = e.customProperties();
for (QMap<QByteArray, QString>::const_iterator it = props.begin(); it != props.end(); it++) {
QString key(it.key());
if (key == QLatin1String(CUSTOM_KOLAB_URL)) {
continue;
}
customProperties.push_back(Kolab::CustomProperty(toStdString(key.remove("X-KOLAB-")), toStdString(it.value())));
}
i.setCustomProperties(customProperties);
}
int toWeekDay(Kolab::Weekday wday)
{
switch (wday) {
case Kolab::Monday:
return 1;
case Kolab::Tuesday:
return 2;
case Kolab::Wednesday:
return 3;
case Kolab::Thursday:
return 4;
case Kolab::Friday:
return 5;
case Kolab::Saturday:
return 6;
case Kolab::Sunday:
return 7;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return 1;
}
Kolab::Weekday fromWeekDay(int wday)
{
switch (wday) {
case 1:
return Kolab::Monday;
case 2:
return Kolab::Tuesday;
case 3:
return Kolab::Wednesday;
case 4:
return Kolab::Thursday;
case 5:
return Kolab::Friday;
case 6:
return Kolab::Saturday;
case 7:
return Kolab::Sunday;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return Kolab::Monday;
}
KCalCore::RecurrenceRule::PeriodType toRecurrenceType(Kolab::RecurrenceRule::Frequency freq)
{
switch(freq) {
case Kolab::RecurrenceRule::FreqNone:
Warning() << "no recurrence?";
break;
case Kolab::RecurrenceRule::Yearly:
return KCalCore::RecurrenceRule::rYearly;
case Kolab::RecurrenceRule::Monthly:
return KCalCore::RecurrenceRule::rMonthly;
case Kolab::RecurrenceRule::Weekly:
return KCalCore::RecurrenceRule::rWeekly;
case Kolab::RecurrenceRule::Daily:
return KCalCore::RecurrenceRule::rDaily;
case Kolab::RecurrenceRule::Hourly:
return KCalCore::RecurrenceRule::rHourly;
case Kolab::RecurrenceRule::Minutely:
return KCalCore::RecurrenceRule::rMinutely;
case Kolab::RecurrenceRule::Secondly:
return KCalCore::RecurrenceRule::rSecondly;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return KCalCore::RecurrenceRule::rNone;
}
Kolab::RecurrenceRule::Frequency fromRecurrenceType(KCalCore::RecurrenceRule::PeriodType freq)
{
switch(freq) {
case KCalCore::RecurrenceRule::rNone:
Warning() << "no recurrence?";
break;
case KCalCore::RecurrenceRule::rYearly:
return Kolab::RecurrenceRule::Yearly;
case KCalCore::RecurrenceRule::rMonthly:
return Kolab::RecurrenceRule::Monthly;
case KCalCore::RecurrenceRule::rWeekly:
return Kolab::RecurrenceRule::Weekly;
case KCalCore::RecurrenceRule::rDaily:
return Kolab::RecurrenceRule::Daily;
case KCalCore::RecurrenceRule::rHourly:
return Kolab::RecurrenceRule::Hourly;
case KCalCore::RecurrenceRule::rMinutely:
return Kolab::RecurrenceRule::Minutely;
case KCalCore::RecurrenceRule::rSecondly:
return Kolab::RecurrenceRule::Secondly;
default:
Error() << "unhandled";
Q_ASSERT(0);
}
return Kolab::RecurrenceRule::FreqNone;
}
KCalCore::RecurrenceRule::WDayPos toWeekDayPos(const Kolab::DayPos &dp)
{
return KCalCore::RecurrenceRule::WDayPos(dp.occurence(), toWeekDay(dp.weekday()));
}
Kolab::DayPos fromWeekDayPos(const KCalCore::RecurrenceRule::WDayPos &dp)
{
return Kolab::DayPos(dp.pos(), fromWeekDay(dp.day()));
}
template <typename T>
void setRecurrence(KCalCore::Incidence &e, const T &event)
{
const Kolab::RecurrenceRule &rrule = event.recurrenceRule();
if (rrule.isValid()) {
KCalCore::Recurrence *rec = e.recurrence();
KCalCore::RecurrenceRule *defaultRR = rec->defaultRRule(true);
Q_ASSERT(defaultRR);
defaultRR->setWeekStart(toWeekDay(rrule.weekStart()));
defaultRR->setRecurrenceType(toRecurrenceType(rrule.frequency()));
defaultRR->setFrequency(rrule.interval());
if (rrule.end().isValid()) {
rec->setEndDateTime(toDate(rrule.end())); //TODO date/datetime setEndDate(). With date-only the start date has to be taken into account.
} else {
rec->setDuration(rrule.count());
}
if (!rrule.bysecond().empty()) {
defaultRR->setBySeconds(QVector<int>::fromStdVector(rrule.bysecond()).toList());
}
if (!rrule.byminute().empty()) {
defaultRR->setByMinutes(QVector<int>::fromStdVector(rrule.byminute()).toList());
}
if (!rrule.byhour().empty()) {
defaultRR->setByHours(QVector<int>::fromStdVector(rrule.byhour()).toList());
}
if (!rrule.byday().empty()) {
QList<KCalCore::RecurrenceRule::WDayPos> daypos;
foreach(const Kolab::DayPos &dp, rrule.byday()) {
daypos.append(toWeekDayPos(dp));
}
defaultRR->setByDays(daypos);
}
if (!rrule.bymonthday().empty()) {
defaultRR->setByMonthDays(QVector<int>::fromStdVector(rrule.bymonthday()).toList());
}
if (!rrule.byyearday().empty()) {
defaultRR->setByYearDays(QVector<int>::fromStdVector(rrule.byyearday()).toList());
}
if (!rrule.byweekno().empty()) {
defaultRR->setByWeekNumbers(QVector<int>::fromStdVector(rrule.byweekno()).toList());
}
if (!rrule.bymonth().empty()) {
defaultRR->setByMonths(QVector<int>::fromStdVector(rrule.bymonth()).toList());
}
}
foreach (const Kolab::cDateTime &dt, event.recurrenceDates()) {
const KDateTime &date = toDate(dt);
if (date.isDateOnly()) {
e.recurrence()->addRDate(date.date());
} else {
e.recurrence()->addRDateTime(date);
}
}
foreach (const Kolab::cDateTime &dt, event.exceptionDates()) {
const KDateTime &date = toDate(dt);
if (date.isDateOnly()) {
e.recurrence()->addExDate(date.date());
} else {
e.recurrence()->addExDateTime(date);
}
}
}
template <typename T, typename I>
void getRecurrence(T &i, const I &e)
{
if (!e.recurs()) {
return;
}
KCalCore::Recurrence *rec = e.recurrence();
KCalCore::RecurrenceRule *defaultRR = rec->defaultRRule(false);
if (!defaultRR) {
Warning() << "no recurrence";
return;
}
Q_ASSERT(defaultRR);
Kolab::RecurrenceRule rrule;
rrule.setWeekStart(fromWeekDay(defaultRR->weekStart()));
rrule.setFrequency(fromRecurrenceType(defaultRR->recurrenceType()));
rrule.setInterval(defaultRR->frequency());
if (defaultRR->duration() != 0) { //Inidcates if end date is set or not
if (defaultRR->duration() > 0) {
rrule.setCount(defaultRR->duration());
}
} else {
rrule.setEnd(fromDate(defaultRR->endDt()));
}
rrule.setBysecond(defaultRR->bySeconds().toVector().toStdVector());
rrule.setByminute(defaultRR->byMinutes().toVector().toStdVector());
rrule.setByhour(defaultRR->byHours().toVector().toStdVector());
std::vector<Kolab::DayPos> daypos;
foreach (const KCalCore::RecurrenceRule::WDayPos &dp, defaultRR->byDays()) {
daypos.push_back(fromWeekDayPos(dp));
}
rrule.setByday(daypos);
rrule.setBymonthday(defaultRR->byMonthDays().toVector().toStdVector());
rrule.setByyearday(defaultRR->byYearDays().toVector().toStdVector());
rrule.setByweekno(defaultRR->byWeekNumbers().toVector().toStdVector());
rrule.setBymonth(defaultRR->byMonths().toVector().toStdVector());
i.setRecurrenceRule(rrule);
std::vector<Kolab::cDateTime> rdates;
foreach (const KDateTime &dt, rec->rDateTimes()) {
rdates.push_back(fromDate(dt));
}
foreach (const QDate &dt, rec->rDates()) {
rdates.push_back(fromDate(KDateTime(dt)));
}
i.setRecurrenceDates(rdates);
std::vector<Kolab::cDateTime> exdates;
foreach (const KDateTime &dt, rec->exDateTimes()) {
exdates.push_back(fromDate(dt));
}
foreach (const QDate &dt, rec->exDates()) {
exdates.push_back(fromDate(KDateTime(dt)));
}
i.setExceptionDates(exdates);
if (!rec->exRules().empty()) {
Warning() << "exrules are not supported";
}
}
template <typename T>
void setTodoEvent(KCalCore::Incidence &i, const T &e)
{
i.setPriority(toPriority(e.priority()));
if (!e.location().empty()) {
i.setLocation(fromStdString(e.location())); //TODO detect richtext
}
if (e.organizer().isValid()) {
i.setOrganizer(KCalCore::Person::Ptr(new KCalCore::Person(fromStdString(e.organizer().name()), fromStdString(e.organizer().email())))); //TODO handle uid too
}
if (!e.url().empty()) {
i.setNonKDECustomProperty(CUSTOM_KOLAB_URL, fromStdString(e.url()));
}
if (e.recurrenceID().isValid()) {
i.setRecurrenceId(toDate(e.recurrenceID())); //TODO THISANDFUTURE
}
setRecurrence(i, e);
foreach (const Kolab::Alarm a, e.alarms()) {
KCalCore::Alarm::Ptr alarm = KCalCore::Alarm::Ptr(new KCalCore::Alarm(&i));
switch (a.type()) {
case Kolab::Alarm::EMailAlarm: {
KCalCore::Person::List receipents;
foreach (Kolab::ContactReference c ,a.attendees()) {
KCalCore::Person::Ptr person = KCalCore::Person::Ptr(new KCalCore::Person(fromStdString(c.name()), fromStdString(c.email())));
receipents.append(person);
}
alarm->setEmailAlarm(fromStdString(a.summary()), fromStdString(a.description()), receipents);
}
break;
case Kolab::Alarm::DisplayAlarm:
alarm->setDisplayAlarm(fromStdString(a.text()));
break;
case Kolab::Alarm::AudioAlarm:
alarm->setAudioAlarm(fromStdString(a.audioFile().uri()));
break;
default:
Error() << "invalid alarm";
}
if (a.start().isValid()) {
alarm->setTime(toDate(a.start()));
} else if (a.relativeStart().isValid()) {
if (a.relativeTo() == Kolab::End) {
alarm->setEndOffset(toDuration(a.relativeStart()));
} else {
alarm->setStartOffset(toDuration(a.relativeStart()));
}
}
alarm->setSnoozeTime(toDuration(a.duration()));
alarm->setRepeatCount(a.numrepeat());
alarm->setEnabled(true);
i.addAlarm(alarm);
}
}
template <typename T, typename I>
void getTodoEvent(T &i, const I &e)
{
i.setPriority(fromPriority(e.priority()));
i.setLocation(toStdString(e.location()));
if (e.organizer() && !e.organizer()->email().isEmpty()) {
i.setOrganizer(Kolab::ContactReference(Kolab::ContactReference::EmailReference, toStdString(e.organizer()->email()), toStdString(e.organizer()->name()))); //TODO handle uid too
}
i.setUrl(toStdString(e.nonKDECustomProperty(CUSTOM_KOLAB_URL)));
i.setRecurrenceID(fromDate(e.recurrenceId()), false); //TODO THISANDFUTURE
getRecurrence(i, e);
std::vector <Kolab::Alarm> alarms;
foreach (const KCalCore::Alarm::Ptr &a, e.alarms()) {
Kolab::Alarm alarm;
//TODO KCalCore disables alarms using KCalCore::Alarm::enabled() (X-KDE-KCALCORE-ENABLED) We should either delete the alarm, or store the attribute .
//Ideally we would store the alarm somewhere and temporarily delete it, so we can restore it when parsing. For now we just remove disabled alarms.
if (!a->enabled()) {
Warning() << "skipping disabled alarm";
continue;
}
switch (a->type()) {
case KCalCore::Alarm::Display:
alarm = Kolab::Alarm(toStdString(a->text()));
break;
case KCalCore::Alarm::Email: {
std::vector<Kolab::ContactReference> receipents;
foreach(const KCalCore::Person::Ptr &p, a->mailAddresses()) {
receipents.push_back(Kolab::ContactReference(toStdString(p->email()), toStdString(p->name())));
}
alarm = Kolab::Alarm(toStdString(a->mailSubject()), toStdString(a->mailText()), receipents);
}
break;
case KCalCore::Alarm::Audio: {
Kolab::Attachment audioFile;
audioFile.setUri(toStdString(a->audioFile()), std::string());
alarm = Kolab::Alarm(audioFile);
}
break;
default:
Error() << "unhandled alarm";
}
if (a->hasTime()) {
alarm.setStart(fromDate(a->time()));
} else if (a->hasStartOffset()) {
alarm.setRelativeStart(fromDuration(a->startOffset()), Start);
} else if (a->hasEndOffset()) {
alarm.setRelativeStart(fromDuration(a->endOffset()), End);
} else {
Error() << "alarm trigger is missing";
continue;
}
alarm.setDuration(fromDuration(a->snoozeTime()), a->repeatCount());
alarms.push_back(alarm);
}
i.setAlarms(alarms);
}
KCalCore::Event::Ptr toKCalCore(const Kolab::Event &event)
{
KCalCore::Event::Ptr e(new KCalCore::Event);
setIncidence(*e, event);
setTodoEvent(*e, event);
if (event.end().isValid()) {
e->setDtEnd(toDate(event.end()));
}
if (event.duration().isValid()) {
e->setDuration(toDuration(event.duration()));
}
if (event.transparency()) {
e->setTransparency(KCalCore::Event::Transparent);
} else {
e->setTransparency(KCalCore::Event::Opaque);
}
return e;
}
Event fromKCalCore(const KCalCore::Event &event)
{
Event e;
getIncidence(e, event);
getTodoEvent(e, event);
if (event.hasEndDate()) {
e.setEnd(fromDate(event.dtEnd()));
} else if (event.hasDuration()) {
e.setDuration(fromDuration(event.duration()));
}
if (event.transparency() == KCalCore::Event::Transparent) {
e.setTransparency(true);
} else {
e.setTransparency(false);
}
return e;
}
KCalCore::Todo::Ptr toKCalCore ( const Todo &todo )
{
KCalCore::Todo::Ptr e(new KCalCore::Todo);
setIncidence(*e, todo);
setTodoEvent(*e, todo);
if (todo.due().isValid()) {
e->setDtDue(toDate(todo.due()));
}
if (!todo.relatedTo().empty()) {
e->setRelatedTo(Conversion::fromStdString(todo.relatedTo().front()), KCalCore::Incidence::RelTypeParent);
if (todo.relatedTo().size() > 1) {
Error() << "only one relation support but got multiple";
}
}
e->setPercentComplete(todo.percentComplete());
return e;
}
Todo fromKCalCore ( const KCalCore::Todo &todo )
{
Todo t;
getIncidence(t, todo);
getTodoEvent(t, todo);
t.setDue(fromDate(todo.dtDue(true)));
t.setPercentComplete(todo.percentComplete());
const QString relatedTo = todo.relatedTo(KCalCore::Incidence::RelTypeParent);
if (!relatedTo.isEmpty()) {
std::vector<std::string> relateds;
relateds.push_back(Conversion::toStdString(relatedTo));
t.setRelatedTo(relateds);
}
return t;
}
KCalCore::Journal::Ptr toKCalCore ( const Journal &journal )
{
KCalCore::Journal::Ptr e(new KCalCore::Journal);
setIncidence(*e, journal);
//TODO contacts
return e;
}
Journal fromKCalCore ( const KCalCore::Journal &journal )
{
Journal j;
getIncidence(j, journal);
//TODO contacts
return j;
}
}
}
|