home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / NewsServerDialog.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.9 KB  |  336 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.    NewsServerDialog.cpp -- dialog for letting the user add a new news server.
  20.    Created: Chris Toshok <toshok@netscape.com>, 24-Oct-96.
  21.  */
  22.  
  23.  
  24.  
  25. #include "NewsServerDialog.h"
  26.  
  27. #include <Xm/Form.h>
  28. #include <Xm/LabelG.h>
  29. #include <Xm/TextF.h>
  30. #include <Xm/ToggleBG.h>
  31.  
  32. #include "felocale.h"
  33. #include "xpgetstr.h"
  34. extern int XFE_MAKE_SURE_SERVER_AND_PORT_ARE_VALID;
  35.  
  36. #define SECURE_PORT_STRING "563"
  37. #define INSECURE_PORT_STRING "119"
  38.  
  39. // we need this in the post() method.
  40. extern "C" void fe_EventLoop();
  41.  
  42. XFE_NewsServerDialog::XFE_NewsServerDialog(Widget parent, char *name,
  43.                                            XFE_Frame *frame)
  44.     : XFE_Dialog(parent, 
  45.                  name, 
  46.                  TRUE, // ok
  47.                  TRUE, // cancel
  48.                  FALSE, // help
  49.                  FALSE, // apply
  50.                  TRUE, // separator
  51.                  TRUE // modal
  52.                  )
  53. {
  54.     Widget server_label, port_label, secure_label;
  55.     Widget form, serverForm, portForm, secureForm;
  56.  
  57.     m_frame = frame;
  58.  
  59.     form = XtCreateManagedWidget("serverDialogForm",
  60.                                  xmFormWidgetClass,
  61.                                  m_chrome,
  62.                                  NULL, 0);
  63.  
  64.     serverForm = XtCreateManagedWidget("serverForm",
  65.                                        xmFormWidgetClass,
  66.                                        form,
  67.                                        NULL, 0);
  68.  
  69.     portForm = XtCreateManagedWidget("portForm",
  70.                                      xmFormWidgetClass,
  71.                                      form,
  72.                                      NULL, 0);
  73.  
  74.     secureForm = XtCreateManagedWidget("secureForm",
  75.                                        xmFormWidgetClass,
  76.                                        form,
  77.                                        NULL, 0);
  78.  
  79.     server_label = XtCreateManagedWidget("serverLabel",
  80.                                          xmLabelGadgetClass,
  81.                                          serverForm,
  82.                                          NULL, 0);
  83.  
  84.     m_serverText = fe_CreateTextField(serverForm,
  85.                                       "serverText",
  86.                                       NULL, 0);
  87.     XtManageChild(m_serverText);
  88.  
  89.     port_label = XtCreateManagedWidget("portLabel",
  90.                                        xmLabelGadgetClass,
  91.                                        portForm,
  92.                                        NULL, 0);
  93.  
  94.     m_portText = fe_CreateTextField(portForm,
  95.                                     "portText",
  96.                                     NULL, 0);
  97.     XtManageChild(m_portText);
  98.  
  99.     secure_label = XtCreateManagedWidget("secureLabel",
  100.                                          xmLabelGadgetClass,
  101.                                          secureForm,
  102.                                          NULL, 0);
  103.  
  104.     m_secureToggle = XtCreateManagedWidget("secureToggle",
  105.                                            xmToggleButtonGadgetClass,
  106.                                            secureForm,
  107.                                            NULL, 0);
  108.  
  109.     // now we dance the body attachment
  110.     XtVaSetValues(secure_label,
  111.                   XmNleftAttachment, XmATTACH_FORM,
  112.                   XmNrightAttachment, XmATTACH_NONE,
  113.                   XmNtopAttachment, XmATTACH_FORM,
  114.                   XmNbottomAttachment, XmATTACH_FORM,
  115.                   NULL);
  116.  
  117.     XtVaSetValues(m_secureToggle,
  118.                   XmNleftAttachment, XmATTACH_WIDGET,
  119.                   XmNleftWidget, secure_label,
  120.                   XmNrightAttachment, XmATTACH_FORM,
  121.                   XmNtopAttachment, XmATTACH_FORM,
  122.                   XmNbottomAttachment, XmATTACH_FORM,
  123.                   NULL);
  124.  
  125.     XtVaSetValues(port_label,
  126.                   XmNleftAttachment, XmATTACH_FORM,
  127.                   XmNrightAttachment, XmATTACH_NONE,
  128.                   XmNtopAttachment, XmATTACH_FORM,
  129.                   XmNbottomAttachment, XmATTACH_FORM,
  130.                   NULL);
  131.  
  132.     XtVaSetValues(m_portText,
  133.                   XmNleftAttachment, XmATTACH_WIDGET,
  134.                   XmNleftWidget, port_label,
  135.                   XmNrightAttachment, XmATTACH_FORM,
  136.                   XmNtopAttachment, XmATTACH_FORM,
  137.                   XmNbottomAttachment, XmATTACH_FORM,
  138.                   NULL);
  139.  
  140.     XtVaSetValues(server_label,
  141.                   XmNleftAttachment, XmATTACH_FORM,
  142.                   XmNrightAttachment, XmATTACH_NONE,
  143.                   XmNtopAttachment, XmATTACH_FORM,
  144.                   XmNbottomAttachment, XmATTACH_FORM,
  145.                   NULL);
  146.  
  147.     XtVaSetValues(m_serverText,
  148.                   XmNleftAttachment, XmATTACH_WIDGET,
  149.                   XmNleftWidget, server_label,
  150.                   XmNrightAttachment, XmATTACH_FORM,
  151.                   XmNtopAttachment, XmATTACH_FORM,
  152.                   XmNbottomAttachment, XmATTACH_FORM,
  153.                   NULL);
  154.  
  155.     XtVaSetValues(serverForm,
  156.                   XmNleftAttachment, XmATTACH_FORM,
  157.                   XmNrightAttachment, XmATTACH_FORM,
  158.                   XmNtopAttachment, XmATTACH_FORM,
  159.                   XmNbottomAttachment, XmATTACH_NONE,
  160.                   NULL);
  161.  
  162.     XtVaSetValues(portForm,
  163.                   XmNleftAttachment, XmATTACH_FORM,
  164.                   XmNrightAttachment, XmATTACH_FORM,
  165.                   XmNtopAttachment, XmATTACH_WIDGET,
  166.                   XmNtopWidget, serverForm,
  167.                   XmNbottomAttachment, XmATTACH_NONE,
  168.                   NULL);
  169.  
  170.     XtVaSetValues(secureForm,
  171.                   XmNleftAttachment, XmATTACH_FORM,
  172.                   XmNrightAttachment, XmATTACH_FORM,
  173.                   XmNtopAttachment, XmATTACH_WIDGET,
  174.                   XmNtopWidget, portForm,
  175.                   XmNbottomAttachment, XmATTACH_NONE,
  176.                   NULL);
  177.  
  178.     XtAddCallback(m_chrome, XmNokCallback, ok_cb, this);
  179.     XtAddCallback(m_chrome, XmNcancelCallback, cancel_cb, this);
  180.  
  181.     fe_SetTextFieldAndCallBack(m_portText, INSECURE_PORT_STRING);
  182.     m_portTextEdited = False;
  183.  
  184.     XtAddCallback(m_secureToggle, XmNvalueChangedCallback, toggle_cb, this);
  185.     XtAddCallback(m_portText, XmNvalueChangedCallback, port_edit_cb, this);
  186. }
  187.  
  188. XFE_NewsServerDialog::~XFE_NewsServerDialog()
  189. {
  190.     // nothing needed (that I'm aware of) yet.
  191. }
  192.  
  193. XP_Bool
  194. XFE_NewsServerDialog::post()
  195. {
  196.     m_doneWithLoop = False;
  197.   
  198.     XtVaSetValues(m_chrome,
  199.                   XmNdeleteResponse, XmUNMAP,
  200.                   NULL);
  201.  
  202.     show();
  203.  
  204.     while(!m_doneWithLoop)
  205.         {
  206.             fe_EventLoop();
  207.         }
  208.  
  209.     return m_retVal;
  210. }
  211.  
  212. const char *
  213. XFE_NewsServerDialog::getServer()
  214. {
  215.     char *foo = fe_GetTextField(m_serverText);
  216.  
  217.     if (strlen(foo) == 0)
  218.         return NULL;
  219.     else
  220.         return foo;
  221. }
  222.  
  223. int
  224. XFE_NewsServerDialog::getPort()
  225. {
  226.     char *foo = fe_GetTextField(m_portText);
  227.  
  228.     if (strlen(foo) == 0)
  229.         return -1;
  230.     else
  231.         return atoi(foo);
  232. }
  233.  
  234. XP_Bool
  235. XFE_NewsServerDialog::isSecure()
  236. {
  237.     return XmToggleButtonGadgetGetState(m_secureToggle);
  238. }
  239.  
  240. XP_Bool
  241. XFE_NewsServerDialog::verify()
  242. {
  243.     if (getServer() == NULL || getPort() <= 0)
  244.         {
  245.             XFE_Alert(m_frame->getContext(), XP_GetString(XFE_MAKE_SURE_SERVER_AND_PORT_ARE_VALID));
  246.             return False;
  247.         }
  248.     else
  249.         {
  250.             return True;
  251.         }
  252. }
  253.  
  254. void
  255. XFE_NewsServerDialog::ok()
  256. {
  257.     if (verify())
  258.         {
  259.             m_doneWithLoop = True;
  260.             m_retVal = True;
  261.             
  262.             hide();
  263.         }
  264. }
  265.  
  266. void
  267. XFE_NewsServerDialog::cancel()
  268. {
  269.     m_doneWithLoop = True;
  270.     m_retVal = False;
  271.  
  272.     hide();
  273. }
  274.  
  275. void
  276. XFE_NewsServerDialog::toggle()
  277. {
  278.     char *text = fe_GetTextField(m_portText);
  279.  
  280.     if (!m_portTextEdited || !strcmp(text, ""))
  281.         {
  282.             XtRemoveCallback(m_portText, XmNvalueChangedCallback, port_edit_cb, this);
  283.             if (XmToggleButtonGadgetGetState(m_secureToggle))
  284.                 {
  285.                     fe_SetTextFieldAndCallBack(m_portText, SECURE_PORT_STRING);
  286.                 }
  287.             else
  288.                 {
  289.                     fe_SetTextFieldAndCallBack(m_portText, INSECURE_PORT_STRING);
  290.                 }
  291.             XtAddCallback(m_portText, XmNvalueChangedCallback, port_edit_cb, this);
  292.             m_portTextEdited = False;
  293.         }
  294.  
  295.     XtFree(text);
  296. }
  297.  
  298. void
  299. XFE_NewsServerDialog::port_edit()
  300. {
  301.     m_portTextEdited = True;
  302. }
  303.  
  304. void
  305. XFE_NewsServerDialog::port_edit_cb(Widget, XtPointer clientData, XtPointer)
  306. {
  307.     XFE_NewsServerDialog *obj = (XFE_NewsServerDialog*)clientData;
  308.  
  309.     obj->port_edit();
  310. }
  311.  
  312. void
  313. XFE_NewsServerDialog::toggle_cb(Widget, XtPointer clientData, XtPointer)
  314. {
  315.     XFE_NewsServerDialog *obj = (XFE_NewsServerDialog*)clientData;
  316.  
  317.     obj->toggle();
  318. }
  319.  
  320. void 
  321. XFE_NewsServerDialog::ok_cb(Widget, XtPointer clientData, XtPointer)
  322. {
  323.     XFE_NewsServerDialog *obj = (XFE_NewsServerDialog*)clientData;
  324.  
  325.     obj->ok();
  326. }
  327.  
  328. void
  329. XFE_NewsServerDialog::cancel_cb(Widget, XtPointer clientData, XtPointer)
  330. {
  331.     XFE_NewsServerDialog *obj = (XFE_NewsServerDialog*)clientData;
  332.  
  333.     obj->cancel();
  334. }
  335.  
  336.