LINUX: afs_create infinite fetchStatus loop
[pkg-k5-afs_openafs.git] / src / WINNT / client_exp / submounts_dlg.cpp
blob0026716105e39b24f133cf65b4acad0827b95ff8
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
10 #include "stdafx.h"
11 #include <winsock2.h>
12 #include <ws2tcpip.h>
14 extern "C" {
15 #include <afs/param.h>
16 #include <afs/stds.h>
19 #include "submounts_dlg.h"
20 #include "add_submount_dlg.h"
21 #include "msgs.h"
22 #include "submount_info.h"
23 #include "hourglass.h"
24 #include <WINNT\afsreg.h>
26 #ifdef _DEBUG
27 #define new DEBUG_NEW
28 #undef THIS_FILE
29 static char THIS_FILE[] = __FILE__;
30 #endif
32 #define PCCHAR(str) ((char *)(const char *)(str))
35 /////////////////////////////////////////////////////////////////////////////
36 // CSubmountsDlg property page
38 IMPLEMENT_DYNCREATE(CSubmountsDlg, CDialog)
40 static CSubmountInfo *ReadSubmtInfo(const CString& strShareName)
42 HOURGLASS hourglass;
44 CSubmountInfo *pInfo = 0;
46 DWORD len;
48 TCHAR pathName[1024];
50 HKEY hkSubmounts;
51 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
52 AFSREG_CLT_OPENAFS_SUBKEY TEXT("\\Submounts"),
54 TEXT("AFS"),
55 REG_OPTION_NON_VOLATILE,
56 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_READ,
57 NULL,
58 &hkSubmounts,
59 NULL );
61 DWORD dwType;
62 DWORD status;
63 len = sizeof(pathName);
64 status = RegQueryValueEx( hkSubmounts, strShareName, 0,
65 &dwType, (LPBYTE)pathName, &len);
66 RegCloseKey( hkSubmounts );
68 if (status || len == 0)
69 return pInfo;
71 pInfo = new CSubmountInfo();
72 pInfo->SetShareName(strShareName);
73 pInfo->SetPathName(pathName);
75 return pInfo;
78 CSubmountsDlg::CSubmountsDlg() : CDialog()
80 InitModalIndirect (TaLocale_GetDialogResource (CSubmountsDlg::IDD));
82 //{{AFX_DATA_INIT(CSubmountsDlg)
83 // NOTE: the ClassWizard will add member initialization here
84 //}}AFX_DATA_INIT
86 m_bAddOnlyMode = FALSE;
89 CSubmountsDlg::~CSubmountsDlg()
91 for (int i = 0; i < m_ToDo.GetSize(); i++)
92 delete m_ToDo[i];
94 m_ToDo.RemoveAll();
97 void CSubmountsDlg::DoDataExchange(CDataExchange* pDX)
99 CDialog::DoDataExchange(pDX);
100 //{{AFX_DATA_MAP(CSubmountsDlg)
101 DDX_Control(pDX, IDC_DELETE, m_Delete);
102 DDX_Control(pDX, IDC_CHANGE, m_Change);
103 DDX_Control(pDX, IDC_LIST, m_SubmtList);
104 //}}AFX_DATA_MAP
108 BEGIN_MESSAGE_MAP(CSubmountsDlg, CDialog)
109 //{{AFX_MSG_MAP(CSubmountsDlg)
110 ON_BN_CLICKED(IDC_ADD, OnAdd)
111 ON_BN_CLICKED(IDC_CHANGE, OnChange)
112 ON_BN_CLICKED(IDC_DELETE, OnDelete)
113 ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
114 ON_BN_CLICKED(IDOK, OnOk)
115 //}}AFX_MSG_MAP
116 END_MESSAGE_MAP()
118 /////////////////////////////////////////////////////////////////////////////
119 // CSubmountsDlg message handlers
121 BOOL CSubmountsDlg::OnInitDialog()
123 CDialog::OnInitDialog();
125 if (m_bAddOnlyMode) {
126 TCHAR szRemoteName[MAX_PATH];
127 ULONG nBufSize = sizeof(szRemoteName);
129 if (WNetGetConnection(m_strAddOnlyPath.Left(2), szRemoteName, &nBufSize) == ERROR_SUCCESS) {
130 CString strAfsShare(szRemoteName);
131 int nSlashPos = strAfsShare.ReverseFind('\\');
132 if (nSlashPos > -1) {
133 strAfsShare = strAfsShare.Mid(nSlashPos + 1);
135 // Get the submount info for this share name
136 CSubmountInfo *pInfo = ReadSubmtInfo(strAfsShare);
137 CString strSharePath;
138 if (pInfo != 0) {
139 ASSERT_VALID(pInfo);
140 strSharePath = pInfo->GetPathName();
143 m_strAddOnlyPath = strSharePath + m_strAddOnlyPath.Mid(2);
147 OnAdd();
148 OnOk();
150 return TRUE;
153 if (!FillSubmtList()) {
154 // ShowMessageBox(IDS_GET_CELL_LIST_ERROR);
155 // EndDialog(0);
156 // return TRUE;
159 return TRUE; // return TRUE unless you set the focus to a control
160 // EXCEPTION: OCX Property Pages should return FALSE
163 BOOL CSubmountsDlg::FillSubmtList()
165 HOURGLASS hourglass;
167 HKEY hkSubmounts;
168 DWORD dwType;
169 DWORD dwIndex;
170 DWORD dwSubmounts;
172 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
173 AFSREG_CLT_OPENAFS_SUBKEY TEXT("\\Submounts"),
175 TEXT("AFS"),
176 REG_OPTION_NON_VOLATILE,
177 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_READ|KEY_WRITE|KEY_QUERY_VALUE,
178 NULL,
179 &hkSubmounts,
180 NULL );
182 RegQueryInfoKey( hkSubmounts,
183 NULL, /* lpClass */
184 NULL, /* lpcClass */
185 NULL, /* lpReserved */
186 NULL, /* lpcSubKeys */
187 NULL, /* lpcMaxSubKeyLen */
188 NULL, /* lpcMaxClassLen */
189 &dwSubmounts, /* lpcValues */
190 NULL, /* lpcMaxValueNameLen */
191 NULL, /* lpcMaxValueLen */
192 NULL, /* lpcbSecurityDescriptor */
193 NULL /* lpftLastWriteTime */
197 for ( dwIndex = 0; dwIndex < dwSubmounts; dwIndex ++ ) {
198 TCHAR submountName[256];
199 DWORD submountNameLen = sizeof(submountName);
201 RegEnumValue( hkSubmounts, dwIndex, submountName, &submountNameLen, NULL,
202 &dwType, NULL, NULL);
204 m_SubmtList.AddString(submountName);
207 RegCloseKey( hkSubmounts );
209 return TRUE;
212 void CSubmountsDlg::OnDelete()
214 HOURGLASS hourglass;
216 int nIndex = m_SubmtList.GetCurSel();
217 ASSERT(nIndex >= 0);
219 CString strSubmt;
220 CString strShareName;
221 m_SubmtList.GetText(nIndex, strSubmt);
223 ASSERT(!strSubmt.IsEmpty());
225 strShareName = strSubmt.SpanExcluding(_T("="));
227 if (ShowMessageBox(IDS_REALLY_DELETE_SUBMT, MB_YESNO | MB_ICONQUESTION, IDS_REALLY_DELETE_SUBMT, strShareName) != IDYES)
228 return;
230 m_SubmtList.DeleteString(nIndex);
232 if (m_SubmtList.GetCount() == 0) {
233 m_Delete.EnableWindow(FALSE);
234 m_Change.EnableWindow(FALSE);
237 CSubmountInfo *pInfo = new CSubmountInfo();
238 pInfo->SetShareName(strShareName);
239 pInfo->SetStatus(SIS_DELETED);
240 AddWork(pInfo);
243 void CSubmountsDlg::OnSelChangeList()
245 m_Delete.EnableWindow(TRUE);
246 m_Change.EnableWindow(TRUE);
249 static BOOL AddSubmt(CSubmountInfo *pInfo)
251 HOURGLASS hourglass;
253 HKEY hkSubmounts;
254 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
255 AFSREG_CLT_OPENAFS_SUBKEY TEXT("\\Submounts"),
257 TEXT("AFS"),
258 REG_OPTION_NON_VOLATILE,
259 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE,
260 NULL,
261 &hkSubmounts,
262 NULL );
264 DWORD status = RegSetValueEx( hkSubmounts, pInfo->GetShareName(), 0, REG_SZ,
265 (const BYTE *)(const TCHAR *) pInfo->GetPathName(),
266 pInfo->GetPathName().GetLength() + 1);
268 RegCloseKey(hkSubmounts);
269 return (status == ERROR_SUCCESS);
272 static BOOL DeleteSubmt(CSubmountInfo *pInfo)
274 HOURGLASS hourglass;
276 HKEY hkSubmounts;
277 RegCreateKeyEx( HKEY_LOCAL_MACHINE,
278 AFSREG_CLT_OPENAFS_SUBKEY TEXT("\\Submounts"),
280 TEXT("AFS"),
281 REG_OPTION_NON_VOLATILE,
282 (IsWow64()?KEY_WOW64_64KEY:0)|KEY_WRITE,
283 NULL,
284 &hkSubmounts,
285 NULL );
287 DWORD status = RegDeleteValue( hkSubmounts, pInfo->GetShareName());
289 RegCloseKey(hkSubmounts);
290 return (status == ERROR_SUCCESS);
293 void CSubmountsDlg::OnAdd()
295 CAddSubmtDlg dlg;
297 if (m_bAddOnlyMode) {
298 CSubmountInfo info("", m_strAddOnlyPath, SIS_ADDED);
299 dlg.SetSubmtInfo(&info);
302 dlg.DoModal();
304 CSubmountInfo *pInfo = dlg.GetSubmtInfo();
305 if (!pInfo)
306 return;
308 m_SubmtList.AddString(pInfo->GetShareName() + "=" + pInfo->GetPathName());
309 AddWork(pInfo);
312 void CSubmountsDlg::OnChange()
314 CAddSubmtDlg dlg;
316 HOURGLASS hourglass;
318 dlg.SetAddMode(FALSE);
320 int nIndex = m_SubmtList.GetCurSel();
321 ASSERT(nIndex >= 0);
323 CString strSubmt;
324 CString strShareName;
325 m_SubmtList.GetText(nIndex, strSubmt);
327 ASSERT(!strSubmt.IsEmpty());
329 strShareName = strSubmt.SpanExcluding(_T("="));
331 CSubmountInfo *pInfo = FindWork(strShareName);
332 if (pInfo != 0)
333 // Make a copy we can free below
334 pInfo = new CSubmountInfo(*pInfo);
335 else
336 pInfo = ReadSubmtInfo(strShareName);
338 if (!pInfo) {
339 ShowMessageBox(IDS_GET_SUBMT_INFO_ERROR, MB_ICONEXCLAMATION, IDS_GET_SUBMT_INFO_ERROR, strShareName);
340 return;
343 dlg.SetSubmtInfo(pInfo);
345 delete pInfo;
347 if (dlg.DoModal() != IDOK)
348 return;
350 pInfo = dlg.GetSubmtInfo();
352 m_SubmtList.DeleteString(nIndex);
353 m_SubmtList.InsertString(nIndex,
354 pInfo->GetShareName() + "=" + pInfo->GetPathName());
356 AddWork(pInfo);
359 void CSubmountsDlg::AddWork(CSubmountInfo *pInfo)
361 ASSERT_VALID(pInfo);
363 HOURGLASS hourglass;
365 BOOL bAdd = TRUE;
367 for (int i = 0; i < m_ToDo.GetSize(); i++) {
368 if (m_ToDo[i]->GetShareName() == pInfo->GetShareName()) {
369 if ((pInfo->GetStatus() == SIS_DELETED) && (m_ToDo[i]->GetStatus() == SIS_ADDED))
370 bAdd = FALSE;
371 delete m_ToDo[i];
372 m_ToDo.RemoveAt(i);
373 break;
377 if (bAdd)
378 m_ToDo.Add(pInfo);
381 BOOL CSubmountsDlg::FixSubmts()
383 for (int i = 0; i < m_ToDo.GetSize(); i++) {
384 SUBMT_INFO_STATUS status = m_ToDo[i]->GetStatus();
385 if ((status == SIS_ADDED) || (status == SIS_CHANGED))
386 if (!AddSubmt(m_ToDo[i]))
387 return FALSE;
388 if (status == SIS_DELETED)
389 if (!DeleteSubmt(m_ToDo[i]))
390 return FALSE;
393 return TRUE;
396 CSubmountInfo *CSubmountsDlg::FindWork(const CString& strShareName)
398 for (int i = 0; i < m_ToDo.GetSize(); i++)
399 if (m_ToDo[i]->GetShareName() == strShareName)
400 return m_ToDo[i];
402 return 0;
405 void CSubmountsDlg::WinHelp(DWORD dwData, UINT nCmd)
407 CDialog::WinHelp(dwData, nCmd);
410 void CSubmountsDlg::OnOk()
412 if (!FixSubmts())
413 ShowMessageBox(IDS_SUBMT_SAVE_FAILED);
415 CDialog::OnOK();
418 void CSubmountsDlg::SetAddOnlyMode(const CString& strAddOnlyPath)
420 m_bAddOnlyMode = TRUE;
421 m_strAddOnlyPath = strAddOnlyPath;