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