stats.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author:
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "stats.h"
  10. #include "WinampAttributes.h"
  11. #include "../nu/AutoChar.h"
  12. #include "../nu/ns_wc.h"
  13. #include "api.h"
  14. #include <malloc.h>
  15. #include <rpc.h>
  16. /* benski> ideas for new stats
  17. bitmask of interesting config options (e.g. 24bit, replay gain)
  18. number of smart views
  19. number of tracks burned
  20. color theme
  21. other things:
  22. add generic key/value system to api_stats for strings (e.g. colortheme)
  23. */
  24. Stats stats;
  25. Stats::Stats()
  26. {
  27. memset(values, 0, sizeof(values));
  28. values[LIBRARY_SIZE]=-1; // for historical reasons
  29. }
  30. void Stats::Init()
  31. {
  32. char str[Stats::NUM_STATS*9+1] = {0}; // each stat is written as 8 digit hex and a comma (9 characters)
  33. char *p=str;
  34. GetPrivateProfileStringA("WinampReg","Stats","",str,sizeof(str),INI_FILEA);
  35. for (int x = 0; x < NUM_STATS; x ++)
  36. {
  37. values[x]=strtol(p,&p,16);
  38. if (*p) p++;
  39. else break;
  40. }
  41. }
  42. void Stats::SetStat(int stat, int value)
  43. {
  44. if (stat >= 0 && stat < NUM_STATS)
  45. values[stat] = value;
  46. }
  47. void Stats::IncrementStat(int stat)
  48. {
  49. if (stat >= 0 && stat < NUM_STATS)
  50. values[stat]++;
  51. }
  52. void Stats::Write()
  53. {
  54. char str[Stats::NUM_STATS*9+1] = {0}; // each stat is written as 8 digit hex and a comma (9 characters)
  55. char *str_ptr = str;
  56. size_t str_size = sizeof(str)/sizeof(*str);
  57. for (int x = 0; x < NUM_STATS; x ++)
  58. {
  59. StringCchPrintfExA(str_ptr, str_size, &str_ptr, &str_size, 0, "%08X,",values[x]);
  60. }
  61. WritePrivateProfileStringA("WinampReg","Stats",str,INI_FILEA);
  62. }
  63. void Stats::GetStats(int stats[NUM_STATS]) const
  64. {
  65. memcpy(stats, values, sizeof(*stats)*NUM_STATS);
  66. }
  67. void Stats::SetString(const char *key, const wchar_t *value)
  68. {
  69. WritePrivateProfileStringA("WinampReg",key,AutoChar(value, CP_UTF8),INI_FILEA);
  70. }
  71. void Stats::GetString(const char *key, wchar_t *value, size_t value_cch) const
  72. {
  73. *value = 0;
  74. char *utf8 = (char *)alloca(value_cch);
  75. if (utf8)
  76. {
  77. GetPrivateProfileStringA("WinampReg",key,"",utf8,(DWORD)value_cch,INI_FILEA);
  78. MultiByteToWideCharSZ(CP_UTF8, 0, utf8, -1, value, (int)value_cch);
  79. }
  80. }
  81. // return a bitmask of interesting configuration choices
  82. /*static int stats_get_cfg()
  83. {
  84. int s = 0;
  85. s |= !!config_replaygain;
  86. s |= (config_audio_bits == 24) << 1;
  87. /* TODO:
  88. agent on or off
  89. EQ on
  90. global hotkeys enabled
  91. info panel on or off
  92. remember search on or off
  93. */
  94. /*}*/
  95. void stats_write(void)
  96. {
  97. /* benski>
  98. write skin and color theme (if available) on close
  99. since we'll have a reliable way to get color themes (gen_ff hasn't loaded yet when versioncheck runs)
  100. and it's a more accurate picture of the skin the user was using
  101. */
  102. const wchar_t *colorTheme = 0;
  103. if (WASABI_API_COLORTHEMES)
  104. colorTheme = WASABI_API_COLORTHEMES->getGammaSet();
  105. stats.SetString("colortheme", colorTheme);
  106. stats.SetString("skin", config_skin);
  107. stats.IncrementStat(Stats::LAUNCHES);
  108. stats.SetStat(Stats::REGVER, 2);
  109. stats.SetStat(Stats::PLEDIT_LENGTH, PlayList_getlength());
  110. stats.Write();
  111. }
  112. void stats_save()
  113. {
  114. stats.Write();
  115. }
  116. void stats_getuidstr(char str[512])
  117. {
  118. GUID uid;
  119. GetPrivateProfileStringA("WinampReg","ID","",str,128,INI_FILEA);
  120. if (strlen(str) > sizeof(GUID)*2) // reset bad ID's which were being generated for some time (fixed in 5.5)
  121. str[0]=0;
  122. if (!str[0])
  123. {
  124. int x;
  125. unsigned char *p;
  126. size_t strsize = 512;
  127. char *strbuf = str;
  128. CoCreateGuid(&uid);
  129. p=(unsigned char *)&uid;
  130. str[0]=0;
  131. for (x = 0; x < sizeof(uid); x ++)
  132. {
  133. StringCchPrintfExA(strbuf, strsize, &strbuf, &strsize, 0, "%02X", p[x]);
  134. }
  135. WritePrivateProfileStringA("WinampReg","ID",str,INI_FILEA);
  136. }
  137. }
  138. void Stats_OnPlay(const wchar_t *playstring)
  139. {
  140. if (!_wcsnicmp(playstring, L"http://", 7)
  141. || !_wcsnicmp(playstring, L"sc://", 5)
  142. || !_wcsnicmp(playstring, L"mms://", 6)
  143. || !_wcsnicmp(playstring, L"icy://", 6))
  144. stats.IncrementStat(Stats::STREAMS_PLAYED);
  145. else if (!_wcsnicmp(playstring, L"cda://", 6) ||
  146. !_wcsicmp(extensionW(playstring), L"cda"))
  147. stats.IncrementStat(Stats::CDS_PLAYED);
  148. else
  149. stats.IncrementStat(Stats::FILES_PLAYED);
  150. }
  151. void stats_init()
  152. {
  153. stats.Init();
  154. }
  155. #define CBCLASS Stats
  156. START_DISPATCH;
  157. VCB(SETSTAT, SetStat);
  158. VCB(INCREMENTSTAT, IncrementStat);
  159. VCB(SETSTRING, SetString);
  160. END_DISPATCH;
  161. #undef CBCLASS