<?php
/**
 * Php-Gtk MySQL administrator - Main application view
 *
 * @license CeCILL 2.0
 * @copyright 2008 Ouest  Systemes Informatiques
 * @author Frederic G. MARAND
 * @version $Id: Pgma_View.php,v 1.2 2008-05-25 20:31:40 cvs Exp $
 */

/**
 * Main application view
 */
class Pgma_View extends Glade_Window 
  {
  /**
   * The application model (config file)
   * @see Pgma_Model
   * @var array
   */
  protected $model;
  
  /**
   * @var Server_Controller 
   */
  protected $serverController;
  
  /**
   * @var GtkFrame
   */
  public $activeDetailView;

  /**
   * Prepare the view and its initial data
   * 
   * @return void
   */
  public function __construct()
    {
    /**
     * 1. Load the view fixed structure
     */
    parent::__construct('winPgma'); // not the standard top-level name
    $this->top->set_size_request(640, 480);
    
    /**
     * 2. Restore session info 
     */
    $this->model = new Pgma_Model(); // load configuration info
    /**
     * @todo Apply layout info restored at this point to the top-level window
     */

    /**
     * 3. connect
     */
    $this->serverController = new Server_Controller($this->model->properties['Auth']);
    try
      {
      $this->serverController->login();
      }
    catch (PDOException $e)
      {
      die('Failed login to the DB server: ' . PHP_EOL 
        . $e->getMessage() . PHP_EOL);
      }
      
    /**
     * Populate the DB view
     */
    $dbStore = new GtkListStore(GObject::TYPE_STRING);
    $arDatabases = $this->serverController->getDatabases();
    foreach ($arDatabases as $dbName)
      {
      $dbStore->append(array($dbName));
      }

    $dbView = $this->glade->get_widget('tvNavigation');
    $dbView->set_model($dbStore);
    
    $dbNameRenderer = new GtkCellRendererText();
    $dbNameRenderer->set_property('width', -1);
    $dbNameRenderer->set_property('text', $dbName);
    $dbCol = new GtkTreeViewColumn('Database', $dbNameRenderer, 'text', 0);
    $dbCol->set_cell_data_func($dbNameRenderer, array($this, 'zebraTreeViewCallback'));
    $dbView->append_column($dbCol);
    }
  }