-result
-_Aul::_DesktopFile::UpdateService(const AppId& appId, const char* value)
-{
- char path[FILENAME_MAX] = {0, };
- MakePath(appId, path, FILENAME_MAX);
-
- return UpdateField(path, _X_TIZEN_SVC, value);
-}
-
-
-result
-_Aul::_DesktopFile::RemoveService(const AppId& appId, const char* operationOnlyValue)
-{
- char path[FILENAME_MAX] = {0, };
- MakePath(appId, path, FILENAME_MAX);
-
- return UpdateField(path, _X_TIZEN_SVC, operationOnlyValue, true);
-}
-
-//
-// Update value of specified field.
-// currently only "x-slp-svc" field is supported.
-//
-#define BUFFER_SIZE 1024
-result
-_Aul::_DesktopFile::UpdateField(const char* path, const char* fieldName, const char* value, bool isRemove)
-{
- SysTryReturnResult(NID_APP, path != null, E_INVALID_ARG, "path should not be null.");
- SysTryReturnResult(NID_APP, fieldName != null, E_INVALID_ARG, "fieldName should not be null.");
- SysTryReturnResult(NID_APP, value != null, E_INVALID_ARG, "value should not be null.");
-
- FILE* fp = fopen(path, "r+");
- SysTryReturnResult(NID_APP, fp != null, E_SYSTEM, "falied to open '%s' due to %s", path, strerror(errno));
-
- char buffer[BUFFER_SIZE] = {0, };
- bool found = false;
- int len = 0;
- int pos = 0;
- int foundpos = 0;
- result r = E_SUCCESS;
- int remains = 0;
-
- ArrayListT<char*> buffers;
- buffers.Construct();
-
- char* pCurrent = null;
-
- while (fgets(buffer, BUFFER_SIZE, fp) != NULL)
- {
- len = strlen(buffer);
- SysTryCatch(NID_APP, len < BUFFER_SIZE, , r = E_INVALID_ARG, "strlen returns invalid value. (%d)", len );
-
- if (found)
- {
- pCurrent = new (std::nothrow) char[len + 1];
- SysTryCatch(NID_APP, pCurrent != null, , r = E_OUT_OF_MEMORY, "failed to allocate mem pCurrent");
-
- strncpy(pCurrent, buffer, len);
- buffers.Add(pCurrent);
- }
- else
- {
- if (strncmp(buffer, fieldName, len) == 0)
- {
- int fieldNameLen = strlen(fieldName);
- SysTryCatch(NID_APP, len > fieldNameLen, , E_INVALID_ARG, "[E_INVALID_ARG] fieldName(%s)", fieldName);
-
- pCurrent = UpdateServiceValueN(buffer + fieldNameLen, value, isRemove);
- SysTryCatch(NID_APP, pCurrent != null, , r = GetLastResult(), "[%s] UpdateServiceValue failed", GetErrorMessage(GetLastResult()));
-
- buffers.Add(pCurrent);
-
- foundpos = pos;
- found = true;
- }
- }
-
- pos += len;
- }
-
- if (found)
- {
- fsetpos(fp, (fpos_t*) &foundpos);
-
- remains = buffers.GetCount(); // prevent infinite loop
- while (buffers.GetCount() > 0 && remains-- > 0)
- {
- pCurrent = null;
- buffers.GetAt(0, pCurrent);
- buffers.RemoveAt(0);
- SysTryCatch(NID_APP, pCurrent != null, , r = E_INVALID_STATE, "");
-
- fputs(pCurrent, fp);
- len = strlen(pCurrent);
- pos += len;
- delete[] pCurrent;
- }
-
- int ret = truncate(path, pos);
- SysTryLog(NID_APP, ret == 0, "Truncate failure (%s).", strerror(errno));
- }
- else
- {
- char svctext[_MAX_TIZEN_SVC_DESC_LEN] = {0, };
- snprintf(svctext, _MAX_TIZEN_SVC_DESC_LEN, "%s=%s\n", fieldName, value);
- fputs(svctext, fp);
- }
- fclose(fp);
-
- return E_SUCCESS;
-
-CATCH:
-
- remains = buffers.GetCount(); // prevent infinite loop
- while (buffers.GetCount() > 0 && remains-- > 0)
- {
- pCurrent = null;
- buffers.GetAt(0, pCurrent);
- buffers.RemoveAt(0);
- if (pCurrent != null)
- {
- delete[] pCurrent;
- }
- }
-
- fclose(fp);