emailuis/nmailui/src/nmutilities.cpp
changeset 53 bf7eb7911fc5
parent 30 759dc5235cdb
child 47 f83bd4ae1fe3
equal deleted inserted replaced
30:759dc5235cdb 53:bf7eb7911fc5
    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 static const int NmShortInterval = 1000; // 1 sec
       
    22 
    21 
    23 // taken from http://www.regular-expressions.info/email.html
    22 // taken from http://www.regular-expressions.info/email.html
    24 static const QRegExp EmailAddressPattern("[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
    23 static const QRegExp EmailAddressPattern("[A-Za-z\\d!#$%&'*+/=?^_`{|}~-]+"
    25                                          "(?:"
    24                                          "(?:"
    26                                          "\\."
    25                                          "\\."
    46 void NmUtilities::getRecipientsFromMessage( const NmMessage &message,
    45 void NmUtilities::getRecipientsFromMessage( const NmMessage &message,
    47     QList<NmAddress> &recipients,
    46     QList<NmAddress> &recipients,
    48     NmAddressValidationType type )
    47     NmAddressValidationType type )
    49 {
    48 {
    50     NM_FUNCTION;
    49     NM_FUNCTION;
    51     
    50 
    52     // validate TO addresses
    51     // validate TO addresses
    53     QList<NmAddress> toRecipients = message.envelope().toRecipients();
    52     QList<NmAddress> toRecipients = message.envelope().toRecipients();
    54     int recipientCount = toRecipients.count();
    53     int recipientCount = toRecipients.count();
    55 
    54 
    56     for (int i = 0; i < recipientCount; ++i) {
    55     for (int i = 0; i < recipientCount; ++i) {
    96   Validates the given string against an email address pattern.
    95   Validates the given string against an email address pattern.
    97 */
    96 */
    98 bool NmUtilities::isValidEmailAddress( const QString &emailAddress )
    97 bool NmUtilities::isValidEmailAddress( const QString &emailAddress )
    99 {
    98 {
   100     NM_FUNCTION;
    99     NM_FUNCTION;
   101     
   100 
   102     return EmailAddressPattern.exactMatch(emailAddress);
   101     return EmailAddressPattern.exactMatch(emailAddress);
   103 }
   102 }
   104 
   103 
   105 /*!
   104 /*!
   106   Generates a display string from an NmAddress object.
   105   Generates a display string from an NmAddress object.
   107 */
   106 */
   108 QString NmUtilities::addressToDisplayName( const NmAddress &address )
   107 QString NmUtilities::addressToDisplayName( const NmAddress &address )
   109 {
   108 {
   110     NM_FUNCTION;
   109     NM_FUNCTION;
   111     
   110 
   112     QString emailAddress = address.address();
   111     QString emailAddress = address.address();
   113     QString displayName = address.displayName();
   112     QString displayName = address.displayName();
   114 
   113 
   115     QString ret;
   114     QString ret;
   116     if (displayName.length() > 0 && displayName != emailAddress) {
   115     if (displayName.length() > 0 && displayName != emailAddress) {
   126   Returns an NmAddress object that is parsed from a display name string.
   125   Returns an NmAddress object that is parsed from a display name string.
   127 */
   126 */
   128 bool NmUtilities::parseEmailAddress( const QString &emailAddress, NmAddress &address )
   127 bool NmUtilities::parseEmailAddress( const QString &emailAddress, NmAddress &address )
   129 {
   128 {
   130     NM_FUNCTION;
   129     NM_FUNCTION;
   131     
   130 
   132     bool foundAddress = false;
   131     bool foundAddress = false;
   133 
   132 
   134     QRegExp rx(EmailAddressPattern);
   133     QRegExp rx(EmailAddressPattern);
   135     // locate the email address in the string
   134     // locate the email address in the string
   136     int pos = rx.indexIn(emailAddress);
   135     int pos = rx.indexIn(emailAddress);
   154   Cleans up display name by stripping extra characters from the beginning and end of the string.
   153   Cleans up display name by stripping extra characters from the beginning and end of the string.
   155 */
   154 */
   156 QString NmUtilities::cleanupDisplayName( const QString &displayName )
   155 QString NmUtilities::cleanupDisplayName( const QString &displayName )
   157 {
   156 {
   158     NM_FUNCTION;
   157     NM_FUNCTION;
   159     
   158 
   160     // find the first and last position that is NOT one of the characters below
   159     // find the first and last position that is NOT one of the characters below
   161     QRegExp rx("[^\\s\"<>]");
   160     QRegExp rx("[^\\s\"<>]");
   162     int firstPos = std::max(rx.indexIn(displayName), 0);
   161     int firstPos = std::max(rx.indexIn(displayName), 0);
   163     int lastPos = rx.lastIndexIn(displayName);
   162     int lastPos = rx.lastIndexIn(displayName);
   164 
   163 
   168 
   167 
   169     return displayName.mid(firstPos, lastPos - firstPos + 1);
   168     return displayName.mid(firstPos, lastPos - firstPos + 1);
   170 }
   169 }
   171 
   170 
   172 /*!
   171 /*!
   173   Opens file specified by QFile handle. Usually used by editor
   172   Opens file specified by RFile handle. Usually used by viewer
   174   for opening added attachments
   173   for opening attachments from message store as RFiles
   175 */
   174 */
   176 int NmUtilities::openFile(QFile &file)
   175 int NmUtilities::openFile(XQSharableFile &file)
   177 {
   176 {
   178     NM_FUNCTION;
   177     NM_FUNCTION;
   179     
   178 
   180     int ret(NmNotFoundError);
   179     int ret(NmNotFoundError);
   181     XQApplicationManager aiwMgr;
   180     XQApplicationManager aiwMgr;
   182     XQAiwRequest *request(NULL);
   181     XQAiwRequest *request(NULL);
   183     request = aiwMgr.create(file);
   182     request = aiwMgr.create(file);
   184     // If request is created then there is a handler for that file
       
   185     if (request) {
       
   186          // Set request arguments
       
   187          QList<QVariant> args;
       
   188          args << file.fileName();
       
   189          request->setArguments(args);
       
   190          // Send the request, ownership of request is transferred
       
   191          bool res = request->send();
       
   192          if (res) {
       
   193              // Request ok, set error status.
       
   194              ret = NmNoError;
       
   195          }
       
   196     }
       
   197     delete request;
       
   198     return ret;
       
   199 }
       
   200 
       
   201 
       
   202 /*!
       
   203   Opens file specified by RFile handle. Usually used by viewer
       
   204   for opening attachments from message store as RFiles
       
   205 */
       
   206 int NmUtilities::openFile(XQSharableFile &file)
       
   207 {
       
   208     NM_FUNCTION;
       
   209     
       
   210     int ret(NmNotFoundError);
       
   211     XQApplicationManager aiwMgr;
       
   212     XQAiwRequest *request(NULL);
       
   213     request = aiwMgr.create(file);  
       
   214     // Create request for the sharable file
   183     // Create request for the sharable file
   215     if (request)
   184     if (request)
   216     {
   185     {
   217         // Set request arguments
   186         // Set request arguments
   218         QList<QVariant> args;
   187         QList<QVariant> args;
   219         args << qVariantFromValue(file);  
   188         args << qVariantFromValue(file);
   220         request->setArguments(args);
   189         request->setArguments(args);
   221         // Send the request, ownership of request is transferred
   190         // Send the request, ownership of request is transferred
   222         bool res = request->send();
   191         bool res = request->send();
   223         if (res) {
   192         if (res) {
   224         // Request ok, set error status.
   193         // Request ok, set error status.
   233  * the string, ellipses are added to the end of the string.
   202  * the string, ellipses are added to the end of the string.
   234  */
   203  */
   235 QString NmUtilities::truncate( const QString &string, int length )
   204 QString NmUtilities::truncate( const QString &string, int length )
   236 {
   205 {
   237     NM_FUNCTION;
   206     NM_FUNCTION;
   238     
   207 
   239     if (string.length() <= length) {
   208     if (string.length() <= length) {
   240         return string;
   209         return string;
   241     }
   210     }
   242 
   211 
   243     QString padding = "...";
   212     QString padding = "...";
   245     return string.mid(0, length - padding.length()) + padding;
   214     return string.mid(0, length - padding.length()) + padding;
   246 }
   215 }
   247 
   216 
   248 /*!
   217 /*!
   249  * Shows an error note. Used by at least editor and viewer classes.
   218  * Shows an error note. Used by at least editor and viewer classes.
   250  * 
   219  *
   251  */
   220  */
   252 void NmUtilities::displayErrorNote(QString noteText)
   221 void NmUtilities::displayErrorNote(QString noteText)
   253 {
   222 {
   254     NM_FUNCTION;
   223     NM_FUNCTION;
   255     
   224 
   256 	HbNotificationDialog *note = new HbNotificationDialog();
   225 	HbNotificationDialog *note = new HbNotificationDialog();
   257 	
   226 
   258 	note->setIcon(HbIcon(QLatin1String("note_warning")));
   227 	note->setIcon(HbIcon(QLatin1String("note_warning")));
   259 	note->setTitle(noteText);
   228 	note->setTitle(noteText);
   260 	note->setTitleTextWrapping(Hb::TextWordWrap);
   229 	note->setTitleTextWrapping(Hb::TextWordWrap);
   261 	note->setDismissPolicy(HbPopup::TapAnywhere);
   230 	note->setDismissPolicy(HbPopup::TapAnywhere);
   262 	note->setAttribute(Qt::WA_DeleteOnClose);
   231 	note->setAttribute(Qt::WA_DeleteOnClose);
   270     on attachment size in bytes
   239     on attachment size in bytes
   271  */
   240  */
   272 QString NmUtilities::attachmentSizeString(const int sizeInBytes)
   241 QString NmUtilities::attachmentSizeString(const int sizeInBytes)
   273 {
   242 {
   274     NM_FUNCTION;
   243     NM_FUNCTION;
   275     
   244 
   276     qreal sizeMb = (qreal)sizeInBytes / (qreal)NmMegabyte;
   245     qreal sizeMb = (qreal)sizeInBytes / (qreal)NmMegabyte;
   277     if (sizeMb < 0.1) {
   246     if (sizeMb < 0.1) {
   278         // 0.1 Mb is the minimum size shown for attachment
   247         // 0.1 Mb is the minimum size shown for attachment
   279         sizeMb = 0.1;
   248         sizeMb = 0.1;
   280     }
   249     }
   281     return QString().sprintf("(%.1f Mb)", sizeMb); // Use loc string when available    
   250     return QString().sprintf("(%.1f Mb)", sizeMb); // Use loc string when available
   282 }
   251 }
   283 
   252 
   284 /*!
   253 /*!
   285     Displays a note with Yes/No buttons. Note has no timeout, i.e. it has to be dismissed manually.
   254     Displays a note with Yes/No buttons. Note has no timeout, i.e. it has to be dismissed manually.
   286     Returns pointer to dialog so that caller can take ownership and handle deletion.
   255     Returns pointer to dialog so that caller can take ownership and handle deletion.
   287     Parameter 'receiver' is the object and 'member' is the slot where user selection is passed. 
   256     Parameter 'receiver' is the object and 'member' is the slot where user selection is passed.
   288 */
   257 */
   289 HbMessageBox* NmUtilities::displayQuestionNote(
   258 HbMessageBox* NmUtilities::displayQuestionNote(
   290     QString noteText, QObject* receiver, const char* member)
   259     QString noteText, QObject* receiver, const char* member)
   291 {
   260 {
   292     NM_FUNCTION;
   261     NM_FUNCTION;
   293     
   262 
   294     HbMessageBox *messageBox = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
   263     HbMessageBox *messageBox = new HbMessageBox(HbMessageBox::MessageTypeQuestion);
   295     messageBox->setText(noteText);
   264     messageBox->setText(noteText);
   296     messageBox->setTimeout(HbMessageBox::NoTimeout); // Note has to be dismissed manually
   265     messageBox->setTimeout(HbMessageBox::NoTimeout); // Note has to be dismissed manually
   297     messageBox->open(receiver, member);
   266     messageBox->open(receiver, member);
   298     return messageBox;
   267     return messageBox;
   299 }
   268 }
   300 
   269 
   301 /*!
   270 /*!
   302  * displays an error note with no buttons. Note dismisses itself after NmShortInterval.
   271  * displays an warning note.
   303  */
   272  */
   304 void NmUtilities::displayWarningNote(QString noteText)
   273 HbMessageBox* NmUtilities::displayWarningNote(QString noteText, QObject* receiver, const char* member)
   305 {
   274 {
   306     NM_FUNCTION;
   275     NM_FUNCTION;
   307     
   276 
   308     HbMessageBox *messageBox = new HbMessageBox(HbMessageBox::MessageTypeWarning);
   277     HbMessageBox *messageBox = new HbMessageBox(HbMessageBox::MessageTypeWarning);
   309     messageBox->setText(noteText);
   278     messageBox->setText(noteText);
   310     messageBox->setTimeout(NmShortInterval);
   279     messageBox->setTimeout(HbMessageBox::NoTimeout); // Note has to be dismissed manually
   311     messageBox->clearActions(); // gets rid of buttons from the note
   280     messageBox->open(receiver, member);
   312     messageBox->setModal(false);
   281     return messageBox;
   313     messageBox->setBackgroundFaded(false);
       
   314     messageBox->show();
       
   315 
       
   316     delete messageBox;
       
   317 }
   282 }
   318 
   283 
   319 /*!
   284 /*!
   320     Function returns localized "Original message" header
   285     Function returns localized "Original message" header
   321     in html format based on envelope
   286     in html format based on envelope
   322 */
   287 */
   323 QString NmUtilities::createReplyHeader(const NmMessageEnvelope &env)
   288 QString NmUtilities::createReplyHeader(const NmMessageEnvelope &env)
   324 {
   289 {
   325     NM_FUNCTION;
   290     NM_FUNCTION;
   326     
   291 
   327     QString ret = "<html><body><br><br>";
   292     QString ret = "<html><body><br><br>";
   328     // Append "----- Original message ----" text
   293     // Append "----- Original message ----" text
   329     ret+=hbTrId("txt_mail_editor_reply_original_msg");                  
   294     ret+=hbTrId("txt_mail_editor_reply_original_msg");
   330     // Append sender
   295     // Append sender
   331     ret+="<br>";
   296     ret+="<br>";
   332     ret+=hbTrId("txt_mail_editor_reply_from");               
   297     ret+=hbTrId("txt_mail_editor_reply_from");
   333     ret+=" ";        
   298     ret+=" ";
   334     if (env.sender().displayName().length()){
   299     if (env.sender().displayName().length()){
   335         ret+=env.sender().displayName();
   300         ret+=env.sender().displayName();
   336     }
   301     }
   337     else{
   302     else{
   338         ret+=env.sender().address();
   303         ret+=env.sender().address();
   339     }   
   304     }
   340     // Append sent time
   305     // Append sent time
   341     ret+="<br>";
   306     ret+="<br>";
   342     ret+=hbTrId("txt_mail_editor_reply_sent");   
   307     ret+=hbTrId("txt_mail_editor_reply_sent");
   343     ret+=" ";  
   308     ret+=" ";
   344     HbExtendedLocale locale = HbExtendedLocale::system();
   309     HbExtendedLocale locale = HbExtendedLocale::system();
   345     QDate sentLocalDate = env.sentTime().toLocalTime().date();
   310     QDateTime localTime = env.sentTime().addSecs(locale.universalTimeOffset());
   346     ret+=locale.format(sentLocalDate, r_qtn_date_usual);   
   311     QDate sentLocalDate = localTime.date();
       
   312     ret+=locale.format(sentLocalDate, r_qtn_date_usual);
   347     // Append to recipients
   313     // Append to recipients
   348     const QList<NmAddress> &toList = env.toRecipients();
   314     const QList<NmAddress> &toList = env.toRecipients();
   349     if (toList.count()){
   315     if (toList.count()){
   350         ret+="<br>";
   316         ret+="<br>";
   351         ret+=hbTrId("txt_mail_editor_reply_to"); 
   317         ret+=hbTrId("txt_mail_editor_reply_to");
   352         ret+=" ";    
   318         ret+=" ";
   353         for (int i=0;i<toList.count();i++){
   319         for (int i=0;i<toList.count();i++){
   354             if (toList[i].displayName().length()){
   320             if (toList[i].displayName().length()){
   355                 ret+=toList[i].displayName();
   321                 ret+=toList[i].displayName();
   356             }
   322             }
   357             else{
   323             else{
   358                 ret+=toList[i].address();
   324                 ret+=toList[i].address();
   359             }
   325             }
   360             if (i!=toList.count()-1){
   326             if (i!=toList.count()-1){
   361                 ret+=";";          
   327                 ret+=";";
   362             }
   328             }
   363         }    
   329         }
   364     }
   330     }
   365     // Append cc recipients
   331     // Append cc recipients
   366     const QList<NmAddress> &ccList = env.ccRecipients();
   332     const QList<NmAddress> &ccList = env.ccRecipients();
   367     if (ccList.count()){
   333     if (ccList.count()){
   368         ret+="<br>";
   334         ret+="<br>";
   369         ret+=hbTrId("txt_mail_editor_reply_cc"); 
   335         ret+=hbTrId("txt_mail_editor_reply_cc");
   370         ret+=" ";         
   336         ret+=" ";
   371         for (int i=0;i<ccList.count();i++){
   337         for (int i=0;i<ccList.count();i++){
   372             if (ccList[i].displayName().length()){
   338             if (ccList[i].displayName().length()){
   373                 ret+=ccList[i].displayName();
   339                 ret+=ccList[i].displayName();
   374             }
   340             }
   375             else{
   341             else{
   376                 ret+=ccList[i].address();
   342                 ret+=ccList[i].address();
   377             }
   343             }
   378             if (i!=toList.count()-1){
   344             if (i!=toList.count()-1){
   379                 ret+=";";          
   345                 ret+=";";
   380             }
   346             }
   381         }    
   347         }
   382     }
   348     }
   383     // Append subject if there is subject to display
   349     // Append subject if there is subject to display
   384     if (env.subject().length()){
   350     if (env.subject().length()){
   385         ret+="<br>";
   351         ret+="<br>";
   386         ret+=hbTrId("txt_mail_editor_reply_subject"); 
   352         ret+=hbTrId("txt_mail_editor_reply_subject");
   387         ret+=" ";    
   353         ret+=" ";
   388         ret+=env.subject();    
   354         ret+=env.subject();
   389     }
   355     }
   390     // Append priority if it is other than normal  
   356     // Append priority if it is other than normal
   391     if (env.priority()!=NmMessagePriorityNormal){   
   357     if (env.priority()!=NmMessagePriorityNormal){
   392         ret+="<br>";
   358         ret+="<br>";
   393         ret+=hbTrId("txt_mail_editor_reply_importance"); 
   359         ret+=hbTrId("txt_mail_editor_reply_importance");
   394         ret+=" ";    
   360         ret+=" ";
   395         if (env.priority()==NmMessagePriorityLow){
   361         if (env.priority()==NmMessagePriorityLow){
   396             ret+=hbTrId("txt_mail_editor_reply_importance_low");         
   362             ret+=hbTrId("txt_mail_editor_reply_importance_low");
   397         }
   363         }
   398         else {
   364         else {
   399             ret+=hbTrId("txt_mail_editor_reply_importance_high"); 
   365             ret+=hbTrId("txt_mail_editor_reply_importance_high");
   400         }
   366         }
   401     }    
   367     }
   402     ret+="<br></body></html>";
   368     ret+="<br></body></html>";
   403     return ret;
   369     return ret;
   404 }
   370 }