src/contacts/qcontactdetail_p.h
changeset 0 876b1a06bc25
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 
       
    43 #ifndef QCONTACTDETAIL_P_H
       
    44 #define QCONTACTDETAIL_P_H
       
    45 
       
    46 //
       
    47 //  W A R N I N G
       
    48 //  -------------
       
    49 //
       
    50 // This file is not part of the Qt API.  It exists purely as an
       
    51 // implementation detail.  This header file may change from version to
       
    52 // version without notice, or even be removed.
       
    53 //
       
    54 // We mean it.
       
    55 //
       
    56 
       
    57 #include "qcontactdetail.h"
       
    58 
       
    59 #include <QSharedData>
       
    60 #include <QString>
       
    61 #include <QHash>
       
    62 
       
    63 QTM_BEGIN_NAMESPACE
       
    64 
       
    65 /*
       
    66   Yet another string class
       
    67 
       
    68   Mostly a wrapper around char *, where we do pointer equality checks before
       
    69   a strcmp, given our common use case of equivalent pointers.
       
    70 
       
    71   Also handles when things get passed in as a QString, by converting to Latin1
       
    72   and caching the result, and caching conversions to QString (hit by definitionName a lot)
       
    73 */
       
    74 class QContactStringHolder
       
    75 {
       
    76 public:
       
    77     QContactStringHolder()
       
    78         : m_str(0)
       
    79     {
       
    80     }
       
    81 
       
    82     ~QContactStringHolder()
       
    83     {
       
    84     }
       
    85 
       
    86     QContactStringHolder(const QContactStringHolder& other)
       
    87         : m_str(other.m_str)
       
    88     {
       
    89     }
       
    90 
       
    91     QContactStringHolder& operator=(const QContactStringHolder& other)
       
    92     {
       
    93         m_str = other.m_str; // checking for ==this is not worth the effort
       
    94         return *this;
       
    95     }
       
    96 
       
    97     QContactStringHolder(const char *str)
       
    98         : m_str(str)
       
    99     {
       
   100     }
       
   101 
       
   102     QContactStringHolder& operator=(const char *str)
       
   103     {
       
   104         m_str = str;
       
   105         return *this;
       
   106     }
       
   107 
       
   108     explicit QContactStringHolder(const QString& str)
       
   109     {
       
   110         *this = str;
       
   111     }
       
   112 
       
   113     QContactStringHolder& operator=(const QString& str)
       
   114     {
       
   115         m_str = s_allocated.value(str, 0);
       
   116         if (!m_str) {
       
   117             m_str = qstrdup(str.toLatin1().constData());
       
   118             s_allocated.insert(str, const_cast<char*>(m_str)); // it's my pointer
       
   119         }
       
   120         return *this;
       
   121     }
       
   122 
       
   123     bool operator==(const char* other) const
       
   124     {
       
   125         return other == m_str || (qstrcmp(other, m_str) == 0);
       
   126     }
       
   127 
       
   128     bool operator==(const QString& other) const
       
   129     {
       
   130         return (s_allocated.value(other, 0) == m_str) || (other == QLatin1String(m_str));
       
   131     }
       
   132 
       
   133     bool operator==(const QContactStringHolder& other) const
       
   134     {
       
   135         return (other.m_str == m_str) || (qstrcmp(other.m_str, m_str) == 0);
       
   136     }
       
   137 
       
   138     QString toQString() const
       
   139     {
       
   140         QString s = s_qstrings.value(m_str);
       
   141         if (!s.isEmpty())
       
   142             return s;
       
   143         s = QString::fromLatin1(m_str);
       
   144         s_qstrings.insert(m_str, s);
       
   145         return s;
       
   146     }
       
   147 
       
   148 public:
       
   149     // The only data we have
       
   150     const char* m_str;
       
   151 
       
   152     static QHash<QString, char*> s_allocated;
       
   153     static QHash<const char *, QString> s_qstrings;
       
   154 };
       
   155 
       
   156 uint qHash(const QContactStringHolder& key);
       
   157 
       
   158 class QContactDetailPrivate : public QSharedData
       
   159 {
       
   160 public:
       
   161     QContactDetailPrivate()
       
   162         : QSharedData(),
       
   163         m_id(lastDetailKey.fetchAndAddOrdered(1)),
       
   164         m_access(QContactDetail::NoConstraint)
       
   165     {
       
   166     }
       
   167 
       
   168     QContactDetailPrivate(const QContactDetailPrivate& other)
       
   169         : QSharedData(other),
       
   170         m_id(other.m_id),
       
   171         m_definitionName(other.m_definitionName),
       
   172         m_values(other.m_values),
       
   173         m_access(other.m_access)
       
   174     {
       
   175     }
       
   176 
       
   177     ~QContactDetailPrivate()
       
   178     {
       
   179     }
       
   180 
       
   181     int m_id; // internal, unique id.
       
   182     QContactStringHolder m_definitionName;
       
   183     QHash<QContactStringHolder, QVariant> m_values;
       
   184     QContactDetail::AccessConstraints m_access;
       
   185 
       
   186     static QAtomicInt lastDetailKey;
       
   187 
       
   188     static void setAccessConstraints(QContactDetail *d, QContactDetail::AccessConstraints constraint)
       
   189     {
       
   190         d->d->m_access = constraint;
       
   191     }
       
   192 
       
   193     static const QContactDetailPrivate* detailPrivate(const QContactDetail& detail)
       
   194     {
       
   195         return detail.d.constData();
       
   196     }
       
   197 };
       
   198 
       
   199 QTM_END_NAMESPACE
       
   200 
       
   201 Q_DECLARE_TYPEINFO(QTM_PREPEND_NAMESPACE(QContactStringHolder), Q_MOVABLE_TYPE);
       
   202 
       
   203 #endif