home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Utilities / Remotes / Source / Remotes.m < prev    next >
Encoding:
Text File  |  1992-09-23  |  13.6 KB  |  445 lines

  1. /*---------------------------------------------------------------------------
  2. Remotes.m -- Copyright (c) 1991 Rex Pruess
  3.   
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.   
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.   
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or send
  17.    electronic mail to the the author.
  18.   
  19. This routine contains the methods for the main menu and for the hosts window.
  20. Seldom used objects are only created if necessary.  This reduces the
  21. application's initial load time.
  22.   
  23. Rex Pruess <rpruess@umaxc.weeg.uiowa.edu>
  24.   
  25. $Header: /rpruess/apps/Remotes3.0/RCS/Remotes.m,v 3.0 92/09/23 22:15:47 rpruess Exp $
  26. -----------------------------------------------------------------------------
  27. $Log:    Remotes.m,v $
  28. Revision 3.0  92/09/23  22:15:47  rpruess
  29. Checked in to RCS to get the revision number updated to 3.0.
  30.  
  31. Revision 2.1  92/09/23  21:29:55  rpruess
  32. Updated code for NeXT System Release 3.0.
  33.  
  34. Revision 2.0  91/01/22  15:33:01  rpruess
  35. Remotes-2.0 was upgraded for NeXT System Release 2.0 (standard or extended).
  36. Remotes-2.0 supports the NeXT supplied Terminal application and the Stuart
  37. shareware product.
  38.  
  39. Revision 1.3  90/05/17  15:02:35  rpruess
  40. Added windowWillResize code to limit minimum/maximum size of the hosts
  41. window.
  42.  
  43. Revision 1.2  90/05/16  09:34:17  rpruess
  44. Added signal handling routine in order to catch the termination of child
  45. processes.  Without this code, a "defunct" process remains on the system
  46. whenever a child process exits.
  47.  
  48. Revision 1.1  90/04/10  14:27:32  rpruess
  49. Initial revision
  50.  
  51. -----------------------------------------------------------------------------*/
  52.  
  53. /* Standard C header files */
  54. #include <libc.h>
  55. #include <signal.h>
  56. #include <stdio.h>
  57. #include <strings.h>
  58.  
  59. /* Remotes class header files */
  60. #import "Remotes.h"
  61. #import "Configure.h"
  62. #import "GNULicense.h"
  63. #import "Help.h"
  64. #import "HostListManager.h"
  65. #import "Info.h"
  66. #import "Prefs.h"
  67.  
  68. /* Appkit header files */
  69. #import <appkit/Application.h>
  70. #import <appkit/Button.h>
  71. #import <appkit/PopUpList.h>
  72.  
  73. #import <defaults/defaults.h>
  74.  
  75. @implementation Remotes
  76.  
  77. /*---------------------------------------------------------------------------
  78. In order to prevent defunct child processes, we must reap them.
  79. -----------------------------------------------------------------------------*/
  80. void reap_child()
  81. {
  82.    int pid;
  83.    union wait status;
  84.    while((pid = wait3(&status, WNOHANG, (struct rusage *) 0)) > 0);
  85. }
  86.  
  87. /*---------------------------------------------------------------------------
  88. Override the new method so the objects are created in the proper order.  The
  89. creation of the HLM object results in the loading of the configuration file by
  90. one of the HLM methods.  The hosts popup list object is created here too.
  91. -----------------------------------------------------------------------------*/
  92. - init
  93. {
  94.    self = [super init];
  95.    
  96.    myConfig = nil;              /* Seldom used objects start as nil. */
  97.    myGNULicense = nil;
  98.    myHelpPanel = nil;
  99.    myInfoPanel = nil;
  100.    myPrefs = nil;
  101.  
  102.    myHLM = [[HostListManager alloc] init];
  103.    hostsPUL = [[PopUpList alloc] init];
  104.  
  105.    [NXApp setDelegate:self];
  106.  
  107.    return self;
  108. }
  109.  
  110. /*---------------------------------------------------------------------------
  111. Override the initialize method so the defaults can be registered.
  112. -----------------------------------------------------------------------------*/
  113. + initialize
  114. {
  115.    char            configFile[MAXCONFIGLEN + 1];
  116.    int             i;
  117.  
  118.    static NXDefaultsVector RemotesDefaults =
  119.    {
  120.       {"ConfigFile", NULL},
  121.       {"Width", NULL},
  122.       {"Height", NULL},
  123.       {"DebugLevel", "off"},
  124.       {"WinLocX", "400.0"},
  125.       {"WinLocY", "700.0"},
  126.       {NULL}
  127.    };
  128.  
  129.    static NXDefaultsVector StuartDefaults =
  130.    {
  131.       {"Columns", "80"},
  132.       {"Lines", "24"},
  133.       {"NXFixedPitchFont", "Ohlfs"},
  134.       {"NXFixedPitchFontSize", "10"},
  135.       {NULL}
  136.    };
  137.  
  138.    static NXDefaultsVector TerminalDefaults =
  139.    {
  140.       {"Columns", "80"},
  141.       {"Rows", "24"},
  142.       {"NXFixedPitchFont", "Ohlfs"},
  143.       {"NXFixedPitchFontSize", "10"},
  144.       {NULL}
  145.    };
  146.  
  147.    /* Register the Stuart/Terminal defaults.  These are easy. */
  148.    NXRegisterDefaults ("Stuart", StuartDefaults);
  149.    NXRegisterDefaults ("Terminal", TerminalDefaults);
  150.  
  151.    /* Set up the config file default pathname (e.g., /userid/.Remotes). */
  152.    strcpy (configFile, NXHomeDirectory ());
  153.    strcat (configFile, "/.Remotes");
  154.  
  155.    /* Assign storage & stuff the config pathname into it. */
  156.    RemotesDefaults[0].value = malloc (strlen (configFile) + 1);
  157.    strcpy (RemotesDefaults[0].value, configFile);
  158.  
  159.    /* Stuff in default hosts window width/height */
  160.    RemotesDefaults[1].value = malloc (16);
  161.    sprintf (RemotesDefaults[1].value, "%.1f", DEFWIDTH);
  162.  
  163.    RemotesDefaults[2].value = malloc (16);
  164.    sprintf (RemotesDefaults[2].value, "%.1f", DEFHEIGHT);
  165.  
  166.    /* Register the Remotes defaults & release the storage. */
  167.    NXRegisterDefaults ([NXApp appName], RemotesDefaults);
  168.  
  169.    for (i = 0; i <= 2; i++)
  170.       free (RemotesDefaults[i].value);
  171.  
  172.    return self;
  173. }
  174.  
  175. /*---------------------------------------------------------------------------
  176. Application did initialize.  Get the previous size & location of the hosts
  177. window and display it the same way.  Fire up any terminal/shells that must be
  178. automatically started.
  179. -----------------------------------------------------------------------------*/
  180. - appDidInit:sender
  181. {
  182.    NXSize          screenSize;  /* Screen width/height */
  183.    const char     *string;      /* Temporary variable */
  184.    int             xMax;        /* Maximum X value allowed */
  185.    int             yMax;        /* Maximum Y value allowed */
  186.    NXRect          windowRect;  /* Coordinates & size of window */
  187.  
  188.    /* Get screen size & assign maximum values allowable. */
  189.    [NXApp getScreenSize:&screenSize];
  190.    xMax = screenSize.width - 5;
  191.    yMax = screenSize.height - 5;
  192.  
  193.    /* Set x, y, width, & height values for the hosts window. */
  194.    string = NXGetDefaultValue ("Remotes", "WinLocX");
  195.    windowRect.origin.x = atof (string);
  196.    if (windowRect.origin.x < 0)
  197.       windowRect.origin.x = 0;
  198.    if (windowRect.origin.x > xMax)
  199.       windowRect.origin.x = xMax;
  200.  
  201.    string = NXGetDefaultValue ("Remotes", "WinLocY");
  202.    windowRect.origin.y = atof (string);
  203.    if (windowRect.origin.y < 0)
  204.       windowRect.origin.y = 0;
  205.    if (windowRect.origin.y > yMax)
  206.       windowRect.origin.y = yMax;
  207.  
  208.    string = NXGetDefaultValue ("Remotes", "Width");
  209.    windowRect.size.width = atof (string);
  210.    if (windowRect.size.width < MINWIDTH ||
  211.       windowRect.size.width > MAXWIDTH)
  212.       windowRect.size.width = DEFWIDTH;
  213.  
  214.    string = NXGetDefaultValue ("Remotes", "Height");
  215.    windowRect.size.height = atof (string);
  216.    if (windowRect.size.height < MINHEIGHT ||
  217.       windowRect.size.height > MAXHEIGHT)
  218.       windowRect.size.height = DEFHEIGHT;
  219.  
  220.    /* Position the hosts window & display it on the screen. */
  221.    [hostsWindow placeWindow:&windowRect];
  222.    [hostsWindow makeKeyAndOrderFront:self];
  223.  
  224.    /* Assign icon for when window is miniturized. */
  225.    [hostsWindow setMiniwindowIcon:"app"];
  226.  
  227.    /* Fire up those autostart guys. */
  228.    [myHLM autoStart:sender];
  229.  
  230.    signal(SIGCHLD, reap_child);
  231.    
  232.    return self;
  233. }
  234.  
  235. /*---------------------------------------------------------------------------
  236. Display the GNU license.
  237. -----------------------------------------------------------------------------*/
  238. - showGNULicense:sender
  239. {
  240.    if (myGNULicense == nil)
  241.       myGNULicense = [[GNULicense alloc] init];
  242.  
  243.    /* Not really necessary, but kinda cutesy. */
  244.    if ([sender isKindOf:[Control class]] == YES) {
  245.       [sender setTarget:myGNULicense];
  246.       [sender setAction:@selector (showLicense:)];
  247.    }
  248.  
  249.    [myGNULicense showLicense:self];
  250.    return self;
  251. }
  252.  
  253. /*---------------------------------------------------------------------------
  254. Display the help panel.
  255. -----------------------------------------------------------------------------*/
  256. - showHelp:sender
  257. {
  258.    if (myHelpPanel == nil)
  259.       myHelpPanel = [[Help alloc] init];
  260.  
  261.    [myHelpPanel showHelpPanel:self];
  262.  
  263.    return self;
  264. }
  265.  
  266. /*---------------------------------------------------------------------------
  267. Display the info panel.
  268. -----------------------------------------------------------------------------*/
  269. - showInfo:sender
  270. {
  271.    if (myInfoPanel == nil)
  272.       myInfoPanel = [[Info alloc] init];
  273.  
  274.    [myInfoPanel showInfoPanel:self];
  275.  
  276.    return self;
  277. }
  278.  
  279. /*---------------------------------------------------------------------------
  280. Fire up the configuration window.
  281. -----------------------------------------------------------------------------*/
  282. - showConfigWindow:sender
  283. {
  284.    if (myConfig == nil) {
  285.       myConfig = [[Configure alloc] init];
  286.       [myConfig setHLM:myHLM];
  287.       [myConfig setHostsButton:hostsButton];
  288.       [myConfig setHostsPUL:hostsPUL];
  289.       [myConfig initConfigWindow:sender];
  290.       [myConfig initConfigButton:sender];
  291.    }
  292.  
  293.    [myConfig showConfigWindow:self];
  294.  
  295.    return self;
  296. }
  297.  
  298. /*---------------------------------------------------------------------------
  299. Fire up the preferences window.
  300. -----------------------------------------------------------------------------*/
  301. - showPrefsWindow:sender
  302. {
  303.    if (myPrefs == nil) {
  304.       myPrefs = [[Prefs alloc] init];
  305.       [myPrefs setHLM:myHLM];
  306.       [myPrefs initPrefs:sender];
  307.    }
  308.  
  309.    [myPrefs showPrefsWindow:self];
  310.  
  311.    return self;
  312. }
  313.  
  314. /*---------------------------------------------------------------------------
  315. Handle login request from the hosts window.
  316. -----------------------------------------------------------------------------*/
  317. - loginNow:sender
  318. {
  319.    [myHLM loginToHost:[[sender selectedCell] title] activate:YES];
  320.    return self;
  321. }
  322.  
  323. /*---------------------------------------------------------------------------
  324. Revert fields in the configuration window or the preferences window.  The
  325. methods in each object will check to see if the window is the key window.
  326. If so, the fields will be reverted.
  327. -----------------------------------------------------------------------------*/
  328. - menuRevert:sender
  329. {
  330.    if (myConfig != nil)
  331.       [myConfig menuRevert:self];
  332.  
  333.    if (myPrefs != nil)
  334.       [myPrefs menuRevert:self];
  335.  
  336.    return self;
  337. }
  338.  
  339. /*---------------------------------------------------------------------------
  340. Hosts window moved so save the new coordinates/size.
  341. -----------------------------------------------------------------------------*/
  342. - windowDidMove:sender
  343. {
  344.    [self windowUpdateDefaults:self];
  345.    return self;
  346. }
  347.  
  348. /*---------------------------------------------------------------------------
  349. Hosts window will resize so validate acceptable values.
  350. -----------------------------------------------------------------------------*/
  351. - windowWillResize:sender toSize:(NXSize *)frameSize
  352. {
  353.     if (frameSize->width < MINWIDTH) frameSize->width = MINWIDTH;
  354.     if (frameSize->width > MAXWIDTH) frameSize->width = MAXWIDTH;
  355.  
  356.     if (frameSize->height < MINHEIGHT) frameSize->height = MINHEIGHT;
  357.     if (frameSize->height > MAXHEIGHT) frameSize->height = MAXHEIGHT;
  358.     
  359.     return self;
  360. }
  361.  
  362. /*---------------------------------------------------------------------------
  363. Hosts window resized so save the new coordinates/size.
  364. -----------------------------------------------------------------------------*/
  365. - windowDidResize:sender
  366. {
  367.    [self windowUpdateDefaults:self];
  368.    return self;
  369. }
  370.  
  371. /*---------------------------------------------------------------------------
  372. Get the new hosts window frame values & save them in the defaults data base.
  373. -----------------------------------------------------------------------------*/
  374. - windowUpdateDefaults:sender
  375. {
  376.    int             i;
  377.    char            string[80];  /* Temporary variable */
  378.    NXRect          windowRect;  /* Coordinates & size of window */
  379.  
  380.    NXDefaultsVector remotesV =
  381.    {
  382.       {"WinLocX", NULL},
  383.       {"WinLocY", NULL},
  384.       {"Width", NULL},
  385.       {"Height", NULL},
  386.       {NULL}
  387.    };
  388.  
  389.    [hostsWindow getFrame:&windowRect];
  390.  
  391.    sprintf (string, "%.1f", windowRect.origin.x);
  392.    remotesV[0].value = malloc (strlen (string) + 1);
  393.    strcpy (remotesV[0].value, string);
  394.  
  395.    sprintf (string, "%.1f", windowRect.origin.y);
  396.    remotesV[1].value = malloc (strlen (string) + 1);
  397.    strcpy (remotesV[1].value, string);
  398.  
  399.    sprintf (string, "%.1f", windowRect.size.width);
  400.    remotesV[2].value = malloc (strlen (string) + 1);
  401.    strcpy (remotesV[2].value, string);
  402.  
  403.    sprintf (string, "%.1f", windowRect.size.height);
  404.    remotesV[3].value = malloc (strlen (string) + 1);
  405.    strcpy (remotesV[3].value, string);
  406.  
  407.    NXWriteDefaults ([NXApp appName], remotesV);
  408.  
  409.    for (i = 0; i <= 3; i++)
  410.       free (remotesV[i].value);
  411.  
  412.    return self;
  413. }
  414.  
  415. /*---------------------------------------------------------------------------
  416. Hosts window.
  417. -----------------------------------------------------------------------------*/
  418. - setHostsWindow:anObject
  419. {
  420.    hostsWindow = anObject;
  421.    return self;
  422. }
  423.  
  424. /*---------------------------------------------------------------------------
  425. Set up the hosts popup button & its associated popup list.
  426. -----------------------------------------------------------------------------*/
  427. - setHostsButton:anObject
  428. {
  429.    hostsButton = anObject;
  430.  
  431.    [myHLM loadPopUpList:hostsPUL];
  432.  
  433.    [hostsButton setTarget:hostsPUL];
  434.    [hostsButton setAction:@selector (popUp:)];
  435.  
  436.    NXAttachPopUpList (hostsButton, hostsPUL);
  437.  
  438.    [hostsPUL setTarget:self];
  439.    [hostsPUL setAction:@selector (loginNow:)];
  440.  
  441.    return self;
  442. }
  443.  
  444. @end
  445.