From 6d2ca771127fb9186372566e5671c79c354b04b9 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Sun, 10 Apr 2011 23:44:49 +0200 Subject: Register file type association on startup in windows. Installs keys to add MuPDF in the "Open With" menu, but does not take over the default association. Run "mupdf.exe -u" to remove the registry key. --- apps/win_main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/apps/win_main.c b/apps/win_main.c index 0054ca17..1e7a24b4 100644 --- a/apps/win_main.c +++ b/apps/win_main.c @@ -38,6 +38,66 @@ static pdfapp_t gapp; static wchar_t wbuf[1024]; static char filename[1024]; +/* + * Create registry keys to associate MuPDF with PDF and XPS files. + */ + +#define OPEN_KEY(parent, name, ptr) \ + RegCreateKeyExA(parent, name, 0, 0, 0, KEY_WRITE, 0, &ptr, 0) + +#define SET_KEY(parent, name, value) \ + RegSetValueExA(parent, name, 0, REG_SZ, value, strlen(value) + 1) + +void install_app(char *argv0) +{ + char buf[512]; + HKEY software, classes, mupdf, dotpdf, dotxps; + HKEY shell, open, command, supported_types; + HKEY pdf_progids, xps_progids; + + OPEN_KEY(HKEY_CURRENT_USER, "Software", software); + OPEN_KEY(software, "Classes", classes); + OPEN_KEY(classes, ".pdf", dotpdf); + OPEN_KEY(dotpdf, "OpenWithProgids", pdf_progids); + OPEN_KEY(classes, ".xps", dotxps); + OPEN_KEY(dotxps, "OpenWithProgids", xps_progids); + OPEN_KEY(classes, "MuPDF", mupdf); + OPEN_KEY(mupdf, "SupportedTypes", supported_types); + OPEN_KEY(mupdf, "shell", shell); + OPEN_KEY(shell, "open", open); + OPEN_KEY(open, "command", command); + + sprintf(buf, "\"%s\" \"%%1\"", argv0); + + SET_KEY(open, "FriendlyAppName", "MuPDF"); + SET_KEY(command, "", buf); + SET_KEY(supported_types, ".pdf", ""); + SET_KEY(supported_types, ".xps", ""); + SET_KEY(pdf_progids, "MuPDF", ""); + SET_KEY(xps_progids, "MuPDF", ""); + + RegCloseKey(dotxps); + RegCloseKey(dotpdf); + RegCloseKey(mupdf); + RegCloseKey(classes); + RegCloseKey(software); +} + +void uninstall_app(void) +{ + HKEY software, classes; + + RegOpenKeyExA(HKEY_CURRENT_USER, "Software", 0, KEY_ALL_ACCESS, &software); + RegOpenKeyExA(software, "Classes", 0, KEY_ALL_ACCESS, &classes); + + RegDeleteTreeA(classes, "MuPDF"); + + RegCloseKey(classes); + RegCloseKey(software); + + MessageBoxA(hwndframe, "MuPDF has been uninstalled.", "MuPDF", MB_ICONWARNING); +} + /* * Dialog boxes */ @@ -788,6 +848,7 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow { int argc; LPWSTR *argv = CommandLineToArgvW(GetCommandLineW(), &argc); + char argv0[256]; MSG msg; int fd; int code; @@ -796,11 +857,18 @@ WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShow pdfapp_init(&gapp); - /* associate(argv[0]); */ + GetModuleFileNameA(NULL, argv0, sizeof argv0); + install_app(argv0); + winopen(); if (argc == 2) { + if (argv[1][0] == '-' && argv[1][1] == 'u' && argv[1][2] == 0) + { + uninstall_app(); + exit(0); + } wcscpy(wbuf, argv[1]); } else -- cgit v1.2.3