File: MySQLStartupPref.m

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (270 lines) | stat: -rw-r--r-- 7,895 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
//  MySQLStartupPref.m
//  MySQLStartup
//
//  Created by Alfredo Kojima on 1/4/05.
//  Copyright (c) 2005 MySQL AB. All rights reserved.
//

#define MYSQL_PREFPANE
#import "MySQLStartupPref.h"
#include "../mahelper.h"
#include "../mahelper_priv.h"
#undef NSLocalizedString
#define NSLocalizedString(a,b) a

@implementation MySQLStartupPref

- (void) mainViewDidLoad
{
  authRef= 0;
  curIsRunning= NO;
  firstTime= YES;

  [self isRunning];
  [self isAutoStarting];
  [NSTimer scheduledTimerWithTimeInterval: 2.0
                                   target:self
                                 selector:@selector(isRunning) 
                                 userInfo:nil 
                                  repeats:YES];

  [NSTimer scheduledTimerWithTimeInterval: 2.0 
                                   target:self
                                 selector:@selector(isAutoStarting) 
                                 userInfo:nil 
                                  repeats:YES];
}

- (void)dealloc
{
  if (authRef)
    AuthorizationFree(authRef,kAuthorizationFlagDefaults);
  [super dealloc];
}


- (BOOL)isAutoStarting
{  
  BOOL autoStart;
  
  if (autoStart= mautoStartState())
    [autoStartCheck setState:NSOnState];
  else
    [autoStartCheck setState:NSOffState];
  
  return autoStart;
}


- (BOOL)isRunning
{
  BOOL state= NO;
  NSTask *task = [[NSTask alloc] init];  
#if 0 // apparently, we can't run suid programs in Tiger anymore...
  NSData *inData = nil;
  NSArray *array = nil;
  NSString *string = nil;
  int i, count;
  
  [task setLaunchPath:@"/bin/ps"];
  [task setArguments:[NSArray arrayWithObjects:@"ax",@"-ocommand",nil]];
#else
  [task setLaunchPath:[NSString stringWithUTF8String:MYSQLADMIN_COMMAND]];
  [task setArguments:[NSArray arrayWithObject:@"ping"]];
#endif
  [task setStandardOutput:[NSPipe pipe]];
  [task setStandardError:[task standardOutput]];
  [task launch];
  [task waitUntilExit];
#if 0
  inData = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
  string = [[[NSString alloc] initWithData:inData encoding:NSASCIIStringEncoding] autorelease];
  array = [string componentsSeparatedByString:@"\n"];
  [task release];
  count= [array count];
  for (i= 0; i < count; i++)
  {
    NSString *line= [array objectAtIndex:i];
    NSRange range;
    
    range= [line rangeOfString:@"mysqld"];
    if (range.location != NSNotFound)
    {
      state= YES;
      break;
    }
  }
#else
  state= [task terminationStatus] == 0 ? YES : NO;
#endif
  if (state != curIsRunning || firstTime)
  {
    NSBundle *bundle= [NSBundle bundleForClass:[self class]];
    firstTime= NO;
    curIsRunning= state;
    if (state)
    {
      [button setTitle:NSLocalizedString(@"Stop MySQL Server", nil)];
      [descrText setStringValue:@"The MySQL Database Server is started and ready for client connections.\n"
        "To shut the Server down, use the \"Stop MySQL Server\" button."];
      [warningText setHidden:NO];
      [[stateText cell] setTextColor: [NSColor greenColor]];
      [stateText setStringValue:@"running"];
      [stateImage setImage:
        [[[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"instance_started"
                                                                 ofType:@"png"]] autorelease]];
    } else {
      [button setTitle:NSLocalizedString(@"Start MySQL Server", nil)];
      [descrText setStringValue:@"The MySQL Database Server is currently stopped.\n"
        "To start it, use the \"Start MySQL Server\" button."];
      [warningText setHidden:YES];
      [[stateText cell] setTextColor: [NSColor redColor]];
      [stateText setStringValue:@"stopped"];
      [stateImage setImage:
        [[[NSImage alloc] initWithContentsOfFile:[bundle pathForResource:@"instance_stopped"
                                                                  ofType:@"png"]] autorelease]];
    }
    [stateImage setNeedsDisplay];
  }
  
  return state;
}



- (int)performCommand:(MAHelperCommand)command
{
  //AuthorizationRef authRef= NULL;
  NSString *helperPath;
  AuthorizationItem right = { "com.mysql.administrator.helper", 0, NULL, 0 };
  AuthorizationRights rightSet = { 1, &right };
  OSStatus status;

  helperPath= [[NSBundle bundleForClass:[self class]] pathForResource:@"mahelper"
                                                               ofType:@""];

  if (!authRef)
  {
    if (AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, 
                            &authRef)!=errAuthorizationSuccess)
    {
      NSLog(@"Could not create authorization reference object.");
      return MAHelperCommandInternalError;
    }
    
    status = AuthorizationCopyRights(authRef, &rightSet, kAuthorizationEmptyEnvironment, 
                                     kAuthorizationFlagDefaults | kAuthorizationFlagPreAuthorize
                                     | kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights,
                                     NULL);
  } else
    status= errAuthorizationSuccess;
  if (status == errAuthorizationSuccess)
  {
    int hstatus;
    
    hstatus = mhelperPerformCommand(authRef, [helperPath UTF8String], command);
  
    return hstatus;
  }
  else
  {
    AuthorizationFree(authRef, kAuthorizationFlagDestroyRights);
    authRef= 0;
    if (status != errAuthorizationCanceled)
    {
      // pre-auth failed
      NSLog(@"Pre-auth failed");
      return MAHelperCommandAuthFailed;
    }
    else
      return MAHelperCommandCancelled;
  }
}


static NSString *errorMessage(int rc)
{
  switch (rc)
  {
    case MAHelperCommandInternalError:
      return @"Internal error.";
    case MAHelperCommandSuccess:
      return @"Success";
    case MAHelperCommandExecFailed:
      return @"Could not execute command.";
    case MAHelperCommandChildError:
      return @"Error in command.";
    case MAHelperCommandAuthFailed:
      return @"Authentication error.";
    case MAHelperCommandOperationFailed:
      return @"Operation failed.";
    case MAHelperCommandCancelled:
      return @"Cancelled.";
    case MAHelperCommandHelperNotFound:
      return @"Helper binary not found. Please reinstall the preference pane.";
    default:
      return [NSString stringWithFormat:@"%i", rc];//@"";
  }
}


- (BOOL)shutdownServer
{
  MAHelperCommand command;
  
  command.authorizedCommandId= MAHelperShutdownMySQL;
  
  return [self performCommand:command]==MAHelperCommandSuccess;
}

- (IBAction)toggleServer:(id)sender
{
  MAHelperCommand command;
  BOOL start= ![self isRunning];
  int rc;

  command.authorizedCommandId= start ? MAHelperStartMySQL : MAHelperStopMySQL;
  
  if ((rc= [self performCommand:command])!=MAHelperCommandSuccess)
  {
    if (rc != MAHelperCommandCancelled)
    {
      if (start)
        NSRunAlertPanel(NSLocalizedString(@"Error",nil), 
                        [NSString stringWithFormat:NSLocalizedString(@"Could not startup MySQL Server.\nReason: %@",nil), errorMessage(rc)], 
                        nil, nil, nil);
      else
	  {
		if (![self shutdownServer])
		{
		  NSRunAlertPanel(NSLocalizedString(@"Error",nil), 
						  [NSString stringWithFormat:NSLocalizedString(@"Could not stop MySQL Server.\nReason: %@",nil), errorMessage(rc)], 
						  nil, nil, nil);
		}
	  }
    }
  }
}


- (IBAction)changeAutoStart:(id)sender
{
  MAHelperCommand command;
  BOOL flag= ![self isAutoStarting];
  int rc;
    
  command.authorizedCommandId= MAHelperToggleAutoStart;
  command.enable= flag;
  if ((rc = [self performCommand:command]) != MAHelperCommandSuccess)
  {
    if (rc != MAHelperCommandCancelled)
      NSRunAlertPanel(NSLocalizedString(@"Error",nil),
                      [NSString stringWithFormat:NSLocalizedString(@"Could not change automatic startup configuration.\nReason: %@",nil), errorMessage(rc)], 
                      nil, nil, nil);
  }
  [self isAutoStarting];
}


@end