emailservices/nmutilities/src/nmuriparser.cpp
author hgs
Thu, 30 Sep 2010 11:43:07 +0300
changeset 74 6c59112cfd31
permissions -rw-r--r--
201039
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
74
hgs
parents:
diff changeset
     1
/*
hgs
parents:
diff changeset
     2
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
hgs
parents:
diff changeset
     3
* All rights reserved.
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
hgs
parents:
diff changeset
     5
* under the terms of "Eclipse Public License v1.0"
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
hgs
parents:
diff changeset
     8
*
hgs
parents:
diff changeset
     9
* Initial Contributors:
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
hgs
parents:
diff changeset
    11
*
hgs
parents:
diff changeset
    12
* Contributors:
hgs
parents:
diff changeset
    13
*
hgs
parents:
diff changeset
    14
* Description: A utility class for parsing URI strings
hgs
parents:
diff changeset
    15
*
hgs
parents:
diff changeset
    16
*/
hgs
parents:
diff changeset
    17
#include "nmuriparser.h"
hgs
parents:
diff changeset
    18
#include "emailtrace.h"
hgs
parents:
diff changeset
    19
hgs
parents:
diff changeset
    20
#include <QUrl>
hgs
parents:
diff changeset
    21
hgs
parents:
diff changeset
    22
hgs
parents:
diff changeset
    23
/*!
hgs
parents:
diff changeset
    24
    Keys for parsing data.
hgs
parents:
diff changeset
    25
*/
hgs
parents:
diff changeset
    26
static const QString NmUriParserEmailSendSubjectKey = "subject";
hgs
parents:
diff changeset
    27
static const QString NmUriParserEmailEmailSendToKey = "to";
hgs
parents:
diff changeset
    28
static const QString NmUriParserEmailSendCcKey = "cc";
hgs
parents:
diff changeset
    29
static const QString NmUriParserEmailSendBccKey = "bcc";
hgs
parents:
diff changeset
    30
static const QString NmUriParserEmailSendBodyTextKey = "body";
hgs
parents:
diff changeset
    31
hgs
parents:
diff changeset
    32
/*!
hgs
parents:
diff changeset
    33
    Class constructor.
hgs
parents:
diff changeset
    34
*/
hgs
parents:
diff changeset
    35
NmUriParser::NmUriParser()
hgs
parents:
diff changeset
    36
{
hgs
parents:
diff changeset
    37
    NM_FUNCTION;
hgs
parents:
diff changeset
    38
}
hgs
parents:
diff changeset
    39
hgs
parents:
diff changeset
    40
/*!
hgs
parents:
diff changeset
    41
    Class destructor.
hgs
parents:
diff changeset
    42
*/
hgs
parents:
diff changeset
    43
NmUriParser::~NmUriParser()
hgs
parents:
diff changeset
    44
{
hgs
parents:
diff changeset
    45
    NM_FUNCTION;
hgs
parents:
diff changeset
    46
}
hgs
parents:
diff changeset
    47
hgs
parents:
diff changeset
    48
/*!
hgs
parents:
diff changeset
    49
    Extracts the data from the given QString into the class members.
hgs
parents:
diff changeset
    50
    \param data QString containing the data.
hgs
parents:
diff changeset
    51
    \return True if success, false otherwise.
hgs
parents:
diff changeset
    52
*/
hgs
parents:
diff changeset
    53
bool NmUriParser::extractData(const QString &data)
hgs
parents:
diff changeset
    54
{
hgs
parents:
diff changeset
    55
    NM_FUNCTION;
hgs
parents:
diff changeset
    56
    
hgs
parents:
diff changeset
    57
    bool success(false);
hgs
parents:
diff changeset
    58
    
hgs
parents:
diff changeset
    59
    QUrl uri(data);
hgs
parents:
diff changeset
    60
    
hgs
parents:
diff changeset
    61
    if (uri.isValid()) {
hgs
parents:
diff changeset
    62
        
hgs
parents:
diff changeset
    63
        mSubject = uri.queryItemValue(NmUriParserEmailSendSubjectKey);
hgs
parents:
diff changeset
    64
        QString to = uri.path();
hgs
parents:
diff changeset
    65
        QString cc = uri.queryItemValue(NmUriParserEmailSendCcKey);
hgs
parents:
diff changeset
    66
        QString bcc = uri.queryItemValue(NmUriParserEmailSendBccKey);
hgs
parents:
diff changeset
    67
        QString bodyText = uri.queryItemValue(NmUriParserEmailSendBodyTextKey);
hgs
parents:
diff changeset
    68
        if (bodyText.length()) {
hgs
parents:
diff changeset
    69
            mBodyText = bodyText;
hgs
parents:
diff changeset
    70
        }
hgs
parents:
diff changeset
    71
        
hgs
parents:
diff changeset
    72
        addAddressesToList(to, mToAddresses);
hgs
parents:
diff changeset
    73
        addAddressesToList(cc, mCcAddresses);
hgs
parents:
diff changeset
    74
        addAddressesToList(bcc, mBccAddresses);
hgs
parents:
diff changeset
    75
        
hgs
parents:
diff changeset
    76
        success = true;
hgs
parents:
diff changeset
    77
    }
hgs
parents:
diff changeset
    78
    
hgs
parents:
diff changeset
    79
    return success;
hgs
parents:
diff changeset
    80
}
hgs
parents:
diff changeset
    81
hgs
parents:
diff changeset
    82
/*!
hgs
parents:
diff changeset
    83
    Appends the given addresses into the given list.
hgs
parents:
diff changeset
    84
    \param address The addresses to append.
hgs
parents:
diff changeset
    85
    \param list The list where the addresses are appended to.
hgs
parents:
diff changeset
    86
*/
hgs
parents:
diff changeset
    87
void NmUriParser::addAddressesToList(QString &addresses, QStringList &list)
hgs
parents:
diff changeset
    88
{
hgs
parents:
diff changeset
    89
    NM_FUNCTION;
hgs
parents:
diff changeset
    90
    
hgs
parents:
diff changeset
    91
    if (!addresses.isEmpty()) {
hgs
parents:
diff changeset
    92
hgs
parents:
diff changeset
    93
        QStringList foundAddresses;
hgs
parents:
diff changeset
    94
                
hgs
parents:
diff changeset
    95
        // Process multiple addresses.
hgs
parents:
diff changeset
    96
        if (addresses.contains(",")) {
hgs
parents:
diff changeset
    97
            QString str;               
hgs
parents:
diff changeset
    98
            while (addresses.contains(",")) {
hgs
parents:
diff changeset
    99
                str = addresses.section(",", 0, 0); // Get the occurance.
hgs
parents:
diff changeset
   100
                addresses.remove(0, (addresses.indexOf(",")+1)); // Remove the occurance.
hgs
parents:
diff changeset
   101
                if (!str.isEmpty()) { // In case str would be empty on some error data. 
hgs
parents:
diff changeset
   102
                    foundAddresses.append(str);    
hgs
parents:
diff changeset
   103
                }
hgs
parents:
diff changeset
   104
            }
hgs
parents:
diff changeset
   105
        }
hgs
parents:
diff changeset
   106
        if (!addresses.isEmpty()) { // In case addresses would be empty on some error data. 
hgs
parents:
diff changeset
   107
            // Last one or single address.
hgs
parents:
diff changeset
   108
            foundAddresses.append(addresses);
hgs
parents:
diff changeset
   109
        }
hgs
parents:
diff changeset
   110
        // Append the found addresses into the given list.
hgs
parents:
diff changeset
   111
        list.append(foundAddresses);
hgs
parents:
diff changeset
   112
    }
hgs
parents:
diff changeset
   113
}
hgs
parents:
diff changeset
   114
    
hgs
parents:
diff changeset
   115
/*!
hgs
parents:
diff changeset
   116
    Resets the data.
hgs
parents:
diff changeset
   117
*/
hgs
parents:
diff changeset
   118
void NmUriParser::resetData()
hgs
parents:
diff changeset
   119
{
hgs
parents:
diff changeset
   120
    NM_FUNCTION;
hgs
parents:
diff changeset
   121
    
hgs
parents:
diff changeset
   122
    mSubject.clear();
hgs
parents:
diff changeset
   123
    mToAddresses.clear();
hgs
parents:
diff changeset
   124
    mCcAddresses.clear();
hgs
parents:
diff changeset
   125
    mBccAddresses.clear();
hgs
parents:
diff changeset
   126
    mBodyText.clear();
hgs
parents:
diff changeset
   127
}
hgs
parents:
diff changeset
   128
hgs
parents:
diff changeset
   129
/*!
hgs
parents:
diff changeset
   130
    Getter for subject
hgs
parents:
diff changeset
   131
    \return QString subject of parsed uri
hgs
parents:
diff changeset
   132
*/
hgs
parents:
diff changeset
   133
QString NmUriParser::subject() const
hgs
parents:
diff changeset
   134
{
hgs
parents:
diff changeset
   135
    return mSubject;
hgs
parents:
diff changeset
   136
}
hgs
parents:
diff changeset
   137
/*!
hgs
parents:
diff changeset
   138
    Getter for to-addresses
hgs
parents:
diff changeset
   139
    \return QStringList list of parsed to-addresses
hgs
parents:
diff changeset
   140
*/
hgs
parents:
diff changeset
   141
QStringList NmUriParser::toAddresses() const
hgs
parents:
diff changeset
   142
{
hgs
parents:
diff changeset
   143
    return mToAddresses;
hgs
parents:
diff changeset
   144
}
hgs
parents:
diff changeset
   145
/*!
hgs
parents:
diff changeset
   146
    Getter for ccAddresses
hgs
parents:
diff changeset
   147
    \return QStringList list of parsed cc-addresses
hgs
parents:
diff changeset
   148
*/
hgs
parents:
diff changeset
   149
QStringList NmUriParser::ccAddresses() const
hgs
parents:
diff changeset
   150
{
hgs
parents:
diff changeset
   151
    return mCcAddresses;
hgs
parents:
diff changeset
   152
}
hgs
parents:
diff changeset
   153
/*!
hgs
parents:
diff changeset
   154
    Getter for bcc-Addresses
hgs
parents:
diff changeset
   155
    \return QStringList list of parsed bcc-addresses
hgs
parents:
diff changeset
   156
*/
hgs
parents:
diff changeset
   157
QStringList NmUriParser::bccAddresses() const
hgs
parents:
diff changeset
   158
{
hgs
parents:
diff changeset
   159
    return mBccAddresses;
hgs
parents:
diff changeset
   160
}
hgs
parents:
diff changeset
   161
/*!
hgs
parents:
diff changeset
   162
    Getter for bcc-Addresses
hgs
parents:
diff changeset
   163
    \return QString parsed body text
hgs
parents:
diff changeset
   164
*/
hgs
parents:
diff changeset
   165
QString NmUriParser::bodyText() const
hgs
parents:
diff changeset
   166
{
hgs
parents:
diff changeset
   167
    return mBodyText;
hgs
parents:
diff changeset
   168
}