File: MAdministrator.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 (607 lines) | stat: -rw-r--r-- 14,162 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//
//  MAdministrator.m
//  MySQL Administrator
//
//  Created by Alfredo Kojima on Thu Jun 24 2004.
//  Copyright (c) 2004 MySQL AB. All rights reserved.
//

#import "MAdministratorController.h"
#import "MAdministrator.h"
#import "MAPanel.h"

#import <MySQLToolsCommon/MConnectionInfo.h>

#include "myx_admin_public_interface.h"

#define DEFAULT_PANEL_WIDTH 800


static NSMutableArray *Panels= nil;
static NSMutableDictionary *PanelsById= nil;

NSString *MASchemaDataSourceChanged=@"MASchemaDataSourceChanged";

BOOL MARegisterPanel(id panelClass)
{
  if (!Panels)
    Panels= [[NSMutableArray alloc] init];
  if (!PanelsById)
    PanelsById= [[NSMutableDictionary alloc] init];
  
  if (![panelClass isSubclassOfClass: [MAPanel class]])
  {
    NSLog(@"ERROR: Not registering invalid panel (%@)", [panelClass className]);
    return NO;
  }
  
  //NSLog(@"registering panel %@", [panelClass className]);
  [Panels addObject: panelClass];
  
  [PanelsById setObject: panelClass forKey: [panelClass className]];
  
  return YES;
}


NSArray *MAGetPanelList()
{
  return Panels;
}



@interface MAdministrator(Private)
- (void)fetchSchemas;
- (void)tablesArrived:(NSArray*)args result:(MYX_SCHEMA_TABLES*)tables;
- (void)requestTablesForSchema:(MSchemaDataSource*)sender item:(MSchemaItem*)item;
@end

@implementation MAdministrator(Private)

- (void)fetchSchemas
{
  MYX_CATALOGS *cats;
  
retry:
  cats= [_dispatcher performCallback:(void*(*)(MYSQL*))myx_get_catalogs
                       waitForWindow:[self window]
                             message:@"Retrieving catalog list..."];
  if (!cats && ![_dispatcher checkConnection] && [_dispatcher reconnect])
    goto retry;

  if (!cats)
  {
    char *tmp;
    NSRunAlertPanel(@"Error",
                    [NSString stringWithFormat:@"Could not retrieve catalogs.\n%s", 
                      tmp= myx_mysql_error(_mysql)],
                    @"OK",nil,nil);
    g_free(tmp);
    NSLog(@"ERROR: Could not retrieve catalog list");
    return;
  }
  
  [_schemaDS updateWithCatalogs:cats];
  [_objectDS updateWithCatalogs:cats];
  
  [[NSNotificationCenter defaultCenter]
 postNotificationName:MASchemaDataSourceChanged
               object:self
             userInfo:[NSDictionary dictionaryWithObject:_objectDS
                                                  forKey:@"ds"]];
  [[NSNotificationCenter defaultCenter]
 postNotificationName:MASchemaDataSourceChanged
               object:self
             userInfo:[NSDictionary dictionaryWithObject:_schemaDS
                                                  forKey:@"ds"]];
  
  if (_catalogs)
    myx_free_catalogs(_catalogs);
  _catalogs= cats;
}


- (void)requestTablesForSchema:(MSchemaDataSource*)sender item:(MSchemaItem*)item
{
  MYX_SCHEMA *schema= [item schema];
  [_dispatcher performCallback:(void*(*)(MYSQL*,void*,void*))myx_get_schema_tables
                      argument:schema->catalog_name
                      argument:schema->schema_name
              finishedSelector:@selector(tablesArrived:result:)
                      argument:[[NSArray arrayWithObjects:sender,item,nil] retain]
                        target:self];
}


- (void)tablesArrived:(NSArray*)args result:(MYX_SCHEMA_TABLES*)tables
{
  MSchemaDataSource *ds= [args objectAtIndex:0];
  MSchemaItem *sitem= [args objectAtIndex:1];
  MYX_SCHEMA *schema= [sitem schema];
  [args autorelease];
  
  if (tables)
  {
    if (schema->schema_tables)
      myx_free_schema_tables(schema->schema_tables);
    schema->schema_tables= tables;
  }
  if (!tables && ![_dispatcher checkConnection] && [_dispatcher reconnect])
  {
    [self requestTablesForSchema:ds item:sitem];
    return;
  }
  if (!tables)
  {
    NSLog(@"could not retrieve tables list");
    return;
  }
  [_objectDS resetSchemaChildren:sitem];
  
  [_objectDS updateSchema:sitem
               withTables:tables];
  
  [[NSNotificationCenter defaultCenter]
 postNotificationName:MASchemaDataSourceChanged
               object:self
             userInfo:[NSDictionary dictionaryWithObject:ds 
                                                  forKey:@"ds"]];
}
@end


@implementation MAdministrator

- (id)init
{  
  NSRect frame;
  
  self = [super initWithWindowNibName:@"Administrator" owner:self];
  if (!self)
    return nil;
  
  [self window];
  if (![self isWindowLoaded])
    NSLog(@"ERROR: Could not load document window");
  
  _panels= [[NSMutableDictionary alloc] init];
  _mysqlLock= [[NSLock alloc] init];
  
  _catalogIcon= [[NSImage imageNamed:@"16x16_Catalog.png"] retain];
  _schemaIcon= [[NSImage imageNamed:@"16x16_Database.png"] retain];
  _tableIcon= [[NSImage imageNamed:@"16x16_Table.png"] retain];
  _columnIcon= [[NSImage imageNamed:@"16x16_Field.png"] retain];
  _keyIcon= [[NSImage imageNamed:@"16x16_KeyColumn.png"] retain];
  
  frame= [[self window] frame];
  if (frame.size.width < DEFAULT_PANEL_WIDTH)
    frame.size.width= DEFAULT_PANEL_WIDTH;
  
  
  _schemaDS= [[MSchemaDataSource alloc] initWithRoot:MCatalogItemType leaf:MSchemaItemType];
  
  _objectDS= [[MSchemaDataSource alloc] initWithRoot:MCatalogItemType leaf:MObjectItemType];
  [_objectDS setTableFetcher:self selector:@selector(requestTablesForSchema:item:)];
  
  
  [[self window] setFrame:frame display:YES animate:NO];
  
  return self;
}


- (id)initWithoutConnection
{
  self = [self init];
  if (!self)
    return nil;
  
  if (self)
  {    
    _connectedInstance= [[NSString stringWithString:@"Not Connected"] retain];
    
    [self setTitleForPanel: nil];
  }
  return self;  
}

- (id)initWithConnection: (MYSQL*)mysql
                    info: (MConnectionInfo*)info
{
  self = [self init];
  if (!self)
    return nil;
  
  if (self)
  {
    int major_version= myx_get_mysql_major_version(mysql);
    
    _mysql= mysql;
    _info= [info retain];
    
    
    if (major_version < 4)
      NSRunAlertPanel(@"Warning", 
                      [NSString stringWithFormat:@"You are connecting to a MySQL server with version %i.%i.x, "
                        "but this application was designed to work with MySQL "
                        "servers 4.x and newer.\n"
                        "Please note that older versions are not supported and "
                        "may not work properly.",
                        major_version, myx_get_mysql_minor_version(mysql)],
                      nil, nil, nil);
    
    
    _dispatcher= [[MMySQLDispatcher alloc] initWithMySQL:mysql
                                                instance:info];
    
    if (mysql->unix_socket)
      _connectedInstance= [[NSString alloc] initWithFormat:@"%@ via socket", [info hostname]];
    else
      _connectedInstance= [[NSString alloc] initWithFormat:@"%@:%i", [info hostname], [info port]];
    
    [self setTitleForPanel: nil];
  }
  return self;
}


- (void)dealloc
{
  [_dispatcher release];
  [_panels release];
  [_connectedInstance release];
  [_mysqlLock release];
  [_catalogIcon release];
  [_schemaIcon release];
  [_tableIcon release];
  [_columnIcon release];
  [_schemaDS release];
  [_objectDS release];
  [_info release];
  
  if (_catalogs)
  {
    myx_free_catalogs(_catalogs);
    _catalogs= 0;
  }
  if (_mysql)
    myx_mysql_close(_mysql);
  
  [super dealloc];
}

- (void)awakeFromNib
{
  NSToolbar *toolbar= [[NSToolbar alloc] initWithIdentifier:@"toolbar"];
  
  [toolbar setDelegate: self];
  [[self window] setToolbar:toolbar];
  
  [toolbar release];
}


- (MMySQLDispatcher*)dispatcher
{
  return _dispatcher;
}


- (void)setCanSwitchPanel:(BOOL)flag
{
  
}

- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar 
     itemForItemIdentifier:(NSString *)itemIdentifier 
 willBeInsertedIntoToolbar:(BOOL)flag
{
  NSToolbarItem *newItem = [[[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier] autorelease];
  id itemClass = [PanelsById objectForKey:itemIdentifier];
  
  [newItem setLabel:[itemClass label]];
  [newItem setPaletteLabel:[itemClass label]];
  [newItem setImage:[itemClass icon]];
  [newItem setToolTip:[itemClass toolTip]];
  [newItem setTarget:self];
  [newItem setAction:@selector(toolbarItemClicked:)];
  //  [newItem setMenuFormRepresentation:[item menuFormRepresentation]];
  
  return newItem;
}

- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
{
  NSMutableArray *array= [NSMutableArray array];
  int i;
  int count= [Panels count];
  //  [array addObject: [[Panels objectAtIndex: 0] className]];
  //  [array addObject: NSToolbarSeparatorItemIdentifier];  
  for (i= 0; i < count; i++)
    [array addObject: [[Panels objectAtIndex: i] className]];
  return array;
}

- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
{
  NSMutableArray *array= [NSMutableArray array];
  int i;
  int count= [Panels count];
  for (i= 0; i < count; i++)
    [array addObject: [[Panels objectAtIndex: i] className]];
  return array;
}

- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
  return [self toolbarAllowedItemIdentifiers:toolbar];
}

- (void)toolbarItemClicked:(id)sender
{
  MAPanel *panel;
  NSString *panelName;
  
  if (sender)
    panelName= [sender itemIdentifier];
  else
    panelName= [[[self window] toolbar] selectedItemIdentifier];
  
  panel= [_panels objectForKey: panelName];
  if (!panel)
  {
    if ([[PanelsById objectForKey: panelName] needsConnection] && ![self isConnected])
    {
      // panel is for when connected only and we're not connected
      panel= [[MAPanel alloc] initWithMessage:@"Not Connected" owner:self];
    }
    else
    {
      // initialize and cache panel if it wasn't yet
      panel= [[[PanelsById objectForKey: panelName] alloc] initWithOwner: self];
      if(panel)
      {
        [_panels setObject: panel forKey: panelName];
        [panel release];
      }
    }
  }
  
  if(panel)
  {
    [self setTitleForPanel: [NSString stringWithFormat:@"Loading %@...", 
      [[PanelsById objectForKey: panelName] label]]];
    [[self window] display];

    [self switchToPanel: panel];
  }
  else
  {
    [[[self window] toolbar] setSelectedItemIdentifier: [_currentPanel className]];  
  }
}

- (void)setTitleForPanel: (NSString*)name
{
  if (name)
    [[self window] setTitle: [NSString stringWithFormat:@"%@ %s %@", 
      name, "", _connectedInstance]];
  else
    [[self window] setTitle: _connectedInstance];
}

- (void)show
{
  NSToolbar *toolbar;
  if (_info)
  {
    MYX_USER_CONNECTION *conn= [_info createUserConnection];
  
    MYX_MACHINE_INFO *mi= myx_get_server_info(conn, _mysql);
    mi= myx_get_server_info(conn, _mysql);
  
    myx_free_user_connection_content(conn);
    g_free(conn);
  }
  toolbar= [[self window] toolbar]; 
  
  [toolbar setSelectedItemIdentifier: [[Panels objectAtIndex:0] className]];
  [self toolbarItemClicked: nil];
  
  [[self window] setFrameAutosaveName: [NSString stringWithFormat:@"administrator.%@",_connectedInstance]];
  
  [[self window] makeKeyAndOrderFront:nil];
}

- (void)close
{
  if (!_closing)
    [[self window] close];
  
  [[MAdministratorController sharedDocumentController] removeInstance:self];
}


- (void)windowWillClose: (NSNotification*)sender
{
  NSEnumerator *enumerator = [_panels keyEnumerator];
  id key;
  
  while ((key= [enumerator nextObject])) 
  {
    [[_panels objectForKey:key] willClose];
  }  
  
  _closing= YES;
  [self close];
}


- (void)switchToPanel: (MAPanel*)panel
{
  NSRect oldFrame;
  NSRect newFrame;
  NSWindow *window= [self window];
  
  if (panel == _currentPanel)
    return;
  
  if (_currentPanel)
  {
    if (![_currentPanel willHide])
      return;
  }
  if (![panel willShow])
    return;
  
  oldFrame= [window frame];
  
  newFrame= [window frameRectForContentRect: [[panel topView] frame]];
  newFrame.size.width= oldFrame.size.width;
  
  newFrame.origin= oldFrame.origin;
  if (newFrame.size.height > oldFrame.size.height)
    newFrame.origin.y+= (oldFrame.size.height - newFrame.size.height);
  
  if (newFrame.size.height < oldFrame.size.height)
    newFrame.size.height= oldFrame.size.height;
  
  if (_currentPanel)
    [[_currentPanel topView] retain];
  [window setContentView:nil];
  
  [window setFrame:newFrame display:YES animate:NO];
  [window setMinSize: [panel defaultFrame].size];
  
  [window setContentView:[panel topView]];
  [[panel topView] setAutoresizingMask:NSViewWidthSizable|NSViewHeightSizable|NSViewMinYMargin];
  
  [self setTitleForPanel: [[PanelsById objectForKey:[[[self window] toolbar] selectedItemIdentifier]] label]];
  
  {
    NSView *sideBox= [panel sideView];
    [drawer setContentView: sideBox];
    if (sideBox)
    {
      [sideBox setFrameSize: [drawer contentSize]];
      [drawer open];
    }
    else
    {
      [drawer close];
    }
  }
  
  if (_currentPanel)
    [_currentPanel didHide];
  [panel didShow];
  
  _currentPanel= panel;
}


- (BOOL)isConnected
{
  if (_mysql)
    return YES;
  return NO;
}

- (BOOL)isLocal
{
  if (_mysql)
    return myx_is_localhost([[_info hostname] UTF8String])!=0;
  else
    return YES;
}

- (int)serverStatus
{
  gchar *output= NULL;
  gint exst;
  
  if (![self isLocal])
  {
    if ([self isConnected])
      return 1;
    
    return -1;
  }  
  if (g_spawn_command_line_sync("/bin/ps ax -ocommand", &output, NULL, &exst, NULL)
      && exst == 0)
  {
    char *ptr= strtok(output, "\n");
    while (ptr != NULL)
    {
      char *a;
      if ((a=strstr(ptr, "mysqld")))
      {
        g_free(output);
        return 1;
      }
      ptr= strtok(NULL, "\n");
    }
  }
  if (output)
    g_free(output);
  return 0;
}

- (void)unlockMySQL
{
  [_mysqlLock unlock];
}

- (MYSQL*)mysql
{
  return _mysql;
}

- (MYSQL*)mysqlLock
{
  [_mysqlLock lock];
  return _mysql;
}

- (MConnectionInfo*)serverInfo
{
  return _info;
}

- (MSchemaDataSource*)sharedSchemaDS
{
  if (!_schemaFetched)
  {
    _schemaFetched= YES;
    [self fetchSchemas];
  }
  return _schemaDS;
}

- (MSchemaDataSource*)sharedObjectDS
{
  if (!_schemaFetched)
  {    
    _schemaFetched= YES;
    [self fetchSchemas];
  }
  return _objectDS;
}

- (void)refreshSchemata
{
  [self fetchSchemas];
}

- (MYX_CATALOGS*)catalogList
{
  return _catalogs;
}

- (NSWindow*)window
{
  return [super window];
}

@end