1/****************************************************************************
2**
3** Copyright (C) 2015 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtVersit module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL21$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 or version 3 as published by the Free
20** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22** following information to ensure the GNU Lesser General Public License
23** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25**
26** As a special exception, The Qt Company gives you certain additional
27** rights. These rights are described in The Qt Company LGPL Exception
28** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29**
30** $QT_END_LICENSE$
31**
32****************************************************************************/
33
34#include "qversitresourcehandler.h"
35
36#include <QtCore/qbytearray.h>
37#include <QtCore/qfile.h>
38#include <QtCore/qstring.h>
39
40#include "qversitdefs_p.h"
41#include "qversitproperty.h"
42
43QT_BEGIN_NAMESPACE_VERSIT
44
45/*!
46 \class QVersitResourceHandler
47 \brief The QVersitResourceHandler class is an interface for clients wishing to implement custom
48 behaviour for loading and saving files to disk when exporting and importing.
49 \ingroup versit-extension
50 \inmodule QtVersit
51
52 \sa QVersitContactImporter
53 \sa QVersitContactExporter
54 \sa QVersitDefaultResourceHandler
55 */
56
57/*!
58 \fn virtual QVersitResourceHandler::~QVersitResourceHandler()
59 Frees any memory used by the handler.
60 */
61
62/*!
63 \fn virtual bool QVersitResourceHandler::saveResource(const QByteArray& contents, const QVersitProperty& property, QString* location) = 0;
64 Saves the binary data \a contents to a file on a persistent storage medium.
65
66 \a property holds the QVersitProperty which is the context in which the binary is coming from.
67 The QVersitResourceHandler can use this, for example, to determine file extension it should choose.
68 *\a location is filled with the contents of the file.
69 Returns true on success, false on failure.
70 */
71
72/*!
73 \fn virtual bool QVersitResourceHandler::loadResource(const QString& location, QByteArray* contents, QString* mimeType) = 0
74 Loads a file from \a location.
75 *\a contents is filled with the contents of the file and *\a mimeType is set to the MIME
76 type that it is determined to be.
77 Returns true on success, false on failure.
78*/
79
80/*!
81 \class QVersitDefaultResourceHandler
82
83 \brief The QVersitDefaultResourceHandler class provides a default implementation of a Versit
84 resource handler.
85 \ingroup versit-extension
86
87 An example resource handler implementation:
88 \snippet qtversitdocsample/qtversitdocsample.cpp Resource handler
89
90 \sa QVersitContactImporter, QVersitContactExporter, QVersitResourceHandler
91 */
92
93class QVersitDefaultResourceHandlerPrivate {
94public:
95 QMap<QString,QString> mFileExtensionMapping;
96};
97
98/*!
99 Constructs a QVersitDefaultResourceHandler.
100 */
101QVersitDefaultResourceHandler::QVersitDefaultResourceHandler()
102 : d(new QVersitDefaultResourceHandlerPrivate)
103{
104 // File extension mappings
105 int fileExtensionCount = sizeof(versitFileExtensionMappings)/sizeof(VersitFileExtensionMapping);
106 for (int i = 0; i < fileExtensionCount; i++) {
107 d->mFileExtensionMapping.insert(
108 akey: QLatin1String(versitFileExtensionMappings[i].extension),
109 avalue: QLatin1String(versitFileExtensionMappings[i].mimeType));
110 }
111}
112
113/*!
114 Frees any memory used by the resource handler.
115 */
116QVersitDefaultResourceHandler::~QVersitDefaultResourceHandler()
117{
118 delete d;
119}
120
121/*!
122 Default resource loader.
123 Loads file from given \a location into \a contents and returns true if successful.
124 Sets the \a mimeType based on the file extension.
125 */
126bool QVersitDefaultResourceHandler::loadResource(const QString& location,
127 QByteArray* contents,
128 QString* mimeType)
129{
130 QString extension = location.split(sep: QLatin1Char('.')).last().toLower();
131 *mimeType = d->mFileExtensionMapping.value(akey: extension);
132 if (location.isEmpty())
133 return false;
134 QFile file(location);
135 if (!file.open(flags: QIODevice::ReadOnly))
136 return false;
137 if (!file.isReadable())
138 return false;
139 *contents = file.readAll();
140 return contents->size() > 0;
141}
142
143/*!
144 Default resource saver.
145 Does nothing and returns false, ignoring \a contents, \a property and \a location. By default,
146 resources aren't persisted because we don't know when it is safe to remove them.
147 */
148bool QVersitDefaultResourceHandler::saveResource(const QByteArray& contents,
149 const QVersitProperty& property,
150 QString* location)
151{
152 Q_UNUSED(contents)
153 Q_UNUSED(property)
154 Q_UNUSED(location)
155 return false;
156}
157
158QT_END_NAMESPACE_VERSIT
159

source code of qtpim/src/versit/qversitresourcehandler.cpp