Modify msg id list delete.
authorKeebum Kim <keebum.kim@samsung.com>
Wed, 5 Jun 2013 05:35:21 +0000 (14:35 +0900)
committerKeebum Kim <keebum.kim@samsung.com>
Wed, 5 Jun 2013 05:35:21 +0000 (14:35 +0900)
Change-Id: I51fd6412e00fe80472e6739a139d08595b1b18e9

framework/storage-handler/MsgStorageMessage.cpp
include/utils/MsgSqliteWrapper.h
packaging/msg-service.spec

index d631c32..ff8d5f1 100755 (executable)
@@ -179,17 +179,18 @@ msg_error_t MsgStoAddMessage(MSG_MESSAGE_INFO_S *pMsg, MSG_SENDINGOPT_INFO_S *pS
 
                        err = plg->addMessage(pMsg, pSendOptInfo, pFileData);
 
-                       if (err != MSG_SUCCESS)
+                       if (err != MSG_SUCCESS) {
                                return MSG_ERR_STORAGE_ERROR;
+                       }
 
                        if (pMsg->msgType.subType == MSG_SENDREQ_MMS) {
                                MSG_DEBUG("pMsg->msgText: %s, pMsg->thumbPath: %s ", pMsg->msgText, pMsg->thumbPath);
 
                                err = MsgStoUpdateMMSMessage(pMsg);
 
-                               if (err != MSG_SUCCESS)
+                               if (err != MSG_SUCCESS) {
                                        return MSG_ERR_STORAGE_ERROR;
-
+                               }
                        }
                }
        }
@@ -1123,11 +1124,16 @@ FREE_MEMORY:
 msg_error_t MsgStoDeleteMessageByList(msg_id_list_s *pMsgIdList)
 {
        MSG_BEGIN();
+
        msg_error_t err = MSG_SUCCESS;
+       if (pMsgIdList->nCount < 1) {
+               MSG_DEBUG("pMsgIdList->nCount < 1");
+               return err;
+       }
+
+       MSG_DEBUG("pMsgIdList->nCount [%d]", pMsgIdList->nCount);
 
        char sqlQuery[MAX_QUERY_LEN+1];
-       char whereQuery[MAX_QUERY_LEN+1];
-       char sqlQuerySubset[(MAX_QUERY_LEN/5)+1];
 
        queue<msg_thread_id_t> threadList;
 
@@ -1139,36 +1145,47 @@ msg_error_t MsgStoDeleteMessageByList(msg_id_list_s *pMsgIdList)
        int listCnt = sizeof(tableList)/sizeof(char *);
        int rowCnt = 0;
 
-       memset(whereQuery, 0x00, sizeof(whereQuery));
-
-       if (pMsgIdList->nCount < 1) {
-               return MSG_SUCCESS;
-       } else {
-               for (int i=0; i < pMsgIdList->nCount; i++) {
-                       memset(sqlQuerySubset, 0x00, sizeof(sqlQuerySubset));
-                       if (i==0)
-                               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "(MSG_ID = %d ", pMsgIdList->msgIdList[i]);
-                       else
-                               snprintf(sqlQuerySubset, sizeof(sqlQuerySubset), "OR MSG_ID = %d ", pMsgIdList->msgIdList[i]);
-                       strncat(whereQuery, sqlQuerySubset, MAX_QUERY_LEN-strlen(whereQuery));
-               }
+       // Clear tmp table
+       memset(sqlQuery, 0x00, sizeof(sqlQuery));
+       snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s;", MSGFW_TMP_MSGID_TABLE_NAME);
+       err = dbHandle.execQuery(sqlQuery);
+       if (err != MSG_SUCCESS) {
+               MSG_DEBUG("Fail to execQuery().");
+               return err;
+       }
+       // Insert tmp msg ids to delete.
+       dbHandle.beginTrans();
+       for (int i = 0; i < pMsgIdList->nCount; i++)
+       {
+               memset(sqlQuery, 0x00, sizeof(sqlQuery));
+               snprintf(sqlQuery, sizeof(sqlQuery), "INSERT INTO %s VALUES (%d);",
+                               MSGFW_TMP_MSGID_TABLE_NAME, pMsgIdList->msgIdList[i]);
+               err = dbHandle.execQuery(sqlQuery);
+               if (err != MSG_SUCCESS) MSG_DEBUG("Fail to execQuery().");
+       }
+       err = dbHandle.endTrans(true);
+       if (err != MSG_SUCCESS) {
+               MSG_DEBUG("Fail to endTrans(true).");
+               dbHandle.endTrans(false);
+               return err;
        }
-       strncat(whereQuery, ");", MAX_QUERY_LEN-strlen(whereQuery));
 
        // Get conversation ID from Folder
        memset(sqlQuery, 0x00, sizeof(sqlQuery));
-       snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT(CONV_ID) FROM %s WHERE ", MSGFW_MESSAGE_TABLE_NAME);
-       strncat(sqlQuery, whereQuery, MAX_QUERY_LEN-strlen(sqlQuery));
+       snprintf(sqlQuery, sizeof(sqlQuery), "SELECT DISTINCT(CONV_ID) FROM %s WHERE MSG_ID IN (SELECT MSG_ID FROM %s);",
+                       MSGFW_MESSAGE_TABLE_NAME, MSGFW_TMP_MSGID_TABLE_NAME);
 
        err = dbHandle.getTable(sqlQuery, &rowCnt);
 
        if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
-               MSG_DEBUG("sql query is %s.", sqlQuery);
+               MSG_DEBUG("Fail to getTable().");
                dbHandle.freeTable();
                return err;
        }
 
-       if (rowCnt <= 0) {
+       MSG_DEBUG("rowCnt [%d]", rowCnt);
+
+       if (rowCnt < 1) {
                dbHandle.freeTable();
                return MSG_SUCCESS;
        }
@@ -1177,70 +1194,63 @@ msg_error_t MsgStoDeleteMessageByList(msg_id_list_s *pMsgIdList)
                MSG_DEBUG("thread ID : %d", dbHandle.getColumnToInt(i));
                threadList.push((msg_thread_id_t)dbHandle.getColumnToInt(i));
        }
-
        dbHandle.freeTable();
 
        /*** Delete Sim Message In Folder **/
        memset(sqlQuery, 0x00, sizeof(sqlQuery));
-       snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE STORAGE_ID = %d AND ", MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM);
-       strncat(sqlQuery, whereQuery, MAX_QUERY_LEN-strlen(sqlQuery));
+       snprintf(sqlQuery, sizeof(sqlQuery), "SELECT MSG_ID FROM %s WHERE STORAGE_ID = %d AND MSG_ID IN (SELECT MSG_ID FROM %s);",
+                       MSGFW_MESSAGE_TABLE_NAME, MSG_STORAGE_SIM, MSGFW_TMP_MSGID_TABLE_NAME);
 
        rowCnt = 0;
 
        err = dbHandle.getTable(sqlQuery, &rowCnt);
 
        if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
-               MSG_DEBUG("sql query is %s.", sqlQuery);
+               MSG_DEBUG("Fail to getTable().");
                dbHandle.freeTable();
-               return err;
+               rowCnt = 0;
        }
 
        for (int i = 1; i <= rowCnt; i++) {
                err = MsgStoDeleteMessage(dbHandle.getColumnToInt(i), false);
-
                if (err != MSG_SUCCESS) {
                        MSG_DEBUG("MsgStoDeleteMessage() Error!!!");
-                       dbHandle.freeTable();
-                       return err;
                }
        }
-
        dbHandle.freeTable();
        /*** **/
 
        dbHandle.beginTrans();
-
        for (int i = 0; i < listCnt; i++) {
                if (!strcmp(tableList[i], MMS_PLUGIN_MESSAGE_TABLE_NAME)) {
 
-                       int rowCnt = 0;
+                       char filePath[MSG_FILEPATH_LEN_MAX];
+                       char dirPath[MSG_FILEPATH_LEN_MAX];
 
-                       char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
-                       char dirPath[MSG_FILEPATH_LEN_MAX] = {0,};
-//                     char thumbnailPath[MSG_FILEPATH_LEN_MAX] = {0,};
+                       rowCnt = 0;
 
-                       //get mms msg id list
+                       // MMS file path to delete.
                        memset(sqlQuery, 0x00, sizeof(sqlQuery));
-                       snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE ", MMS_PLUGIN_MESSAGE_TABLE_NAME);
-                       strncat(sqlQuery, whereQuery, MAX_QUERY_LEN-strlen(sqlQuery));
+                       snprintf(sqlQuery, sizeof(sqlQuery), "SELECT FILE_PATH FROM %s WHERE MSG_ID IN (SELECT MSG_ID FROM %s);",
+                                       MMS_PLUGIN_MESSAGE_TABLE_NAME, MSGFW_TMP_MSGID_TABLE_NAME);
 
                        err = dbHandle.getTable(sqlQuery, &rowCnt);
-                       MSG_DEBUG("rowCnt %d", rowCnt);
 
                        if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
-                               MSG_DEBUG("sqlQuery [%s]", sqlQuery);
-                               dbHandle.freeTable();
-                               dbHandle.endTrans(false);
-                               return err;
+                               MSG_DEBUG("Fail to getTable().");
+                               rowCnt = 0;
                        }
 
+                       MSG_DEBUG("rowCnt %d", rowCnt);
+
                        for (int i = 1; i <= rowCnt; i++) {
                                memset(filePath, 0x00, sizeof(filePath));
+                               memset(dirPath, 0x00, sizeof(dirPath));
                                dbHandle.getColumnToString(i, MSG_FILEPATH_LEN_MAX, filePath);
 
                                MSG_DEBUG("filePath [%s]", filePath);
 
-                               //delete raw file
+                               // Delete raw file.
                                snprintf(dirPath, sizeof(dirPath), "%s.dir", filePath);
 
                                if (remove(filePath) == -1)
@@ -1252,71 +1262,70 @@ msg_error_t MsgStoDeleteMessageByList(msg_id_list_s *pMsgIdList)
 
                                rmdir(dirPath);
                        }
-
                        dbHandle.freeTable();
+
                } else if (!strcmp(tableList[i], MSGFW_MMS_PREVIEW_TABLE_NAME)) {
+
                        char filePath[MSG_FILEPATH_LEN_MAX] = {0,};
+
+                       rowCnt = 0;
+
                        memset(sqlQuery, 0x00, sizeof(sqlQuery));
                        snprintf(sqlQuery, sizeof(sqlQuery),
                                        "SELECT VALUE FROM %s "
                                        "WHERE (TYPE=%d OR TYPE=%d) "
-                                       "AND ",
-                                       MSGFW_MMS_PREVIEW_TABLE_NAME, MSG_MMS_ITEM_TYPE_IMG, MSG_MMS_ITEM_TYPE_VIDEO);
+                                       "AND MSG_ID IN (SELECT MSG_ID FROM %s);",
+                                       MSGFW_MMS_PREVIEW_TABLE_NAME,
+                                       MSG_MMS_ITEM_TYPE_IMG, MSG_MMS_ITEM_TYPE_VIDEO,
+                                       MSGFW_TMP_MSGID_TABLE_NAME);
 
-                       strncat(sqlQuery, whereQuery, MAX_QUERY_LEN-strlen(sqlQuery));
+                       err = dbHandle.getTable(sqlQuery, &rowCnt);
 
-                       if (dbHandle.prepareQuery(sqlQuery) != MSG_SUCCESS) {
-                               dbHandle.endTrans(false);
-                               return MSG_ERR_DB_PREPARE;
+                       if (err != MSG_SUCCESS && err != MSG_ERR_DB_NORECORD) {
+                               MSG_DEBUG("Fail to getTable().");
+                               rowCnt = 0;
                        }
 
-                       while (dbHandle.stepQuery() == MSG_ERR_DB_ROW) {
-                               memset(filePath, 0x00, sizeof(filePath));
+                       MSG_DEBUG("rowCnt %d", rowCnt);
 
-                               strncpy(filePath, (char *)dbHandle.columnText(0), MSG_FILEPATH_LEN_MAX);
+                       for (int i = 1; i <= rowCnt; i++) {
+                               memset(filePath, 0x00, sizeof(filePath));
+                               dbHandle.getColumnToString(i, MSG_FILEPATH_LEN_MAX, filePath);
                                if (remove(filePath) == -1)
                                        MSG_DEBUG("Fail to delete file [%s]", filePath);
                                else
                                        MSG_DEBUG("Success to delete file [%s]", filePath);
                        }
-
-                       dbHandle.finalizeQuery();
+                       dbHandle.freeTable();
                }
 
                memset(sqlQuery, 0x00, sizeof(sqlQuery));
-
-               snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE ", tableList[i]);
-               strncat(sqlQuery, whereQuery, MAX_QUERY_LEN-strlen(sqlQuery));
+               snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s WHERE MSG_ID IN (SELECT MSG_ID FROM %s);",
+                               tableList[i], MSGFW_TMP_MSGID_TABLE_NAME);
 
                // Delete Message in specific folder from table
                err = dbHandle.execQuery(sqlQuery);
                if (err != MSG_SUCCESS) {
-                       MSG_DEBUG("sqlQuery [%s]", sqlQuery);
-                       dbHandle.endTrans(false);
-                       return err;
+                       MSG_DEBUG("Fail to execQuery().");
                }
        }
 
        // Clear Conversation table
        err = MsgStoClearConversationTable(&dbHandle);
-       if (err != MSG_SUCCESS) {
-               dbHandle.endTrans(false);
-               return err;
-       }
+       if (err != MSG_SUCCESS) MSG_DEBUG("Fail to MsgStoClearConversationTable().");
 
        // Update Address
        while (!threadList.empty()) {
                err = MsgStoUpdateConversation(&dbHandle, threadList.front());
-
+               if (err != MSG_SUCCESS) MSG_DEBUG("Fail to MsgStoUpdateConversation().");
                threadList.pop();
-
-               if (err != MSG_SUCCESS) {
-                       dbHandle.endTrans(false);
-                       return err;
-               }
        }
-
-       dbHandle.endTrans(true);
+       err = dbHandle.endTrans(true);
+       if (err != MSG_SUCCESS) {
+               MSG_DEBUG("Fail to endTrans(true).");
+               dbHandle.endTrans(false);
+               return err;
+       }
 
        int smsCnt = 0;
        int mmsCnt = 0;
@@ -1326,34 +1335,14 @@ msg_error_t MsgStoDeleteMessageByList(msg_id_list_s *pMsgIdList)
 
        MsgSettingSetIndicator(smsCnt, mmsCnt);
 
-#if 0
-/*** Create thread  for noti and phone log delete. **/
-       if (pMsgIdList->nCount > 0) {
-               msg_id_list_s *pToDeleteMsgIdListCpy = NULL;
-               pToDeleteMsgIdListCpy = (msg_id_list_s *)new char[sizeof(msg_id_list_s)];
-               memset(pToDeleteMsgIdListCpy, 0x00, sizeof(msg_id_list_s));
-               pToDeleteMsgIdListCpy->nCount = pMsgIdList->nCount;
-               pToDeleteMsgIdListCpy->msgIdList = (msg_message_id_t *)new char[sizeof(msg_message_id_t)*pMsgIdList->nCount];
-               memcpy(pToDeleteMsgIdListCpy->msgIdList, pMsgIdList->msgIdList, sizeof(msg_message_id_t)*pMsgIdList->nCount);
-
-               if (g_idle_add(startToDeletePhoneLog, (void *)pToDeleteMsgIdListCpy) == 0) {
-                       MSG_DEBUG("startToDeletePhoneLog not invoked: %s", strerror(errno));
-                       // memory free
-                       if (pToDeleteMsgIdListCpy != NULL) {
-                               //free peer info list
-                               if (pToDeleteMsgIdListCpy->msgIdList != NULL)
-                                       delete [] pToDeleteMsgIdListCpy->msgIdList;
-
-                               delete [] pToDeleteMsgIdListCpy;
-                       }
-                       err = MSG_ERR_UNKNOWN;
-               }
-       }
-/*** **/
-#endif
-
        MsgRefreshNoti(false);
 
+       // Clear tmp table
+       memset(sqlQuery, 0x00, sizeof(sqlQuery));
+       snprintf(sqlQuery, sizeof(sqlQuery), "DELETE FROM %s;", MSGFW_TMP_MSGID_TABLE_NAME);
+       err = dbHandle.execQuery(sqlQuery);
+       if (err != MSG_SUCCESS) MSG_DEBUG("Fail to execQuery().");
+
        MSG_END();
        return MSG_SUCCESS;
 }
index 08f8301..5e10c0f 100755 (executable)
@@ -42,6 +42,9 @@
 #define MSGFW_REPORT_TABLE_NAME                                "MSG_REPORT_TABLE"
 #define MSGFW_PUSH_CONFIG_TABLE_NAME                   "MSG_PUSHCFG_TABLE"
 #define MSGFW_MMS_PREVIEW_TABLE_NAME                   "MSG_MMS_PREVIEW_INFO_TABLE"
+
+#define MSGFW_TMP_MSGID_TABLE_NAME                     "MSG_TMP_MSGID_TABLE"
+
 #define MAX_QUERY_LEN                                  3072
 #define MAX_FOLDER_NAME_LEN            20
 #define MAX_ACCOUNT_NAME_LEN   51
index 86bed39..c0534ee 100755 (executable)
@@ -1,6 +1,6 @@
 Name:           msg-service
-Version:        0.9.2
-Release:        3
+Version:        0.9.3
+Release:        1
 License:        Flora
 Summary:        Messaging Framework Library
 Group:          System/Libraries
@@ -292,6 +292,9 @@ then
        APPCODE INTEGER,
        SECURE INTEGER );
 
+       CREATE TABLE MSG_TMP_MSGID_TABLE (
+       MSG_ID INTEGER );
+
     CREATE INDEX MSG_CONVERSATION_INDEX ON MSG_CONVERSATION_TABLE(CONV_ID);
     CREATE INDEX MSG_FOLDER_INDEX ON MSG_FOLDER_TABLE(FOLDER_ID);
     CREATE INDEX MSG_MESSAGE_INDEX ON MSG_MESSAGE_TABLE(MSG_ID, CONV_ID, FOLDER_ID);