Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 641 lines (595 sloc) 21.794 kb
d0577d5 Prof Syd Xu first commit
bububa authored
1 //
2 // ConnectionWindowController.m
3 // MongoHub
4 //
5 // Created by Syd on 10-4-25.
6 // Copyright 2010 MusicPeace.ORG. All rights reserved.
7 //
8
9 #import "Configure.h"
10 #import "NSString+Extras.h"
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
11 #import "NSProgressIndicator+Extras.h"
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
12 #import <BWToolkitFramework/BWToolkitFramework.h>
d0577d5 Prof Syd Xu first commit
bububa authored
13 #import "ConnectionWindowController.h"
14 #import "QueryWindowController.h"
15 #import "AddDBController.h";
16 #import "AddCollectionController.h"
29c952e Prof Syd Xu add admin auth support
bububa authored
17 #import "AuthWindowController.h"
903c7a2 Prof Syd Xu add importFromMySQL and exportToMySQL
bububa authored
18 #import "ImportWindowController.h"
19 #import "ExportWindowController.h"
d0577d5 Prof Syd Xu first commit
bububa authored
20 #import "ResultsOutlineViewController.h"
21 #import "DatabasesArrayController.h"
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
22 #import "StatMonitorTableController.h"
d0577d5 Prof Syd Xu first commit
bububa authored
23 #import "Connection.h"
24 #import "Sidebar.h"
25 #import "SidebarNode.h"
26 #import "MongoDB.h"
eeec665 Prof Syd Xu fixed some UI bug, fixed an _id related bug
bububa authored
27 #import "Tunnel.h"
d0577d5 Prof Syd Xu first commit
bububa authored
28
29 @implementation ConnectionWindowController
30
31 @synthesize managedObjectContext;
32 @synthesize databaseArrayController;
33 @synthesize resultsOutlineViewController;
34 @synthesize conn;
35 @synthesize mongoDB;
36 @synthesize sidebar;
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
37 @synthesize loaderIndicator;
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
38 @synthesize monitorButton;
b6c1571 Prof Syd Xu Add reconnect button
bububa authored
39 @synthesize reconnectButton;
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
40 @synthesize monitorSheetController;
41 @synthesize statMonitorTableController;
d0577d5 Prof Syd Xu first commit
bububa authored
42 @synthesize databases;
43 @synthesize collections;
44 @synthesize selectedDB;
45 @synthesize selectedCollection;
46 @synthesize sshTunnel;
47 @synthesize addDBController;
48 @synthesize addCollectionController;
49 @synthesize resultsTitle;
239249e Prof Syd Xu use nsbundle version key
bububa authored
50 @synthesize bundleVersion;
29c952e Prof Syd Xu add admin auth support
bububa authored
51 @synthesize authWindowController;
903c7a2 Prof Syd Xu add importFromMySQL and exportToMySQL
bububa authored
52 @synthesize importWindowController;
53 @synthesize exportWindowController;
d0577d5 Prof Syd Xu first commit
bububa authored
54
eeec665 Prof Syd Xu fixed some UI bug, fixed an _id related bug
bububa authored
55
d0577d5 Prof Syd Xu first commit
bububa authored
56 - (id)init {
57 if (![super initWithWindowNibName:@"ConnectionWindow"]) return nil;
58 return self;
59 }
60
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
61 - (void) tunnelStatusChanged: (Tunnel*) tunnel status: (NSString*) status {
8cc1db9 Prof Syd Xu fixed application crash during open/close connection window.
bububa authored
62 NSLog(@"SSH TUNNEL STATUS: %@", status);
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
63 if( [status isEqualToString: @"CONNECTED"] ){
64 exitThread = YES;
65 [self connect:YES];
66 }
eeec665 Prof Syd Xu fixed some UI bug, fixed an _id related bug
bububa authored
67 }
68
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
69 - (void) connect:(BOOL)haveHostAddress {
70 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
71 [loaderIndicator start];
b6c1571 Prof Syd Xu Add reconnect button
bububa authored
72 [reconnectButton setEnabled:NO];
73 [monitorButton setEnabled:NO];
d67d476 Prof Syd Xu Add replica set connection support.
bububa authored
74 bool connected;
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
75 NSString *hostaddress = [[[NSString alloc] init] autorelease];
76 if (!haveHostAddress && [conn.usessh intValue]==1) {
77 NSString *portForward = [[NSString alloc] initWithFormat:@"L:%@:%@:%@:%@", conn.hostport, conn.host, conn.sshhost, conn.bindport];
78 NSMutableArray *portForwardings = [[NSMutableArray alloc] initWithObjects:portForward, nil];
eeec665 Prof Syd Xu fixed some UI bug, fixed an _id related bug
bububa authored
79 [portForward release];
8cc1db9 Prof Syd Xu fixed application crash during open/close connection window.
bububa authored
80 if (!sshTunnel)
81 sshTunnel =[[Tunnel alloc] init];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
82 [sshTunnel setDelegate:self];
eeec665 Prof Syd Xu fixed some UI bug, fixed an _id related bug
bububa authored
83 [sshTunnel setUser:conn.sshuser];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
84 [sshTunnel setHost:conn.sshhost];
85 [sshTunnel setPassword:conn.sshpassword];
eeec665 Prof Syd Xu fixed some UI bug, fixed an _id related bug
bububa authored
86 [sshTunnel setPort:[conn.sshport intValue]];
87 [sshTunnel setPortForwardings:portForwardings];
88 [sshTunnel setAliveCountMax:3];
89 [sshTunnel setAliveInterval:30];
90 [sshTunnel setTcpKeepAlive:YES];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
91 [sshTunnel setCompression:YES];
8cc1db9 Prof Syd Xu fixed application crash during open/close connection window.
bububa authored
92 //[sshTunnel start];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
93 [portForwardings release];
94 return;
95 }else if (!haveHostAddress && [conn.host isEqualToString:@"flame.mongohq.com"]) {
724aa4a Prof Syd Xu add support for mongohq
bububa authored
96 hostaddress = [NSString stringWithFormat:@"%@:%@/%@", conn.host, conn.hostport, conn.defaultdb];
a360773 Prof Syd Xu fixed several crash bugs;
bububa authored
97 connected = mongoDB = [[MongoDB alloc] initWithConn:hostaddress];
d0577d5 Prof Syd Xu first commit
bububa authored
98 }else {
a360773 Prof Syd Xu fixed several crash bugs;
bububa authored
99 if ([conn.userepl intValue] == 1) {
d67d476 Prof Syd Xu Add replica set connection support.
bububa authored
100 hostaddress = conn.repl_name;
101 NSArray *tmp = [conn.servers componentsSeparatedByString:@","];
102 NSMutableArray *hosts = [[NSMutableArray alloc] initWithCapacity:[tmp count]];
103 for (NSString *h in tmp) {
104 NSString *host = [h stringByTrimmingWhitespace];
105 if ([host length] == 0) {
106 continue;
107 }
108 [hosts addObject:host];
109 }
110 connected = mongoDB = [[MongoDB alloc] initWithConn:conn.repl_name hosts:hosts];
111 [hosts release];
112 }else{
113 hostaddress = [NSString stringWithFormat:@"%@:%@", conn.host, conn.hostport];
114 connected = mongoDB = [[MongoDB alloc] initWithConn:hostaddress];
115 }
724aa4a Prof Syd Xu add support for mongohq
bububa authored
116 }
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
117 [loaderIndicator stop];
d67d476 Prof Syd Xu Add replica set connection support.
bububa authored
118 if (connected) {
119 if ([conn.adminuser isPresent]) {
120 [mongoDB authUser:conn.adminuser pass:conn.adminpass database:conn.defaultdb];
121 }
122
123 if (![conn.defaultdb isPresent]) {
124 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addDB:) name:kNewDBWindowWillClose object:nil];
125 }
126
127 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addCollection:) name:kNewCollectionWindowWillClose object:nil];
b6c1571 Prof Syd Xu Add reconnect button
bububa authored
128 [reconnectButton setEnabled:YES];
d67d476 Prof Syd Xu Add replica set connection support.
bububa authored
129 [monitorButton setEnabled:YES];
130 [self reloadSidebar];
131 [self showServerStatus:nil];
132 }
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
133 [pool release];
134 }
135
136 - (void)windowDidLoad {
137 [super windowDidLoad];
138 exitThread = NO;
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
139 NSString *appVersion = [[NSString alloc] initWithFormat:@"version(%@[%@])", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"], [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey] ];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
140 [bundleVersion setStringValue: appVersion];
141 [appVersion release];
142 [self connect:NO];
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
143 if ([conn.usessh intValue]==1) {
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
144 [NSThread detachNewThreadSelector: @selector(checkTunnel) toTarget:self withObject:nil ];
145 }
146 }
147
b6c1571 Prof Syd Xu Add reconnect button
bububa authored
148 - (IBAction)reconnect:(id)sender
149 {
150 [self connect:NO];
151 if ([conn.usessh intValue]==1) {
152 [NSThread detachNewThreadSelector: @selector(checkTunnel) toTarget:self withObject:nil ];
153 }
154 }
155
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
156 - (void)checkTunnel {
157 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
158 while(!exitThread){
159 @synchronized(self){
8cc1db9 Prof Syd Xu fixed application crash during open/close connection window.
bububa authored
160 if ([sshTunnel running] == NO){
161 [sshTunnel start];
162 }else if( [sshTunnel running] == YES && [sshTunnel checkProcess] == NO ){
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
163 [sshTunnel stop];
164 [NSThread sleepForTimeInterval:2];
165 [sshTunnel start];
8cc1db9 Prof Syd Xu fixed application crash during open/close connection window.
bububa authored
166 }
167 [sshTunnel readStatus];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
168 }
169 [NSThread sleepForTimeInterval:3];
170 }
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
171 [NSThread exit];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
172 [pool release];
d0577d5 Prof Syd Xu first commit
bububa authored
173 }
174
175 - (void)dealloc {
176 [managedObjectContext release];
177 [databaseArrayController release];
178 [resultsOutlineViewController release];
179 [conn release];
180 [mongoDB release];
181 [sidebar release];
182 [databases release];
183 [collections release];
184 [selectedDB release];
185 [selectedCollection release];
186 [sshTunnel release];
187 [addDBController release];
188 [addCollectionController release];
189 [resultsTitle release];
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
190 [loaderIndicator release];
b6c1571 Prof Syd Xu Add reconnect button
bububa authored
191 [reconnectButton release];
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
192 [monitorButton release];
193 [monitorSheetController release];
194 [statMonitorTableController release];
239249e Prof Syd Xu use nsbundle version key
bububa authored
195 [bundleVersion release];
29c952e Prof Syd Xu add admin auth support
bububa authored
196 [authWindowController release];
903c7a2 Prof Syd Xu add importFromMySQL and exportToMySQL
bububa authored
197 [importWindowController release];
198 [exportWindowController release];
d0577d5 Prof Syd Xu first commit
bububa authored
199 [super dealloc];
200 }
201
202 - (void)windowWillClose:(NSNotification *)notification {
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
203 if ([sshTunnel running]) {
204 [sshTunnel stop];
205 }
206 //exitThread = YES;
b6c1571 Prof Syd Xu Add reconnect button
bububa authored
207 resultsOutlineViewController = nil;
d0577d5 Prof Syd Xu first commit
bububa authored
208 selectedDB = nil;
209 selectedCollection = nil;
a360773 Prof Syd Xu fixed several crash bugs;
bububa authored
210 [super release];
d0577d5 Prof Syd Xu first commit
bububa authored
211 }
212
213 - (void)reloadSidebar {
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
214 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
215 [sidebar addSection:@"1" caption:@"DATABASES"];
216 [self reloadDBList];
217 [sidebar reloadData];
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
218 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
219 }
220
221 - (void)reloadDBList {
222 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
223 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
224 //[selectedDB release];
225 selectedDB = nil;
226 //[selectedCollection release];
227 selectedCollection = nil;
228 [collections release];
229 collections = [[NSMutableArray alloc] init];
230 [databases release];
724aa4a Prof Syd Xu add support for mongohq
bububa authored
231 if ([conn.defaultdb isPresent]) {
232 databases = [[NSMutableArray alloc] initWithObjects:conn.defaultdb, nil];
233 }else {
234 databases = [[NSMutableArray alloc ] initWithArray:[mongoDB listDatabases]];
235 }
236
d0577d5 Prof Syd Xu first commit
bububa authored
237 [databaseArrayController clean:conn databases:databases];
238 [sidebar removeItem:@"2"];
239 unsigned int i=1;
240 for (NSString *db in databases) {
241 [sidebar addChild:@"1" key:[NSString stringWithFormat:@"1.%d", i] caption:db icon:[NSImage imageNamed:@"dbicon.png"] action:@selector(useDB:) target:self];
242 i ++ ;
243 }
244 [sidebar reloadData];
245 [sidebar expandItem:@"1"];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
246 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
247 [pool release];
248 }
249
250 - (void)useDB:(id)sender {
251 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
252 NSString *dbname = [[NSString alloc] initWithFormat:@"%@", [sender caption]];
253 if (![[selectedDB caption] isEqualToString:dbname]) {
254 //[selectedDB release];
255 selectedDB = (SidebarNode *)sender;
256 }
257 //[selectedCollection release];
258 selectedCollection = nil;
259 NSString *user=nil;
260 NSString *password=nil;
261 Database *db = [databaseArrayController dbInfo:conn name:dbname];
262 if (db) {
263 user = db.user;
264 password = db.password;
265 }
266 [db release];
267 [collections release];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
268 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
269 collections = [[NSMutableArray alloc] initWithArray:[mongoDB listCollections:dbname user:user password:password]];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
270 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
271 [dbname release];
272
273 [sidebar removeItem:@"2"];
274 [sidebar addSection:@"2" caption:[[selectedDB caption] uppercaseString]];
275 unsigned int i = 1;
276 for (NSString *collection in collections) {
277 [sidebar addChild:@"2" key:[NSString stringWithFormat:@"2.%d", i] caption:collection icon:[NSImage imageNamed:@"collectionicon.png"] action:@selector(useCollection:) target:self];
278 i ++ ;
279 }
280 [sidebar reloadData];
281 [sidebar setBadge:[selectedDB nodeKey] count:[collections count]];
282 [sidebar expandItem:@"2"];
283 [self showDBStats:nil];
284 [pool release];
285 }
286
287 - (void)useCollection:(id)sender
288 {
289 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
290 NSString *collectionname = [[NSString alloc] initWithFormat:@"%@", [sender caption] ];
291 if ([collectionname isPresent]) {
292 //[selectedCollection release];
293 selectedCollection = (SidebarNode *)sender;
294 [self showCollStats:nil];
295 }
296 [collectionname release];
297 [pool release];
298 }
299
300 - (IBAction)showServerStatus:(id)sender
301 {
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
302 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
303 [resultsTitle setStringValue:[NSString stringWithFormat:@"Server %@:%@ stats", conn.host, conn.hostport]];
a360773 Prof Syd Xu fixed several crash bugs;
bububa authored
304 NSArray *serverStats = [[NSArray alloc] initWithArray:[mongoDB serverStatus]];
305 NSMutableArray *results = [[NSMutableArray alloc] initWithArray:serverStats];
306 [serverStats release];
d0577d5 Prof Syd Xu first commit
bububa authored
307 resultsOutlineViewController.results = results;
a360773 Prof Syd Xu fixed several crash bugs;
bububa authored
308 [resultsOutlineViewController.myOutlineView reloadData];
d0577d5 Prof Syd Xu first commit
bububa authored
309 [results release];
4475240 Prof Syd Xu Fix a bug in SSH Tunnel stop
bububa authored
310 [loaderIndicator stop];
7e12a09 Prof Syd Xu ssh tunnel support, fixed a memory leak crash in ConnectionWindowControl...
bububa authored
311
d0577d5 Prof Syd Xu first commit
bububa authored
312 }
313
314 - (IBAction)showDBStats:(id)sender
315 {
316 if (selectedDB==nil) {
317 NSRunAlertPanel(@"Error", @"Please specify a database!", @"OK", nil, nil);
318 return;
319 }
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
320 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
321 [resultsTitle setStringValue:[NSString stringWithFormat:@"Database %@ stats", [selectedDB caption]]];
322 NSString *user=nil;
323 NSString *password=nil;
324 Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
325 if (db) {
326 user = db.user;
327 password = db.password;
328 }
329 [db release];
330 NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[mongoDB dbStats:[selectedDB caption]
331 user:user
332 password:password]];
333 resultsOutlineViewController.results = results;
334 [resultsOutlineViewController.myOutlineView reloadData];
335 [results release];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
336 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
337 //NSLog(@"STATUS: %@", results);
338 }
339
340 - (IBAction)showCollStats:(id)sender
a360773 Prof Syd Xu fixed several crash bugs;
bububa authored
341 {NSLog(@"showCollStats");
d0577d5 Prof Syd Xu first commit
bububa authored
342 if (selectedDB==nil || selectedCollection==nil) {
343 NSRunAlertPanel(@"Error", @"Please specify a collection!", @"OK", nil, nil);
344 return;
345 }
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
346 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
347 [resultsTitle setStringValue:[NSString stringWithFormat:@"Collection %@.%@ stats", [selectedDB caption], [selectedCollection caption]]];
348 NSString *user=nil;
349 NSString *password=nil;
350 Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption] ];
351 if (db) {
352 user = db.user;
353 password = db.password;
354 }
355 [db release];
356 NSMutableArray *results = [[NSMutableArray alloc] initWithArray:[mongoDB collStats:[selectedCollection caption]
357 forDB:[selectedDB caption]
358 user:user
359 password:password] ];
360 //NSLog(@"STATUS: %@", results);
361 resultsOutlineViewController.results = results;
362 [resultsOutlineViewController.myOutlineView reloadData];
363 [results release];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
364 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
365 }
366
367 - (IBAction)createDBorCollection:(id)sender
368 {
369 if (selectedCollection) {
370 [self createCollectionForDB:[selectedDB caption]];
371 }else {
372 [self createDB];
373 }
374 }
375
376 - (void)createCollectionForDB:(NSString *)dbname
377 {
378 if (!addCollectionController)
379 {
380 addCollectionController = [[AddCollectionController alloc] init];
381 }
382 addCollectionController.dbname = dbname;
383 [addCollectionController showWindow:self];
384 }
385
386 - (void)createDB
387 {
724aa4a Prof Syd Xu add support for mongohq
bububa authored
388 if ([conn.defaultdb isPresent]) {
389 NSRunAlertPanel(@"Error", @"Could not create database!", @"OK", nil, nil);
390 return;
391 }
d0577d5 Prof Syd Xu first commit
bububa authored
392 if (!addDBController)
393 {
394 addDBController = [[AddDBController alloc] init];
395 }
29c952e Prof Syd Xu add admin auth support
bububa authored
396 addDBController.managedObjectContext = self.managedObjectContext;
397 addDBController.conn = self.conn;
d0577d5 Prof Syd Xu first commit
bububa authored
398 [addDBController showWindow:self];
399 }
400
401 - (void)addDB:(id)sender
402 {
403 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
404 if (![sender object]) {
405 return;
406 }
407 NSString *dbname = [[NSString alloc] initWithString:[[sender object] objectForKey:@"dbname"]];
408 NSString *user = [[NSString alloc] initWithString:[[sender object] objectForKey:@"user"]];
409 NSString *password = [[NSString alloc] initWithString:[[sender object] objectForKey:@"password"]];
410 [mongoDB dbStats:dbname
411 user:user
412 password:password];
413 [dbname release];
414 [user release];
415 [password release];
416 [self reloadSidebar];
417 [pool release];
418 }
419
420 - (void)addCollection:(id)sender
421 {
422 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
423 if (![sender object]) {
424 return;
425 }
426 NSString *dbname = [[NSString alloc] initWithString:[[sender object] objectForKey:@"dbname"]];
427 NSString *collectionname = [[NSString alloc] initWithString:[[sender object] objectForKey:@"collectionname"]];
428 NSString *user=nil;
429 NSString *password=nil;
430 Database *db = [databaseArrayController dbInfo:conn name:dbname];
431 if (db) {
432 user = db.user;
433 password = db.password;
434 }
435 [db release];
436 [mongoDB createCollection:collectionname
437 forDB:dbname
438 user:user
439 password:password];
440 if ([[selectedDB caption] isEqualToString:dbname]) {
441 [sidebar selectItem:[selectedDB nodeKey]];
442 }
443 [dbname release];
444 [collectionname release];
445 [pool release];
446 }
447
448 - (IBAction)dropDBorCollection:(id)sender
449 {
450 if (selectedCollection) {
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
451 [self dropWarning:[NSString stringWithFormat:@"COLLECTION:%@", [selectedCollection caption]]];
d0577d5 Prof Syd Xu first commit
bububa authored
452 }else {
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
453 [self dropWarning:[NSString stringWithFormat:@"DB:%@", [selectedDB caption]]];
d0577d5 Prof Syd Xu first commit
bububa authored
454 }
455 }
456
457 - (void)dropCollection:(NSString *)collectionname ForDB:(NSString *)dbname
458 {
459 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
460 NSString *user=nil;
461 NSString *password=nil;
462 Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
463 if (db) {
464 user = db.user;
465 password = db.password;
466 }
467 [db release];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
468 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
469 [mongoDB dropCollection:collectionname
470 forDB:dbname
471 user:user
472 password:password];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
473 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
474 if ([[selectedDB caption] isEqualToString:dbname]) {
475 [sidebar selectItem:[selectedDB nodeKey]];
476 }
477 [pool release];
478 }
479
480 - (void)dropDB
481 {
724aa4a Prof Syd Xu add support for mongohq
bububa authored
482 if ([conn.defaultdb isPresent]) {
483 NSRunAlertPanel(@"Error", @"Could not drop database!", @"OK", nil, nil);
484 return;
485 }
d0577d5 Prof Syd Xu first commit
bububa authored
486 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
487 NSString *user=nil;
488 NSString *password=nil;
489 Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
490 if (db) {
491 user = db.user;
492 password = db.password;
493 }
494 [db release];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
495 [loaderIndicator start];
d0577d5 Prof Syd Xu first commit
bububa authored
496 [mongoDB dropDB:[selectedDB caption]
497 user:user
498 password:password];
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
499 [loaderIndicator stop];
d0577d5 Prof Syd Xu first commit
bububa authored
500 [self reloadSidebar];
501 [pool release];
502 }
503
504 - (IBAction)query:(id)sender
505 {
506 if (!selectedCollection) {
507 NSRunAlertPanel(@"Error", @"Please choose a collection!", @"OK", nil, nil);
508 return;
509 }
510
511 QueryWindowController *queryWindowController = [[QueryWindowController alloc] init];
512 queryWindowController.managedObjectContext = self.managedObjectContext;
513 queryWindowController.conn = conn;
514 queryWindowController.dbname = [selectedDB caption];
515 queryWindowController.collectionname = [selectedCollection caption];
516 queryWindowController.mongoDB = mongoDB;
517 [queryWindowController showWindow:sender];
518 }
29c952e Prof Syd Xu add admin auth support
bububa authored
519
520 - (IBAction)showAuth:(id)sender
521 {
724aa4a Prof Syd Xu add support for mongohq
bububa authored
522 if ([conn.defaultdb isPresent]) {
523 NSRunAlertPanel(@"Error", @"Could not auth for database!", @"OK", nil, nil);
524 return;
525 }
526
29c952e Prof Syd Xu add admin auth support
bububa authored
527 if (!selectedDB)
528 {
529 NSRunAlertPanel(@"Error", @"Please choose a database!", @"OK", nil, nil);
530 return;
531 }
532 if (!authWindowController)
533 {
534 authWindowController = [[AuthWindowController alloc] init];
535 }
536 Database *db = [databaseArrayController dbInfo:conn name:[selectedDB caption]];
537 if (db) {
538 [authWindowController.userTextField setStringValue:db.user];
539 [authWindowController.passwordTextField setStringValue:db.password];
540 }else {
541 [authWindowController.userTextField setStringValue:@""];
542 [authWindowController.passwordTextField setStringValue:@""];
543 }
544 authWindowController.managedObjectContext = self.managedObjectContext;
545 authWindowController.conn = self.conn;
546 authWindowController.dbname = [selectedDB caption];
547 [authWindowController showWindow:self];
548 }
549
903c7a2 Prof Syd Xu add importFromMySQL and exportToMySQL
bububa authored
550 - (IBAction)importFromMySQL:(id)sender
551 {
552 if (selectedDB==nil) {
553 NSRunAlertPanel(@"Error", @"Please specify a database!", @"OK", nil, nil);
554 return;
555 }
556 if (!importWindowController)
557 {
558 importWindowController = [[ImportWindowController alloc] init];
559 }
560 importWindowController.managedObjectContext = self.managedObjectContext;
561 importWindowController.conn = self.conn;
562 importWindowController.mongoDB = mongoDB;
563 importWindowController.dbname = [selectedDB caption];
564 [importWindowController showWindow:self];
565 }
566
567 - (IBAction)exportToMySQL:(id)sender
568 {
569 if (selectedDB==nil) {
570 NSRunAlertPanel(@"Error", @"Please specify a database!", @"OK", nil, nil);
571 return;
572 }
573 if (!exportWindowController)
574 {
575 exportWindowController = [[ExportWindowController alloc] init];
576 }
577 exportWindowController.managedObjectContext = self.managedObjectContext;
578 exportWindowController.conn = self.conn;
579 exportWindowController.mongoDB = mongoDB;
580 exportWindowController.dbname = [selectedDB caption];
581 [exportWindowController showWindow:self];
582 }
583
73cdd2c Prof Syd Xu Add Confirm Sheet before drop DB or Collection
bububa authored
584 - (void)alertDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
585 {
586 if (returnCode == NSAlertFirstButtonReturn)
587 {
588 if (selectedCollection) {
589 [self dropCollection:[selectedCollection caption] ForDB:[selectedDB caption]];
590 }else {
591 [self dropDB];
592 }
593 }
594 }
595
596 - (void)dropWarning:(NSString *)msg
597 {
598 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
599 [alert addButtonWithTitle:@"OK"];
600 [alert addButtonWithTitle:@"Cancel"];
601 [alert setMessageText:[NSString stringWithFormat:@"Drop this %@?", msg]];
602 [alert setInformativeText:[NSString stringWithFormat:@"Dropped %@ cannot be restored.", msg]];
603 [alert setAlertStyle:NSWarningAlertStyle];
604 [alert beginSheetModalForWindow:[self window] modalDelegate:self
605 didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
606 contextInfo:nil];
607 }
608
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
609 - (IBAction)startMonitor:(id)sender {
610 monitorStopped = NO;
611 [NSThread detachNewThreadSelector: @selector(updateMonitor) toTarget:self withObject:nil ];
612 [monitorSheetController openSheet:sender];
613 NSLog(@"startMonitor");
614 }
615
616 - (IBAction)stopMonitor:(id)sender {
617 [monitorSheetController closeSheet:sender];
618 monitorStopped = YES;
619 NSLog(@"stopMonitor");
620 }
621
622 - (void)updateMonitor {
623 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
2aaf0b0 Prof Syd Xu more accurate mongo stat time interval
bububa authored
624 NSDate *prev = [NSDate date];
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
625 mongo::BSONObj a = [mongoDB serverStat];
626 while (!monitorStopped) {
627 [NSThread sleepForTimeInterval:1];
2aaf0b0 Prof Syd Xu more accurate mongo stat time interval
bububa authored
628 NSDate *now = [NSDate date];
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
629 mongo::BSONObj b = [mongoDB serverStat];
2aaf0b0 Prof Syd Xu more accurate mongo stat time interval
bububa authored
630 NSDictionary *item = [mongoDB serverMonitor:a second:b currentDate:now previousDate:prev];
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
631 a = b;
2aaf0b0 Prof Syd Xu more accurate mongo stat time interval
bububa authored
632 prev = now;
f0632b4 Prof Syd Xu Add mongo stat monitor
bububa authored
633 [statMonitorTableController addObject:item];
634
635 }
636 [NSThread exit];
637 [pool release];
638 }
639
d0577d5 Prof Syd Xu first commit
bububa authored
640 @end
Something went wrong with that request. Please try again.