home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / PrefsLdapProp.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  14.0 KB  |  528 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* 
  19.    PrefsLdapProp.cpp -- The LDAP server properties dialog
  20.    Created: Linda Wei <lwei@netscape.com>, 7-Feb-97.
  21.  */
  22.  
  23.  
  24.  
  25. #include "felocale.h"
  26. #include "structs.h"
  27. #include "fonts.h"
  28. #include "xpassert.h"
  29. #include "xfe.h"
  30. #include "PrefsDialog.h"
  31. #include "PrefsLdapProp.h"
  32.  
  33. #include <Xm/Label.h>
  34. #include <Xm/LabelG.h>
  35. #include <Xm/PushB.h>
  36. #include <Xm/PushBG.h>
  37. #include <Xm/ToggleB.h>
  38. #include <Xm/Form.h>
  39. #include <Xm/Frame.h>
  40. #include <Xm/TextF.h> 
  41. #include <Xm/ToggleBG.h> 
  42. #include <Xfe/Xfe.h>
  43.  
  44. extern "C"
  45. {
  46.     char *XP_GetString(int i);
  47. }
  48.  
  49. // ==================== Public Member Functions ====================
  50.  
  51. // Member:       XFE_PrefsLdapPropDialog
  52. // Description:  Constructor
  53. // Inputs:
  54. // Side effects: Creates the LDAP Server Properties dialog
  55.  
  56. XFE_PrefsLdapPropDialog::XFE_PrefsLdapPropDialog(XFE_PrefsDialog *prefsDialog,
  57.                                                  XFE_PrefsPageMailNewsAddrBook *addrBookPage,
  58.                                                  Widget     parent,      // dialog parent
  59.                                                  char      *name,        // dialog name
  60.                                                  Boolean    modal)       // modal dialog?
  61.     : XFE_Dialog(parent, 
  62.                  name,
  63.                  TRUE,     // ok
  64.                  TRUE,     // cancel
  65.                  FALSE,    // help
  66.                  FALSE,    // apply
  67.                  FALSE,    // separator
  68.                  modal     // modal
  69.                  ),
  70.       m_prefsDialog(prefsDialog),
  71.       m_addrBookPage(addrBookPage),
  72.       m_prefsDataLdapProp(0)
  73. {
  74.     PrefsDataLdapProp *fep = NULL;
  75.  
  76.     fep = new PrefsDataLdapProp;
  77.     memset(fep, 0, sizeof(PrefsDataLdapProp));
  78.     m_prefsDataLdapProp = fep;
  79.  
  80.     Widget     kids[100];
  81.     Arg        av[50];
  82.     int        ac;
  83.     int        i;
  84.  
  85.     Widget     form;
  86.     form = XtVaCreateWidget("form", xmFormWidgetClass, m_chrome,
  87.                             XmNmarginWidth, 8,
  88.                             XmNtopAttachment, XmATTACH_FORM,
  89.                             XmNleftAttachment, XmATTACH_FORM,
  90.                             XmNrightAttachment, XmATTACH_FORM,
  91.                             XmNbottomAttachment, XmATTACH_FORM,
  92.                             NULL);
  93.     XtManageChild (form);
  94.  
  95.     Widget   desc_label;
  96.     Widget   server_label;
  97.     Widget   root_label;
  98.     Widget   port_number_label;
  99.     Widget   number_of_hit_label;
  100.     Widget   desc_text;
  101.     Widget   server_text;
  102.     Widget   root_text;
  103.     Widget   port_number_text;
  104.     Widget   number_of_hit_text;
  105.     Widget   secure_toggle;
  106. #if 0
  107.     Widget   save_passwd_toggle;
  108. #endif
  109.  
  110.     ac = 0;
  111.     i = 0;
  112.  
  113.     kids[i++] = desc_label = 
  114.         XmCreateLabelGadget(form, "descLabel", av, ac);
  115.  
  116.     kids[i++] = server_label = 
  117.         XmCreateLabelGadget(form, "serverLabel", av, ac);
  118.  
  119.     kids[i++] = root_label = 
  120.         XmCreateLabelGadget(form, "rootLabel", av, ac);
  121.  
  122.     kids[i++] = port_number_label = 
  123.         XmCreateLabelGadget(form, "portNumberLabel", av, ac);
  124.  
  125.     kids[i++] = number_of_hit_label = 
  126.         XmCreateLabelGadget(form, "numHitLabel", av, ac);
  127.  
  128.     kids[i++] = desc_text = 
  129.         fe_CreateTextField(form, "descText", av, ac);
  130.  
  131.     kids[i++] = server_text = 
  132.         fe_CreateTextField(form, "serverText", av, ac);
  133.  
  134.     kids[i++] = root_text = 
  135.         fe_CreateTextField(form, "rootText", av, ac);
  136.  
  137.     kids[i++] = port_number_text = 
  138.         fe_CreateTextField(form, "portNumberText", av, ac);
  139.  
  140.     kids[i++] = number_of_hit_text = 
  141.         fe_CreateTextField(form, "numberOfHitText", av, ac);
  142.  
  143.     kids[i++] = secure_toggle =
  144.         XmCreateToggleButtonGadget(form, "secure", av, ac);
  145.  
  146. #if 0
  147.     kids[i++] = save_passwd_toggle =
  148.         XmCreateToggleButtonGadget(form, "savePasswd", av, ac);
  149. #endif
  150.  
  151.     fep->desc_text = desc_text;
  152.     fep->server_text = server_text;
  153.     fep->root_text = root_text;
  154.     fep->port_number_text = port_number_text;
  155.     fep->number_of_hit_text = number_of_hit_text;
  156.     fep->secure_toggle = secure_toggle;
  157. #if 0
  158.     fep->save_passwd_toggle = save_passwd_toggle;
  159. #endif
  160.  
  161.     int labels_width;
  162.     labels_width = XfeVaGetWidestWidget(desc_label,
  163.                                         server_label,
  164.                                         root_label,
  165.                                         port_number_label,
  166.                                         number_of_hit_label,
  167.                                         NULL);
  168.  
  169.     int labels_height;
  170.     labels_height = XfeVaGetTallestWidget(desc_label,
  171.                                           desc_text,
  172.                                           secure_toggle,
  173.                                           NULL);
  174.  
  175.     XtVaSetValues(desc_label,
  176.                   XmNheight, labels_height,
  177.                   RIGHT_JUSTIFY_VA_ARGS(desc_label,labels_width),
  178.                   XmNtopAttachment, XmATTACH_FORM,
  179.                   XmNbottomAttachment, XmATTACH_NONE,
  180.                   NULL);
  181.     
  182.     XtVaSetValues(desc_text,
  183.                   XmNcolumns, 35,
  184.                   XmNheight, labels_height,
  185.                   XmNtopAttachment, XmATTACH_FORM,
  186.                   XmNleftAttachment, XmATTACH_FORM,
  187.                   XmNleftOffset, labels_width,
  188.                   XmNrightAttachment, XmATTACH_NONE,
  189.                   XmNbottomAttachment, XmATTACH_NONE,
  190.                   NULL);
  191.     
  192.     XtVaSetValues(server_label,
  193.                   XmNheight, labels_height,
  194.                   RIGHT_JUSTIFY_VA_ARGS(server_label,labels_width),
  195.                   XmNtopAttachment, XmATTACH_WIDGET,
  196.                   XmNtopWidget, desc_label,
  197.                   XmNbottomAttachment, XmATTACH_NONE,
  198.                   NULL);
  199.     
  200.     XtVaSetValues(server_text,
  201.                   XmNcolumns, 35,
  202.                   XmNheight, labels_height,
  203.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  204.                   XmNtopWidget, server_label,
  205.                   XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  206.                   XmNleftWidget, desc_text,
  207.                   XmNrightAttachment, XmATTACH_NONE,
  208.                   XmNbottomAttachment, XmATTACH_NONE,
  209.                   NULL);
  210.     
  211.     XtVaSetValues(root_label,
  212.                   XmNheight, labels_height,
  213.                   RIGHT_JUSTIFY_VA_ARGS(root_label,labels_width),
  214.                   XmNtopAttachment, XmATTACH_WIDGET,
  215.                   XmNtopWidget, server_label,
  216.                   XmNbottomAttachment, XmATTACH_NONE,
  217.                   NULL);
  218.     
  219.     XtVaSetValues(root_text,
  220.                   XmNcolumns, 35,
  221.                   XmNheight, labels_height,
  222.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  223.                   XmNtopWidget, root_label,
  224.                   XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  225.                   XmNleftWidget, desc_text,
  226.                   XmNrightAttachment, XmATTACH_NONE,
  227.                   XmNbottomAttachment, XmATTACH_NONE,
  228.                   NULL);
  229.     
  230.     XtVaSetValues(port_number_label,
  231.                   XmNheight, labels_height,
  232.                   RIGHT_JUSTIFY_VA_ARGS(port_number_label,labels_width),
  233.                   XmNtopAttachment, XmATTACH_WIDGET,
  234.                   XmNtopWidget, root_label,
  235.                   XmNbottomAttachment, XmATTACH_NONE,
  236.                   NULL);
  237.     
  238.     XtVaSetValues(port_number_text,
  239.                   XmNcolumns, 6,
  240.                   XmNheight, labels_height,
  241.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  242.                   XmNtopWidget, port_number_label,
  243.                   XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  244.                   XmNleftWidget, desc_text,
  245.                   XmNrightAttachment, XmATTACH_NONE,
  246.                   XmNbottomAttachment, XmATTACH_NONE,
  247.                   NULL);
  248.     
  249.     XtVaSetValues(number_of_hit_label,
  250.                   XmNheight, labels_height,
  251.                   RIGHT_JUSTIFY_VA_ARGS(number_of_hit_label,labels_width),
  252.                   XmNtopAttachment, XmATTACH_WIDGET,
  253.                   XmNtopWidget, port_number_label,
  254.                   XmNbottomAttachment, XmATTACH_NONE,
  255.                   NULL);
  256.     
  257.     XtVaSetValues(number_of_hit_text,
  258.                   XmNcolumns, 6,
  259.                   XmNheight, labels_height,
  260.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  261.                   XmNtopWidget, number_of_hit_label,
  262.                   XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  263.                   XmNleftWidget, desc_text,
  264.                   XmNrightAttachment, XmATTACH_NONE,
  265.                   XmNbottomAttachment, XmATTACH_NONE,
  266.                   NULL);
  267.     
  268.     XtVaSetValues(secure_toggle,
  269.                   XmNindicatorType, XmONE_OF_MANY,
  270.                   XmNheight, labels_height,
  271.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  272.                   XmNtopWidget, port_number_text,
  273.                   XmNleftAttachment, XmATTACH_WIDGET,
  274.                   XmNleftWidget, port_number_text,
  275.                   XmNrightAttachment, XmATTACH_NONE,
  276.                   XmNbottomAttachment, XmATTACH_NONE,
  277.                   NULL);
  278.     
  279. #if 0
  280.     XtVaSetValues(save_passwd_toggle,
  281.                   XmNindicatorType, XmONE_OF_MANY,
  282.                   XmNheight, labels_height,
  283.                   XmNtopAttachment, XmATTACH_OPPOSITE_WIDGET,
  284.                   XmNtopWidget, number_of_hit_text,
  285.                   XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
  286.                   XmNleftWidget, secure_toggle,
  287.                   XmNrightAttachment, XmATTACH_NONE,
  288.                   XmNbottomAttachment, XmATTACH_NONE,
  289.                   NULL);
  290. #endif
  291.     
  292.     // Add callbacks
  293.  
  294.     XtAddCallback(m_chrome, XmNokCallback, cb_ok, this);
  295.     XtAddCallback(m_chrome, XmNcancelCallback, cb_cancel, this);
  296.  
  297.     XtManageChildren(kids, i);
  298.     XtManageChild(form);
  299. }
  300.  
  301. // Member:       ~XFE_PrefsLdapPropDialog
  302. // Description:  Destructor
  303. // Inputs:
  304. // Side effects: 
  305.  
  306. XFE_PrefsLdapPropDialog::~XFE_PrefsLdapPropDialog()
  307. {
  308.     delete m_prefsDataLdapProp;
  309. }
  310.  
  311. // Member:       show
  312. // Description:  Pop up dialog
  313. // Inputs:
  314. // Side effects: 
  315.  
  316. void XFE_PrefsLdapPropDialog::show()
  317. {
  318.     // Manage the top level
  319.  
  320.     XFE_Dialog::show();
  321.  
  322.     // Set focus to the OK button
  323.  
  324.     XmProcessTraversal(m_okButton, XmTRAVERSE_CURRENT);
  325. }
  326.  
  327. // Member:       initPage
  328. // Description:  Initializes page for MailNewsLang
  329. // Inputs:
  330. // Side effects: 
  331.  
  332. void XFE_PrefsLdapPropDialog::initPage(DIR_Server *server)
  333. {
  334.     XP_ASSERT(m_prefsDataLdapProp);
  335.     PrefsDataLdapProp    *fep = m_prefsDataLdapProp;
  336.     char                  buf[256];
  337.  
  338.     m_server = server;
  339.     if (server) {
  340.         fe_SetTextField(fep->desc_text, server->description);
  341.         fe_SetTextField(fep->server_text, server->serverName);
  342.         fe_SetTextField(fep->root_text, server->searchBase);
  343.         PR_snprintf(buf, sizeof(buf), "%d", server->port);
  344.         XtVaSetValues(fep->port_number_text, XmNvalue, buf, 0);
  345.         PR_snprintf(buf, sizeof(buf), "%d", server->maxHits);
  346.         XtVaSetValues(fep->number_of_hit_text, XmNvalue, buf, 0);
  347.         XtVaSetValues(fep->secure_toggle, XmNset, server->isSecure, 0);
  348. #if 0
  349.         XtVaSetValues(fep->save_passwd_toggle, XmNset, server->savePassword, 0);
  350. #endif
  351.     }
  352. }
  353.  
  354. // Member:       verifyPage
  355. // Description:  verify page for MailNewsLang
  356. // Inputs:
  357. // Side effects: 
  358.  
  359. Boolean XFE_PrefsLdapPropDialog::verifyPage()
  360. {
  361.     return TRUE;
  362. }
  363.  
  364. // Member:       getContext
  365. // Description:  returns context
  366. // Inputs:
  367. // Side effects: 
  368.  
  369. MWContext *XFE_PrefsLdapPropDialog::getContext()
  370. {
  371.     return (m_prefsDialog->getContext());
  372. }
  373.  
  374. // Member:       getData
  375. // Description:  returns data
  376. // Inputs:
  377. // Side effects: 
  378.  
  379. PrefsDataLdapProp *XFE_PrefsLdapPropDialog::getData()
  380. {
  381.     return (m_prefsDataLdapProp);
  382. }
  383.  
  384. // Member:       getEditDir
  385. // Description:  returns the directory being edited
  386. // Inputs:
  387. // Side effects: 
  388.  
  389. DIR_Server *XFE_PrefsLdapPropDialog::getEditDir()
  390. {
  391.     return m_server;
  392. }
  393.  
  394. // Member:       getAddrBookPage
  395. // Description:  returns the Address Book prefs page
  396. // Inputs:
  397. // Side effects: 
  398.  
  399. XFE_PrefsPageMailNewsAddrBook *XFE_PrefsLdapPropDialog::getAddrBookPage()
  400. {
  401.     return m_addrBookPage;
  402. }
  403.  
  404. // Member:       getPrefsDialog
  405. // Description:  returns preferences dialog
  406. // Inputs:
  407. // Side effects: 
  408.  
  409. XFE_PrefsDialog *XFE_PrefsLdapPropDialog::getPrefsDialog()
  410. {
  411.     return (m_prefsDialog);
  412. }
  413.  
  414. // Friend:       prefsLangCb_ok
  415. // Description:  
  416. // Inputs:
  417. // Side effects: 
  418.  
  419. void XFE_PrefsLdapPropDialog::cb_ok(Widget    w,
  420.                                     XtPointer closure,
  421.                                     XtPointer callData)
  422. {
  423.     XFE_PrefsLdapPropDialog *theDialog = (XFE_PrefsLdapPropDialog *)closure;
  424.     PrefsDataLdapProp       *fep = theDialog->getData();
  425.     Bool                     create = theDialog->getEditDir() ? False : True;
  426.     DIR_Server              *dir = 0;
  427.     
  428.     if (create) {
  429.         dir = (DIR_Server *)XP_ALLOC(sizeof(DIR_Server));
  430.         DIR_InitServer(dir);
  431.     }
  432.     else {
  433.         dir = theDialog->getEditDir();
  434.         XP_FREEIF(dir->description);
  435.         XP_FREEIF(dir->serverName);
  436.         XP_FREEIF(dir->searchBase);
  437.         DIR_InitServer(dir);
  438.     }
  439.  
  440.     char   *desc = 0;
  441.     char   *server = 0;
  442.     char   *root = 0;
  443.     char   *port_num_text = 0;
  444.     char   *num_hits_text = 0;
  445.     char    dummy;
  446.     int     port_num = 0;
  447.     int     num_hits = 0;
  448.     Boolean b;
  449.     char    temp[1024];
  450.     char   *ptr;
  451.  
  452.     // TODO: error checking
  453.  
  454.     desc = fe_GetTextField(fep->desc_text);
  455.     server = fe_GetTextField(fep->server_text);
  456.     root = fe_GetTextField(fep->root_text);
  457.     XtVaGetValues(fep->port_number_text, XmNvalue, &port_num_text, 0);
  458.     XtVaGetValues(fep->number_of_hit_text, XmNvalue, &num_hits_text, 0);
  459.  
  460.     if (1 != sscanf(port_num_text, " %d %c", &port_num, &dummy) ||
  461.         port_num < 0) {
  462.         // TODO: error
  463.     }
  464.     if (port_num_text) XtFree(port_num_text);
  465.  
  466.     if (1 == sscanf(num_hits_text, " %d %c", &num_hits, &dummy) &&
  467.         num_hits < 0) {
  468.         // TODO: error
  469.     }
  470.     if (num_hits_text) XtFree(num_hits_text);
  471.  
  472.     if (ptr = XP_STRCHR(server, '.')) {
  473.         XP_STRCPY(temp, ptr+1);
  474.         if (ptr = XP_STRCHR(temp, '.')) {
  475.             *ptr = '\0';
  476.         }
  477.     }
  478.     else {
  479.         XP_STRCPY(temp, server);
  480.     }
  481.  
  482.     dir->description = desc ? desc : XP_STRDUP("");
  483.     dir->serverName =  server ? server : XP_STRDUP("");
  484.     dir->searchBase =  root ? root : XP_STRDUP("");
  485.     // dir->htmlGateway =  NULL; // no loner use
  486.     dir->fileName = WH_FileName(WH_TempName(xpAddrBook, temp), xpAddrBook);
  487.     dir->port = port_num;
  488.     dir->maxHits = num_hits;
  489.     XtVaGetValues(fep->secure_toggle, XmNset, &b, 0);
  490.     dir->isSecure = b;
  491. #if 0
  492.     XtVaGetValues(fep->save_passwd_toggle, XmNset, &b, 0);
  493.     dir->savePassword = b;
  494. #endif
  495.     dir->dirType = LDAPDirectory; 
  496.  
  497.     // Insert into list if this is create
  498.  
  499.     if (create) {
  500.         XFE_PrefsPageMailNewsAddrBook *dir_page = theDialog->getAddrBookPage();
  501.         dir_page->insertDir(dir);
  502.     }
  503.  
  504.     // Simulate a cancel
  505.  
  506.     theDialog->cb_cancel(w, closure, callData);
  507. }
  508.  
  509. // Member:       prefsLangCb_cancel
  510. // Description:  
  511. // Inputs:
  512. // Side effects: 
  513.  
  514. void XFE_PrefsLdapPropDialog::cb_cancel(Widget    /* w */,
  515.                                         XtPointer closure,
  516.                                         XtPointer /* callData */)
  517. {
  518.     XFE_PrefsLdapPropDialog *theDialog = (XFE_PrefsLdapPropDialog *)closure;
  519.  
  520.     XtRemoveCallback(theDialog->m_chrome, XmNokCallback, cb_ok, theDialog);
  521.     XtRemoveCallback(theDialog->m_chrome, XmNcancelCallback, cb_cancel, theDialog);
  522.  
  523.     // Delete the dialog
  524.  
  525.     delete theDialog;
  526. }
  527.  
  528.