~ubuntu-branches/ubuntu/quantal/qtmobility/quantal

« back to all changes in this revision

Viewing changes to plugins/declarative/organizer/qmllorganizermodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-16 16:18:07 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101116161807-k2dzt2nyse975r3l
Tags: 1.1.0-0ubuntu1
* New upstream release
* Syncronise with Debian, no remaining changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
**
3
 
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4
 
** All rights reserved.
5
 
** Contact: Nokia Corporation (qt-info@nokia.com)
6
 
**
7
 
** This file is part of the Qt Mobility Components.
8
 
**
9
 
** $QT_BEGIN_LICENSE:BSD$
10
 
** You may use this file under the terms of the BSD license as follows:
11
 
**
12
 
** "Redistribution and use in source and binary forms, with or without
13
 
** modification, are permitted provided that the following conditions are
14
 
** met:
15
 
**   * Redistributions of source code must retain the above copyright
16
 
**     notice, this list of conditions and the following disclaimer.
17
 
**   * Redistributions in binary form must reproduce the above copyright
18
 
**     notice, this list of conditions and the following disclaimer in
19
 
**     the documentation and/or other materials provided with the
20
 
**     distribution.
21
 
**   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22
 
**     the names of its contributors may be used to endorse or promote
23
 
**     products derived from this software without specific prior written
24
 
**     permission.
25
 
**
26
 
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
 
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28
 
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29
 
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30
 
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31
 
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32
 
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33
 
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34
 
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35
 
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
 
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37
 
** $QT_END_LICENSE$
38
 
**
39
 
****************************************************************************/
40
 
 
41
 
#include <qorganizeritemdetails.h>
42
 
#include <qorganizeritemrequests.h>
43
 
#include "qmlorganizermodel.h"
44
 
#include "qmlorganizeritem.h"
45
 
#include <QDebug>
46
 
#include <QSharedPointer>
47
 
 
48
 
QMLOrganizerModel::QMLOrganizerModel(QObject *parent)
49
 
    :QAbstractListModel(parent),
50
 
    m_manager(0),
51
 
    m_start(QDateTime::currentDateTime()),
52
 
    m_end(QDateTime::currentDateTime())
53
 
{
54
 
 
55
 
}
56
 
 
57
 
QMLOrganizerModel::QMLOrganizerModel(QOrganizerItemManager* manager, const QDateTime& start, const QDateTime& end, QObject *parent) :
58
 
    QAbstractListModel(parent),
59
 
    m_start(start),
60
 
    m_end(end)
61
 
{
62
 
    QHash<int, QByteArray> roleNames;
63
 
    roleNames = QAbstractItemModel::roleNames();
64
 
    roleNames.insert(OrganizerItemIdRole, "itemId");
65
 
    roleNames.insert(OrganizerItemRole, "item");
66
 
    setRoleNames(roleNames);
67
 
 
68
 
 
69
 
    connect(&m_request, SIGNAL(resultsAvailable()), this, SLOT(itemsReceived()));
70
 
 
71
 
    m_filter.setStartPeriod(start);
72
 
    m_filter.setEndPeriod(end);
73
 
    m_request.setFilter(m_filter);
74
 
 
75
 
    setManager(manager);
76
 
}
77
 
 
78
 
QString QMLOrganizerModel::manager()
79
 
{
80
 
    if (m_manager)
81
 
        return m_manager->managerName();
82
 
    return QString();
83
 
}
84
 
 
85
 
QDateTime QMLOrganizerModel::startPeriod() const
86
 
{
87
 
    return m_start;
88
 
}
89
 
 
90
 
QDateTime QMLOrganizerModel::endPeriod() const
91
 
{
92
 
    return m_end;
93
 
}
94
 
 
95
 
void QMLOrganizerModel::setStartPeriod(const QDateTime& start)
96
 
{
97
 
    m_start = start;
98
 
    m_filter.setStartPeriod(m_start);
99
 
    m_request.setFilter(m_filter);
100
 
}
101
 
 
102
 
void QMLOrganizerModel::setEndPeriod(const QDateTime& end)
103
 
{
104
 
    m_end = end;
105
 
    m_filter.setEndPeriod(m_end);
106
 
    m_request.setFilter(m_filter);
107
 
}
108
 
 
109
 
 
110
 
int QMLOrganizerModel::rowCount(const QModelIndex &parent) const
111
 
{
112
 
    Q_UNUSED(parent);
113
 
    return m_items.count();
114
 
}
115
 
 
116
 
void QMLOrganizerModel::setManager(QOrganizerItemManager* manager)
117
 
{
118
 
    disconnect(this, SLOT(reloadData()));
119
 
    m_manager = manager;
120
 
    connect(m_manager, SIGNAL(dataChanged()), this, SLOT(reloadData()));
121
 
    m_request.setManager(m_manager);
122
 
    reloadData();
123
 
}
124
 
 
125
 
void QMLOrganizerModel::reloadData()
126
 
{
127
 
    m_request.start();
128
 
}
129
 
 
130
 
void QMLOrganizerModel::itemsReceived()
131
 
{
132
 
    reset();
133
 
    foreach (QMLOrganizerItem* qitem, m_items) {
134
 
       delete qitem;
135
 
    }
136
 
    m_items.clear();
137
 
 
138
 
    QList<QOrganizerItem> items = m_request.items();
139
 
    beginInsertRows(QModelIndex(), 0, items.count());
140
 
    foreach (const QOrganizerItem& item, items) {
141
 
        QMLOrganizerItem* qitem = new QMLOrganizerItem(this);
142
 
        qitem->setItem(item);
143
 
        m_items.append(qitem);
144
 
    }
145
 
    endInsertRows();
146
 
}
147
 
 
148
 
 
149
 
 
150
 
QVariant QMLOrganizerModel::data(const QModelIndex &index, int role) const
151
 
{
152
 
    QMLOrganizerItem* item  = m_items.at(index.row());
153
 
    switch(role) {
154
 
        case OrganizerItemRole:
155
 
            return QVariant::fromValue(item);
156
 
    }
157
 
    return QVariant();
158
 
}
159