comparison src/view_dir_list.c @ 973:ad2ff9608beb

improved refresh in view_dir_list
author nadvornik
date Tue, 19 Aug 2008 22:58:31 +0000
parents c414002a1f27
children 9ff64efe11eb
comparison
equal deleted inserted replaced
972:2ea1059a6091 973:ad2ff9608beb
140 if (fd) return fd->path; 140 if (fd) return fd->path;
141 141
142 return NULL; 142 return NULL;
143 } 143 }
144 144
145 static void vdlist_populate(ViewDir *vd) 145 static gint vdlist_populate(ViewDir *vd, gboolean clear)
146 { 146 {
147 GtkListStore *store; 147 GtkListStore *store;
148 GList *work; 148 GList *work;
149 149 GtkTreeIter iter;
150 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view))); 150 gint valid;
151 gtk_list_store_clear(store); 151 gchar *filepath;
152 152 GList *old_list;
153 work = VDLIST_INFO(vd, list);
154 while (work)
155 {
156 FileData *fd;
157 GtkTreeIter iter;
158 GdkPixbuf *pixbuf;
159 const gchar *date = "";
160
161 fd = work->data;
162
163 if (access_file(fd->path, R_OK | X_OK) && fd->name)
164 {
165 if (fd->name[0] == '.' && fd->name[1] == '\0')
166 {
167 pixbuf = vd->pf->open;
168 }
169 else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0')
170 {
171 pixbuf = vd->pf->parent;
172 }
173 else
174 {
175 pixbuf = vd->pf->close;
176 if (options->layout.show_directory_date)
177 date = text_from_time(fd->date);
178 }
179 }
180 else
181 {
182 pixbuf = vd->pf->deny;
183 }
184
185 gtk_list_store_append(store, &iter);
186 gtk_list_store_set(store, &iter,
187 DIR_COLUMN_POINTER, fd,
188 DIR_COLUMN_ICON, pixbuf,
189 DIR_COLUMN_NAME, fd->name,
190 DIR_COLUMN_DATE, date,
191 -1);
192
193 work = work->next;
194 }
195
196 vd->click_fd = NULL;
197 vd->drop_fd = NULL;
198 }
199
200 gint vdlist_set_fd(ViewDir *vd, FileData *dir_fd)
201 {
202 gint ret; 153 gint ret;
203 FileData *fd; 154 FileData *fd;
204 gchar *old_path = NULL; 155
205 gchar *filepath; 156 old_list = VDLIST_INFO(vd, list);
206
207 if (!dir_fd) return FALSE;
208 if (vd->dir_fd == dir_fd) return TRUE;
209
210 if (vd->dir_fd)
211 {
212 gchar *base;
213
214 base = remove_level_from_path(vd->dir_fd->path);
215 if (strcmp(base, dir_fd->path) == 0)
216 {
217 old_path = g_strdup(vd->dir_fd->name);
218 }
219 g_free(base);
220 }
221
222 file_data_unref(vd->dir_fd);
223 vd->dir_fd = file_data_ref(dir_fd);
224
225 filelist_free(VDLIST_INFO(vd, list));
226 157
227 ret = filelist_read(vd->dir_fd, NULL, &VDLIST_INFO(vd, list)); 158 ret = filelist_read(vd->dir_fd, NULL, &VDLIST_INFO(vd, list));
228 VDLIST_INFO(vd, list) = filelist_sort(VDLIST_INFO(vd, list), SORT_NAME, TRUE); 159 VDLIST_INFO(vd, list) = filelist_sort(VDLIST_INFO(vd, list), SORT_NAME, TRUE);
229 160
230 /* add . and .. */ 161 /* add . and .. */
243 fd = file_data_new_simple(filepath); 174 fd = file_data_new_simple(filepath);
244 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd); 175 VDLIST_INFO(vd, list) = g_list_prepend(VDLIST_INFO(vd, list), fd);
245 g_free(filepath); 176 g_free(filepath);
246 } 177 }
247 178
248 vdlist_populate(vd); 179 store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(vd->view)));
180 if (clear) gtk_list_store_clear(store);
181
182 valid = gtk_tree_model_iter_children(GTK_TREE_MODEL(store), &iter, NULL);
183
184 work = VDLIST_INFO(vd, list);
185 while (work)
186 {
187 gint match;
188 GdkPixbuf *pixbuf;
189 const gchar *date = "";
190 fd = work->data;
191 gint done = FALSE;
192
193 if (access_file(fd->path, R_OK | X_OK) && fd->name)
194 {
195 if (fd->name[0] == '.' && fd->name[1] == '\0')
196 {
197 pixbuf = vd->pf->open;
198 }
199 else if (fd->name[0] == '.' && fd->name[1] == '.' && fd->name[2] == '\0')
200 {
201 pixbuf = vd->pf->parent;
202 }
203 else
204 {
205 pixbuf = vd->pf->close;
206 if (options->layout.show_directory_date)
207 date = text_from_time(fd->date);
208 }
209 }
210 else
211 {
212 pixbuf = vd->pf->deny;
213 }
214
215 while (!done)
216 {
217 FileData *old_fd = NULL;
218
219 if (valid)
220 {
221 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
222 DIR_COLUMN_POINTER, &old_fd,
223 -1);
224
225 if (fd == old_fd)
226 {
227 match = 0;
228 }
229 else
230 {
231 match = filelist_sort_compare_filedata_full(fd, old_fd, SORT_NAME, TRUE);
232
233 if (match == 0) g_warning("multiple fd for the same path");
234 }
235
236 }
237 else
238 {
239 match = -1;
240 }
241
242 if (match < 0)
243 {
244 GtkTreeIter new;
245
246 if (valid)
247 {
248 gtk_list_store_insert_before(store, &new, &iter);
249 }
250 else
251 {
252 gtk_list_store_append(store, &new);
253 }
254
255 gtk_list_store_set(store, &new,
256 DIR_COLUMN_POINTER, fd,
257 DIR_COLUMN_ICON, pixbuf,
258 DIR_COLUMN_NAME, fd->name,
259 DIR_COLUMN_DATE, date,
260 -1);
261
262 done = TRUE;
263 }
264 else if (match > 0)
265 {
266 valid = gtk_list_store_remove(store, &iter);
267 }
268 else
269 {
270 gtk_list_store_set(store, &iter,
271 DIR_COLUMN_ICON, pixbuf,
272 DIR_COLUMN_NAME, fd->name,
273 DIR_COLUMN_DATE, date,
274 -1);
275
276 if (valid) valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter);
277
278 done = TRUE;
279 }
280 }
281 work = work->next;
282 }
283
284 while (valid)
285 {
286 FileData *old_fd;
287 gtk_tree_model_get(GTK_TREE_MODEL(store), &iter, DIR_COLUMN_POINTER, &old_fd, -1);
288
289 valid = gtk_list_store_remove(store, &iter);
290 }
291
292
293 vd->click_fd = NULL;
294 vd->drop_fd = NULL;
295
296 filelist_free(old_list);
297 return ret;
298 }
299
300 gint vdlist_set_fd(ViewDir *vd, FileData *dir_fd)
301 {
302 gint ret;
303 gchar *old_path = NULL;
304
305 if (!dir_fd) return FALSE;
306 if (vd->dir_fd == dir_fd) return TRUE;
307
308 if (vd->dir_fd)
309 {
310 gchar *base;
311
312 base = remove_level_from_path(vd->dir_fd->path);
313 if (strcmp(base, dir_fd->path) == 0)
314 {
315 old_path = g_strdup(vd->dir_fd->name);
316 }
317 g_free(base);
318 }
319
320 file_data_unref(vd->dir_fd);
321 vd->dir_fd = file_data_ref(dir_fd);
322
323 ret = vdlist_populate(vd, TRUE);
249 324
250 if (old_path) 325 if (old_path)
251 { 326 {
252 /* scroll to make last path visible */ 327 /* scroll to make last path visible */
253 FileData *found = NULL; 328 FileData *found = NULL;
275 return ret; 350 return ret;
276 } 351 }
277 352
278 void vdlist_refresh(ViewDir *vd) 353 void vdlist_refresh(ViewDir *vd)
279 { 354 {
280 FileData *dir_fd; 355 vdlist_populate(vd, FALSE);
281
282 dir_fd = vd->dir_fd;
283 vd->dir_fd = NULL;
284 vdlist_set_fd(vd, dir_fd);
285 file_data_unref(dir_fd);
286 } 356 }
287 357
288 gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data) 358 gint vdlist_press_key_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
289 { 359 {
290 ViewDir *vd = data; 360 ViewDir *vd = data;