windows-talocale-20060829
[openafs.git] / src / WINNT / afsusrmgr / command.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 "TaAfsUsrMgr.h"
16 #include "command.h"
17 #include "window.h"
18 #include "creds.h"
19 #include "action.h"
20 #include "columns.h"
21 #include "cell_prop.h"
22 #include "options.h"
23 #include "usr_prop.h"
24 #include "usr_cpw.h"
25 #include "usr_create.h"
26 #include "usr_delete.h"
27 #include "grp_prop.h"
28 #include "grp_rename.h"
29 #include "grp_create.h"
30 #include "grp_delete.h"
31 #include "mch_create.h"
32 #include "mch_delete.h"
33
34
35 /*
36  * PROTOTYPES _________________________________________________________________
37  *
38  */
39
40 void ShowContextMenu (POPUPMENU pm, HWND hList, LPASIDLIST pAsidList, POINT *pptScreen);
41
42 void Command_OnView (WORD wCmd);
43 void Command_OnShowActions (void);
44 void Command_OnRefresh (void);
45 void Command_OnUnlock (void);
46 void Command_OnProperties (void);
47 void Command_OnMembership (void);
48 void Command_OnChangePassword (void);
49 void Command_OnRename (void);
50 void Command_OnCreateUser (void);
51 void Command_OnCreateGroup (void);
52 void Command_OnCreateMachine (void);
53 void Command_OnDelete (void);
54
55 void Command_OnKey_CtrlTab (BOOL fForward);
56 void Command_OnKey_Tab (BOOL fForward);
57 void Command_OnKey_Return (void);
58 void Command_OnKey_Menu (void);
59 void Command_OnKey_Esc (void);
60 void Command_OnKey_Properties (void);
61
62
63 /*
64  * POPUP-MENU SUPPORT _________________________________________________________
65  *
66  */
67
68 void OnRightClick (POPUPMENU pm, HWND hList, POINT *pptScreen)
69 {
70    if (IsWindow (hList))
71       {
72       // Which items are selected in the given list?  (Note: if we're
73       // responding to a WM_CONTEXTMENU, then the act of clicking the
74       // mouse button has already changed the selection as appropriate.)
75       //
76       LPASIDLIST pAsidList;
77       if ((pAsidList = Display_GetSelectedList()) != NULL)
78          {
79          if (pAsidList->cEntries == 0)
80             {
81             asc_AsidListFree (&pAsidList);
82             pAsidList = NULL;
83             }
84          }
85
86       // If we don't have screen coordinates, the user must have clicked
87       // the Menu key on the keyboard. Pick the center of the first
88       // selected item's region (or the center of the list if none),
89       // and display the appropriate menu.
90       //
91       if (!pptScreen)
92          {
93          RECT rWindow;
94          GetWindowRect (hList, &rWindow);
95
96          HLISTITEM hItemSelected = NULL;
97          if (pAsidList)
98             hItemSelected = FastList_FindItem (hList, (LPARAM)(pAsidList->aEntries[0].idObject));
99
100          POINT ptScreen;
101          if (!hItemSelected)
102             {
103             ptScreen.x = rWindow.left + (rWindow.right - rWindow.left)/2;
104             ptScreen.y = rWindow.top + (rWindow.bottom - rWindow.top)/2;
105             }
106          else
107             {
108             FASTLISTITEMREGIONS Reg;
109             FastList_GetItemRegions (hList, hItemSelected, &Reg);
110             ptScreen.x = rWindow.left + Reg.rLabel.left + (Reg.rLabel.right - Reg.rLabel.left)/2;
111             ptScreen.y = rWindow.top + Reg.rLabel.top + (Reg.rLabel.bottom - Reg.rLabel.top)/2;
112             ptScreen.x = min (max (ptScreen.x, rWindow.left), rWindow.right);
113             ptScreen.y = min (max (ptScreen.y, rWindow.top), rWindow.bottom);
114             }
115
116          ShowContextMenu (pm, hList, pAsidList, &ptScreen);
117          }
118       else
119          {
120          POINT ptClient = *pptScreen;
121          ScreenToClient (hList, &ptClient);
122
123          // Did the user click on the header bar for the list?
124          //
125          if (FL_HitTestForHeaderBar (hList, ptClient))
126             {
127             HMENU hm = TaLocale_LoadMenu (MENU_COLUMNS);
128             DisplayContextMenu (hm, *pptScreen, GetParent(hList));
129             }
130          else
131             {
132             ShowContextMenu (pm, hList, pAsidList, pptScreen);
133             }
134          }
135
136       if (pAsidList)
137          asc_AsidListFree (&pAsidList);
138       }
139 }
140
141
142 void ShowContextMenu (POPUPMENU pm, HWND hList, LPASIDLIST pAsidList, POINT *pptScreen)
143 {
144    HMENU hm = NULL;
145
146    switch (pm)
147       {
148       case pmUSER:
149          if (pAsidList)
150             {
151             hm = TaLocale_LoadMenu (MENU_USER);
152             if (pAsidList->cEntries > 1)
153                EnableMenu (hm, M_CPW, FALSE);
154             }
155          else // (!pAsidList)
156             {
157             hm = TaLocale_LoadMenu (MENU_USER_NONE);
158             Main_SetViewMenus (hm);
159             }
160          break;
161
162       case pmGROUP:
163          if (pAsidList)
164             {
165             hm = TaLocale_LoadMenu (MENU_GROUP);
166             if (pAsidList->cEntries > 1)
167                EnableMenu (hm, M_RENAME, FALSE);
168             }
169          else // (!pAsidList)
170             {
171             hm = TaLocale_LoadMenu (MENU_GROUP_NONE);
172             Main_SetViewMenus (hm);
173             }
174          break;
175
176       case pmMACHINE:
177          if (pAsidList)
178             {
179             hm = TaLocale_LoadMenu (MENU_MACHINE);
180             }
181          else // (!pAsidList)
182             {
183             hm = TaLocale_LoadMenu (MENU_MACHINE_NONE);
184             Main_SetViewMenus (hm);
185             }
186          break;
187       }
188
189    if (hm)
190       {
191       DisplayContextMenu (hm, *pptScreen, GetParent(hList));
192       }
193 }
194
195
196 /*
197  * ROUTINES ___________________________________________________________________
198  *
199  */
200
201 void OnContextCommand (WORD wCmd)
202 {
203    switch (wCmd)
204       {
205       // Menu commands
206       //
207       case M_OPENCELL:
208          (void)OpenCellDialog();
209          break;
210
211       case M_CREDENTIALS:
212          (void)NewCredsDialog();
213          break;
214
215       case M_EXIT:
216          Quit (0);
217          break;
218
219       case M_VIEW_LARGE:
220       case M_VIEW_SMALL:
221       case M_VIEW_DETAILS:
222       case M_VIEW_ONE:
223       case M_VIEW_TWO:
224       case M_VIEW_STATUS:
225          Command_OnView (wCmd);
226          break;
227
228       case M_ACTIONS:
229          Command_OnShowActions();
230          break;
231
232       case M_COLUMNS:
233          ShowColumnsDialog (g.hMain);
234          break;
235
236       case M_OPTIONS:
237          ShowOptionsDialog (g.hMain);
238          break;
239
240       case M_USER_CREATE:
241          Command_OnCreateUser();
242          break;
243
244       case M_GROUP_CREATE:
245          Command_OnCreateGroup();
246          break;
247
248       case M_MACHINE_CREATE:
249          Command_OnCreateMachine();
250          break;
251
252       case M_RENAME:
253          Command_OnRename();
254          break;
255
256       case M_DELETE:
257          Command_OnDelete();
258          break;
259
260       case M_CPW:
261          Command_OnChangePassword();
262          break;
263
264       case M_SELECTALL:
265          Display_SelectAll();
266          break;
267
268       case M_REFRESH:
269          Command_OnRefresh();
270          break;
271
272       case M_MEMBERSHIP:
273          Command_OnMembership();
274          break;
275
276       case M_UNLOCK:
277          Command_OnUnlock();
278          break;
279
280       case M_PROPERTIES:
281          Command_OnProperties();
282          break;
283
284       case M_CELL_PROPERTIES:
285          Cell_ShowProperties();
286          break;
287
288       case M_REFRESHALL:
289          StartTask (taskREFRESH, NULL, (PVOID)g.idCell);
290          break;
291
292       case M_CONTENTS:
293          WinHelp (g.hMain, cszHELPFILENAME, HELP_FINDER, 0);
294          break;
295
296       case M_FIND:
297          Help_FindCommand();
298          break;
299
300       case M_LOOKUP:
301          Help_FindError();
302          break;
303
304       case M_ABOUT:
305          Help_About();
306          break;
307
308       // Keyboard handlers (we use accelerator table entries to catch
309       // tab/return/esc/etc, and mimic a normal dialog box's behavior)
310       //
311       case M_KEY_RETURN:
312          Command_OnKey_Return();
313          break;
314
315       case M_KEY_CTRLTAB:
316          Command_OnKey_CtrlTab (TRUE);
317          break;
318
319       case M_KEY_CTRLBACKTAB:
320          Command_OnKey_CtrlTab (FALSE);
321          break;
322
323       case M_KEY_TAB:
324          Command_OnKey_Tab (TRUE);
325          break;
326
327       case M_KEY_BACKTAB:
328          Command_OnKey_Tab (FALSE);
329          break;
330
331       case M_KEY_MENU:
332          Command_OnKey_Menu();
333          break;
334
335       case M_KEY_ESC:
336          Command_OnKey_Esc();
337          break;
338
339       case M_KEY_PROPERTIES:
340          Command_OnKey_Properties();
341          break;
342       }
343 }
344
345
346 /*
347  * COMMAND HANDLERS _______________________________________________________________________________
348  *
349  */
350
351 void Command_OnView (WORD wCmd)
352 {
353    // Which tab is currently being displayed?
354    //
355    HWND hTab = GetDlgItem (g.hMain, IDC_TAB);
356    int iTab = TabCtrl_GetCurSel (hTab);
357
358    // Find the appropriate current view settings for this tab
359    //
360    LPVIEWINFO pvi = (iTab == 0) ? &gr.viewUsr : (iTab == 1) ? &gr.viewGrp : &gr.viewMch;
361    ICONVIEW iv = (iTab == 0) ? gr.ivUsr : (iTab == 1) ? gr.ivGrp : gr.ivMch;
362
363    // ...and prepare modified versions of them
364    //
365    VIEWINFO viNew = *pvi;
366    ICONVIEW ivNew = iv;
367
368    switch (wCmd)
369       {
370       case M_VIEW_LARGE:
371          viNew.lvsView = (viNew.lvsView & (~FLS_VIEW_MASK)) | FLS_VIEW_LARGE;
372          break;
373       case M_VIEW_SMALL:
374          viNew.lvsView = (viNew.lvsView & (~FLS_VIEW_MASK)) | FLS_VIEW_SMALL;
375          break;
376       case M_VIEW_DETAILS:
377          viNew.lvsView = (viNew.lvsView & (~FLS_VIEW_MASK)) | FLS_VIEW_LIST;
378          break;
379       case M_VIEW_TWO:
380          ivNew = ivTWOICONS;
381          break;
382       case M_VIEW_ONE:
383          ivNew = ivONEICON;
384          break;
385       case M_VIEW_STATUS:
386          ivNew = ivSTATUS;
387          break;
388       }
389
390    if ((viNew.lvsView & FLS_VIEW_MASK) != FLS_VIEW_LIST)
391       {
392       viNew.iSort = 0; // sort alphabetically by name
393       }
394
395    // Apply the changes, and update the main window's menu items
396    //
397    Display_RefreshView (&viNew, ivNew);
398    Main_SetMenus ();
399 }
400
401
402 void Command_OnShowActions (void)
403 {
404    if ((gr.fShowActions = !gr.fShowActions) == TRUE)
405       Actions_OpenWindow();
406    else
407       Actions_CloseWindow();
408    Main_SetMenus();
409 }
410
411
412 void Command_OnRefresh (void)
413 {
414    LPASIDLIST pAsidList;
415    if ((pAsidList = Display_GetSelectedList()) != NULL)
416       {
417       StartTask (taskREFRESHMULT, NULL, pAsidList);
418       }
419 }
420
421
422 void Command_OnUnlock (void)
423 {
424    LPASIDLIST pAsidList;
425    if ((pAsidList = Display_GetSelectedList()) != NULL)
426       {
427       StartTask (taskUSER_UNLOCK, NULL, pAsidList);
428       }
429 }
430
431
432 void Command_OnProperties (void)
433 {
434    LPASIDLIST pAsidList;
435    if ((pAsidList = Display_GetSelectedList()) != NULL)
436       {
437       // See what kind of objects are selected
438       //
439       BOOL fAnyUsers = FALSE;
440       BOOL fAnyGroups = FALSE;
441
442       for (size_t ii = 0; ii < pAsidList->cEntries; ++ii)
443          {
444          ASOBJTYPE Type;
445          if (!asc_ObjectTypeGet_Fast (g.idClient, g.idCell, pAsidList->aEntries[ ii ].idObject, &Type))
446             continue;
447          switch (Type)
448             {
449             case TYPE_USER:
450                fAnyUsers = TRUE;
451                break;
452             case TYPE_GROUP:
453                fAnyGroups = TRUE;
454                break;
455             }
456          }
457
458       // If they're homogenous, display the appropriate properties dialog
459       //
460       if (fAnyUsers && !fAnyGroups)
461          User_ShowProperties (pAsidList);
462       else if (fAnyGroups && !fAnyUsers)
463          Group_ShowProperties (pAsidList);
464       else
465          asc_AsidListFree (&pAsidList);
466       }
467 }
468
469
470 void Command_OnMembership (void)
471 {
472    LPASIDLIST pAsidList;
473    if ((pAsidList = Display_GetSelectedList()) != NULL)
474       {
475       // See what kind of objects are selected
476       //
477       BOOL fAnyUsers = FALSE;
478       BOOL fAnyGroups = FALSE;
479
480       for (size_t ii = 0; ii < pAsidList->cEntries; ++ii)
481          {
482          ASOBJTYPE Type;
483          if (!asc_ObjectTypeGet_Fast (g.idClient, g.idCell, pAsidList->aEntries[ ii ].idObject, &Type))
484             continue;
485          switch (Type)
486             {
487             case TYPE_USER:
488                fAnyUsers = TRUE;
489                break;
490             case TYPE_GROUP:
491                fAnyGroups = TRUE;
492                break;
493             }
494          }
495
496       // If they're homogenous, display the appropriate membership dialog
497       //
498       if (fAnyUsers && !fAnyGroups)
499          User_ShowProperties (pAsidList, uptMEMBERSHIP);
500       else if (fAnyGroups && !fAnyUsers)
501          Group_ShowProperties (pAsidList, gptMEMBERS);
502       else
503          asc_AsidListFree (&pAsidList);
504       }
505 }
506
507
508 void Command_OnChangePassword (void)
509 {
510    LPASIDLIST pAsidList;
511    if ((pAsidList = Display_GetSelectedList()) != NULL)
512       {
513       if (pAsidList->cEntries == 1)
514          {
515          ASOBJTYPE Type;
516          if (asc_ObjectTypeGet_Fast (g.idClient, g.idCell, pAsidList->aEntries[0].idObject, &Type))
517             {
518             if (Type == TYPE_USER)
519                User_ShowChangePassword (g.hMain, pAsidList->aEntries[0].idObject);
520             }
521          }
522       }
523 }
524
525
526 void Command_OnRename (void)
527 {
528    LPASIDLIST pAsidList;
529    if ((pAsidList = Display_GetSelectedList()) != NULL)
530       {
531       if (pAsidList->cEntries == 1)
532          {
533          ASOBJTYPE Type;
534          if (asc_ObjectTypeGet_Fast (g.idClient, g.idCell, pAsidList->aEntries[0].idObject, &Type))
535             {
536             if (Type == TYPE_GROUP)
537                Group_ShowRename (g.hMain, pAsidList->aEntries[0].idObject);
538             }
539          }
540       }
541 }
542
543
544 void Command_OnCreateUser (void)
545 {
546    User_ShowCreate (g.hMain);
547 }
548
549
550 void Command_OnCreateGroup (void)
551 {
552    Group_ShowCreate (g.hMain);
553 }
554
555
556 void Command_OnCreateMachine (void)
557 {
558    Machine_ShowCreate (g.hMain);
559 }
560
561
562 void Command_OnDelete (void)
563 {
564    LPASIDLIST pAsidList;
565    if ((pAsidList = Display_GetSelectedList()) != NULL)
566       {
567       // See what kind of objects are selected
568       //
569       BOOL fAnyUsers = FALSE;
570       BOOL fAnyGroups = FALSE;
571       BOOL fAnyMachines = FALSE;
572
573       for (size_t ii = 0; ii < pAsidList->cEntries; ++ii)
574          {
575          ASOBJTYPE Type;
576          if (!asc_ObjectTypeGet_Fast (g.idClient, g.idCell, pAsidList->aEntries[ ii ].idObject, &Type))
577             continue;
578          switch (Type)
579             {
580             case TYPE_USER:
581                if (fIsMachineAccount (pAsidList->aEntries[ ii ].idObject))
582                   fAnyMachines = TRUE;
583                else
584                   fAnyUsers = TRUE;
585                break;
586             case TYPE_GROUP:
587                fAnyGroups = TRUE;
588                break;
589             }
590          }
591
592       // If they're homogenous, display a matching delete confirmation dialog
593       //
594       if (fAnyUsers && !fAnyGroups && !fAnyMachines)
595          User_ShowDelete (pAsidList);
596       else if (fAnyGroups && !fAnyUsers && !fAnyMachines)
597          Group_ShowDelete (pAsidList);
598       else if (fAnyMachines && !fAnyUsers && !fAnyGroups)
599          Machine_ShowDelete (pAsidList);
600       else
601          asc_AsidListFree (&pAsidList);
602       }
603 }
604
605
606 /*
607  * KEYBOARD HANDLERS ______________________________________________________________________________
608  *
609  */
610
611 void Command_OnKey_Tab (BOOL fForward)
612 {
613    // The tab-cycle should go:
614    //    TabControl <-> TabChildControls
615    //
616    HWND hFocus = GetFocus();
617    HWND hTabChild = GetTabChild (GetDlgItem (g.hMain, IDC_TAB));
618
619    if (fForward)
620       {
621       if (hFocus == GetDlgItem (g.hMain, IDC_TAB))
622          {
623          PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)GetNextDlgTabItem (hTabChild, NULL, FALSE), TRUE);
624          }
625       else
626          {
627          if (GetNextDlgTabItem (hTabChild, hFocus, FALSE) == GetNextDlgTabItem (hTabChild, NULL, FALSE))
628             PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)GetDlgItem (g.hMain, IDC_TAB), TRUE);
629          else
630             PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)GetNextDlgTabItem (hTabChild, hFocus, FALSE), TRUE);
631          }
632       }
633    else // (!fForward)
634       {
635       if (hFocus == GetDlgItem (g.hMain, IDC_TAB))
636          {
637          PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)GetLastDlgTabItem (hTabChild), TRUE);
638          }
639       else
640          {
641          if (hFocus == GetNextDlgTabItem (hTabChild, NULL, FALSE))
642             PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)GetDlgItem (g.hMain, IDC_TAB), TRUE);
643          else
644             PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)GetNextDlgTabItem (hTabChild, hFocus, TRUE), TRUE);
645          }
646       }
647 }
648
649
650 void Command_OnKey_CtrlTab (BOOL fForward)
651 {
652    HWND hTabs = GetDlgItem (g.hMain, IDC_TAB);
653    size_t iTab = TabCtrl_GetCurSel (hTabs);
654    size_t cTabs = TabCtrl_GetItemCount (hTabs);
655
656    if (fForward)
657       iTab = (iTab == cTabs-1) ? (0) : (iTab+1);
658    else
659       iTab = (iTab == 0) ? (cTabs-1) : (iTab-1);
660
661    Main_PrepareTabChild (iTab);
662
663    PostMessage (g.hMain, WM_NEXTDLGCTL, (WPARAM)hTabs, TRUE);
664 }
665
666
667 void Command_OnKey_Return (void)
668 {
669    static NMHDR hdr;
670    hdr.hwndFrom = GetFocus();
671    hdr.idFrom = GetWindowLong (GetFocus(), GWL_ID);
672    hdr.code = FLN_LDBLCLICK;
673    PostMessage (GetParent (GetFocus()), WM_NOTIFY, 0, (LPARAM)&hdr);
674 }
675
676
677 void Command_OnKey_Menu (void)
678 {
679    HWND hFocus = GetFocus();
680    if (fIsFastList (hFocus))
681       {
682       // Is the user or group tab showing?
683       //
684       HWND hDlg = GetTabChild (GetDlgItem (g.hMain, IDC_TAB));
685       HWND hList = GetDlgItem (hDlg, IDC_GROUPS_LIST);
686       POPUPMENU pm = pmGROUP;
687       if (!IsWindow (hList))
688          {
689          hList = GetDlgItem (hDlg, IDC_USERS_LIST);
690          pm = pmUSER;
691          }
692       if (!IsWindow (hList))
693          {
694          hList = GetDlgItem (hDlg, IDC_MACHINES_LIST);
695          pm = pmMACHINE;
696          }
697
698       if (IsWindow (hList))
699          {
700          // Pretend the user right-clicked
701          //
702          OnRightClick (pm, hList);
703          }
704       }
705 }
706
707
708 void Command_OnKey_Esc (void)
709 {
710    HWND hFocus = GetFocus();
711    if (fIsFastList (hFocus))
712       {
713       FastList_SelectNone (hFocus);
714       }
715 }
716
717
718 void Command_OnKey_Properties (void)
719 {
720    OnContextCommand (M_PROPERTIES);
721 }
722