Leaked source code of windows server 2003
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.

215 lines
9.3 KiB

  1. //
  2. // TOOLBAR.CPP
  3. //
  4. #include "precomp.h"
  5. static BOOL importToolbarInfoHelper(LPCTSTR pcszInsFile, LPCTSTR pcszToolbarWorkDir, LPCTSTR pcszToolbarInf,
  6. BOOL fImportToolbars);
  7. BOOL WINAPI ImportToolbarInfoA(LPCSTR pcszInsFile, LPCSTR pcszToolbarWorkDir, LPCSTR pcszToolbarInf,
  8. BOOL fImportToolbars)
  9. {
  10. USES_CONVERSION;
  11. return importToolbarInfoHelper(A2CT(pcszInsFile), A2CT(pcszToolbarWorkDir),
  12. A2CT(pcszToolbarInf), fImportToolbars);
  13. }
  14. BOOL WINAPI ImportToolbarInfoW(LPCWSTR pcwszInsFile, LPCWSTR pcwszToolbarWorkDir, LPCWSTR pcwszToolbarInf,
  15. BOOL fImportToolbars)
  16. {
  17. USES_CONVERSION;
  18. return importToolbarInfoHelper(W2CT(pcwszInsFile), W2CT(pcwszToolbarWorkDir),
  19. W2CT(pcwszToolbarInf), fImportToolbars);
  20. }
  21. static BOOL importQuickLaunchFiles(LPCTSTR pszSourceFileOrPath, LPCTSTR pszTargetPath,
  22. LPCTSTR pcszToolbarInf, LPCTSTR pszIns)
  23. {
  24. LPTSTR pszAuxFile;
  25. BOOL fResult;
  26. if (!PathFileExists(pszSourceFileOrPath))
  27. return FALSE;
  28. fResult = TRUE;
  29. if (!PathIsDirectory(pszSourceFileOrPath)) { // file
  30. TCHAR szTargetFile[MAX_PATH];
  31. TCHAR szBuf[16];
  32. UINT nNumFiles;
  33. fResult = PathCreatePath(pszTargetPath);
  34. if (!fResult)
  35. return FALSE;
  36. pszAuxFile = PathFindFileName(pszSourceFileOrPath);
  37. PathCombine(szTargetFile, pszTargetPath, pszAuxFile);
  38. SetFileAttributes(szTargetFile, FILE_ATTRIBUTE_NORMAL);
  39. fResult = CopyFile(pszSourceFileOrPath, szTargetFile, FALSE);
  40. if (!fResult)
  41. return FALSE;
  42. //----- Update the ins file -----
  43. nNumFiles = (UINT)GetPrivateProfileInt(QUICKLAUNCH, IK_NUMFILES, 0, pszIns);
  44. wnsprintf(szBuf, ARRAYSIZE(szBuf), TEXT("%u"), ++nNumFiles);
  45. WritePrivateProfileString(QUICKLAUNCH, IK_NUMFILES, szBuf, pszIns);
  46. ASSERT(nNumFiles > 0);
  47. wnsprintf(szBuf, ARRAYSIZE(szBuf), FILE_TEXT, nNumFiles - 1);
  48. WritePrivateProfileString(QUICKLAUNCH, szBuf, pszAuxFile, pszIns);
  49. }
  50. else { // directory
  51. // BUGBUG: Won't copy files in sub-dirs under pszSourceFileOrPath
  52. WIN32_FIND_DATA fd;
  53. TCHAR szSourceFile[MAX_PATH];
  54. TCHAR szLnkDesc[MAX_PATH];
  55. TCHAR szLnkFile[MAX_PATH];
  56. HANDLE hFindFile;
  57. StrCpy(szSourceFile, pszSourceFileOrPath);
  58. PathAddBackslash(szSourceFile);
  59. // remember the pos where the filename would get copied
  60. pszAuxFile = szSourceFile + lstrlen(szSourceFile);
  61. StrCpy(pszAuxFile, TEXT("*.*"));
  62. if (LoadString(g_hInst, IDS_IELNK, szLnkDesc, ARRAYSIZE(szLnkDesc)) == 0)
  63. StrCpy(szLnkDesc, TEXT("Launch Internet Explorer Browser"));
  64. StrCpy(szLnkFile, szLnkDesc);
  65. StrCat(szLnkFile, TEXT(".lnk"));
  66. // copy all the files in pszSourceFileOrPath to pszTargetPath
  67. hFindFile = FindFirstFile(szSourceFile, &fd);
  68. if (hFindFile != INVALID_HANDLE_VALUE) {
  69. fResult = TRUE;
  70. do {
  71. // skip ".", ".." and all sub-dirs
  72. if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  73. continue;
  74. if (StrCmpI(fd.cFileName, szLnkFile) == 0)
  75. {
  76. TCHAR szLnkTitle[MAX_PATH];
  77. TCHAR szQLName[64];
  78. TCHAR szInfSect[MAX_PATH*4];
  79. if (LoadString(g_hInst, IDS_IE, szLnkTitle, ARRAYSIZE(szLnkTitle)) == 0)
  80. StrCpy(szLnkTitle, TEXT("Internet Explorer"));
  81. if (LoadString(g_hInst, IDS_QUICK_LAUNCH, szQLName, ARRAYSIZE(szQLName)) == 0)
  82. StrCpy(szQLName, TEXT("Quick Launch"));
  83. ZeroMemory(szInfSect, sizeof(szInfSect));
  84. wnsprintf(szInfSect, ARRAYSIZE(szInfSect), BROWSERLNKSECT, szQLName, szLnkDesc, szLnkTitle);
  85. WritePrivateProfileSection(TEXT("AddQuick.Links"), szInfSect, pcszToolbarInf);
  86. WritePrivateProfileString(DEFAULTINSTALL, UPDATE_INIS, TEXT("AddQuick.Links"), pcszToolbarInf);
  87. WritePrivateProfileSection(TEXT("MSIExploreDestinationSecWin"), TEXT("49000=MSIExploreLDIDSection,5\r\n49050=QuickLinksLDIDSection,5\r\n\0\0"),
  88. pcszToolbarInf);
  89. WritePrivateProfileString(DEFAULTINSTALL, TEXT("CustomDestination"), TEXT("MSIExploreDestinationSecWin"), pcszToolbarInf);
  90. WritePrivateProfileSection(TEXT("MSIExploreLDIDSection"), TEXT("\"HKLM\",\"SOFTWARE\\Microsoft\\IE Setup\\Setup\",\"Path\",\"Internet Explorer 4.0\",\"%24%\\%PROGRAMF%\"\r\n\0\0"),
  91. pcszToolbarInf);
  92. WritePrivateProfileSection(TEXT("QuickLinksLDIDSection"), TEXT("\"HKCU\",\"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\",\"AppData\",\"Internet Explorer 4.0\",\"%25%\\Application Data\"\r\n\0\0"),
  93. pcszToolbarInf);
  94. WritePrivateProfileString(NULL, NULL, NULL, pcszToolbarInf);
  95. WritePrivateProfileString(QUICKLAUNCH, IK_KEEPIELNK, TEXT("1"), pszIns);
  96. continue;
  97. }
  98. StrCpy(pszAuxFile, fd.cFileName);
  99. // keep going even if copying of a file fails, but return FALSE in case of error
  100. fResult = fResult && importQuickLaunchFiles(szSourceFile, pszTargetPath, pcszToolbarInf, pszIns);
  101. } while (FindNextFile(hFindFile, &fd));
  102. FindClose(hFindFile);
  103. }
  104. }
  105. return fResult;
  106. }
  107. static BOOL importToolbarInfoHelper(LPCTSTR pcszInsFile, LPCTSTR pcszToolbarWorkDir, LPCTSTR pcszToolbarInf,
  108. BOOL fImportToolbars)
  109. {
  110. BOOL bRet = FALSE;
  111. HKEY hkToolbar;
  112. if (pcszInsFile == NULL || pcszToolbarWorkDir == NULL || pcszToolbarInf == NULL)
  113. return FALSE;
  114. // Before processing anything, first clear out the entries in the INS file and delete work dirs
  115. // clear out the entries in the INS file that correspond to importing toolbars
  116. WritePrivateProfileString(DESKTOP_OBJ_SECT, IMPORT_TOOLBARS, TEXT("0"), pcszInsFile);
  117. WritePrivateProfileString(EXTREGINF, TOOLBARS, NULL, pcszInsFile);
  118. // delete the QUICKLAUNCH section in the INS file
  119. WritePrivateProfileString(QUICKLAUNCH, NULL, NULL, pcszInsFile);
  120. // blow away the pcszToolbarWorkDir and pcszToolbarInf
  121. PathRemovePath(pcszToolbarWorkDir);
  122. PathRemovePath(pcszToolbarInf);
  123. if (!fImportToolbars)
  124. return TRUE;
  125. if (RegOpenKeyEx(HKEY_CURRENT_USER, KEY_TOOLBAR_VAL, 0, KEY_DEFAULT_ACCESS, &hkToolbar) == ERROR_SUCCESS)
  126. {
  127. TCHAR szQuickLaunchPath[MAX_PATH];
  128. DWORD cbSize = sizeof(szQuickLaunchPath);
  129. // prepare the quick launch folder path
  130. if (SHGetValue(HKEY_CURRENT_USER, SHELLFOLDERS_KEY, APPDATA_VALUE, NULL, (LPBYTE) szQuickLaunchPath, &cbSize) == ERROR_SUCCESS)
  131. {
  132. TCHAR szFullInfName[MAX_PATH];
  133. HANDLE hInf;
  134. // "Quick Launch" name is localizable; so read it from the resource
  135. if (LoadString(g_hInst, IDS_QUICK_LAUNCH, szFullInfName, ARRAYSIZE(szFullInfName)) == 0)
  136. StrCpy(szFullInfName, TEXT("Quick Launch"));
  137. PathAppend(szQuickLaunchPath, TEXT("Microsoft\\Internet Explorer"));
  138. PathAppend(szQuickLaunchPath, szFullInfName);
  139. if (PathIsFileSpec(pcszToolbarInf)) // create TOOLBAR.INF under pcszToolbarWorkDir
  140. PathCombine(szFullInfName, pcszToolbarWorkDir, pcszToolbarInf);
  141. else
  142. StrCpy(szFullInfName, pcszToolbarInf);
  143. // create TOOLBAR.INF file
  144. if ((hInf = CreateNewFile(szFullInfName)) != INVALID_HANDLE_VALUE)
  145. {
  146. DWORD_PTR dwRes;
  147. TCHAR szBuf[MAX_PATH];
  148. SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM) SAVE_TASKBARS,
  149. SMTO_NORMAL | SMTO_ABORTIFHUNG , 20000, &dwRes);
  150. // first, write the standard goo - [Version], [DefaultInstall], etc. - to TOOLBAR.INF
  151. WriteStringToFile(hInf, (LPCVOID) INF_ADD, StrLen(INF_ADD));
  152. ExportRegKey2Inf(hkToolbar, TEXT("HKCU"), KEY_TOOLBAR_VAL, hInf);
  153. WriteStringToFile(hInf, (LPCVOID) TEXT("\r\n"), 2);
  154. CloseFile(hInf);
  155. // copy all the quick launch files from the quick launch folder to pcszToolbarWorkDir
  156. importQuickLaunchFiles(szQuickLaunchPath, pcszToolbarWorkDir, szFullInfName, pcszInsFile);
  157. // update the INS file
  158. WritePrivateProfileString(DESKTOP_OBJ_SECT, IMPORT_TOOLBARS, TEXT("1"), pcszInsFile);
  159. WritePrivateProfileString(DESKTOP_OBJ_SECT, OPTION, TEXT("1"), pcszInsFile);
  160. wnsprintf(szBuf, ARRAYSIZE(szBuf), TEXT("*,%s,") IS_DEFAULTINSTALL, PathFindFileName(pcszToolbarInf));
  161. WritePrivateProfileString(IS_EXTREGINF, TOOLBARS, szBuf, pcszInsFile);
  162. bRet = TRUE;
  163. }
  164. }
  165. RegCloseKey(hkToolbar);
  166. }
  167. return bRet;
  168. }