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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
#import "MAStartupVariables.h"
#import "MAXMLGUIController.h"
#include <fcntl.h>
#include <errno.h>
#import <MySQLToolsCommon/NSView_extras.h>
#import <MySQLToolsCommon/MDialogs.h>
#include "mahelper.h"
@implementation MAStartupVariables
- (int)performOpenCommand:(MAHelperCommand)command
withRight:(const char*)rightName
{
NSString *helperPath;
AuthorizationItem right = { rightName, 0, NULL, 0 };
AuthorizationRights rightSet = { 1, &right };
OSStatus status;
int fd= -1;
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 -1;
}
}
status = AuthorizationCopyRights(_authRef, &rightSet, kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults | kAuthorizationFlagPreAuthorize
| kAuthorizationFlagInteractionAllowed | kAuthorizationFlagExtendRights,
NULL);
if (status == errAuthorizationSuccess)
{
int hstatus;
char *error_message= NULL;
hstatus = mhelperPerformOpenCommand(_authRef, [helperPath fileSystemRepresentation], command,
&fd, &error_message);
if (hstatus != MAHelperCommandSuccess || fd < 0)
{
if (!(error_message && strcmp(error_message, "OK")==0))
{
NSLog(@"error_message %s", error_message);
NSRunAlertPanel(nil, [NSString stringWithFormat:@"Could not open /etc/my.cnf.\n%s",error_message?:""],
@"OK", NULL, NULL);
return -1;
}
}
if (error_message)
free(error_message);
}
else
{
AuthorizationFree(_authRef, kAuthorizationFlagDestroyRights);
_authRef= 0;
}
return fd;
}
- (IBAction)openMerlinSite:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.mysql.com/products/enterprise/"]];
}
- (IBAction)discardChanges:(id)sender
{
[_xmlGUI revertToSaved];
}
- (IBAction)saveChanges:(id)sender
{
MAHelperCommand command= { MAHelperOpenNewMyCnf, NO };
int fd;
fd= [self performOpenCommand:command
withRight:"com.mysql.administrator.write_mycnf"];
if (fd >= 0)
{
if ([_xmlGUI saveTo:fd])
{
command.authorizedCommandId= MAHelperCommitMyCnf;
if ([self performOpenCommand:command
withRight:"com.mysql.administrator.write_mycnf"] < 0)
{
NSLog(@"Error writing my.cnf changes");
}
}
close(fd);
}
}
- (void)setupWithSection:(NSString*)section
ofFile:(NSString*)file
{
char *version= [_owner mysql] ? myx_get_mysql_full_version([_owner mysql]) : g_strdup("5.0");
int i;
strtok(version, "-");
_xmlGUI= [[MAXMLGUIController alloc] initInTabView:tabView
mysql:[_owner mysql]
version:version
file:file
section:section];
[popup removeAllItems];
for (i= 0; i < [tabView numberOfTabViewItems]; i++)
{
[popup addItemWithTitle:[[tabView tabViewItemAtIndex:i] label]];
}
NSTabViewItem *advisorItem= [[NSTabViewItem alloc] initWithIdentifier:@"advisor"];
[advisorItem setView: adView];
[tabView addTabViewItem:[advisorItem autorelease]];
[popup addItemWithTitle:@"Administration & Security Advisors"];
[noteText setStringValue:
[NSString stringWithFormat:@"Editing file \"%@\", section \"%@\"",
file, section]];
[topBox setEnabledRecursive:YES];
// force refresh
[tabView selectLastTabViewItem: self];
[tabView selectFirstTabViewItem: self];
g_free(version);
}
- (IBAction)moreInfo:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.mysql.com/network"]];
}
- (bool)openConfigFile:(NSString*)file
{
MYX_STRINGLIST *sections;
MYX_ADMIN_LIB_ERROR error;
bool retval= true;
sections= myx_get_all_cnf_sections([file fileSystemRepresentation], &error);
if (!sections)
{
[noteText setStringValue:@"Could not read configuration file."];
}
else
{
NSString *section= nil;
unsigned int i;
[sampleText setString:[NSString stringWithContentsOfFile:@"/etc/my.cnf"]];
[sectionPop removeAllItems];
for (i= 0; i < sections->strings_num; i++)
{
if (strncmp(sections->strings[i], "mysqld", 6)==0)
[sectionPop addItemWithTitle:[NSString stringWithUTF8String:sections->strings[i]]];
}
if ([sectionPop numberOfItems] == 0)
{
section= @"mysqld";
}
else if ([sectionPop numberOfItems] == 1)
{
section= [NSString stringWithUTF8String:sections->strings[0]];
}
else
{
[pickPanel center];
[pickPanel makeKeyAndOrderFront:self];
if ([NSApp runModalForWindow:pickPanel] == NSOKButton)
{
section= [[sectionPop selectedItem] title];
}
else
{
retval= false; // user pressed CANCEL - dont show the panel
}
}
if (section)
[self setupWithSection:section ofFile:file];
else
[noteText setStringValue:@"No file open."];
}
myx_free_stringlist(sections);
return retval;
}
- (IBAction)openFile:(id)sender
{
}
- (void)windowWillClose:(NSNotification *)aNotification
{
[NSApp stopModalWithCode:NSCancelButton];
}
- (IBAction)sectionOK:(id)sender
{
[NSApp stopModalWithCode:NSOKButton];
[pickPanel orderOut:self];
}
+ (NSImage*)icon
{
return [NSImage imageNamed:@"OSX-Icons_37.png"];
}
+ (NSString*)label
{
return @"Options";
}
+ (NSString*)toolTip
{
return @"Configurable Startup Variables of the MySQL Server.";
}
+ (BOOL)needsConnection
{
return NO;
}
- (void)alertDidEnd:(NSAlert *)alert
returnCode:(int)returnCode
contextInfo:(void *)contextInfo
{
[[alert window] orderOut:self];
if (returnCode == NSAlertDefaultReturn)
{
FILE *f= fopen("/etc/my.cnf", "w+");
if (!f && errno == EACCES)
{
MAHelperCommand command= { MAHelperOpenNewMyCnf, NO };
int fd= [self performOpenCommand:command
withRight:"com.mysql.administrator.write_mycnf"];
if (fd < 0)
return;
else
{
f= fdopen(fd, "w+");
command.authorizedCommandId= MAHelperCommitMyCnf;
[self performOpenCommand:command
withRight:"com.mysql.administrator.write_mycnf"];
fclose(f);
}
}
else if (!f)
{
NSRunAlertPanel(nil,
[NSString stringWithFormat:@"Could not create file /etc/my.cnf\n%s", strerror(errno)],
@"OK",nil,nil);
return;
}
else
{
fprintf(f, "[mysqld]\n");
fclose(f);
}
[self openConfigFile:@"/etc/my.cnf"];
}
}
- (id)initWithOwner: (id<MAdministratorProtocol>)owner
{
self= [super initWithNibFile: @"StartupVariables" panelOwner: owner];
if (self)
{
if ([owner isLocal])
{
[bigText setHidden:YES];
[tabView removeTabViewItem:[tabView tabViewItemAtIndex:0]];
[topBox setEnabledRecursive:NO];
_defaultFrame= [[self topView] frame];
if (access("/etc/my.cnf", F_OK)!=0)
{
NSAlert *alert= [NSAlert alertWithMessageText:@"Configuration File not Found"
defaultButton:@"Create"
alternateButton:@"Cancel"
otherButton:nil
informativeTextWithFormat:@"The my.cnf file could not be located at /etc/my.cnf\nClick \"Create\" to initialize a new one or \"Cancel\"."];
[alert beginSheetModalForWindow:[_owner window]
modalDelegate:self
didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
}
else
{
if([self openConfigFile:@"/etc/my.cnf"] == false)
return nil;
}
}
else
{
[tabView setHidden:YES];
[bigText setHidden:NO];
[popup removeAllItems];
[popup setEnabled:NO];
[topBox setEnabledRecursive:NO];
}
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
if (_authRef)
AuthorizationFree(_authRef, kAuthorizationFlagDestroyRights);
[_xmlGUI release];
[super dealloc];
}
@end
|