45 #include "imgIContainer.h"
46 #include "imgIRequest.h"
47 #include "nsIDOMDocument.h"
48 #include "nsIDOMElement.h"
49 #include "nsIDOMHTMLImageElement.h"
50 #include "nsIImageLoadingContent.h"
51 #include "nsIPrefService.h"
52 #include "nsIPrefLocalizedString.h"
53 #include "nsIServiceManager.h"
54 #include "nsIStringBundle.h"
55 #include "nsNetUtil.h"
58 #include "nsIProcess.h"
59 #include "nsICategoryManager.h"
61 #include "nsDirectoryServiceUtils.h"
62 #include "nsAppDirectoryServiceDefs.h"
63 #include "nsDirectoryServiceDefs.h"
64 #include "nsIWindowsRegKey.h"
65 #include "nsUnicharUtils.h"
73 #define _WIN32_WINNT 0x0600
83 #define REG_SUCCEEDED(val) \
84 (val == ERROR_SUCCESS)
86 #define REG_FAILED(val) \
87 (val != ERROR_SUCCESS)
98 const nsString &flatName = PromiseFlatString(aKeyName);
100 DWORD res = ::RegOpenKeyExW(aKeyRoot, flatName.get(), 0, KEY_READ, aKey);
104 case ERROR_ACCESS_DENIED:
105 return NS_ERROR_FILE_ACCESS_DENIED;
106 case ERROR_FILE_NOT_FOUND:
107 return NS_ERROR_NOT_AVAILABLE;
115 OpenKeyForWriting(HKEY aStartKey,
const nsAString& aKeyName, HKEY* aKey)
117 const nsString &flatName = PromiseFlatString(aKeyName);
120 DWORD res = ::RegCreateKeyExW(aStartKey, flatName.get(), 0, NULL,
121 0, KEY_READ | KEY_WRITE, NULL, aKey,
126 case ERROR_ACCESS_DENIED:
127 return NS_ERROR_FILE_ACCESS_DENIED;
128 case ERROR_FILE_NOT_FOUND:
129 res = ::RegCreateKeyExW(aStartKey, flatName.get(), 0, NULL,
130 0, KEY_READ | KEY_WRITE, NULL, aKey,
132 if (res != ERROR_SUCCESS)
133 return NS_ERROR_FILE_ACCESS_DENIED;
216 #define APP_REG_NAME L"Firefox"
217 #define CLS_HTML "FirefoxHTML"
218 #define CLS_URL "FirefoxURL"
219 #define CPL_DESKTOP L"Control Panel\\Desktop"
220 #define VAL_OPEN "\"%APPPATH%\" -requestPending -osint -url \"%1\""
221 #define VAL_FILE_ICON "%APPPATH%,1"
223 #define CPL_DESKTOP L"ControlPanel\\Desktop"
224 #define VAL_OPEN "\"%APPPATH%\" -osint -url \"%1\""
225 #define VAL_FILE_ICON "%APPPATH%,-2"
228 #define DI "\\DefaultIcon"
229 #define SOP "\\shell\\open\\command"
232 #define MAKE_KEY_NAME1(PREFIX, MID) \
275 #if MOZ_WINSDK_TARGETVER >= MOZ_NTDDI_LONGHORN
276 IApplicationAssociationRegistration* pAAR;
278 HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
281 IID_IApplicationAssociationRegistration,
285 hr = pAAR->QueryAppIsDefaultAll(AL_EFFECTIVE,
298 nsWindowsShellService::IsDefaultBrowser(PRBool aStartupCheck,
299 PRBool* aIsDefaultBrowser)
305 mCheckedThisSession = PR_TRUE;
310 *aIsDefaultBrowser = PR_TRUE;
313 if (!::GetModuleFileNameW(0, exePath,
MAX_BUF))
314 return NS_ERROR_FAILURE;
319 if (!::GetLongPathNameW(exePath, exePath,
MAX_BUF))
320 return NS_ERROR_FAILURE;
323 nsAutoString appLongPath(exePath);
327 for (settings = gSettings; settings < end; ++
settings) {
328 NS_ConvertUTF8toUTF16 dataLongPath(settings->
valueData);
331 PRInt32
offset = dataLongPath.Find(
"%APPPATH%");
332 dataLongPath.Replace(offset, 9, appLongPath);
334 ::ZeroMemory(currValue,
sizeof(currValue));
338 *aIsDefaultBrowser = PR_FALSE;
342 DWORD len =
sizeof currValue;
343 DWORD res = ::RegQueryValueExW(theKey, PromiseFlatString(
value).
get(),
344 NULL, NULL, (LPBYTE)currValue, &len);
346 ::RegCloseKey(theKey);
348 !dataLongPath.Equals(currValue, CaseInsensitiveCompare)) {
350 *aIsDefaultBrowser = PR_FALSE;
358 if (*aIsDefaultBrowser)
366 nsWindowsShellService::SetDefaultBrowser(PRBool aClaimAllTypes, PRBool aForAllUsers)
370 nsCOMPtr<nsIProperties> directoryService =
371 do_GetService(NS_DIRECTORY_SERVICE_CONTRACTID, &rv);
372 NS_ENSURE_SUCCESS(rv, rv);
374 nsCOMPtr<nsILocalFile> appHelper;
375 rv = directoryService->Get(NS_XPCOM_CURRENT_PROCESS_DIR, NS_GET_IID(nsILocalFile), getter_AddRefs(appHelper));
376 NS_ENSURE_SUCCESS(rv, rv);
378 rv = appHelper->AppendNative(NS_LITERAL_CSTRING(
"uninstall"));
379 NS_ENSURE_SUCCESS(rv, rv);
381 rv = appHelper->AppendNative(NS_LITERAL_CSTRING(
"helper.exe"));
382 NS_ENSURE_SUCCESS(rv, rv);
384 nsAutoString appHelperPath;
385 rv = appHelper->GetPath(appHelperPath);
386 NS_ENSURE_SUCCESS(rv, rv);
389 appHelperPath.AppendLiteral(
" /SetAsDefaultAppGlobal");
391 appHelperPath.AppendLiteral(
" /SetAsDefaultAppUser");
394 STARTUPINFOW si = {
sizeof(si), 0};
395 PROCESS_INFORMATION pi = {0};
397 BOOL ok = CreateProcessW(NULL, (LPWSTR)appHelperPath.get(), NULL, NULL,
398 FALSE, 0, NULL, NULL, &si, &pi);
401 return NS_ERROR_FAILURE;
403 CloseHandle(pi.hProcess);
404 CloseHandle(pi.hThread);
410 if (!::GetModuleFileNameW(0, exePath,
MAX_BUF))
411 return NS_ERROR_FAILURE;
413 nsAutoString appLongPath(exePath);
416 SetRegKey(NS_LITERAL_STRING(
".png"), EmptyString(),
417 NS_LITERAL_STRING(
"pngfile"));
418 SetRegKey(NS_LITERAL_STRING(
".png"), NS_LITERAL_STRING(
"Content Type"),
419 NS_LITERAL_STRING(
"image/png"));
423 SetRegKey(NS_LITERAL_STRING(
".htm"), EmptyString(),
424 NS_LITERAL_STRING(
"htmlfile"));
425 SetRegKey(NS_LITERAL_STRING(
".html"), EmptyString(),
426 NS_LITERAL_STRING(
"htmlfile"));
427 SetRegKey(NS_LITERAL_STRING(
".bmp"), EmptyString(),
428 NS_LITERAL_STRING(
"bmpfile"));
429 SetRegKey(NS_LITERAL_STRING(
".gif"), EmptyString(),
430 NS_LITERAL_STRING(
"giffile"));
431 SetRegKey(NS_LITERAL_STRING(
".jpe"), EmptyString(),
432 NS_LITERAL_STRING(
"jpegfile"));
433 SetRegKey(NS_LITERAL_STRING(
".jpg"), EmptyString(),
434 NS_LITERAL_STRING(
"jpegfile"));
435 SetRegKey(NS_LITERAL_STRING(
".jpeg"), EmptyString(),
436 NS_LITERAL_STRING(
"jpegfile"));
438 for (settings = gSettings; settings < end; ++
settings) {
439 NS_ConvertUTF8toUTF16 dataLongPath(settings->
valueData);
442 PRInt32 offset = dataLongPath.Find(
"%APPPATH%");
443 dataLongPath.Replace(offset, 9, appLongPath);
444 SetRegKey(
key,
value, dataLongPath);
450 ::RegFlushKey(HKEY_CLASSES_ROOT);
458 nsWindowsShellService::SetRegKey(
const nsString& aKeyName,
459 const nsString& aValueName,
463 DWORD len =
sizeof buf;
466 nsresult rv = OpenKeyForWriting(HKEY_CLASSES_ROOT, aKeyName, &theKey);
471 DWORD res = ::RegQueryValueExW(theKey, PromiseFlatString(aValueName).
get(),
472 NULL, NULL, (LPBYTE)buf, &len);
476 nsAutoString current(buf);
477 if (
REG_FAILED(res) || !current.Equals(aValue)) {
478 const nsString &flatValue = PromiseFlatString(aValue);
480 ::RegSetValueExW(theKey, PromiseFlatString(aValueName).
get(),
481 0, REG_SZ, (
const BYTE *)flatValue.get(),
482 (flatValue.Length() + 1) *
sizeof(PRUnichar));
486 ::RegCloseKey(theKey);
491 nsWindowsShellService::GetShouldCheckDefaultBrowser(PRBool* aResult)
495 if (mCheckedThisSession) {
500 nsCOMPtr<nsIPrefBranch>
prefs;
503 pserve->GetBranch(
"", getter_AddRefs(prefs));
511 nsWindowsShellService::SetShouldCheckDefaultBrowser(PRBool aShouldCheck)
513 nsCOMPtr<nsIPrefBranch>
prefs;
516 pserve->GetBranch(
"", getter_AddRefs(prefs));
526 nsRefPtr<gfxImageSurface> image;
527 nsresult rv = aImage->CopyCurrentFrame(getter_AddRefs(image));
528 NS_ENSURE_SUCCESS(rv, rv);
530 PRInt32
width = image->Width();
531 PRInt32
height = image->Height();
533 PRUint8* bits = image->Data();
534 PRUint32 length = image->GetDataSize();
535 PRUint32 bpr = PRUint32(image->Stride());
536 PRInt32 bitCount = bpr/
width;
540 BITMAPINFOHEADER bmi;
541 bmi.biSize =
sizeof(BITMAPINFOHEADER);
545 bmi.biBitCount = (WORD)bitCount*8;
546 bmi.biCompression = BI_RGB;
547 bmi.biSizeImage = length;
548 bmi.biXPelsPerMeter = 0;
549 bmi.biYPelsPerMeter = 0;
551 bmi.biClrImportant = 0;
557 bf.bfOffBits =
sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
558 bf.bfSize = bf.bfOffBits + bmi.biSizeImage;
561 nsCOMPtr<nsIOutputStream> stream;
562 rv = NS_NewLocalFileOutputStream(getter_AddRefs(stream), aFile);
563 NS_ENSURE_SUCCESS(rv, rv);
566 rv = NS_ERROR_FAILURE;
569 stream->Write((
const char*)&bf,
sizeof(BITMAPFILEHEADER), &written);
570 if (written ==
sizeof(BITMAPFILEHEADER)) {
571 stream->Write((
const char*)&bmi,
sizeof(BITMAPINFOHEADER), &written);
572 if (written ==
sizeof(BITMAPINFOHEADER)) {
578 stream->Write(((
const char*)bits) + i, bpr, &written);
579 if (written == bpr) {
582 rv = NS_ERROR_FAILURE;
596 nsWindowsShellService::SetDesktopBackground(nsIDOMElement* aElement,
601 nsCOMPtr<imgIContainer> container;
602 nsCOMPtr<nsIDOMHTMLImageElement> imgElement(do_QueryInterface(aElement));
607 nsCOMPtr<nsIImageLoadingContent> imageContent =
608 do_QueryInterface(aElement, &rv);
613 nsCOMPtr<imgIRequest> request;
614 rv = imageContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
615 getter_AddRefs(request));
618 rv = request->GetImage(getter_AddRefs(container));
620 return NS_ERROR_FAILURE;
624 nsCOMPtr<nsIStringBundleService>
625 bundleService(do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv));
626 NS_ENSURE_SUCCESS(rv, rv);
628 nsCOMPtr<nsIStringBundle> shellBundle;
630 getter_AddRefs(shellBundle));
631 NS_ENSURE_SUCCESS(rv, rv);
634 nsString fileLeafName;
635 rv = shellBundle->GetStringFromName
636 (NS_LITERAL_STRING(
"desktopBackgroundLeafNameWin").
get(),
637 getter_Copies(fileLeafName));
638 NS_ENSURE_SUCCESS(rv, rv);
641 nsCOMPtr<nsIFile>
file;
642 rv = NS_GetSpecialDirectory(NS_APP_APPLICATION_REGISTRY_DIR,
643 getter_AddRefs(file));
644 NS_ENSURE_SUCCESS(rv, rv);
647 rv = file->Append(fileLeafName);
648 NS_ENSURE_SUCCESS(rv, rv);
651 rv = file->GetPath(path);
652 NS_ENSURE_SUCCESS(rv, rv);
658 if (NS_SUCCEEDED(rv)) {
659 PRBool result = PR_FALSE;
663 DWORD res = ::RegCreateKeyExW(HKEY_CURRENT_USER,
CPL_DESKTOP,
664 0, NULL, REG_OPTION_NON_VOLATILE,
665 KEY_WRITE, NULL, &key, &dwDisp);
668 PRUnichar tile[2],
style[2];
687 PRInt32 size = 3 *
sizeof(PRUnichar);
688 ::RegSetValueExW(key, L
"TileWallpaper",
689 0, REG_SZ, (
const BYTE *)tile, size);
690 ::RegSetValueExW(key, L
"WallpaperStyle",
691 0, REG_SZ, (
const BYTE *)style, size);
692 ::SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, (PVOID)path.get(),
693 SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
696 ::RegSetValueExW(key, L
"Tile",
697 0, REG_DWORD, (
const BYTE *)&tile,
sizeof(DWORD));
700 PRInt32 size = (path.Length() + 1) *
sizeof(PRUnichar);
701 ::RegSetValueExW(key, L
"Wallpaper",
702 0, REG_SZ, (
const BYTE *)path.get(), size);
703 ::SendMessage(HWND_BROADCAST, WM_SETTINGCHANGE, NULL, 0);
711 ::RegFlushKey(HKEY_CURRENT_USER);
719 nsWindowsShellService::OpenApplication(PRInt32 aApplication)
721 nsAutoString application;
722 switch (aApplication) {
724 application.AssignLiteral(
"Mail");
727 application.AssignLiteral(
"News");
748 DWORD type, len =
sizeof buf;
749 DWORD res = ::RegQueryValueExW(theKey, EmptyString().
get(), 0,
750 &type, (LPBYTE)&buf, &len);
756 ::RegCloseKey(theKey);
759 application.AppendLiteral(
"\\");
760 application.Append(buf);
761 application.AppendLiteral(
"\\shell\\open\\command");
767 ::ZeroMemory(buf,
sizeof(buf));
769 res = ::RegQueryValueExW(theKey, EmptyString().
get(), 0,
770 &type, (LPBYTE)&buf, &len);
772 return NS_ERROR_FAILURE;
775 ::RegCloseKey(theKey);
779 nsAutoString
path(buf);
780 PRInt32 end = path.Length();
781 PRInt32 cursor = 0, temp = 0;
782 ::ZeroMemory(buf,
sizeof(buf));
784 cursor = path.FindChar(
'%', cursor);
788 temp = path.FindChar(
'%', cursor + 1);
791 ::ZeroMemory(&buf,
sizeof(buf));
793 ::GetEnvironmentVariableW(nsAutoString(Substring(path, cursor, temp - cursor)).
get(),
798 path.Replace((cursor - 1), temp - cursor + 2, nsDependentString(buf));
802 while (cursor < end);
805 PROCESS_INFORMATION pi;
807 ::ZeroMemory(&si,
sizeof(STARTUPINFOW));
808 ::ZeroMemory(&pi,
sizeof(PROCESS_INFORMATION));
810 BOOL success = ::CreateProcessW(NULL, (LPWSTR)path.get(), NULL,
811 NULL, FALSE, 0, NULL, NULL,
814 return NS_ERROR_FAILURE;
820 nsWindowsShellService::GetDesktopBackgroundColor(PRUint32* aColor)
822 PRUint32 color = ::GetSysColor(COLOR_DESKTOP);
823 *aColor = (GetRValue(color) << 16) | (GetGValue(color) << 8) | GetBValue(color);
828 nsWindowsShellService::SetDesktopBackgroundColor(PRUint32 aColor)
830 int aParameters[2] = { COLOR_BACKGROUND, COLOR_DESKTOP };
831 BYTE r = (aColor >> 16);
832 BYTE g = (aColor << 16) >> 24;
833 BYTE b = (aColor << 24) >> 24;
834 COLORREF colors[2] = { RGB(r,g,b), RGB(r,g,b) };
836 ::SetSysColors(
sizeof(aParameters) /
sizeof(
int), aParameters, colors);
841 PRBool result = PR_FALSE;
845 DWORD rv = ::RegCreateKeyExW(HKEY_CURRENT_USER,
846 L
"Control Panel\\Colors", 0, NULL,
847 REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL,
852 sprintf((
char*)rgb,
"%u %u %u\0", r, g, b);
853 NS_ConvertUTF8toUTF16 backColor(rgb);
855 ::RegSetValueExW(key, L
"Background",
856 0, REG_SZ, (
const BYTE *)backColor.get(),
857 (backColor.Length() + 1) *
sizeof(PRUnichar));
868 nsWindowsShellService::GetUnreadMailCount(PRUint32*
aCount)
874 DWORD type, unreadCount;
875 DWORD len =
sizeof unreadCount;
876 DWORD res = ::RegQueryValueExW(accountKey, L
"MessageCount", 0,
877 &type, (LPBYTE)&unreadCount, &len);
879 *aCount = unreadCount;
882 ::RegCloseKey(accountKey);
891 NS_NAMED_LITERAL_STRING(unread,
892 "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\UnreadMail\\");
895 DWORD res = ::RegOpenKeyExW(HKEY_CURRENT_USER, unread.get(), 0,
896 KEY_ENUMERATE_SUB_KEYS, &mailKey);
901 DWORD len =
sizeof subkeyName;
902 res = ::RegEnumKeyExW(mailKey, i++, subkeyName, &len, NULL, NULL,
906 res = ::RegOpenKeyExW(mailKey, PromiseFlatString(subkeyName).
get(),
907 0, KEY_READ, &accountKey);
909 *aResult = accountKey;
912 ::RegCloseKey(mailKey);
923 ::RegCloseKey(mailKey);
929 nsWindowsShellService::OpenApplicationWithURI(nsILocalFile* aApplication,
930 const nsACString& aURI)
933 nsCOMPtr<nsIProcess> process =
934 do_CreateInstance(
"@mozilla.org/process/util;1", &rv);
938 rv = process->Init(aApplication);
942 const nsCString spec(aURI);
943 const char* specStr = spec.get();
944 return process->Run(PR_FALSE, &specStr, 1);
948 nsWindowsShellService::GetDefaultFeedReader(nsILocalFile** _retval)
953 nsCOMPtr<nsIWindowsRegKey> regKey =
954 do_CreateInstance(
"@mozilla.org/windows-registry-key;1", &rv);
955 NS_ENSURE_SUCCESS(rv, rv);
957 rv = regKey->Open(nsIWindowsRegKey::ROOT_KEY_CLASSES_ROOT,
958 NS_LITERAL_STRING(
"feed\\shell\\open\\command"),
960 NS_ENSURE_SUCCESS(rv, rv);
963 rv = regKey->ReadStringValue(EmptyString(), path);
964 NS_ENSURE_SUCCESS(rv, rv);
966 return NS_ERROR_FAILURE;
968 if (path.First() ==
'"') {
970 path = Substring(path, 1, path.FindChar(
'"', 1) - 1);
974 path = Substring(path, 0, path.FindChar(
' '));
977 nsCOMPtr<nsILocalFile> defaultReader =
978 do_CreateInstance(
"@mozilla.org/file/local;1", &rv);
979 NS_ENSURE_SUCCESS(rv, rv);
981 rv = defaultReader->InitWithPath(path);
982 NS_ENSURE_SUCCESS(rv, rv);
985 rv = defaultReader->Exists(&exists);
986 NS_ENSURE_SUCCESS(rv, rv);
988 return NS_ERROR_FAILURE;
990 NS_ADDREF(*_retval = defaultReader);
static SETTING gSettings[]
PRBool IsDefaultBrowserVista(PRBool *aIsDefaultBrowser)
const long APPLICATION_NEWS
#define REG_SUCCEEDED(val)
PRBool GetMailAccountKey(HKEY *aResult)
const long APPLICATION_MAIL
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
const NS_PREFSERVICE_CONTRACTID
#define PREF_CHECKDEFAULTBROWSER
const long BACKGROUND_TILE
NS_IMPL_ISUPPORTS2(sbAlbumArtService, sbIAlbumArtService, nsIObserver) NS_IMETHODIMP sbAlbumArtService
static nsresult WriteBitmap(nsIFile *aFile, imgIContainer *aImage)
#define SHELLSERVICE_PROPERTIES
_dialogDatepicker settings
static nsresult OpenKeyForReading(HKEY aKeyRoot, const nsAString &aKeyName, HKEY *aKey)
#define MAKE_KEY_NAME1(PREFIX, MID)
const long BACKGROUND_CENTER
restoreHistoryPrecursor aCount
_getSelectedPageStyle s i
const long BACKGROUND_STRETCH