~ubuntu-branches/ubuntu/raring/kdepim/raring-proposed

« back to all changes in this revision

Viewing changes to korganizer/views/todoview/kotodomodel.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2012-06-07 07:56:38 UTC
  • mfrom: (0.2.27)
  • Revision ID: package-import@ubuntu.com-20120607075638-0luhdq11z7sgvs4m
Tags: 4:4.8.80-0ubuntu1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "kotodomodel.h"
26
26
#include "koglobals.h"
 
27
#include "kohelper.h"
27
28
#include "koprefs.h"
28
 
#include "kohelper.h"
29
29
 
30
30
#include <calendarsupport/calendar.h>
31
 
#include <calendarsupport/calendaradaptor.h>
32
31
#include <calendarsupport/dndfactory.h>
33
32
#include <calendarsupport/incidencechanger.h>
34
33
#include <calendarsupport/kcalprefs.h>
35
34
#include <calendarsupport/utils.h>
36
35
 
37
 
#include <KCalCore/CalFormat>
38
 
#include <KCalCore/Todo>
 
36
#include <KCalCore/Attachment>
 
37
#include <KCalCore/Attendee>
 
38
 
 
39
#include <KCalUtils/ICalDrag>
39
40
#include <KCalUtils/IncidenceFormatter>
40
 
 
41
 
#ifndef KORG_NODND
42
 
  #include <KCalUtils/DndFactory>
43
 
  #include <KCalUtils/ICalDrag>
44
 
  #include <KCalUtils/VCalDrag>
45
 
#endif
 
41
#include <KCalUtils/VCalDrag>
46
42
 
47
43
#include <KPIMUtils/Email>
48
44
 
49
 
#include <KDebug>
50
 
#include <KLocale>
51
45
#include <KMessageBox>
52
46
 
53
 
#include <QBrush>
54
47
#include <QIcon>
55
 
 
56
 
#ifndef KORG_NODND
57
 
  #include <QMimeData>
58
 
#endif
59
 
 
60
 
using namespace KCalUtils;
 
48
#include <QMimeData>
61
49
 
62
50
/** This class represents a node in the todo-tree. */
63
51
struct KOTodoModel::TodoTreeNode : QObject
194
182
  mFlatView = false;
195
183
}
196
184
 
197
 
static bool isDueToday( const Todo::Ptr &todo )
 
185
static bool isDueToday( const KCalCore::Todo::Ptr &todo )
198
186
{
199
187
  return !todo->isCompleted() && todo->dtDue().date() == QDate::currentDate();
200
188
}
341
329
QModelIndex KOTodoModel::moveIfParentChanged( TodoTreeNode *curNode, const Akonadi::Item &aitem,
342
330
                                              bool addParentIfMissing )
343
331
{
344
 
  const Todo::Ptr todo = CalendarSupport::todo( aitem );
 
332
  const KCalCore::Todo::Ptr todo = CalendarSupport::todo( aitem );
345
333
  // find the model index of the changed incidence
346
334
  QModelIndex miChanged = getModelIndex( curNode );
347
335
 
349
337
  TodoTreeNode *ttOldParent = curNode->mParent;
350
338
 
351
339
  // get the new parent
352
 
  Todo::Ptr newParent;
 
340
  KCalCore::Todo::Ptr newParent;
353
341
 
354
342
  if ( !mFlatView ) {
355
343
    // in flat view, no todo has a parent
358
346
      const QString parentUid = todo->relatedTo();
359
347
      if ( !parentUid.isEmpty() ) {
360
348
        Akonadi::Item parentItem = mCalendar->itemForIncidenceUid( parentUid );
361
 
        Incidence::Ptr inc = CalendarSupport::incidence( parentItem );
362
 
        if ( inc && inc->type() == Incidence::TypeTodo ) {
363
 
          newParent = Todo::Ptr( static_cast<Todo *>( inc ->clone() ) );
 
349
        KCalCore::Incidence::Ptr inc = CalendarSupport::incidence( parentItem );
 
350
        if ( inc && inc->type() == KCalCore::Incidence::TypeTodo ) {
 
351
          newParent = KCalCore::Todo::Ptr( static_cast<KCalCore::Todo *>( inc ->clone() ) );
364
352
        }
365
353
      }
366
354
    }
378
366
    TodoTreeNode *ttNewParent = 0;
379
367
    if ( newParent ) {
380
368
      Akonadi::Item newParentItem;
381
 
      newParentItem.setPayload<Todo::Ptr>(newParent);
 
369
      newParentItem.setPayload<KCalCore::Todo::Ptr>(newParent);
382
370
      ttNewParent = findTodo( CalendarSupport::incidence ( newParentItem )->uid() );
383
371
      if ( !ttNewParent && addParentIfMissing ) {
384
372
        ttNewParent = insertTodo( newParentItem );
433
421
 
434
422
void KOTodoModel::expandTodoIfNeeded( const Akonadi::Item &todoItem )
435
423
{
436
 
  Todo::Ptr todo = CalendarSupport::todo( todoItem );
 
424
  KCalCore::Todo::Ptr todo = CalendarSupport::todo( todoItem );
437
425
  if ( !todo ) {
438
426
    return;
439
427
  }
444
432
  }
445
433
}
446
434
 
447
 
bool KOTodoModel::isInHierarchyLoop( const Todo::Ptr &todo ) const
 
435
bool KOTodoModel::isInHierarchyLoop( const KCalCore::Todo::Ptr &todo ) const
448
436
{
449
437
  if ( !todo ) {
450
438
    return false;
453
441
  QString parentUid = todo->relatedTo();
454
442
 
455
443
  if ( !parentUid.isEmpty() ) {
456
 
    Incidence::Ptr i = CalendarSupport::incidence( mCalendar->itemForIncidenceUid( parentUid ) );
457
 
    QList<Incidence::Ptr > processedParents;
 
444
    KCalCore::Incidence::Ptr i =
 
445
      CalendarSupport::incidence( mCalendar->itemForIncidenceUid( parentUid ) );
 
446
    QList<KCalCore::Incidence::Ptr > processedParents;
458
447
 
459
448
    // Lets iterate through all parents, if we find one with the same
460
449
    // uid then there's a loop.
486
475
KOTodoModel::TodoTreeNode *KOTodoModel::insertTodo( const Akonadi::Item &todoItem,
487
476
                                                    bool checkRelated )
488
477
{
489
 
  const Todo::Ptr todo = CalendarSupport::todo( todoItem );
 
478
  const KCalCore::Todo::Ptr todo = CalendarSupport::todo( todoItem );
490
479
  if ( !mFlatView && checkRelated && todo && !todo->relatedTo().isEmpty() ) {
491
480
    const QString parentUid = todo->relatedTo();
492
481
    Akonadi::Item parentItem = mCalendar->itemForIncidenceUid( parentUid );
493
 
    Incidence::Ptr incidence = CalendarSupport::incidence( parentItem );
494
 
    Todo::Ptr relatedTodo = incidence.dynamicCast<Todo>();
 
482
    KCalCore::Incidence::Ptr incidence = CalendarSupport::incidence( parentItem );
 
483
    KCalCore::Todo::Ptr relatedTodo = incidence.dynamicCast<KCalCore::Todo>();
495
484
 
496
485
    // check if there are recursively linked todos, or if we got broken input data
497
486
    if ( isInHierarchyLoop( todo ) || !relatedTodo ) {
501
490
 
502
491
    // FIXME(AKONADI_PORT) how to handle the related incidences where we don't know the item?
503
492
    Akonadi::Item relatedTodoItem;
504
 
    relatedTodoItem.setPayload( Todo::Ptr( relatedTodo->clone() ) );
 
493
    relatedTodoItem.setPayload( KCalCore::Todo::Ptr( relatedTodo->clone() ) );
505
494
 
506
495
    // if the parent is not already in the tree, we have to insert it first.
507
496
    // necessary because we can't rely on todos coming in a defined order.
560
549
 
561
550
  ret |= Qt::ItemIsDragEnabled;
562
551
 
563
 
  const Todo::Ptr todo = CalendarSupport::todo( node->mTodo );
 
552
  const KCalCore::Todo::Ptr todo = CalendarSupport::todo( node->mTodo );
564
553
 
565
554
  if ( mCalendar->hasChangeRights( node->mTodo ) ) {
566
555
    // the following columns are editable:
660
649
    return QVariant();
661
650
  }
662
651
 
663
 
  const Todo::Ptr todo = CalendarSupport::todo( node->mTodo );
 
652
  const KCalCore::Todo::Ptr todo = CalendarSupport::todo( node->mTodo );
664
653
 
665
654
  if ( role == Qt::DisplayRole ) {
666
655
    switch ( index.column() ) {
679
668
      return QVariant( todo->percentComplete() );
680
669
    case DueDateColumn:
681
670
      if ( todo->hasDueDate() && todo->dtDue().date().isValid() ) {
682
 
        return QVariant( IncidenceFormatter::dateToString( todo->dtDue() ) );
 
671
        return QVariant(
 
672
          KCalUtils::IncidenceFormatter::dateToString( todo->dtDue() ) );
683
673
      } else {
684
674
        return QVariant( QString() );
685
675
      }
692
682
    case DescriptionColumn:
693
683
      return QVariant( todo->description() );
694
684
    case CalendarColumn:
695
 
      return QVariant( CalendarSupport::displayName( node->mTodo.parentCollection() ) );
 
685
      return QVariant( CalendarSupport::displayName( mCalendar, node->mTodo.parentCollection() ) );
696
686
    }
697
687
    return QVariant();
698
688
  }
714
704
    case DescriptionColumn:
715
705
      return QVariant( todo->description() );
716
706
    case CalendarColumn:
717
 
      return QVariant( CalendarSupport::displayName( node->mTodo.parentCollection() ) );
 
707
      return QVariant( CalendarSupport::displayName( mCalendar, node->mTodo.parentCollection() ) );
718
708
    }
719
709
    return QVariant();
720
710
  }
722
712
  // set the tooltip for every item
723
713
  if ( role == Qt::ToolTipRole ) {
724
714
    if ( KOPrefs::instance()->enableToolTips() ) {
725
 
      return QVariant( IncidenceFormatter::toolTipStr(
726
 
                         CalendarSupport::displayName( node->mTodo.parentCollection() ),
727
 
                         todo, QDate(), true,
728
 
                         CalendarSupport::KCalPrefs::instance()->timeSpec() ) );
 
715
      return QVariant(
 
716
        KCalUtils::IncidenceFormatter::toolTipStr(
 
717
          CalendarSupport::displayName( mCalendar, node->mTodo.parentCollection() ),
 
718
          todo, QDate(), true,
 
719
          CalendarSupport::KCalPrefs::instance()->timeSpec() ) );
729
720
    } else {
730
721
      return QVariant();
731
722
    }
864
855
    return true;
865
856
  }
866
857
 
867
 
  const Todo::Ptr todo = CalendarSupport::todo( node->mTodo );
 
858
  const KCalCore::Todo::Ptr todo = CalendarSupport::todo( node->mTodo );
868
859
 
869
860
  if ( mCalendar->hasChangeRights( node->mTodo ) ) {
870
 
    Todo::Ptr oldTodo( todo->clone() );
 
861
    KCalCore::Todo::Ptr oldTodo( todo->clone() );
871
862
    CalendarSupport::IncidenceChanger::WhatChanged modified =
872
863
      CalendarSupport::IncidenceChanger::UNKNOWN_MODIFIED;
873
864
 
934
925
  }
935
926
}
936
927
 
937
 
#ifndef KORG_NODND
938
928
Qt::DropActions KOTodoModel::supportedDropActions() const
939
929
{
940
930
  // Qt::CopyAction not supported yet
945
935
{
946
936
  QStringList ret;
947
937
 
948
 
  ret << ICalDrag::mimeType() << VCalDrag::mimeType();
 
938
  ret << KCalUtils::ICalDrag::mimeType() << KCalUtils::VCalDrag::mimeType();
949
939
 
950
940
  return ret;
951
941
}
975
965
  }
976
966
 
977
967
  if ( mCalendar && mChanger &&
978
 
       ( ICalDrag::canDecode( data ) || VCalDrag::canDecode( data ) ) ) {
 
968
       ( KCalUtils::ICalDrag::canDecode( data ) || KCalUtils::VCalDrag::canDecode( data ) ) ) {
979
969
    CalendarSupport::DndFactory dndFactory (
980
970
      CalendarSupport::CalendarAdaptor::Ptr(
981
971
        new CalendarSupport::CalendarAdaptor( mCalendar, 0 ) ), true );
982
 
    Todo::Ptr t = dndFactory.createDropTodo( data );
983
 
    Event::Ptr e = dndFactory.createDropEvent( data );
 
972
    KCalCore::Todo::Ptr t = dndFactory.createDropTodo( data );
 
973
    KCalCore::Event::Ptr e = dndFactory.createDropEvent( data );
984
974
 
985
975
    if ( t ) {
986
976
      // we don't want to change the created todo, but the one which is already
987
977
      // stored in our calendar / tree
988
978
      TodoTreeNode *ttTodo = findTodo( t->uid() );
989
979
      Q_ASSERT( ttTodo ); //TODO if the todo is not found, just insert a new one
990
 
      Todo::Ptr todo = CalendarSupport::todo( ttTodo->mTodo );
 
980
      KCalCore::Todo::Ptr todo = CalendarSupport::todo( ttTodo->mTodo );
991
981
 
992
 
      Todo::Ptr destTodo;
 
982
      KCalCore::Todo::Ptr destTodo;
993
983
      if ( parent.isValid() ) {
994
984
        TodoTreeNode *node = static_cast<TodoTreeNode *>( parent.internalPointer() );
995
985
        if ( node->isValid() ) {
997
987
        }
998
988
      }
999
989
 
1000
 
      Incidence::Ptr tmp = destTodo;
 
990
      KCalCore::Incidence::Ptr tmp = destTodo;
1001
991
      while ( tmp ) {
1002
992
        if ( tmp->uid() == todo->uid() ) {
1003
993
          KMessageBox::information(
1010
1000
        tmp = CalendarSupport::incidence( mCalendar->itemForIncidenceUid( parentUid ) );
1011
1001
      }
1012
1002
 
1013
 
      Todo::Ptr oldTodo = Todo::Ptr( todo->clone() );
 
1003
      KCalCore::Todo::Ptr oldTodo = KCalCore::Todo::Ptr( todo->clone() );
1014
1004
      // destTodo is empty when we drag a to-do out of a relationship
1015
1005
      todo->setRelatedTo( destTodo ? destTodo->uid() : QString() );
1016
1006
      mChanger->changeIncidence( oldTodo, ttTodo->mTodo,
1029
1019
      }
1030
1020
 
1031
1021
      TodoTreeNode *node = static_cast<TodoTreeNode *>( parent.internalPointer() );
1032
 
      Todo::Ptr destTodo = CalendarSupport::todo( node->mTodo );
 
1022
      KCalCore::Todo::Ptr destTodo = CalendarSupport::todo( node->mTodo );
1033
1023
 
1034
1024
      if ( data->hasText() ) {
1035
1025
        QString text = data->text();
1036
1026
 
1037
 
        Todo::Ptr oldTodo = Todo::Ptr( destTodo->clone() );
 
1027
        KCalCore::Todo::Ptr oldTodo = KCalCore::Todo::Ptr( destTodo->clone() );
1038
1028
 
1039
1029
        if( text.startsWith( QLatin1String( "file:" ) ) ) {
1040
 
          destTodo->addAttachment( Attachment::Ptr( new Attachment( text ) ) );
 
1030
          destTodo->addAttachment(
 
1031
            KCalCore::Attachment::Ptr( new KCalCore::Attachment( text ) ) );
1041
1032
        } else {
1042
1033
          QStringList emails = KPIMUtils::splitAddressList( text );
1043
1034
          for ( QStringList::ConstIterator it = emails.constBegin();
1045
1036
            QString name, email, comment;
1046
1037
            if ( KPIMUtils::splitAddress( *it, name, email, comment ) ==
1047
1038
                 KPIMUtils::AddressOk ) {
1048
 
              destTodo->addAttendee( Attendee::Ptr( new Attendee( name, email ) ) );
 
1039
              destTodo->addAttendee(
 
1040
                KCalCore::Attendee::Ptr( new KCalCore::Attendee( name, email ) ) );
1049
1041
            }
1050
1042
          }
1051
1043
        }
1058
1050
 
1059
1051
  return false;
1060
1052
}
1061
 
#endif /*KORG_NODND*/
1062
1053
 
1063
1054
#include "kotodomodel.moc"