Windows NT 4.0 source code leak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

425 lines
9.7 KiB

4 years ago
  1. #include "xerox.h"
  2. #include "group.h"
  3. PGROUP pGroups = NULL;
  4. LPSTR pszCurrentGroup = NULL;
  5. /*
  6. * Adds the names of all defined groups to the listbox/combo box given.
  7. *
  8. * Returns TRUE if success and there are >0 groups defined.
  9. */
  10. BOOL GroupListInit(
  11. HWND hwnd,
  12. BOOL fIsCB)
  13. {
  14. UINT addStringMsg = LB_ADDSTRING;
  15. PGROUP pGroup;
  16. if (pGroups == NULL) {
  17. return(FALSE);
  18. }
  19. if (fIsCB) {
  20. addStringMsg = CB_ADDSTRING;
  21. }
  22. pGroup = pGroups;
  23. while (pGroup) {
  24. SendMessage(hwnd, addStringMsg, 0, (LONG)pGroup->pszName);
  25. pGroup = pGroup->next;
  26. }
  27. return(TRUE);
  28. }
  29. BOOL DeleteGroupDefinition(
  30. LPSTR szName)
  31. {
  32. PGROUP pGroup, pGroupPrev;
  33. PTITLELIST ptl;
  34. pGroupPrev = NULL;
  35. pGroup = pGroups;
  36. while (pGroup) {
  37. if (!_stricmp(pGroup->pszName, szName)) {
  38. if (pGroupPrev == NULL) {
  39. pGroups = pGroup->next;
  40. } else {
  41. pGroupPrev->next = pGroup->next;
  42. }
  43. while (pGroup->ptl != NULL) {
  44. ptl = pGroup->ptl;
  45. pGroup->ptl = ptl->next;
  46. Free(ptl->pszTitle);
  47. Free(ptl->pszClass);
  48. Free(ptl);
  49. }
  50. Free(pGroup);
  51. if (pGroups != NULL) {
  52. pszCurrentGroup = pGroups->pszName;
  53. } else {
  54. pszCurrentGroup = NULL;
  55. }
  56. return(TRUE);
  57. }
  58. pGroupPrev = pGroup;
  59. pGroup = pGroup->next;
  60. }
  61. return(FALSE);
  62. }
  63. BOOL AddGroupDefinition(
  64. LPSTR szName,
  65. HWND hwndList)
  66. {
  67. PGROUP pGroup, pGroupEnd;
  68. PTITLELIST ptl;
  69. HWND hwnd;
  70. int cItems, cb;
  71. char szClass[MAX_STRING_BYTES];
  72. /*
  73. * Don't allow duplicate groups.
  74. * This is how an existing group can be replaced.
  75. */
  76. DeleteGroupDefinition(szName);
  77. pGroup = Alloc(sizeof(GROUP));
  78. if (pGroup == NULL) {
  79. return(FALSE);
  80. }
  81. pGroup->pszName = Alloc(strlen(szName) + 1);
  82. if (pGroup->pszName == NULL) {
  83. Free(pGroup);
  84. return(FALSE);
  85. }
  86. strcpy(pGroup->pszName, szName);
  87. pGroup->ptl = NULL;
  88. cItems = SendMessage(hwndList, LB_GETCOUNT, 0, 0);
  89. while (cItems--) {
  90. ptl = Alloc(sizeof(TITLELIST));
  91. if (ptl != NULL) {
  92. cb = SendMessage(hwndList, LB_GETTEXTLEN, cItems, 0);
  93. if (cb) {
  94. ptl->pszTitle = Alloc(cb + 1);
  95. if (ptl->pszTitle != NULL) {
  96. SendMessage(hwndList, LB_GETTEXT, cItems, (LONG)ptl->pszTitle);
  97. hwnd = (HWND)SendMessage(hwndList, LB_GETITEMDATA, cItems, 0);
  98. GetClassName(hwnd, szClass, sizeof(szClass));
  99. ptl->pszClass = Alloc(strlen(szClass) + 1);
  100. if (ptl->pszClass != NULL) {
  101. strcpy(ptl->pszClass, szClass);
  102. ptl->next = pGroup->ptl;
  103. pGroup->ptl = ptl;
  104. } else {
  105. Free(ptl->pszTitle);
  106. Free(ptl);
  107. }
  108. } else{
  109. Free(ptl);
  110. }
  111. } else {
  112. Free(ptl);
  113. }
  114. }
  115. }
  116. if (pGroup->ptl == NULL) {
  117. Free(pGroup);
  118. return(FALSE);
  119. }
  120. /*
  121. * Put it on the end of the list. This is an awkward attempt
  122. * at making the initial group the first group defined. Probably
  123. * should save this info in the registry.
  124. */
  125. pGroup->next = NULL;
  126. if (pGroups != NULL) {
  127. pGroupEnd = pGroups;
  128. while (pGroupEnd->next != NULL) {
  129. pGroupEnd = pGroupEnd->next;
  130. }
  131. pGroupEnd->next = pGroup;
  132. } else {
  133. pGroups = pGroup;
  134. }
  135. pszCurrentGroup = pGroup->pszName;
  136. return(TRUE);
  137. }
  138. BOOL SelectGroupDefinition(
  139. LPSTR szName,
  140. HWND hwndList,
  141. BOOL DisplayMissingWin)
  142. {
  143. PGROUP pGroup;
  144. HWND hwndAdd;
  145. PTITLELIST ptl;
  146. pGroup = FindGroup(szName);
  147. if (pGroup == NULL) {
  148. return(FALSE);
  149. }
  150. while (SendMessage(hwndList, LB_GETCOUNT, 0, 0)) {
  151. SendMessage(hwndList, LB_DELETESTRING, 0, 0);
  152. }
  153. ptl = pGroup->ptl;
  154. while (ptl) {
  155. hwndAdd = FindWindow(ptl->pszClass, ptl->pszTitle);
  156. if (DisplayMissingWin || (hwndAdd != NULL)) {
  157. if (hwndAdd == NULL) {
  158. hwndAdd = INVALID_HANDLE_VALUE;
  159. }
  160. AddLBItemhwnd(hwndList, ptl->pszTitle, (LONG)hwndAdd);
  161. }
  162. ptl = ptl->next;
  163. }
  164. pszCurrentGroup = pGroup->pszName;
  165. return(TRUE);
  166. }
  167. LPSTR GetCurrentGroup()
  168. {
  169. return(pszCurrentGroup);
  170. }
  171. VOID SetNoCurrentGroup(HWND hwnd, LPSTR szTitle)
  172. {
  173. SetWindowText(hwnd, szTitle);
  174. pszCurrentGroup = NULL;
  175. }
  176. PGROUP FindGroup(
  177. LPSTR szName)
  178. {
  179. PGROUP pGroup;
  180. pGroup = pGroups;
  181. while (pGroup) {
  182. if (!_stricmp(szName, pGroup->pszName)) {
  183. return(pGroup);
  184. }
  185. pGroup = pGroup->next;
  186. }
  187. return(NULL);
  188. }
  189. int CountGroups()
  190. {
  191. PGROUP pGroup;
  192. int c = 0;
  193. pGroup = pGroups;
  194. while (pGroup) {
  195. c++;
  196. pGroup = pGroup->next;
  197. }
  198. return(c);
  199. }
  200. VOID SaveGroups()
  201. {
  202. DWORD cbSave = 1; // for last NULL terminator.
  203. int cTitles;
  204. PGROUP pGroup;
  205. LPSTR pBuf, psz;
  206. LPWORD pw;
  207. HKEY hKey;
  208. PTITLELIST ptl;
  209. if (ERROR_SUCCESS !=
  210. RegCreateKey(HKEY_CURRENT_USER,
  211. "Software\\Microsoft\\Xerox", &hKey)) {
  212. return;
  213. }
  214. pGroup = pGroups;
  215. while (pGroup) {
  216. cbSave += strlen(pGroup->pszName) + 1;
  217. ptl = pGroup->ptl;
  218. while (ptl) {
  219. cbSave += strlen(ptl->pszTitle) + 2 + strlen(ptl->pszClass) + 2;
  220. ptl = ptl->next;
  221. }
  222. pGroup = pGroup->next;
  223. }
  224. if (cbSave == 0) {
  225. return;
  226. }
  227. pBuf = psz = Alloc(cbSave);
  228. if (pBuf == NULL) {
  229. return;
  230. }
  231. RegSetValueEx(hKey, "Groups", 0, REG_MULTI_SZ, "\0\0", 2);
  232. pGroup = pGroups;
  233. while (pGroup) {
  234. strcpy(psz, pGroup->pszName);
  235. psz += strlen(psz) + 1;
  236. ptl = pGroup->ptl;
  237. while (ptl) {
  238. *psz++ = '\t';
  239. strcpy(psz, ptl->pszTitle);
  240. psz += strlen(psz) + 1;
  241. *psz++ = '\t';
  242. strcpy(psz, ptl->pszClass);
  243. psz += strlen(psz) + 1;
  244. ptl = ptl->next;
  245. }
  246. pGroup = pGroup->next;
  247. }
  248. *psz = '\0'; // double NULL terminate last string.
  249. RegSetValueEx(hKey, "Groups", 0, REG_MULTI_SZ, pBuf, cbSave);
  250. RegCloseKey(hKey);
  251. Free(pBuf);
  252. }
  253. VOID FreeGroups()
  254. {
  255. PTITLELIST ptl;
  256. PGROUP pGroup;
  257. pszCurrentGroup = NULL;
  258. while (pGroups) {
  259. while (pGroups->ptl) {
  260. ptl = pGroups->ptl;
  261. Free(ptl->pszTitle);
  262. Free(ptl->pszClass);
  263. pGroups->ptl = ptl->next;
  264. Free(ptl);
  265. }
  266. pGroup = pGroups;
  267. pGroups = pGroups->next;
  268. Free(pGroup);
  269. }
  270. }
  271. VOID LoadGroups()
  272. {
  273. int cTitles;
  274. HKEY hKey;
  275. DWORD cbLoad = 0;
  276. DWORD dwType;
  277. LPSTR pBuf, psz;
  278. PTITLELIST ptl;
  279. PGROUP pGroup, pGroupEnd = NULL;
  280. FreeGroups();
  281. if (ERROR_SUCCESS !=
  282. RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Xerox", &hKey)) {
  283. return;
  284. }
  285. RegQueryValueEx(hKey, "Groups", 0, &dwType, NULL, &cbLoad);
  286. if (dwType != REG_MULTI_SZ) {
  287. RegCloseKey(hKey);
  288. return;
  289. }
  290. if (cbLoad) {
  291. pBuf = psz = Alloc(cbLoad);
  292. if (pBuf == NULL) {
  293. return;
  294. }
  295. if (ERROR_SUCCESS != RegQueryValueEx(hKey, "Groups", 0, &dwType, pBuf, &cbLoad)) {
  296. Free(pBuf);
  297. return;
  298. }
  299. while (*psz) {
  300. pGroup = Alloc(sizeof(GROUP));
  301. if (pGroup == NULL) {
  302. Free(pBuf);
  303. return;
  304. }
  305. pGroup->pszName = Alloc(strlen(psz) + 1);
  306. if (pGroup->pszName == NULL) {
  307. Free(pBuf);
  308. Free(pGroup);
  309. return;
  310. }
  311. strcpy(pGroup->pszName, psz);
  312. pGroup->ptl = NULL;
  313. psz += strlen(psz) + 1;
  314. while (*psz == '\t') {
  315. psz++;
  316. ptl = Alloc(sizeof(TITLELIST));
  317. if (ptl == NULL) {
  318. Free(pBuf);
  319. return;
  320. }
  321. ptl->pszTitle = Alloc(strlen(psz));
  322. if (ptl->pszTitle == NULL) {
  323. Free(pBuf);
  324. Free(ptl);
  325. return;
  326. }
  327. strcpy(ptl->pszTitle, psz);
  328. psz += strlen(psz) + 2;
  329. ptl->pszClass = Alloc(strlen(psz));
  330. if (ptl->pszClass == NULL) {
  331. Free(pBuf);
  332. Free(ptl);
  333. return;
  334. }
  335. strcpy(ptl->pszClass, psz);
  336. psz += strlen(psz) + 1;
  337. ptl->next = pGroup->ptl;
  338. pGroup->ptl = ptl;
  339. }
  340. /*
  341. * Restore groups to origional order.
  342. */
  343. if (pGroupEnd == NULL) {
  344. pGroups = pGroup;
  345. pGroupEnd = pGroup;
  346. } else {
  347. pGroupEnd->next = pGroup;
  348. pGroupEnd = pGroup;
  349. }
  350. pGroup->next = NULL;
  351. }
  352. Free(pBuf);
  353. /*
  354. * Set default current group to first in list.
  355. */
  356. if (pGroups != NULL) {
  357. pszCurrentGroup = pGroups->pszName;
  358. }
  359. }
  360. }
  361. PTITLELIST FindPtl(LPSTR pszWindowName)
  362. {
  363. PGROUP pGroup;
  364. PTITLELIST ptl;
  365. if ((pGroup = FindGroup(GetCurrentGroup())) == NULL) {
  366. return(NULL);
  367. }
  368. ptl = pGroup->ptl;
  369. while (ptl != NULL) {
  370. if (_stricmp(ptl->pszTitle, pszWindowName) == 0) {
  371. return(ptl);
  372. }
  373. return(NULL);
  374. }
  375. }