summaryrefslogtreecommitdiffstats
path: root/src/libraries/qmfclient/qmailcontentmanager.cpp
blob: 170b443f56df89315cf89e12f431c9ee45c084f8 (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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** 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 The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmailcontentmanager.h"
#include "qmailmessage.h"
#include "qmaillog.h"
#include "qmailpluginmanager.h"

#define PLUGIN_KEY "contentmanagers"

namespace {

typedef QMap<QString, QMailContentManager*> ContentPluginMap;

ContentPluginMap init(QMailPluginManager& manager)
{
    ContentPluginMap map;

    foreach (const QString &item, manager.list()) {
        QObject *instance(manager.instance(item));
        if (QMailContentManagerPlugin *iface = qobject_cast<QMailContentManagerPlugin*>(instance))
            map.insert(iface->key(), iface->create());
    }

    if(map.isEmpty()) {
        qMailLog(Messaging) << "No content manager plugins found. Message content will not be available.";
    }

    return map;
}

QString & defaultFilter()
{
    static QString filter;
    return filter;
}

QString & defaultIndexer()
{
    static QString index;
    return index;
}

ContentPluginMap &contentPlugins()
{
    static QMailPluginManager manager(PLUGIN_KEY);
    static ContentPluginMap map(init(manager));
    return map;
}

QMailContentManager *mapping(const QString &scheme)
{
    ContentPluginMap::const_iterator it = contentPlugins().find(scheme);
    if (it != contentPlugins().end())
        return it.value();

    qMailLog(Messaging) << "Unable to map content manager for scheme:" << scheme;
    return 0;
}

}

/*!
    \class QMailContentManagerFactory

    \brief The QMailContentManagerFactory class creates objects implementing the QMailContentManager interface.
    \ingroup messaginglibrary

    The QMailContentManagerFactory class creates objects that manage the storage and retrieval
    of message content via the QMailContentManger interface.  The factory allows implementations to
    be loaded from plugin libraries, and to be retrieved by the name of the content management
    scheme they implement.

    To create a new class that can be created via the QMailContentManagerFactory,
    implement a plugin that derives from QMailContentManagerPlugin.

    \sa QMailContentManager, QMailContentManagerPlugin
*/

/*!
    Returns a list of all content management schemes for which content manager objects can be instantiated by the factory.
*/
QStringList QMailContentManagerFactory::schemes()
{
    return contentPlugins().keys();
}


/*!
    Returns the default filter content manager scheme for filtering supported by the factory. Returns an empty string
    if none are found.
*/
QString QMailContentManagerFactory::defaultFilterScheme()
{
    return defaultFilter();
}

/*!
    Returns the default content indexing scheme supported by the factory. Returns an empty string
    if none are found.
*/

QString QMailContentManagerFactory::defaultIndexerScheme()
{
    return defaultIndexer();
}

/*!
    Returns the default content management scheme supported by the factory.
*/
QString QMailContentManagerFactory::defaultScheme()
{
    const QStringList &list(schemes());
    if (list.isEmpty())
        return QString();

    if (list.contains("qmfstoragemanager"))
        return "qmfstoragemanager";
    else 
        return list.first();
}

/*!
    Creates a content manager object for the scheme identified by \a scheme.
*/
QMailContentManager *QMailContentManagerFactory::create(const QString &scheme)
{
    return mapping(scheme);
}

/*!
    Performs any initialization tasks for content managers known to the factory.
    Returns false if any content managers are unable to perform initialiation tasks.
*/
bool QMailContentManagerFactory::init()
{
    ContentPluginMap::iterator end(contentPlugins().end());
    for (ContentPluginMap::iterator it(contentPlugins().begin()); it != end; ++it) {
        if (!(*it)->init())
            return false;
        else if (defaultFilter().isEmpty() && (*it)->role() == QMailContentManager::FilterRole)
            defaultFilter() = it.key();
        else if (defaultIndexer().isEmpty() && (*it)->role() == QMailContentManager::IndexRole)
            defaultIndexer() = it.key();

    }
    return true;
}

/*!
    Clears the content managed by all content managers known to the factory.
*/
void QMailContentManagerFactory::clearContent()
{
    foreach (QMailContentManager *manager, contentPlugins().values())
        manager->clearContent();
}


/*!
    \class QMailContentManagerPluginInterface

    \brief The QMailContentManagerPluginInterface class defines the interface to plugins that provide message content management facilities.
    \ingroup messaginglibrary

    The QMailContentManagerPluginInterface class defines the interface to message content manager plugins.  Plugins will 
    typically inherit from QMailContentManagerPlugin rather than this class.

    \sa QMailContentManagerPlugin, QMailContentManager, QMailContentManagerFactory
*/

/*!
    \fn QString QMailContentManagerPluginInterface::key() const

    Returns a unique string identifying the content management scheme implemented by the plugin.
*/

/*!
    \fn QMailContentManager* QMailContentManagerPluginInterface::create()

    Creates an instance of the QMailContentManager class provided by the plugin.
*/


/*!
    \class QMailContentManagerPlugin

    \brief The QMailContentManagerPlugin class defines a base class for implementing message content manager plugins.
    \ingroup messaginglibrary

    The QMailContentManagerPlugin class provides a base class for plugin classes that provide message content management
    functionality.  Classes that inherit QMailContentManagerPlugin need to provide overrides of the
    \l {QMailContentManagerPlugin::key()}{key} and \l {QMailContentManagerPlugin::create()}{create} member functions.

    \sa QMailContentManagerPluginInterface, QMailContentManager, QMailContentManagerFactory
*/

/*!
    Creates a message content manager plugin instance.
*/
QMailContentManagerPlugin::QMailContentManagerPlugin()
{
}

/*!
    Destroys the QMailContentManagerPlugin object.
*/
QMailContentManagerPlugin::~QMailContentManagerPlugin()
{
}

/*!
    Returns the list of interfaces implemented by this plugin.
*/
QStringList QMailContentManagerPlugin::keys() const
{
    return QStringList() << "QMailContentManagerPluginInterface";
}


/*!
    \class QMailContentManager

    \brief The QMailContentManager class defines the interface to objects that provide a storage facility for message content.
    \ingroup messaginglibrary

    Qt Extended uses the QMailContentManager interface to delegate the filtering, storage, retrieval and indexing of message content
    from the QMailStore class to classes loaded from plugin libraries.  A library may provide this service by exporting a
    class implementing the QMailContentManager interface, and an associated instance of QMailContentManagerPlugin.

    There are three roles a content manager can take, as defined by \l{QMailContentManager::ManagerRole}{ManagerRole}. This changes
    the order in which the plugins are called.

    FilterRole: A content manager may be defined as a filtering content manager by returning FilterRole from
    \l{QMailContentManager::role()}{Role}. Filtering content managers are called before message operations take place, and before
    the message is stored on the file system. Filtering content managers may change any details, and messages will be added to the database with
    the changes. As messages are not in the database, they will not have an id -- and setting one is invalid. Filtering content managers
    are not notified when a message is deleted, so they should be careful not to leak file system space. Filtering content managers
    should only return errors in fatal errors, not to indicate they filtered a message or not.

    IndexRole: A content manager may be defined as an indexing content manager by returning IndexRole from
    \l{QMailContentManager::role()}{Role}. Indexing content managers are called after successful operations have taken place,
    and the fields updated. An indexing content manager should not modify existing messages, but only be used to do something
    external to QMF (e.g. Add/Remove the message from an external index). Indexing content manager should only return errors
    in very serious circumstances.

    StorageRole: A content manager may be defined as a storage content manager by returning StorageRole from
    \l{QMailContentManager::role()}{Role} (the default). Storage content managers are used for saving and
    loading messages from the file system. The particular storage content managed used for a message is determined by
    the \l{QMailMessageMetaData::contentScheme()}{contentScheme} function of a QMailMessage object.  The identifier of
    the message content is provided by the corresponding \l{QMailMessageMetaData::contentIdentifier()}{contentIdentifier} 
    function; this property is provided for the use of the storage content manager code, and is opaque to the remainder of the
    system.

    If a storage content manager provides data to clients by creating references to file-backed memory
    mappings, then the content manager must ensure that those files remain valid.  The existing content
    within the file must not be modified, and the file must not be truncated.  If the content manager
    updates the content of a message which is already exported using memory mappings, then the updated 
    content should be stored to a new content location, and the message object updated with the new 
    \l{QMailMessageMetaData::contentIdentifier()}{contentIdentifier} information.

    If a stored message contains parts whose content is only partially available (as defined by
    QMailMessagePartContainer::partialContentAvailable() and QMailMessagePartContainer::contentAvailable()), 
    the content manager must ensure that the partial data is returned to clients in the same transfer 
    encoding that it was stored with.

    \sa QMailStore, QMailMessage
*/

/*!
    \enum QMailContentManager::DurabilityRequirement
    
    This enum type is used to define the dequirement for durability in a content management request.

    \value EnsureDurability     The content manager should make the requested changes durable before reporting success.
    \value DeferDurability      The content manager may defer ensuring that changes are durable until the next invocation of ensureDurability().
    \value NoDurability         The content manager will not make changes durable.
*/

/*!
    \enum QMailContentManager::ManagerRole

    This enum type is used to define the purpose of the plugin

    \value FilterRole The content manager is a filtering content manager. It is used to modify messages
           that match its own criteria. Filtering content managers are called before any others.
    \value StorageRole The content manager is a storage content manager. It is used to storage messages
           persistently. Storage content managers are called after filter content managers, but
           before index content managers.
    \value IndexRole The content manager is for indexing purposes. It is called after the message has been
           stored.

*/

/*!
    \fn QMailStore::ErrorCode QMailContentManager::add(QMailMessage *message, QMailContentManager::DurabilityRequirement durability)

    Tells the content manager of a new \a message. If this is a storage content manager it should add to storage and then update the
    message such that its \l{QMailMessageMetaData::contentIdentifier()}{contentIdentifier}
    property contains the location at which the content is stored.

    Returns \l{QMailStore::NoError}{NoError} to indicate success.

    If \l{QMailMessageMetaData::contentIdentifier()}{contentIdentifier} is already populated at invocation, 
    the content manager should determine whether the supplied identifier can be used.  If not, it should 
    use an alternate location and update \a message with the new identifier.

    If \a durability is \l{QMailContentManager::EnsureDurability}{EnsureDurability} then the content
    manager should ensure that the message addition has been recorded in a durable fashion before reporting
    success to the caller.
*/

/*!
    \fn QMailStore::ErrorCode QMailContentManager::update(QMailMessage *message, QMailContentManager::DurabilityRequirement durability)

    Notifies the content manager that \a message has been updated. If this is a storage content manager
    it should update the message content stored at the location indicated
    by \l{QMailMessageMetaData::contentIdentifier()}{contentIdentifier}, to contain the current 
    content of \a message.  
    Returns \l{QMailStore::NoError}{NoError} to indicate successful update of the message content.

    If the updated content is not stored to the existing location, the content manager should 
    use an alternate location and update \a message with the new 
    \l{QMailMessageMetaData::contentIdentifier()}{contentIdentifier}.

    The existing content should be removed if the update causes a new content identifier to 
    be allocated.  If the previous content cannot be removed, but the update was otherwise 
    successful, the content manager should return \l{QMailStore::ContentNotRemoved}{ContentNotRemoved} 
    to indicate that removal of the content should be retried at a later time.

    If \a durability is \l{QMailContentManager::EnsureDurability}{EnsureDurability} then the content
    manager should ensure that the message update has been recorded in a durable fashion before reporting
    success to the caller.
*/

/*!
    \fn QMailStore::ErrorCode QMailContentManager::ensureDurability()

    Requests that the content manager ensure that any previous actions that were performed with the
    \l{QMailContentManager::DeferDurability}{DeferDurability} option be made durable.
*/

/*!
    \fn QMailStore::ErrorCode QMailContentManager::ensureDurability(const QList<QString> &identifiers)

    Requests that the content manager ensure that message data will be made durable for messages
    identified by the given content \a identifiers.

    To be used with the \l{QMailContentManager::DeferDurability}{NoDurability} option.
*/

/*!
    \fn QMailStore::ErrorCode QMailContentManager::remove(const QString &identifier)

    Notifies the content manager that the message identified by \a identifier has been removed.

    The content manager should only remove data it has created with association to identifier. For instance
    a content manager that does indexing, should only remove the data it has created reaction to the identifier,
    but not the file itself.

    Returns \l{QMailStore::NoError}{NoError} to indicate that the message content
    has been successfully removed.

    If the content cannot be removed, the content manager should return 
    \l{QMailStore::ContentNotRemoved}{ContentNotRemoved} to indicate that removal of the content
    should be retried at a later time.

    If the identified content does not already exist, the content manager should return \l{QMailStore::InvalidId}{InvalidId}.
*/

/*!
    \fn QMailStore::ErrorCode QMailContentManager::load(const QString &identifier, QMailMessage *message)

    Requests that the storage content manager load the message content stored at the location indicated
    by \a identifier into the message record \a message.  Returns \l{QMailStore::NoError}{NoError} to indicate that the message 
    content has been successfully loaded.

    If the identified content does not already exist, the content manager should return \l{QMailStore::InvalidId}{InvalidId}.
*/

/*! \internal */
QMailContentManager::QMailContentManager()
{
}

/*! \internal */
QMailContentManager::~QMailContentManager()
{
}

/*!
    Notifies the content manager that the messages identified by \a identifiers have been removed.

    The content manager should only remove data it has created with association to identifier. For instance
    a content manager that does indexing, should only remove the data it has created reaction to the identifier,
    but not the file itself.

    Returns \l{QMailStore::NoError}{NoError} to indicate that the message content
    has been successfully removed.

    If the content cannot be removed, the content manager should return
    \l{QMailStore::ContentNotRemoved}{ContentNotRemoved} to indicate that removal of the content
    should be retried at a later time.

    If the identified content does not already exist, the content manager should return \l{QMailStore::InvalidId}{InvalidId}.
*/

QMailStore::ErrorCode QMailContentManager::remove(const QList<QString> &identifiers)
{
     QMailStore::ErrorCode returnError(QMailStore::NoError);
    foreach(QString const &identifier, identifiers) {
        QMailStore::ErrorCode err(remove(identifier));
        if (returnError == QMailStore::NoError) // report the first failure, but continue.
            returnError = err;
    }

    return returnError;
}


/*!
    Directs the content manager to perform any initialization tasks required.
    The content manager should return false if unable to perform initialization tasks; otherwise return true.

    This function is called by the mail store after it has been successfully initialized.
*/
bool QMailContentManager::init()
{
    return true;
}

/*!
    Directs the content manager to clear any message content that it is responsible for.

    This function is called by the mail store to remove all existing data, typically in test conditions.
*/
void QMailContentManager::clearContent()
{
}

/*!
    This function is called by the mail store to query the purpose of this particular plugin.
*/
QMailContentManager::ManagerRole QMailContentManager::role() const
{
    return StorageRole;
}