Standardize License information
[openafs.git] / src / WINNT / client_exp / submounts_dlg.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "stdafx.h"
16 #include "submounts_dlg.h"
17 #include "add_submount_dlg.h"
18 #include "msgs.h"
19 #include "submount_info.h"
20 #include "hourglass.h"
21
22
23 #ifdef _DEBUG
24 #define new DEBUG_NEW
25 #undef THIS_FILE
26 static char THIS_FILE[] = __FILE__;
27 #endif
28
29 #define PCCHAR(str)             ((char *)(const char *)(str))
30
31
32 /////////////////////////////////////////////////////////////////////////////
33 // CSubmountsDlg property page
34
35 IMPLEMENT_DYNCREATE(CSubmountsDlg, CDialog)
36
37 static CSubmountInfo *ReadSubmtInfo(const CString& strShareName)
38 {
39         HOURGLASS hourglass;
40
41         CSubmountInfo *pInfo = 0;
42
43         DWORD len;
44
45         char pathName[1024];
46
47         len = GetPrivateProfileString("AFS Submounts",
48                                       PCCHAR(strShareName),
49                                       "", pathName, sizeof(pathName),
50                                       "afsdsbmt.ini");
51
52         if (len == 0 || len == sizeof(pathName) - 1)
53                 return pInfo;
54
55         pInfo = new CSubmountInfo();
56         pInfo->SetShareName(strShareName);
57         pInfo->SetPathName(pathName);
58
59         return pInfo;
60 }
61
62 CSubmountsDlg::CSubmountsDlg() : CDialog()
63 {
64         InitModalIndirect (TaLocale_GetDialogResource (CSubmountsDlg::IDD));
65
66         //{{AFX_DATA_INIT(CSubmountsDlg)
67                 // NOTE: the ClassWizard will add member initialization here
68         //}}AFX_DATA_INIT
69
70         m_bAddOnlyMode = FALSE;
71 }
72
73 CSubmountsDlg::~CSubmountsDlg()
74 {
75         for (int i = 0; i < m_ToDo.GetSize(); i++)
76                 delete m_ToDo[i];
77
78         m_ToDo.RemoveAll();
79 }
80
81 void CSubmountsDlg::DoDataExchange(CDataExchange* pDX)
82 {
83         CDialog::DoDataExchange(pDX);
84         //{{AFX_DATA_MAP(CSubmountsDlg)
85         DDX_Control(pDX, IDC_DELETE, m_Delete);
86         DDX_Control(pDX, IDC_CHANGE, m_Change);
87         DDX_Control(pDX, IDC_LIST, m_SubmtList);
88         //}}AFX_DATA_MAP
89 }
90
91
92 BEGIN_MESSAGE_MAP(CSubmountsDlg, CDialog)
93         //{{AFX_MSG_MAP(CSubmountsDlg)
94         ON_BN_CLICKED(IDC_ADD, OnAdd)
95         ON_BN_CLICKED(IDC_CHANGE, OnChange)
96         ON_BN_CLICKED(IDC_DELETE, OnDelete)
97         ON_LBN_SELCHANGE(IDC_LIST, OnSelChangeList)
98         ON_BN_CLICKED(IDOK, OnOk)
99         //}}AFX_MSG_MAP
100 END_MESSAGE_MAP()
101
102 /////////////////////////////////////////////////////////////////////////////
103 // CSubmountsDlg message handlers
104
105 BOOL CSubmountsDlg::OnInitDialog() 
106 {
107         CDialog::OnInitDialog();
108
109         if (m_bAddOnlyMode) {
110                 TCHAR szRemoteName[MAX_PATH];
111                 ULONG nBufSize = sizeof(szRemoteName);
112
113                 if (WNetGetConnection(m_strAddOnlyPath.Left(2), szRemoteName, &nBufSize) == ERROR_SUCCESS) {
114                         CString strAfsShare(szRemoteName);
115                         int nSlashPos = strAfsShare.ReverseFind('\\');
116                         if (nSlashPos > -1) {
117                                 strAfsShare = strAfsShare.Mid(nSlashPos + 1);
118
119                                 // Get the submount info for this share name
120                                 CSubmountInfo *pInfo = ReadSubmtInfo(strAfsShare);
121                                 CString strSharePath;
122                                 if (pInfo != 0) {
123                                         ASSERT_VALID(pInfo);
124                                         strSharePath = pInfo->GetPathName();
125                                 }
126
127                                 m_strAddOnlyPath = strSharePath + m_strAddOnlyPath.Mid(2);
128                         }
129                 }
130
131                 OnAdd();
132                 OnOk();
133
134                 return TRUE;
135         }
136
137         if (!FillSubmtList()) {
138 //              ShowMessageBox(IDS_GET_CELL_LIST_ERROR);
139 //              EndDialog(0);
140 //              return TRUE;
141         }
142
143         return TRUE;  // return TRUE unless you set the focus to a control
144                       // EXCEPTION: OCX Property Pages should return FALSE
145 }
146
147 BOOL CSubmountsDlg::FillSubmtList()
148 {
149         HOURGLASS hourglass;
150
151         DWORD lsize, size = 500;
152         char *buf = NULL, *next;
153
154         /*
155          * We don't know how large a buffer we need.  Keep doubling it until
156          * we're sure we have enough.
157          */
158         do {
159                 size *= 2;
160                 if (buf != NULL) free(buf);
161                 buf = (char *)malloc(size);
162                 lsize = GetPrivateProfileSection("AFS Submounts", buf, size,
163                                                  "afsdsbmt.ini");
164         }
165         while (lsize >= size - 2);
166
167         if (lsize != 0) {
168                 next = buf;
169                 do {
170                         m_SubmtList.AddString(next);
171                         next += (strlen(next) + 1);
172                 }
173                 while (*next);
174         }
175
176         free(buf);
177
178         return TRUE;
179 }
180
181 void CSubmountsDlg::OnDelete() 
182 {
183         HOURGLASS hourglass;
184         
185         int nIndex = m_SubmtList.GetCurSel();
186         ASSERT(nIndex >= 0);
187
188         CString strSubmt;
189         CString strShareName;
190         m_SubmtList.GetText(nIndex, strSubmt);
191
192         ASSERT(!strSubmt.IsEmpty());
193
194         strShareName = strSubmt.SpanExcluding("=");
195
196         if (ShowMessageBox(IDS_REALLY_DELETE_SUBMT, MB_YESNO | MB_ICONQUESTION, IDS_REALLY_DELETE_SUBMT, strShareName) != IDYES)
197                 return;
198
199         m_SubmtList.DeleteString(nIndex);
200
201         if (m_SubmtList.GetCount() == 0) {
202                 m_Delete.EnableWindow(FALSE);
203                 m_Change.EnableWindow(FALSE);
204         }
205
206         CSubmountInfo *pInfo = new CSubmountInfo();
207         pInfo->SetShareName(strShareName);
208         pInfo->SetStatus(SIS_DELETED);
209         AddWork(pInfo);
210 }
211
212 void CSubmountsDlg::OnSelChangeList() 
213 {
214         m_Delete.EnableWindow(TRUE);
215         m_Change.EnableWindow(TRUE);
216 }
217
218 static BOOL AddSubmt(CSubmountInfo *pInfo)
219 {
220         HOURGLASS hourglass;
221
222         BOOL written =
223                 WritePrivateProfileString("AFS Submounts",
224                                           PCCHAR(pInfo->GetShareName()),
225                                           PCCHAR(pInfo->GetPathName()),
226                                           "afsdsbmt.ini");
227
228         return written;
229 }
230
231 static BOOL DeleteSubmt(CSubmountInfo *pInfo)
232 {
233         HOURGLASS hourglass;
234
235         BOOL written =
236                 WritePrivateProfileString("AFS Submounts",
237                                           PCCHAR(pInfo->GetShareName()),
238                                           NULL,
239                                           "afsdsbmt.ini");
240         return written;
241 }
242
243 void CSubmountsDlg::OnAdd() 
244 {
245         CAddSubmtDlg dlg;
246
247         if (m_bAddOnlyMode) {
248                 CSubmountInfo info("", m_strAddOnlyPath, SIS_ADDED);
249                 dlg.SetSubmtInfo(&info);
250         }
251
252         dlg.DoModal();
253
254         CSubmountInfo *pInfo = dlg.GetSubmtInfo();
255         if (!pInfo)
256                 return;
257
258         m_SubmtList.AddString(pInfo->GetShareName() + "=" + pInfo->GetPathName());
259         AddWork(pInfo);
260 }
261
262 void CSubmountsDlg::OnChange() 
263 {
264         CAddSubmtDlg dlg;
265
266         HOURGLASS hourglass;
267
268         dlg.SetAddMode(FALSE);
269
270         int nIndex = m_SubmtList.GetCurSel();
271         ASSERT(nIndex >= 0);
272
273         CString strSubmt;
274         CString strShareName;
275         m_SubmtList.GetText(nIndex, strSubmt);
276
277         ASSERT(!strSubmt.IsEmpty());
278
279         strShareName = strSubmt.SpanExcluding("=");
280
281         CSubmountInfo *pInfo = FindWork(strShareName);
282         if (pInfo != 0)
283                 // Make a copy we can free below
284                 pInfo = new CSubmountInfo(*pInfo);
285         else
286                 pInfo = ReadSubmtInfo(strShareName);
287
288         if (!pInfo) {
289                 ShowMessageBox(IDS_GET_SUBMT_INFO_ERROR, MB_ICONEXCLAMATION, IDS_GET_SUBMT_INFO_ERROR, strShareName);
290                 return;
291         }
292
293         dlg.SetSubmtInfo(pInfo);
294         
295         delete pInfo;
296
297         if (dlg.DoModal() != IDOK)
298                 return;
299                 
300         pInfo = dlg.GetSubmtInfo();
301
302         m_SubmtList.DeleteString(nIndex);
303         m_SubmtList.InsertString(nIndex,
304                 pInfo->GetShareName() + "=" + pInfo->GetPathName());
305
306         AddWork(pInfo);
307 }
308
309 void CSubmountsDlg::AddWork(CSubmountInfo *pInfo)
310 {
311         ASSERT_VALID(pInfo);
312
313         HOURGLASS hourglass;
314
315         BOOL bAdd = TRUE;
316
317         for (int i = 0; i < m_ToDo.GetSize(); i++) {
318                 if (m_ToDo[i]->GetShareName() == pInfo->GetShareName()) {
319                         if ((pInfo->GetStatus() == SIS_DELETED) && (m_ToDo[i]->GetStatus() == SIS_ADDED))
320                                 bAdd = FALSE;
321                         delete m_ToDo[i];
322                         m_ToDo.RemoveAt(i);
323                         break;  
324                 }
325         }
326
327         if (bAdd)
328                 m_ToDo.Add(pInfo);
329 }
330
331 BOOL CSubmountsDlg::FixSubmts()
332 {
333         for (int i = 0; i < m_ToDo.GetSize(); i++) {
334                 SUBMT_INFO_STATUS status = m_ToDo[i]->GetStatus();
335                 if ((status == SIS_ADDED) || (status == SIS_CHANGED))
336                         if (!AddSubmt(m_ToDo[i]))
337                                 return FALSE;
338                 if (status == SIS_DELETED)
339                         if (!DeleteSubmt(m_ToDo[i]))
340                                 return FALSE;
341         }
342
343         return TRUE;
344 }
345
346 CSubmountInfo *CSubmountsDlg::FindWork(const CString& strShareName)
347 {
348         for (int i = 0; i < m_ToDo.GetSize(); i++)
349                 if (m_ToDo[i]->GetShareName() == strShareName)
350                         return m_ToDo[i];
351
352         return 0;
353 }
354
355 void CSubmountsDlg::WinHelp(DWORD dwData, UINT nCmd) 
356 {
357         CDialog::WinHelp(dwData, nCmd);
358 }
359
360 void CSubmountsDlg::OnOk() 
361 {
362         if (!FixSubmts())
363                 ShowMessageBox(IDS_SUBMT_SAVE_FAILED);
364
365         CDialog::OnOK();
366 }
367
368 void CSubmountsDlg::SetAddOnlyMode(const CString& strAddOnlyPath)
369 {
370         m_bAddOnlyMode = TRUE;
371         m_strAddOnlyPath = strAddOnlyPath;
372 }
373