home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Networking / SambaManager / Service.m < prev    next >
Encoding:
Text File  |  1998-04-01  |  6.5 KB  |  288 lines

  1. /*
  2.     SambaManger. A graphical frontend to configure the NetInfo enhanced samba.
  3.     Copyright (C) 1998  Robert Frank
  4.  
  5.     This program is free software; you can redistribute it and/or modify
  6.     it under the terms of the GNU General Public License as published by
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.         
  19.         Robert Frank, frank@ifi.unibas.ch
  20. */
  21.  
  22. #import "Service.h"
  23. #import "Controller.h"
  24. #import "NIProperty.h"
  25.  
  26. // The property names of a service's directory:
  27. #define S_NAME                "name"
  28.  
  29. // Static (class) variables:
  30. static NXStringTable    *strings = nil;        // The localized strings.
  31.  
  32. @implementation Service
  33.  
  34. // ************************************************************************
  35. // Local methods:
  36. - free
  37. {
  38.         [ni_dirObj free];
  39.         return [super free];
  40. }
  41.  
  42. - init:sender dirObj:(NIDirectory *)dir delta:(NXCoord *)offset service:(const char *)sName
  43. {
  44. NXCoord        loffset;
  45. NXRect        wframe;
  46. char            ud[1024], *dName;
  47.  
  48.  
  49.         // Do higher level inits first.
  50.         [super init];
  51.         controller = sender;
  52.         ni_dirObj = dir;
  53.  
  54.         if (*offset < 100.0)
  55.             loffset = *offset + 20.0;
  56.         else
  57.             loffset = 0.0;
  58.  
  59.         if (window == nil) {    
  60.             strcpy(ud, [self name]);
  61.             strcat(ud, ".nib");
  62.             if (![NXApp loadNibSection:ud owner:self])
  63.                 return [super free];
  64.         }
  65.  
  66.         // Move each new window by a little offset.
  67.         [window getFrame:&wframe];
  68.         wframe.origin.x+= loffset;
  69.         wframe.origin.y-= loffset;
  70.         [window moveTo:wframe.origin.x :wframe.origin.y];
  71.         
  72.         if (sName)
  73.             [window setTitle:[strings valueForStringKey:"Title:Loading ..."]];
  74.         else
  75.             [window setTitle:[strings valueForStringKey:"Title:Untitled"]];
  76.         
  77.         [window makeKeyAndOrderFront:self];
  78.         [window disableFlushWindow];
  79.             
  80.         if (![self setupAndLoad])
  81.             return [super free];
  82.         
  83.         if (sName) {
  84.             dName = (char *)[dir domainName];
  85.             sprintf(ud, "%s/%s", (dName && *dName && (dName[1] != '\0'))?dName:"", sName);
  86.             [window setTitleAsFilename:ud];
  87.         }
  88.         
  89.         [window reenableFlushWindow];
  90.         [window display];
  91.         *offset = loffset;
  92.  
  93.         return self;
  94. }
  95.  
  96. // ************************************************************************
  97. // Class methods:
  98. + initialize
  99. // Called once by the run time system
  100. {
  101. char    buf[MAXPATHLEN + 1];
  102.  
  103.         if (!strings)
  104.             if ( [[NXBundle mainBundle] getPath:buf forResource:"Service" ofType:"strings"] )
  105.                 strings = [[[NXStringTable alloc] init] readFromFile:buf];
  106.         return nil;
  107. }
  108.  
  109. + (const char *)stringFor:(const char *)strng
  110. {
  111.         return [strings valueForStringKey:strng];
  112. }
  113.  
  114. + new:sender at:(NXCoord *)offset
  115. {
  116.         return nil;
  117. }
  118.  
  119. + open:sender at:(NXCoord *)offset
  120. {
  121.         return nil;
  122. }
  123.  
  124. // ************************************************************************
  125. //    Methods:
  126.  
  127. - (BOOL)minimumOK
  128. {
  129.         if (![[ni_dirObj property:S_NAME] values]) {
  130.             NXRunAlertPanel(getString("Alert:Alert"),
  131.                                             getString("Message:Must have a name"),
  132.                                             getString("Button:OK"),NULL,NULL);
  133.             return NO;
  134.         }
  135.         return YES;
  136. }
  137.  
  138. - revert:sender
  139. {
  140.         if ([ni_dirObj reset])
  141.             [window setDocEdited:NO];
  142.     return self;
  143. }
  144.  
  145. - save:sender
  146. {
  147. char    ud[1024], *dName;
  148. id    status = nil;
  149.  
  150.         if ([self minimumOK])
  151.             if (status = [ni_dirObj save]) {
  152.                 [ni_dirObj setAllowChangName:NO];
  153.                 [window setDocEdited:NO];
  154.                 dName = (char *)[ni_dirObj domainName];
  155.                 sprintf(ud, "%s/%s", (dName && *dName && (dName[1] != '\0'))?dName:"", [ni_dirObj baseName]);
  156.                 [window setTitleAsFilename:ud];
  157.             }
  158.         return status;
  159. }
  160.  
  161. - saveToDomain:sender
  162. {
  163. char    ud[1024], *dName;
  164. id        status = nil;
  165.  
  166.         if ([self minimumOK])
  167.             if (status = [ni_dirObj saveToDomain]) {
  168.                 [ni_dirObj setAllowChangName:NO];
  169.                 [window setDocEdited:NO];
  170.                 dName = (char *)[ni_dirObj domainName];
  171.                 sprintf(ud, "%s/%s", (dName && *dName && (dName[1] != '\0'))?dName:"", [ni_dirObj baseName]);
  172.                 [window setTitleAsFilename:ud];
  173.             }
  174.         return status;
  175. }
  176.  
  177. - close:sender
  178. {
  179. int        choice;
  180.  
  181.         if ([window isDocEdited]) {
  182.             choice = NXRunAlertPanel([strings valueForStringKey:"Alert:Warning"],
  183.                                                              [strings valueForStringKey:"Message:Save changes?"],
  184.                                                              [strings valueForStringKey:"Button:Save"],
  185.                                                              [strings valueForStringKey:"Button:Don't Save"],
  186.                                                              [strings valueForStringKey:"Button:Cancel"],
  187.                                                              [ni_dirObj baseName]?[ni_dirObj baseName]:[window title]);
  188.  
  189.             switch (choice) {
  190.                 case NX_ALERTDEFAULT:
  191.                     if (![self save:self])
  192.                         return nil;
  193.                 case NX_ALERTALTERNATE:
  194.                     break;
  195.                 default:
  196.                     return nil;
  197.             }
  198.         }
  199.  
  200.         if (sender) {
  201.             [window setDelegate:nil];
  202.             [window close];
  203.             [controller removeFromServiceList:self];
  204.             [self free];
  205.         }
  206.     return self;
  207. }
  208.  
  209. - delete:sender
  210. {
  211. int        status;
  212.  
  213.         status = NXRunAlertPanel([strings valueForStringKey:"Alert:Delete"],
  214.                                                             [strings valueForStringKey:"Message:Really delete this entry from NetInfo?"],
  215.                                                             [strings valueForStringKey:"Button:Yes"],
  216.                                                             [strings valueForStringKey:"Button:No"],
  217.                                                             NULL, NULL);
  218.         switch (status) {
  219.             case NX_ALERTDEFAULT: 
  220.                 break;
  221.             case NX_ALERTALTERNATE:
  222.             default:
  223.                 return nil;
  224.         }
  225.  
  226.         if ([ni_dirObj delete]) {
  227.             [window setDocEdited:NO];
  228.             [window performClose:self];
  229.             return self;
  230.         } else
  231.         return nil;
  232. }
  233.  
  234. // Display the window.
  235. - show:sender
  236. {
  237.         [window makeKeyAndOrderFront:self];
  238.         return self;
  239. }
  240.  
  241. // Check if the item passed is identical to this one.
  242. - (BOOL)isSame:(const char *)str inDomain:(const char *)dName
  243. {
  244.         return !strcmp([ni_dirObj directory], str) && !strcmp([ni_dirObj domainName], dName);
  245. }
  246.  
  247. - (BOOL)isOld:sender
  248. {
  249.         return ([ni_dirObj domainName] != NULL);
  250. }
  251.  
  252. - setupAndLoad
  253. {
  254.         return self;
  255. }
  256.  
  257. // ************************************************************************
  258. // Delegates:
  259.  
  260. - textDidGetKeys:sender isEmpty:(BOOL)flag
  261. {
  262.         [window setDocEdited:YES];
  263.         return self;
  264. }
  265.  
  266. - (BOOL)propertyWillChange:sender value:(char **)string default:(BOOL *)remove index:(int)anInt
  267. {
  268.         return NO;
  269. }
  270.  
  271. - propertyDidChange:sender value:(const char *)string
  272. {
  273.         [window setDocEdited:YES];
  274.         return self;
  275. }
  276.  
  277. - windowWillClose:sender
  278. {
  279.         if ([self close:nil]) {
  280.             [sender setDelegate:nil];
  281.             [controller removeFromServiceList:self];
  282.             [self free];
  283.             return self;
  284.         } else
  285.             return nil;
  286. }
  287. @end
  288.