This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
OpenSQLManager/sys/windows/widgets/connection_sidebar.php

304 lines
7.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();
$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'));
$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();
$this->treeview->insert_column_with_data_func(1, 'Connection name', $cell_renderer, array($this, 'set_label'));
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);
}
// --------------------------------------------------------------------------
/**
* 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);
$path = $path_array[0][0];
$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
}
$this->menu = $this->conn_popup_menu($path, $event, $col, $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
*
* @param array $pos
2012-02-28 10:38:13 -05:00
* @param object $event
* @return void
2012-02-27 12:14:49 -05:00
*/
public function conn_popup_menu($pos, $event, $col, $all)
2012-02-27 12:14:49 -05:00
{
$this->menu = new GtkMenu();
// Set up menu items
{
$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()
{
//@todo implement
}
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
*/
public function remove_connection($col)
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;
}
//@todo implement
2012-02-28 10:38:13 -05:00
$model = $this->treeview->get_model();
2012-02-24 17:53:16 -05:00
}
// --------------------------------------------------------------------------
/**
* Add a connection to the connection manager
2012-03-28 09:57:57 -04:00
*
2012-02-28 10:38:13 -05:00
* @param string $key
* @param array $vals
* @return void
2012-02-24 17:53:16 -05:00
*/
2012-02-28 10:38:13 -05:00
public function add_connection($key, $vals)
2012-02-24 17:53:16 -05:00
{
//@todo implement
2012-02-28 10:38:13 -05:00
$model = $this->treeview->get_model();
2012-02-24 17:53:16 -05:00
}
}
2012-02-24 17:53:16 -05:00
// End of connection_sidebar.php