emailuis/nmailui/src/nmutilities.cpp
changeset 74 6c59112cfd31
parent 68 83cc6bae1de8
equal deleted inserted replaced
69:4e54af54a4a1 74:6c59112cfd31
    37                                          "(?:"
    37                                          "(?:"
    38                                          "[A-Za-z\\d-]*[A-Za-z\\d]"
    38                                          "[A-Za-z\\d-]*[A-Za-z\\d]"
    39                                          ")?"
    39                                          ")?"
    40                                         );
    40                                         );
    41 
    41 
       
    42 static const QRegExp NmEmailDisplayNamePattern("<"+NmEmailAddressPattern.pattern()+">$");
       
    43 static const QRegExp NmEmailGreaterThenLessThen(">|<");
    42 /*!
    44 /*!
    43    Gets valid, invalid or all the recipients from a message
    45    Gets valid, invalid or all the recipients from a message
    44  */
    46  */
    45 void NmUtilities::getRecipientsFromMessage( const NmMessage &message,
    47 void NmUtilities::getRecipientsFromMessage( const NmMessage &message,
    46     QList<NmAddress> &recipients,
    48     QList<NmAddress> &recipients,
   173 
   175 
   174 /*!
   176 /*!
   175   Opens file specified by XQSharableFile handle. Usually used by viewer
   177   Opens file specified by XQSharableFile handle. Usually used by viewer
   176   for opening attachments from message store as RFiles
   178   for opening attachments from message store as RFiles
   177 */
   179 */
   178 int NmUtilities::openFile(XQSharableFile &file)
   180 int NmUtilities::openFile(XQSharableFile &file, QObject* listener)
   179 {
   181 {
   180     NM_FUNCTION;
   182     NM_FUNCTION;
   181 
   183 
   182     int ret(NmNotFoundError);
   184     int ret(NmNotFoundError);
   183     XQApplicationManager aiwMgr;
   185     XQApplicationManager aiwMgr;
   184     XQAiwRequest *request(NULL);
   186     XQAiwRequest *request(NULL);
   185     request = aiwMgr.create(file);
   187     request = aiwMgr.create(file);
   186     // Create request for the sharable file
   188     // Create request for the sharable file
   187     if (request)
   189     if (request)
   188     {
   190     {
       
   191         QObject::connect(request, SIGNAL(requestOk(const QVariant&)),
       
   192                             listener, SLOT(fileOpenCompleted(const QVariant&)));
       
   193         QObject::connect(request, SIGNAL(requestError(int, const QString&)),
       
   194                                     listener, SLOT(fileOpenError(int, const QString&)));
   189         // Set request arguments
   195         // Set request arguments
   190         QList<QVariant> args;
   196         QList<QVariant> args;
   191         args << qVariantFromValue(file);
   197         args << qVariantFromValue(file);
   192         request->setArguments(args);
   198         request->setArguments(args);
       
   199         request->setSynchronous(false);
   193         // Send the request, ownership of request is transferred
   200         // Send the request, ownership of request is transferred
   194         bool res = request->send();
   201         bool res = request->send();
   195         if (res) {
   202         if (res) {
   196         // Request ok, set error status.
   203         // Request ok, set error status.
   197         ret = NmNoError;
   204         ret = NmNoError;
   198         }
   205         }
   199     }
   206     }
   200     return ret;
   207     return ret;
       
   208 }
       
   209 
       
   210 /*!
       
   211    Opens an attachment part via QtHighway. Message part object that is passed
       
   212    to this function must contain binary content, because it is written to a
       
   213    temporary file. List passed is appended with the new temporary filename
       
   214    that is opened. This can be then used to delete the temporary files created
       
   215    with for example NmUtilities::deleteTempFiles function.
       
   216    
       
   217    \param part attachment message part which should contain binary content.
       
   218    \param tmpFiles list of filenames that is appended with new one.
       
   219    \param listener XQAiwRequest listener for listening request send completion
       
   220    \retun int NmGeneralError, if the above requirements aren't filled. Also
       
   221               if file type isn't supported.
       
   222 */
       
   223 int NmUtilities::openAttachment(NmMessagePart *part, 
       
   224                                 QList<QString> &tmpFiles,
       
   225                                 QObject* listener)
       
   226 {
       
   227     int ret(NmGeneralError);
       
   228 
       
   229     if (part) {
       
   230         // Create a temp file that points to system temp folder.
       
   231         QFile file(QDir::tempPath()+QDir::separator()+part->attachmentName());
       
   232             
       
   233         if (file.open(QIODevice::ReadWrite)) {
       
   234             // Write content to file and close it so it can be used by other
       
   235             // processes.
       
   236             if (file.write(part->binaryContent()) > 0) {
       
   237                 file.close();
       
   238                 
       
   239                 // Create a request from the file.
       
   240                 XQApplicationManager aiwMgr;
       
   241                 XQAiwRequest *request = aiwMgr.create(file);
       
   242                 if (request)
       
   243                 {
       
   244                     QObject::connect(request, SIGNAL(requestOk(const QVariant&)),
       
   245                                         listener, SLOT(fileOpenCompleted(const QVariant&)));
       
   246                     QObject::connect(request, SIGNAL(requestError(int, const QString&)),
       
   247                                         listener, SLOT(fileOpenError(int, const QString&)));
       
   248                     // Set request argument.
       
   249                     QList<QVariant> args;
       
   250                     args << file.fileName();
       
   251                     request->setArguments(args);
       
   252                     if (listener) {
       
   253                         request->setSynchronous(false);
       
   254                     }
       
   255                     // Send the request
       
   256                     bool res = request->send();
       
   257                     if (res) {
       
   258                         // Request ok, set error status.
       
   259                         ret = NmNoError;
       
   260                     }
       
   261                 }
       
   262                 if (!listener) {
       
   263                     delete request;    
       
   264                 }
       
   265             }
       
   266             tmpFiles.append(file.fileName());
       
   267         }
       
   268     }
       
   269     return ret;
       
   270 }
       
   271 
       
   272 /*
       
   273  * Deletes files represented in the given list. Also clears
       
   274  * the list after deletion.
       
   275  * 
       
   276  * \param tmpFiles list of files to be removed.
       
   277  */
       
   278 void NmUtilities::deleteTempFiles(QList<QString> &tmpFiles)
       
   279 {
       
   280     foreach (QString fileName,tmpFiles) {
       
   281         QFile::remove(fileName);
       
   282     }
       
   283     tmpFiles.clear();
   201 }
   284 }
   202 
   285 
   203 /*!
   286 /*!
   204  * Truncate a string to a specific length. If length is less than
   287  * Truncate a string to a specific length. If length is less than
   205  * the string, ellipses are added to the end of the string.
   288  * the string, ellipses are added to the end of the string.
   397     else {
   480     else {
   398         nmAddress = new NmAddress(str);
   481         nmAddress = new NmAddress(str);
   399     }
   482     }
   400     return nmAddress;
   483     return nmAddress;
   401 }
   484 }
       
   485 
       
   486 /*
       
   487  * Function creates a list of NmAddress objects from a string list, that contains email addresses.
       
   488  * \param const QStringList &strlist list of email address strings
       
   489  * \return QList<NmAddress*> *NmAddresses list of NmAddress objects. Returns NULL if strlist does
       
   490  * not contain strings.
       
   491  */
       
   492 QList<NmAddress*> *NmUtilities::qstringListToNmAddressList(const QStringList &strlist)
       
   493 {
       
   494     QList<NmAddress*> *addresses = NULL;
       
   495     if(!strlist.isEmpty()) {
       
   496         addresses = new QList<NmAddress*>;
       
   497         QString tempString;
       
   498         foreach(tempString, strlist) {
       
   499             NmAddress *address = new NmAddress(tempString);
       
   500             addresses->append(address);
       
   501         }
       
   502     }
       
   503     return addresses;
       
   504 }