home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / Programming / Source / Gopher_1.12 / GopherDispatcher.m < prev    next >
Encoding:
Text File  |  1992-03-16  |  5.6 KB  |  243 lines

  1. ////////////////////////////////////////////////////////////////
  2. // This is the object that runs the app.
  3.  
  4. #import "GopherDispatcher.h"
  5. #import <stdio.h>
  6. int shouldUseSound = 0;
  7.  
  8. @implementation GopherDispatcher
  9.  
  10. // These are the global variables used to hold the
  11. // hostname and port of the next Gopher to be opened.
  12. char defaultHost[256];
  13. int defaultPort;
  14. int defaultApp;
  15.  
  16. //////////////////////////////////////////////////////////////
  17. // Create a new Gopher window.
  18.  
  19. - newGopher:sender
  20. {
  21.     id        newWindow;
  22.  
  23.     strncpy(defaultHost, NXGetDefaultValue("Gopher", "GopherHost"), 256);
  24.     [GopherHostTextItem setStringValue:defaultHost];
  25.  
  26.     defaultPort = atoi(NXGetDefaultValue("Gopher", "GopherPort"));
  27.     [GopherPortTextItem setIntValue:defaultPort];
  28.  
  29.     defaultApp = atoi(NXGetDefaultValue("Gopher", "TermApp"));
  30.  
  31.     newWindow = [[GopherClient new] setDispatcher:self];
  32.     [self newFrontWindow:newWindow];
  33.  
  34.     return self;
  35. }
  36.  
  37. //////////////////////////////////////////////////////////////
  38. // Open the Open window
  39.  
  40. - openWindow:sender
  41. {
  42.     [OpenHostTextItem setStringValue:defaultHost];
  43.     [OpenPortTextItem setIntValue:defaultPort];
  44.     [OpenWindow makeKeyAndOrderFront:self];
  45.  
  46.     return self;
  47. }
  48.  
  49. //////////////////////////////////////////////////////////////
  50. // Open a new Gopher.
  51.  
  52. - openGopher:sender
  53. {
  54.     id        newWindow;
  55.  
  56.     [OpenWindow performClose:self];
  57.  
  58.     strncpy(defaultHost, [OpenHostTextItem stringValue], 250);
  59.     defaultPort = [OpenPortTextItem intValue];
  60.  
  61.     newWindow = [[GopherClient new] setDispatcher:self];
  62.     [self newFrontWindow:newWindow];
  63.  
  64.  
  65.     return self;
  66. }
  67.  
  68. //////////////////////////////////////////////////////////////
  69. // Close the current Gopher.
  70.  
  71. - closeGopher:sender
  72. {
  73.     [self oldFrontWindow:currentWindow];
  74.     [currentWindow close];
  75.  
  76.     return self;
  77. }
  78.  
  79. //////////////////////////////////////////////////////////////
  80. // Print the contents of the current window.
  81. // Since the Print... menu item should be disabled when necessary,
  82. // the current window should always be a Gopher window when this
  83. // comes up.
  84.  
  85. - print:sender
  86. {
  87.     [currentWindow print];
  88.  
  89.     return self;
  90. }
  91.  
  92. //////////////////////////////////////////////////////////////
  93. // Save the contents of the current window.
  94.  
  95. - save:sender
  96. {
  97.     id mySavePanel;
  98.  
  99.     mySavePanel = [SavePanel new];
  100.     [mySavePanel setTitle:"Save Gopher text"];
  101.     [mySavePanel setPrompt:"Save text into :"];
  102.  
  103.     if ([mySavePanel runModalForDirectory:"." 
  104.                                             file:[currentWindow  currentCellName]])
  105.     [currentWindow saveAs:[mySavePanel filename]];
  106.  
  107.     return self;
  108. }
  109.  
  110. ///////////////////////////////////////////////////////////////
  111. // Find the next text string if possible
  112.  
  113. - findText:sender
  114. {
  115.      [currentWindow highlightNextWord];
  116.     
  117.      return self;
  118. }
  119.  
  120.  
  121. //////////////////////////////////////////////////////////////
  122. // Show current item info.
  123.  
  124. - itemInfo:sender
  125. {
  126.     [currentWindow showItemInfo];
  127.  
  128.     return self;
  129. }
  130.  
  131. //////////////////////////////////////////////////////////////
  132. // Stop currently playing sound (if any).
  133.  
  134. - stopSound:sender
  135. {
  136.     if (mutex_try_lock(soundIsPlaying) != 0) {    // No sound playing
  137.     mutex_unlock(soundIsPlaying);
  138.     return self;
  139.     }
  140.  
  141.     if (mutex_try_lock(soundShouldStop) != 0) {
  142.     soundShouldStopFlag = 1;
  143.     mutex_unlock(soundShouldStop);
  144.     }
  145.  
  146.     return self;
  147. }
  148.  
  149. //////////////////////////////////////////////////////////////
  150. // This is called every time a Gopher window becomes active.
  151. // The sender is NOT the window itself, but the GopherClient
  152. // responsible for it (which is its delegate).
  153.  
  154. - newFrontWindow:sender
  155. {
  156.     [PrintMenuCell setEnabled:YES];
  157.     [SaveMenuCell setEnabled:YES];
  158.     [CloseMenuCell setEnabled:YES];
  159.     currentWindow = sender;
  160.  
  161.     return self;
  162. }
  163.  
  164. //////////////////////////////////////////////////////////////
  165. // This is called every time a Gopher window becomes inactive.
  166. // The sender is NOT the window itself, but the GopherClient
  167. // responsible for it (which is its delegate).
  168.  
  169. - oldFrontWindow:sender
  170. {
  171.     if (currentWindow == sender) {    // Only if this was the current window
  172.     [PrintMenuCell setEnabled:NO];
  173.     [SaveMenuCell setEnabled:NO];
  174.     [CloseMenuCell setEnabled:NO];
  175.     currentWindow = NIL;
  176.     }
  177.  
  178.     return self;
  179. }
  180.  
  181. //////////////////////////////////////////////////////////////
  182. // 
  183.  
  184. - savePreferences:sender
  185. {
  186.     
  187.     NXWriteDefault("Gopher", "GopherHost", [GopherHostTextItem stringValue]);
  188.     NXWriteDefault("Gopher", "GopherPort", [GopherPortTextItem stringValue]);
  189.  
  190.     defaultApp = [teminalApp selectedRow];
  191.     if ( defaultApp == 1 )
  192.     NXWriteDefault("Gopher", "TermApp", "1");
  193.     else
  194.     NXWriteDefault("Gopher", "TermApp", "0");
  195.         
  196.     [PreferenceWindow performClose:self];
  197.  
  198.     return self;
  199. }
  200.  
  201. ///////////////////////////////////////////////////////////////////////////////
  202. // Delegate methods
  203.  
  204.  
  205. //////////////////////////////////////////////////////////////
  206. // Is called after the application has opened.
  207.  
  208. - appDidInit:sender
  209. {
  210.     static NXDefaultsVector GopherDefaults = {
  211.     {"GopherHost", DEFAULT_SERVER},
  212.     {"GopherPort", DEFAULT_PORT},
  213.     {"TermApp", "0"},
  214.     {NULL}
  215.     };
  216.     struct stat myStat;
  217.  
  218.     if (NXRegisterDefaults("Gopher", GopherDefaults) == 0)
  219.     /*error*/;
  220.  
  221.     strncpy(defaultHost, NXGetDefaultValue("Gopher", "GopherHost"), 256);
  222.     [GopherHostTextItem setStringValue:defaultHost];
  223.  
  224.     defaultPort = atoi(NXGetDefaultValue("Gopher", "GopherPort"));
  225.     [GopherPortTextItem setIntValue:defaultPort];
  226.  
  227.     defaultApp = atoi(NXGetDefaultValue("Gopher", "TermApp"));
  228.     [teminalApp selectCellAt:defaultApp:0];
  229.  
  230.     soundIsPlaying = mutex_alloc();
  231.     soundShouldStop = mutex_alloc();
  232.  
  233.     if (stat(PlayCommand, &myStat) == 0)
  234.     if (myStat.st_mode & 0000001)
  235.         shouldUseSound = 1;
  236.  
  237.     [[GopherClient new] setDispatcher:self];
  238.  
  239.     return self;
  240. }
  241.  
  242. @end
  243.