calendarui/views/dayview/src/calendaymodel.cpp
author hgs
Mon, 09 Aug 2010 18:30:52 +0530
changeset 57 bb2d3e476f29
parent 45 b6db4fd4947b
permissions -rw-r--r--
201031
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description: Single day item view model
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
57
hgs
parents: 45
diff changeset
    18
// System includes
45
hgs
parents:
diff changeset
    19
#include <calenservices.h>
hgs
parents:
diff changeset
    20
#include <agendautil.h>
57
hgs
parents: 45
diff changeset
    21
hgs
parents: 45
diff changeset
    22
// User includes
45
hgs
parents:
diff changeset
    23
#include "calendaymodel.h"
hgs
parents:
diff changeset
    24
hgs
parents:
diff changeset
    25
hgs
parents:
diff changeset
    26
/*!
57
hgs
parents: 45
diff changeset
    27
 BC Day Model constructor
hgs
parents: 45
diff changeset
    28
 \a date defined day for which entries will be fetched \a services is handle to 
hgs
parents: 45
diff changeset
    29
 organizer services \a parent
45
hgs
parents:
diff changeset
    30
 */
57
hgs
parents: 45
diff changeset
    31
CalenDayModel::CalenDayModel(const QDateTime &date, MCalenServices &services, QObject *parent) :
hgs
parents: 45
diff changeset
    32
    QAbstractListModel(parent), mServices(services)
45
hgs
parents:
diff changeset
    33
{
57
hgs
parents: 45
diff changeset
    34
    if (date.isValid()) {
hgs
parents: 45
diff changeset
    35
        loadAndStoreInstances(date);
hgs
parents: 45
diff changeset
    36
    }
45
hgs
parents:
diff changeset
    37
}
hgs
parents:
diff changeset
    38
hgs
parents:
diff changeset
    39
/*
57
hgs
parents: 45
diff changeset
    40
 \reimp
45
hgs
parents:
diff changeset
    41
 */
57
hgs
parents: 45
diff changeset
    42
int CalenDayModel::rowCount(const QModelIndex &parent) const
45
hgs
parents:
diff changeset
    43
{
hgs
parents:
diff changeset
    44
    Q_UNUSED( parent )
hgs
parents:
diff changeset
    45
    return mEntryList.count();
hgs
parents:
diff changeset
    46
}
hgs
parents:
diff changeset
    47
hgs
parents:
diff changeset
    48
/*
57
hgs
parents: 45
diff changeset
    49
 \reimp
45
hgs
parents:
diff changeset
    50
 */
hgs
parents:
diff changeset
    51
QVariant CalenDayModel::data(const QModelIndex &index, int role) const
hgs
parents:
diff changeset
    52
{
57
hgs
parents: 45
diff changeset
    53
    if (!index.isValid()) {
45
hgs
parents:
diff changeset
    54
        return QVariant();
57
hgs
parents: 45
diff changeset
    55
    }
45
hgs
parents:
diff changeset
    56
57
hgs
parents: 45
diff changeset
    57
    if (index.row() >= mEntryList.count()) {
45
hgs
parents:
diff changeset
    58
        return QVariant();
57
hgs
parents: 45
diff changeset
    59
    }
45
hgs
parents:
diff changeset
    60
57
hgs
parents: 45
diff changeset
    61
    if (role == CalenDayEntry) {
45
hgs
parents:
diff changeset
    62
        return mEntryList.at(index.row());
57
hgs
parents: 45
diff changeset
    63
    }
hgs
parents: 45
diff changeset
    64
    else {
45
hgs
parents:
diff changeset
    65
        return QVariant();
57
hgs
parents: 45
diff changeset
    66
    }
45
hgs
parents:
diff changeset
    67
}
hgs
parents:
diff changeset
    68
hgs
parents:
diff changeset
    69
/*!
57
hgs
parents: 45
diff changeset
    70
 Resets model. Old events are removed. Evenets for given day are fetched.
hgs
parents: 45
diff changeset
    71
 \a date defined day for which entries will be fetched
45
hgs
parents:
diff changeset
    72
 */
hgs
parents:
diff changeset
    73
void CalenDayModel::refreshModel(const QDateTime &date)
hgs
parents:
diff changeset
    74
{
57
hgs
parents: 45
diff changeset
    75
    beginResetModel();
hgs
parents: 45
diff changeset
    76
    loadAndStoreInstances(date);
hgs
parents: 45
diff changeset
    77
    endResetModel();
45
hgs
parents:
diff changeset
    78
}
hgs
parents:
diff changeset
    79
hgs
parents:
diff changeset
    80
/*!
57
hgs
parents: 45
diff changeset
    81
 Fetches entries via. organizer API and stores it in member container 
hgs
parents: 45
diff changeset
    82
 */
hgs
parents: 45
diff changeset
    83
void CalenDayModel::loadAndStoreInstances(const QDateTime &date)
hgs
parents: 45
diff changeset
    84
{
hgs
parents: 45
diff changeset
    85
    mDateTime = date;
hgs
parents: 45
diff changeset
    86
    //Filter flags
hgs
parents: 45
diff changeset
    87
    AgendaUtil::FilterFlags filter = AgendaUtil::FilterFlags(AgendaUtil::IncludeAppointments
hgs
parents: 45
diff changeset
    88
        | AgendaUtil::IncludeEvents);
hgs
parents: 45
diff changeset
    89
    QList<AgendaEntry> list;
hgs
parents: 45
diff changeset
    90
    // Fetch the instance list from the agenda interface
hgs
parents: 45
diff changeset
    91
    list = mServices.agendaInterface()->createEntryIdListForDay(date, filter);
hgs
parents: 45
diff changeset
    92
hgs
parents: 45
diff changeset
    93
    mEntryList.clear();
hgs
parents: 45
diff changeset
    94
hgs
parents: 45
diff changeset
    95
    foreach(AgendaEntry entry, list){
hgs
parents: 45
diff changeset
    96
        mEntryList.append(QVariant::fromValue(entry));
hgs
parents: 45
diff changeset
    97
    }
hgs
parents: 45
diff changeset
    98
}
hgs
parents: 45
diff changeset
    99
hgs
parents: 45
diff changeset
   100
/*!
hgs
parents: 45
diff changeset
   101
 Retruns date (day). Model holds events for this day. 
45
hgs
parents:
diff changeset
   102
 */
hgs
parents:
diff changeset
   103
QDateTime CalenDayModel::modelDate() const
hgs
parents:
diff changeset
   104
{
57
hgs
parents: 45
diff changeset
   105
    return mDateTime;
45
hgs
parents:
diff changeset
   106
}