2008-07-01 22:57:50 +00:00
|
|
|
|
/* GTK - The GIMP Toolkit
|
2003-03-21 20:34:02 +00:00
|
|
|
|
* gtkfilesystemmodel.c: GtkTreeModel wrapping a GtkFileSystem
|
|
|
|
|
* Copyright (C) 2003, Red Hat, Inc.
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2008-06-22 14:28:52 +00:00
|
|
|
|
#include "config.h"
|
2003-10-23 04:22:32 +00:00
|
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
|
#include "gtkfilesystemmodel.h"
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2009-06-24 14:36:20 +00:00
|
|
|
|
#include "gtkfilesystem.h"
|
2003-11-03 20:24:04 +00:00
|
|
|
|
#include "gtkintl.h"
|
2004-03-29 19:52:16 +00:00
|
|
|
|
#include "gtkmarshalers.h"
|
2009-06-30 13:15:55 +00:00
|
|
|
|
#include "gtktreedatalist.h"
|
2004-02-25 03:03:11 +00:00
|
|
|
|
#include "gtktreednd.h"
|
2003-10-23 04:22:32 +00:00
|
|
|
|
#include "gtktreemodel.h"
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-09-03 23:22:21 +00:00
|
|
|
|
/*** Structure: how GtkFileSystemModel works
|
|
|
|
|
*
|
|
|
|
|
* This is a custom GtkTreeModel used to hold a collection of files for GtkFileChooser. There are two use cases:
|
|
|
|
|
*
|
|
|
|
|
* 1. The model populates itself from a folder, using the GIO file enumerator API. This happens if you use
|
|
|
|
|
* _gtk_file_system_model_new_for_directory(). This is the normal usage for showing the contents of a folder.
|
|
|
|
|
*
|
|
|
|
|
* 2. The caller populates the model by hand, with files not necessarily in the same folder. This happens
|
|
|
|
|
* if you use _gtk_file_system_model_new() and then _gtk_file_system_model_add_and_query_file(). This is
|
|
|
|
|
* the special kind of usage for "search" and "recent-files", where the file chooser gives the model the
|
|
|
|
|
* files to be displayed.
|
|
|
|
|
*
|
|
|
|
|
* Each file is kept in a FileModelNode structure. Each FileModelNode holds a GFile* and other data. All the
|
|
|
|
|
* node structures have the same size, determined at runtime, depending on the number of columns that were passed
|
|
|
|
|
* to _gtk_file_system_model_new() or _gtk_file_system_model_new_for_directory() (that is, the size of a node is
|
|
|
|
|
* not sizeof (FileModelNode), but rather model->node_size). The last field in the FileModelNode structure,
|
|
|
|
|
* node->values[], is an array of GValue, used to hold the data for those columns.
|
|
|
|
|
*
|
|
|
|
|
* The model stores an array of FileModelNode structures in model->files. This is a GArray where each element is
|
|
|
|
|
* model->node_size bytes in size (the model computes that node size when initializing itself). There are
|
|
|
|
|
* convenience macros, get_node() and node_index(), to access that array based on an array index or a pointer to
|
|
|
|
|
* a node inside the array.
|
|
|
|
|
*
|
|
|
|
|
* The model accesses files through two of its fields:
|
|
|
|
|
*
|
|
|
|
|
* model->files - GArray of FileModelNode structures.
|
|
|
|
|
*
|
|
|
|
|
* model->file_lookup - hash table that maps a GFile* to an index inside the model->files array.
|
|
|
|
|
*
|
|
|
|
|
* The model->file_lookup hash table is populated lazily. It is both accessed and populated with the
|
|
|
|
|
* node_get_for_file() function. The invariant is that the files in model->files[n] for n < g_hash_table_size
|
|
|
|
|
* (model->file_lookup) are already added to the hash table. The hash table will get cleared when we re-sort the
|
|
|
|
|
* files, as the array will be in a different order and the indexes need to be rebuilt.
|
|
|
|
|
*
|
|
|
|
|
* Each FileModelNode has a node->visible field, which indicates whether the node is visible in the GtkTreeView.
|
|
|
|
|
* A node may be invisible if, for example, it corresponds to a hidden file and the file chooser is not showing
|
|
|
|
|
* hidden files.
|
|
|
|
|
*
|
|
|
|
|
* Since not all nodes in the model->files array may be visible, we need a way to map visible row indexes from
|
2009-09-04 17:01:16 +00:00
|
|
|
|
* the treeview to array indexes in our array of files. And thus we introduce a bit of terminology:
|
2009-09-03 23:22:21 +00:00
|
|
|
|
*
|
2009-09-04 17:01:16 +00:00
|
|
|
|
* index - An index in the model->files array. All variables/fields that represent indexes are either called
|
|
|
|
|
* "index" or "i_*", or simply "i" for things like loop counters.
|
|
|
|
|
*
|
|
|
|
|
* row - An index in the GtkTreeView, i.e. the index of a row within the outward-facing API of the
|
|
|
|
|
* GtkFileSystemModel. However, note that our rows are 1-based, not 0-based, for the reason explained in the
|
|
|
|
|
* following paragraph. Variables/fields that represent visible rows are called "row", or "r_*", or simply
|
|
|
|
|
* "r".
|
|
|
|
|
*
|
|
|
|
|
* Each FileModelNode has a node->row field which is the number of visible rows in the treeview, *before and
|
|
|
|
|
* including* that node. This means that node->row is 1-based, instead of 0-based --- this makes some code
|
2009-09-03 23:22:21 +00:00
|
|
|
|
* simpler, believe it or not :) This also means that when the calling GtkTreeView gives us a GtkTreePath, we
|
2009-09-04 17:01:16 +00:00
|
|
|
|
* turn the 0-based treepath into a 1-based row for our purposes. If a node is not visible, it will have the
|
|
|
|
|
* same row number as its closest preceding visible node.
|
|
|
|
|
*
|
|
|
|
|
* We try to compute the node->row fields lazily. A node is said to be "valid" if its node->row is accurate.
|
|
|
|
|
* For this, the model keeps a model->n_nodes_valid field which is the count of valid nodes starting from the
|
|
|
|
|
* beginning of the model->files array. When a node changes its information, or when a node gets deleted, that
|
|
|
|
|
* node and the following ones get invalidated by simply setting model->n_nodes_valid to the array index of the
|
|
|
|
|
* node. If the model happens to need a node's row number and that node is in the model->files array after
|
|
|
|
|
* model->n_nodes_valid, then the nodes get re-validated up to the sought node. See node_validate_rows() for
|
|
|
|
|
* this logic.
|
2009-09-03 23:22:21 +00:00
|
|
|
|
*
|
2009-09-04 17:01:16 +00:00
|
|
|
|
* You never access a node->row directly. Instead, call node_get_tree_row(). That function will validate the nodes
|
|
|
|
|
* up to the sought one if the node is not valid yet, and it will return a proper 0-based row.
|
2009-09-03 23:22:21 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** DEFINES ***/
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* priority used for all async callbacks in the main loop
|
|
|
|
|
* This should be higher than redraw priorities so multiple callbacks
|
|
|
|
|
* firing can be handled without intermediate redraws */
|
|
|
|
|
#define IO_PRIORITY G_PRIORITY_DEFAULT
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* random number that everyone else seems to use, too */
|
|
|
|
|
#define FILES_PER_QUERY 100
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
typedef struct _FileModelNode FileModelNode;
|
|
|
|
|
typedef struct _GtkFileSystemModelClass GtkFileSystemModelClass;
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
struct _FileModelNode
|
|
|
|
|
{
|
|
|
|
|
GFile * file; /* file represented by this node or NULL for editable */
|
|
|
|
|
GFileInfo * info; /* info for this file or NULL if unknown */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
guint row; /* if valid (see model->n_valid_indexes), visible nodes before and including
|
2009-09-03 23:22:21 +00:00
|
|
|
|
* this one - see the "Structure" comment above.
|
|
|
|
|
*/
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
guint visible :1; /* if the file is currently visible */
|
|
|
|
|
guint frozen_add :1; /* true if the model was frozen and the entry has not been added yet */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GValue values[1]; /* actually n_columns values */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
struct _GtkFileSystemModel
|
2009-06-17 08:39:26 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GObject parent_instance;
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GFile * dir; /* directory that's displayed */
|
|
|
|
|
guint dir_thaw_source;/* GSource id for unfreezing the model */
|
2009-08-27 00:27:40 +00:00
|
|
|
|
char * attributes; /* attributes the file info must contain, or NULL for all attributes */
|
|
|
|
|
GFileMonitor * dir_monitor; /* directory that is monitored, or NULL if monitoring was not supported */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GCancellable * cancellable; /* cancellable in use for all operations - cancelled on dispose */
|
|
|
|
|
GArray * files; /* array of FileModelNode containing all our files */
|
2009-09-30 23:39:33 +00:00
|
|
|
|
gsize node_size; /* Size of a FileModelNode structure once its ->values field has n_columns */
|
2009-09-04 17:01:16 +00:00
|
|
|
|
guint n_nodes_valid; /* count of valid nodes (i.e. those whose node->row is accurate) */
|
|
|
|
|
GHashTable * file_lookup; /* mapping of GFile => array index in model->files
|
2009-09-02 19:51:02 +00:00
|
|
|
|
* This hash table doesn't always have the same number of entries as the files array;
|
|
|
|
|
* it can get cleared completely when we resort.
|
|
|
|
|
* The hash table gets re-populated in node_get_for_file() if this mismatch is
|
|
|
|
|
* detected.
|
|
|
|
|
*/
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
guint n_columns; /* number of columns */
|
|
|
|
|
GType * column_types; /* types of each column */
|
|
|
|
|
GtkFileSystemModelGetValue get_func; /* function to call to fill in values in columns */
|
|
|
|
|
gpointer get_data; /* data to pass to get_func */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-24 14:36:20 +00:00
|
|
|
|
GtkFileFilter * filter; /* filter to use for deciding which nodes are visible */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
int sort_column_id; /* current sorting column */
|
|
|
|
|
GtkSortType sort_order; /* current sorting order */
|
|
|
|
|
GList * sort_list; /* list of sorting functions */
|
|
|
|
|
GtkTreeIterCompareFunc default_sort_func; /* default sort function */
|
|
|
|
|
gpointer default_sort_data; /* data to pass to default sort func */
|
|
|
|
|
GDestroyNotify default_sort_destroy; /* function to call to destroy default_sort_data */
|
|
|
|
|
|
|
|
|
|
guint frozen; /* number of times we're frozen */
|
2009-06-17 08:39:26 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gboolean filter_on_thaw :1;/* set when filtering needs to happen upon thawing */
|
|
|
|
|
gboolean sort_on_thaw :1;/* set when sorting needs to happen upon thawing */
|
|
|
|
|
|
|
|
|
|
guint show_hidden :1; /* whether to show hidden files */
|
|
|
|
|
guint show_folders :1;/* whether to show folders */
|
|
|
|
|
guint show_files :1; /* whether to show files */
|
|
|
|
|
};
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
|
|
|
|
#define GTK_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
|
|
|
|
|
#define GTK_IS_FILE_SYSTEM_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_FILE_SYSTEM_MODEL))
|
|
|
|
|
#define GTK_FILE_SYSTEM_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_FILE_SYSTEM_MODEL, GtkFileSystemModelClass))
|
|
|
|
|
|
|
|
|
|
struct _GtkFileSystemModelClass
|
|
|
|
|
{
|
|
|
|
|
GObjectClass parent_class;
|
2004-03-29 19:52:16 +00:00
|
|
|
|
|
|
|
|
|
/* Signals */
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
void (*finished_loading) (GtkFileSystemModel *model, GError *error);
|
2004-03-29 19:52:16 +00:00
|
|
|
|
};
|
|
|
|
|
|
2009-08-31 20:08:43 +00:00
|
|
|
|
static void add_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile *file,
|
|
|
|
|
GFileInfo *info);
|
2009-08-31 20:11:44 +00:00
|
|
|
|
static void remove_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile *file);
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* iter setup:
|
|
|
|
|
* @user_data: the model
|
|
|
|
|
* @user_data2: GUINT_TO_POINTER of array index of current entry
|
|
|
|
|
*
|
|
|
|
|
* All other fields are unused. Note that the array index does not corrspond
|
|
|
|
|
* 1:1 with the path index as entries might not be visible.
|
|
|
|
|
*/
|
|
|
|
|
#define ITER_INDEX(iter) GPOINTER_TO_UINT((iter)->user_data2)
|
|
|
|
|
#define ITER_IS_VALID(model, iter) ((model) == (iter)->user_data)
|
|
|
|
|
#define ITER_INIT_FROM_INDEX(model, _iter, _index) G_STMT_START {\
|
|
|
|
|
g_assert (_index < (model)->files->len); \
|
|
|
|
|
(_iter)->user_data = (model); \
|
|
|
|
|
(_iter)->user_data2 = GUINT_TO_POINTER (_index); \
|
|
|
|
|
}G_STMT_END
|
2004-03-29 19:52:16 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** FileModelNode ***/
|
2004-03-29 19:52:16 +00:00
|
|
|
|
|
2009-09-03 23:22:21 +00:00
|
|
|
|
/* Get a FileModelNode structure given an index in the model->files array of nodes */
|
2009-08-26 23:42:49 +00:00
|
|
|
|
#define get_node(_model, _index) ((FileModelNode *) ((_model)->files->data + (_index) * (_model)->node_size))
|
2009-09-03 23:22:21 +00:00
|
|
|
|
|
|
|
|
|
/* Get an index within the model->files array of nodes, given a FileModelNode* */
|
2009-08-26 23:42:49 +00:00
|
|
|
|
#define node_index(_model, _node) (((gchar *) (_node) - (_model)->files->data) / (_model)->node_size)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
/* @up_to_index: smallest model->files array index that will be valid after this call
|
|
|
|
|
* @up_to_row: smallest node->row that will be valid after this call
|
2009-09-03 23:22:21 +00:00
|
|
|
|
*
|
2009-09-04 17:01:16 +00:00
|
|
|
|
* If you want to validate up to an index or up to a row, specify the index or
|
|
|
|
|
* the row you want and specify G_MAXUINT for the other argument. Pass
|
|
|
|
|
* G_MAXUINT for both arguments for "validate everything".
|
2009-09-03 23:22:21 +00:00
|
|
|
|
*/
|
2003-03-21 20:34:02 +00:00
|
|
|
|
static void
|
2009-09-04 17:01:16 +00:00
|
|
|
|
node_validate_rows (GtkFileSystemModel *model, guint up_to_index, guint up_to_row)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
guint i, row;
|
2004-03-29 19:52:16 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->files->len == 0)
|
|
|
|
|
return;
|
2009-09-04 17:01:16 +00:00
|
|
|
|
|
|
|
|
|
up_to_index = MIN (up_to_index, model->files->len - 1);
|
|
|
|
|
|
|
|
|
|
i = model->n_nodes_valid;
|
|
|
|
|
if (i != 0)
|
|
|
|
|
row = get_node (model, i - 1)->row;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
else
|
2009-09-04 17:01:16 +00:00
|
|
|
|
row = 0;
|
|
|
|
|
|
|
|
|
|
while (i <= up_to_index && row <= up_to_row)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
FileModelNode *node = get_node (model, i);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (node->visible)
|
2009-09-04 17:01:16 +00:00
|
|
|
|
row++;
|
|
|
|
|
node->row = row;
|
|
|
|
|
i++;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
2009-09-04 17:01:16 +00:00
|
|
|
|
model->n_nodes_valid = i;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static guint
|
2009-09-04 17:01:16 +00:00
|
|
|
|
node_get_tree_row (GtkFileSystemModel *model, guint index)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
if (model->n_nodes_valid <= index)
|
|
|
|
|
node_validate_rows (model, index, G_MAXUINT);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
return get_node (model, index)->row - 1;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
node_invalidate_index (GtkFileSystemModel *model, guint id)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
model->n_nodes_valid = MIN (model->n_nodes_valid, id);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static GtkTreePath *
|
|
|
|
|
gtk_tree_path_new_from_node (GtkFileSystemModel *model, guint id)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
guint i = node_get_tree_row (model, id);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_assert (i < model->files->len);
|
|
|
|
|
|
|
|
|
|
return gtk_tree_path_new_from_indices (i, -1);
|
|
|
|
|
}
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
2009-09-02 22:10:46 +00:00
|
|
|
|
emit_row_inserted_for_node (GtkFileSystemModel *model, guint id)
|
|
|
|
|
{
|
|
|
|
|
GtkTreePath *path;
|
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
|
|
path = gtk_tree_path_new_from_node (model, id);
|
|
|
|
|
ITER_INIT_FROM_INDEX (model, &iter, id);
|
|
|
|
|
gtk_tree_model_row_inserted (GTK_TREE_MODEL (model), path, &iter);
|
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
emit_row_changed_for_node (GtkFileSystemModel *model, guint id)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
{
|
|
|
|
|
GtkTreePath *path;
|
|
|
|
|
GtkTreeIter iter;
|
2004-01-22 02:39:31 +00:00
|
|
|
|
|
2009-09-02 22:10:46 +00:00
|
|
|
|
path = gtk_tree_path_new_from_node (model, id);
|
|
|
|
|
ITER_INIT_FROM_INDEX (model, &iter, id);
|
|
|
|
|
gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, &iter);
|
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-09-04 17:01:16 +00:00
|
|
|
|
emit_row_deleted_for_row (GtkFileSystemModel *model, guint row)
|
2009-09-02 22:10:46 +00:00
|
|
|
|
{
|
|
|
|
|
GtkTreePath *path;
|
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
path = gtk_tree_path_new_from_indices (row, -1);
|
2009-09-02 22:10:46 +00:00
|
|
|
|
gtk_tree_model_row_deleted (GTK_TREE_MODEL (model), path);
|
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
node_set_visible (GtkFileSystemModel *model, guint id, gboolean visible)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node = get_node (model, id);
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (node->visible == visible ||
|
|
|
|
|
node->frozen_add)
|
|
|
|
|
return;
|
2004-01-22 02:39:31 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (visible)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node->visible = TRUE;
|
|
|
|
|
node_invalidate_index (model, id);
|
2009-09-02 22:10:46 +00:00
|
|
|
|
emit_row_inserted_for_node (model, id);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
guint row;
|
2009-09-02 22:10:46 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
row = node_get_tree_row (model, id);
|
|
|
|
|
g_assert (row < model->files->len);
|
2009-09-02 22:10:46 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node->visible = FALSE;
|
|
|
|
|
node_invalidate_index (model, id);
|
2009-09-04 17:01:16 +00:00
|
|
|
|
emit_row_deleted_for_row (model, row);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
node_should_be_visible (GtkFileSystemModel *model, guint id)
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
FileModelNode *node = get_node (model, id);
|
2009-06-24 14:36:20 +00:00
|
|
|
|
GtkFileFilterInfo filter_info = { 0, };
|
|
|
|
|
GtkFileFilterFlags required;
|
|
|
|
|
gboolean is_folder, result;
|
|
|
|
|
char *mime_type = NULL;
|
|
|
|
|
char *filename = NULL;
|
|
|
|
|
char *uri = NULL;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (node->info == NULL)
|
|
|
|
|
return FALSE;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (!model->show_hidden &&
|
|
|
|
|
(g_file_info_get_is_hidden (node->info) || g_file_info_get_is_backup (node->info)))
|
|
|
|
|
return FALSE;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
is_folder = _gtk_file_info_consider_as_directory (node->info);
|
|
|
|
|
|
|
|
|
|
/* wtf? */
|
|
|
|
|
if (model->show_folders != model->show_files &&
|
|
|
|
|
model->show_folders != is_folder)
|
|
|
|
|
return FALSE;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-24 14:36:20 +00:00
|
|
|
|
if (is_folder)
|
|
|
|
|
return TRUE;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-06-24 14:36:20 +00:00
|
|
|
|
if (model->filter == NULL)
|
|
|
|
|
return TRUE;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-06-24 14:36:20 +00:00
|
|
|
|
/* fill info */
|
|
|
|
|
required = gtk_file_filter_get_needed (model->filter);
|
|
|
|
|
|
|
|
|
|
filter_info.contains = GTK_FILE_FILTER_DISPLAY_NAME;
|
|
|
|
|
filter_info.display_name = g_file_info_get_display_name (node->info);
|
|
|
|
|
|
|
|
|
|
if (required & GTK_FILE_FILTER_MIME_TYPE)
|
|
|
|
|
{
|
2009-09-03 17:09:09 +00:00
|
|
|
|
const char *s = g_file_info_get_content_type (node->info);
|
|
|
|
|
if (s)
|
|
|
|
|
{
|
|
|
|
|
mime_type = g_content_type_get_mime_type (s);
|
|
|
|
|
if (mime_type)
|
|
|
|
|
{
|
|
|
|
|
filter_info.mime_type = mime_type;
|
|
|
|
|
filter_info.contains |= GTK_FILE_FILTER_MIME_TYPE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-24 14:36:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (required & GTK_FILE_FILTER_FILENAME)
|
|
|
|
|
{
|
|
|
|
|
filename = g_file_get_path (node->file);
|
2009-09-02 23:26:00 +00:00
|
|
|
|
if (filename)
|
2009-06-24 14:36:20 +00:00
|
|
|
|
{
|
|
|
|
|
filter_info.filename = filename;
|
|
|
|
|
filter_info.contains |= GTK_FILE_FILTER_FILENAME;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (required & GTK_FILE_FILTER_URI)
|
|
|
|
|
{
|
|
|
|
|
uri = g_file_get_uri (node->file);
|
|
|
|
|
if (uri)
|
|
|
|
|
{
|
|
|
|
|
filter_info.uri = uri;
|
|
|
|
|
filter_info.contains |= GTK_FILE_FILTER_URI;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = gtk_file_filter_filter (model->filter, &filter_info);
|
|
|
|
|
|
|
|
|
|
g_free (mime_type);
|
|
|
|
|
g_free (filename);
|
|
|
|
|
g_free (uri);
|
|
|
|
|
|
|
|
|
|
return result;
|
2004-02-25 03:03:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** GtkTreeModel ***/
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
|
|
|
|
static GtkTreeModelFlags
|
|
|
|
|
gtk_file_system_model_get_flags (GtkTreeModel *tree_model)
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* GTK_TREE_MODEL_ITERS_PERSIST doesn't work with arrays :( */
|
|
|
|
|
return GTK_TREE_MODEL_LIST_ONLY;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
|
|
|
|
gtk_file_system_model_get_n_columns (GtkTreeModel *tree_model)
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
|
|
|
|
|
|
|
|
|
return model->n_columns;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static GType
|
|
|
|
|
gtk_file_system_model_get_column_type (GtkTreeModel *tree_model,
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gint i)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (i >= 0 && (guint) i < model->n_columns, G_TYPE_NONE);
|
|
|
|
|
|
|
|
|
|
return model->column_types[i];
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static int
|
|
|
|
|
compare_indices (gconstpointer key, gconstpointer _node)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
const FileModelNode *node = _node;
|
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
return GPOINTER_TO_UINT (key) - node->row;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_iter_nth_child (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
|
GtkTreeIter *parent,
|
|
|
|
|
gint n)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
|
|
|
|
char *node;
|
2009-09-04 17:01:16 +00:00
|
|
|
|
guint id;
|
|
|
|
|
guint row_to_find;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_val_if_fail (n >= 0, FALSE);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (parent != NULL)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
row_to_find = n + 1; /* plus one as our node->row numbers are 1-based; see the "Structure" comment at the beginning */
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
if (model->n_nodes_valid > 0 &&
|
|
|
|
|
get_node (model, model->n_nodes_valid - 1)->row >= row_to_find)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
/* Fast path - the nodes are valid up to the sought one.
|
|
|
|
|
*
|
|
|
|
|
* First, find a node with the sought row number...*/
|
|
|
|
|
|
|
|
|
|
node = bsearch (GUINT_TO_POINTER (row_to_find),
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->files->data,
|
2009-09-04 17:01:16 +00:00
|
|
|
|
model->n_nodes_valid,
|
2009-08-26 23:42:49 +00:00
|
|
|
|
model->node_size,
|
2009-06-30 13:15:55 +00:00
|
|
|
|
compare_indices);
|
|
|
|
|
if (node == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
/* ... Second, back up until we find the first visible node with that row number */
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
id = node_index (model, node);
|
|
|
|
|
while (!get_node (model, id)->visible)
|
|
|
|
|
id--;
|
2009-09-04 17:01:16 +00:00
|
|
|
|
|
|
|
|
|
g_assert (get_node (model, id)->row == row_to_find);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-09-04 17:01:16 +00:00
|
|
|
|
/* Slow path - the nodes need to be validated up to the sought one */
|
|
|
|
|
|
2009-09-04 17:31:37 +00:00
|
|
|
|
node_validate_rows (model, G_MAXUINT, n); /* note that this is really "n", not row_to_find - see node_validate_rows() */
|
2009-09-04 17:01:16 +00:00
|
|
|
|
id = model->n_nodes_valid - 1;
|
|
|
|
|
if (model->n_nodes_valid == 0 || get_node (model, id)->row != row_to_find)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return FALSE;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
ITER_INIT_FROM_INDEX (model, iter, id);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_get_iter (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
|
GtkTreePath *path)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (gtk_tree_path_get_depth (path) > 0, FALSE);
|
|
|
|
|
|
|
|
|
|
return gtk_file_system_model_iter_nth_child (tree_model,
|
|
|
|
|
iter,
|
|
|
|
|
NULL,
|
2010-06-28 18:15:10 +00:00
|
|
|
|
gtk_tree_path_get_indices (path)[0]);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
|
static GtkTreePath *
|
|
|
|
|
gtk_file_system_model_get_path (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_val_if_fail (ITER_IS_VALID (model, iter), NULL);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return gtk_tree_path_new_from_node (model, ITER_INDEX (iter));
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_get_value (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
|
gint column,
|
|
|
|
|
GValue *value)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
const GValue *original;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail ((guint) column < model->n_columns);
|
|
|
|
|
g_return_if_fail (ITER_IS_VALID (model, iter));
|
2004-01-17 04:34:49 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
original = _gtk_file_system_model_get_value (model, iter, column);
|
|
|
|
|
if (original)
|
|
|
|
|
{
|
|
|
|
|
g_value_init (value, G_VALUE_TYPE (original));
|
|
|
|
|
g_value_copy (original, value);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
else
|
|
|
|
|
g_value_init (value, model->column_types[column]);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_iter_next (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
|
|
|
|
guint i;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_val_if_fail (ITER_IS_VALID (model, iter), FALSE);
|
|
|
|
|
|
|
|
|
|
for (i = ITER_INDEX (iter) + 1; i < model->files->len; i++)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node = get_node (model, i);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (node->visible)
|
|
|
|
|
{
|
|
|
|
|
ITER_INIT_FROM_INDEX (model, iter, i);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return FALSE;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_iter_children (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
|
GtkTreeIter *parent)
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return FALSE;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_iter_has_child (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return FALSE;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gint
|
|
|
|
|
gtk_file_system_model_iter_n_children (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (tree_model);
|
|
|
|
|
|
|
|
|
|
if (iter)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return 0;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
return node_get_tree_row (model, model->files->len - 1) + 1;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_iter_parent (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
|
GtkTreeIter *child)
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return FALSE;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_ref_node (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
2009-09-04 17:49:53 +00:00
|
|
|
|
/* nothing to do */
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_unref_node (GtkTreeModel *tree_model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
2009-09-04 17:49:53 +00:00
|
|
|
|
/* nothing to do */
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_iface_init (GtkTreeModelIface *iface)
|
2004-02-25 03:03:11 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
iface->get_flags = gtk_file_system_model_get_flags;
|
|
|
|
|
iface->get_n_columns = gtk_file_system_model_get_n_columns;
|
|
|
|
|
iface->get_column_type = gtk_file_system_model_get_column_type;
|
|
|
|
|
iface->get_iter = gtk_file_system_model_get_iter;
|
|
|
|
|
iface->get_path = gtk_file_system_model_get_path;
|
|
|
|
|
iface->get_value = gtk_file_system_model_get_value;
|
|
|
|
|
iface->iter_next = gtk_file_system_model_iter_next;
|
|
|
|
|
iface->iter_children = gtk_file_system_model_iter_children;
|
|
|
|
|
iface->iter_has_child = gtk_file_system_model_iter_has_child;
|
|
|
|
|
iface->iter_n_children = gtk_file_system_model_iter_n_children;
|
|
|
|
|
iface->iter_nth_child = gtk_file_system_model_iter_nth_child;
|
|
|
|
|
iface->iter_parent = gtk_file_system_model_iter_parent;
|
|
|
|
|
iface->ref_node = gtk_file_system_model_ref_node;
|
|
|
|
|
iface->unref_node = gtk_file_system_model_unref_node;
|
|
|
|
|
}
|
2004-02-25 03:03:11 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** GtkTreeSortable ***/
|
2004-02-25 03:03:11 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
typedef struct _SortData SortData;
|
|
|
|
|
struct _SortData {
|
|
|
|
|
GtkFileSystemModel * model;
|
|
|
|
|
GtkTreeIterCompareFunc func;
|
|
|
|
|
gpointer data;
|
|
|
|
|
int order; /* -1 to invert sort order or 1 to keep it */
|
|
|
|
|
};
|
2004-02-25 03:03:11 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* returns FALSE if no sort necessary */
|
|
|
|
|
static gboolean
|
|
|
|
|
sort_data_init (SortData *data, GtkFileSystemModel *model)
|
|
|
|
|
{
|
|
|
|
|
GtkTreeDataSortHeader *header;
|
2004-02-25 03:03:11 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->files->len <= 2)
|
2004-02-25 03:03:11 +00:00
|
|
|
|
return FALSE;
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
switch (model->sort_column_id)
|
|
|
|
|
{
|
|
|
|
|
case GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID:
|
|
|
|
|
if (!model->default_sort_func)
|
|
|
|
|
return FALSE;
|
|
|
|
|
data->func = model->default_sort_func;
|
|
|
|
|
data->data = model->default_sort_data;
|
|
|
|
|
break;
|
|
|
|
|
case GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID:
|
|
|
|
|
return FALSE;
|
|
|
|
|
default:
|
|
|
|
|
header = _gtk_tree_data_list_get_header (model->sort_list, model->sort_column_id);
|
|
|
|
|
if (header == NULL)
|
|
|
|
|
return FALSE;
|
|
|
|
|
data->func = header->func;
|
|
|
|
|
data->data = header->data;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2004-02-25 03:03:11 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
data->order = model->sort_order == GTK_SORT_DESCENDING ? -1 : 1;
|
|
|
|
|
data->model = model;
|
2004-02-25 03:03:11 +00:00
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static int
|
|
|
|
|
compare_array_element (gconstpointer a, gconstpointer b, gpointer user_data)
|
2004-03-29 19:52:16 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
SortData *data = user_data;
|
|
|
|
|
GtkTreeIter itera, iterb;
|
|
|
|
|
|
|
|
|
|
ITER_INIT_FROM_INDEX (data->model, &itera, node_index (data->model, a));
|
|
|
|
|
ITER_INIT_FROM_INDEX (data->model, &iterb, node_index (data->model, b));
|
|
|
|
|
return data->func (GTK_TREE_MODEL (data->model), &itera, &iterb, data->data) * data->order;
|
2004-03-29 19:52:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-05-01 21:41:12 +00:00
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_sort (GtkFileSystemModel *model)
|
2004-03-29 19:52:16 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
SortData data;
|
2004-03-29 19:52:16 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->frozen)
|
|
|
|
|
{
|
|
|
|
|
model->sort_on_thaw = TRUE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (sort_data_init (&data, model))
|
|
|
|
|
{
|
|
|
|
|
GtkTreePath *path;
|
2009-09-04 18:06:54 +00:00
|
|
|
|
guint i;
|
|
|
|
|
guint r, n_visible_rows;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-09-04 17:01:16 +00:00
|
|
|
|
node_validate_rows (model, G_MAXUINT, G_MAXUINT);
|
2009-09-04 18:06:54 +00:00
|
|
|
|
n_visible_rows = node_get_tree_row (model, model->files->len - 1) + 1;
|
2009-09-04 17:01:16 +00:00
|
|
|
|
model->n_nodes_valid = 0;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_hash_table_remove_all (model->file_lookup);
|
2009-09-04 18:06:54 +00:00
|
|
|
|
g_qsort_with_data (get_node (model, 1), /* start at index 1; don't sort the editable row */
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->files->len - 1,
|
2009-08-26 23:42:49 +00:00
|
|
|
|
model->node_size,
|
2009-06-30 13:15:55 +00:00
|
|
|
|
compare_array_element,
|
|
|
|
|
&data);
|
2009-09-04 17:01:16 +00:00
|
|
|
|
g_assert (model->n_nodes_valid == 0);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_assert (g_hash_table_size (model->file_lookup) == 0);
|
2009-09-04 18:06:54 +00:00
|
|
|
|
if (n_visible_rows)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
{
|
2009-09-04 18:06:54 +00:00
|
|
|
|
int *new_order = g_new (int, n_visible_rows);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-09-04 18:06:54 +00:00
|
|
|
|
r = 0;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
for (i = 0; i < model->files->len; i++)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node = get_node (model, i);
|
|
|
|
|
if (!node->visible)
|
|
|
|
|
{
|
2009-09-04 18:06:54 +00:00
|
|
|
|
node->row = r;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-16 10:41:40 +00:00
|
|
|
|
new_order[r] = node->row - 1;
|
2009-09-04 18:06:54 +00:00
|
|
|
|
r++;
|
|
|
|
|
node->row = r;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
2009-09-04 18:06:54 +00:00
|
|
|
|
g_assert (r == n_visible_rows);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
path = gtk_tree_path_new ();
|
|
|
|
|
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (model),
|
|
|
|
|
path,
|
|
|
|
|
NULL,
|
|
|
|
|
new_order);
|
|
|
|
|
gtk_tree_path_free (path);
|
|
|
|
|
g_free (new_order);
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->sort_on_thaw = FALSE;
|
2004-03-29 19:52:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_sort_node (GtkFileSystemModel *model, guint node)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* FIXME: improve */
|
|
|
|
|
gtk_file_system_model_sort (model);
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_get_sort_column_id (GtkTreeSortable *sortable,
|
|
|
|
|
gint *sort_column_id,
|
|
|
|
|
GtkSortType *order)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (sort_column_id)
|
|
|
|
|
*sort_column_id = model->sort_column_id;
|
|
|
|
|
if (order)
|
|
|
|
|
*order = model->sort_order;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
|
|
|
|
|
model->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
|
|
|
|
|
return FALSE;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return TRUE;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_set_sort_column_id (GtkTreeSortable *sortable,
|
|
|
|
|
gint sort_column_id,
|
|
|
|
|
GtkSortType order)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if ((model->sort_column_id == sort_column_id) &&
|
|
|
|
|
(model->sort_order == order))
|
2003-03-21 20:34:02 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkTreeDataSortHeader *header = NULL;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
header = _gtk_tree_data_list_get_header (model->sort_list,
|
|
|
|
|
sort_column_id);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* We want to make sure that we have a function */
|
|
|
|
|
g_return_if_fail (header != NULL);
|
|
|
|
|
g_return_if_fail (header->func != NULL);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (model->default_sort_func != NULL);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
model->sort_column_id = sort_column_id;
|
|
|
|
|
model->sort_order = order;
|
|
|
|
|
|
|
|
|
|
gtk_tree_sortable_sort_column_changed (sortable);
|
|
|
|
|
|
|
|
|
|
gtk_file_system_model_sort (model);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-23 15:31:10 +00:00
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_set_sort_func (GtkTreeSortable *sortable,
|
|
|
|
|
gint sort_column_id,
|
|
|
|
|
GtkTreeIterCompareFunc func,
|
|
|
|
|
gpointer data,
|
|
|
|
|
GDestroyNotify destroy)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
|
|
|
|
|
|
|
|
|
|
model->sort_list = _gtk_tree_data_list_set_header (model->sort_list,
|
|
|
|
|
sort_column_id,
|
|
|
|
|
func, data, destroy);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->sort_column_id == sort_column_id)
|
|
|
|
|
gtk_file_system_model_sort (model);
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_set_default_sort_func (GtkTreeSortable *sortable,
|
|
|
|
|
GtkTreeIterCompareFunc func,
|
|
|
|
|
gpointer data,
|
|
|
|
|
GDestroyNotify destroy)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->default_sort_destroy)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GDestroyNotify d = model->default_sort_destroy;
|
|
|
|
|
|
|
|
|
|
model->default_sort_destroy = NULL;
|
|
|
|
|
d (model->default_sort_data);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->default_sort_func = func;
|
|
|
|
|
model->default_sort_data = data;
|
|
|
|
|
model->default_sort_destroy = destroy;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
|
|
|
|
|
gtk_file_system_model_sort (model);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
gtk_file_system_model_has_default_sort_func (GtkTreeSortable *sortable)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (sortable);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return (model->default_sort_func != NULL);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_sortable_init (GtkTreeSortableIface *iface)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
iface->get_sort_column_id = gtk_file_system_model_get_sort_column_id;
|
|
|
|
|
iface->set_sort_column_id = gtk_file_system_model_set_sort_column_id;
|
|
|
|
|
iface->set_sort_func = gtk_file_system_model_set_sort_func;
|
|
|
|
|
iface->set_default_sort_func = gtk_file_system_model_set_default_sort_func;
|
|
|
|
|
iface->has_default_sort_func = gtk_file_system_model_has_default_sort_func;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** GtkTreeDragSource ***/
|
|
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
|
drag_source_row_draggable (GtkTreeDragSource *drag_source,
|
|
|
|
|
GtkTreePath *path)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (drag_source);
|
|
|
|
|
GtkTreeIter iter;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
|
|
|
|
|
return FALSE;
|
2004-01-17 04:34:49 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return ITER_INDEX (&iter) != 0;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
drag_source_drag_data_get (GtkTreeDragSource *drag_source,
|
|
|
|
|
GtkTreePath *path,
|
|
|
|
|
GtkSelectionData *selection_data)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (drag_source);
|
|
|
|
|
FileModelNode *node;
|
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
char *uris[2];
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (!gtk_file_system_model_get_iter (GTK_TREE_MODEL (model), &iter, path))
|
|
|
|
|
return FALSE;
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node = get_node (model, ITER_INDEX (&iter));
|
|
|
|
|
if (node->file == NULL)
|
|
|
|
|
return FALSE;
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
uris[0] = g_file_get_uri (node->file);
|
|
|
|
|
uris[1] = NULL;
|
|
|
|
|
gtk_selection_data_set_uris (selection_data, uris);
|
|
|
|
|
g_free (uris[0]);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return TRUE;
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
drag_source_iface_init (GtkTreeDragSourceIface *iface)
|
2003-07-23 15:31:10 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
iface->row_draggable = drag_source_row_draggable;
|
|
|
|
|
iface->drag_data_get = drag_source_drag_data_get;
|
|
|
|
|
iface->drag_data_delete = NULL;
|
2003-07-23 15:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** GtkFileSystemModel ***/
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* Signal IDs */
|
|
|
|
|
enum {
|
|
|
|
|
FINISHED_LOADING,
|
|
|
|
|
LAST_SIGNAL
|
2006-05-01 21:41:12 +00:00
|
|
|
|
};
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static guint file_system_model_signals[LAST_SIGNAL] = { 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkFileSystemModel, _gtk_file_system_model, G_TYPE_OBJECT,
|
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
|
|
|
|
|
gtk_file_system_model_iface_init)
|
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
|
|
|
|
|
gtk_file_system_model_sortable_init)
|
|
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
|
|
|
|
|
drag_source_iface_init))
|
|
|
|
|
|
2006-05-01 21:41:12 +00:00
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_dispose (GObject *object)
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
|
|
|
|
|
|
2009-08-31 20:53:45 +00:00
|
|
|
|
if (model->dir_thaw_source)
|
|
|
|
|
{
|
|
|
|
|
g_source_remove (model->dir_thaw_source);
|
|
|
|
|
model->dir_thaw_source = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_cancellable_cancel (model->cancellable);
|
|
|
|
|
if (model->dir_monitor)
|
|
|
|
|
g_file_monitor_cancel (model->dir_monitor);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->dispose (object);
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_finalize (GObject *object)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = GTK_FILE_SYSTEM_MODEL (object);
|
|
|
|
|
guint i;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
for (i = 0; i < model->files->len; i++)
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-09-04 18:42:46 +00:00
|
|
|
|
int v;
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
FileModelNode *node = get_node (model, i);
|
|
|
|
|
if (node->file)
|
|
|
|
|
g_object_unref (node->file);
|
|
|
|
|
if (node->info)
|
|
|
|
|
g_object_unref (node->info);
|
2009-09-04 18:42:46 +00:00
|
|
|
|
|
2009-09-30 23:49:33 +00:00
|
|
|
|
for (v = 0; v < model->n_columns; v++)
|
|
|
|
|
if (G_VALUE_TYPE (&node->values[v]) != G_TYPE_INVALID)
|
2009-09-04 18:42:46 +00:00
|
|
|
|
g_value_unset (&node->values[v]);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
g_array_free (model->files, TRUE);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_object_unref (model->cancellable);
|
|
|
|
|
g_free (model->attributes);
|
|
|
|
|
if (model->dir)
|
|
|
|
|
g_object_unref (model->dir);
|
|
|
|
|
if (model->dir_monitor)
|
|
|
|
|
g_object_unref (model->dir_monitor);
|
|
|
|
|
g_hash_table_destroy (model->file_lookup);
|
2009-06-24 14:36:20 +00:00
|
|
|
|
if (model->filter)
|
|
|
|
|
g_object_unref (model->filter);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-09-30 23:49:33 +00:00
|
|
|
|
g_slice_free1 (sizeof (GType) * model->n_columns, model->column_types);
|
2009-09-04 18:42:46 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
_gtk_tree_data_list_header_free (model->sort_list);
|
|
|
|
|
if (model->default_sort_destroy)
|
|
|
|
|
model->default_sort_destroy (model->default_sort_data);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
G_OBJECT_CLASS (_gtk_file_system_model_parent_class)->finalize (object);
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
_gtk_file_system_model_class_init (GtkFileSystemModelClass *class)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gobject_class->finalize = gtk_file_system_model_finalize;
|
|
|
|
|
gobject_class->dispose = gtk_file_system_model_dispose;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
file_system_model_signals[FINISHED_LOADING] =
|
|
|
|
|
g_signal_new (I_("finished-loading"),
|
|
|
|
|
G_OBJECT_CLASS_TYPE (gobject_class),
|
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
|
G_STRUCT_OFFSET (GtkFileSystemModelClass, finished_loading),
|
|
|
|
|
NULL, NULL,
|
|
|
|
|
_gtk_marshal_VOID__POINTER,
|
|
|
|
|
G_TYPE_NONE, 1, G_TYPE_POINTER);
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
_gtk_file_system_model_init (GtkFileSystemModel *model)
|
|
|
|
|
{
|
|
|
|
|
model->show_files = TRUE;
|
|
|
|
|
model->show_folders = TRUE;
|
|
|
|
|
model->show_hidden = FALSE;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->file_lookup = g_hash_table_new (g_file_hash, (GEqualFunc) g_file_equal);
|
|
|
|
|
model->cancellable = g_cancellable_new ();
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/*** API ***/
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_closed_enumerator (GObject *object, GAsyncResult *res, gpointer data)
|
|
|
|
|
{
|
|
|
|
|
g_file_enumerator_close_finish (G_FILE_ENUMERATOR (object), res, NULL);
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static gboolean
|
|
|
|
|
thaw_func (gpointer data)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model = data;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
_gtk_file_system_model_thaw_updates (model);
|
|
|
|
|
model->dir_thaw_source = 0;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return FALSE;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_got_files (GObject *object, GAsyncResult *res, gpointer data)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GFileEnumerator *enumerator = G_FILE_ENUMERATOR (object);
|
|
|
|
|
GtkFileSystemModel *model = data;
|
|
|
|
|
GList *walk, *files;
|
|
|
|
|
GError *error = NULL;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gdk_threads_enter ();
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
files = g_file_enumerator_next_files_finish (enumerator, res, &error);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (files)
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->dir_thaw_source == 0)
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
_gtk_file_system_model_freeze_updates (model);
|
|
|
|
|
model->dir_thaw_source = gdk_threads_add_timeout_full (IO_PRIORITY + 1,
|
|
|
|
|
50,
|
|
|
|
|
thaw_func,
|
|
|
|
|
model,
|
|
|
|
|
NULL);
|
|
|
|
|
}
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
for (walk = files; walk; walk = walk->next)
|
|
|
|
|
{
|
|
|
|
|
const char *name;
|
|
|
|
|
GFileInfo *info;
|
|
|
|
|
GFile *file;
|
|
|
|
|
|
|
|
|
|
info = walk->data;
|
|
|
|
|
name = g_file_info_get_name (info);
|
|
|
|
|
if (name == NULL)
|
|
|
|
|
{
|
|
|
|
|
/* Shouldn't happen, but the APIs allow it */
|
|
|
|
|
g_object_unref (info);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
file = g_file_get_child (model->dir, name);
|
2009-08-31 20:08:43 +00:00
|
|
|
|
add_file (model, file, info);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_object_unref (file);
|
|
|
|
|
g_object_unref (info);
|
|
|
|
|
}
|
|
|
|
|
g_list_free (files);
|
2003-04-09 16:52:13 +00:00
|
|
|
|
|
2009-08-31 21:13:05 +00:00
|
|
|
|
g_file_enumerator_next_files_async (enumerator,
|
|
|
|
|
g_file_is_native (model->dir) ? 50 * FILES_PER_QUERY : FILES_PER_QUERY,
|
|
|
|
|
IO_PRIORITY,
|
|
|
|
|
model->cancellable,
|
|
|
|
|
gtk_file_system_model_got_files,
|
|
|
|
|
model);
|
|
|
|
|
}
|
|
|
|
|
else
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-11-02 18:49:11 +00:00
|
|
|
|
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
2009-11-02 18:49:11 +00:00
|
|
|
|
g_file_enumerator_close_async (enumerator,
|
|
|
|
|
IO_PRIORITY,
|
|
|
|
|
model->cancellable,
|
|
|
|
|
gtk_file_system_model_closed_enumerator,
|
|
|
|
|
NULL);
|
|
|
|
|
if (model->dir_thaw_source != 0)
|
|
|
|
|
{
|
|
|
|
|
g_source_remove (model->dir_thaw_source);
|
|
|
|
|
model->dir_thaw_source = 0;
|
|
|
|
|
_gtk_file_system_model_thaw_updates (model);
|
|
|
|
|
}
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-11-02 18:49:11 +00:00
|
|
|
|
g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0, error);
|
|
|
|
|
}
|
2009-08-31 21:13:05 +00:00
|
|
|
|
|
2009-07-01 08:32:26 +00:00
|
|
|
|
if (error)
|
|
|
|
|
g_error_free (error);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
}
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gdk_threads_leave ();
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_query_done (GObject * object,
|
|
|
|
|
GAsyncResult *res,
|
|
|
|
|
gpointer data)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = data; /* only a valid pointer if not cancelled */
|
|
|
|
|
GFile *file = G_FILE (object);
|
|
|
|
|
GFileInfo *info;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
info = g_file_query_info_finish (file, res, NULL);
|
|
|
|
|
if (info == NULL)
|
|
|
|
|
return;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2011-03-25 09:53:05 +00:00
|
|
|
|
gdk_threads_enter ();
|
2009-06-30 13:15:55 +00:00
|
|
|
|
_gtk_file_system_model_update_file (model, file, info, TRUE);
|
2011-03-25 09:53:05 +00:00
|
|
|
|
gdk_threads_leave ();
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_monitor_change (GFileMonitor * monitor,
|
|
|
|
|
GFile * file,
|
|
|
|
|
GFile * other_file,
|
|
|
|
|
GFileMonitorEvent type,
|
|
|
|
|
GtkFileSystemModel *model)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
switch (type)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
case G_FILE_MONITOR_EVENT_CREATED:
|
|
|
|
|
case G_FILE_MONITOR_EVENT_CHANGED:
|
|
|
|
|
case G_FILE_MONITOR_EVENT_ATTRIBUTE_CHANGED:
|
|
|
|
|
/* We can treat all of these the same way */
|
|
|
|
|
g_file_query_info_async (file,
|
|
|
|
|
model->attributes,
|
2009-09-02 18:54:52 +00:00
|
|
|
|
G_FILE_QUERY_INFO_NONE,
|
2009-06-30 13:15:55 +00:00
|
|
|
|
IO_PRIORITY,
|
|
|
|
|
model->cancellable,
|
|
|
|
|
gtk_file_system_model_query_done,
|
|
|
|
|
model);
|
|
|
|
|
break;
|
|
|
|
|
case G_FILE_MONITOR_EVENT_DELETED:
|
2011-03-25 09:53:05 +00:00
|
|
|
|
gdk_threads_enter ();
|
2009-08-31 20:11:44 +00:00
|
|
|
|
remove_file (model, file);
|
2011-03-25 09:53:05 +00:00
|
|
|
|
gdk_threads_leave ();
|
2009-06-30 13:15:55 +00:00
|
|
|
|
break;
|
|
|
|
|
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
|
|
|
|
|
/* FIXME: use freeze/thaw with this somehow? */
|
|
|
|
|
case G_FILE_MONITOR_EVENT_PRE_UNMOUNT:
|
|
|
|
|
case G_FILE_MONITOR_EVENT_UNMOUNTED:
|
|
|
|
|
default:
|
|
|
|
|
/* ignore these */
|
|
|
|
|
break;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_got_enumerator (GObject *dir, GAsyncResult *res, gpointer data)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GtkFileSystemModel *model = data;
|
|
|
|
|
GFileEnumerator *enumerator;
|
|
|
|
|
GError *error = NULL;
|
2004-03-01 19:48:28 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gdk_threads_enter ();
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
enumerator = g_file_enumerate_children_finish (G_FILE (dir), res, &error);
|
|
|
|
|
if (enumerator == NULL)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2010-03-29 14:26:14 +00:00
|
|
|
|
if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
|
|
|
|
|
{
|
|
|
|
|
g_signal_emit (model, file_system_model_signals[FINISHED_LOADING], 0, error);
|
|
|
|
|
g_error_free (error);
|
|
|
|
|
}
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
else
|
2003-04-09 16:52:13 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_file_enumerator_next_files_async (enumerator,
|
|
|
|
|
g_file_is_native (model->dir) ? 50 * FILES_PER_QUERY : FILES_PER_QUERY,
|
|
|
|
|
IO_PRIORITY,
|
|
|
|
|
model->cancellable,
|
|
|
|
|
gtk_file_system_model_got_files,
|
|
|
|
|
model);
|
|
|
|
|
g_object_unref (enumerator);
|
|
|
|
|
model->dir_monitor = g_file_monitor_directory (model->dir,
|
2009-08-27 00:25:18 +00:00
|
|
|
|
G_FILE_MONITOR_NONE,
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->cancellable,
|
2009-08-27 00:25:18 +00:00
|
|
|
|
NULL); /* we don't mind if directory monitoring isn't supported, so the GError is NULL here */
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->dir_monitor)
|
|
|
|
|
g_signal_connect (model->dir_monitor,
|
|
|
|
|
"changed",
|
|
|
|
|
G_CALLBACK (gtk_file_system_model_monitor_change),
|
|
|
|
|
model);
|
2003-04-09 16:52:13 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
gdk_threads_leave ();
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_set_n_columns (GtkFileSystemModel *model,
|
|
|
|
|
gint n_columns,
|
|
|
|
|
va_list args)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
guint i;
|
2003-04-09 16:52:13 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_assert (model->files == NULL);
|
2009-08-26 23:38:03 +00:00
|
|
|
|
g_assert (n_columns > 0);
|
2003-04-09 16:52:13 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->n_columns = n_columns;
|
2009-08-26 23:42:49 +00:00
|
|
|
|
model->column_types = g_slice_alloc (sizeof (GType) * n_columns);
|
|
|
|
|
|
|
|
|
|
model->node_size = sizeof (FileModelNode) + sizeof (GValue) * (n_columns - 1); /* minus 1 because FileModelNode.values[] has a default size of 1 */
|
2003-04-09 16:52:13 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
for (i = 0; i < (guint) n_columns; i++)
|
2003-04-09 16:52:13 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GType type = va_arg (args, GType);
|
|
|
|
|
if (! _gtk_tree_data_list_check_type (type))
|
2003-04-09 16:52:13 +00:00
|
|
|
|
{
|
2009-08-26 23:38:03 +00:00
|
|
|
|
g_error ("%s: type %s cannot be a column type for GtkFileSystemModel\n", G_STRLOC, g_type_name (type));
|
|
|
|
|
return; /* not reached */
|
2003-04-09 16:52:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->column_types[i] = type;
|
2003-04-09 16:52:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->sort_list = _gtk_tree_data_list_header_new (n_columns, model->column_types);
|
2003-04-09 16:52:13 +00:00
|
|
|
|
|
2009-08-26 23:52:24 +00:00
|
|
|
|
model->files = g_array_sized_new (FALSE, FALSE, model->node_size, FILES_PER_QUERY);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* add editable node at start */
|
|
|
|
|
g_array_set_size (model->files, 1);
|
2009-08-26 23:42:49 +00:00
|
|
|
|
memset (get_node (model, 0), 0, model->node_size);
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_set_directory (GtkFileSystemModel *model,
|
|
|
|
|
GFile * dir,
|
|
|
|
|
const gchar * attributes)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-08-27 00:10:51 +00:00
|
|
|
|
g_assert (G_IS_FILE (dir));
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
model->dir = g_object_ref (dir);
|
|
|
|
|
model->attributes = g_strdup (attributes);
|
|
|
|
|
|
|
|
|
|
g_file_enumerate_children_async (model->dir,
|
|
|
|
|
attributes,
|
2009-08-27 00:14:07 +00:00
|
|
|
|
G_FILE_QUERY_INFO_NONE,
|
2009-06-30 13:15:55 +00:00
|
|
|
|
IO_PRIORITY,
|
|
|
|
|
model->cancellable,
|
|
|
|
|
gtk_file_system_model_got_enumerator,
|
|
|
|
|
model);
|
2003-04-09 16:52:13 +00:00
|
|
|
|
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-24 16:41:03 +00:00
|
|
|
|
static GtkFileSystemModel *
|
|
|
|
|
_gtk_file_system_model_new_valist (GtkFileSystemModelGetValue get_func,
|
|
|
|
|
gpointer get_data,
|
|
|
|
|
guint n_columns,
|
|
|
|
|
va_list args)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model;
|
|
|
|
|
|
|
|
|
|
model = g_object_new (GTK_TYPE_FILE_SYSTEM_MODEL, NULL);
|
|
|
|
|
model->get_func = get_func;
|
|
|
|
|
model->get_data = get_data;
|
|
|
|
|
|
|
|
|
|
gtk_file_system_model_set_n_columns (model, n_columns, args);
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_new:
|
2009-06-24 16:41:03 +00:00
|
|
|
|
* @get_func: function to call for getting a value
|
|
|
|
|
* @get_data: user data argument passed to @get_func
|
|
|
|
|
* @n_columns: number of columns
|
|
|
|
|
* @...: @n_columns #GType types for the columns
|
|
|
|
|
*
|
|
|
|
|
* Creates a new #GtkFileSystemModel object. You need to add files
|
2009-08-31 20:08:43 +00:00
|
|
|
|
* to the list using _gtk_file_system_model_add_and_query_file()
|
|
|
|
|
* or _gtk_file_system_model_update_file().
|
2009-06-24 16:41:03 +00:00
|
|
|
|
*
|
|
|
|
|
* Return value: the newly created #GtkFileSystemModel
|
|
|
|
|
**/
|
|
|
|
|
GtkFileSystemModel *
|
|
|
|
|
_gtk_file_system_model_new (GtkFileSystemModelGetValue get_func,
|
|
|
|
|
gpointer get_data,
|
|
|
|
|
guint n_columns,
|
|
|
|
|
...)
|
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model;
|
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (get_func != NULL, NULL);
|
|
|
|
|
g_return_val_if_fail (n_columns > 0, NULL);
|
|
|
|
|
|
|
|
|
|
va_start (args, n_columns);
|
|
|
|
|
model = _gtk_file_system_model_new_valist (get_func, get_data, n_columns, args);
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_new_for_directory:
|
2009-06-30 13:15:55 +00:00
|
|
|
|
* @directory: the directory to show.
|
2010-02-19 16:53:17 +00:00
|
|
|
|
* @attributes: (allow-none): attributes to immediately load or %NULL for all
|
2009-08-27 00:10:26 +00:00
|
|
|
|
* @get_func: function that the model should call to query data about a file
|
|
|
|
|
* @get_data: user data to pass to the @get_func
|
|
|
|
|
* @n_columns: number of columns
|
|
|
|
|
* @...: @n_columns #GType types for the columns
|
2009-06-30 13:15:55 +00:00
|
|
|
|
*
|
|
|
|
|
* Creates a new #GtkFileSystemModel object. The #GtkFileSystemModel
|
|
|
|
|
* object wraps the given @directory as a #GtkTreeModel.
|
2009-06-24 16:41:03 +00:00
|
|
|
|
* The model will query the given directory with the given @attributes
|
|
|
|
|
* and add all files inside the directory automatically. If supported,
|
|
|
|
|
* it will also monitor the drectory and update the model's
|
|
|
|
|
* contents to reflect changes, if the @directory supports monitoring.
|
2009-06-30 13:15:55 +00:00
|
|
|
|
*
|
2009-06-24 16:41:03 +00:00
|
|
|
|
* Return value: the newly created #GtkFileSystemModel
|
2009-06-30 13:15:55 +00:00
|
|
|
|
**/
|
|
|
|
|
GtkFileSystemModel *
|
2009-06-24 16:41:03 +00:00
|
|
|
|
_gtk_file_system_model_new_for_directory (GFile * dir,
|
|
|
|
|
const gchar * attributes,
|
|
|
|
|
GtkFileSystemModelGetValue get_func,
|
|
|
|
|
gpointer get_data,
|
|
|
|
|
guint n_columns,
|
|
|
|
|
...)
|
2006-05-01 21:41:12 +00:00
|
|
|
|
{
|
|
|
|
|
GtkFileSystemModel *model;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
va_list args;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_val_if_fail (G_IS_FILE (dir), NULL);
|
2009-06-24 16:41:03 +00:00
|
|
|
|
g_return_val_if_fail (get_func != NULL, NULL);
|
|
|
|
|
g_return_val_if_fail (n_columns > 0, NULL);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
va_start (args, n_columns);
|
2009-06-24 16:41:03 +00:00
|
|
|
|
model = _gtk_file_system_model_new_valist (get_func, get_data, n_columns, args);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
va_end (args);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_set_directory (model, dir, attributes);
|
2006-05-01 21:41:12 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return model;
|
2006-05-01 21:41:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
static void
|
|
|
|
|
gtk_file_system_model_refilter_all (GtkFileSystemModel *model)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
guint i;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (model->frozen)
|
2003-03-21 20:34:02 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->filter_on_thaw = TRUE;
|
|
|
|
|
return;
|
2003-03-21 20:34:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
_gtk_file_system_model_freeze_updates (model);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/* start at index 1, don't change the editable */
|
|
|
|
|
for (i = 1; i < model->files->len; i++)
|
|
|
|
|
{
|
|
|
|
|
node_set_visible (model, i, node_should_be_visible (model, i));
|
|
|
|
|
}
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->filter_on_thaw = FALSE;
|
|
|
|
|
_gtk_file_system_model_thaw_updates (model);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_set_show_hidden:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @show_hidden: whether hidden files should be displayed
|
|
|
|
|
*
|
|
|
|
|
* Sets whether hidden files should be included in the #GtkTreeModel
|
|
|
|
|
* for display.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_set_show_hidden (GtkFileSystemModel *model,
|
|
|
|
|
gboolean show_hidden)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
show_hidden = show_hidden != FALSE;
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (show_hidden != model->show_hidden)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->show_hidden = show_hidden;
|
|
|
|
|
gtk_file_system_model_refilter_all (model);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_set_show_folders:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @show_folders: whether folders should be displayed
|
|
|
|
|
*
|
|
|
|
|
* Sets whether folders should be included in the #GtkTreeModel for
|
|
|
|
|
* display.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_set_show_folders (GtkFileSystemModel *model,
|
|
|
|
|
gboolean show_folders)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
|
|
|
|
|
show_folders = show_folders != FALSE;
|
|
|
|
|
|
|
|
|
|
if (show_folders != model->show_folders)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->show_folders = show_folders;
|
|
|
|
|
gtk_file_system_model_refilter_all (model);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_set_show_files:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @show_files: whether files (as opposed to folders) should
|
|
|
|
|
* be displayed.
|
|
|
|
|
*
|
|
|
|
|
* Sets whether files (as opposed to folders) should be included
|
|
|
|
|
* in the #GtkTreeModel for display.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_set_show_files (GtkFileSystemModel *model,
|
|
|
|
|
gboolean show_files)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
|
|
|
|
|
show_files = show_files != FALSE;
|
|
|
|
|
|
|
|
|
|
if (show_files != model->show_files)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
model->show_files = show_files;
|
|
|
|
|
gtk_file_system_model_refilter_all (model);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-07-01 08:32:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_get_cancellable:
|
|
|
|
|
* @model: the model
|
|
|
|
|
*
|
|
|
|
|
* Gets the cancellable used by the @model. This is the cancellable used
|
|
|
|
|
* internally by the @model that will be cancelled when @model is
|
|
|
|
|
* disposed. So you can use it for operations that should be cancelled
|
|
|
|
|
* when the model goes away.
|
|
|
|
|
*
|
|
|
|
|
* Returns: The cancellable used by @model
|
|
|
|
|
**/
|
2009-06-30 13:15:55 +00:00
|
|
|
|
GCancellable *
|
|
|
|
|
_gtk_file_system_model_get_cancellable (GtkFileSystemModel *model)
|
|
|
|
|
{
|
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return model->cancellable;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 08:32:26 +00:00
|
|
|
|
/**
|
2009-09-10 21:33:37 +00:00
|
|
|
|
* _gtk_file_system_model_iter_is_visible:
|
2009-07-01 08:32:26 +00:00
|
|
|
|
* @model: the model
|
|
|
|
|
* @iter: a valid iterator
|
|
|
|
|
*
|
|
|
|
|
* Checks if the iterator is visible. A visible iterator references
|
|
|
|
|
* a row that is currently exposed using the #GtkTreeModel API. If
|
|
|
|
|
* the iterator is invisible, it references a file that is not shown
|
|
|
|
|
* for some reason, such as being filtered by the current filter or
|
|
|
|
|
* being a hidden file.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if the iterator is visible
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2009-09-10 21:33:37 +00:00
|
|
|
|
_gtk_file_system_model_iter_is_visible (GtkFileSystemModel *model,
|
|
|
|
|
GtkTreeIter *iter)
|
2009-07-01 08:32:26 +00:00
|
|
|
|
{
|
|
|
|
|
FileModelNode *node;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), FALSE);
|
|
|
|
|
g_return_val_if_fail (iter != NULL, FALSE);
|
|
|
|
|
|
|
|
|
|
node = get_node (model, ITER_INDEX (iter));
|
|
|
|
|
return node->visible;
|
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_get_info:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @iter: a #GtkTreeIter pointing to a row of @model
|
|
|
|
|
*
|
|
|
|
|
* Gets the #GFileInfo structure for a particular row
|
|
|
|
|
* of @model.
|
|
|
|
|
*
|
|
|
|
|
* Return value: a #GFileInfo structure. This structure
|
|
|
|
|
* is owned by @model and must not be modified or freed.
|
|
|
|
|
* If you want to keep the information for later use,
|
|
|
|
|
* you must take a reference, since the structure may be
|
|
|
|
|
* freed on later changes to the file system. If you have
|
|
|
|
|
* called _gtk_file_system_model_add_editable() and the @iter
|
|
|
|
|
* corresponds to the row that this function returned, the
|
|
|
|
|
* return value will be NULL.
|
|
|
|
|
**/
|
|
|
|
|
GFileInfo *
|
|
|
|
|
_gtk_file_system_model_get_info (GtkFileSystemModel *model,
|
|
|
|
|
GtkTreeIter *iter)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
|
2009-07-01 08:32:26 +00:00
|
|
|
|
g_return_val_if_fail (iter != NULL, NULL);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node = get_node (model, ITER_INDEX (iter));
|
|
|
|
|
g_assert (node->info == NULL || G_IS_FILE_INFO (node->info));
|
|
|
|
|
return node->info;
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_get_file:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @iter: a #GtkTreeIter pointing to a row of @model
|
|
|
|
|
*
|
|
|
|
|
* Gets the file for a particular row in @model.
|
|
|
|
|
*
|
|
|
|
|
* Return value: the file. This object is owned by @model and
|
|
|
|
|
* or freed. If you want to save the path for later use,
|
|
|
|
|
* you must take a ref, since the object may be freed
|
|
|
|
|
* on later changes to the file system.
|
|
|
|
|
**/
|
|
|
|
|
GFile *
|
|
|
|
|
_gtk_file_system_model_get_file (GtkFileSystemModel *model,
|
|
|
|
|
GtkTreeIter *iter)
|
2003-03-27 00:10:25 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
FileModelNode *node;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node = get_node (model, ITER_INDEX (iter));
|
|
|
|
|
return node->file;
|
|
|
|
|
}
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_get_value:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @iter: a #GtkTreeIter pointing to a row of @model
|
|
|
|
|
* @column: the column to get the value for
|
|
|
|
|
*
|
|
|
|
|
* Gets the value associated with the given row @iter and @column.
|
|
|
|
|
* If no value is available yet and the default value should be used,
|
|
|
|
|
* %NULL is returned.
|
|
|
|
|
* This is a performance optimization for the calls
|
|
|
|
|
* gtk_tree_model_get() or gtk_tree_model_get_value(), which copy
|
|
|
|
|
* the value and spend a considerable amount of time in iterator
|
|
|
|
|
* lookups. Both of which are slow.
|
|
|
|
|
*
|
|
|
|
|
* Returns: a pointer to the actual value as stored in @model or %NULL
|
|
|
|
|
* if no value available yet.
|
|
|
|
|
**/
|
|
|
|
|
const GValue *
|
|
|
|
|
_gtk_file_system_model_get_value (GtkFileSystemModel *model,
|
|
|
|
|
GtkTreeIter * iter,
|
|
|
|
|
int column)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node;
|
|
|
|
|
|
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), NULL);
|
|
|
|
|
g_return_val_if_fail (column >= 0 && (guint) column < model->n_columns, NULL);
|
|
|
|
|
|
|
|
|
|
node = get_node (model, ITER_INDEX (iter));
|
|
|
|
|
|
|
|
|
|
if (!G_VALUE_TYPE (&node->values[column]))
|
2003-03-27 00:10:25 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_value_init (&node->values[column], model->column_types[column]);
|
|
|
|
|
if (!model->get_func (model,
|
|
|
|
|
node->file,
|
|
|
|
|
node->info,
|
|
|
|
|
column,
|
|
|
|
|
&node->values[column],
|
|
|
|
|
model->get_data))
|
|
|
|
|
{
|
|
|
|
|
g_value_unset (&node->values[column]);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2003-03-27 00:10:25 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
return &node->values[column];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static guint
|
|
|
|
|
node_get_for_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile * file)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
i = GPOINTER_TO_UINT (g_hash_table_lookup (model->file_lookup, file));
|
|
|
|
|
if (i != 0)
|
|
|
|
|
return i;
|
|
|
|
|
|
2009-09-02 19:51:02 +00:00
|
|
|
|
/* Node 0 is the editable row and has no associated file or entry in the table, so we start counting from 1.
|
|
|
|
|
*
|
|
|
|
|
* The invariant here is that the files in model->files[n] for n < g_hash_table_size (model->file_lookup)
|
|
|
|
|
* are already added to the hash table. The table can get cleared when we re-sort; this loop merely rebuilds
|
|
|
|
|
* our (file -> index) mapping on demand.
|
|
|
|
|
*
|
|
|
|
|
* If we exit the loop, the next pending batch of mappings will be resolved when this function gets called again
|
|
|
|
|
* with another file that is not yet in the mapping.
|
|
|
|
|
*/
|
2009-06-30 13:15:55 +00:00
|
|
|
|
for (i = g_hash_table_size (model->file_lookup) + 1; i < model->files->len; i++)
|
2003-03-27 00:10:25 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
FileModelNode *node = get_node (model, i);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_hash_table_insert (model->file_lookup, node->file, GUINT_TO_POINTER (i));
|
|
|
|
|
if (g_file_equal (node->file, file))
|
|
|
|
|
return i;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
return 0;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 08:32:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_get_iter_for_file:
|
|
|
|
|
* @model: the model
|
|
|
|
|
* @iter: the iterator to be initialized
|
|
|
|
|
* @file: the file to look up
|
|
|
|
|
*
|
|
|
|
|
* Initializes @iter to point to the row used for @file, if @file is part
|
|
|
|
|
* of the model. Note that upon successful return, @iter may point to an
|
|
|
|
|
* invisible row in the @model. Use
|
2009-09-10 21:33:37 +00:00
|
|
|
|
* _gtk_file_system_model_iter_is_visible() to make sure it is visible to
|
2009-07-01 08:32:26 +00:00
|
|
|
|
* the tree view.
|
|
|
|
|
*
|
|
|
|
|
* Returns: %TRUE if file is part of the model and @iter was initialized
|
|
|
|
|
**/
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gboolean
|
|
|
|
|
_gtk_file_system_model_get_iter_for_file (GtkFileSystemModel *model,
|
|
|
|
|
GtkTreeIter *iter,
|
|
|
|
|
GFile * file)
|
2003-03-27 00:10:25 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
guint i;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_val_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model), FALSE);
|
|
|
|
|
g_return_val_if_fail (iter != NULL, FALSE);
|
|
|
|
|
g_return_val_if_fail (G_IS_FILE (file), FALSE);
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
i = node_get_for_file (model, file);
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (i == 0)
|
|
|
|
|
return FALSE;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
ITER_INIT_FROM_INDEX (model, iter, i);
|
|
|
|
|
return TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2009-08-31 20:08:43 +00:00
|
|
|
|
* add_file:
|
2009-06-30 13:15:55 +00:00
|
|
|
|
* @model: the model
|
|
|
|
|
* @file: the file to add
|
|
|
|
|
* @info: the information to associate with the file
|
|
|
|
|
*
|
|
|
|
|
* Adds the given @file with its associated @info to the @model.
|
|
|
|
|
* If the model is frozen, the file will only show up after it is thawn.
|
|
|
|
|
**/
|
2009-08-31 20:08:43 +00:00
|
|
|
|
static void
|
|
|
|
|
add_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile *file,
|
|
|
|
|
GFileInfo *info)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
{
|
|
|
|
|
FileModelNode *node;
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (G_IS_FILE (file));
|
|
|
|
|
g_return_if_fail (G_IS_FILE_INFO (info));
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-08-26 23:42:49 +00:00
|
|
|
|
node = g_slice_alloc0 (model->node_size);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node->file = g_object_ref (file);
|
|
|
|
|
if (info)
|
|
|
|
|
node->info = g_object_ref (info);
|
|
|
|
|
node->frozen_add = model->frozen ? TRUE : FALSE;
|
2008-06-10 00:39:35 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_array_append_vals (model->files, node, 1);
|
2009-08-26 23:42:49 +00:00
|
|
|
|
g_slice_free1 (model->node_size, node);
|
2003-03-27 00:10:25 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (!model->frozen)
|
|
|
|
|
node_set_visible (model, model->files->len -1,
|
|
|
|
|
node_should_be_visible (model, model->files->len - 1));
|
|
|
|
|
gtk_file_system_model_sort_node (model, model->files->len -1);
|
2003-03-27 00:10:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
2009-08-31 20:11:44 +00:00
|
|
|
|
* remove_file:
|
2009-06-30 13:15:55 +00:00
|
|
|
|
* @model: the model
|
|
|
|
|
* @file: file to remove from the model. The file must have been
|
|
|
|
|
* added to the model previously
|
|
|
|
|
*
|
|
|
|
|
* Removes the given file from the model. If the file is not part of
|
|
|
|
|
* @model, this function does nothing.
|
|
|
|
|
**/
|
2009-08-31 20:11:44 +00:00
|
|
|
|
static void
|
|
|
|
|
remove_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile *file)
|
2003-03-27 00:10:25 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
FileModelNode *node;
|
|
|
|
|
guint id;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (G_IS_FILE (file));
|
|
|
|
|
|
|
|
|
|
id = node_get_for_file (model, file);
|
|
|
|
|
if (id == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
node = get_node (model, id);
|
|
|
|
|
node_set_visible (model, id, FALSE);
|
2009-09-02 20:21:45 +00:00
|
|
|
|
|
|
|
|
|
g_hash_table_remove (model->file_lookup, file);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_object_unref (node->file);
|
2009-09-02 20:21:45 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
if (node->info)
|
|
|
|
|
g_object_unref (node->info);
|
2009-09-02 20:21:45 +00:00
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_array_remove_index (model->files, id);
|
2010-05-02 12:32:18 +00:00
|
|
|
|
g_hash_table_remove_all (model->file_lookup);
|
2009-09-04 17:31:37 +00:00
|
|
|
|
/* We don't need to resort, as removing a row doesn't change the sorting order */
|
2003-03-27 00:10:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_update_file:
|
|
|
|
|
* @model: the model
|
|
|
|
|
* @file: the file
|
|
|
|
|
* @info: the new file info
|
|
|
|
|
* @requires_resort: FIXME: get rid of this argument
|
|
|
|
|
*
|
|
|
|
|
* Tells the file system model that the file changed and that the
|
|
|
|
|
* new @info should be used for it now. If the file is not part of
|
|
|
|
|
* @model, it will get added automatically.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_update_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile *file,
|
|
|
|
|
GFileInfo *info,
|
|
|
|
|
gboolean requires_resort)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
FileModelNode *node;
|
|
|
|
|
guint i, id;
|
2009-09-02 20:23:21 +00:00
|
|
|
|
GFileInfo *old_info;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (G_IS_FILE (file));
|
|
|
|
|
g_return_if_fail (G_IS_FILE_INFO (info));
|
|
|
|
|
|
|
|
|
|
id = node_get_for_file (model, file);
|
|
|
|
|
if (id == 0)
|
2010-08-24 18:17:15 +00:00
|
|
|
|
{
|
|
|
|
|
add_file (model, file, info);
|
|
|
|
|
id = node_get_for_file (model, file);
|
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
node = get_node (model, id);
|
2009-09-02 20:23:21 +00:00
|
|
|
|
|
|
|
|
|
old_info = node->info;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
node->info = g_object_ref (info);
|
2009-09-02 20:23:21 +00:00
|
|
|
|
if (old_info)
|
|
|
|
|
g_object_unref (old_info);
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
for (i = 0; i < model->n_columns; i++)
|
|
|
|
|
{
|
|
|
|
|
if (G_VALUE_TYPE (&node->values[i]))
|
|
|
|
|
g_value_unset (&node->values[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (node->visible)
|
2009-09-02 22:10:46 +00:00
|
|
|
|
emit_row_changed_for_node (model, id);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
|
|
|
|
if (requires_resort)
|
|
|
|
|
gtk_file_system_model_sort_node (model, id);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_set_filter:
|
|
|
|
|
* @mode: a #GtkFileSystemModel
|
2010-02-19 16:53:17 +00:00
|
|
|
|
* @filter: (allow-none): %NULL or filter to use
|
2009-06-30 13:15:55 +00:00
|
|
|
|
*
|
2009-06-24 14:36:20 +00:00
|
|
|
|
* Sets a filter to be used for deciding if a row should be visible or not.
|
|
|
|
|
* Directories are always visible.
|
2009-06-30 13:15:55 +00:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_set_filter (GtkFileSystemModel *model,
|
2009-06-24 14:36:20 +00:00
|
|
|
|
GtkFileFilter * filter)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-09-10 22:22:12 +00:00
|
|
|
|
GtkFileFilter *old_filter;
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
2009-06-24 14:36:20 +00:00
|
|
|
|
g_return_if_fail (filter == NULL || GTK_IS_FILE_FILTER (filter));
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-06-24 14:36:20 +00:00
|
|
|
|
if (filter)
|
|
|
|
|
g_object_ref (filter);
|
2009-09-10 22:22:12 +00:00
|
|
|
|
|
|
|
|
|
old_filter = model->filter;
|
2009-06-24 14:36:20 +00:00
|
|
|
|
model->filter = filter;
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-09-10 22:22:12 +00:00
|
|
|
|
if (old_filter)
|
|
|
|
|
g_object_unref (old_filter);
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gtk_file_system_model_refilter_all (model);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_add_editable:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @iter: Location to return the iter corresponding to the editable row
|
|
|
|
|
*
|
|
|
|
|
* Adds an "empty" row at the beginning of the model. This does not refer to
|
|
|
|
|
* any file, but is a temporary placeholder for a file name that the user will
|
|
|
|
|
* type when a corresponding cell is made editable. When your code is done
|
|
|
|
|
* using this temporary row, call _gtk_file_system_model_remove_editable().
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_add_editable (GtkFileSystemModel *model, GtkTreeIter *iter)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (!get_node (model, 0)->visible);
|
|
|
|
|
|
|
|
|
|
node_set_visible (model, 0, TRUE);
|
|
|
|
|
ITER_INIT_FROM_INDEX (model, iter, 0);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_remove_editable:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
*
|
|
|
|
|
* Removes the "empty" row at the beginning of the model that was
|
|
|
|
|
* created with _gtk_file_system_model_add_editable(). You should call
|
|
|
|
|
* this function when your code is finished editing this temporary row.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_remove_editable (GtkFileSystemModel *model)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (get_node (model, 0)->visible);
|
|
|
|
|
|
|
|
|
|
node_set_visible (model, 0, FALSE);
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_freeze_updates:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
*
|
|
|
|
|
* Freezes most updates on the model, so that performing multiple
|
|
|
|
|
* operations on the files in the model do not cause any events.
|
|
|
|
|
* Use _gtk_file_system_model_thaw_updates() to resume proper
|
|
|
|
|
* operations. It is fine to call this function multiple times as
|
|
|
|
|
* long as freeze and thaw calls are balanced.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_freeze_updates (GtkFileSystemModel *model)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
|
|
|
|
|
model->frozen++;
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_thaw_updates:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
*
|
|
|
|
|
* Undoes the effect of a previous call to
|
|
|
|
|
* _gtk_file_system_model_freeze_updates()
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_thaw_updates (GtkFileSystemModel *model)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
gboolean stuff_added;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (model->frozen > 0);
|
|
|
|
|
|
|
|
|
|
model->frozen--;
|
|
|
|
|
if (model->frozen > 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
stuff_added = get_node (model, model->files->len - 1)->frozen_add;
|
|
|
|
|
|
|
|
|
|
if (model->filter_on_thaw)
|
|
|
|
|
gtk_file_system_model_refilter_all (model);
|
|
|
|
|
if (model->sort_on_thaw)
|
|
|
|
|
gtk_file_system_model_sort (model);
|
|
|
|
|
if (stuff_added)
|
|
|
|
|
{
|
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < model->files->len; i++)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node = get_node (model, i);
|
|
|
|
|
|
|
|
|
|
if (!node->frozen_add)
|
|
|
|
|
continue;
|
|
|
|
|
node->frozen_add = FALSE;
|
|
|
|
|
node_set_visible (model, i, node_should_be_visible (model, i));
|
|
|
|
|
}
|
|
|
|
|
}
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-30 13:15:55 +00:00
|
|
|
|
/**
|
2009-09-04 18:53:46 +00:00
|
|
|
|
* _gtk_file_system_model_clear_cache:
|
2009-06-30 13:15:55 +00:00
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @column: the column to clear or -1 for all columns
|
|
|
|
|
*
|
|
|
|
|
* Clears the cached values in the model for the given @column. Use
|
|
|
|
|
* this function whenever your get_value function would return different
|
|
|
|
|
* values for a column.
|
|
|
|
|
* The file chooser uses this for example when the icon theme changes to
|
|
|
|
|
* invalidate the cached pixbufs.
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_clear_cache (GtkFileSystemModel *model,
|
|
|
|
|
int column)
|
2003-03-26 16:24:35 +00:00
|
|
|
|
{
|
2009-06-30 13:15:55 +00:00
|
|
|
|
guint i;
|
|
|
|
|
int start, end;
|
|
|
|
|
gboolean changed;
|
|
|
|
|
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (column >= -1 && (guint) column < model->n_columns);
|
|
|
|
|
|
2009-09-04 18:53:46 +00:00
|
|
|
|
if (column > -1)
|
2009-06-30 13:15:55 +00:00
|
|
|
|
{
|
|
|
|
|
start = column;
|
|
|
|
|
end = column + 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
start = 0;
|
|
|
|
|
end = model->n_columns;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < model->files->len; i++)
|
|
|
|
|
{
|
|
|
|
|
FileModelNode *node = get_node (model, i);
|
|
|
|
|
changed = FALSE;
|
|
|
|
|
for (column = start; column < end; column++)
|
|
|
|
|
{
|
|
|
|
|
if (!G_VALUE_TYPE (&node->values[column]))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
g_value_unset (&node->values[column]);
|
|
|
|
|
changed = TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (changed && node->visible)
|
2009-09-02 22:10:46 +00:00
|
|
|
|
emit_row_changed_for_node (model, i);
|
2009-06-30 13:15:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* FIXME: resort? */
|
2003-03-26 16:24:35 +00:00
|
|
|
|
}
|
2009-06-30 13:15:55 +00:00
|
|
|
|
|
2009-06-24 20:43:14 +00:00
|
|
|
|
/**
|
|
|
|
|
* _gtk_file_system_model_add_and_query_file:
|
|
|
|
|
* @model: a #GtkFileSystemModel
|
|
|
|
|
* @file: the file to add
|
|
|
|
|
* @attributes: attributes to query before adding the file
|
|
|
|
|
*
|
|
|
|
|
* This is a conenience function that calls g_file_query_info_async() on
|
2009-08-31 20:08:43 +00:00
|
|
|
|
* the given file, and when successful, adds it to the model.
|
|
|
|
|
* Upon failure, the @file is discarded.
|
2009-06-24 20:43:14 +00:00
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
_gtk_file_system_model_add_and_query_file (GtkFileSystemModel *model,
|
|
|
|
|
GFile * file,
|
|
|
|
|
const char * attributes)
|
|
|
|
|
{
|
|
|
|
|
g_return_if_fail (GTK_IS_FILE_SYSTEM_MODEL (model));
|
|
|
|
|
g_return_if_fail (G_IS_FILE (file));
|
|
|
|
|
g_return_if_fail (attributes != NULL);
|
|
|
|
|
|
|
|
|
|
g_file_query_info_async (file,
|
|
|
|
|
attributes,
|
2009-09-02 18:54:52 +00:00
|
|
|
|
G_FILE_QUERY_INFO_NONE,
|
2009-06-24 20:43:14 +00:00
|
|
|
|
IO_PRIORITY,
|
|
|
|
|
model->cancellable,
|
|
|
|
|
gtk_file_system_model_query_done,
|
|
|
|
|
model);
|
|
|
|
|
}
|