rendered paste body<?phperror_reporting(E_ALL);/** * * Marc Quinton - 2006 december. * * PhpGtkCustomTreeModel sample examples reeading directories. * * PhpGtkCustomTreeModel implements a customisable GtkTreeView model. * * a good tutorial about this in python : * http://www.moeraki.com/pygtktutorial/pygtk2tutorial/sec-GenericTreeModel.html * * * here is a list of available methods for PhpGtkCustomTreeModel : * * on_get_flags(); * on_get_n_columns(); * on_get_column_type($index); * on_get_path($node); * on_get_iter($path); * on_iter_children($iter); * on_iter_has_child($iter); * on_iter_n_children($iter); * on_iter_nth_child($iter, $n); * on_iter_parent($iter); * on_get_value($iter, $colum); * on_iter_next($iter) * */class MyIter { public $idx = null;}/** * an abtract class that implements basic methods for a flat (text) list. * */abstract class FlatTextModel extends PhpGtkCustomTreeModel{ protected $iter = null; protected $debug; function __construct() { parent::__construct(); $this->debug=false; } abstract public function on_get_value($iter, $n); abstract public function on_iter_next($iter); # can handle only List feature. public function on_get_flags() { return Gtk::TREE_MODEL_LIST_ONLY; } # number of colums. public function on_get_n_columns() { return 1; } # only one column of type string. public function on_get_column_type($index) { return Gtk::TYPE_STRING; } public function on_get_path($node) { exit; $this->debug("on_get_path\n"); return $node; } # flat list public function on_get_iter($path) { $this->debug("on_get_iter\n"); assert(count($path) == 1); $this->iter->idx = $path[0]; return $this->iter; } # flat list. public function on_iter_children($iter) { return null; } ## flat list. public function on_iter_has_child($iter) { return false; } # flat list. public function on_iter_n_children($iter) { return 0; } # flat list. public function on_iter_nth_child($iter, $n) { return null; } # flat list. public function on_iter_parent($iter) { return null; } protected function debug($txt){ if($this->debug) echo $txt; }}/** * DirectoryListModelBasic * * create a model based on directory listing - only view directory content. * */class DirectoryListModelBasic extends FlatTextModel { protected $dir; protected $content; function __construct($dir = '.') { parent::__construct(); $this->dir = $dir; $this->build_array($dir); } protected function build_array($dir){ $this->content = array(); $this->content = array_merge( $this->get_subdirs($dir), $this->get_files($dir) ); } public function on_get_value($iter, $n) { $this->debug("on_get_value\n"); assert($n == 0); return $this->content[$this->iter->idx]; } public function on_iter_next($iter) { $this->debug("on_iter_next\n"); $num = count($this->content); if ($this->iter->idx == $num-1) { return null; } else { $this->iter->idx++; return $this->iter; } } protected function has_subdirs($dir){ $subdirs = $this->get_subdirs($dir); if(count($subdirs) == 0) return false; return true; } protected function get_subdirs($dir, $sort=true){ if(!is_readable($dir)) return array(); $d = opendir($dir); if(!$d) return array(); $list = array(); while(($file = readdir($d)) !== false){ if($file != '.' && $file != '..'){ $fullpath = $dir . '/' . $file; if(is_dir($fullpath)) $list[] = $file; } } if($sort) $this->sort($list); return $list; } protected function get_files($dir, $sort=true){ if(!is_readable($dir)) return array(); $d = opendir($dir); if(!$d) return array(); $list = array(); while(($file = readdir($d)) !== false){ if($file != '.' && $file != '..'){ $fullpath = $dir . '/' . $file; if(!is_dir($fullpath)) $list[] = $file; } } if($sort) $this->sort($list); return $list; } protected function sort(&$array){ natcasesort($array); } protected function filename($iter){ return $this->content[$this->iter->idx]; } protected function fullpath($filename){ return $this->dir . '/' . $filename; }}define('DIRECTORY_MODEL_COL_FILENAME', 0);define('DIRECTORY_MODEL_COL_SIZE', 1);define('DIRECTORY_MODEL_COL_PERMS', 2);define('DIRECTORY_MODEL_COL_DATE_C', 3); # file date - creationdefine('DIRECTORY_MODEL_COL_DATE_M', 4); # file date - modificationdefine('DIRECTORY_MODEL_COL_DATE_A', 5); # file date - accessclass DirectoryListModelMulticol extends DirectoryListModelBasic{ protected $column_types = array( DIRECTORY_MODEL_COL_FILENAME => Gtk::TYPE_STRING, DIRECTORY_MODEL_COL_SIZE => Gtk::TYPE_LONG, DIRECTORY_MODEL_COL_PERMS => Gtk::TYPE_STRING, DIRECTORY_MODEL_COL_DATE_C => Gtk::TYPE_STRING, DIRECTORY_MODEL_COL_DATE_M => Gtk::TYPE_STRING, DIRECTORY_MODEL_COL_DATE_A => Gtk::TYPE_STRING ); # number of colums. public function on_get_n_columns() { return count($this->column_types); } # only one column of type string. public function on_get_column_type($index) { return $this->column_types[$index]; } # TODO : create a cache. public function on_get_value($iter, $n) { switch($n){ case DIRECTORY_MODEL_COL_FILENAME: # filename return parent::on_get_value($iter, $n); case DIRECTORY_MODEL_COL_SIZE: # filesize return filesize($this->fullpath($this->filename($iter))); case DIRECTORY_MODEL_COL_PERMS: # file mode return $this->fileperms($this->fullpath($this->filename($iter))); case DIRECTORY_MODEL_COL_DATE_C: # file date $list = stat($this->fullpath($this->filename($iter))); return date('Y/m/d H:i:s', $list['ctime']); case DIRECTORY_MODEL_COL_DATE_M: # file date $list = stat($this->fullpath($this->filename($iter))); return date('Y/m/d H:i:s', $list['mtime']); case DIRECTORY_MODEL_COL_DATE_A: # file date $list = stat($this->fullpath($this->filename($iter))); return date('Y/m/d H:i:s', $list['atime']); default: return 'that colums does not exists'; } } protected function fileperms($file){ # window users, you can change here :-) $perms = fileperms($file); return decoct($perms); }}$test = 2;$w = new GtkWindow();$w->set_default_size(100, 250);$w->set_position(Gtk::WIN_POS_CENTER);$w->connect_simple('destroy', array('gtk', 'main_quit')); $sc = new GtkScrolledWindow(); $sc->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC); $w->add($sc); switch($test){ case 1: $model = new DirectoryListModelBasic('/etc'); $tree_view = new GtkTreeView($model); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("filename", $cell, 'text', 0); $tree_view->append_column($col); break; case 2: $model = new DirectoryListModelMulticol('/etc'); $tree_view = new GtkTreeView($model); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("filename", $cell, 'text', DIRECTORY_MODEL_COL_FILENAME); $tree_view->append_column($col); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("size", $cell, 'text', DIRECTORY_MODEL_COL_SIZE); $tree_view->append_column($col); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("perms", $cell, 'text', DIRECTORY_MODEL_COL_PERMS); $tree_view->append_column($col); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("date(Creation)", $cell, 'text', DIRECTORY_MODEL_COL_DATE_C); $tree_view->append_column($col); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("Modif", $cell, 'text', DIRECTORY_MODEL_COL_DATE_M); $tree_view->append_column($col); $cell = new GtkCellRendererText(); $col = new GtkTreeViewColumn("Acces", $cell, 'text', DIRECTORY_MODEL_COL_DATE_C); $tree_view->append_column($col); $w->set_default_size(720, 400); break; } $sc->add($tree_view); $w->show_all();gtk::main();?>