qtmobility/src/versit/qversitresourcehandler.cpp
changeset 1 2b40d63a9c3d
child 8 71781823f776
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     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 "qversitdefaultresourcehandler_p.h"
       
    45 #include "qversitdefs_p.h"
       
    46 #include <QFile>
       
    47 
       
    48 QTM_USE_NAMESPACE
       
    49 
       
    50 /*!
       
    51   \class QVersitResourceHandler
       
    52   \preliminary
       
    53   \brief The QVersitResourceHandler class is an interface for clients wishing to implement custom
       
    54   behaviour for loading and saving files to disk when exporting and importing.
       
    55   \ingroup versit
       
    56 
       
    57   \sa QVersitContactImporter
       
    58   \sa QVersitContactExporter
       
    59   \sa QVersitDefaultResourceHandler
       
    60  */
       
    61 
       
    62 /*!
       
    63   \fn virtual QVersitResourceHandler::~QVersitResourceHandler()
       
    64   Frees any memory used by the handler.
       
    65  */
       
    66 
       
    67 /*!
       
    68   \fn virtual bool QVersitResourceHandler::saveResource(const QByteArray& contents, const QVersitProperty& property, QString* location) = 0;
       
    69   Saves the binary data \a contents to a file on a persistent storage medium.
       
    70  
       
    71   \a property holds the QVersitProperty which is the context in which the binary is coming from.
       
    72   The QVersitResourceHandler can use this, for example, to determine file extension it should choose.
       
    73   *\a location is filled with the contents of the file.
       
    74   Returns true on success, false on failure.
       
    75  */
       
    76 
       
    77 /*!
       
    78  \fn virtual bool QVersitResourceHandler::loadResource(const QString& location, QByteArray* contents, QString* mimeType) = 0
       
    79  Loads a file from \a location.
       
    80  *\a contents is filled with the contents of the file and *\a mimeType is set to the MIME
       
    81  type that it is determined to be.
       
    82  Returns true on success, false on failure.
       
    83 */
       
    84 
       
    85 /*!
       
    86   \class QVersitDefaultResourceHandler
       
    87  
       
    88   \brief The QVersitDefaultResourceHandler class provides a default implementation of a Versit
       
    89   resource handler.
       
    90  
       
    91   An example resource handler implementation:
       
    92   \snippet ../../doc/src/snippets/qtversitdocsample/qtversitdocsample.cpp Resource handler
       
    93   \ingroup versit
       
    94  
       
    95   \sa QVersitContactImporter, QVersitContactExporter, QVersitResourceHandler
       
    96  */
       
    97 
       
    98 /*!
       
    99   Constructs a QVersitDefaultResourceHandler.
       
   100  */
       
   101 QVersitDefaultResourceHandler::QVersitDefaultResourceHandler()
       
   102     : d(new QVersitDefaultResourceHandlerPrivate)
       
   103 {
       
   104     // File extension mappings
       
   105     int fileExtensionCount = sizeof(versitFileExtensionMappings)/sizeof(VersitMapping);
       
   106     for (int i = 0; i < fileExtensionCount; i++) {
       
   107         d->mFileExtensionMapping.insert(
       
   108             QLatin1String(versitFileExtensionMappings[i].contactString),
       
   109             QLatin1String(versitFileExtensionMappings[i].versitString));
       
   110     }
       
   111 }
       
   112 
       
   113 /*!
       
   114   Frees any memory used by the resource handler.
       
   115  */
       
   116 QVersitDefaultResourceHandler::~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  */
       
   126 bool QVersitDefaultResourceHandler::loadResource(const QString& location,
       
   127                                                  QByteArray* contents,
       
   128                                                  QString* mimeType)
       
   129 {
       
   130     QString extension = location.split(QLatin1Char('.')).last().toLower();
       
   131     *mimeType = d->mFileExtensionMapping.value(extension);
       
   132     if (location.isEmpty())
       
   133         return false;
       
   134     QFile file(location);
       
   135     file.open(QIODevice::ReadOnly);
       
   136     if (!file.isReadable())
       
   137         return false;
       
   138     *contents = file.readAll();
       
   139     return contents->size() > 0;
       
   140 }
       
   141 
       
   142 /*!
       
   143   Default resource saver.
       
   144   Does nothing and returns false, ignoring \a contents, \a property and \a location.  By default,
       
   145   resources aren't persisted because we don't know when it is safe to remove them.
       
   146  */
       
   147 bool QVersitDefaultResourceHandler::saveResource(const QByteArray& contents,
       
   148                                                  const QVersitProperty& property,
       
   149                                                  QString* location)
       
   150 {
       
   151     Q_UNUSED(contents)
       
   152     Q_UNUSED(property)
       
   153     Q_UNUSED(location)
       
   154     return false;
       
   155 }