src/hbcore/utils/hbsmileytheme.cpp
changeset 0 16d8024aca5e
child 21 4633027730f5
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include "hbsmileytheme.h"
       
    27 #include "hbsmileythemeparser_p.h"
       
    28 #include <QVariant>
       
    29 #include <QSharedData>
       
    30 #include <QHash>
       
    31 #include <QSet>
       
    32 
       
    33 class HbSmileyThemePrivate : public QSharedData
       
    34 {
       
    35 public:
       
    36 
       
    37     HbSmileyThemePrivate();
       
    38     HbSmileyThemePrivate(const HbSmileyThemePrivate &other);
       
    39     ~HbSmileyThemePrivate();
       
    40 
       
    41 public: // data
       
    42 
       
    43     QHash<QString,QSet<QString> > smileyToPatterns;
       
    44     QHash <QString, QString> patternToSmiley;
       
    45 };
       
    46 
       
    47 HbSmileyThemePrivate::HbSmileyThemePrivate()
       
    48 {
       
    49 }
       
    50 
       
    51 HbSmileyThemePrivate::HbSmileyThemePrivate(const HbSmileyThemePrivate &other) : QSharedData(other),
       
    52         smileyToPatterns(other.smileyToPatterns),
       
    53         patternToSmiley(other.patternToSmiley)
       
    54 {
       
    55 }
       
    56 
       
    57 HbSmileyThemePrivate::~HbSmileyThemePrivate()
       
    58 {
       
    59 }
       
    60 
       
    61 
       
    62 /*!
       
    63     @beta
       
    64     @hbcore
       
    65     \class HbSmileyTheme
       
    66     \brief HbSmileyTheme is container class for defining a smiley theme.
       
    67 
       
    68     Smiley is a sequence of keyboard characters used to represent a person's mood or emotion, especially :) or :-)
       
    69     or other smiling depiction. Smileys are also represented by graphics so called emoticons.
       
    70     In HbSmileyTheme \a smiley refers to emoticons and \a pattern refers to character sequence representation of a smiley.
       
    71     In a smiley theme each smiley is associated with one or multiple patterns. Each smiley is identified by a string that
       
    72     is the file name of the graphics file representing the corresponding emoticon.
       
    73 
       
    74     This class implements copy-on-write semantics, so copy constructor and assignment operator
       
    75     only perform shallow copy and are fast. A deep copy is done if the theme properties are changed.
       
    76 
       
    77     Brief example of how to create a smiley themes and use it.
       
    78 
       
    79     Example usage:
       
    80     \dontinclude ultimatecodesnippet/ultimatecodesnippet.cpp
       
    81     \skip Start of snippet 46
       
    82     \until End of snippet 46
       
    83 
       
    84     Smiley themes can be loaded from smiley theme (.sml) files.
       
    85 
       
    86     Example usage:
       
    87     \dontinclude ultimatecodesnippet/ultimatecodesnippet.cpp
       
    88     \skip Start of snippet 47
       
    89     \until End of snippet 47
       
    90 
       
    91     Here is an example of a smiley theme file describing the file syntax.
       
    92 
       
    93     \include unittest_hbsmileytheme/my_smileys_theme.sml
       
    94 
       
    95 
       
    96     \sa HbAbstractEdit
       
    97 */
       
    98 
       
    99 
       
   100 /*!
       
   101 * Default constructor. Constructs a null smiley theme.
       
   102 *
       
   103 *  \sa isNull()
       
   104 */
       
   105 HbSmileyTheme::HbSmileyTheme()
       
   106 {
       
   107     d = new HbSmileyThemePrivate();
       
   108 }
       
   109 
       
   110 /*!
       
   111 * Constructs a smiley theme from a smiley theme definition file.
       
   112 * If the file does not exist or is of an unknown format, the smiley theme becomes a null theme.
       
   113 *
       
   114 *  \sa isNull() load()
       
   115 */
       
   116 HbSmileyTheme::HbSmileyTheme(const QString &fileName)
       
   117 {
       
   118     d = new HbSmileyThemePrivate();
       
   119     load(fileName);
       
   120 }
       
   121 
       
   122 
       
   123 /*!
       
   124 *   Constructs a smiley theme that is a copy of the given smiley theme \a other.
       
   125 */
       
   126 HbSmileyTheme::HbSmileyTheme(const HbSmileyTheme &other) : d(other.d)
       
   127 {
       
   128 }
       
   129 
       
   130 /*!
       
   131 *   Assigns the given smiley theme \a other to this smiley theme and returns a reference
       
   132 *   to this smiley theme.
       
   133 *
       
   134 *   \sa HbSmileyTheme()
       
   135 */
       
   136 HbSmileyTheme &HbSmileyTheme::operator=(const HbSmileyTheme &other)
       
   137 {
       
   138     if (&other != this) {
       
   139         d = other.d;
       
   140     }
       
   141     return *this;
       
   142 }
       
   143 
       
   144 /*!
       
   145 * Destroys the smiley theme.
       
   146 */
       
   147 HbSmileyTheme::~HbSmileyTheme()
       
   148 {
       
   149 }
       
   150 
       
   151 /*!
       
   152 * Loads a smiley theme from a smiley theme definition file
       
   153 * Returns true if the smiley theme was successfully loaded; otherwise returns false.
       
   154 */
       
   155 bool HbSmileyTheme::load (const QString &fileName)
       
   156 {
       
   157     HbSmileyTheme tmpTheme;
       
   158     HbSmileyThemeParser parser;
       
   159 
       
   160     bool ret = parser.parseDefinitionFile(fileName,tmpTheme);
       
   161     if (ret) {
       
   162         *this = tmpTheme;
       
   163     }
       
   164     return ret;
       
   165 }
       
   166 
       
   167 
       
   168 /*!
       
   169 * Returns true if the smiley theme doesn't contain any smileys.
       
   170 * This is the case when the smiley theme is default constructed.
       
   171 */
       
   172 bool HbSmileyTheme::isNull() const
       
   173 {
       
   174     return d->smileyToPatterns.isEmpty();
       
   175 }
       
   176 
       
   177 /*!
       
   178 * Inserts a smiley to the smiley theme with a given pattern.
       
   179 * \a smiley identifies smiley and represents the icon name of the smiley.
       
   180 * It can be any name that is valid for an icon name for HbIcon.
       
   181 *
       
   182 * \a pattern is a textual representation of the smiley.
       
   183 * If the smiley already exists with \a smiley in the theme then \a pattern will be
       
   184 * an additional textual representation for the smiley.
       
   185 *
       
   186 * \sa remove() replace()
       
   187 */
       
   188 void HbSmileyTheme::insert(const QString &smiley, const QString &pattern)
       
   189 {
       
   190     if (!smiley.isEmpty() && !pattern.isEmpty()) {
       
   191         d.detach();
       
   192         QSet<QString> patterns = d->smileyToPatterns.value(smiley);
       
   193         patterns << pattern;
       
   194         d->smileyToPatterns[smiley] = patterns;
       
   195         d->patternToSmiley[pattern] = smiley;
       
   196     }
       
   197 }
       
   198 
       
   199 
       
   200 /*!
       
   201 * Inserts a smiley to the smiley theme with a given pattern list.
       
   202 * \a smiley identifies smiley and represents the icon name of the smiley.
       
   203 * It can be any name that is valid for an icon name for HbIcon.
       
   204 *
       
   205 *
       
   206 * \a patterns is a list of textual representation of the smiley.
       
   207 * If the smiley already exists with \a smiley in the theme then \a pattern list will be
       
   208 * additional textual representations for the smiley.
       
   209 *
       
   210 * \sa remove() replace()
       
   211 */
       
   212 void HbSmileyTheme::insert(const QString &smiley, const QStringList &patterns)
       
   213 {
       
   214     foreach(QString pattern, patterns) {
       
   215         insert(smiley,pattern);
       
   216     }
       
   217 }
       
   218 
       
   219 /*!
       
   220 * Replaces the smiley patterns for \a smiley.
       
   221 * This is equivalent to calling remove(smiley) and subsequently insert(smiley,patterns).
       
   222 *
       
   223 * \sa remove() insert()
       
   224 */
       
   225 void HbSmileyTheme::replace(const QString &smiley, const QStringList &patterns)
       
   226 {
       
   227     remove(smiley);
       
   228     insert(smiley,patterns);
       
   229 }
       
   230 
       
   231 /*!
       
   232 * Replaces the smiley patterns for \a smiley with a single \a pattern.
       
   233 * This is equivalent to calling remove(smiley) and subsequently insert(smiley,pattern).
       
   234 *
       
   235 *
       
   236 * \sa remove() insert()
       
   237 */
       
   238 void HbSmileyTheme::replace(const QString &smiley, const QString &pattern)
       
   239 {
       
   240     remove(smiley);
       
   241     insert(smiley,pattern);
       
   242 }
       
   243 
       
   244 /*!
       
   245 * Removes \a smiley from the smiley theme along with its associated patterns.
       
   246 *
       
   247 * \sa insert() replace() removePattern()
       
   248 */
       
   249 void HbSmileyTheme::remove(const QString &smiley)
       
   250 {
       
   251     QSet<QString> tmpPatterns = d->smileyToPatterns.value(smiley);
       
   252     if (!tmpPatterns.isEmpty()) {
       
   253         d.detach();
       
   254         d->smileyToPatterns.remove(smiley);
       
   255         foreach(QString pattern, tmpPatterns) {
       
   256             d->patternToSmiley.remove(pattern);
       
   257         }
       
   258     }
       
   259 }
       
   260 
       
   261 /*!
       
   262 * Removes \a pattern from the theme.
       
   263 *
       
   264 *
       
   265 * \sa insert() replace() remove()
       
   266 */
       
   267 void HbSmileyTheme::removePattern(const QString &pattern)
       
   268 {
       
   269     QString smiley = d->patternToSmiley.value(pattern);
       
   270     if (!smiley.isEmpty()) {
       
   271         QSet<QString> tmpPatterns = d->smileyToPatterns.value(smiley);
       
   272         if (!tmpPatterns.isEmpty()) {
       
   273             tmpPatterns.remove(pattern);
       
   274             d.detach();
       
   275             if (tmpPatterns.isEmpty()) {
       
   276                 d->smileyToPatterns.remove(smiley);
       
   277             } else {
       
   278                 d->smileyToPatterns[smiley] = tmpPatterns;
       
   279             }
       
   280             d->patternToSmiley.remove(pattern);
       
   281         }
       
   282     }
       
   283 }
       
   284 
       
   285 /*!
       
   286 *
       
   287 * Remove all smileys from the theme and sets the smiley theme to the same state as
       
   288 * when default constructed.
       
   289 *
       
   290 * \sa isNull()
       
   291 */
       
   292 void HbSmileyTheme::clear()
       
   293 {
       
   294     if (!isNull()) {
       
   295         d.detach();
       
   296         d->smileyToPatterns.clear();
       
   297         d->patternToSmiley.clear();        
       
   298     }
       
   299 }
       
   300 
       
   301 /*!
       
   302 * Returns an existing smiley from the smiley theme for a given \a pattern
       
   303 * If the smiley doesn't exist it returns a null string.
       
   304 *
       
   305 * \sa smileys()
       
   306 */
       
   307 QString HbSmileyTheme::smiley(const QString &pattern) const
       
   308 {
       
   309     return d->patternToSmiley.value(pattern);
       
   310 }
       
   311 
       
   312 
       
   313 /*!
       
   314 * Returns the existing smileys from the smiley theme.
       
   315 *
       
   316 * \sa patterns()
       
   317 */
       
   318 QStringList HbSmileyTheme::smileys() const
       
   319 {
       
   320     return d->smileyToPatterns.keys();
       
   321 }
       
   322 
       
   323 
       
   324 /*!
       
   325 * Returns the currently associated textual patterns for \a smiley.
       
   326 *
       
   327 * \sa smileys()
       
   328 */
       
   329 QStringList HbSmileyTheme::patterns(const QString &smiley) const
       
   330 {
       
   331     QSet<QString> tmpPatterns = d->smileyToPatterns.value(smiley);
       
   332     return tmpPatterns.toList();
       
   333 }
       
   334 
       
   335 /*!
       
   336 * Convenience method.
       
   337 * Returns the textual patterns for all the smileys in the theme.
       
   338 *
       
   339 * \sa smileys()
       
   340 */
       
   341 QStringList HbSmileyTheme::patterns() const
       
   342 {
       
   343     return d->patternToSmiley.keys();
       
   344 }
       
   345 
       
   346 
       
   347 /*!
       
   348 * Returns the smiley theme as a QVariant.
       
   349 */
       
   350 HbSmileyTheme::operator QVariant() const
       
   351 {
       
   352     return QVariant::fromValue(*this);
       
   353 }
       
   354