Simplify code to prep wx rewrite

This commit is contained in:
Timothy Warren 2012-05-29 14:29:20 -04:00
parent 49e531b591
commit 72897157bd
12 changed files with 71 additions and 1737 deletions

View File

@ -51,10 +51,10 @@ function log_fatal()
$fatal = array(E_ERROR, E_PARSE, E_RECOVERABLE_ERROR);
// Log error.
/*if(in_array($error['type'], $fatal))
{*/
if(in_array($error['type'], $fatal))
{
file_put_contents('errors.txt', print_r($error, TRUE), FILE_APPEND);
//}
}
}
register_shutdown_function('OpenSQLManager\log_fatal');
@ -151,6 +151,9 @@ require_once(BASE_DIR . "/db/autoload.php");
// ! App Bootstrap class
// --------------------------------------------------------------------------
// The main Window object
$main = new Main();
/**
* Class for the app itself
*
@ -165,7 +168,7 @@ class OpenSQLManager extends \wxApp {
*/
public function OnInit()
{
$main = new Main();
global $main;
$main->show();
return 0;
@ -178,13 +181,20 @@ class OpenSQLManager extends \wxApp {
*/
public function OnExit()
{
\wxExit();
return 0;
}
}
// Create the main window
// Create the app instance
$app = new OpenSQLManager();
// Create platform information object
$platform = new \wxPlatformInfo();
// Pass fatal exceptions to wxAPP
\wxHandleFatalExceptions(TRUE);
// Start the wx event loop
\wxApp::SetInstance($app);
\wxEntry();

View File

@ -15,6 +15,8 @@
// ! Global Functions
// --------------------------------------------------------------------------
namespace OpenSQLManager;
/**
* Convert an array to an object
*
@ -28,7 +30,7 @@ function array_to_object($array)
return $array;
}
$obj = new StdClass();
$obj = new \StdClass();
foreach($array as $k => &$v)
{
@ -48,17 +50,7 @@ function array_to_object($array)
*/
function alert($message)
{
$dialog = new GTKMessageDialog(
NULL,
Gtk::DIALOG_MODAL,
Gtk::MESSAGE_INFO,
Gtk::BUTTONS_OK,
$message
);
$dialog->set_position(Gtk::WIN_POS_CENTER);
$dialog->run();
$dialog->destroy();
return \wxMessageBox($message, 'Info', wxOK);
}
// --------------------------------------------------------------------------
@ -71,7 +63,7 @@ function alert($message)
*/
function error($message)
{
$dialog = new GTKMessageDialog(
/*$dialog = new GTKMessageDialog(
NULL,
Gtk::DIALOG_MODAL,
Gtk::MESSAGE_ERROR,
@ -81,7 +73,7 @@ function error($message)
$dialog->set_position(Gtk::WIN_POS_CENTER);
$dialog->run();
$dialog->destroy();
$dialog->destroy();*/
}
// --------------------------------------------------------------------------
@ -94,7 +86,7 @@ function error($message)
*/
function confirm($message)
{
$dialog = new GTKMessageDialog(
/*$dialog = new GTKMessageDialog(
NULL,
Gtk::DIALOG_MODAL,
Gtk::MESSAGE_QUESTION,
@ -106,7 +98,7 @@ function confirm($message)
$answer = $dialog->run();
$dialog->destroy();
return ($answer === Gtk::RESPONSE_YES) ? TRUE : FALSE;
return ($answer === Gtk::RESPONSE_YES) ? TRUE : FALSE;*/
}
// --------------------------------------------------------------------------
@ -118,29 +110,21 @@ function confirm($message)
*/
function about()
{
$dlg = new GtkAboutDialog();
/*$dlg = new \wxAboutDialogInfo();
$dlg->set_program_name(PROGRAM_NAME);
$dlg->set_version(VERSION);
$dlg->SetName(PROGRAM_NAME);
$dlg->SetVersion(VERSION);
$dlg->set_copyright("Copyright (c) ".date('Y')." Timothy J. Warren");
$dlg->SetCopyright("Copyright (c) ".date('Y')." Timothy J. Warren");
$dlg->set_website('https://github.com/aviat4ion/OpenSQLManager');
$dlg->set_website_label('Fork on Github');
$dlg->SetWebSite('https://github.com/aviat4ion/OpenSQLManager','Fork on Github');
$dlg->set_license(file_get_contents(BASE_DIR . "/LICENSE"));
$dlg->SetLicense(file_get_contents(BASE_DIR . "/LICENSE"));
$dlg->set_authors(array(
$dlg->SetDevelopers(array(
'Timothy J. Warren',
//'Nathan Dupuie',
));
/*$dlg->set_artists(array(
'Nathan Dupuie',
));*/
$dlg->run();
$dlg->destroy();
\wxGenericAboutBox($dlg);*/
}
// End of functions.php

View File

@ -13,13 +13,15 @@
// --------------------------------------------------------------------------
namespace OpenSQLManager;
/**
* Widget managing saved database connections
*
* @package OpenSQLManager
* @subpackage Widgets
*/
class Connection_Sidebar extends GtkVBox {
class Connection_Sidebar extends \wxWindow {
/**
* Reference to Settings instance
@ -81,29 +83,7 @@ class Connection_Sidebar extends GtkVBox {
{
parent::__construct();
$this->settings =& Settings::get_instance();
$add_button = new GtkButton();
$add_button->set_label("New Connnection");
$add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_ADD, Gtk::ICON_SIZE_SMALL_TOOLBAR));
$add_button->connect_simple('clicked', array($this, 'new_conn'));
// Treeview to show database connections
{
// Render the treeview
$this->_render();
// Set up context menu event
$this->treeview->connect('button-press-event', array($this, 'on_button'));
$selection = $this->treeview->get_selection();
$selection->set_mode(GTK::SELECTION_SINGLE);
}
$this->pack_start($this->treeview);
$this->pack_start($add_button, FALSE);
$this->settings =& \Settings::get_instance();
}
// --------------------------------------------------------------------------
@ -115,348 +95,8 @@ class Connection_Sidebar extends GtkVBox {
*/
protected function _render()
{
// Create the treeview
$this->treeview = (isset($this->treeview))
? $this->treeview
: new Data_Grid(new GTKTreeStore(Gobject::TYPE_PHP_VALUE));
$model = $this->treeview->get_model();
// Add the existing connections to the model
$db_conns = $this->settings->get_dbs();
if( ! empty($db_conns))
{
foreach($db_conns as $name => &$props)
{
if (is_array($props))
{
$props = array_to_object($props);
}
$db = $props;
$db->name = $name;
$iter = $model->append();
$model->set($iter, 0, $db);
}
}
// Icon column
$cell_renderer = new GtkCellRendererPixbuf();
$this->treeview->insert_column_with_data_func(0, 'Type', $cell_renderer, array($this, 'set_icon'));
// Label column
$cell_renderer = new GtkCellRendererText();
$this->treeview->insert_column_with_data_func(1, 'Name', $cell_renderer, array($this, 'set_label'));
// Status column
$cell_renderer = new GtkCellRendererPixbuf();
$this->treeview->insert_column_with_data_func(2, 'Status', $cell_renderer, array($this, 'set_status_icon'));
// Connect event to change database tabs
$this->treeview->connect('cursor-changed', array($this, 'switch_tab'));
// Connect event to connect on double-click
$this->treeview->connect('row-activated', array($this, 'db_connect'));
}
// --------------------------------------------------------------------------
/**
* Sets the icon for the current db type
*
* @param GtkTreeView Column $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @return void
*/
public function set_icon($col, $cell, $model, $iter)
{
$col->set_reorderable(TRUE);
$info = $model->get_value($iter, 0);
$db_type = strtolower($info->type);
$img_file = BASE_DIR."/images/{$db_type}-logo-32.png";
if(is_file($img_file))
{
$cell->set_property('pixbuf', GdkPixbuf::new_from_file($img_file));
}
else
{
// Load an empty image if the db image doesn't exist
$img = new GtkImage();
$cell->set_property('pixbuf', $img->get_pixbuf());
}
}
// --------------------------------------------------------------------------
/**
* Sets the label of the current db connection
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @return void
*/
public function set_label($col, $cell, $model, $iter)
{
$col->set_reorderable(TRUE);
$info = $model->get_value($iter, 0);
$cell->set_property('text', $info->name);
}
// --------------------------------------------------------------------------
/**
* Sets the status icon of the current db connection
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @return void
*/
public function set_status_icon($col, $cell, $model, $iter)
{
$col->set_reorderable(TRUE);
$info = $model->get_value($iter, 0);
$conns = DB_Reg::get_connections();
if(in_array($info->name, $conns))
{
$cell->set_property('stock-id', 'gtk-yes');
}
else
{
$cell->set_property('stock-id', 'gtk-no');
}
}
// --------------------------------------------------------------------------
/**
* Returns window for creating a new database connection
*
* @return Add_DB object
*/
public function new_conn()
{
return new Add_DB();
}
// --------------------------------------------------------------------------
/**
* Event for mouse clicks on connection sidebar
*
* @param GtkTreeView $view
* @param $event
* @return void
*/
public function on_button($view, $event)
{
if ($event->button !== 3 || empty($view))
{
return;
}
// Right click
if ($event->button == 3)
{
// get the row and column
$path_array = $view->get_path_at_pos($event->x, $event->y);
$col = $path_array[1];
// Don't try to get values for an item that doesn't exist. Instead, return,
// so that the program doesn't crash because someone thought it funny
// to click on the empty area of the treeview.
if(empty($col))
{
return;
}
}
$this->menu = $this->conn_popup_menu($path_array);
}
// --------------------------------------------------------------------------
/**
* Creates and displays a context menu for the selected connection
*
* @param array $all
* @return void
*/
public function conn_popup_menu($all)
{
$this->menu = new GtkMenu();
$data = $this->treeview->get(0);
$conns = DB_Reg::get_connections();
// Don't try to set up popup menu
// on ambiguous areas
if ( ! is_object($data))
{
return;
}
// Set up menu items
{
// Show disconnect
if (in_array($data->name, $conns))
{
$connect = new GtkImageMenuItem('Disconnect');
$connect->set_image(GtkImage::new_from_stock(GTK::STOCK_DISCONNECT, GTK::ICON_SIZE_MENU));
$connect->connect_simple('activate', array($this, 'db_disconnect'));
}
else
{
$connect = new GtkImageMenuItem('Connect');
$connect->set_image(GtkImage::new_from_stock(GTK::STOCK_CONNECT, GTK::ICON_SIZE_MENU));
$connect->connect_simple('activate', array($this, 'db_connect'));
}
$this->menu->append($connect);
$edit = new GtkImageMenuItem('Edit Connection');
$edit->set_image(GtkImage::new_from_stock(GTK::STOCK_EDIT, GTK::ICON_SIZE_MENU));
$edit->connect_simple('activate', array($this, 'edit_connection'));
$this->menu->append($edit);
$remove = new GtkImageMenuItem('Delete Connection');
$remove->set_image(GtkImage::new_from_stock(GTK::STOCK_CANCEL, Gtk::ICON_SIZE_MENU));
$remove->connect_simple('activate', array($this, 'remove_connection'), $all);
$this->menu->append($remove);
}
// Popup the menu
$this->menu->show_all();
$this->menu->popup();
}
// --------------------------------------------------------------------------
/**
* Recreate sidebar widget to update connections
*
* @return void
*/
public function refresh()
{
$this->treeview->reset();
$this->_render();
}
// --------------------------------------------------------------------------
/**
* Update the connection information for an existing connection
*
* @return void
*/
public function edit_connection()
{
return new Edit_Db($this->treeview->get(0));
}
// --------------------------------------------------------------------------
/**
* Remove a connection from the connection manager
*
* @return void
*/
public function remove_connection()
{
if ( ! confirm("Are you sure you want to remove this database connection?"))
{
return;
}
// Get the data from the model for the current selection
$data = $this->treeview->get(0);
// Remove the connection from the settings
$this->settings->remove_db($data->name);
// Refresh the sidebar
$this->refresh();
}
// --------------------------------------------------------------------------
/**
* Create connection to a database
*
* @return void
*/
public function db_connect()
{
$data = $this->treeview->get(0);
// Make sure to catch connection exceptions
try
{
$conn =& DB_Reg::get_db($data->name);
$this->conn_name = $data->name;
}
catch(PDOException $e)
{
error("Could not connect to database:\n". $e->getMessage());
return;
}
DB_Tabs::get_db_tabs($conn);
}
// --------------------------------------------------------------------------
/**
* Disconnect from a database
*
* @return void
*/
public function db_disconnect()
{
$data = $this->treeview->get(0);
DB_Reg::remove_db($data->name);
DB_Tabs::reset($data->name);
$this->refresh();
}
// --------------------------------------------------------------------------
/**
* Change tabs based on db connection selected
*
* @param GtkTreeView $view
* @return void
*/
public function switch_tab($view)
{
$data = $view->get(0);
$conns = DB_Reg::get_connections();
// Don't reset if you are over the same database
if ($data->name === $this->conn_name)
{
return;
}
if (in_array($data->name, $conns))
{
$this->db_connect();
}
}
}
// End of connection_sidebar.php

View File

@ -13,18 +13,20 @@
// --------------------------------------------------------------------------
namespace OpenSQLManager;
/**
* Class to simplify dealing with GtkTreeView
*
* @package OpenSQLManager
* @subpackage Widgets
*/
class Data_Grid extends GtkTreeView {
class Data_Grid extends \wxGrid {
/**
* GtkTreeStore object
*
* @var GtkTreeStore
* @var wxGridTableBase
*/
protected $model;
@ -45,137 +47,5 @@ class Data_Grid extends GtkTreeView {
parent::__construct();
}
}
// --------------------------------------------------------------------------
/**
* Get the value of the model for the current selection
*
* @param int pos
* @return mixed
*/
public function get($pos = 0)
{
if ( ! is_numeric($pos))
{
return parent::get($pos);
}
// Get the selection object of the row
$sel = $this->get_selection();
// Get the model and iterator for the selected row
list($model, $iter) = $sel->get_selected();
// Return on lack of $iter
if (is_null($iter))
{
return;
}
// Get the data from the model
return $model->get_value($iter, $pos);
}
// --------------------------------------------------------------------------
/**
* Clear datagrid
*
* @return void
*/
public function reset()
{
$this->model->clear();
$cols = $this->get_columns();
foreach($cols as &$c)
{
$this->remove_column($c);
}
}
// --------------------------------------------------------------------------
/**
* Adds a column of data to the model
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
* @param int $i
* @return void
*/
public function add_data_col($col, $cell, $model, $iter, $i=0)
{
$data = $model->get_value($iter, $i);
if (empty($data))
{
return;
}
$col->set_visible(TRUE);
$cell->set_property('text', $data);
}
// --------------------------------------------------------------------------
/**
* Create a datagrid from the provided data array
*
* @param array $data
* @param array $events
* @return void
*/
public function render_data($data, $events=array())
{
if ( ! is_array($data))
{
return;
}
$model = new StdClass();
$cols = ( ! empty($data)) ? array_keys($data[0]) : array();
// Add columns to model
$col_num = (count($cols) > 0) ? count($cols) : 1;
$model_args = array_fill(0, $col_num, Gobject::TYPE_PHP_VALUE);
$eval_string = '$model = new GTKTreeStore('.implode(',', $model_args).');';
// Shame, shame, but how else?
eval($eval_string);
$this->set_model($model);
$this->set_headers_clickable(TRUE);
// Set the data in the model
for($i=0, $c = count($data); $i < $c; $i++)
{
// Add a row
$row = $model->insert($i);
$j = -1;
$vals = array($row);
foreach($data[$i] as &$v)
{
$vals[] = ++$j;
$vals[] = trim($v);
}
call_user_func_array(array($model, 'set'), $vals);
}
// Add columns to view
foreach($cols as $i => &$c)
{
$renderer = new GtkCellRendererText();
$renderer->set_property('editable', TRUE);
$this->insert_column_with_data_func($i, $c, $renderer, array($this, 'add_data_col'), $i);
}
}
}
// End of data_grid.php

View File

@ -1,636 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Widget for adding / editing database connections
*
* @package OpenSQLManager
* @subpackage Widgets
*/
class DB_Info_Widget extends GtkTable {
/**
* Alias to Settings::get_instance
*
* @var Settings
*/
private $settings;
/**
* Connection name
*
* @var GtkEntry
*/
protected $conn;
/**
* Connection database name
*
* @var GtkEntry
*/
protected $conn_db;
/**
* Connection database type
*
* @var GtkComboBox
*/
protected $dbtype;
/**
* Connection database host
*
* @var GtkEntry
*/
protected $host;
/**
* Connection user name
*
* @var GtkEntry
*/
protected $user;
/**
* Connection password
*
* @var GtkEntry
*/
protected $pass;
/**
* Connection database file
*
* @var GtkFileChooserButton
*/
protected $db_file;
/**
* Connection port
*
* @var GtkEntry
*/
protected $port;
/**
* Reference to last connection name
*
* @var string
*/
protected $old_conn;
/**
* Label for connection name
*
* @var GtkLabel
*/
protected $lblconn;
/**
* Label for connection database name
*
* @var Gtklabel
*/
protected $lblconn_db;
/**
* Label for database type
*
* @var Gtklabel
*/
protected $lbldbtype;
/**
* Label for database host
*
* @var Gtklabel
*/
protected $lblhost;
/**
* Label for database connection user
*
* @var GtkLabel
*/
protected $lbluser;
/**
* Label for database connection password
*
* @var GtkLabel
*/
protected $lblpass;
/**
* Label for database file
*
* @var GtkLabel
*/
protected $lbldb_file;
/**
* Label for database connection port
*
* @var GtkLabel
*/
protected $lblport;
// --------------------------------------------------------------------------
// ! Start of methods
// --------------------------------------------------------------------------
/**
* No params = add, params = edit
*
* @param object $db
* @return void
*/
public function __construct($db=null)
{
parent::__construct();
$this->settings =& Settings::get_instance();
if (is_null($db))
{
$db = new StdClass();
$db->name = '';
$db->host = '';
$db->user = '';
$db->pass = '';
$db->port = '';
$db->conn_db = '';
$db->type = '';
$db->file = NULL;
}
// Set up the form elements, with default values
$this->conn = new GtkEntry();
$this->host = new GtkEntry();
$this->user = new GtkEntry();
$this->pass = new GtkEntry();
$this->port = new GtkEntry();
$this->conn_db = new GtkEntry();
$this->dbtype = GtkComboBox::new_text();
$this->db_file = new GtkFileChooserButton("Select a database file",
Gtk::FILE_CHOOSER_ACTION_OPEN);
// Populate the available database types
$db_types = $this->get_available_dbs();
foreach($db_types as &$t)
{
$this->dbtype->append_text($t);
}
$lower_db_types = array_map('strtolower', $db_types);
// Populate the text fields with default values
$this->conn->set_text($db->name);
$this->host->set_text($db->host);
$this->user->set_text($db->user);
$this->pass->set_text($db->pass);
$this->conn_db->set_text($db->conn_db);
$this->port->set_text($db->port);
// Layout the table
$this->_layout();
// Select the proper db type if editing
if ( ! empty($db->type))
{
// Set the old conn variable for editing
$this->old_conn = $db->name;
$dbtype = strtolower($db->type);
// Set the db type based on the current connection
$this->dbtype->set_active(array_search($dbtype, $lower_db_types));
// Set default path
if ( ! empty($db->file))
{
$this->db_file->set_filename($db->file);
}
// Re-populate the text fields with their actual values
// This seems to work around a PHP-GTK bug...it SHOULD work
// to set them the first time...
$this->conn->set_text($db->name);
$this->host->set_text($db->host);
$this->user->set_text($db->user);
$this->pass->set_text($db->pass);
$this->conn_db->set_text($db->conn_db);
$this->port->set_text($db->port);
}
}
// --------------------------------------------------------------------------
/**
* Set defaults for new database type
*
* @return void
*/
public function change_db()
{
$new_db = $this->dbtype->get_active_text();
// Reset
$this->host->set_text('127.0.0.1');
$this->db_file->set_filename(NULL);
$this->port->show();
$this->lblport->show();
$this->db_file->hide();
$this->lbldb_file->hide();
$this->host->show();
$this->lblhost->show();
$this->user->set_text('');
$this->pass->set_text('');
$this->port->set_text('');
$this->conn_db->set_text('');
$this->conn_db->show();
$this->lblconn_db->show();
switch($new_db)
{
default:
break;
case "MySQL":
$this->user->set_text('root');
$this->port->set_text(3306);
break;
case "PostgreSQL":
$this->user->set_text('postgres');
$this->port->set_text(5432);
break;
case "Firebird":
$this->user->set_text('sysdba');
$this->pass->set_text('masterkey');
$this->lbldb_file->show();
$this->db_file->show();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
case "ODBC":
$this->lbldb_file->show();
$this->db_file->show();
break;
case "SQLite":
$this->lbldb_file->show();
$this->db_file->show();
$this->port->hide();
$this->lblport->hide();
$this->host->hide();
$this->lblhost->hide();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
}
}
// --------------------------------------------------------------------------
/**
* Like change_db function, but save current values
*
* @return void
*/
public function set_db()
{
$dbtype = strtolower($this->dbtype->get_active_text());
// Reset
$this->db_file->hide();
$this->lbldb_file->hide();
switch($dbtype)
{
default:
break;
case "firebird":
$this->lbldb_file->show();
$this->db_file->show();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
case "odbc":
$this->lbldb_file->show();
$this->db_file->show();
break;
case "sqlite":
$this->lbldb_file->show();
$this->db_file->show();
$this->port->hide();
$this->lblport->hide();
$this->host->hide();
$this->lblhost->hide();
$this->conn_db->hide();
$this->lblconn_db->hide();
break;
}
}
// --------------------------------------------------------------------------
/**
* Adds the database to the settings file
*
* @return void
*/
public function db_add()
{
$data = array(
'type' => strtolower($this->dbtype->get_active_text()),
'host' => $this->host->get_text(),
'user' => $this->user->get_text(),
'pass' => $this->pass->get_text(),
'port' => $this->port->get_text(),
'file' => $this->db_file->get_filename(),
'conn_db' => $this->conn_db->get_text(),
'name' => $this->conn->get_text(),
);
$res = $this->settings->add_db($data['name'], $data);
if ($res === FALSE)
{
error("Failed to add database - Connection information invalid");
}
// Pass to connection sidebar to update
Connection_Sidebar::get_instance()->refresh();
// Destroy the parent window
$parent_window = $this->get_parent_window();
$parent_window->destroy();
}
// --------------------------------------------------------------------------
/**
* Edit an existing database connection
*
* @return void
*/
public function db_edit()
{
$data = array(
'type' => strtolower($this->dbtype->get_active_text()),
'host' => $this->host->get_text(),
'user' => $this->user->get_text(),
'pass' => $this->pass->get_text(),
'port' => $this->port->get_text(),
'file' => $this->db_file->get_filename(),
'conn_db' => $this->conn_db->get_text(),
'name' => $this->conn->get_text(),
);
if ($this->settings->edit_db($this->old_conn, $data))
{
// Let the user know the connection has been updated
alert("Changes to database connection have been saved");
}
else
{
error("Error saving changes");
}
// Pass to connection sidebar to update
Connection_Sidebar::get_instance()->refresh();
// Destroy the parent window
$parent_window = $this->get_parent_window();
$parent_window->destroy();
}
// --------------------------------------------------------------------------
/**
* Test a db connection, and display a popup with the result of the test
*
* @return void
*/
public function test_conn()
{
$params = new stdClass();
$params->type = strtolower($this->dbtype->get_active_text());
$params->host = $this->host->get_text();
$params->user = $this->user->get_text();
$params->pass = $this->pass->get_text();
$params->port = $this->port->get_text();
$params->file = $this->db_file->get_filename();
$params->conn_db = $this->conn_db->get_text();
// Return early if a db type isn't selected.
// Better to bail out then crash because of
// silly user input.
if( empty($params->type))
{
error("Failed to connect - Invalid connection settings");
return;
}
// Catch connection exceptions, and
// display the error message to the
// user so they can edit the db
// parameters
try
{
new Query_Builder($params);
}
catch (PDOException $e)
{
error("Error connecting to database: \n\n" . $e->getMessage());
return;
}
// Successful Connection?
// Tell the user!
alert("Successfully Connected.");
}
// --------------------------------------------------------------------------
/**
* Checks what database drivers are available
*
* @return array
*/
public function get_available_dbs()
{
$drivers = array();
// Check if there is pdo support
if( ! function_exists('pdo_drivers'))
{
return FALSE;
}
$pdo_drivers = pdo_drivers();
// Add PDO drivers
foreach($pdo_drivers as &$d)
{
// Skip sqlite2 as opposed to sqlite3
if($d === 'sqlite2' && (in_array('sqlite', $pdo_drivers) || in_array('sqlite3', $pdo_drivers)))
{
continue;
}
// Use the ibase_functions over PDO::Firebird, at least for now
if($d === 'firebird')
{
continue;
}
// Replace default capitalization with something that looks better.
$d = str_replace("sql", "SQL", $d);
$d = str_ireplace("pg", "Postgre", $d);
$d = str_ireplace("odbc", "ODBC", $d);
$d = ucfirst($d);
$drivers[] = $d;
}
// Add firebird support, if exists
if(function_exists('fbird_connect') && ! in_array('firebird', $pdo_drivers))
{
$drivers[] = "Firebird";
}
sort($drivers);
return $drivers;
}
// --------------------------------------------------------------------------
/**
* Simple helper function for adding a row to the GtkTable
*
* @param string $label
* @param string $vname
* @param int &$y1
* @param int &$y2
* @return void
*/
private function _add_row($label, $vname, &$y1, &$y2)
{
$lbl = 'lbl'.$vname;
$this->$lbl = new GtkLabel($label);
$lblalign = new GtkAlignment(0, 0.5, 0, 0);
$lblalign->add($this->$lbl);
$vname =& $this->$vname;
$this->attach($lblalign, 0, 1, ++$y1, ++$y2);
$this->attach($vname, 1, 2, $y1, $y2);
}
// --------------------------------------------------------------------------
/**
* Table layout
*
* @return void
*/
private function _layout()
{
// Reset defaults when changing db types
$this->dbtype->connect_simple("changed", array($this, "change_db"));
//Table attach
//$tbl->attach(left_start, right_stop, top_start, bottom_stop)
// Placeholder vars for y values, so that rows can be
// easily moved
$y1 = -1;
$y2 = 0;
// Connection name
$this->_add_row("Connection name", 'conn', $y1, $y2);
// Database type
$dbtypelbl = new GtkLabel("Database Type");
$typealign = new GtkAlignment(0, 0.5, 0, 0);
$typealign->add($dbtypelbl);
$this->attach($typealign, 0, 1, ++$y1, ++$y2);
$this->attach($this->dbtype, 1, 2, $y1, $y2);
// DB File
$this->_add_row("Database File", 'db_file', $y1, $y2);
// First Db
$this->_add_row("Database Name", 'conn_db', $y1, $y2);
// Host
$this->_add_row("Host", 'host', $y1, $y2);
// Port
$this->_add_row("Port", 'port', $y1, $y2);
// Username
$this->_add_row("User", 'user', $y1, $y2);
// Password
$this->_add_row("Password", 'pass', $y1, $y2);
// Add/Edit connection button
{
$conn_name = $this->conn->get_text();
$caption = (empty($conn_name)) ? 'Add Connection' : 'Update Connection';
$add_button = new GtkButton();
$add_button->set_label($caption);
( ! empty($conn_name))
? $add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_SAVE,
GTK::ICON_SIZE_SMALL_TOOLBAR))
: $add_button->set_image(GTKImage::new_from_stock(GTK::STOCK_ADD,
Gtk::ICON_SIZE_SMALL_TOOLBAR));
$this->attach($add_button, 0, 1, ++$y1, ++$y2);
if ( ! empty($conn_name))
{
$add_button->connect_simple("clicked", array($this, 'db_edit'));
}
else
{
$add_button->connect_simple("clicked", array($this, 'db_add'));
}
}
// Test connection button
{
$test_button = new GtkButton();
$test_button->set_label("Test Connection");
$this->attach($test_button, 1, 2, $y1, $y2);
$test_button->connect_simple("clicked", array($this, 'test_conn'));
}
}
}
// End of db_info_widget.php

View File

@ -1,33 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Class for generating db-structure editing views
*
* @package OpenSQLManager
* @subpackage Widgets
*/
class DB_Structure_Widget extends GTKTable {
/**
* Initialize the class
*/
public function __construct()
{
parent::__construct();
}
}
// End of db_structure_widget.php

View File

@ -13,13 +13,15 @@
// --------------------------------------------------------------------------
namespace OpenSQLManager;
/**
* Tabbed Container for database properties
*
* @package OpenSQLManager
* @subpackage Widgets
*/
class DB_tabs extends GtkNotebook {
class DB_tabs extends \wxNotebook {
/**
* Current Tab Widget object
@ -77,249 +79,7 @@ class DB_tabs extends GtkNotebook {
$widget = new Data_Grid();
}
$this->append_page($widget, new GtkLabel($label));
}
// --------------------------------------------------------------------------
/**
* Create popup window with table data
*
* @param GTKTreeView $view
* @param array $path
* @param GtkTreeviewColumn $col
* @param Query_Builder $conn
* @return void
*/
public function show_table_data($view, $path, $col, &$conn)
{
$table = $view->get(0);
$query = $conn->get($table);
$data = $query->fetchAll(PDO::FETCH_ASSOC);
return new DB_Table_data($data);
}
// --------------------------------------------------------------------------
/**
* Create tabs for database aspects
*
* @param Query_Builder $conn
* @return void
*/
public static function get_db_tabs(&$conn)
{
// Empty the tabs
self::reset();
self::$instance->hide_all();
// 'Databases' Tab
self::_add_tab($conn, 'Databases', 'Db Name', 'get_dbs');
// 'Schemas' Tab
self::_add_tab($conn, 'Schemas', 'Schema Name', 'get_schemas');
// 'Tables' Tab
self::_add_tab($conn, 'Tables', 'Table Name', 'get_tables', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
// 'System Tables' Tab
self::_add_tab($conn, 'System Tables', 'Table Name', 'get_system_tables', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
// 'Views' Tab
self::_add_tab($conn, 'Views', 'View Name', 'get_views', array(
array(
'row-activated',
array(self::$instance, 'show_table_data'),
$conn
)
));
// 'Sequences' Tab
self::_add_tab($conn, 'Sequences', 'Sequence Name', 'get_sequences');
// 'Triggers' Tab
self::_add_row_tab($conn, 'Triggers','get_triggers');
// 'Procedures' Tab
self::_add_row_tab($conn, 'Procedures', 'get_procedures');
// 'Functions' Tab
self::_add_row_tab($conn, 'Functions', 'get_functions');
// Show the tabs
self::$instance->show_all();
}
// --------------------------------------------------------------------------
/**
* Remove current tabs
*
* @param string $conn_name
* @return void
*/
public static function reset($conn_name = '')
{
self::$instance->hide_all();
for($i=self::$instance->get_n_pages(); $i >= 0; $i--)
{
self::$instance->remove_page($i);
}
if ( ! empty($conn_name))
{
unset(self::$instance->data->{$conn_name});
}
self::$instance->show_all();
}
// --------------------------------------------------------------------------
/**
* Simplify adding tabs to the Notebook object
*
* @param object $conn
* @param string $tab_name
* @param string $col_name
* @param string $method
* @param array $events
* @return void
*/
private static function _add_tab(&$conn, $tab_name, $col_name, $method, $events=array())
{
$tab = new Data_Grid(new GTKTreeStore(Gobject::TYPE_PHP_VALUE));
$tab_model = $tab->get_model();
$conn_name = $conn->conn_name;
$instance_data = self::$instance->_get_db_info($conn_name, $tab_name);
$tab_data = ($instance_data === FALSE)
? call_user_func_array(array($conn, $method), array())
: $instance_data;
self::$instance->_set_db_info($conn_name, $tab_name, $tab_data);
if ($tab_data !== FALSE)
{
foreach($tab_data as &$d)
{
$tab_model->append(null, array($d));
}
$cell_renderer = new GtkCellRendererText();
$cell_renderer->set_property('editable', FALSE);
$tab->insert_column_with_data_func(0, $col_name, $cell_renderer, array($tab, 'add_data_col'));
if ( ! empty($events))
{
foreach($events as &$method)
{
call_user_func_array(array($tab, 'connect'), $method);
}
}
self::$instance->add_tab($tab_name, $tab);
}
self::$instance->show_all();
return;
}
// --------------------------------------------------------------------------
/**
* Add a multidimensional array to a tab
*
* @param object $conn
* @param string $tab_name
* @param string $method
* @return void
*/
private static function _add_row_tab(&$conn, $tab_name, $method)
{
$conn_name = $conn->conn_name;
$instance_data = self::$instance->_get_db_info($conn_name, $tab_name);
$tab_data = ($instance_data === FALSE)
? call_user_func_array(array($conn, $method), array())
: $instance_data;
self::$instance->_set_db_info($conn_name, $tab_name, $tab_data);
if ( ! empty($tab_data))
{
$tab = new Data_Grid();
$tab->render_data($tab_data);
self::$instance->add_tab($tab_name, $tab);
}
self::$instance->show_all();
return;
}
// --------------------------------------------------------------------------
/**
* Returns cached database data for the tab and connection specified
*
* @param string $conn_name
* @param string $tab_name
* @return mixed
*/
private function _get_db_info($conn_name, $tab_name)
{
$data =& self::$instance->data;
if ( ! isset($data->{$conn_name}))
{
$data->{$conn_name}= array();
return FALSE;
}
if ( ! isset($data->{$conn_name}[$tab_name]))
{
return FALSE;
}
return $data->{$conn_name}[$tab_name];
}
// --------------------------------------------------------------------------
/**
* Sets cached database data for the tab and connection specified
*
* @param string $conn_name
* @param string $tab_name
* @param mixed $data
*/
private function _set_db_info($conn_name, $tab_name, $data)
{
self::$instance->data->{$conn_name}[$tab_name] = $data;
//$this->append_page($widget, new GtkLabel($label));
}
}
// End of db_tabs.php

View File

@ -1,42 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Window controlling addition of database connections
*
* @package OpenSQLManager
* @subpackage Windows
*/
class Add_DB extends GtkWindow {
/**
* Create the window
*/
public function __construct()
{
parent::__construct();
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_title("Add Database Connection");
// Create the layout table
$connection_form = new DB_Info_Widget();
// Add the Vbox, and show the window
$this->add($connection_form);
$this->show_all();
}
}
// End of add_db.php

View File

@ -1,63 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Popup window to display database table data
*
* @package OpenSQLManager
* @subpackage Windows
*/
class DB_Table_Data extends GtkWindow {
/**
* Reference to the scrolled window
*
* @var GtkScrolledWindow
*/
protected $win;
/**
* Create and populate the window
*
* @param array $data
* @return void
*/
public function __construct($data)
{
parent::__construct();
$this->set_title("Table Data");
$this->set_position(Gtk::WIN_POS_CENTER_ALWAYS);
$this->set_destroy_with_parent(TRUE);
// Add the scrolled window
$this->win = new GTKScrolledWindow();
$this->win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$this->add($this->win);
// Resize to a sane size
$this->set_size_request(640, 480);
// Layout the widgets
$view = new Data_Grid();
$view->render_data($data);
// Add the grid to the window
$this->win->add_with_viewport($view);
// Show everything
$this->show_all();
}
}

View File

@ -1,47 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Window controlling modifications of database connections
*
* @package OpenSQLManager
* @subpackage Windows
*/
class Edit_DB extends GtkWindow {
/**
* Connection editing window
*
* @param object $db_params
*/
public function __construct($db_params)
{
parent::__construct();
$this->set_position(Gtk::WIN_POS_CENTER);
$this->set_title("Edit Database Connection");
// Create the layout table
$connection_form = new DB_Info_Widget($db_params);
// Add the Vbox, and show the window
$this->add($connection_form);
$this->show_all();
// Hide fields
$connection_form->set_db();
}
}
// End of edit_db.php

View File

@ -1,54 +0,0 @@
<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @package OpenSQLManager
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Window for editing db table structure
*
* @package OpenSQLManager
* @subpackage Windows
*/
class Edit_Table extends GtkWindow {
/**
* Database fields for the current database table
*
* @var array
*/
private $fields;
/**
* Create the window, and set basic properties
*
* @param string $name
* @param array $fields
*/
public function __construct($name="", array $fields=array())
{
// Create the window
parent::__construct();
if ( ! empty($name))
{
$this->set_title($name);
}
if ( ! empty($fields))
{
$this->fields = $fields;
}
}
}
// End of edit_table.php

View File

@ -45,32 +45,18 @@ class Main extends \wxFrame {
public function __construct()
{
parent::__construct(NULL, NULL, PROGRAM_NAME, \wxDefaultPosition, new \wxSize(800, 600));
$this->SetSizeHints(\wxDefaultSize, \wxDefaultSize);
$this->_create_menu();
$sbar = $this->CreateStatusBar(2);
$sbar->SetStatusText("OpenSQLManager");
$this->settings =& \Settings::get_instance();
/*if ( ! is_null($this->settings->width) && ! is_null($this->settings->height))
{
// Resize to the last size
$this->set_size_request($this->settings->width, $this->settings->height);
}
else
{
// Resize to a sane size
$this->set_size_request(640, 480);
}
if (! is_null($this->settings->position))
{
$this->move($this->settings->position[0], $this->settings->position[1]);
}
else
{
$this->set_position(Gtk::WIN_POS_CENTER);
}
// Layout the interface
$this->_main_layout();*/
$this->_main_layout();
}
// --------------------------------------------------------------------------
@ -82,28 +68,19 @@ class Main extends \wxFrame {
*/
public function __destruct()
{
// Save the Window position
/*$this->settings->position = $this->get_position();
list($width, $height) = $this->get_size();
// Save the Window hegiht
$this->settings->height = $height;
// Save the Window width
$this->settings->width = $width;*/
$this->Destroy();
}
// --------------------------------------------------------------------------
/**
* Exits the GTK loop
* Exits the wx loop
*
* @return void
*/
public function quit()
{
Gtk::main_quit();
$this->Destroy();
}
// --------------------------------------------------------------------------
@ -116,37 +93,14 @@ class Main extends \wxFrame {
*/
private function _main_layout()
{
$this->set_title(PROGRAM_NAME);
// Quit when this window is closed
$this->connect_simple('destroy', array('gtk', 'main_quit'));
// Main Vbox that everything is contained in
$main_vbox = new GTKVBox(FALSE, 5);
// Main Hpaned for columns
$hpane = new GTKHPaned();
// Add the menubar
$main_vbox->pack_start($this->_create_menu(), FALSE, FALSE);
// Add the main interface area hbox
$main_vbox->pack_start($hpane);
$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
$scrolled_win->add_with_viewport(DB_tabs::get_instance());
// Set up the main menu
$this->_create_menu();
$win = new \wxSplitterWindow($this);
$win->setSplitMode(wxSPLIT_HORIZONTAL);
// Add the connection sidebar
$this->connection_sidebar =& Connection_Sidebar::get_instance();
// Add the left column to the hpane
$hpane->pack1($this->connection_sidebar, FALSE);
$hpane->pack2($scrolled_win);
// Add the Vbox, and show the window
$this->add($main_vbox);
$this->show_all();
}
// --------------------------------------------------------------------------
@ -162,40 +116,31 @@ class Main extends \wxFrame {
$menu_bar = new \wxMenuBar();
// Menu Bar Top Items
$top_file_menu = new GtkMenuItem('_File');
$top_help_menu = new GtkMenuItem('_Help');
// Add sub Menus to top items
$file_menu = new GtkMenu();
$top_file_menu->set_submenu($file_menu);
$help_menu = new GtkMenu();
$top_help_menu->set_submenu($help_menu);
$top_file_menu = new \wxMenu();
$top_help_menu = new \wxMenu();
// File Menu
{
// Set up the quit item
$quit = new GtkImageMenuItem(GTK::STOCK_QUIT);
$quit->connect_simple('activate', array($this, 'quit'));
$file_menu->append($quit);
$top_file_menu->Append(2, "&Quit", "Exit the program");
$this->Connect(2, wxEVT_COMMAND_MENU_SELECTED, array($this, "quit"));
// Add the top level menu to the menubar
$menu_bar->append($top_file_menu);
$menu_bar->Append($top_file_menu, "&File");
}
// Help Menu
{
// Set up the about item
$about = new GtkImageMenuItem(GTK::STOCK_ABOUT);
$about->connect_simple('activate', 'about');
$help_menu->append($about);
//$top_help_menu->Append(4, "&About", "About this program");
//$this->Connect(4, wxEVT_COMMAND_MENU_SELECTED, "about");
// Add the top level menu to the menubar
$menu_bar->append($top_help_menu);
$menu_bar->Append($top_help_menu, "&Help");
}
return $menu_bar;
$this->SetMenuBar($menu_bar);
}
}
// End of main.php