OpenSQLManager/sys/windows/widgets/connection_sidebar.php

384 lines
9.0 KiB
PHP
Raw Normal View History

<?php
/**
* OpenSQLManager
*
* Free Database manager for Open Source Databases
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
* @link https://github.com/aviat4ion/OpenSQLManager
2012-03-28 09:57:57 -04:00
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
2012-03-28 09:57:57 -04:00
/**
* Widget managing saved database connections
*/
class Connection_Sidebar extends GtkVBox {
2012-03-29 16:26:50 -04:00
protected $settings, $menu, $treeview;
2012-02-28 10:38:13 -05:00
private static $instance;
2012-02-24 17:53:16 -05:00
2012-03-26 12:55:52 -04:00
/**
* Return the current instance of the class
*
* @return Connection_Sidebar
*/
2012-02-28 10:38:13 -05:00
public static function &get_instance()
{
if( ! isset(self::$instance))
{
$name = __CLASS__;
self::$instance = new $name();
}
return self::$instance;
}
2012-03-26 12:55:52 -04:00
// --------------------------------------------------------------------------
2012-02-28 10:38:13 -05:00
/**
* Constructor method
*/
public function __construct()
{
parent::__construct();
$this->settings =& Settings::get_instance();
2012-04-02 16:56:55 -04:00
//$dblabel = new GtkLabel('Database Connections');
//$dblabel->set_alignment(0,0);
$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'));
2012-04-02 16:56:55 -04:00
//$this->pack_start($dblabel, FALSE, FALSE);
// Treeview to show database connections
{
2012-02-28 10:38:13 -05:00
// Render the treeview
2012-03-28 09:57:57 -04:00
$this->_render();
2012-02-28 10:38:13 -05:00
// Set up context menu event
$this->treeview->connect('button-press-event', array($this, 'on_button'));
2012-02-27 12:14:49 -05:00
2012-02-28 10:38:13 -05:00
$selection = $this->treeview->get_selection();
$selection->set_mode(GTK::SELECTION_SINGLE);
}
2012-03-28 09:57:57 -04:00
2012-02-28 10:38:13 -05:00
$this->pack_start($this->treeview);
$this->pack_start($add_button, FALSE);
}
2012-03-26 12:55:52 -04:00
// --------------------------------------------------------------------------
2012-02-28 10:38:13 -05:00
/**
* Renders the connection sidebar widget
*/
protected function _render()
{
// Create the treeview
$this->treeview = (isset($this->treeview))
? $this->treeview
: new Data_Grid();
2012-03-29 16:26:50 -04:00
$model = $this->treeview->get_model();
2012-02-28 10:38:13 -05:00
// Add the existing connections to the model
$db_conns = $this->settings->get_dbs();
if( ! empty($db_conns))
{
foreach($db_conns as $name => $props)
{
2012-03-30 09:58:41 -04:00
if (is_array($props))
{
$props = array_to_object($props);
}
2012-02-28 10:38:13 -05:00
$db = $props;
$db->name = $name;
2012-03-29 16:26:50 -04:00
$iter = $model->append();
$model->set($iter, 0, $db);
2012-02-28 10:38:13 -05:00
}
}
2012-03-28 09:57:57 -04:00
2012-02-28 10:38:13 -05:00
// Icon column
$cell_renderer = new GtkCellRendererPixbuf();
$this->treeview->insert_column_with_data_func(0, 'Type', $cell_renderer, array($this, 'set_icon'));
2012-02-28 10:38:13 -05:00
// Label column
$cell_renderer = new GtkCellRendererText();
2012-04-02 16:56:55 -04:00
$this->treeview->insert_column_with_data_func(1, 'Name', $cell_renderer, array($this, 'set_label'));
2012-04-02 10:23:27 -04:00
// Status column
$cell_renderer = new GtkCellRendererPixbuf();
$this->treeview->insert_column_with_data_func(2, 'Status', $cell_renderer, array($this, 'set_status_icon'));
2012-02-28 10:38:13 -05:00
}
// --------------------------------------------------------------------------
/**
* Sets the icon for the current db type
2012-03-28 09:57:57 -04:00
*
* @param GtkTreeView Column $col
* @param GtkCellRenderer $cell
2012-02-28 10:38:13 -05:00
* @param GtkTreeModel $this->model
* @param GtkTreeIter $iter
*/
public function set_icon($col, $cell, $model, $iter)
{
$col->set_reorderable(TRUE);
2012-03-29 16:26:50 -04:00
$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
2012-03-28 09:57:57 -04:00
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
2012-02-28 10:38:13 -05:00
* @param GtkTreeModel $this->model
* @param GtkTreeIter $iter
*/
public function set_label($col, $cell, $model, $iter)
{
$col->set_reorderable(TRUE);
2012-03-29 16:26:50 -04:00
$info = $model->get_value($iter, 0);
$cell->set_property('text', $info->name);
}
// --------------------------------------------------------------------------
2012-04-02 10:23:27 -04:00
/**
* Sets the status icon of the current db connection
*
* @param GtkTreeViewColumn $col
* @param GtkCellRenderer $cell
* @param GtkTreeModel $model
* @param GtkTreeIter $iter
*/
public function set_status_icon($col, $cell, $model, $iter)
{
2012-04-02 10:52:46 -04:00
$col->set_reorderable(TRUE);
$info = $model->get_value($iter, 0);
list($width, $height) = Gtk::icon_size_lookup(Gtk::ICON_SIZE_SMALL_TOOLBAR);
2012-04-02 10:23:27 -04:00
2012-04-02 10:52:46 -04:00
$conns = DB_Reg::get_connections();
2012-04-02 16:56:55 -04:00
if(in_array($info->name, $conns))
2012-04-02 10:52:46 -04:00
{
2012-04-02 16:56:55 -04:00
$cell->set_property('stock-id', 'gtk-yes');
2012-04-02 10:52:46 -04:00
}
else
{
2012-04-02 16:56:55 -04:00
$cell->set_property('stock-id', 'gtk-no');
}
2012-04-02 10:23:27 -04:00
}
// --------------------------------------------------------------------------
/**
* Returns window for creating a new database connection
2012-03-28 09:57:57 -04:00
*
* @return Add_DB object
*/
public function new_conn()
{
return new Add_DB();
}
2012-02-24 17:53:16 -05:00
// --------------------------------------------------------------------------
2012-02-27 12:14:49 -05:00
/**
* Event for mouse clicks on connection sidebar
2012-03-28 09:57:57 -04:00
*
2012-02-27 12:14:49 -05:00
* @param GtkTreeView $view
* @param $event
* @return void
*/
public function on_button($view, $event)
{
if ($event->button !== 3 || empty($view))
{
return;
}
2012-02-27 12:14:49 -05:00
// Right click
if ($event->button == 3)
2012-02-27 12:14:49 -05:00
{
// 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;
}
2012-02-27 12:14:49 -05:00
}
2012-04-02 08:43:27 -04:00
$this->menu = $this->conn_popup_menu($path_array);
2012-02-27 12:14:49 -05:00
}
// --------------------------------------------------------------------------
/**
2012-02-28 10:38:13 -05:00
* Creates and displays a context menu for the selected connection
2012-03-28 09:57:57 -04:00
*
2012-03-30 13:09:38 -04:00
* @param array $all
2012-02-28 10:38:13 -05:00
* @return void
2012-02-27 12:14:49 -05:00
*/
2012-04-02 08:43:27 -04:00
public function conn_popup_menu($all)
2012-02-27 12:14:49 -05:00
{
$this->menu = new GtkMenu();
2012-04-02 16:56:55 -04:00
$data = $this->treeview->get(0);
$conns = DB_Reg::get_connections();
2012-02-27 12:14:49 -05:00
// Set up menu items
{
2012-04-02 16:56:55 -04:00
// 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'));
}
2012-04-02 09:47:13 -04:00
$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);
}
2012-03-28 09:57:57 -04:00
// Popup the menu
2012-02-27 12:14:49 -05:00
$this->menu->show_all();
$this->menu->popup();
}
// --------------------------------------------------------------------------
/**
* Recreate sidebar widget to update connections
*/
public function refresh()
{
$this->treeview->reset();
$this->_render();
}
// --------------------------------------------------------------------------
2012-03-27 15:47:56 -04:00
/**
* Update the connection information for an existing connection
*/
public function edit_connection()
{
return new Edit_Db($this->treeview->get(0));
2012-03-27 15:47:56 -04:00
}
2012-03-28 09:57:57 -04:00
// --------------------------------------------------------------------------
2012-03-27 15:47:56 -04:00
2012-02-24 17:53:16 -05:00
/**
* Remove a connection from the connection manager
2012-03-28 09:57:57 -04:00
*
2012-02-28 10:38:13 -05:00
* @return void
2012-02-24 17:53:16 -05:00
*/
2012-03-30 13:09:38 -04:00
public function remove_connection()
2012-02-24 17:53:16 -05:00
{
2012-03-27 15:47:56 -04:00
if ( ! confirm("Are you sure you want to remove this database connection?"))
{
return;
}
2012-03-30 13:09:38 -04:00
// 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();
2012-02-24 17:53:16 -05:00
}
2012-04-02 09:47:13 -04:00
// --------------------------------------------------------------------------
/**
* Create connection to a database
*
* @param string $name
* @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);
}
catch(PDOException $e)
{
error("Could not connect to database:\n". $e->getMessage());
return;
}
DB_Tabs::get_instance()->get_db_tabs($conn);
}
2012-04-02 16:56:55 -04:00
// --------------------------------------------------------------------------
/**
* Disconnect from a database
*/
public function db_disconnect()
{
$data = $this->treeview->get(0);
DB_Reg::remove_db($data->name);
$this->refresh();
}
}
2012-02-24 17:53:16 -05:00
// End of connection_sidebar.php