Changeset 935

Show
Ignore:
Timestamp:
01/14/05 16:25:52 (4 years ago)
Author:
morris
Message:

Split button controls to WCDashboardController

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • WiredServer/trunk/WCStatusController.h

    r923 r935  
    1 /* $Id: WCStatusController.h,v 1.1 2005/01/07 23:14:20 morris Exp $ */ 
     1/* $Id: WCStatusController.h,v 1.2 2005/01/14 15:25:52 morris Exp $ */ 
    22 
    33/* 
     
    3131        IBOutlet NSTextField                    *_versionTextField; 
    3232         
    33         IBOutlet NSButton                               *_startButton; 
    34         IBOutlet NSButton                               *_restartButton; 
    35         IBOutlet NSButton                               *_reloadButton; 
    36         IBOutlet NSButton                               *_registerButton; 
    37         IBOutlet NSButton                               *_indexButton; 
    38  
    3933        IBOutlet NSTextField                    *_currentUsersTextField; 
    4034        IBOutlet NSTextField                    *_totalUsersTextField; 
     
    5448         
    5549        NSTimer                                                 *_timer; 
    56  
    5750        NSDate                                                  *_uptime; 
    5851         
     52        NSMutableArray                                  *_status; 
    5953        NSMutableArray                                  *_usersData; 
    6054        NSMutableArray                                  *_downloadsData, *_uploadsData; 
     
    6357        unsigned long long                              _inBytes, _outBytes; 
    6458         
    65         BOOL                                                    _requiresAuthorization; 
    66         BOOL                                                    _launchesAtBoot; 
    6759        BOOL                                                    _running; 
    6860        BOOL                                                    _available; 
    69         BOOL                                                    _authorized; 
    7061} 
    7162 
     
    7970 
    8071- (void)                                                        awakeFromController; 
    81  
    82 - (void)                                                        setRequiresAuthorization:(BOOL)value; 
    83 - (BOOL)                                                        requiresAuthorization; 
    84 - (void)                                                        setLaunchesAtBoot:(BOOL)value; 
    85 - (BOOL)                                                        launchesAtBoot; 
     72- (void)                                                        updateFromController; 
    8673 
    8774- (BOOL)                                                        isRunning; 
    8875- (BOOL)                                                        isAvailable; 
    89 - (BOOL)                                                        isAuthorized; 
    90  
    91 - (IBAction)                                            start:(id)sender; 
    92 - (IBAction)                                            restart:(id)sender; 
    93 - (IBAction)                                            reload:(id)sender; 
    94 - (IBAction)                                            registerWithTracker:(id)sender; 
    95 - (IBAction)                                            indexFiles:(id)sender; 
     76- (NSArray *)                                           status; 
    9677 
    9778@end 
  • WiredServer/trunk/WCStatusController.m

    r923 r935  
    1 /* $Id: WCStatusController.m,v 1.1 2005/01/07 23:14:20 morris Exp $ */ 
     1/* $Id: WCStatusController.m,v 1.2 2005/01/14 15:25:52 morris Exp $ */ 
    22 
    33/* 
     
    3535- (void)                                updatePid; 
    3636- (void)                                updateStatus; 
    37  
    38 - (NSArray *)                   arguments; 
     37- (void)                                updateInterface; 
    3938 
    4039@end 
     
    6463         
    6564        // --- create arrays of graph data 
     65        _status = [[NSMutableArray alloc] init]; 
    6666        _usersData = [[NSMutableArray alloc] initWithCapacity:WCStatusDataPoints]; 
    6767        _downloadsData = [[NSMutableArray alloc] initWithCapacity:WCStatusDataPoints]; 
     
    7777                [_outData addObject:value]; 
    7878        } 
    79  
    80         // --- subscribe to these 
    81         [[NSNotificationCenter defaultCenter] 
    82                 addObserver:self 
    83                    selector:@selector(applicationWillTerminate:) 
    84                            name:NSApplicationWillTerminateNotification 
    85                          object:NULL]; 
    86          
    87         [[NSNotificationCenter defaultCenter] 
    88                 addObserver:self 
    89                    selector:@selector(authorizationStatusDidChange:) 
    90                            name:WCAuthorizationStatusDidChange 
    91                          object:NULL]; 
    92  
    93         [[NSNotificationCenter defaultCenter] 
    94                 addObserver:self 
    95                    selector:@selector(configDidChange:) 
    96                            name:WCConfigDidChange 
    97                          object:NULL]; 
    9879} 
    9980 
     
    11495 
    11596 
     97- (void)updateFromController { 
     98        [_timer fire]; 
     99} 
     100 
     101 
     102 
    116103- (void)dealloc { 
    117104        [_timer release]; 
    118105 
     106        [_status release]; 
    119107        [_usersData release]; 
    120108        [_downloadsData release]; 
     
    130118#pragma mark - 
    131119 
    132 - (void)applicationWillTerminate:(NSNotification *)notification { 
    133         if([self isRunning] && [WCSettings boolForKey:WCTerminateServerOnQuit]) 
    134                 [self start:self]; 
    135 } 
    136  
    137  
    138  
    139 - (void)authorizationStatusDidChange:(NSNotification *)notification { 
    140         _authorized = [[WCAuthorization authorization] isAuthorized]; 
    141          
    142         [self update]; 
    143 } 
    144  
    145  
    146  
    147 - (void)configDidChange:(NSNotification *)notification { 
    148         if([self isAvailable] && [self isRunning]) 
    149                 [self reload:self]; 
    150 } 
    151  
    152  
    153  
    154 #pragma mark - 
    155  
    156120- (void)statusTimer:(NSTimer *)timer { 
    157121        [self updatePid]; 
    158         [self updateStatus]; 
     122         
     123        if(_running) 
     124                [self updateStatus]; 
     125         
     126        if(_running && [_status count] > 0) 
     127                [self updateInterface]; 
    159128         
    160129        [_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:[WCSettings doubleForKey:WCUpdateInterval]]]; 
     
    166135 
    167136- (void)update { 
    168         BOOL    enabled; 
    169         BOOL    authorized; 
    170  
    171137        // --- set running status 
    172138        if([self isRunning]) { 
     
    180146                        [_statusTextField setStringValue:NSLS(@"Wired is not available", @"Status")]; 
    181147        } 
    182  
    183         // --- update start/stop button titles 
    184         if([self isRunning]) 
    185                 [_startButton setTitle:NSLS(@"Stop", "Stop button title")]; 
    186         else 
    187                 [_startButton setTitle:NSLS(@"Start", "Start button title")]; 
    188          
    189         // --- update buttons 
    190         authorized = [self requiresAuthorization] ? [self isAuthorized] : YES; 
    191         enabled = authorized && [self isAvailable]; 
    192         [_startButton setEnabled:enabled]; 
    193         enabled = authorized && [self isAvailable] && [self isRunning]; 
    194         [_restartButton setEnabled:enabled]; 
    195         [_reloadButton setEnabled:enabled]; 
    196         [_registerButton setEnabled:enabled]; 
    197         [_indexButton setEnabled:enabled]; 
    198148} 
    199149 
     
    287237         
    288238        // --- running status changed? 
    289         if(running != [self isRunning]) { 
     239        if(running != _running) { 
    290240                _running = running; 
    291241                 
     
    302252 
    303253- (void)updateStatus { 
    304         static BOOL                     loaded; 
    305         NSString                        *status, *inSpeedString, *outSpeedString; 
    306         NSArray                         *items; 
    307         NSDate                          *date; 
    308         NSTimeInterval          interval; 
    309         unsigned long long      inBytes, outBytes; 
    310         unsigned int            users, downloads, uploads, inSpeed, outSpeed; 
     254        NSString        *status; 
     255        NSDate          *date; 
    311256         
    312257        // --- read wired status file 
    313258        status = [NSString stringWithContentsOfFile:WCExpandWiredPath(@"wired.status")]; 
    314259         
    315         if(status) { 
    316                 interval = [WCSettings doubleForKey:WCUpdateInterval]; 
    317                 items = [status componentsSeparatedByString:@" "]; 
    318                 date = [NSDate dateWithTimeIntervalSince1970:[[items objectAtIndex:0] intValue]]; 
     260        if(!status) { 
     261                [_status removeAllObjects]; 
     262        } else { 
     263                [_status setArray:[status componentsSeparatedByString:@" "]]; 
     264                 
     265                date = [NSDate dateWithTimeIntervalSince1970:[[_status objectAtIndex:0] intValue]]; 
    319266                 
    320267                if(![date isEqualToDate:_uptime]) { 
    321268                        [_uptime release]; 
    322269                        _uptime = [date retain]; 
    323                          
    324                         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(update) object:NULL]; 
    325                         [self performSelector:@selector(update) withObject:NULL afterDelay:0.1]; 
     270 
     271                        [self performSelectorOnce:@selector(update) withObject:NULL afterDelay:0.1]; 
    326272                } 
    327                  
    328                 users = [[items objectAtIndex:1] unsignedIntValue]; 
     273        } 
     274
     275 
     276 
     277 
     278- (void)updateInterface { 
     279        NSString                        *inSpeedString, *outSpeedString; 
     280        NSTimeInterval          interval; 
     281        unsigned long long      inBytes, outBytes; 
     282        unsigned int            inSpeed, outSpeed; 
     283        int                                     users, downloads, uploads; 
     284         
     285        // --- update users graph 
     286        if(_usersGraphView) { 
     287                users = [[_status objectAtIndex:1] intValue]; 
    329288                [_usersData removeObjectAtIndex:0]; 
    330                 [_usersData addObject:[NSNumber numberWithUnsignedInt:users]]; 
    331                  
    332                 // --- update users graph 
     289                [_usersData addObject:[NSNumber numberWithInt:users]]; 
    333290                [_usersGraphView setInData:_usersData]; 
    334291                [_usersGraphView setNeedsDisplay:YES]; 
     
    338295                                ? NSLS(@"user", @"User singular") 
    339296                                : NSLS(@"users", @"User plural")]]; 
    340                  
    341                 // --- get transfers 
    342                 downloads = [[items objectAtIndex:3] unsignedIntValue]; 
    343                 uploads = [[items objectAtIndex:5] unsignedIntValue]; 
     297        } 
     298         
     299        // --- update transfers graph 
     300        if(_transfersGraphView) { 
     301                downloads = [[_status objectAtIndex:3] unsignedIntValue]; 
     302                uploads = [[_status objectAtIndex:5] unsignedIntValue]; 
    344303                [_downloadsData removeObjectAtIndex:0]; 
    345304                [_downloadsData addObject:[NSNumber numberWithUnsignedInt:downloads]]; 
    346305                [_uploadsData removeObjectAtIndex:0]; 
    347306                [_uploadsData addObject:[NSNumber numberWithUnsignedInt:uploads]]; 
    348                  
    349                 // --- update transfers graph 
    350307                [_transfersGraphView setInData:_uploadsData]; 
    351308                [_transfersGraphView setOutData:_downloadsData]; 
     
    361318                                ? NSLS(@"dl", @"Download singular") 
    362319                                : NSLS(@"dls", @"Download plural")]]; 
    363                  
    364                 // --- get bandwidth 
    365                 inBytes = [[items objectAtIndex:8] unsignedLongLongValue]; 
    366                 outBytes = [[items objectAtIndex:7] unsignedLongLongValue]; 
     320        } 
     321                 
     322        // --- update bandwidth graph 
     323        if(_bandwidthGraphView) { 
     324                inBytes = [[_status objectAtIndex:8] unsignedLongLongValue]; 
     325                outBytes = [[_status objectAtIndex:7] unsignedLongLongValue]; 
    367326                inSpeed = 0; 
    368327                outSpeed = 0; 
    369328                 
    370                 if(loaded) { 
    371                         if(inBytes > _inBytes) 
     329                if(_inBytes > 0 && _outBytes > 0) { 
     330                        interval = [WCSettings doubleForKey:WCUpdateInterval]; 
     331 
     332                        if(_inBytes > 0 && inBytes > _inBytes) 
    372333                                inSpeed = (inBytes - _inBytes) / interval; 
    373334                         
     
    375336                                outSpeed = (outBytes - _outBytes) / interval; 
    376337                } 
    377                  
    378                 // --- update bandwidth data 
     338 
     339                inSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:inSpeed]]; 
     340                outSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:outSpeed]]; 
     341                 
    379342                [_inData removeObjectAtIndex:0]; 
    380343                [_inData addObject:[NSNumber numberWithUnsignedInt:inSpeed]]; 
    381344                [_outData removeObjectAtIndex:0]; 
    382345                [_outData addObject:[NSNumber numberWithUnsignedInt:outSpeed]]; 
    383                  
    384                 // --- get strings 
    385                 inSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:inSpeed]]; 
    386                 outSpeedString = [NSSWF:@"%@/s", [NSString humanReadableStringForSize:outSpeed]]; 
    387                  
    388                 // --- update bandwidth graph 
    389346                [_bandwidthGraphView setInData:_inData]; 
    390347                [_bandwidthGraphView setOutData:_outData]; 
     
    396353                        outSpeedString, 
    397354                        NSLS(@"out", @"Speed out")]]; 
    398                  
    399                 // --- update fields 
    400                 [_currentUsersTextField setIntValue:users]; 
    401                 [_totalUsersTextField setStringValue:[items objectAtIndex:2]]; 
    402                 [_currentDownloadsTextField setIntValue:downloads]; 
    403                 [_totalDownloadsTextField setStringValue:[items objectAtIndex:4]]; 
    404                 [_currentUploadsTextField setIntValue:uploads]; 
    405                 [_totalUploadsTextField setStringValue:[items objectAtIndex:6]]; 
    406                 [_dataInTextField setStringValue:[NSString humanReadableStringForSize:inBytes]]; 
    407                 [_dataOutTextField setStringValue:[NSString humanReadableStringForSize:outBytes]]; 
    408                 [_dataInPerSecTextField setStringValue:inSpeedString]; 
    409                 [_dataOutPerSecTextField setStringValue:outSpeedString]; 
    410                  
    411                 // --- reset 
     355 
    412356                _inBytes = inBytes; 
    413357                _outBytes = outBytes; 
    414                 loaded = YES; 
    415         } 
    416 
    417  
    418  
    419  
    420 #pragma mark - 
    421  
    422 - (NSArray *)arguments { 
    423         NSMutableArray  *arguments; 
    424          
    425         arguments = [NSMutableArray array]; 
    426          
    427         switch([[WCSettings objectForKey:WCLogMethod] intValue]) { 
    428                 case WCLogMethodSyslog: 
    429                         [arguments addObject:@"-s"]; 
    430                         [arguments addObject:[WCSettings objectForKey:WCSyslogFacility]]; 
    431                         break; 
    432                          
    433                 case WCLogMethodFile: 
    434                         [arguments addObject:@"-L"]; 
    435                         [arguments addObject:[WCSettings objectForKey:WCLogFile]]; 
    436                          
    437                         if([WCSettings boolForKey:WCLimitLogFile]) { 
    438                                 [arguments addObject:@"-i"]; 
    439                                 [arguments addObject:[NSSWF:@"%u", [WCSettings intForKey:WCLimitLogFileLines]]]; 
    440                         } 
    441                         break; 
    442         } 
    443          
    444         return arguments; 
    445 
    446  
    447  
    448  
    449 #pragma mark - 
    450  
    451 - (void)setRequiresAuthorization:(BOOL)value { 
    452         _requiresAuthorization = value; 
    453 
    454  
    455  
    456  
    457 - (BOOL)requiresAuthorization { 
    458         return _requiresAuthorization; 
    459 
    460  
    461  
    462  
    463 - (void)setLaunchesAtBoot:(BOOL)value { 
    464         NSFileManager           *fileManager; 
    465         NSString                        *path, *owner, *temp, *string; 
    466         WCAuthorization         *authorization; 
    467         WCConfig                        *config; 
    468         BOOL                            status; 
    469          
    470         authorization = [WCAuthorization authorization]; 
    471         fileManager = [NSFileManager defaultManager]; 
    472         path = WCExpandWiredPath(@"etc/wired.startup"); 
    473          
    474         if(value) { 
    475                 string = [[self arguments] componentsJoinedByString:@" "]; 
    476                 owner = [fileManager ownerAtPath:path]; 
    477                  
    478                 if(!owner) 
    479                         owner = [fileManager ownerAtPath:[path stringByDeletingLastPathComponent]]; 
    480                  
    481                 if([owner isEqualToString:NSUserName()]) { 
    482                         [string writeToFile:path atomically:YES]; 
    483                 } 
    484                 else if([authorization isAuthorized]) { 
    485                         temp = [NSFileManager temporaryPathWithPrefix:@"wired" suffix:@"startup"]; 
    486                         status = [string writeToFile:temp atomically:YES]; 
    487                          
    488                         if(status) 
    489                                 status = [authorization movePath:temp toPath:path]; 
    490                          
    491                         if(status) { 
    492                                 config = [[WCConfigController configController] config]; 
    493                                 status = [authorization changeOwnerOfPath:path 
    494                                                                                                   toOwner:[config stringForKey:@"user"] 
    495                                                                                                         group:[config stringForKey:@"group"]]; 
    496                         } 
    497                 } 
    498         } else { 
    499                 if([[fileManager ownerAtPath:path] isEqualToString:NSUserName()]) 
    500                         [fileManager removeFileAtPath:path]; 
    501                 else if([authorization isAuthorized]) 
    502                         [authorization removeFileAtPath:path]; 
    503         } 
    504                  
    505         _launchesAtBoot = value; 
    506 
    507  
    508  
    509  
    510 - (BOOL)launchesAtBoot { 
    511         return _launchesAtBoot; 
     358        } 
     359 
     360        // --- update fields 
     361        [_currentUsersTextField setIntValue:users]; 
     362        [_totalUsersTextField setStringValue:[_status objectAtIndex:2]]; 
     363        [_currentDownloadsTextField setIntValue:downloads]; 
     364        [_totalDownloadsTextField setStringValue:[_status objectAtIndex:4]]; 
     365        [_currentUploadsTextField setIntValue:uploads]; 
     366        [_totalUploadsTextField setStringValue:[_status objectAtIndex:6]]; 
     367        [_dataInTextField setStringValue:[NSString humanReadableStringForSize:inBytes]]; 
     368        [_dataOutTextField setStringValue:[NSString humanReadableStringForSize:outBytes]]; 
     369        [_dataInPerSecTextField setStringValue:inSpeedString]; 
     370        [_dataOutPerSecTextField setStringValue:outSpeedString]; 
    512371} 
    513372 
     
    528387 
    529388 
    530 - (BOOL)isAuthorized { 
    531         return _authorized; 
    532 
    533  
    534  
    535  
    536 #pragma mark - 
    537  
    538 - (IBAction)start:(id)sender { 
    539         NSMutableArray  *arguments; 
    540          
    541         arguments = [NSMutableArray array]; 
    542          
    543         if([self isRunning]) { 
    544                 [arguments addObject:@"stop"]; 
    545         } else { 
    546                 [arguments addObject:@"start"]; 
    547                 [arguments addObjectsFromArray:[self arguments]]; 
    548         } 
    549          
    550         [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 
    551                                                                                           arguments:arguments]; 
    552 
    553  
    554  
    555  
    556 - (IBAction)restart:(id)sender { 
    557         NSMutableArray  *arguments; 
    558          
    559         arguments = [NSMutableArray array]; 
    560         [arguments addObject:@"restart"]; 
    561         [arguments addObjectsFromArray:[self arguments]]; 
    562          
    563         [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 
    564                                                                                           arguments:arguments]; 
    565 
    566  
    567  
    568  
    569 - (IBAction)reload:(id)sender { 
    570         [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 
    571                                                                                           arguments:[NSArray arrayWithObject:@"reload"]]; 
    572 
    573  
    574  
    575  
    576 - (IBAction)registerWithTracker:(id)sender { 
    577         [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 
    578                                                                                           arguments:[NSArray arrayWithObject:@"register"]]; 
    579 
    580  
    581  
    582  
    583 - (IBAction)indexFiles:(id)sender { 
    584         [[WCAuthorization authorization] launchTaskWithPath:WCExpandWiredPath(@"wiredctl") 
    585                                                                                           arguments:[NSArray arrayWithObject:@"index"]]; 
     389- (NSArray *)status { 
     390        return [_status count] > 0 ? _status : NULL; 
    586391} 
    587392