Archive
/
/
/
/
/
/
FileBrowserModel.h
2008-10-06 11:18:27 UTC
previous
next
/* Echo Media Player
* Copyright (C) 2008 Shane O'Connell
*
* [ The original file includes a copyright header in this location describing
* the file as being released under the terms of the GNU General Public
* License. It has been removed in order to display the file as part of the
* archive. ]
*/
#ifndef FILEBROWSERMODEL_H_
#define FILEBROWSERMODEL_H_
#include <list>
#include <gtkmm.h>
#include <giomm.h>
class FileBrowserModel : public Glib::Object, public Gtk::TreeModel
{
// ----------------------- Constructor -----------------------
public:
static Glib::RefPtr<FileBrowserModel> create(const std::string& uri)
{ return Glib::RefPtr<FileBrowserModel>(new FileBrowserModel(uri)); }
protected:
FileBrowserModel(const std::string& uri);
virtual ~FileBrowserModel()
{}
// ----------------------- Column information -----------------------
public:
class Columns : public Gtk::TreeModelColumnRecord {
public:
Gtk::TreeModelColumn<Glib::ustring> filename;
Gtk::TreeModelColumn<Glib::RefPtr<Gio::Icon> > icon;
Columns() { add(filename); add(icon); }
};
const Columns& get_columns() const { return m_columns; }
protected:
Columns m_columns;
// ----------------------- Internal filesystem representation -----------------------
protected:
class Node {
public:
Node(Node* parent, int index, Glib::ustring display_name, std::string uri,
bool is_directory, Glib::RefPtr<Gio::Icon> icon) :
m_parent(parent), m_index(index), m_display_name(display_name), m_uri(uri),
m_is_directory(is_directory), m_icon(icon), m_children_known(!is_directory)
{}
Node* get_parent() const { return m_parent; }
int get_index() const { return m_index; }
const Glib::ustring& get_display_name() const { return m_display_name; }
const std::string& get_uri() const { return m_uri; }
bool is_directory() const { return m_is_directory; }
Glib::RefPtr<Gio::Icon> get_icon() const { return m_icon; }
int get_num_children();
Node* get_child(int index);
bool operator<(const Node& otherNode) const;
private:
void add_children();
Node* m_parent;
int m_index;
Glib::ustring m_display_name;
std::string m_uri;
bool m_is_directory;
Glib::RefPtr<Gio::Icon> m_icon;
std::vector<Node> m_children;
bool m_children_known;
};
Node* m_root_node;
// ----------------------- TreeModel interface -----------------------
protected:
int m_stamp; // Used to invalidate all old iters when model updated
// TODO also implement the iter_ref/iter_unref for performance
virtual GType get_column_type_vfunc(int index) const;
virtual void get_value_vfunc(const Gtk::TreeIter& iter, int column, Glib::ValueBase& value) const;
virtual bool get_iter_vfunc(const Gtk::TreePath& path, Gtk::TreeIter& iter) const;
virtual Gtk::TreePath get_path_vfunc(const Gtk::TreeIter& iter) const;
virtual bool iter_next_vfunc(const Gtk::TreeIter& iter, Gtk::TreeIter& iter_next) const;
virtual int iter_n_children_vfunc(const Gtk::TreeIter& iter) const;
virtual bool iter_nth_child_vfunc(const Gtk::TreeIter& parent, int n, Gtk::TreeIter& iter) const;
virtual bool iter_nth_root_child_vfunc (int n, Gtk::TreeIter& iter) const;
virtual bool iter_parent_vfunc(const Gtk::TreeIter& child, Gtk::TreeIter& iter) const;
virtual bool iter_is_valid (const Gtk::TreeIter& iter) const
{ return iter.get_stamp() == m_stamp; }
virtual bool iter_children_vfunc(const Gtk::TreeIter& parent, Gtk::TreeIter& iter) const
{ return iter_nth_child_vfunc(parent, 0, iter); }
virtual bool iter_has_child_vfunc(const Gtk::TreeIter& iter) const
{ return (iter_n_children_vfunc(iter) > 0); }
virtual int iter_n_root_children_vfunc() const
{ return m_root_node->get_num_children(); }
virtual int get_n_columns_vfunc() const
{ return get_columns().size(); }
virtual Gtk::TreeModelFlags get_flags_vfunc() const
{ return Gtk::TreeModelFlags(0); }
};
#endif /* FILEBROWSERMODEL_H_ */