+ return -1;
+
+ if(umount)
+ {
+ free(device->mount_point);
+ device->mount_point = NULL;
+ }
+ else
+ device->mount_point = strdup(mount_point);
+
+ return 0;
+}
+
+/**
+Refreshes both the internal devices array and the GUI list.
+*/
+int refresh_devices(GuiContext *context, int umount)
+{
+ GtkListStore *store;
+ GtkTreeSelection *selection;
+ int n_listed;
+ time_t latest;
+ int i;
+ GtkTreeIter iter;
+
+ store = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(context->list)));
+ gtk_list_store_clear(store);
+
+ free_devices(context->devices);
+
+ context->devices = get_devices();
+ if(!context->devices)
+ return 0;
+
+ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(context->list));
+
+ /* Populate the list with devices in appropriate state. */
+ n_listed = 0;
+ latest = 0;
+ for(i=0; context->devices[i].node; ++i)
+ {
+ Device *dev;
+
+ dev = &context->devices[i];
+ if(umount<0 || !dev->mount_point==!umount)
+ {
+ gtk_list_store_append(store, &iter);
+ gtk_list_store_set(store, &iter,
+ 0, dev->description,
+ 1, dev,
+ 2, !!dev->mount_point,
+ 3, dev->mount_point,
+ -1);
+ if(dev->time>latest)
+ {
+ /* Pre-select the device that appeared on the system most recently. */
+ latest = dev->time;
+ gtk_tree_selection_select_iter(selection, &iter);
+ }
+
+ ++n_listed;
+ }
+ }
+
+ return n_listed;
+}
+
+/**
+Handles an automatic refresh of the device list in response to inotify events.
+*/
+gboolean refresh_devices_idle(gpointer data)
+{
+ GuiContext *context = (GuiContext *)data;
+ int n_listed;
+
+ n_listed = refresh_devices(context, -1);
+ context->refresh_pending = 0;
+
+ if(context->autohide)
+ {
+ if(n_listed)
+ gtk_widget_show_all(context->window);
+ else
+ gtk_widget_hide(context->window);
+ }
+
+ return FALSE;
+}
+
+/**
+Callback for selection in the device list changing. Updates the action button
+label according to device status.
+*/
+void selection_changed(GtkTreeSelection *selection, gpointer user_data)
+{
+ GuiContext *context = (GuiContext *)user_data;
+ GtkTreeIter iter;
+ GtkTreeModel *model;
+ Device *device;
+
+ if(!gtk_tree_selection_get_selected(selection, &model, &iter))
+ return;
+
+ gtk_tree_model_get(model, &iter, 1, &device, -1);
+ gtk_button_set_label(GTK_BUTTON(context->button), (device->mount_point ? "Unmount" : "Mount"));
+}
+
+/**
+Callback for activating a row in the device list. Mounts or unmounts the
+device depending on operating mode.
+*/
+void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
+{
+ GuiContext *context = (GuiContext *)user_data;
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ Device *device;
+ int ret;
+ char output[1024];
+ int pid;
+
+ model = gtk_tree_view_get_model(list);
+
+ if(!gtk_tree_model_get_iter(model, &iter, path))
+ return;
+
+ gtk_tree_model_get(model, &iter, 1, &device, -1);
+ ret = toggle_device(device, output, sizeof(output));
+ if(ret)