settings.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include ".\settings.h"
  2. #include <shlwapi.h>
  3. #include <strsafe.h>
  4. Settings::Settings(void)
  5. {
  6. dumpPath = NULL;
  7. logPath = NULL;
  8. smtpServer = NULL;
  9. smtpUser = NULL;
  10. smtpPwd = NULL;
  11. path = NULL;
  12. smtpAddress = NULL;
  13. updatePath = TRUE;
  14. createDMP = TRUE;
  15. createLOG = TRUE;
  16. autoRestart = FALSE;
  17. silentMode = TRUE;
  18. sendData = TRUE;
  19. zipData = TRUE;
  20. zipPath = NULL;
  21. sendByClient = TRUE;
  22. sendBySMTP = FALSE;
  23. smtpPort = 25;
  24. smtpAuth = TRUE;
  25. dumpType = 0;
  26. logSystem = TRUE;
  27. logRegistry = TRUE;
  28. logStack = TRUE;
  29. logModule = TRUE;
  30. }
  31. Settings::~Settings(void)
  32. {
  33. if (dumpPath) free(dumpPath);
  34. if (logPath) free(logPath);
  35. if (smtpServer) free(smtpServer);
  36. if (smtpUser) free(smtpUser);
  37. if (smtpPwd) free(smtpPwd);
  38. if (path) free(path);
  39. if (smtpAddress) free(smtpAddress);
  40. }
  41. void Settings::SetPath(wchar_t *iniPath)
  42. {
  43. size_t size = lstrlen(iniPath);
  44. if (path) free(path);
  45. path = NULL;
  46. path = (wchar_t*)malloc((size + 1) * sizeof(wchar_t));
  47. StringCchCopy(path, size+1, iniPath);
  48. wchar_t iniFile[MAX_PATH*2] = {0};
  49. size += 14 * sizeof(wchar_t);
  50. CreateDirectory(iniPath, NULL);
  51. StringCchPrintf(iniFile, size, L"%s\\feedback.ini", iniPath);
  52. cfg.SetIniFile(iniFile);
  53. }
  54. const wchar_t* Settings::GetPath(void)
  55. {
  56. return path;
  57. }
  58. BOOL Settings::Load(void)
  59. {
  60. if (!cfg.IsFileExist()) return FALSE;
  61. cfg.SetSection(L"General");
  62. updatePath = cfg.ReadInt(L"UpdatePath", TRUE);
  63. if (updatePath) return FALSE;
  64. createDMP = cfg.ReadInt(L"CreateDmp", TRUE);
  65. createLOG = cfg.ReadInt(L"CreateLog", TRUE);
  66. autoRestart = cfg.ReadInt(L"AutoRestart", FALSE);
  67. silentMode = cfg.ReadInt(L"SilentMode", TRUE);
  68. sendData = cfg.ReadInt(L"SendData", TRUE);
  69. cfg.SetSection(L"Send");
  70. sendByClient = cfg.ReadInt(L"UseClient", TRUE);
  71. sendBySMTP = cfg.ReadInt(L"UseSMTP", FALSE);
  72. smtpPort = cfg.ReadInt(L"Port", 25);
  73. smtpAuth = cfg.ReadInt(L"ReqAuth", TRUE);
  74. CreateStrCopy(&smtpAddress, cfg.ReadStringW(L"Address", L"[email protected]"));
  75. CreateStrCopy(&smtpServer, cfg.ReadStringW(L"Server", NULL));
  76. CreateStrCopy(&smtpUser, cfg.ReadStringW(L"User", NULL));
  77. CreateStrCopy(&smtpPwd, cfg.ReadStringW(L"Pwd", NULL));
  78. cfg.SetSection(L"Zip");
  79. zipData = cfg.ReadInt(L"ZipData", TRUE);
  80. CreateStrCopy(&zipPath, cfg.ReadStringW(L"Path", NULL));
  81. cfg.SetSection(L"Dump");
  82. dumpType = cfg.ReadInt(L"Type", 0);
  83. CreateStrCopy(&dumpPath, cfg.ReadStringW(L"Path", NULL));
  84. cfg.SetSection(L"Log");
  85. logSystem = cfg.ReadInt(L"System", TRUE);
  86. logRegistry = cfg.ReadInt(L"Registry", TRUE);
  87. logStack = cfg.ReadInt(L"Stack", TRUE);
  88. logModule = cfg.ReadInt(L"Module", TRUE);
  89. CreateStrCopy(&logPath, cfg.ReadStringW(L"Path", NULL));
  90. return TRUE;
  91. }
  92. void Settings::CreateStrCopy(wchar_t **dest, const wchar_t* source)
  93. {
  94. if (*dest) free(*dest);
  95. *dest = NULL;
  96. if (source)
  97. {
  98. size_t len = lstrlen(source) + 1;
  99. *dest = (wchar_t*) malloc(len*sizeof(wchar_t));
  100. StringCchCopy(*dest, len, source);
  101. }
  102. }
  103. BOOL Settings::Save(void)
  104. {
  105. BOOL error = FALSE;
  106. if (FALSE == cfg.SetSection(L"General")) error = TRUE;
  107. if (FALSE == cfg.Write(L"UpdatePath", FALSE)) error = TRUE;
  108. if (FALSE == cfg.Write(L"CreateDmp", createDMP)) error = TRUE;
  109. if (FALSE == cfg.Write(L"CreateLog", createLOG)) error = TRUE;
  110. if (FALSE == cfg.Write(L"AutoRestart", autoRestart)) error = TRUE;
  111. if (FALSE == cfg.Write(L"SilentMode", silentMode)) error = TRUE;
  112. if (FALSE == cfg.Write(L"SendData", sendData)) error = TRUE;
  113. if (FALSE == cfg.SetSection(L"Send")) error = TRUE;
  114. if (FALSE == cfg.Write(L"UseClient", sendByClient)) error = TRUE;
  115. if (FALSE == cfg.Write(L"UseSMTP", sendBySMTP)) error = TRUE;
  116. if (FALSE == cfg.Write(L"Port", smtpPort)) error = TRUE;
  117. if (FALSE == cfg.Write(L"Server", smtpServer)) error = TRUE;
  118. if (FALSE == cfg.Write(L"Address", smtpAddress)) error = TRUE;
  119. if (FALSE == cfg.Write(L"ReqAuth", smtpAuth)) error = TRUE;
  120. if (FALSE == cfg.Write(L"User", smtpUser)) error = TRUE;
  121. if (FALSE == cfg.Write(L"Pwd", smtpPwd)) error = TRUE;
  122. if (FALSE == cfg.SetSection(L"Zip")) error = TRUE;
  123. if (FALSE == cfg.Write(L"ZipData", zipData)) error = TRUE;
  124. if (FALSE == cfg.Write(L"Path", zipPath)) error = TRUE;
  125. if (FALSE == cfg.SetSection(L"Dump")) error = TRUE;
  126. if (FALSE == cfg.Write(L"Type", dumpType)) error = TRUE;
  127. if (FALSE == cfg.Write(L"Path", dumpPath)) error = TRUE;
  128. if (FALSE == cfg.SetSection(L"Log")) error = TRUE;
  129. if (FALSE == cfg.Write(L"System", logSystem)) error = TRUE;
  130. if (FALSE == cfg.Write(L"Registry", logRegistry)) error = TRUE;
  131. if (FALSE == cfg.Write(L"Stack", logStack)) error = TRUE;
  132. if (FALSE == cfg.Write(L"Module", logModule)) error = TRUE;
  133. if (FALSE == cfg.Write(L"Path", logPath)) error = TRUE;
  134. return !error;
  135. }
  136. BOOL Settings::CreateDefault(wchar_t* iniPath)
  137. {
  138. wchar_t temp[MAX_PATH] = {0};
  139. int len;
  140. createDMP = TRUE;
  141. createLOG = TRUE;
  142. autoRestart = FALSE;
  143. silentMode = TRUE;
  144. sendData = TRUE;
  145. // zip
  146. PathCombine(temp, iniPath, L"report.zip");
  147. len = (int)wcslen(temp) + 1;
  148. zipData = TRUE;
  149. zipPath = (wchar_t*) malloc(len*2);
  150. StringCchCopy(zipPath, len, temp);
  151. // send
  152. sendByClient = TRUE;
  153. sendBySMTP = FALSE;
  154. smtpPort = 25;
  155. smtpAddress = (wchar_t*) malloc(32*2);
  156. StringCchCopy(smtpAddress, 32, L"[email protected]");
  157. smtpAuth = TRUE;
  158. smtpServer = NULL;
  159. smtpUser = NULL;
  160. smtpPwd = NULL;
  161. // dump
  162. PathCombine(temp, iniPath, L"_crash.dmp");
  163. len = (int)wcslen(temp) + 1;
  164. dumpType = NULL;
  165. dumpPath = (wchar_t*) malloc(len*2);
  166. StringCchCopy(dumpPath, len, temp);
  167. // log
  168. logSystem = TRUE;
  169. logRegistry = TRUE;
  170. logStack = TRUE;
  171. logModule = TRUE;
  172. PathCombine(temp, iniPath, L"_crash.log");
  173. len = (int)wcslen(temp) + 1;
  174. logPath = (wchar_t*) malloc(len*2);
  175. StringCchCopy(logPath, len, temp);
  176. return TRUE;
  177. }
  178. BOOL Settings::IsOk(void)
  179. {
  180. return (logPath != NULL && dumpPath != NULL);
  181. }
  182. void Settings::ClearTempData(void)
  183. {
  184. cfg.Write(L"Temp", L"TS", L"");
  185. cfg.Write(L"Temp", L"LOG", L"0");
  186. cfg.Write(L"Temp", L"DMP", L"0");
  187. }
  188. void Settings::WriteErrorTS(const wchar_t *time)
  189. {
  190. cfg.Write(L"Temp", L"TS", time);
  191. }
  192. void Settings::WriteLogCollectResult(BOOL result)
  193. {
  194. cfg.Write(L"Temp", L"LOG", result);
  195. }
  196. void Settings::WriteDmpCollectResult(BOOL result)
  197. {
  198. cfg.Write(L"Temp", L"DMP", result);
  199. }
  200. void Settings::WriteWinamp(const wchar_t *winamp)
  201. {
  202. cfg.Write(L"Temp", L"WA", winamp);
  203. }
  204. const wchar_t* Settings::ReadErrorTS(void)
  205. {
  206. return cfg.ReadStringW(L"Temp", L"TS", L"");
  207. }
  208. BOOL Settings::ReadLogCollectResult(void)
  209. {
  210. return cfg.ReadInt(L"Temp", L"LOG", 0);
  211. }
  212. BOOL Settings::ReadDmpCollectResult(void)
  213. {
  214. return cfg.ReadInt(L"Temp", L"DMP", 0);
  215. }
  216. const wchar_t* Settings::ReadWinamp(void)
  217. {
  218. return cfg.ReadStringW(L"Temp", L"WA", L"");
  219. }
  220. void Settings::WriteBody(const wchar_t *body)
  221. {
  222. cfg.Write(L"Temp", L"Body", body);
  223. }
  224. const wchar_t* Settings::ReadBody(void)
  225. {
  226. return cfg.ReadStringW(L"Temp", L"Body", L"");
  227. }