SkinList.cpp
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:4k
源码类别:

VC书籍

开发平台:

Visual C++

  1. /**************************************************************************************
  2.  *                                                                                    *
  3.  *                                                                                    *
  4.  **************************************************************************************/
  5. #include "SkinList.h"
  6. #include <windows.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. /* 
  10.  * 外壳信息类
  11.  */
  12. SkinInfo::SkinInfo(char *directory) {
  13. this->directory = (char *) new char[strlen(directory)+1]; 
  14. this->directory = strcpy(this->directory, directory);
  15. this->directory[strlen(directory)] = '';
  16. this->name      = strrchr(this->directory, '\') + 1;
  17. if(name == NULL) {
  18. name = directory;
  19. }
  20. }
  21. SkinInfo::~SkinInfo() {
  22. free(this->directory);
  23. }
  24. /* 
  25.  * 外壳链表类
  26.  */
  27. SkinList::SkinList() {
  28. this->totalSkins = 0;
  29. this->skins      = NULL;
  30. this->skinsDir   = NULL;
  31. /*
  32.  * 从注册表中得到链表路径
  33.  */
  34. HKEY     key;
  35. DWORD    created, size, type;
  36. LONG     result;
  37. /*
  38.  * 打开注册键
  39.  */
  40. result = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\DivXNetworks\ThePlaya",
  41. 0, "CONFIG", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 
  42. NULL, &key, &created);
  43. if(result != ERROR_SUCCESS) {
  44. MessageBox(NULL, "Couldn't load skins directories", "", MB_OK);
  45. return;
  46. }
  47. switch(created) {
  48. case REG_CREATED_NEW_KEY:
  49. /*
  50.  * 第一次启动(缺省)
  51.  * 
  52.  */
  53. this->skinsDir = NULL;
  54. break;
  55. case REG_OPENED_EXISTING_KEY:
  56. /*
  57.  * 读值
  58.    */
  59. this->skinsDir = (char *) new char[MAX_PATH];
  60. size = MAX_PATH;
  61. result = RegQueryValueEx(key, "SkinsDir", 0, &type, (BYTE *)this->skinsDir, &size);
  62. if(result == ERROR_MORE_DATA) {
  63. this->skinsDir = (char *) realloc(this->skinsDir, size);
  64. result = RegQueryValueEx(key, "SkinsDir", 0, &type, (BYTE *)this->skinsDir, &size);
  65. }
  66. if(result != ERROR_SUCCESS) {
  67. free(this->skinsDir);
  68. this->skinsDir = NULL;
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. RegCloseKey(key);
  75. }
  76. SkinList::~SkinList() {
  77. free(this->skinsDir);
  78. }
  79. void SkinList::Add(SkinInfo *skinInfo) {
  80. skinlist_t *node;
  81. node = (skinlist_t *) new skinlist_t;
  82. node->info     = skinInfo;
  83. node->next     = this->skins;
  84. this->skins = node;
  85. this->totalSkins++;
  86. }
  87. int SkinList::SetDir(char *dir) {
  88. this->skinsDir = dir;
  89. HKEY     key;
  90. DWORD    created;
  91. LONG     result;
  92. /*
  93.  * 尝试打开注册键
  94.  */
  95. result = RegCreateKeyEx(HKEY_CURRENT_USER, "Software\DivXNetworks\ThePlaya",
  96. 0, "CONFIG", REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 
  97. NULL, &key, &created);
  98. if(result != ERROR_SUCCESS) {
  99. MessageBox(NULL, "Couldn't save skins dir", "", MB_OK);
  100. return 0;
  101. }
  102. RegSetValueEx(key, "SkinsDir", 0, REG_SZ, (BYTE *) this->skinsDir, strlen(this->skinsDir));
  103. return 1;
  104. }
  105. int SkinList::Scan() {
  106. this->totalSkins = 0;
  107. this->skins      = NULL;
  108. if(this->skinsDir != NULL && strcmp(this->skinsDir, "Default") != 0) {
  109. WIN32_FIND_DATA dirData;
  110. HANDLE          hFind = NULL, hSkin = NULL;
  111. char           *str;
  112. str = (char *) new char[(strlen(this->skinsDir))];
  113. strcpy(str, this->skinsDir);
  114. hFind = FindFirstFile(strcat(str, "\*.*"), &dirData);
  115. if(hFind != NULL) {
  116. if(strcmp(dirData.cFileName, ".") != 0 && strcmp(dirData.cFileName, "..") != 0) {
  117. if(dirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  118. this->Add(new SkinInfo(dirData.cFileName));
  119. }
  120. }
  121. while(FindNextFile(hFind, &dirData)) {
  122. if(strcmp(dirData.cFileName, ".") != 0 && strcmp(dirData.cFileName, "..") != 0) {
  123. if(dirData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  124. char filename[MAX_PATH];
  125. strcpy(filename, this->skinsDir);
  126. strcat(strcat(filename, "\"), dirData.cFileName);
  127. this->Add(new SkinInfo(filename));
  128. }
  129. }
  130. }
  131. FindClose(hFind);
  132. }
  133. }
  134. return 0;
  135. }
  136. int Reset() {
  137. return 0;
  138. }
  139. int SkinList::getNumberOfSkins() {
  140. return this->totalSkins;
  141. }
  142. SkinInfo *SkinList::getSkinInfo(int position) {
  143. if(position >= 0 && position < this->totalSkins) {
  144. int         i;
  145. skinlist_t *node;
  146. node = this->skins;
  147. for(i = 0; i < position; i++) {
  148. node = node->next;
  149. }
  150. return node->info;
  151. }
  152. return NULL;
  153. }