tools/installer/ChooseMethod.cpp
author David Anderson <dvander@alliedmods.net>
Sun Nov 25 04:46:18 2007 +0000 (2007-11-25)
changeset 1722 2c16f160f5dd
parent 1651 077571ea8942
child 1723 befc8cfb4f3c
permissions -rw-r--r--
file copying now works, dialogs re-display at their last location, fixed up a bunch of interface mistakes, etc
     1 #include "InstallerMain.h"
     2 #include "InstallerUtil.h"
     3 #include "ChooseMethod.h"
     4 #include "Welcome.h"
     5 #include "GamesList.h"
     6 #include "SelectGame.h"
     7 
     8 unsigned int method_chosen = 0;
     9 TCHAR method_path[MAX_PATH];
    10 
    11 bool SelectFolder(HWND hOwner)
    12 {
    13 	BROWSEINFO info;
    14 	LPITEMIDLIST pidlist;
    15 	TCHAR path[MAX_PATH];
    16 
    17 	if (FAILED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED)))
    18 	{
    19 		return false;
    20 	}
    21 	
    22 	info.hwndOwner = hOwner;
    23 	info.pidlRoot = NULL;
    24 	info.pszDisplayName = path;
    25 	info.lpszTitle = _T("Select a game/mod folder");
    26 	info.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
    27 	info.lpfn = NULL;
    28 	info.lParam = 0;
    29 	info.iImage = 0;
    30 
    31 	if ((pidlist = SHBrowseForFolder(&info)) == NULL)
    32 	{
    33 		CoUninitialize();
    34 		return false;
    35 	}
    36 
    37 	/* This hellish code is from MSDN and translate shortcuts to real targets.
    38 	 * God almighty, I wish Window used real symlinks.
    39 	 */
    40 	bool acquire_success = false;
    41 	bool is_link = false;
    42 	IShellFolder *psf = NULL;
    43 	LPCITEMIDLIST new_item_list;
    44 	HRESULT hr;
    45 	
    46 	hr = SHBindToParent(pidlist, IID_IShellFolder, (void **)&psf, &new_item_list);
    47 	if (SUCCEEDED(hr))
    48 	{
    49 		IShellLink *psl = NULL;
    50 
    51 		hr = psf->GetUIObjectOf(hOwner, 1, &new_item_list, IID_IShellLink, NULL, (void **)&psl);
    52 		if (SUCCEEDED(hr))
    53 		{
    54 			LPITEMIDLIST new_item_list;
    55 
    56 			hr = psl->GetIDList(&new_item_list);
    57 			if (SUCCEEDED(hr))
    58 			{
    59 				is_link = true;
    60 
    61 				hr = SHGetPathFromIDList(new_item_list, method_path);
    62 				if (SUCCEEDED(hr))
    63 				{
    64 					acquire_success = true;
    65 				}
    66 
    67 				CoTaskMemFree(new_item_list);
    68 			}
    69 			psl->Release();
    70 		}
    71 		psf->Release();
    72 	}
    73 
    74 	if (!acquire_success && !is_link)
    75 	{
    76 		hr = SHGetPathFromIDList(pidlist, method_path);
    77 		if (SUCCEEDED(hr))
    78 		{
    79 			acquire_success = true;
    80 		}
    81 	}
    82 
    83 	/* That was awful.  shoo, shoo, COM */
    84 	CoTaskMemFree(pidlist);
    85 	CoUninitialize();
    86 
    87 	return acquire_success;
    88 }
    89 
    90 INT_PTR CALLBACK ChooseMethodHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    91 {
    92 	switch (message)
    93 	{
    94 	case WM_COMMAND:
    95 		{
    96 			if (LOWORD(wParam) == ID_METHOD_BACK)
    97 			{
    98 				UpdateGlobalPosition(hDlg);
    99 				EndDialog(hDlg, (INT_PTR)DisplayWelcome);
   100 				return (INT_PTR)TRUE;
   101 			}
   102 			else if (LOWORD(wParam) == ID_METHOD_EXIT
   103 					 || LOWORD(wParam) == ID_CLOSE)
   104 			{
   105 				return AskToExit(hDlg);
   106 			}
   107 			else if (LOWORD(wParam) == IDC_METHOD_DED_SERVER
   108 					 || LOWORD(wParam) == IDC_METHOD_ALONE_SERVER
   109 					 || LOWORD(wParam) == IDC_METHOD_LISTEN_SERVER
   110 					 || LOWORD(wParam) == IDC_METHOD_UPLOAD_FTP
   111 					 || LOWORD(wParam) == IDC_METHOD_CUSTOM_FOLDER)
   112 			{
   113 				method_chosen = LOWORD(wParam);
   114 				HWND button = GetDlgItem(hDlg, ID_METHOD_NEXT);
   115 				EnableWindow(button, TRUE);
   116 				break;
   117 			}
   118 			else if (LOWORD(wParam) == ID_METHOD_NEXT)
   119 			{
   120 				unsigned int game_type = 0;
   121 
   122 				switch (method_chosen)
   123 				{
   124 				case IDC_METHOD_DED_SERVER:
   125 					{
   126 						game_type = GAMES_DEDICATED;
   127 						break;
   128 					}
   129 				case IDC_METHOD_ALONE_SERVER:
   130 					{
   131 						game_type = GAMES_STANDALONE;
   132 						break;
   133 					}
   134 				case IDC_METHOD_LISTEN_SERVER:
   135 					{
   136 						game_type = GAMES_LISTEN;
   137 						break;
   138 					}
   139 				case IDC_METHOD_UPLOAD_FTP:
   140 					{
   141 						break;
   142 					}
   143 				case IDC_METHOD_CUSTOM_FOLDER:
   144 					{
   145 						int val;
   146 
   147 						if (!SelectFolder(hDlg))
   148 						{
   149 							break;
   150 						}
   151 						
   152 						val = IsValidFolder(method_path);
   153 						if (val != GAMEINFO_IS_USABLE)
   154 						{
   155 							DisplayBadFolderDialog(hDlg, val);
   156 							break;
   157 						}
   158 					}
   159 				}
   160 
   161 				if (game_type != 0)
   162 				{
   163 					int reason;
   164 
   165 					if ((reason = FindGames(game_type)) < 1)
   166 					{
   167 						DisplayBadGamesDialog(hDlg, game_type, reason);
   168 						break;
   169 					}
   170 
   171 					/* If we got a valid games list, we can display the next 
   172 					 * dialog box.
   173 					 */
   174 					UpdateGlobalPosition(hDlg);
   175 					EndDialog(hDlg, (INT_PTR)DisplaySelectGame);
   176 					return (INT_PTR)TRUE;
   177 				}
   178 			}
   179 			break;
   180 		}
   181 	case WM_INITDIALOG:
   182 		{
   183 			SetToGlobalPosition(hDlg);
   184 			return (INT_PTR)TRUE;
   185 		}
   186 	}
   187 
   188 	return (INT_PTR)FALSE;
   189 }
   190 
   191 void *DisplayChooseMethod(HWND hWnd)
   192 {
   193 	INT_PTR val;
   194 	
   195 	if ((val = DialogBox(
   196 		g_hInstance, 
   197 		MAKEINTRESOURCE(IDD_CHOOSE_METHOD),
   198 		hWnd,
   199 		ChooseMethodHandler)) == -1)
   200 	{
   201 		return NULL;
   202 	}
   203 
   204 	return (void *)val;
   205 }
   206