| 1 | // HotKeyDlg.cpp |
|---|
| 2 | // $Id$ |
|---|
| 3 | // |
|---|
| 4 | // Eraser. Secure data removal. For Windows. |
|---|
| 5 | // Copyright © 1997-2001 Sami Tolvanen (sami@tolvanen.com). |
|---|
| 6 | // Copyright © 2001-2006 Garrett Trant (support@heidi.ie). |
|---|
| 7 | // Copyright © 2007 The Eraser Project |
|---|
| 8 | // |
|---|
| 9 | // This program is free software; you can redistribute it and/or |
|---|
| 10 | // modify it under the terms of the GNU General Public License |
|---|
| 11 | // as published by the Free Software Foundation; either version 2 |
|---|
| 12 | // of the License, or (at your option) any later version. |
|---|
| 13 | // |
|---|
| 14 | // This program is distributed in the hope that it will be useful, |
|---|
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | // GNU General Public License for more details. |
|---|
| 18 | // |
|---|
| 19 | // You should have received a copy of the GNU General Public License |
|---|
| 20 | // along with this program; if not, write to the Free Software |
|---|
| 21 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
|---|
| 22 | // 02111-1307, USA. |
|---|
| 23 | |
|---|
| 24 | #include "stdafx.h" |
|---|
| 25 | #include "Eraser.h" |
|---|
| 26 | #include "HotKeyDlg.h" |
|---|
| 27 | #include "KeyComboDlg.h" |
|---|
| 28 | #include <shared/key.h> |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | const LPCTSTR szAccelerKey = "Acceler"; |
|---|
| 32 | static const int iColumnCount = 2; |
|---|
| 33 | static const LPTSTR szColumnNames[] = |
|---|
| 34 | { |
|---|
| 35 | "Menu command", |
|---|
| 36 | "Hot key combination" |
|---|
| 37 | }; |
|---|
| 38 | |
|---|
| 39 | static const LPTSTR szDefAccelerKeys[] = |
|---|
| 40 | { |
|---|
| 41 | "E", |
|---|
| 42 | "M", |
|---|
| 43 | "U" |
|---|
| 44 | }; |
|---|
| 45 | |
|---|
| 46 | static int iColumnWidths[] = |
|---|
| 47 | { |
|---|
| 48 | 190, 110 |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | static LPCTSTR szCommandNames[] = |
|---|
| 53 | { |
|---|
| 54 | "Erase", |
|---|
| 55 | "Eraser Secure Move", |
|---|
| 56 | "Erase Unused Space" |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | static void CreateList(CListCtrl& lcHKey) |
|---|
| 60 | { |
|---|
| 61 | LVCOLUMN lvc; |
|---|
| 62 | ZeroMemory(&lvc, sizeof(LVCOLUMN)); |
|---|
| 63 | |
|---|
| 64 | lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH; |
|---|
| 65 | lvc.fmt = LVCFMT_LEFT; |
|---|
| 66 | lvc.pszText = szColumnNames[0]; |
|---|
| 67 | lvc.cx = iColumnWidths[0]; |
|---|
| 68 | lvc.iSubItem = 0; |
|---|
| 69 | lcHKey.InsertColumn(0, &lvc); |
|---|
| 70 | |
|---|
| 71 | lvc.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH; |
|---|
| 72 | lvc.fmt = LVCFMT_LEFT; |
|---|
| 73 | lvc.pszText = szColumnNames[1]; |
|---|
| 74 | lvc.cx = iColumnWidths[1]; |
|---|
| 75 | lvc.iSubItem = 1; |
|---|
| 76 | lcHKey.InsertColumn(1, &lvc); |
|---|
| 77 | |
|---|
| 78 | lcHKey.SetExtendedStyle(LVS_EX_HEADERDRAGDROP | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | __declspec(dllimport) bool no_registry; |
|---|
| 82 | |
|---|
| 83 | void InitRegistry() |
|---|
| 84 | { |
|---|
| 85 | CKey kReg_reg; |
|---|
| 86 | CIniKey kReg_ini; |
|---|
| 87 | CKey &kReg = no_registry ? kReg_ini : kReg_reg; |
|---|
| 88 | CString strPath = _T(""); |
|---|
| 89 | strPath.Format("%s\\%s", ERASER_REGISTRY_BASE, szAccelerKey); |
|---|
| 90 | |
|---|
| 91 | if (!no_registry) { |
|---|
| 92 | if (!kReg.Open(HKEY_CURRENT_USER, strPath, FALSE)) |
|---|
| 93 | { |
|---|
| 94 | if (kReg.Open(HKEY_CURRENT_USER, strPath, TRUE)) |
|---|
| 95 | { |
|---|
| 96 | for (int i = 0; i < iCommandCount; i++) { |
|---|
| 97 | CString strCommandKey = szDefAccelerKeys[i]; |
|---|
| 98 | kReg.SetValue(strCommandKey,szCommandNames[i]); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | kReg.Close(); |
|---|
| 102 | } |
|---|
| 103 | } else { |
|---|
| 104 | kReg.Open(HKEY_CURRENT_USER, strPath); |
|---|
| 105 | DWORD count = 0; |
|---|
| 106 | kReg.GetValue(count, "__count", 0); |
|---|
| 107 | if (!count) { |
|---|
| 108 | CString temp; |
|---|
| 109 | for (int i = 0; i < iCommandCount; i++) { |
|---|
| 110 | temp.Format("__key_%ld", i); |
|---|
| 111 | kReg.SetValue(szDefAccelerKeys[i], temp); |
|---|
| 112 | temp.Format("__value_%ld", i); |
|---|
| 113 | kReg.SetValue(szCommandNames[i], temp); |
|---|
| 114 | } |
|---|
| 115 | kReg.SetValue(iCommandCount, "__count"); |
|---|
| 116 | } |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void CHotKeyDlg::LoadValuesFromRegistry() |
|---|
| 121 | { |
|---|
| 122 | CKey kReg_reg; |
|---|
| 123 | CIniKey kReg_ini; |
|---|
| 124 | CKey &kReg = no_registry ? kReg_ini : kReg_reg; |
|---|
| 125 | CString strPath =_T(""); |
|---|
| 126 | CString strValueName=_T(""); |
|---|
| 127 | CString strValue=_T(""); |
|---|
| 128 | |
|---|
| 129 | strPath.Format("%s\\%s", ERASER_REGISTRY_BASE, szAccelerKey); |
|---|
| 130 | InitRegistry(); |
|---|
| 131 | |
|---|
| 132 | try |
|---|
| 133 | { |
|---|
| 134 | m_lcHotKeys.DeleteAllItems(); |
|---|
| 135 | if (kReg.Open(HKEY_CURRENT_USER, strPath)) |
|---|
| 136 | { |
|---|
| 137 | if (!no_registry) { |
|---|
| 138 | DWORD dwId = 0; |
|---|
| 139 | while (kReg.GetNextValueName(strValueName,dwId)) { |
|---|
| 140 | if (kReg.GetValue(strValue,strValueName)) |
|---|
| 141 | { |
|---|
| 142 | m_arKeyValues.SetAt(strValueName,strValue); |
|---|
| 143 | } |
|---|
| 144 | dwId++; |
|---|
| 145 | strValueName=""; |
|---|
| 146 | strValueName.ReleaseBuffer(); |
|---|
| 147 | } |
|---|
| 148 | } else { |
|---|
| 149 | DWORD count = 0; |
|---|
| 150 | kReg.GetValue(count, "__count", 0); |
|---|
| 151 | for (int i = 0; i < count; i++) { |
|---|
| 152 | CString temp, key, value; |
|---|
| 153 | temp.Format("__key_%ld", i); |
|---|
| 154 | kReg.GetValue(key, temp, ""); |
|---|
| 155 | temp.Format("__value_%ld", i); |
|---|
| 156 | kReg.GetValue(value, temp, ""); |
|---|
| 157 | if ((key.GetLength() > 0) && (value.GetLength() > 0)) |
|---|
| 158 | m_arKeyValues.SetAt(key, value); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | } |
|---|
| 163 | catch(...) |
|---|
| 164 | { |
|---|
| 165 | ASSERT(FALSE); |
|---|
| 166 | } |
|---|
| 167 | kReg.Close(); |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void CHotKeyDlg::UpdateHotKeyList(CListCtrl& lcHKey) |
|---|
| 171 | { |
|---|
| 172 | lcHKey.SetRedraw(FALSE); |
|---|
| 173 | |
|---|
| 174 | LV_ITEM lvi; |
|---|
| 175 | POSITION pos; |
|---|
| 176 | CString strKey = _T(""); |
|---|
| 177 | CString strCommand =_T(""); |
|---|
| 178 | |
|---|
| 179 | ZeroMemory(&lvi, sizeof(LV_ITEM)); |
|---|
| 180 | try |
|---|
| 181 | { |
|---|
| 182 | lcHKey.DeleteAllItems(); |
|---|
| 183 | BYTE nItem = 0; |
|---|
| 184 | for (pos = m_arKeyValues.GetStartPosition(); pos != NULL; nItem++) |
|---|
| 185 | { |
|---|
| 186 | m_arKeyValues.GetNextAssoc(pos,strCommand,strKey); |
|---|
| 187 | |
|---|
| 188 | lvi.mask = LVIF_TEXT ; |
|---|
| 189 | lvi.iItem = nItem; |
|---|
| 190 | lvi.iSubItem = 0; |
|---|
| 191 | lvi.pszText = strCommand.GetBuffer(strCommand.GetLength()); |
|---|
| 192 | lvi.iItem = lcHKey.InsertItem(&lvi); |
|---|
| 193 | |
|---|
| 194 | lvi.mask = LVIF_TEXT; |
|---|
| 195 | lvi.iSubItem = 1; |
|---|
| 196 | lvi.pszText = strKey.GetBuffer(strKey.GetLength()); //strCommandKey.GetBuffer(strTmp.GetLength()); |
|---|
| 197 | lcHKey.SetItem(&lvi); |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | catch (...) |
|---|
| 201 | { |
|---|
| 202 | ASSERT(FALSE); |
|---|
| 203 | } |
|---|
| 204 | lcHKey.SetRedraw(TRUE); |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | void CHotKeyDlg::saveListToRegistry() |
|---|
| 208 | { |
|---|
| 209 | CKey kReg_reg; |
|---|
| 210 | CIniKey kReg_ini; |
|---|
| 211 | CKey &kReg = no_registry ? kReg_ini : kReg_reg; |
|---|
| 212 | CString strPath; |
|---|
| 213 | |
|---|
| 214 | strPath.Format("%s\\%s", ERASER_REGISTRY_BASE, szAccelerKey); |
|---|
| 215 | |
|---|
| 216 | if (kReg.Open(HKEY_CURRENT_USER, strPath)) |
|---|
| 217 | { |
|---|
| 218 | CString strKey=_T(""), strCommand = _T(""); |
|---|
| 219 | POSITION pos; |
|---|
| 220 | int cnt = 0; |
|---|
| 221 | for (pos = m_arKeyValues.GetStartPosition(); pos != NULL;) |
|---|
| 222 | { |
|---|
| 223 | m_arKeyValues.GetNextAssoc(pos,strCommand,strKey); |
|---|
| 224 | if (!no_registry) { |
|---|
| 225 | kReg.SetValue(strKey,strCommand); |
|---|
| 226 | } else { |
|---|
| 227 | CString temp; |
|---|
| 228 | temp.Format("__key_%ld", cnt); |
|---|
| 229 | kReg.SetValue(strKey, temp); |
|---|
| 230 | temp.Format("__value_%ld", cnt); |
|---|
| 231 | kReg.SetValue(strCommand, temp); |
|---|
| 232 | } |
|---|
| 233 | cnt++; |
|---|
| 234 | } |
|---|
| 235 | if (no_registry) |
|---|
| 236 | kReg.SetValue(cnt, "__count"); |
|---|
| 237 | } |
|---|
| 238 | kReg.Close(); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | // CHotKeyDlg dialog |
|---|
| 242 | |
|---|
| 243 | IMPLEMENT_DYNAMIC(CHotKeyDlg, CDialog) |
|---|
| 244 | CHotKeyDlg::CHotKeyDlg(CWnd* pParent /*=NULL*/, int iValCnt) |
|---|
| 245 | : CDialog(CHotKeyDlg::IDD, pParent),m_arKeyValues(),m_lcHotKeys() |
|---|
| 246 | { |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | CHotKeyDlg::~CHotKeyDlg() |
|---|
| 250 | { |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | void CHotKeyDlg::DoDataExchange(CDataExchange* pDX) |
|---|
| 254 | { |
|---|
| 255 | CDialog::DoDataExchange(pDX); |
|---|
| 256 | DDX_Control(pDX, IDC_LIST_HOTKEYS, m_lcHotKeys); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | BOOL CHotKeyDlg::OnInitDialog() |
|---|
| 260 | { |
|---|
| 261 | try |
|---|
| 262 | { |
|---|
| 263 | CDialog::OnInitDialog(); |
|---|
| 264 | CreateList(m_lcHotKeys); |
|---|
| 265 | LoadValuesFromRegistry(); |
|---|
| 266 | UpdateHotKeyList(m_lcHotKeys); |
|---|
| 267 | GetDlgItem(IDCHANGE)->EnableWindow(FALSE); |
|---|
| 268 | } |
|---|
| 269 | catch(...) |
|---|
| 270 | { |
|---|
| 271 | ASSERT(FALSE); |
|---|
| 272 | } |
|---|
| 273 | return TRUE; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | BEGIN_MESSAGE_MAP(CHotKeyDlg, CDialog) |
|---|
| 278 | ON_BN_CLICKED(IDCHANGE, OnBnClickedChange) |
|---|
| 279 | ON_WM_ACTIVATE() |
|---|
| 280 | ON_NOTIFY(NM_CLICK, IDC_LIST_HOTKEYS, OnNMClickListHotkeys) |
|---|
| 281 | ON_BN_CLICKED(IDC_BUTTON_OKCHANGE, OnBnClickedButtonOk) |
|---|
| 282 | END_MESSAGE_MAP() |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | // CHotKeyDlg message handlers |
|---|
| 286 | |
|---|
| 287 | void CHotKeyDlg::OnBnClickedChange() |
|---|
| 288 | { |
|---|
| 289 | // TODO: Add your control notification handler code here |
|---|
| 290 | if (POSITION pos=m_lcHotKeys.GetFirstSelectedItemPosition()) |
|---|
| 291 | { |
|---|
| 292 | CString strKey; |
|---|
| 293 | strKey=m_lcHotKeys.GetItemText((int)pos-1,0); |
|---|
| 294 | CKeyComboDlg dlg(m_lcHotKeys.GetItemText((int)pos-1,1),strKey); |
|---|
| 295 | if (IDOK ==dlg.DoModal()) |
|---|
| 296 | { |
|---|
| 297 | m_arKeyValues.SetAt(strKey,dlg.m_strValue); |
|---|
| 298 | UpdateHotKeyList(m_lcHotKeys); |
|---|
| 299 | } |
|---|
| 300 | GetDlgItem(IDCHANGE)->EnableWindow(FALSE); |
|---|
| 301 | } |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | void CHotKeyDlg::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) |
|---|
| 305 | { |
|---|
| 306 | CDialog::OnActivate(nState, pWndOther, bMinimized); |
|---|
| 307 | |
|---|
| 308 | // TODO: Add your message handler code here |
|---|
| 309 | UpdateHotKeyList(m_lcHotKeys); |
|---|
| 310 | GetDlgItem(IDCHANGE)->EnableWindow(FALSE); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | |
|---|
| 315 | void CHotKeyDlg::OnNMClickListHotkeys(NMHDR *pNMHDR, LRESULT *pResult) |
|---|
| 316 | { |
|---|
| 317 | // TODO: Add your control notification handler code here |
|---|
| 318 | if (m_lcHotKeys.GetFirstSelectedItemPosition() != NULL) |
|---|
| 319 | GetDlgItem(IDCHANGE)->EnableWindow(TRUE); |
|---|
| 320 | else |
|---|
| 321 | GetDlgItem(IDCHANGE)->EnableWindow(FALSE); |
|---|
| 322 | *pResult = 0; |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | void CHotKeyDlg::OnBnClickedButtonOk() |
|---|
| 326 | { |
|---|
| 327 | saveListToRegistry(); |
|---|
| 328 | this->OnOK(); |
|---|
| 329 | } |
|---|