Nicer error handling

This commit is contained in:
Pierre Pronchery 2009-08-17 22:37:34 +00:00
parent 7c10e1a000
commit fb1e3ec7fe
2 changed files with 50 additions and 27 deletions

View File

@ -129,6 +129,7 @@ unsigned int browser_cnt = 0;
/* public */ /* public */
/* functions */ /* functions */
/* browser_new */ /* browser_new */
static gboolean _new_idle(gpointer data);
static int _new_pixbufs(Browser * browser); static int _new_pixbufs(Browser * browser);
static GtkListStore * _create_store(Browser * browser); static GtkListStore * _create_store(Browser * browser);
@ -147,6 +148,7 @@ Browser * browser_new(char const * directory)
GtkWidget * menu; GtkWidget * menu;
GtkWidget * menuitem; GtkWidget * menuitem;
#endif #endif
char * p;
if((browser = malloc(sizeof(*browser))) == NULL) if((browser = malloc(sizeof(*browser))) == NULL)
{ {
@ -322,10 +324,6 @@ Browser * browser_new(char const * directory)
browser_set_view(browser, BV_DETAILS); browser_set_view(browser, BV_DETAILS);
gtk_widget_grab_focus(browser->detailview); gtk_widget_grab_focus(browser->detailview);
#endif #endif
if(directory != NULL)
browser_set_location(browser, directory);
else
browser_go_home(browser);
/* preferences */ /* preferences */
browser->pr_window = NULL; browser->pr_window = NULL;
@ -333,12 +331,36 @@ Browser * browser_new(char const * directory)
/* about */ /* about */
browser->ab_window = NULL; browser->ab_window = NULL;
/* open directory */
if(directory != NULL && (p = strdup(directory)) != NULL)
browser->current = g_list_append(browser->current, p);
g_idle_add(_new_idle, browser);
gtk_container_add(GTK_CONTAINER(browser->window), vbox); gtk_container_add(GTK_CONTAINER(browser->window), vbox);
gtk_widget_show_all(browser->window); gtk_widget_show_all(browser->window);
browser_cnt++; browser_cnt++;
return browser; return browser;
} }
static gboolean _new_idle(gpointer data)
{
Browser * browser = data;
char * p;
if(browser->current == NULL)
browser_go_home(browser);
else
{
p = browser->current->data;
browser->current = g_list_delete_link(browser->current,
browser->current);
browser_set_location(browser, p);
free(p);
}
return FALSE;
}
static int _new_pixbufs(Browser * browser) static int _new_pixbufs(Browser * browser)
{ {
char * file[] = { "gnome-fs-regular", char * file[] = { "gnome-fs-regular",
@ -486,7 +508,7 @@ int browser_error(Browser * browser, char const * message, int ret)
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", "Error"); GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "%s", "Error");
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
"%s", message); "%s: %s", message, strerror(errno));
gtk_window_set_title(GTK_WINDOW(dialog), "Error"); gtk_window_set_title(GTK_WINDOW(dialog), "Error");
if(ret < 0) if(ret < 0)
{ {
@ -655,7 +677,7 @@ void browser_refresh(Browser * browser)
|| fstat(fd, &st) != 0 || fstat(fd, &st) != 0
|| (dir = fdopendir(fd)) == NULL) || (dir = fdopendir(fd)) == NULL)
{ {
browser_error(browser, strerror(errno), 0); browser_error(browser, browser->current->data, 0);
if(fd >= 0) if(fd >= 0)
close(fd); close(fd);
return; return;
@ -663,13 +685,13 @@ void browser_refresh(Browser * browser)
#else #else
if((dir = opendir(browser->current->data)) == NULL) if((dir = opendir(browser->current->data)) == NULL)
{ {
browser_error(browser, strerror(errno), 0); browser_error(browser, browser->current->data, 0);
return; return;
} }
fd = dirfd(dir); fd = dirfd(dir);
if(fstat(fd, &st) != 0) if(fstat(fd, &st) != 0)
{ {
browser_error(browser, strerror(errno), 0); browser_error(browser, browser->current->data, 0);
closedir(dir); closedir(dir);
return; return;
} }
@ -738,8 +760,8 @@ static void _refresh_path(Browser * browser)
/* _refresh_new */ /* _refresh_new */
static int _new_loop(Browser * browser); static int _refresh_new_loop(Browser * browser);
static gboolean _new_idle(gpointer data); static gboolean _refresh_new_idle(gpointer data);
static void _refresh_done(Browser * browser); static void _refresh_done(Browser * browser);
static void _refresh_new(Browser * browser) static void _refresh_new(Browser * browser)
@ -747,21 +769,22 @@ static void _refresh_new(Browser * browser)
unsigned int i; unsigned int i;
gtk_list_store_clear(browser->store); gtk_list_store_clear(browser->store);
for(i = 0; i < IDLE_LOOP_ICON_CNT && _new_loop(browser) == 0; i++); for(i = 0; i < IDLE_LOOP_ICON_CNT
&& _refresh_new_loop(browser) == 0; i++);
if(i == IDLE_LOOP_ICON_CNT) if(i == IDLE_LOOP_ICON_CNT)
browser->refresh_id = g_idle_add(_new_idle, browser); browser->refresh_id = g_idle_add(_refresh_new_idle, browser);
else else
_refresh_done(browser); _refresh_done(browser);
} }
/* _new_loop */ /* _refresh_new_loop */
static int _loop_status(Browser * browser); static int _loop_status(Browser * browser);
static void _loop_insert(Browser * browser, GtkTreeIter * iter, static void _loop_insert(Browser * browser, GtkTreeIter * iter,
char const * path, char const * display, struct stat * lst, char const * path, char const * display, struct stat * lst,
struct stat * st, gboolean updated); struct stat * st, gboolean updated);
static int _new_loop(Browser * browser) static int _refresh_new_loop(Browser * browser)
{ {
struct dirent * de; struct dirent * de;
GtkTreeIter iter; GtkTreeIter iter;
@ -1029,12 +1052,13 @@ static void _insert_dir(Browser * browser, GdkPixbuf ** icon_24,
#endif #endif
} }
static gboolean _new_idle(gpointer data) static gboolean _refresh_new_idle(gpointer data)
{ {
Browser * browser = data; Browser * browser = data;
unsigned int i; unsigned int i;
for(i = 0; i < IDLE_LOOP_ICON_CNT && _new_loop(browser) == 0; i++); for(i = 0; i < IDLE_LOOP_ICON_CNT
&& _refresh_new_loop(browser) == 0; i++);
if(i == IDLE_LOOP_ICON_CNT) if(i == IDLE_LOOP_ICON_CNT)
return TRUE; return TRUE;
_refresh_done(browser); _refresh_done(browser);
@ -1260,7 +1284,7 @@ void browser_set_location(Browser * browser, char const * path)
return; return;
} }
else else
browser_error(browser, realpath, 0); /* XXX call strerror() */ browser_error(browser, realpath, 0);
free(realpath); free(realpath);
} }

View File

@ -77,12 +77,12 @@ void on_file_new_folder(GtkWidget * widget, gpointer data)
if((path = malloc(strlen(cur) + sizeof(newfolder) + 1)) == NULL) if((path = malloc(strlen(cur) + sizeof(newfolder) + 1)) == NULL)
{ {
browser_error(browser, strerror(errno), 0); browser_error(browser, "malloc", 0);
return; return;
} }
sprintf(path, "%s/%s", cur, newfolder); sprintf(path, "%s/%s", cur, newfolder);
if(mkdir(path, 0777) != 0) if(mkdir(path, 0777) != 0)
browser_error(browser, strerror(errno), 0); browser_error(browser, path, 0);
free(path); free(path);
} }
@ -655,7 +655,6 @@ void on_updir(GtkWidget * widget, gpointer data)
Browser * browser = data; Browser * browser = data;
char * dir; char * dir;
browser = data;
dir = g_path_get_dirname(browser->current->data); dir = g_path_get_dirname(browser->current->data);
browser_set_location(browser, dir); browser_set_location(browser, dir);
g_free(dir); g_free(dir);
@ -764,14 +763,14 @@ void on_filename_edited(GtkCellRendererText * renderer, gchar * arg1,
{ {
q = g_filename_from_utf8(p, -1, NULL, NULL, NULL); q = g_filename_from_utf8(p, -1, NULL, NULL, NULL);
if(rename(path, q != NULL ? q : p) != 0) if(rename(path, q != NULL ? q : p) != 0)
browser_error(browser, strerror(errno), 0); browser_error(browser, path, 0);
else else
gtk_list_store_set(browser->store, &iter, BR_COL_PATH, gtk_list_store_set(browser->store, &iter, BR_COL_PATH,
p, BR_COL_DISPLAY_NAME, arg2, -1); p, BR_COL_DISPLAY_NAME, arg2, -1);
free(q); free(q);
} }
else if(link(path, p) != 0 || unlink(path) != 0) else if(link(path, p) != 0 || unlink(path) != 0)
browser_error(browser, strerror(errno), 0); browser_error(browser, path, 0);
else else
gtk_list_store_set(browser->store, &iter, BR_COL_PATH, p, gtk_list_store_set(browser->store, &iter, BR_COL_PATH, p,
BR_COL_DISPLAY_NAME, arg2, -1); BR_COL_DISPLAY_NAME, arg2, -1);
@ -1039,12 +1038,12 @@ static void _on_popup_new_text_file(GtkWidget * widget, gpointer data)
if((path = malloc(strlen(cur) + sizeof(newtext) + 1)) == NULL) if((path = malloc(strlen(cur) + sizeof(newtext) + 1)) == NULL)
{ {
browser_error(browser, strerror(errno), 0); browser_error(browser, "malloc", 0);
return; return;
} }
sprintf(path, "%s/%s", cur, newtext); sprintf(path, "%s/%s", cur, newtext);
if((fd = creat(path, 0666)) < 0) if((fd = creat(path, 0666)) < 0)
browser_error(browser, strerror(errno), 0); browser_error(browser, path, 0);
else else
close(fd); close(fd);
free(path); free(path);
@ -1240,11 +1239,11 @@ static void _on_icon_run(GtkWidget * widget, gpointer data)
if(res != GTK_RESPONSE_YES) if(res != GTK_RESPONSE_YES)
return; return;
if((pid = fork()) == -1) if((pid = fork()) == -1)
browser_error(cb->browser, strerror(errno), 0); browser_error(cb->browser, "fork", 0);
else if(pid == 0) else if(pid == 0)
{ {
execl(cb->path, cb->path, NULL); execl(cb->path, cb->path, NULL);
browser_error(cb->browser, strerror(errno), 0); browser_error(cb->browser, "fork", 0);
exit(127); exit(127);
} }
} }
@ -1271,5 +1270,5 @@ static void _on_icon_unmount(GtkWidget * widget, gpointer data)
IconCallback * cb = data; IconCallback * cb = data;
if(unmount(cb->path, 0) != 0) if(unmount(cb->path, 0) != 0)
browser_error(cb->browser, strerror(errno), 0); browser_error(cb->browser, cb->path, 0);
} }