emailuis/nmailui/src/nmutilities.cpp
changeset 54 997a02608b3a
parent 53 bf7eb7911fc5
child 51 d845db10c0d4
equal deleted inserted replaced
53:bf7eb7911fc5 54:997a02608b3a
    16 */
    16 */
    17 
    17 
    18 #include "nmuiheaders.h"
    18 #include "nmuiheaders.h"
    19 
    19 
    20 static const int NmMegabyte = 1048576;
    20 static const int NmMegabyte = 1048576;
    21 
    21 static const qreal NmMinAttachmentSize = 0.1;
    22 // taken from http://www.regular-expressions.info/email.html
    22 // taken from http://www.regular-expressions.info/email.html
    23 static const QRegExp EmailAddressPattern("[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
    23 static const QRegExp NmEmailAddressPattern("[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
    24                                          "(?:"
    24                                          "(?:"
    25                                          "\\."
    25                                          "\\."
    26                                          "[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
    26                                          "[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
    27                                          ")*"
    27                                          ")*"
    28                                          "@"
    28                                          "@"
    46     QList<NmAddress> &recipients,
    46     QList<NmAddress> &recipients,
    47     NmAddressValidationType type )
    47     NmAddressValidationType type )
    48 {
    48 {
    49     NM_FUNCTION;
    49     NM_FUNCTION;
    50 
    50 
       
    51     // Get envelope from message
       
    52     const NmMessageEnvelope &env = message.envelope();
       
    53     
    51     // validate TO addresses
    54     // validate TO addresses
    52     QList<NmAddress> toRecipients = message.envelope().toRecipients();
    55     QList<NmAddress> toRecipients = env.toRecipients();
    53     int recipientCount = toRecipients.count();
    56     int recipientCount = toRecipients.count();
    54 
    57 
    55     for (int i = 0; i < recipientCount; ++i) {
    58     for (int i = 0; i < recipientCount; ++i) {
    56         bool validAddress = isValidEmailAddress(toRecipients.at(i).address());
    59         bool validAddress = isValidEmailAddress(toRecipients.at(i).address());
    57 
    60 
    58         if (type == Default ||
    61         if (type == NmDefault ||
    59             type == ValidAddress && validAddress ||
    62             type == NmValidAddress && validAddress ||
    60             type == InvalidAddress && !validAddress) {
    63             type == NmInvalidAddress && !validAddress) {
    61             recipients.append(toRecipients.at(i));
    64             recipients.append(toRecipients.at(i));
    62         }
    65         }
    63     }
    66     }
    64 
    67 
    65     // validate CC addresses
    68     // validate CC addresses
    66     QList<NmAddress> ccRecipients = message.envelope().ccRecipients();
    69     QList<NmAddress> ccRecipients = env.ccRecipients();
    67     recipientCount = ccRecipients.count();
    70     recipientCount = ccRecipients.count();
    68 
    71 
    69     for (int i = 0; i < recipientCount; ++i) {
    72     for (int i = 0; i < recipientCount; ++i) {
    70         bool validAddress = isValidEmailAddress(ccRecipients.at(i).address());
    73         bool validAddress = isValidEmailAddress(ccRecipients.at(i).address());
    71 
    74 
    72         if (type == Default ||
    75         if (type == NmDefault ||
    73             type == ValidAddress && validAddress ||
    76             type == NmValidAddress && validAddress ||
    74             type == InvalidAddress && !validAddress) {
    77             type == NmInvalidAddress && !validAddress) {
    75             recipients.append(ccRecipients.at(i));
    78             recipients.append(ccRecipients.at(i));
    76         }
    79         }
    77     }
    80     }
    78 
    81 
    79     // validate BCC addresses
    82     // validate BCC addresses
    80     QList<NmAddress> bccRecipients = message.envelope().bccRecipients();
    83     QList<NmAddress> bccRecipients = env.bccRecipients();
    81     recipientCount = bccRecipients.count();
    84     recipientCount = bccRecipients.count();
    82 
    85 
    83     for (int i = 0; i < recipientCount; ++i) {
    86     for (int i = 0; i < recipientCount; ++i) {
    84         bool validAddress = isValidEmailAddress(bccRecipients.at(i).address());
    87         bool validAddress = isValidEmailAddress(bccRecipients.at(i).address());
    85 
    88 
    86         if (type == Default ||
    89         if (type == NmDefault ||
    87             type == ValidAddress && validAddress ||
    90             type == NmValidAddress && validAddress ||
    88             type == InvalidAddress && !validAddress) {
    91             type == NmInvalidAddress && !validAddress) {
    89             recipients.append(bccRecipients.at(i));
    92             recipients.append(bccRecipients.at(i));
    90         }
    93         }
    91     }
    94     }
    92 }
    95 }
    93 
    96 
    96 */
    99 */
    97 bool NmUtilities::isValidEmailAddress( const QString &emailAddress )
   100 bool NmUtilities::isValidEmailAddress( const QString &emailAddress )
    98 {
   101 {
    99     NM_FUNCTION;
   102     NM_FUNCTION;
   100 
   103 
   101     return EmailAddressPattern.exactMatch(emailAddress);
   104     return NmEmailAddressPattern.exactMatch(emailAddress);
   102 }
   105 }
   103 
   106 
   104 /*!
   107 /*!
   105   Generates a display string from an NmAddress object.
   108   Generates a display string from an NmAddress object.
   106 */
   109 */
   126 */
   129 */
   127 bool NmUtilities::parseEmailAddress( const QString &emailAddress, NmAddress &address )
   130 bool NmUtilities::parseEmailAddress( const QString &emailAddress, NmAddress &address )
   128 {
   131 {
   129     NM_FUNCTION;
   132     NM_FUNCTION;
   130 
   133 
   131     bool foundAddress = false;
   134     bool foundAddress(false);
   132 
   135 
   133     QRegExp rx(EmailAddressPattern);
   136     QRegExp rx(NmEmailAddressPattern);
   134     // locate the email address in the string
   137     // locate the email address in the string
   135     int pos = rx.indexIn(emailAddress);
   138     int pos = rx.indexIn(emailAddress);
   136     if (pos != -1) {
   139     if (pos != -1) {
   137         // extract the email address...
   140         // extract the email address...
   138         int matchedLen = rx.matchedLength();
   141         int matchedLen = rx.matchedLength();
   167 
   170 
   168     return displayName.mid(firstPos, lastPos - firstPos + 1);
   171     return displayName.mid(firstPos, lastPos - firstPos + 1);
   169 }
   172 }
   170 
   173 
   171 /*!
   174 /*!
   172   Opens file specified by RFile handle. Usually used by viewer
   175   Opens file specified by XQSharableFile handle. Usually used by viewer
   173   for opening attachments from message store as RFiles
   176   for opening attachments from message store as RFiles
   174 */
   177 */
   175 int NmUtilities::openFile(XQSharableFile &file)
   178 int NmUtilities::openFile(XQSharableFile &file)
   176 {
   179 {
   177     NM_FUNCTION;
   180     NM_FUNCTION;
   241 QString NmUtilities::attachmentSizeString(const int sizeInBytes)
   244 QString NmUtilities::attachmentSizeString(const int sizeInBytes)
   242 {
   245 {
   243     NM_FUNCTION;
   246     NM_FUNCTION;
   244 
   247 
   245     qreal sizeMb = (qreal)sizeInBytes / (qreal)NmMegabyte;
   248     qreal sizeMb = (qreal)sizeInBytes / (qreal)NmMegabyte;
   246     if (sizeMb < 0.1) {
   249     if (sizeMb < NmMinAttachmentSize) {
   247         // 0.1 Mb is the minimum size shown for attachment
   250         // NmMinAttachmentSize (0.1Mb) is the minimum size shown for attachment
   248         sizeMb = 0.1;
   251         sizeMb = NmMinAttachmentSize;
   249     }
   252     }   
   250     return QString().sprintf("(%.1f Mb)", sizeMb); // Use loc string when available
   253     return QString().sprintf("(%.1f Mb)", sizeMb); // Use loc string when available
   251 }
   254 }
   252 
   255 
   253 /*!
   256 /*!
   254     Displays a note with Yes/No buttons. Note has no timeout, i.e. it has to be dismissed manually.
   257     Displays a note with Yes/No buttons. Note has no timeout, i.e. it has to be dismissed manually.