summaryrefslogtreecommitdiffstats
path: root/src/messaging/qmtmengine_symbian_p.h
blob: cb82ad1d5dec10d3dbd9935046cba6da847c2a33 (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
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
/****************************************************************************
**
** 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$
**
****************************************************************************/

#ifndef QMTMENGINE_SYMBIAN_H
#define QMTMENGINE_SYMBIAN_H

#include <QMap>
#include <QSet>
#include <QList>
#include <QHash>

#include <e32base.h>
#include <msvapi.h> // MMsvSessionObserver
#include "qmessagemanager.h"
#include "qmessagestore_symbian_p.h"
#include "qmessagefilter.h"
#include "qmessagefilter_p.h"
#include "qmessagefolderfilter.h"
#include "qmessageservice.h"
#ifdef QTHIGHWAYUSED
#include <xqappmgr.h>
#endif

class CRichText;
class CCharFormatLayer;
class CParaFormatLayer;
class CMsvSession;
class CClientMtmRegistry;
class CSmsClientMtm;
class CMmsClientMtm;
class CSmtpClientMtm;
class CImap4ClientMtm;
class CPop3ClientMtm;
class CMsvFindOperation;


QTM_BEGIN_NAMESPACE

class CMessagesFindOperation;
class QMessageId;
class QMessageAccount;
class CAsynchronousMTMOperation;

struct MessageEvent
{
    int sessionEvent;
    TUid msgType;
    QMessageStorePrivate::NotificationType notificationType;
    TMsvId folderId;
    TMsvId messageId;
    QMessageManager::NotificationFilterIdSet matchingFilters;
    bool unfiltered;
};

struct PendingSend
{
    TMsvId messageId;
    QMessageServicePrivate* privateService;
};

struct MessageQueryInfo
{
    int operationId;
    bool isQuery;
    QString body;
    QMessageDataComparator::MatchFlags matchFlags;
    QMessageFilter filter;
    QMessageSortOrder sortOrder;
    int offset;
    int limit;
    QMessageServicePrivate* privateService;
    CMessagesFindOperation* findOperation;
    int currentFilterListIndex;
    QMessageIdList ids;
    int count;
    bool canceled;
};

class CMTMEngine : public QObject, public CActive, public MMsvSessionObserver
{
    Q_OBJECT

public:
    enum MTMType
    {
        MTMTypeSMS,
        MTMTypeMMS,
        MTMTypeSMTP,
        MTMTypeIMAP,
        MTMTypePOP3
    };    
    
    static CMTMEngine* instance();

    CMTMEngine();
    ~CMTMEngine();
    
    QMessageAccountIdList queryAccounts(const QMessageAccountFilter &filter, const QMessageAccountSortOrder &sortOrder, uint limit, uint offset) const;
    int countAccounts(const QMessageAccountFilter &filter) const;
    QMessageAccount account(const QMessageAccountId &id) const;
    QMessageAccountId defaultAccount(QMessage::Type type) const;
    
    QMessageFolderIdList queryFolders(const QMessageFolderFilter &filter, const QMessageFolderSortOrder &sortOrder, uint limit, uint offset) const;
    int countFolders(const QMessageFolderFilter &filter) const;
    QMessageFolder folder(const QMessageFolderId &id) const;
    
    bool addMessage(QMessage *m);
    bool updateMessage(QMessage *m);
    bool removeMessage(const QMessageId &id, QMessageManager::RemovalOption option);
    bool removeMessages(const QMessageFilter &filter, QMessageManager::RemovalOption option);
    bool queryMessages(QMessageServicePrivate& privateService, const QMessageFilter &filter, const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
    bool queryMessages(QMessageServicePrivate& privateService, const QMessageFilter &filter, const QString &body, QMessageDataComparator::MatchFlags matchFlags, const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
    bool countMessages(QMessageServicePrivate& privateService, const QMessageFilter &filter);
    bool showMessage(const QMessageId &id);
    bool composeMessage(const QMessage &message);
    
    QMessage message(const QMessageId& id) const;
    
    bool storeMMS(QMessage &message);
    bool sendMMS(QMessageServicePrivate& privateService, QMessage &message);
    bool storeEmail(QMessage &message);
    bool sendEmail(QMessageServicePrivate& privateService, QMessage &message);
    bool storeSMS(QMessage &message);
    bool sendSMS(QMessageServicePrivate& privateService, QMessage &message);
    bool retrieve(QMessageServicePrivate& privateService, const QMessageId &messageId, const QMessageContentContainerId& id);
    bool retrieveBody(QMessageServicePrivate& privateService, const QMessageId& id);
    bool retrieveHeader(QMessageServicePrivate& privateService, const QMessageId& id);
    bool exportUpdates(QMessageServicePrivate& privateService, const QMessageAccountId &id);
    
    QByteArray attachmentContent(long int messageId, unsigned int attachmentId);
    QString attachmentTextContent(long int messageId, unsigned int attachmentId, const QByteArray &charset);
    
    QMessageManager::NotificationFilterId registerNotificationFilter(QMessageStorePrivate& aPrivateStore,
                                        const QMessageFilter& filter);
    void unregisterNotificationFilter(QMessageManager::NotificationFilterId notificationFilterId);

    bool handleMessageSendingNotifications(TMsvSessionEvent aEvent, TUid aMsgType, TMsvId aFolderId,
                                           TMsvId aMessageId, TUint sendingState);
    void notification(TMsvSessionEvent aEvent, TUid aMsgType, TMsvId aFolderId, TMsvId aMessageId, CMsvEntry* pEntry);
    void filterAndOrderMessagesReady(bool success, int operationId, QMessageIdList ids, int numberOfHandledFilters,
                                     bool resultSetOrdered);
    
    void cancel(QMessageServicePrivate& privateService);

    inline RFs& FsSession() const { return((RFs&)iFsSession); }

public slots:
    void cleanupMTMBackend();
    
private:
    void updateEmailAccountsL() const;
    bool switchToMTMRootEntry(MTMType aMTMType);
    TMsvId mtmServiceEntryIdL(MTMType aMTMType) const;
    CBaseMtm* mtmByType(MTMType aMTMType);
    CBaseMtm* mtmByUid(TUid aMTMUid);
    TUid mtmUidByType(MTMType aMTMType);
    TMsvId standardFolderId(QMessage::StandardFolder standardFolder);
    
    QMessageFolderIdList allFolders() const;
    QMessageFolderIdList folderIdsByAccountId(const QMessageAccountId& accountId) const;
    QMessageFolderIdList folderIdsByServiceEntryId(const TMsvId& serviceEntryId, const TMsvId& folderServiceEntryId) const;
    QMessageFolderId createQMessageFolderId(const TMsvId& serviceEntryId, const TMsvId& folderId) const;
    TMsvId serviceEntryIdFromQMessageFolderId(const QMessageFolderId& folderId) const;
    TMsvId folderIdFromQMessageFolderId(const QMessageFolderId& folderId) const;
    
    QMessageAccountIdList accountsByType(QMessage::Type type) const;
    QMessageAccountId accountIdByServiceId(TMsvId serviceId) const;
    
    QMessageFolder folderL(const QMessageFolderId &id) const;

    static bool accountLessThan(const QMessageAccountId accountId1, const QMessageAccountId accountId2);
    void orderAccounts(QMessageAccountIdList& accountIds,  const QMessageAccountSortOrder &sortOrder) const;
    void applyOffsetAndLimitToAccountIds(QMessageAccountIdList& idList, int offset, int limit) const;
    static bool folderLessThan(const QMessageFolderId folderId1, const QMessageFolderId folderId2);
    void orderFolders(QMessageFolderIdList& folderIds,  const QMessageFolderSortOrder &sortOrder) const;
    static bool messageLessThan(const QMessage& message1, const QMessage& message2);
    void orderMessages(QMessageIdList& messageIds,  const QMessageSortOrder &sortOrder) const;
    
    void handleNestedFiltersFromFolderFilter(QMessageFolderFilter &filter) const;
    void handleNestedFiltersFromMessageFilter(QMessageFilter &filter) const;

    void queryMessagesL(QMessageServicePrivate& privateService, const QMessageFilter &filter, const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
    void queryMessagesL(QMessageServicePrivate& privateService, const QMessageFilter &filter, const QString &body, QMessageDataComparator::MatchFlags matchFlags, const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
    void countMessagesL(QMessageServicePrivate& privateService, const QMessageFilter &filter);
    void applyOffsetAndLimitToMsgIds(QMessageIdList& idList, int offset, int limit) const;
    
    QMessageFolderIdList filterMessageFolders(const QMessageFolderFilter& filter, bool& filterHandled) const;
    QMessageFolderIdList filterMessageFoldersL(const QMessageFolderFilter& filter, bool& filterHandled) const;
    void applyOffsetAndLimitToMsgFolderIds(QMessageFolderIdList& idList, int offset, int limit) const;
    
    bool removeMessageL(const QMessageId &id, QMessageManager::RemovalOption option);
    void copyMessageL(TMsvId aMessageId, TMsvId aFolder);
    QMessage messageL(const QMessageId& id, bool onlyBasicHeaderFields = false) const;
    QMessage smsMessageL(CMsvEntry& receivedEntry, long int messageId, bool onlyBasicHeaderFields = false) const;
    QMessage mmsMessageL(CMsvEntry& receivedEntry, long int messageId, bool onlyBasicHeaderFields = false) const;
    QMessage emailMessageL(CMsvEntry& receivedEntry, long int messageId, bool onlyBasicHeaderFields = false) const;
    bool composeSMSL(const QMessage &message);
    bool composeMMSL(const QMessage &message);
    bool composeEmailL(const QMessage &message);
    void tryToDeliverMessageNotifications();
    void updateMMSL(QMessage &message);
    void updateSMSL(QMessage &message);
    void updateEmailL(QMessage &message);
    void showMessageL(const QMessageId &id);
    
    void storeMMSL(QMessage &message);
    void sendMMSL(QMessageServicePrivate& privateService, QMessage &message);
    void storeEmailL(QMessage &message);
    void sendEmailL(QMessageServicePrivate& privateService, QMessage &message);
    void storeSMSL(QMessage &message);
    bool validateSMS();
    void sendSMSL(QMessageServicePrivate& privateService, QMessage &message);
    void retrieveL(QMessageServicePrivate& privateService, const QMessageId &messageId, const QMessageContentContainerId& id);
    void retrieveBodyL(QMessageServicePrivate& privateService, const QMessageId& id);
    void retrieveHeaderL(QMessageServicePrivate& privateService, const QMessageId& id);
    void exportUpdatesL(QMessageServicePrivate& privateService, const QMessageAccountId &id);
    void appendAttachmentToMessage(QMessage& message, QMessageContentContainer& attachment) const;
    QByteArray attachmentContentL(long int messageId, unsigned int attachmentId);
    
    QString privateFolderPath();
    
    QDateTime symbianTTimetoQDateTime(const TTime& time) const;
    TTime qDateTimeToSymbianTTime(const QDateTime& date) const;
    
    static void cmsvEntryCleanup(TAny* aCMsvEntry);    
    CMsvEntry* retrieveCMsvEntryAndPushToCleanupStack(TMsvId id = 0) const;
    void releaseCMsvEntryAndPopFromCleanupStack(CMsvEntry* pEntry) const;
    
    CAsynchronousMTMOperation* createAsynchronousMTMOperation(QMessageServicePrivate& privateService,
                                                              CBaseMtm* mtm,
                                                              TMsvId serviceId);
    void deleteAsynchronousMTMOperation(CAsynchronousMTMOperation *apOperation);
    bool checkIfWaitingDiscardClearMessage(TMsvId aMessageId);
    
    bool isGsm0338CompatibleUnicodeCharacter(TUint16 aValue);

private: // from CActive
    void RunL();
    void DoCancel();

private: //from MMsvSessionObserver:
    void HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1,
                             TAny* aArg2, TAny* aArg3);

private:
    RFs iFsSession;
    QMessageStorePrivate* ipMessageStorePrivate;
    
    bool iSessionReady;
    bool iListenForNotifications;

    mutable CRichText*        ipRichText;
    mutable CCharFormatLayer* ipCharFormatLayer;
    mutable CParaFormatLayer* ipParaFormatLayer;

    MTMType             iMTMType;       
    
    CMsvSession*        ipMsvSession;
    CClientMtmRegistry* ipClientMtmReg;
    CSmsClientMtm*      ipSmsMtm;
    CMmsClientMtm*      ipMmsMtm;
    CSmtpClientMtm*     ipSmtpMtm;
    CImap4ClientMtm*    ipImap4Mtm;
    CPop3ClientMtm*     ipPop3Mtm;
    TBuf<KMaxPath>      iPath;
    
    QString iSMSAccountidAsString;
    QString iMMSAccountidAsString;

    mutable RPointerArray<CMsvEntry> iCmsvEntryPoolFree;
    mutable RPointerArray<CMsvEntry> iCmsvEntryPoolInUse;
    
    CMsvFindOperation*  ipFindOperation;
    
    QMessageManager::NotificationFilterId _filterId;
    QMap<QMessageManager::NotificationFilterId, QMessageFilter> _filters;
    bool iBasicFieldsAreEnoughForFiltering;
    
    mutable QHash<QString, QMessageAccount> iAccounts;
    mutable QMessageAccountId idefaultEmailAccountId;
    
    int                 iDeliveryTriesCounter;
    QList<MessageEvent> iUndeliveredMessageEvents;
    QList<MessageEvent> iPendingMessageEvents;
    RTimer              iTimer; // Timer used for delaying delivering of received
                                // messages until messages are ready to be read
    QMap<TMsvId, TMsvSessionEvent>  iMessagesInPreparation;
    TMsvId iMessageId;
    bool iNewMessage;
    
    mutable int iOperationIds;
    mutable QList<MessageQueryInfo> iMessageQueries;
    mutable QMap<QMessageServicePrivate*, CAsynchronousMTMOperation*> m_mtmOperations;
    mutable QList<PendingSend> iPendingSends;
    
    mutable QMessageAccountSortOrder iCurrentAccountOrdering;
    mutable QMessageFolderSortOrder iCurrentFolderOrdering;
    mutable QMessageSortOrder iCurrentMessageOrdering;
    
#ifdef QTHIGHWAYUSED    
    XQApplicationManager iAiwMgr;
#endif
    
    friend class QMessageService;
    friend class CMessagesFindOperation;
    friend class CAsynchronousMTMOperation;
};

class CMessagesFindOperation : public CActive
{
public:
    CMessagesFindOperation(CMTMEngine& aOwner, CMsvSession* apMsvSession, int aOperationId); 
    ~CMessagesFindOperation();

    void filterAndOrderMessages(const QMessageFilter& filter,
                                const QMessageSortOrder& sortOrder,
                                const QString body = QString(),
                                QMessageDataComparator::MatchFlags matchFlags = 0);
    void filterAndOrderMessages(const QMessageFilterPrivate::SortedMessageFilterList& filters,
                                const QMessageSortOrder& sortOrder,
                                const QString body = QString(),
                                QMessageDataComparator::MatchFlags matchFlags = 0);
    
protected: // From CActive
    void RunL();
    void DoCancel();
    
private:
    void getAllMessagesL(const TMsvSelectionOrdering sortOrder = TMsvSelectionOrdering());
    void getAccountSpecificMessagesL(QMessageAccount& messageAccount, const TMsvSelectionOrdering sortOrder, QMessageFilterPrivate* privateFolderFilter = NULL);
    void getServiceSpecificMessagesL(TMsvId serviceId, const TMsvSelectionOrdering sortOrder, QMessageFilterPrivate* privateFolderFilter = NULL);
    void getServiceSpecificMessagesFromFolderL(TMsvId serviceId, const TMsvSelectionOrdering sortOrder, TMsvId standardFolderId = NULL);

private: // Data
    CMTMEngine& iOwner;
    CMsvSession* ipMsvSession;
    
    int iNumberOfHandledFilters;
    int iOperationId;
    TMsvSelectionOrdering iOrdering;
    bool iResultCorrectlyOrdered;
    QMessageIdList iIdList;
    QMessageFilterPrivate::SortedMessageFilterList iFilterList;
    
    CMsvEntrySelection* ipEntrySelection;
    CMsvFindOperation* ipMsvFindOperation;
    
    mutable RTimer iTimer;
};

class QMTMWait : public CActive
{
public:
    QMTMWait(TInt aPriority = EPriorityStandard);
    ~QMTMWait();

    void start();

protected: // From CActive
    void RunL();
    void DoCancel();
    
private: // Data
    QEventLoop m_eventLoop;
};

class CAsynchronousMTMOperation : public CActive
{
public:
    enum MTMOperation
    {
        MTMOperationRetrieveMessageHeader,
        MTMOperationRetrieveMessageBody,
        MTMOperationRetrieveMessageAttachments,
        MTMOperationFullSync,
    };    

    enum MTMOperationStep
    {
        MTMOperationStepConnect,
        MTMOperationStepDoOperation,
        MTMOperationStepDisconnect,
        MTMOperationStepFinished
    };    
    
    CAsynchronousMTMOperation(CMTMEngine& aParent,
                              QMessageServicePrivate& privateService,
                              CBaseMtm* mtm,
                              TMsvId serviceId,
                              TInt aOperationId);
    ~CAsynchronousMTMOperation();

    bool retrieveMessageHeader(TMsvId aMessageId);
    bool retrieveMessageBody(TMsvId aMessageId);
    bool retrieveMessageAttachments(TMsvId aMessageId);
    bool doFullSync();

protected: // From CActive
    void RunL();
    void DoCancel();
    
private: // Data
    CMTMEngine* ipParent;

    TInt iOperationId;
    TMsvId iServiceId;
    TMsvId iMessageId;
    QMessageServicePrivate* ipPrivateService;
    
    CBaseMtm*           ipMTM;
    MTMOperation        iOperation;
    MTMOperationStep    iOperationStep;
    
    CMsvEntrySelection* ipMsvEntrySelection;
    CMsvOperation*      ipMsvOperation;
    
    bool                isActive;

    friend class CMTMEngine;
};

QTM_END_NAMESPACE
#endif // QMTMENGINE_SYMBIAN_H