aboutsummaryrefslogtreecommitdiffstats
path: root/Source
diff options
context:
space:
mode:
Diffstat (limited to 'Source')
-rw-r--r--Source/SPQueryConsole.h38
-rw-r--r--Source/SPQueryConsole.m135
-rw-r--r--Source/TableContent.h8
-rw-r--r--Source/TableContent.m4
-rw-r--r--Source/TableDocument.h15
-rw-r--r--Source/TableDocument.m130
6 files changed, 226 insertions, 104 deletions
diff --git a/Source/SPQueryConsole.h b/Source/SPQueryConsole.h
new file mode 100644
index 00000000..e1072904
--- /dev/null
+++ b/Source/SPQueryConsole.h
@@ -0,0 +1,38 @@
+//
+// SPQueryConsole.h
+// sequel-pro
+//
+// Created by Stuart Connolly (stuconnolly.com) on Jan 30, 2009
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// More info at <http://code.google.com/p/sequel-pro/>
+
+#import <Cocoa/Cocoa.h>
+
+@interface SPQueryConsole : NSWindowController
+{
+ IBOutlet NSTextView *consoleTextView;
+}
+
+- (IBAction)clearConsole:(id)sender;
+- (IBAction)saveConsoleAs:(id)sender;
+
+- (void)showMessageInConsole:(NSString *)message;
+- (void)showErrorInConsole:(NSString *)error;
+
+- (NSTextView *)consoleTextView;
+
+@end
diff --git a/Source/SPQueryConsole.m b/Source/SPQueryConsole.m
new file mode 100644
index 00000000..34391119
--- /dev/null
+++ b/Source/SPQueryConsole.m
@@ -0,0 +1,135 @@
+//
+// SPQueryConsole.m
+// sequel-pro
+//
+// Created by Stuart Connolly (stuconnolly.com) on Jan 30, 2009
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// More info at <http://code.google.com/p/sequel-pro/>
+
+#import "SPQueryConsole.h"
+
+#define DEFAULT_CONSOLE_LOG_FILENAME @"untitled"
+#define DEFAULT_CONSOLE_LOG_FILE_EXTENSION @"log"
+
+@interface SPQueryConsole (PrivateAPI)
+
+- (void)_appendMessageToConsole:(NSString *)message withColor:(NSColor *)color;
+
+@end
+
+@implementation SPQueryConsole
+
+// -------------------------------------------------------------------------------
+// clearConsole:
+//
+// Clears the console by setting its displayed text to an empty string.
+// -------------------------------------------------------------------------------
+- (IBAction)clearConsole:(id)sender
+{
+ [consoleTextView setString:@""];
+}
+
+// -------------------------------------------------------------------------------
+// saveConsoleAs:
+//
+// Presents the user with a save panel to the save the current console to a log file.
+// -------------------------------------------------------------------------------
+- (IBAction)saveConsoleAs:(id)sender
+{
+ NSSavePanel *panel = [NSSavePanel savePanel];
+
+ [panel setRequiredFileType:DEFAULT_CONSOLE_LOG_FILE_EXTENSION];
+
+ [panel setExtensionHidden:NO];
+ [panel setAllowsOtherFileTypes:YES];
+ [panel setCanSelectHiddenExtension:YES];
+
+ [panel beginSheetForDirectory:nil file:DEFAULT_CONSOLE_LOG_FILENAME modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
+}
+
+// -------------------------------------------------------------------------------
+// showMessageInConsole:
+//
+// Shows the supplied message in the console.
+// -------------------------------------------------------------------------------
+- (void)showMessageInConsole:(NSString *)message
+{
+ [self _appendMessageToConsole:message withColor:[NSColor blackColor]];
+}
+
+// -------------------------------------------------------------------------------
+// showErrorInConsole:
+//
+// Shows the supplied error in the console.
+// -------------------------------------------------------------------------------
+- (void)showErrorInConsole:(NSString *)error
+{
+ [self _appendMessageToConsole:error withColor:[NSColor redColor]];
+}
+
+// -------------------------------------------------------------------------------
+// consoleTextView
+//
+// Return a reference to the console's text view.
+// -------------------------------------------------------------------------------
+- (NSTextView *)consoleTextView
+{
+ return consoleTextView;
+}
+
+// -------------------------------------------------------------------------------
+// savePanelDidEnd:returnCode:contextInfo:
+//
+// Called when the NSSavePanel sheet ends.
+// -------------------------------------------------------------------------------
+- (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
+{
+ if (returnCode == NSOKButton) {
+ [[[consoleTextView textStorage] string] writeToFile:[sheet filename] atomically:YES encoding:NSUTF8StringEncoding error:NULL];
+ }
+}
+
+@end
+
+@implementation SPQueryConsole (PrivateAPI)
+
+// -------------------------------------------------------------------------------
+// _appendMessageToConsole:withColor:
+//
+// Appeds the supplied string to the query console, coloring the text using the
+// supplied color.
+// -------------------------------------------------------------------------------
+- (void)_appendMessageToConsole:(NSString *)message withColor:(NSColor *)color
+{
+ int begin, end;
+
+ // Set the selected range of the text view to be the very last character
+ [consoleTextView setSelectedRange:NSMakeRange([[consoleTextView string] length], 0)];
+ begin = [[consoleTextView string] length];
+
+ // Apped the message to the current text storage using the text view's current typing attributes
+ [[consoleTextView textStorage] appendAttributedString:[[NSAttributedString alloc] initWithString:message attributes:[consoleTextView typingAttributes]]];
+ end = [[consoleTextView string] length];
+
+ // Color the text we just added
+ [consoleTextView setTextColor:color range:NSMakeRange(begin, (end - begin))];
+
+ // Scroll to the text we just added
+ [consoleTextView scrollRangeToVisible:[consoleTextView selectedRange]];
+}
+
+@end
diff --git a/Source/TableContent.h b/Source/TableContent.h
index ac7a8249..e0802e3b 100644
--- a/Source/TableContent.h
+++ b/Source/TableContent.h
@@ -23,19 +23,18 @@
//
// More info at <http://code.google.com/p/sequel-pro/>
-
#import <Cocoa/Cocoa.h>
#import <MCPKit_bundled/MCPKit_bundled.h>
#import "CMCopyTable.h"
#import "CMMCPConnection.h"
#import "CMMCPResult.h"
-
-@interface TableContent : NSObject {
-
+@interface TableContent : NSObject
+{
IBOutlet id tableDocumentInstance;
IBOutlet id tablesListInstance;
IBOutlet id tableDataInstance;
+ IBOutlet id queryConsoleInstance;
IBOutlet id tableWindow;
IBOutlet CMCopyTable *tableContentView;
@@ -68,7 +67,6 @@
NSUserDefaults *prefs;
int numRows;
bool areShowingAllRows;
-
}
//table methods
diff --git a/Source/TableContent.m b/Source/TableContent.m
index 3baf5713..c2db04c2 100644
--- a/Source/TableContent.m
+++ b/Source/TableContent.m
@@ -29,7 +29,7 @@
#import "CMImageView.h"
#import "SPDataCellFormatter.h"
#import "SPTableData.h"
-
+#import "SPQueryConsole.h"
@implementation TableContent
@@ -1183,7 +1183,7 @@
[filteredResult replaceObjectAtIndex:rowIndex withObject:[NSMutableDictionary dictionaryWithDictionary:oldRow]];
isEditingRow = NO;
isEditingNewRow = NO;
- [tableDocumentInstance showErrorInConsole:[NSString stringWithFormat:NSLocalizedString(@"/* WARNING %@ No rows have been affected */\n", @"warning shown in the console when no rows have been affected after writing to the db"), currentTime]];
+ [queryConsoleInstance showErrorInConsole:[NSString stringWithFormat:NSLocalizedString(@"/* WARNING %@ No rows have been affected */\n", @"warning shown in the console when no rows have been affected after writing to the db"), currentTime]];
return YES;
// On success...
diff --git a/Source/TableDocument.h b/Source/TableDocument.h
index 61b770c3..7227f0dd 100644
--- a/Source/TableDocument.h
+++ b/Source/TableDocument.h
@@ -31,10 +31,9 @@
/**
* The TableDocument class controls the primary database view window.
*/
-
@interface TableDocument : NSDocument
{
- //IBOutlets
+ // IBOutlets
IBOutlet id keyChainInstance;
IBOutlet id tablesListInstance;
IBOutlet id tableSourceInstance;
@@ -43,12 +42,12 @@
IBOutlet id tableDumpInstance;
IBOutlet id tableDataInstance;
IBOutlet id tableStatusInstance;
+ IBOutlet id queryConsoleInstance;
IBOutlet id tableWindow;
IBOutlet id connectSheet;
IBOutlet id databaseSheet;
IBOutlet id variablesSheet;
- IBOutlet id consoleDrawer;
IBOutlet id queryProgressBar;
IBOutlet id favoritesButton;
@@ -64,8 +63,9 @@
IBOutlet id connectProgressBar;
IBOutlet id connectProgressStatusText;
IBOutlet id databaseNameField;
+ IBOutlet id databaseEncodingButton;
+ IBOutlet id addDatabaseButton;
IBOutlet id chooseDatabaseButton;
- IBOutlet id consoleTextView;
IBOutlet id variablesTableView;
IBOutlet NSTabView *tableTabView;
@@ -126,13 +126,6 @@
- (IBAction)closeDatabaseSheet:(id)sender;
- (IBAction)removeDatabase:(id)sender;
-//console methods
-- (void)toggleConsole:(id)sender;
-- (void)clearConsole:(id)sender;
-- (BOOL)consoleIsOpened;
-- (void)showMessageInConsole:(NSString *)message;
-- (void)showErrorInConsole:(NSString *)error;
-
//encoding methods
- (void)setConnectionEncoding:(NSString *)mysqlEncoding reloadingViews:(BOOL)reloadViews;
- (NSString *)databaseEncoding;
diff --git a/Source/TableDocument.m b/Source/TableDocument.m
index 6fae3a0b..1e3537b2 100644
--- a/Source/TableDocument.m
+++ b/Source/TableDocument.m
@@ -33,6 +33,7 @@
#import "TableStatus.h"
#import "ImageAndTextCell.h"
#import "SPGrowlController.h"
+#import "SPQueryConsole.h"
#import "SPSQLParser.h"
#import "SPTableData.h"
@@ -503,15 +504,18 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
{
int code = 0;
- if (![tablesListInstance selectionShouldChangeInTableView:nil])
+ if (![tablesListInstance selectionShouldChangeInTableView:nil]) {
return;
+ }
[databaseNameField setStringValue:@""];
+
[NSApp beginSheet:databaseSheet
modalForWindow:tableWindow
modalDelegate:self
didEndSelector:nil
contextInfo:nil];
+
code = [NSApp runModalForWindow:databaseSheet];
[NSApp endSheet:databaseSheet];
@@ -522,12 +526,23 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
return;
}
+ // This check is not necessary anymore as the add database button is now only enabled if the name field
+ // has a length greater than zero. We'll leave it in just in case.
if ([[databaseNameField stringValue] isEqualToString:@""]) {
NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, NSLocalizedString(@"Database must have a name.", @"message of panel when no db name is given"));
return;
}
- [mySQLConnection queryString:[NSString stringWithFormat:@"CREATE DATABASE `%@`", [databaseNameField stringValue]]];
+ NSString *createStatement = [NSString stringWithFormat:@"CREATE DATABASE `%@`", [databaseNameField stringValue]];
+
+ // If there is an encoding selected other than the default we must specify it in CREATE DATABASE statement
+ if ([databaseEncodingButton indexOfSelectedItem] > 0) {
+ createStatement = [NSString stringWithFormat:@"%@ DEFAULT CHARACTER SET `%@`", createStatement, [self mysqlEncodingFromDisplayEncoding:[databaseEncodingButton title]]];
+ }
+
+ // Create the database
+ [mySQLConnection queryString:createStatement];
+
if (![[mySQLConnection getLastErrorMessage] isEqualToString:@""]) {
//error while creating db
NSBeginAlertSheet(NSLocalizedString(@"Error", @"error"), NSLocalizedString(@"OK", @"OK button"), nil, nil, tableWindow, self, nil, nil, nil, [NSString stringWithFormat:NSLocalizedString(@"Couldn't create database.\nMySQL said: %@", @"message of panel when creation of db failed"), [mySQLConnection getLastErrorMessage]]);
@@ -572,74 +587,14 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
NSBeginAlertSheet(NSLocalizedString(@"Warning", @"warning"), NSLocalizedString(@"Delete", @"delete button"), NSLocalizedString(@"Cancel", @"cancel button"), nil, tableWindow, self, nil, @selector(sheetDidEnd:returnCode:contextInfo:), @"removedatabase", [NSString stringWithFormat:NSLocalizedString(@"Do you really want to delete the database %@?", @"message of panel asking for confirmation for deleting db"), [self database]]);
}
-#pragma mark console methods
+#pragma mark Console methods
-//console methods
/**
- * shows or hides the console
+ * Shows or hides the console
*/
- (void)toggleConsole:(id)sender
{
- if ([self consoleIsOpened]) {
- [consoleDrawer close];
- } else {
- [consoleTextView scrollRangeToVisible:[consoleTextView selectedRange]];
- [consoleDrawer openOnEdge:NSMinYEdge];
- }
-}
-
-/**
- * clears the console
- */
-- (void)clearConsole:(id)sender
-{
- [consoleTextView setString:@""];
-}
-
-/**
- * returns YES if the console is visible
- */
-- (BOOL)consoleIsOpened
-{
- return ([consoleDrawer state] == NSDrawerOpeningState || [consoleDrawer state] == NSDrawerOpenState);
-}
-
-/**
- * shows a message in the console
- */
-- (void)showMessageInConsole:(NSString *)message
-{
- int begin, end;
-
- [consoleTextView setSelectedRange:NSMakeRange([[consoleTextView string] length],0)];
- begin = [[consoleTextView string] length];
- [consoleTextView replaceCharactersInRange:NSMakeRange(begin,0) withString:message];
- end = [[consoleTextView string] length];
- [consoleTextView setTextColor:[NSColor blackColor] range:NSMakeRange(begin,end-begin)];
-
- if ([self consoleIsOpened]) {
- [consoleTextView displayIfNeeded];
- [consoleTextView scrollRangeToVisible:[consoleTextView selectedRange]];
- }
-}
-
-/**
- * shows an error in the console (red)
- */
-- (void)showErrorInConsole:(NSString *)error
-{
- int begin, end;
-
- [consoleTextView setSelectedRange:NSMakeRange([[consoleTextView string] length],0)];
- begin = [[consoleTextView string] length];
- [consoleTextView replaceCharactersInRange:NSMakeRange(begin,0) withString:error];
- end = [[consoleTextView string] length];
- [consoleTextView setTextColor:[NSColor redColor] range:NSMakeRange(begin,end-begin)];
-
- if ([self consoleIsOpened]) {
- [consoleTextView displayIfNeeded];
- [consoleTextView scrollRangeToVisible:[consoleTextView selectedRange]];
- }
+ [[queryConsoleInstance window] setIsVisible:![[queryConsoleInstance window] isVisible]];
}
#pragma mark Encoding Methods
@@ -1279,13 +1234,15 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
[toolbarItem setPaletteLabel:NSLocalizedString(@"Show/Hide Console", @"toolbar item for show/hide console")];
//set up tooltip and image
[toolbarItem setToolTip:NSLocalizedString(@"Show or hide the console which shows all MySQL commands performed by Sequel Pro", @"tooltip for toolbar item for show/hide console")];
- if ( [self consoleIsOpened] ) {
+
+ if ([[queryConsoleInstance window] isVisible]) {
[toolbarItem setLabel:NSLocalizedString(@"Hide Console", @"toolbar item for hide console")];
[toolbarItem setImage:[NSImage imageNamed:@"hideconsole"]];
} else {
[toolbarItem setLabel:NSLocalizedString(@"Show Console", @"toolbar item for showconsole")];
[toolbarItem setImage:[NSImage imageNamed:@"showconsole"]];
}
+
//set up the target action
[toolbarItem setTarget:self];
[toolbarItem setAction:@selector(toggleConsole:)];
@@ -1298,7 +1255,7 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
[toolbarItem setToolTip:NSLocalizedString(@"Clear the console which shows all MySQL commands performed by Sequel Pro", @"tooltip for toolbar item for clear console")];
[toolbarItem setImage:[NSImage imageNamed:@"clearconsole"]];
//set up the target action
- [toolbarItem setTarget:self];
+ [toolbarItem setTarget:queryConsoleInstance];
[toolbarItem setAction:@selector(clearConsole:)];
} else if ([itemIdentifier isEqualToString:@"SwitchToTableStructureToolbarItemIdentifier"]) {
@@ -1401,8 +1358,8 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
*/
- (BOOL)validateToolbarItem:(NSToolbarItem *)toolbarItem;
{
- if ( [[toolbarItem itemIdentifier] isEqualToString:@"ToggleConsoleIdentifier"] ) {
- if ( [self consoleIsOpened] ) {
+ if ([[toolbarItem itemIdentifier] isEqualToString:@"ToggleConsoleIdentifier"]) {
+ if ([[queryConsoleInstance window] isVisible]) {
[toolbarItem setLabel:@"Hide Console"];
[toolbarItem setImage:[NSImage imageNamed:@"hideconsole"]];
} else {
@@ -1414,12 +1371,12 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
return YES;
}
+// NSDocument methods
-//NSDocument methods
-- (NSString *)windowNibName
-/*
- returns the name of the nib file
+/**
+ * Returns the name of the nib file
*/
+- (NSString *)windowNibName
{
return @"DBView";
}
@@ -1450,20 +1407,19 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
//set up interface
if ( [prefs boolForKey:@"useMonospacedFonts"] ) {
- [consoleTextView setFont:[NSFont fontWithName:@"Monaco" size:[NSFont smallSystemFontSize]]];
+ [[queryConsoleInstance consoleTextView] setFont:[NSFont fontWithName:@"Monaco" size:[NSFont smallSystemFontSize]]];
[syntaxViewContent setFont:[NSFont fontWithName:@"Monaco" size:[NSFont smallSystemFontSize]]];
while ( (theCol = [theCols nextObject]) ) {
[[theCol dataCell] setFont:[NSFont fontWithName:@"Monaco" size:10]];
}
} else {
- [consoleTextView setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
+ [[queryConsoleInstance consoleTextView] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
[syntaxViewContent setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
while ( (theCol = [theCols nextObject]) ) {
[[theCol dataCell] setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
}
}
- [consoleDrawer setContentSize:NSMakeSize(110,110)];
//set up toolbar
[self setupToolbar];
@@ -1490,29 +1446,28 @@ NSString *TableDocumentFavoritesControllerFavoritesDidChange = @"TableDocumentFa
} else {
return YES;
}
-
}
+#pragma mark SMySQL delegate methods
-//SMySQL delegate methods
+/**
+ * Invoked when framework will perform a query
+ */
- (void)willQueryString:(NSString *)query
-/*
-invoked when framework will perform a query
-*/
{
NSString *currentTime = [[NSDate date] descriptionWithCalendarFormat:@"%H:%M:%S" timeZone:nil locale:nil];
- [self showMessageInConsole:[NSString stringWithFormat:@"/* MySQL %@ */ %@;\n", currentTime, query]];
+ [queryConsoleInstance showMessageInConsole:[NSString stringWithFormat:@"/* MySQL %@ */ %@;\n", currentTime, query]];
}
+/**
+ * Invoked when query gave an error
+ */
- (void)queryGaveError:(NSString *)error
-/*
-invoked when query gave an error
-*/
{
NSString *currentTime = [[NSDate date] descriptionWithCalendarFormat:@"%H:%M:%S" timeZone:nil locale:nil];
- [self showErrorInConsole:[NSString stringWithFormat:@"/* ERROR %@ */ %@;\n", currentTime, error]];
+ [queryConsoleInstance showErrorInConsole:[NSString stringWithFormat:@"/* ERROR %@ */ %@;\n", currentTime, error]];
}
#pragma mark Connection sheet delegate methods
@@ -1529,6 +1484,9 @@ invoked when query gave an error
|| [aNotification object] == socketField || [aNotification object] == portField) {
[favoritesController setSelectionIndexes:[NSIndexSet indexSet]];
}
+ else if ([aNotification object] == databaseNameField) {
+ [addDatabaseButton setEnabled:([[databaseNameField stringValue] length] > 0)];
+ }
}
#pragma mark SplitView delegate methods