home *** CD-ROM | disk | FTP | other *** search
- /*
- Miranda Installer - Installs nightlies and Miranda addons.
- Copyright (C) 2002-2003 Goblineye Entertainment
-
- Authors: Saar (Tornado) and Kai (kai_b)
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- #include "MirandaInstaller.h"
- #pragma hdrstop
-
-
- static bool Is_Using_NT(bool fOnlyNT4) // returns true if we're using an NT operating system
- {
- OSVERSIONINFO osInfo;
- osInfo.dwOSVersionInfoSize = sizeof(osInfo);
-
- if (!GetVersionEx(&osInfo))
- return(false);
-
- if (osInfo.dwPlatformId == VER_PLATFORM_WIN32_NT) // if we're using an NT OS and the OS isn't NT3.5
- {
- if ((osInfo.dwMajorVersion == 3) && (fOnlyNT4)) // it's actually NT3.5
- return(false);
- return(true);
- }
- else
- return(false);
- }
-
-
-
- static bool Get_ModulePath_From_Window(HWND hWnd,char *lpszoutPath,WORD wLen)
- {
- static bool bUsingNT4 = Is_Using_NT(true); // assuming that the OS doesn't change
- bool fRetVal = true;
-
- if (bUsingNT4) // only using NT4 and above
- {
- // get the module of the window
- HMODULE hModule = (HMODULE)GetWindowLongPtr(hWnd,GWLP_HINSTANCE);
-
- DWORD dwProcessID;
- GetWindowThreadProcessId(hWnd,&dwProcessID); // get process ID
-
- // open it with full access
- HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS,false,dwProcessID);
- HINSTANCE hPSAPI = LoadLibrary("psapi.dll");
-
- if ((hProcess != NULL) && (hPSAPI != NULL))
- {
- pGMFileNameEx pGetModuleFNX = (pGMFileNameEx)GetProcAddress(hPSAPI,"GetModuleFileNameExA");
- if (pGetModuleFNX != NULL) // if everything is ok
- {
- fRetVal = (pGetModuleFNX(hProcess,hModule,lpszoutPath,wLen)) ? (true) : (false);
- }
- else
- {
- fRetVal = false;
- }
- }
- else
- {
- fRetVal = false;
- }
-
- if (hPSAPI != NULL)
- {
- FreeLibrary(hPSAPI);
- }
-
- if (hProcess != NULL)
- {
- CloseHandle(hProcess); // close handle to process
- }
- }
- else
- {
- // win95/98/me way, very easy
- fRetVal = GetWindowModuleFileName(hWnd,lpszoutPath,wLen) > 0;
- }
- return(fRetVal);
- }
-
-
-
- BOOL CALLBACK EnumMirWndProc(HWND hWnd,LPARAM lParam)
- {
- char szName[32];
- GetClassName(hWnd,szName,32);
-
- // temporary fix to MultiWindow plugin bug. should be removed later.
- if (lstrcmp(szName,"Miranda") == 0) // it's miranda
- {
- char szWndPath[MAX_PATH + 1];
- if (!Get_ModulePath_From_Window(hWnd,szWndPath,MAX_PATH+1))
- {
- return(true);
- }
-
- // hmm
- // messy
- // but easy
- // if length is longer than our original, cut it
- int iLen = lstrlen(g_WorkOptions.szMirandaDirectory);
- if (lstrlen(szWndPath) > iLen)
- {
- szWndPath[iLen] = '\0'; // bah
- }
-
- if (lstrcmpi(szWndPath,g_WorkOptions.szMirandaDirectory) == 0) // found it
- {
- *((HWND*)lParam) = hWnd;
- return(false); // stop enumeration
- }
- }
- return(true);
- }
-
-
-
- // returns the handle to the first found window if the app is running
- // how this works: the enum proc tries to find a window in the system, which has the class "Miranda"
- // and resides in the same given miranda directory
- // we don't check the filename of the module, only the directory
- // that should be enough (checking the filename is no good, coz the module path given could be different then miranda32.exe)
- HWND Is_App_Running(void)
- {
- HWND hWnd = NULL; // will receive the handle, if found
- EnumWindows(EnumMirWndProc,(LPARAM)&hWnd); // enumerate windows (find miranda)
- return(hWnd);
- }
-
-
-
- // lpoutText is assumed to be big enough to hold this
- void Format_DisplaySize(char *lpoutText,DWORD dwSize) // format size into lpoutText
- {
- if (dwSize >= 1048576) // MB
- {
- wsprintf(lpoutText,Translate("%dMB"),dwSize / 1048576);
- }
- else if (dwSize >= 1024) // KB
- {
- wsprintf(lpoutText,Translate("%dKB"),dwSize / 1024);
- }
- else // bytes
- {
- wsprintf(lpoutText,Translate("%d bytes"),dwSize);
- }
- }
-
-
-
- // clean up a given file list, the general file list is used A LOT, so a cleanup function is a must
- void Cleanup_FileList(GENERAL_FILELIST **ppFileList)
- {
- if ((ppFileList == NULL) || (*ppFileList == NULL))
- {
- return;
- }
-
- GENERAL_FILELIST *pFileList = *ppFileList;
- while (pFileList != NULL)
- {
- GENERAL_FILELIST *pNext = pFileList->next;
- delete [] pFileList->lpFilename;
- delete pFileList;
- pFileList = pNext;
- }
- *ppFileList = NULL;
- }
-
-
-
- // pretty straight forward... converts slashes to backslashes
- // reverse means backslashes to slashes
- void Switch_Slashes(char *lpText,bool fReverse)
- {
- while (*lpText != '\0')
- {
- if (fReverse) // backslash to slash
- {
- if (*lpText == '\\')
- { *lpText = '/'; }
- }
- else if (*lpText == '/') // reverse it
- { *lpText = '\\'; }
-
- lpText++; // local copy
- }
- }
-
-
-
- // simple as that
- void Restart_Miranda(void)
- {
- char szPath[MAX_PATH + 1];
- lstrcpy(szPath,g_WorkOptions.szMirandaDirectory);
- lstrcat(szPath,"\\miranda32.exe");
- ShellExecute(NULL,"open",szPath,((*g_WorkOptions.szDefaultProfile == '\0') ? (NULL) : (g_WorkOptions.szDefaultProfile)),NULL,SW_SHOW);
- }
-
-
-
- bool RegisterApplication(const char *szTypeName, const char *szDescription)
- {
- HKEY hKey=NULL, hKeyTypeName=NULL;
- char buf[MAX_PATH+32];
-
- // register the type description
- RegCreateKeyEx(HKEY_CLASSES_ROOT,(LPCTSTR)szTypeName,0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKeyTypeName,NULL);
- RegSetValueEx(hKeyTypeName,"",0,REG_SZ,(const BYTE*)szDescription,lstrlen(szDescription)+1);
- DWORD dwValue = 0x10000;
- RegSetValueEx(hKeyTypeName,"EditFlags",0,REG_BINARY,(BYTE*)&dwValue,sizeof(dwValue));
-
- // register the program executable for this type name
- RegCreateKeyEx(hKeyTypeName,"shell\\open",0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
- // set a nice context menu command string
- RegSetValueEx(hKey,"",0,REG_SZ,(const BYTE*)Translate("&Install"),lstrlen(Translate("&Install"))+1);
- RegCreateKeyEx(hKey,"command",0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
- GetModuleFileName(NULL,buf+1,MAX_PATH); // build the command line
- *buf = '"';
- lstrcat(buf,"\" \"%1\"");
-
- // This is the most important line here. It should only fail if something went wrong before.
- if (RegSetValueEx(hKey,"",0,REG_SZ,(const BYTE*)buf,lstrlen(buf)+1)!=ERROR_SUCCESS)
- {
- if (hKey!=NULL) RegCloseKey(hKey);
- if (hKeyTypeName!=NULL) RegCloseKey(hKeyTypeName);
- return false;
- }
- RegCloseKey(hKey); // close this HKEY, we don't need it anymore.
-
- // The following lines may fail, we don't care much
-
- // set an icon for registered file types
- RegCreateKeyEx(hKeyTypeName,"DefaultIcon",0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
- GetModuleFileName(NULL,buf,MAX_PATH); // build an icon path
- lstrcat(buf,",1"); // ** document icon, use second icon in library
- RegSetValueEx(hKey,"",0,REG_SZ,(const BYTE*)buf,lstrlen(buf)+1);
- if (hKey!=NULL) RegCloseKey(hKey);
-
- // try to find ZIP file reg entry
- RegOpenKeyEx(HKEY_CLASSES_ROOT,".zip",0,KEY_READ,&hKey);
- int nSize = MAX_PATH;
- if (RegQueryValueEx(hKey,"",0,NULL,(LPBYTE)buf,(LPDWORD)&nSize)==ERROR_SUCCESS)
- {
- RegCloseKey(hKey);
- lstrcat(buf,"\\shell\\open\\command");
- // open and read shell/open/command from ZIP program reg key
- RegOpenKeyEx(HKEY_CLASSES_ROOT,buf,0,KEY_READ,&hKey);
- nSize = MAX_PATH;
- RegQueryValueEx(hKey,"",0,NULL,(LPBYTE)buf,(LPDWORD)&nSize);
- if (hKey!=NULL) RegCloseKey(hKey);
-
- // register 'Open as ZIP'
- RegCreateKeyEx(hKeyTypeName,"shell\\openZIP",0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
- // set a nice context menu command string
- RegSetValueEx(hKey,"",0,REG_SZ,(const BYTE*)Translate("Open as &ZIP"),lstrlen(Translate("Open as &ZIP"))+1);
- RegCreateKeyEx(hKey,"command",0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
- RegSetValueEx(hKey,"",0,REG_SZ,(const BYTE*)buf,lstrlen(buf)+1);
- }
-
- if (hKey!=NULL) RegCloseKey(hKey);
- if (hKeyTypeName!=NULL) RegCloseKey(hKeyTypeName);
- return true;
- }
-
-
-
- bool RegisterExtension(const char *szExtension, const char *szTypeName)
- {
- HKEY hKey;
-
- // register the type name for the extension
- RegCreateKeyEx(HKEY_CLASSES_ROOT,(LPCTSTR)szExtension,0,"",REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&hKey,NULL);
- if (RegSetValueEx(hKey,"",0,REG_SZ,(const BYTE*)szTypeName,lstrlen(szTypeName)+1)!=ERROR_SUCCESS) {
- if (hKey!=NULL) RegCloseKey(hKey);
- return false; // this is bad
- }
-
- // we set our own content type to increase compatibility with mozilla
- RegSetValueEx(hKey,"Content Type",0,REG_SZ,(const BYTE*)MI_CONTENT_TYPE,lstrlen(MI_CONTENT_TYPE)+1);
-
- if (hKey!=NULL) RegCloseKey(hKey);
- return true;
- }