summaryrefslogtreecommitdiffstats
path: root/plugins/organizer/symbian/organizersymbianutils.cpp
blob: 1d99b697920f6828fb5b765b0821a52c7704afab (plain)
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
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "organizersymbianutils.h"
#include <badesca.h>
#include <QColor>

namespace OrganizerSymbianUtils
{

QString toQString(const TDesC8 &des)
{
    return QString::fromUtf8((const char *)des.Ptr(), des.Length());
}

QString toQString(const TDesC16 &des)
{
    return QString::fromUtf16(des.Ptr(), des.Length());
}

TPtrC8 toPtrC8(const QByteArray &bytes)
{
    return TPtrC8(reinterpret_cast<const TUint8*>(bytes.constData()), bytes.size());
}

TPtrC16 toPtrC16(const QString &string)
{
    return TPtrC16(reinterpret_cast<const TUint16*>(string.utf16()), string.size());
}

TCalTime toTCalTimeL(const QDateTime &dateTime)
{
    TCalTime calTime;
    calTime.SetTimeUtcL(Time::NullTTime());
    if (dateTime.isValid())      
        calTime.SetTimeLocalL(toTTime(dateTime, Qt::LocalTime));
    return calTime;
}

TCalTime toTCalTimeL(const QDate &date)
{
    TCalTime calTime;
    calTime.SetTimeUtcL(Time::NullTTime());
    if (date.isValid())
        calTime = toTCalTimeL(QDateTime(date));
    return calTime;
}

QDateTime toQDateTimeL(const TCalTime &calTime)
{
    // NOTE: 
    // The calendar/agenda server has a problem with UTC times. It will move
    // the UTC time with daylight saving time. UTC time should be fixed 
    // always and the actual local time for a calendar entry should be 
    // calculated from the UTC time. Fortunately local time's reported
    // from the server are correct and using it with TCalTime/QDateTime
    // conversions seems work as as workaround for this issue.
    return toQDateTime(calTime.TimeLocalL(), Qt::LocalTime);
}

TTime toTTime(const QDateTime &dateTime, Qt::TimeSpec timeSpec)
{
    TTime time = Time::NullTTime();
    if (dateTime.isValid()) {
        QDateTime qdt = dateTime.toTimeSpec(timeSpec);
        // NOTE: TDateTime month & day start from zero
        TDateTime tdt(qdt.date().year(), TMonth(qdt.date().month()-1), qdt.date().day()-1,
            qdt.time().hour(), qdt.time().minute(), qdt.time().second(), qdt.time().msec()*1000);
        time = TTime(tdt);
    }
    return time;
}

QDateTime toQDateTime(const TTime &time, Qt::TimeSpec timeSpec)
{
    QDateTime qdt;
    if (time != Time::NullTTime()) {
        TDateTime tdt = time.DateTime();
        qdt.setTimeSpec(timeSpec);
        // NOTE: TDateTime month & day start from zero
        qdt.setDate(QDate(tdt.Year(), tdt.Month()+1, tdt.Day()+1));
        qdt.setTime(QTime(tdt.Hour(), tdt.Minute(), tdt.Second(), tdt.MicroSecond()/1000));
    }
    return qdt.toTimeSpec(Qt::LocalTime); // return with default timespec set
}

QOrganizerCollectionId toCollectionId(quint64 collectionId)
{
    return QOrganizerCollectionId(new QOrganizerCollectionSymbianEngineId(collectionId));
}

QOrganizerItemId toItemId(quint64 collectionId, quint32 itemId)
{
    return QOrganizerItemId(new QOrganizerItemSymbianEngineId(collectionId, itemId));
}

TCalLocalUid toTCalLocalUid(const QOrganizerItemId& itemId)
{
    // TODO: should we have a check for engineLocalIdType here?
    return static_cast<const QOrganizerItemSymbianEngineId*>(QOrganizerManagerEngine::engineItemId(itemId))->calLocalUid();
}

quint64 toTCalCollectionId(const QOrganizerItemId& itemId)
{
    // TODO: should we have a check for engineLocalIdType here?
    return static_cast<const QOrganizerItemSymbianEngineId*>(QOrganizerManagerEngine::engineItemId(itemId))->calCollectionId();
}

QOrganizerCollectionId getCollectionId(const QOrganizerItemId& itemId)
{
    // TODO: should we have a check for engineLocalIdType here?
    quint64 calCollectionId  = static_cast<const QOrganizerItemSymbianEngineId*>(QOrganizerManagerEngine::engineItemId(itemId))->calCollectionId();
    return toCollectionId(calCollectionId);
}

#ifdef SYMBIAN_CALENDAR_V2

void setCalInfoPropertyL(CCalCalendarInfo *calInfo, TCalenPropertyUid propertyUid, const QString &value)
{    
    TBuf8<KPropertyKeyLen> keyBuff;
    keyBuff.AppendNum(propertyUid);
    QByteArray bytes = value.toUtf8();
    TPtrC8 des = toPtrC8(bytes);
    calInfo->SetPropertyL(keyBuff, des);
}

QVariantMap toMetaDataL(const CCalCalendarInfo &calInfo)
{
    QVariantMap metaData;
    
    metaData.insert(OrganizerSymbianCollection::KeyIsValid, (bool) calInfo.IsValid());
    metaData.insert(QOrganizerCollection::KeyName, toQString(calInfo.NameL()));
    metaData.insert(OrganizerSymbianCollection::KeyFileName, toQString(calInfo.FileNameL()));
    metaData.insert(QOrganizerCollection::KeyDescription, toQString(calInfo.DescriptionL()));
    TRgb color = calInfo.Color();
    QColor qcolor(color.Red(), color.Green(), color.Blue(), color.Alpha());
    metaData.insert(QOrganizerCollection::KeyColor, qcolor);
    metaData.insert(OrganizerSymbianCollection::KeyEnabled, (bool) calInfo.Enabled());
    
    CDesC8Array* keys = calInfo.PropertyKeysL();
    CleanupStack::PushL(keys);
    for (int i=0; i<keys->Count(); i++) {
        
        // Get key and value 
        QString propKey = toQString(keys->MdcaPoint(i));
        TPtrC8 propValue = calInfo.PropertyValueL(keys->MdcaPoint(i));
           
        // Try converting the key to int
        bool ok = false;
        int calenPropertyUid = propKey.toInt(&ok);
        if (!ok) {
            // Default conversion to byte array
            QByteArray value((const char*) propValue.Ptr(), propValue.Size());
            metaData.insert(propKey, value);
            continue;
        }

        // Convert the predefined properties
        if (calenPropertyUid == EFolderLUID) {
            TPckgBuf<TUint> value;
            value.Copy(propValue);
            metaData.insert(OrganizerSymbianCollection::KeyFolderLUID, (uint) value());
        } else if (calenPropertyUid == ECreationTime) {
            TPckgBuf<TTime> value;
            value.Copy(propValue);
            metaData.insert(OrganizerSymbianCollection::KeyCreationTime, toQDateTime(value(), Qt::UTC));
        } else if (calenPropertyUid == EModificationTime) {
            TPckgBuf<TTime> value;
            value.Copy(propValue);
            metaData.insert(OrganizerSymbianCollection::KeyModificationTime, toQDateTime(value(), Qt::UTC));
        } else if (calenPropertyUid == ESyncStatus) {
            TPckgBuf<TBool> value;
            value.Copy(propValue);
            metaData.insert(OrganizerSymbianCollection::KeySyncStatus, (bool) value());
        } else if (calenPropertyUid == EIsSharedFolder) {
            TPckgBuf<TBool> value;
            value.Copy(propValue);
            metaData.insert(OrganizerSymbianCollection::KeyIsSharedFolder, (bool) value());
        } else if (calenPropertyUid == EGlobalUUID) {
            metaData.insert(OrganizerSymbianCollection::KeyGlobalUUID, toQString(propValue));
        } else if (calenPropertyUid == EDeviceSyncServiceOwner) {
            TPckgBuf<TUint> value;
            value.Copy(propValue);
            metaData.insert(OrganizerSymbianCollection::KeyDeviceSyncServiceOwner, (uint) value());
        } else if (calenPropertyUid == EOwnerName) {
            metaData.insert(OrganizerSymbianCollection::KeyOwnerName, toQString(propValue));
        } else if (calenPropertyUid == EMarkAsDelete) {
            // Hide this from client
        } else {
            // Default conversion for unknown property
            QByteArray value((const char*) propValue.Ptr(), propValue.Size());
            metaData.insert(propKey, value);
        }
        // TODO: EDeviceSyncProfileID can't find any reference of the type.. uint?
        // TODO: ESyncConfigEnabled can't find any reference of the type.. bool?
    }
    
    CleanupStack::PopAndDestroy(keys);     
    
    return metaData;
}

CCalCalendarInfo* toCalInfoLC(QVariantMap metaData)
{
    // Create a new calendar info
    CCalCalendarInfo* calInfo = CCalCalendarInfo::NewL();
    CleanupStack::PushL(calInfo);
    
    // Is valid
    // NOTE: calendar server controls this
    metaData.remove(OrganizerSymbianCollection::KeyIsValid);
    
    // Filename
    // NOTE: filename is set only when creating a new calendar...
    metaData.remove(OrganizerSymbianCollection::KeyFileName);
    
    // Name
    QString name = metaData.value(QOrganizerCollection::KeyName).toString();
    metaData.remove(QOrganizerCollection::KeyName);
    if (!name.isEmpty())
        calInfo->SetNameL(toPtrC16(name));
    
    // Description
    QString description = metaData.value(QOrganizerCollection::KeyDescription).toString();
    metaData.remove(QOrganizerCollection::KeyDescription);
    if (!description.isEmpty())
        calInfo->SetDescriptionL(toPtrC16(description));
    
    // Color
    if (metaData.keys().contains(QOrganizerCollection::KeyColor)) {
        QColor qcolor = metaData.value(QOrganizerCollection::KeyColor).value<QColor>();
        TRgb color(qcolor.red(), qcolor.green(), qcolor.blue(), qcolor.alpha());
        calInfo->SetColor(color);
        metaData.remove(QOrganizerCollection::KeyColor);
    }
    
    // Enabled
    if (metaData.keys().contains(OrganizerSymbianCollection::KeyEnabled)) {
        calInfo->SetEnabled(metaData.value(OrganizerSymbianCollection::KeyEnabled).toBool());
        metaData.remove(OrganizerSymbianCollection::KeyEnabled);
    }
        
    // Set remaining metadata as properties
    foreach (QString key, metaData.keys()) {
        
        QVariant value = metaData.value(key);
        
        // Set known properties by converting to correct type
        if (key == OrganizerSymbianCollection::KeyFolderLUID) {
            setCalInfoPropertyL(calInfo, EFolderLUID, (TUint) value.toUInt());
        } else if (key == OrganizerSymbianCollection::KeyCreationTime) {
            // Don't allow client to set this.
        } else if (key == OrganizerSymbianCollection::KeyModificationTime) {
            // Don't allow client to set this.
        } else if (key == OrganizerSymbianCollection::KeySyncStatus) {
            setCalInfoPropertyL(calInfo, ESyncStatus, (TBool) value.toBool());
        } else if (key == OrganizerSymbianCollection::KeyIsSharedFolder) {
            setCalInfoPropertyL(calInfo, EIsSharedFolder, (TBool) value.toBool());
        } else if (key == OrganizerSymbianCollection::KeyGlobalUUID) {
            setCalInfoPropertyL(calInfo, EGlobalUUID, value.toString());
        } else if (key == OrganizerSymbianCollection::KeyDeviceSyncServiceOwner) {
            setCalInfoPropertyL(calInfo, EDeviceSyncServiceOwner, (TUint) value.toUInt());
        } else if (key == OrganizerSymbianCollection::KeyOwnerName) {
            setCalInfoPropertyL(calInfo, EOwnerName, value.toString());
        } else if (key == OrganizerSymbianCollection::KeyMarkAsDelete) {
            // Don't allow client to change this. This flag should be hidden from client.
        } else {
            // Default conversion for unknown property
            QByteArray keyBytes = key.toUtf8();
            TPtrC8 propName = toPtrC8(keyBytes);
            QByteArray valueBytes = metaData.value(key).toByteArray();
            TPtrC8 propValue = toPtrC8(valueBytes);
            calInfo->SetPropertyL(propName, propValue);
        }
        // TODO: EDeviceSyncProfileID can't find any reference of the type.. uint?
        // TODO: ESyncConfigEnabled can't find any reference of the type.. bool?
    }    
    return calInfo;
}
#endif // SYMBIAN_CALENDAR_V2

} // namespace OrganizerSymbianUtils