qtmobility/src/messaging/addresshelper.cpp
changeset 1 2b40d63a9c3d
child 11 06b8e2af4411
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 "addresshelper_p.h"
       
    43 #include <qpair.h>
       
    44 
       
    45 QTM_BEGIN_NAMESPACE
       
    46 static QPair<int, int> findDelimiters(const QString& text)
       
    47 {
       
    48     int first = -1;
       
    49     int second = -1;
       
    50 
       
    51     bool quoted = false;
       
    52     bool escaped = false;
       
    53 
       
    54     const QChar* const begin = text.constData();
       
    55     const QChar* const end = begin + text.length();
       
    56     for (const QChar* it = begin; it != end; ++it ) {
       
    57         if ( !escaped && ( *it == '\\' ) ) {
       
    58             escaped = true;
       
    59             continue;
       
    60         }
       
    61 
       
    62         if ( !quoted && *it == '"' && !escaped ) {
       
    63             quoted = true;
       
    64         }
       
    65         else if ( quoted && *it == '"' && !escaped ) {
       
    66             quoted = false;
       
    67         }
       
    68 
       
    69         if ( !quoted ) {
       
    70             if ( first == -1 && *it == '<' ) {
       
    71                 first = (it - begin);
       
    72             }
       
    73             else if ( second == -1 && *it == '>' ) {
       
    74                 second = (it - begin);
       
    75                 break;
       
    76             }
       
    77         }
       
    78 
       
    79         escaped = false;
       
    80     }
       
    81 
       
    82     return qMakePair(first, second);
       
    83 }
       
    84 
       
    85 void qParseMailbox(QString& input, QString& name, QString& address, QString& suffix, bool &startDelimeterFound, bool &endDelimeterFound)
       
    86 {
       
    87     // See if there is a trailing suffix
       
    88     int pos = input.indexOf("/TYPE=");
       
    89     if (pos != -1)
       
    90     {
       
    91         suffix = input.mid(pos + 6);
       
    92         input = input.left(pos);
       
    93     }
       
    94 
       
    95     // Separate the email address from the name
       
    96     QPair<int, int> delimiters = findDelimiters(input);
       
    97 
       
    98     if (delimiters.first == -1 && delimiters.second == -1)
       
    99     {
       
   100         name = address = input.trimmed();
       
   101     }
       
   102     else
       
   103     {
       
   104         if (delimiters.first == -1)
       
   105         {
       
   106             // Unmatched '>'
       
   107             address = input.left( delimiters.second );
       
   108             if ( name.isEmpty() )
       
   109                 name = address;
       
   110         }
       
   111         else
       
   112         {
       
   113             name = input.left( delimiters.first );
       
   114 
       
   115             if (delimiters.second == -1)
       
   116                 address = input.right(input.length() - delimiters.first - 1);
       
   117             else
       
   118                 address = input.mid(delimiters.first + 1, (delimiters.second - delimiters.first - 1)).trimmed();
       
   119         }
       
   120         name = name.trimmed();
       
   121     }
       
   122 
       
   123     if (delimiters.first == -1)
       
   124         startDelimeterFound = false;
       
   125     else
       
   126         startDelimeterFound = true;
       
   127 
       
   128     if (delimiters.second == -1)
       
   129         endDelimeterFound = false;
       
   130     else
       
   131         endDelimeterFound = true;
       
   132 }
       
   133 
       
   134 QTM_END_NAMESPACE