File: getdir.c

package info (click to toggle)
gtoaster 0.19991130-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,700 kB
  • ctags: 1,318
  • sloc: ansic: 12,739; sh: 353; makefile: 217; sed: 93
file content (287 lines) | stat: -rw-r--r-- 8,509 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

/* get the directory tree */

#include <gtk/gtk.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>

#include "main.h"
#include "getdir.h"
#include "dircache.h" // finally,some step towards "highspeed"
#include "dndsetup.h"
#include "menusys.h"

void getdir_drag(GtkWidget *w,
		 GdkDragContext *ct,
		 GtkSelectionData *data,
		 guint inf,
		 guint time,
		 getdir_dirinfo *info)
{
	
	strcpy((gchar*)&info->receiverstring,"file:");
	strcat((gchar*)&info->receiverstring,(gchar*)&info->dirname);
	strcat((gchar*)&info->receiverstring,CRLF);
	gtk_selection_data_set (data,
				data->target,
				8,(gchar*)&info->receiverstring,
				strlen((gchar*)&info->receiverstring));
}
;

void getdir_drop(GtkWidget *w,
		 GdkDragContext *c,
		 gint x,
		 gint y,
		 GtkSelectionData *data,
		 guint inf,
		 guint time,
		 getdir_dirinfo *info)
{
	if ((data->length>=0) && (data->format == 8)
	    && (info->receiver!=NULL))
	  {
		  info->recdata=(gchar*)data->data;
		  info->receiver(w,info);
		  gtk_drag_finish(c,TRUE,FALSE,time);
		  return;
	  }
	;
	gtk_drag_finish (c,FALSE,FALSE,time);
}
;

void getdir_cb_collapse(GtkWidget *item,getdir_dirinfo *info);
void getdir_cb_expand(GtkWidget *item,getdir_dirinfo *info) // this is the internal callback for reloading directory contents
{
        /* only remove if present - may not be present if chdir is executed
	 * from the filelist by double clicking on a directory within a 
	 * newly mounted partition */
 	if (GTK_TREE_ITEM_SUBTREE(GTK_TREE_ITEM(item))!=NULL)
		gtk_tree_item_remove_subtree(GTK_TREE_ITEM(item));
	if ((dircache_subdirentries((char*)&info->dirname))||(info->parent==NULL))
	  {
		  /* check this existance of subdir entries once more to fix a drawing error */
		  /* do not remove expand sign if parent tree item */
		  gtk_signal_disconnect_by_func(GTK_OBJECT(item),
						GTK_SIGNAL_FUNC(getdir_cb_expand),
						info);
		  getdir_tree(GTK_TREE_ITEM(item),
			      info);		
		  gtk_signal_connect(GTK_OBJECT(item),"collapse",
				     GTK_SIGNAL_FUNC(getdir_cb_collapse),info);
		  gtk_tree_item_expand(GTK_TREE_ITEM(item));
	  }
	;
}
; // do only call this the first time a directory is expanded,afterwards disable signal handler
  // this call would end up in some infinite recursion otherwise


void getdir_cb_collapse(GtkWidget *item,getdir_dirinfo *info) // remove directory from memory once its leaf has been closed
{
	GList *selection;

	selection=GTK_TREE_SELECTION(GTK_TREE_ITEM_SUBTREE(GTK_ITEM(item)));
	if ((selection==NULL) || (selection->data!=item))
	    gtk_tree_select_child(GTK_TREE_ROOT_TREE
				  (GTK_TREE_ITEM_SUBTREE
				   (GTK_ITEM(item)))
				  ,item);
	gtk_signal_connect(GTK_OBJECT(item),"expand",    // reconnect reading routine
			   GTK_SIGNAL_FUNC(getdir_cb_expand),info);
	gtk_signal_disconnect_by_func(GTK_OBJECT(item),
				      GTK_SIGNAL_FUNC(getdir_cb_collapse),
				      info);
	gtk_tree_item_remove_subtree(GTK_TREE_ITEM(item));
	gtk_tree_item_set_subtree(GTK_TREE_ITEM(item),
				  gtk_tree_new());            // add dummy tree instead of the real directory

/* FIXME: do remove the getdir_expanddirinfo structs connected to the 
 * Items of the collapsing subtree,no plan how to do this though */

}
;

/* select item with name "name" from "tree",which is expanded automatically
 * if this shouldnt be already the case. 
 *  name: item name
 *  tree: GtkTreeItem,as the tree has to be expandable on demand */
void getdir_selectsubdir(GtkWidget *tree,
			 char *name)
{
	GtkTree *subtree;
	GList *current;
	GtkTreeItem *found;
	GtkLabel *label;
	char *labeltext;

	/* the item has to take care of expand event count for itself
	 * anyway,so why should we care about that here then ? ;-) */
	gtk_tree_item_expand(GTK_TREE_ITEM(tree));
	subtree=GTK_TREE(GTK_TREE_ITEM_SUBTREE(tree));
	current=subtree->children;
	found=NULL;
	while ((current!=NULL)&&(found==NULL))
	  {		  
		  label=GTK_LABEL(gtk_container_children(GTK_CONTAINER(current->data))->data);
		  gtk_label_get(label,&labeltext);
		  if (!strcmp(name,labeltext))
		      found=GTK_TREE_ITEM(current->data);
		  
		  current=current->next;
	  };
	if (found!=NULL)
	    gtk_tree_select_child(GTK_TREE_ROOT_TREE(GTK_TREE_ITEM_SUBTREE(tree)),
				  GTK_WIDGET(found));
}
;
		

getdir_dirinfo *getdir_dirinfo_create(char *path,
				      GtkSignalFunc select,
				      GtkSignalFunc unselect,
				      void *data,
				      int allowdrag,				      
				      void (*receiver)(GtkWidget *,void*),
				      menusys_menu *popup)
{
	getdir_dirinfo *di;
	di=(getdir_dirinfo*)malloc(sizeof(getdir_dirinfo));
	strcpy((char*)&di->dirname,path);
	di->select=select;
	di->unselect=unselect;
	di->data=data;
	di->allowdrag=allowdrag;
	di->receiver=receiver;
	di->popup=popup;
	return di;
}
;

void getdir_connectstdsignals(GtkWidget *item,
			      GtkWidget *parent,
			      getdir_dirinfo *info)
{	
	info->referringto=item;
	info->parent=parent;
	gtk_signal_connect(GTK_OBJECT(item),"expand",
			   GTK_SIGNAL_FUNC(getdir_cb_expand),info);
	gtk_signal_connect(GTK_OBJECT(item),"select",
			   info->select,info);
	gtk_signal_connect(GTK_OBJECT(item),"deselect",
			   info->unselect,info);
	
	if (info->allowdrag)
	  {		  
		  dndsetup_drag(item,
				GTK_SIGNAL_FUNC(getdir_drag),
				NULL,
				info);
	  }
	;
	
	if (info->receiver!=NULL)
	  {
		  /* do no more use dndsetup here, just connect the signal
		   * handler but do not setup the item as drop destination.
		   * all drop events to a tree are currently by 
		   * droptreepatch.c,which then redistributes those
		   * events to their receivers */
		  gtk_signal_connect (GTK_OBJECT(item),
				      "drag_data_received",
				      GTK_SIGNAL_FUNC(getdir_drop),
				      info);
	  }
	;
	
	if (info->popup!=NULL)
	    menusys_attachpopupmenu(item,info->popup,(gpointer)info);
}
;
	

/* GtkTreeItem *host is new: it is necessary to make drag and drop support
 * work,as when drag and drop is enabled a Tree Item has to be connected
 * all the way down to the root tree,which has to be attached to some visible
 * widget already. To maintain getdir_trees full functionality,you are
 * allowed to pass a NULL pointer instead of the GtkTreeItem,if the tree
 * you intend to create is a root tree. otherwise getdir_tree will
 * automatically attach the created tree to the passed GtkTreeItem. */
GtkWidget *getdir_tree(GtkTreeItem *host,getdir_dirinfo *di)
{
	GtkWidget *dirtree;
	DIR *dir;
	GtkWidget *entrytree;
	struct dirent *entry;
	char newdir[256];
	getdir_dirinfo *getdir_expanddirinfo;
			
	dirtree=gtk_tree_new();
	if (host!=NULL)
	  {		  
		  gtk_tree_item_set_subtree(host,dirtree);
		  gtk_widget_show(dirtree);
	  }
	;
	
	gtk_tree_append(GTK_TREE(dirtree),
			gtk_tree_item_new_with_label("Test"));
	/* this line seems to solve a major problem ;-)
	 * the thing is that I do not know why yet ....
	 * This time it really looks like some bug in the GtkTree Widget
	 * see Changelog for Details on that...
	 * and btw: it is intended that the widget added here is never
	 * actually shown within the tree,as it is not even intended to
	 * exist at all */
	
	dir=opendir((gchar*)&di->dirname);
	if (dir!=NULL)
	  {		  
		  while ((entry=readdir(dir))!=NULL)
		    {
			    if ( (strcmp((char*)&entry->d_name,".")) &&
				(strcmp((char*)&entry->d_name,"..")) )
			      {
				      strcpy((char*)&newdir,(gchar*)&di->dirname);
				      if (di->dirname[strlen((gchar*)&di->dirname)-1]!="/"[0])
					  strcat((char*)&newdir,"/");
				      strcat((char*)&newdir,(char*)&entry->d_name);
				      if (dircache_isdir((char*)&newdir)) {
					      entrytree=gtk_tree_item_new_with_label((char*)&entry->d_name);
					      gtk_tree_append(GTK_TREE(dirtree),entrytree);
					      if (dircache_subdirentries((char*)&newdir)>0)
						{
							gtk_tree_item_set_subtree(GTK_TREE_ITEM(entrytree),
										  gtk_tree_new());
						}       // do add a dummy tree only - real directory is read within expand callback
					      ;
					      gtk_widget_show(entrytree);
					      getdir_expanddirinfo=
						  getdir_dirinfo_create((char*)&newdir,
									di->select,
									di->unselect,
									di->data,
									di->allowdrag,
									di->receiver,
									di->popup);					      
					      getdir_connectstdsignals(entrytree,
								       GTK_WIDGET(host),
								       getdir_expanddirinfo);
				      }
				      ;
			      }
			    ;
		    }
		  ;
		  closedir(dir);
	  }
	;
	return dirtree;
}
;