home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #12 / Amiga Plus CD - 2002 - No. 12.iso / AmigaOS / Aplus_Dev / AP-Website / download / pgp / pgp5gui-174b.lha / PGP5GUI-Src.lha / PGP5GUI-Src / SelPGPKeyGUI.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-23  |  7.7 KB  |  343 lines

  1. /*
  2. ** PGP5GUI - A GUI using Magic User Interface v3.8
  3. **
  4. ** Copyright 23-JUNE-1998 by Stefan Zakarias, All Rights Reserved.
  5. **
  6. ** This source code is released as FREEWARE - Use it for whatever you like,
  7. ** as long as NO financial reward is gained by you for such usage.
  8. **
  9. ** If you use any parts of the this source code for anything, give ME credit
  10. ** wherever credit is due, please ;-)
  11. */
  12.  
  13.  
  14. #ifndef MAKE_ID
  15. #define MAKE_ID(a,b,c,d) ((ULONG) (a)<<24 | (ULONG) (b)<<16 | (ULONG) (c)<<8 | (ULONG) (d))
  16. #endif
  17.  
  18. /* Types */
  19. #include <exec/types.h>
  20.  
  21. /* Libraries */
  22. #include <libraries/mui.h>
  23. #include <exec/memory.h>
  24.  
  25. /* Prototypes */
  26. #ifdef __GNUC__
  27. #include <clib/muimaster_protos.h>
  28. #include <proto/alib.h>
  29. #endif
  30.  
  31. #include <proto/muimaster.h>
  32. #include <clib/alib_protos.h>
  33. #include <proto/exec.h>
  34. #include <proto/dos.h>
  35.  
  36. /*  Ansi  */
  37. #include <string.h>
  38.  
  39. #include "PGP5GUI.h"
  40. #include "SelPGPKeyGUI.h"
  41.  
  42. extern char *keystable[];
  43. extern char *ReadyMSG;
  44.  
  45. /*
  46. ** A function used by the "listview" gadget to split a line
  47. ** into columns to be displayed by the "listview" gadget.
  48. */
  49. #ifdef __SASC
  50. #define REG(x) register __ ## x
  51.  
  52. static __saveds __asm void
  53. DisplayLI_TextFunc(REG(a0) struct Hook *hook, REG(a2) char **array, REG(a1) char *str)
  54. {
  55. #endif
  56. #ifdef __GNUC__
  57. static void
  58. DisplayLI_TextFunc(void)
  59. {
  60.     register char **a2 __asm("a2");           char **array = a2;
  61.     register char *a1 __asm("a1");            char *str = a1;
  62.     register struct Hook *a0 __asm("a0");    struct Hook *hook = a0;
  63. #endif
  64.  
  65.     static char algo[ALGO_SIZE];
  66.     static char keyID[KEYID_SIZE];
  67.     static char created[CREATED_SIZE];
  68.     static char userID[USERID_SIZE];
  69.     static char buffer[BUFFER_SIZE];
  70.  
  71.     char *indptr;
  72.  
  73.     /* Is this a string with text in it? */
  74.     if (str)
  75.     {
  76.         /*
  77.         ** We've got text in the string...
  78.         ** Break up the line and store it as columns.
  79.         */
  80.  
  81.         /* Get a copy of 'str' into 'buffer' */
  82.         strcpy(buffer, str);
  83.  
  84.         /* Get a pointer to 'buffer' */
  85.         indptr = buffer;
  86.  
  87.         /* NULL-terminate the 'Algo' field */
  88.         *(indptr + (ALGO_SIZE - 1)) = NULL;
  89.  
  90.         /* Copy the 'Algo' field to first array */
  91.         strcpy(algo, indptr);
  92.  
  93.         /* Increment string ptr. to 'KeyID' field */
  94.         indptr += ALGO_SIZE;
  95.  
  96.         /* NULL-terminate the 'KeyID' field */
  97.         *(indptr + (KEYID_SIZE - 1)) = NULL;
  98.  
  99.         /* Copy the 'KeyID' field to second array */
  100.         strcpy(keyID, indptr);
  101.  
  102.         /* Increment string ptr. to 'Created' field */
  103.         indptr += KEYID_SIZE;
  104.  
  105.         /* NULL-terminate the 'Created' field */
  106.         *(indptr + (CREATED_SIZE - 1)) = NULL;
  107.  
  108.         /* Copy the 'Created' field to third array */
  109.         strcpy(created, indptr);
  110.  
  111.         /* Increment string ptr. to 'UserID' field */
  112.         indptr += CREATED_SIZE;
  113.  
  114.         /* 'UserID' field is already NULL-terminated (EOL)
  115.         ** Copy the 'UserID' field to fourth array
  116.         */
  117.         strcpy(userID, indptr);
  118.  
  119.         array[0] = algo;
  120.         array[1] = keyID;
  121.         array[2] = created;
  122.         array[3] = userID;
  123.     }
  124.     else
  125.     {
  126.         /*
  127.         ** No text in the string...
  128.         ** A signal by the "listview" gadget that tells us
  129.         ** to re-initialise the title info for the columns.
  130.         */
  131.         array[0] = "\33b\33uAlgo";
  132.         array[1] = "\33b\33uKeyID";
  133.         array[2] = "\33b\33uCreated";
  134.         array[3] = "\33b\33uUserID";
  135.     }
  136. }
  137.  
  138. /* A Hook structure used by the "listview" gadget. */
  139. static const struct Hook DisplayLI_TextHook =
  140. {
  141.     { NULL,NULL },
  142.     (VOID *)DisplayLI_TextFunc,
  143.     NULL,NULL
  144. };
  145.  
  146. /*
  147. ** Generate the MUI GUI for the user.
  148. */
  149. static struct ObjApp_SelKey *
  150. CreateSelectPGPKApp(char *wintitle, ULONG multiselect)
  151. {
  152.     struct ObjApp_SelKey *ObjectApp;
  153.  
  154.     APTR GROUP_KeyLister, GR_BT_SEL_OKCAN;
  155.  
  156.     if (!(ObjectApp = AllocVec(sizeof(struct ObjApp_SelKey),MEMF_PUBLIC|MEMF_CLEAR)))
  157.         return(NULL);
  158.  
  159.     ObjectApp->LV_KEYLISTVIEW = ListObject,
  160.         MUIA_Frame, MUIV_Frame_InputList,
  161.         MUIA_List_DisplayHook, &DisplayLI_TextHook,
  162.          MUIA_List_Format, "P=\33l BAR,P=\33l BAR,P=\33l BAR,P=\33l",
  163.         MUIA_List_SourceArray, keystable,
  164.         MUIA_List_Title, TRUE,
  165.     End;
  166.  
  167.     ObjectApp->LV_KEYLISTVIEW = ListviewObject,
  168.         MUIA_Listview_MultiSelect, multiselect,
  169.         MUIA_Listview_List, ObjectApp->LV_KEYLISTVIEW,
  170.     End;
  171.  
  172.     ObjectApp->BT_SEL_OK = SimpleButton("Ok");
  173.  
  174.     ObjectApp->BT_SEL_CANCEL = SimpleButton("Cancel");
  175.  
  176.     GR_BT_SEL_OKCAN = GroupObject,
  177.         MUIA_Group_Horiz, TRUE,
  178.         Child, ObjectApp->BT_SEL_OK,
  179.         Child, ObjectApp->BT_SEL_CANCEL,
  180.     End;
  181.  
  182.     GROUP_KeyLister = GroupObject,
  183.         MUIA_Frame, MUIV_Frame_Group,
  184.         MUIA_FrameTitle, "Choose a key",
  185.         Child, ObjectApp->LV_KEYLISTVIEW,
  186.         Child, GR_BT_SEL_OKCAN,
  187.     End;
  188.  
  189.     ObjectApp->WI_SelectPGP5Key = WindowObject,
  190.         MUIA_Window_Title, wintitle,
  191.         MUIA_Window_CloseGadget, FALSE,
  192.         MUIA_Window_ID, MAKE_ID('W', 'I', 'N', '2'),
  193.         MUIA_Window_NoMenus, TRUE,
  194.         WindowContents, GROUP_KeyLister,
  195.     End;
  196.  
  197.     ObjectApp->App = ApplicationObject,
  198.         MUIA_Application_Base, "PGP5GUI-KEYLIST",
  199.         MUIA_Application_Title, "PGP5GUI-KEYLIST",
  200.         SubWindow, ObjectApp->WI_SelectPGP5Key,
  201.     End;
  202.  
  203.  
  204.     if (!ObjectApp->App)
  205.     {
  206.         FreeVec(ObjectApp);
  207.         return(NULL);
  208.     }
  209.  
  210.     DoMethod(ObjectApp->BT_SEL_OK,
  211.         MUIM_Notify, MUIA_Pressed, FALSE,
  212.         ObjectApp->App,
  213.         2,
  214.         MUIM_Application_ReturnID, SEL_OK
  215.         );
  216.  
  217.     DoMethod(ObjectApp->BT_SEL_CANCEL,
  218.         MUIM_Notify, MUIA_Pressed, FALSE,
  219.         ObjectApp->App,
  220.         2,
  221.         MUIM_Application_ReturnID, SEL_CANCEL
  222.         );
  223.  
  224.     DoMethod(ObjectApp->WI_SelectPGP5Key,
  225.         MUIM_Window_SetCycleChain, ObjectApp->LV_KEYLISTVIEW,
  226.         ObjectApp->BT_SEL_OK,
  227.         ObjectApp->BT_SEL_CANCEL,
  228.         0
  229.         );
  230.  
  231.     set(ObjectApp->WI_SelectPGP5Key, MUIA_Window_Open, TRUE);
  232.  
  233.     return(ObjectApp);
  234. }
  235.  
  236. int
  237. SelectPGPKey(struct ObjApp *prevApp, char *selected[], char *winname, ULONG multiselect)
  238. {
  239.     struct ObjApp_SelKey *App;    /* Object */
  240.     BOOL running = TRUE;
  241.     ULONG signal;
  242.  
  243.     char *fib = 0;
  244.     char keyID[16],    keyinfo[BUFFER_SIZE];
  245.  
  246.     int retval = 0, i;
  247.  
  248.     LONG id = MUIV_List_NextSelected_Start;
  249.  
  250.     set(prevApp->TX_Status, MUIA_Text_Contents, "\33cWaiting for key choice");
  251.  
  252.     /* Make the parent window go to sleep */
  253.     set(prevApp->WI_Main, MUIA_Window_Sleep, TRUE);
  254.  
  255.     /* Create Object */
  256.     if (!(App = CreateSelectPGPKApp(winname, multiselect)))
  257.     {
  258.         PrintFault(IoErr(), "Couldn't create SelectPGPK MUI");
  259.         return(20);
  260.     }
  261.  
  262.     while (running)
  263.     {
  264.         switch (DoMethod(App->App,MUIM_Application_Input,&signal))
  265.         {
  266.             case SEL_CANCEL:
  267.                 /*
  268.                 ** User pressed 'Cancel' button!
  269.                 ** Signal to break out of this loop.
  270.                 */
  271.                 running = FALSE;
  272.                 retval = -1;
  273.                 break;
  274.  
  275.             case SEL_OK:
  276.                 /*
  277.                 ** User pressed 'OK'.
  278.                 ** Return selected entry (if any) in 'selected' array.
  279.                 */
  280.                 for (i = 0; i < 256; i++)
  281.                 {
  282.                     /* Check for next selected entry */
  283.                     DoMethod(App->LV_KEYLISTVIEW, MUIM_List_NextSelected, &id);
  284.  
  285.                     /*
  286.                     ** No more entries selected (end of list)???
  287.                     ** Break out of loop.
  288.                     */
  289.                     if (id == MUIV_List_NextSelected_End)
  290.                         break;
  291.  
  292.                     /* Get entry that is selected */
  293.                     DoMethod(App->LV_KEYLISTVIEW, MUIM_List_GetEntry, id, &fib);
  294.  
  295.                     /* Get a copy of the line into 'keyinfo' */
  296.                     strcpy(keyinfo, fib);
  297.  
  298.                     /* Terminate the end of the keyID info */
  299.                        *(keyinfo + ((KEYID_SIZE + ALGO_SIZE) - 1)) = NULL;
  300.  
  301.                     /* Get the keyID info into 'keyID' */
  302.                     strcpy(keyID, keyinfo + ALGO_SIZE);
  303.  
  304.                     /* If line is not empty, copy line to allocated memory */
  305.                     if (strlen(keyID))
  306.                         strcpy(selected[i], keyID);
  307.                     else
  308.                         break;    /* Protect against any weird future bugs ;-) */
  309.                 }
  310.  
  311.                 *selected[i] = '\0';
  312.  
  313.                 /* Signal to break out of this loop. */
  314.                 running = FALSE;
  315.                 break;
  316.  
  317.             default:
  318.                 break;
  319.         }
  320.  
  321.         if (running && signal)
  322.             Wait(signal);
  323.     }
  324.  
  325.     /* De-allocate and free up the resources used by the MUI GUI */
  326.     MUI_DisposeObject(App->App);
  327.     FreeVec(App);
  328.  
  329.     /* Make the parent window wake up */
  330.     set(prevApp->WI_Main, MUIA_Window_Sleep, FALSE);
  331.  
  332.     /* Wake up the parent window */
  333.     set(prevApp->WI_Main, MUIA_Window_Activate, TRUE);
  334.  
  335.     set(prevApp->TX_Status, MUIA_Text_Contents, ReadyMSG);
  336.  
  337.     /*
  338.     ** Return flag to caller:
  339.     ** 0 = OK, -1 = Cancel, 20 = Failed MUI App
  340.     */
  341.     return(retval);
  342. }
  343.