summaryrefslogtreecommitdiffstats
path: root/src/messaging/modestengine_maemo_p.h
blob: 24bda6c2de8da072e473648644b0c3280e89de4b (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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/****************************************************************************
**
** 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$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, 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, Digia gives you certain additional
** rights.  These rights are described in the Digia 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.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef MODESTENGINE_MAEMO_H
#define MODESTENGINE_MAEMO_H

#include "qmessagemanager.h"
#include "qmessagefilter_p.h"
#include "qmessageservice.h"

#include "gconf/gconf-client.h"
#include "libosso.h"

#include <QMap>
#include <QString>
#include <QDBusArgument>
#include <QDBusPendingCallWatcher>
#include <QFileInfoList>
#include <QThread>
#include <QMutex>
#include <QEventLoop>

class QDBusInterface;
class QFileSystemWatcher;
class QEventLoop;

QTM_BEGIN_NAMESPACE

typedef QMap< QString, QString > ModestStringMap;
typedef QList< ModestStringMap > ModestStringMapList;

static const int maxCacheSize = 100000;

class QMessageService;
class QMessageServicePrivate;
class QMessageStorePrivate;
class MessageQueryThread;

struct MessageQueryInfo
{
    int queryId;
    QString body;
    QMessageDataComparator::MatchFlags matchFlags;
    QMessageFilter filter;
    QMessageSortOrder sortOrder;
    int limit;
    int offset;
    QMessageServicePrivate* privateService;
    QDBusPendingCallWatcher* pendingCallWatcher;
    int currentFilterListIndex;
    int handledFiltersCount;
    QMessageIdList ids;
    QString realAccountId;
    bool isQuery;
    bool returnWithSingleShot;
    bool allMessagesQuery;
    MessageQueryThread *queryThread;
};

struct ModestUnreadMessageDBusStruct
{
    qlonglong timeStamp;
    QString subject;
};

struct ModestAccountsUnreadMessagesDBusStruct
{
    QString accountId;
    QString accountName;
    QString accountProtocol;
    qlonglong unreadCount;
    QList<ModestUnreadMessageDBusStruct> unreadMessages;
};

struct ModestMessage
{
    QString id;
    QString subject;
    QString folder;
    QString sender;
    long long size;
    bool hasAttachment;
    bool isUnread;
    long long timeStamp;
};

typedef enum {
    MessagingModestFolderTypeUnknown,
    MessagingModestFolderTypeNormal,
    MessagingModestFolderTypeInbox,
    MessagingModestFolderTypeOutbox,
    MessagingModestFolderTypeTrash,
    MessagingModestFolderTypeJunk,
    MessagingModestFolderTypeSent,
    MessagingModestFolderTypeRoot,
    MessagingModestFolderTypeNotes,
    MessagingModestFolderTypeDrafts,
    MessagingModestFolderTypeContacts,
    MessagingModestFolderTypeCalendar,
    MessagingModestFolderTypeArchive,
    MessagingModestFolderTypeMerge,
    MessagingModestFolderTypeNum
} MessagingModestFolderType;

struct MessagingModestFolder
{
    QString id;
    QString modestId;
    QString parentAccountId;
    QString parentFolderId;
    MessagingModestFolderType type;
    QString name;
    QString localizedName;
};

typedef enum {
    MessagingModestMessageNotDefined  = 0,
    MessagingModestMessageAnswered    = 1<<0,
    MessagingModestMessageDeleted     = 1<<1,
    MessagingModestMessageDraft       = 1<<2,
    MessagingModestMessageFlagged     = 1<<3,
    MessagingModestMessageSeen        = 1<<4,
    MessagingModestMessageAttachments = 1<<5,
    MessagingModestMessageCached      = 1<<6,
    MessagingModestMessagePartial     = 1<<7,
    MessagingModestMessageExpunged    = 1<<8,
    MessagingModestMessageHasPriority = 1<<9|1<<10
} MessagingModestMessageFlags;

typedef enum {
    MessagingModestMessagePriorityNotDefined = 0,
    MessagingModestMessageHighPriority       = 0<<9|1<<10,
    MessagingModestMessageNormalPriority     = 0<<9|0<<10,
    MessagingModestMessageLowPriority        = 1<<9|0<<10,
} MessagingModestMessagePriority;

struct MessagingModestMimePart
{   
    QString mimeType;
    bool isAttachment;
    QString fileName;
    QString contentId;
};

struct MessagingModestMessage
{
    QString id;
    QString url;
    QString accountId;
    QString folderId;
    QString mimeType;
    QString from;
    QString to;
    QString cc;
    QString bcc;
    QString replyTo;
    QString subject;
    qlonglong dateReceived;
    qlonglong dateSent;
    qlonglong size;
    MessagingModestMessageFlags flags;
    MessagingModestMessagePriority priority;
    QList<MessagingModestMimePart> mimeParts;
};

struct INotifyEvent
{
    int watchDescriptor;
    uint32_t mask;
    QString fileName;
};

class INotifyWatcher : public QThread
{
    Q_OBJECT

public:
    enum FileNotification
    {
        FileNotificationAdded,
        FileNotificationUpdated,
        FileNotificationRemoved
    };

    INotifyWatcher();
    ~INotifyWatcher();

    void run();
    int addFile(const QString& path, uint eventsToWatch = 0);
    QStringList files() const;
    int addDirectory(const QString& path, uint eventsToWatch = 0);
    QStringList directories() const;
    void clear();

private slots:
    void notifySlot();

signals:
   void fileChanged(int watchDescriptor, QString filePath, uint events);

private: //Data
    int m_inotifyFileDescriptor;
    QMutex m_mutex;
    QMap<int, QString> m_files;
    QMap<int, QString> m_dirs;
};

class ModestEngine : public QObject
{
    Q_OBJECT

public:
    enum EmailProtocol
    {
        EmailProtocolUnknown = -1,
        EmailProtocolPop3 = 1,
        EmailProtocolIMAP,
    };

    enum NotificationType
    {
        None = 0,
        Added,
        Updated,
        Removed
    };

    static ModestEngine* instance();

    ModestEngine();
    ~ModestEngine();

    bool exportUpdates(const QMessageAccountId &id);

    QMessageAccountIdList queryAccounts(const QMessageAccountFilter &filter, const QMessageAccountSortOrder &sortOrder,
                                        uint limit, uint offset, bool &isFiltered, bool &isSorted) const;
    int countAccounts(const QMessageAccountFilter &filter) const;
    QMessageAccount account(const QMessageAccountId &id) const;
    QMessageAccountId defaultAccount() const;

    QMessageFolderIdList queryFolders(const QMessageFolderFilter &filter, const QMessageFolderSortOrder &sortOrder,
                                      uint limit, uint offset, bool &isFiltered, bool &isSorted) const;
    int countFolders(const QMessageFolderFilter &filter) const;
    QMessageFolder folder(const QMessageFolderId &id) const;

    QMessage message(const QMessageId &id, bool useCache = true) const;
    bool addMessage(QMessage &message);
    bool updateMessage(QMessage &message);
    bool removeMessage(const QMessageId &id, QMessageManager::RemovalOption option);

    bool queryMessages(QMessageService& messageService, const QMessageFilter &filter,
                       const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
    bool queryMessages(QMessageService& messageService, const QMessageFilter &filter,
                       const QString &body, QMessageDataComparator::MatchFlags matchFlags,
                       const QMessageSortOrder &sortOrder, uint limit, uint offset) const;
    bool countMessages(QMessageService& messageService, const QMessageFilter &filter);

    QMessageIdList queryMessagesSync(const QMessageFilter &filter, const QMessageSortOrder &sortOrder,
                                     uint limit, uint offset, bool &isFiltered, bool &isSorted) const;
    QMessageIdList queryMessagesSync(const QMessageFilter &filter, const QString &body,
                                     QMessageDataComparator::MatchFlags matchFlags,
                                     const QMessageSortOrder &sortOrder, uint limit, uint offset,
                                     bool &isFiltered, bool &isSorted) const;
    int countMessagesSync(const QMessageFilter &filter) const;

    bool sendEmail(QMessage &message);
    bool composeEmail(const QMessage &message);
    bool showMessage(const QMessageId &id);

    QMessageManager::NotificationFilterId registerNotificationFilter(QMessageStorePrivate& messageStore,
                                                                     const QMessageFilter& filter,
                                                                     QMessageManager::NotificationFilterId id = 0);
    void unregisterNotificationFilter(QMessageManager::NotificationFilterId notificationFilterId);
    QByteArray getMimePart (const QMessageId &id, const QString &attachmentId);

    bool retrieveBody(QMessageService& messageService, const QMessageId &id);
    bool retrieve(QMessageService& messageService, const QMessageId &messageId, const QMessageContentContainerId &id, QMessage *msg = 0);

    bool retrieveMessageMimeInformation(QMessage& message);

private:
    QFileInfoList localFolders() const;
    void appendLocalSubFolders(QFileInfoList& fileInfoList, int startIndex) const;
    void appendIMAPSubFolders(QFileInfoList& fileInfoList, int startIndex) const;
    QFileInfoList accountFolders(QMessageAccountId& accountId) const;
    QString localRootFolder() const;
    QString accountRootFolder(QMessageAccountId& accountId) const;
    void foldersFromModest(QList<MessagingModestFolder>& folders) const;
    bool startObservingModestFolder(const QMessageFolderId& folderId) const;
    bool stopObservingModestFolder(const QMessageFolderId& folderId) const;
    EmailProtocol accountEmailProtocol(QMessageAccountId& accountId) const;
    QString accountEmailProtocolAsString(const QMessageAccountId& accountId) const;
    QString accountUsername(QMessageAccountId& accountId) const;
    QString accountHostname(QMessageAccountId& accountId) const;

    void updateEmailAccounts() const;

    bool filterMessage(const QMessage& message, QMessageFilterPrivate::SortedMessageFilterList filterList, int start) const;
    bool queryAndFilterMessages(MessageQueryInfo &msgQueryInfo) const;
    bool startQueryingAndFilteringMessages(MessageQueryInfo &msgQueryInfo) const;
    bool searchMessages(MessageQueryInfo &msgQueryInfo, const QStringList& accountIds,
                        const QStringList& folderUris, const QString& body,
                        const QDateTime& startTimeStamp, const QDateTime& endTimeStamp,
                        const QDateTime& startReceptionTimeStamp, const QDateTime& endReceptionTimeStamp) const;
    void searchNewMessages(const QString& searchString, const QString& folderToSearch,
                           const QDateTime& startDate, const QDateTime& endDate,
                           int searchflags, uint minimumMessageSize) const;
    void handleQueryFinished(int queryIndex) const;

    void watchAllKnownEmailFolders();
    void notification(const QMessageId& messageId, NotificationType notificationType) const;

    QMessageAccountId accountIdFromModestMessageId(const QString& modestMessageId) const;
    QMessageFolderId folderIdFromModestMessageId(const QString& modestMessageId,
                                                 const QMessageAccountId accountId = QMessageAccountId()) const;

    MessagingModestMessage messageFromModest(const QString& accountId,
                                             const QString &folderId,
                                             const QString& messageId) const;

    QString modestAccountIdFromAccountId(const QMessageAccountId& accountId) const;
    QString modestFolderIdFromFolderId(const QMessageFolderId& folderId) const;
    QString modestFolderUriFromFolderId(const QMessageFolderId& folderId) const;
    QString modestAccountIdFromMessageId(const QMessageId& messageId, bool checkProtocol = true) const;
    QString modestAccountIdFromFolderId(const QMessageFolderId& folderId, bool checkProtocol = true) const;
    QString modestFolderIdFromMessageId(const QMessageId& messageId) const;
    QString modestMessageIdFromMessageId(const QMessageId& messageId) const;
    void replaceProtocol(QString& id, const QString& newProtocol) const;
    QMessageAccountId realAccountId(const MessagingModestMessage& modestMessage) const;
    QMessageAccountId accountIdFromMessageId(const QMessageId& messageId) const;
    QMessageAccountId accountIdFromFolderId(const QMessageFolderId& folderId) const;
    QMessageAccountId accountIdFromModestAccountId(const QString& accountId) const;
    QMessageFolderId folderIdFromModestFolderId(const QMessageAccountId& accountId,
                                                bool isLocalFolder,
                                                const QString& folderId) const;
    QMessageId messageIdFromModestMessageId(const QString& messageId) const;
    QMessageId messageIdFromModestMessageFilePath(const QString& messageFilePath) const;

    QMessage messageFromModestMessage(const MessagingModestMessage& modestMessage,
                                      QMessageAccountId accountId = QMessageAccountId()) const;
    void appendAttachmentToMessage(QMessage& message, QMessageContentContainer& attachment) const;

    static QString unescapeString(const QString& string);
    static QString escapeString(const QString& string);

    QMessage::StandardFolder standardFolderFromModestFolderId(const QString& modestFolderId) const;
    QString modestFolderIdFromStandardFolder(QMessage::StandardFolder standardFolder) const;

    ModestStringMap getModestSenderInfo(QMessage &message);
    ModestStringMap getModestRecipients(QMessage &message);
    ModestStringMap getModestMessageData(QMessage &message);
    ModestStringMapList getModestAttachments(QMessage &message);
    ModestStringMapList getModestImages(QMessage &message);
    uint getModestPriority(QMessage &message);
    ModestStringMap getModestHeaders(QMessage &message);

    bool accountExists(const QMessageAccountId& accountId) const;

    void mimeInformationFromModestMessageToMessage(const MessagingModestMessage& modestMessage,
                                                   QMessage& message) const;


private slots:
    void searchMessagesHeadersReceivedSlot(QDBusMessage msg);
    void searchMessagesHeadersFetchedSlot(QDBusMessage msg);
    void mimePartDownloadFinishedSlot(QDBusMessage msg);
    void folderUpdatedSlot(QDBusMessage msg);
    void messageReadChangedSlot(QDBusMessage msg);
    void pendingGetUnreadMessagesFinishedSlot(QDBusPendingCallWatcher* pendingCallWatcher);
    void pendingSearchFinishedSlot(QDBusPendingCallWatcher* pendingCallWatcher);
    void fileChangedSlot(int watchDescriptor, QString filePath, uint events);
    void sendEmailCallEnded(QDBusPendingCallWatcher *watcher);
    void addMessageCallEnded(QDBusPendingCallWatcher *watcher);
    void stateChanged(QMessageService::State newState);
    void returnQueryResultsSlot();
    void modestFolderContentsChangedSlot(QDBusMessage msg);
    void messageQueryFinishedSlot(void* queryThread, QList<QtMobility::QMessageId> ids);

private: //Data
    GConfClient *m_gconfclient;

    QDBusInterface *m_ModestDBusInterface;
    QDBusInterface *m_QtmPluginDBusInterface;

    INotifyWatcher m_MailFoldersWatcher;

    mutable QHash<QString, QMessageAccount> iAccounts;
    mutable QMessageAccountId iDefaultEmailAccountId;

    mutable QList<MessageQueryInfo> m_pendingMessageQueries;

    QMap<QMessageManager::NotificationFilterId, QMessageFilter> m_filters;
    QMessageManager::NotificationFilterId m_filterId;
    QMessageStorePrivate* m_messageStore;

    QMap<QString, QDateTime> accountsLatestTimestamp;

    mutable QStringList m_latestAddOrRemoveNotifications;

    mutable QMap<QString, MessagingModestFolder> m_folderCache;

    mutable QMap<int, QMessageServicePrivate*> m_pending_downloads;

    mutable QMessageFolderIdList m_observed_folders;

    mutable bool m_allEmailMessagesInCache;

    // Following variables are used for sync queries
    mutable QMessageService m_service;
    mutable QEventLoop      m_eventLoop;
    mutable QMessageIdList  m_ids;
    mutable int             m_count;
    mutable bool            m_isSorted;
    mutable bool            m_isFiltered;
};

class MessageQueryThread : public QThread
{
    Q_OBJECT

public:
    MessageQueryThread(const QMessageFilter &filter, const QMessageSortOrder &sortOrder);
    ~MessageQueryThread();

    void run();

signals:
    void queryFinished(void* queryThread, QList<QtMobility::QMessageId> ids);

private:
    QMessageFilter    m_filter;
    QMessageSortOrder m_sortOrder;
};

QTM_END_NAMESPACE

// Marshall the MyStructure data into a D-Bus argument
QDBusArgument &operator<<(QDBusArgument &argument, const QtMobility::ModestStringMap &map);

// Retrieve the MyStructure data from the D-Bus argument
const QDBusArgument &operator>>(const QDBusArgument &argument, QtMobility::ModestStringMap &map);

Q_DECLARE_METATYPE(QtMobility::ModestStringMap);
Q_DECLARE_METATYPE(QtMobility::ModestStringMapList);
Q_DECLARE_METATYPE(QtMobility::INotifyWatcher::FileNotification);
Q_DECLARE_METATYPE(QtMobility::ModestUnreadMessageDBusStruct);
Q_DECLARE_METATYPE(QtMobility::ModestAccountsUnreadMessagesDBusStruct);
Q_DECLARE_METATYPE(QtMobility::ModestMessage);
Q_DECLARE_METATYPE(QtMobility::MessagingModestMimePart);
Q_DECLARE_METATYPE(QtMobility::MessagingModestFolder);
Q_DECLARE_METATYPE(QList<QtMobility::QMessageId>);

#endif // MODESTENGINE_MAEMO_H