Split the mounting logic out of row_activated
[pmount-gui.git] / main.c
1 /* Required for strdup, snprintf, getopt */
2 #define _XOPEN_SOURCE 500
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <dirent.h>
9 #include <mntent.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <sys/select.h>
13 #include <gtk/gtk.h>
14 #include <gdk/gdkkeysyms.h>
15
16 typedef struct sProperty
17 {
18         char *name;
19         char *value;
20 } Property;
21
22 typedef struct sDevice
23 {
24         char *node;
25         char *label;
26         char *description;
27         int mounted;
28         time_t time;
29 } Device;
30
31 int verbosity = 0;
32 char *post_mount_command = NULL;
33
34 /**
35 Parses a string of the form name=value and places the components in a Property
36 structure.  Returns 0 on success, or -1 if the string wasn't a valid property.
37 */
38 int parse_property(char *str, int size, Property *prop)
39 {
40         int equals = -1;
41         int i;
42
43         for(i=0; (equals<0 && i<size); ++i)
44                 if(str[i]=='=')
45                         equals = i;
46
47         if(equals<0)
48                 return -1;
49
50         prop->name = malloc(equals+1);
51         strncpy(prop->name, str, equals);
52         prop->name[equals] = 0;
53
54         prop->value = malloc(size-equals);
55         strncpy(prop->value, str+equals+1, size-equals-1);
56         prop->value[size-equals-1] = 0;
57
58         return 0;
59 }
60
61 /**
62 Retrieves all properties associated with a /dev node.  The returned array is
63 terminated with an entry containing NULL values.  Use free_properties to free
64 the array.
65 */
66 Property *get_device_properties(char *node)
67 {
68         int pid;
69         int pipe_fd[2];
70         char *buf;
71         int bufsize;
72         int pos = 0;
73         int eof = 0;
74         Property *props = NULL;
75         int n_props = 0;
76
77         pipe(pipe_fd);
78
79         pid = fork();
80         if(pid==0)
81         {
82                 /* Child process */
83                 if(verbosity>=2)
84                         printf("Running udevadm info -q property -n \"%s\"\n", node);
85
86                 close(pipe_fd[0]);
87                 dup2(pipe_fd[1], 1);
88
89                 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
90                 _exit(1);
91         }
92         else if(pid<0)
93         {
94                 close(pipe_fd[0]);
95                 close(pipe_fd[1]);
96
97                 return NULL;
98         }
99
100         /* Parent process */
101         close(pipe_fd[1]);
102
103         bufsize = 256;
104         buf = (char *)malloc(bufsize);
105
106         while(1)
107         {
108                 int newline;
109                 int i;
110                 Property prop;
111
112                 if(!eof)
113                 {
114                         int len;
115
116                         len = read(pipe_fd[0], buf+pos, bufsize-pos);
117                         if(len==0)
118                                 eof = 1;
119                         else if(len==-1)
120                                 break;
121                         pos += len;
122                 }
123
124                 newline = -1;
125                 for(i=0; (newline<0 && i<pos); ++i)
126                         if(buf[i]=='\n')
127                                 newline = i;
128
129                 if(newline<0)
130                 {
131                         if(eof)
132                                 break;
133
134                         /* There was no newline in the buffer but there is more output to
135                         be read.  Try again with a larger buffer. */
136                         bufsize *= 2;
137                         buf = (char *)realloc(buf, bufsize);
138                         continue;
139                 }
140
141                 if(parse_property(buf, newline, &prop)==0)
142                 {
143                         /* Reserve space for a sentinel value as well. */
144                         props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
145                         props[n_props] = prop;
146                         ++n_props;
147
148                         memmove(buf, buf+newline+1, pos-newline-1);
149                         pos -= newline+1;
150                 }
151                 else
152                         break;
153         }
154
155         free(buf);
156
157         if(props)
158         {
159                 /* Terminate the array with NULL pointers. */
160                 props[n_props].name = NULL;
161                 props[n_props].value = NULL;
162         }
163
164         waitpid(pid, NULL, 0);
165         close(pipe_fd[0]);
166
167         return props;
168 }
169
170 /**
171 Looks for a property in an array of properties and returns its value.  Returns
172 NULL if the property was not found.
173 */
174 char *get_property_value(Property *props, char *name)
175 {
176         int i;
177         for(i=0; props[i].name; ++i)
178                 if(strcmp(props[i].name, name)==0)
179                         return props[i].value;
180         return NULL;
181 }
182
183 /**
184 Checks if a property has a specific value.  A NULL value is matched if the
185 property does not exist.
186 */
187 int match_property_value(Property *props, char *name, char *value)
188 {
189         char *v = get_property_value(props, name);
190         if(!v)
191                 return value==NULL;
192         return strcmp(v, value)==0;
193 }
194
195 /**
196 Frees an array of properties and all strings contained in it.
197 */
198 void free_properties(Property *props)
199 {
200         int i;
201         if(!props)
202                 return;
203         for(i=0; props[i].name; ++i)
204         {
205                 free(props[i].name);
206                 free(props[i].value);
207         }
208         free(props);
209 }
210
211 /**
212 Reads device names from an fstab/mtab file.
213 */
214 char **get_mount_entries(char *filename, int (*predicate)(struct mntent *))
215 {
216         FILE *file;
217         struct mntent *me;
218         char **devices = NULL;
219         int n_devices = 0;
220
221         file = setmntent(filename, "r");
222         if(!file)
223                 return NULL;
224
225         while((me = getmntent(file)))
226                 if(!predicate || predicate(me))
227                 {
228                         devices = (char **)realloc(devices, (n_devices+2)*sizeof(char *));
229                         devices[n_devices] = strdup(me->mnt_fsname);
230                         ++n_devices;
231                 }
232
233         endmntent(file);
234         if(devices)
235                 devices[n_devices] = NULL;
236
237         return devices;
238 }
239
240 /**
241 Returns an array of all currently mounted devices.
242 */
243 char **get_mounted_devices(void)
244 {
245         return get_mount_entries("/etc/mtab", NULL);
246 }
247
248 /**
249 Checks if an fstab entry has the user option set.
250 */
251 int is_user_mountable(struct mntent *me)
252 {
253         return hasmntopt(me, "user")!=NULL;
254 }
255
256 /**
257 Returns an array of user-mountable devices listed in fstab.
258 */
259 char **get_fstab_devices(void)
260 {
261         return get_mount_entries("/etc/fstab", &is_user_mountable);
262 }
263
264 /**
265 Checks if an array of strings contains the specified string.
266 */
267 int is_in_array(char **array, char *str)
268 {
269         int i;
270         if(!array || !str)
271                 return 0;
272         for(i=0; array[i]; ++i)
273                 if(!strcmp(str, array[i]))
274                         return 1;
275         return 0;
276 }
277
278 /**
279 Frees an array of strings.
280 */
281 void free_string_array(char **array)
282 {
283         int i;
284         if(!array)
285                 return;
286         for(i=0; array[i]; ++i)
287                 free(array[i]);
288         free(array);
289 }
290
291 /**
292 Checks if a partition identified by a sysfs path is on a removable device.
293 */
294 int is_removable(char *devpath)
295 {
296         char fnbuf[256];
297         int len;
298         char *ptr;
299         int fd;
300
301         len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
302         /* Default to not removable if the path was too long. */
303         if(len+10>=(int)sizeof(fnbuf))
304                 return 0;
305
306         /* We got a partition as a parameter, but the removable property is on the
307         disk.  Replace the last component with "removable". */
308         for(ptr=fnbuf+len; (ptr>fnbuf && *ptr!='/'); --ptr) ;
309         strcpy(ptr, "/removable");
310
311         fd = open(fnbuf, O_RDONLY);
312         if(fd!=-1)
313         {
314                 char c;
315                 read(fd, &c, 1);
316                 close(fd);
317                 if(c=='1')
318                 {
319                         if(verbosity>=2)
320                                 printf("  Removable\n");
321                         return 1;
322                 }
323                 if(verbosity>=2)
324                         printf("  Not removable\n");
325         }
326
327         return 0;
328 }
329
330 /**
331 Checks if a partition's disk or any of its parent devices are connected to any
332 of a set of buses.  The device is identified by a sysfs path.  The bus array
333 must be terminated with a NULL entry.
334 */
335 int check_buses(char *devpath, char **buses)
336 {
337         char fnbuf[256];
338         char *ptr;
339         int len;
340
341         len = snprintf(fnbuf, sizeof(fnbuf), "/sys%s", devpath);
342         /* Default to no match if the path was too long. */
343         if(len+10>=(int)sizeof(fnbuf))
344                 return 0;
345
346         for(ptr=fnbuf+len; ptr>fnbuf+12; --ptr)
347                 if(*ptr=='/')
348                 {
349                         char linkbuf[256];
350                         /* Replace the last component with "subsystem". */
351                         strcpy(ptr, "/subsystem");
352                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
353
354                         if(len!=-1)
355                         {
356                                 linkbuf[len] = 0;
357                                 /* Extract the last component of the subsystem symlink. */
358                                 for(; (len>0 && linkbuf[len-1]!='/'); --len) ;
359
360                                 if(verbosity>=2)
361                                 {
362                                         *ptr = 0;
363                                         printf("  Subsystem of %s is %s\n", fnbuf, linkbuf+len);
364                                 }
365
366                                 if(is_in_array(buses, linkbuf+len))
367                                         return 1;
368                         }
369                 }
370
371         return 0;
372 }
373
374 /**
375 Check if an array of properties describes a device that can be mounted.  An
376 array of explicitly allowed devices can be passed in as well.  Both arrays must
377 be terminated by a NULL entry.
378 */
379 int can_mount(Property *props, char **allowed)
380 {
381         static char *removable_buses[] = { "usb", "firewire", 0 };
382         char *devname;
383         char *devpath;
384         char *bus;
385
386         devname = get_property_value(props, "DEVNAME");
387         if(is_in_array(allowed, devname))
388                 return 1;
389
390         /* Special case for CD devices, since they are not partitions.  Only allow
391         mounting if media is inserted. */
392         if(match_property_value(props, "ID_TYPE", "cd") && match_property_value(props, "ID_CDROM_MEDIA", "1"))
393                 return 1;
394
395         /* Only allow mounting partitions. */
396         if(!match_property_value(props, "DEVTYPE", "partition"))
397                 return 0;
398
399         devpath = get_property_value(props, "DEVPATH");
400         if(is_removable(devpath))
401                 return 1;
402
403         /* Certain buses are removable by nature, but devices only advertise
404         themselves as removable if they support removable media, e.g. memory card
405         readers. */
406         bus = get_property_value(props, "ID_BUS");
407         if(is_in_array(removable_buses, bus))
408                 return 1;
409
410         return check_buses(devpath, removable_buses);
411 }
412
413 /**
414 Returns an array of all device nodes in a directory.  Symbolic links are
415 dereferenced.
416 */
417 char **get_device_nodes(char *dirname)
418 {
419         DIR *dir;
420         struct dirent *de;
421         char fnbuf[256];
422         char linkbuf[256];
423         struct stat st;
424         char **nodes = NULL;
425         int n_nodes = 0;
426         char **checked = NULL;
427         int n_checked = 0;
428         int i;
429
430         dir = opendir(dirname);
431         if(!dir)
432                 return NULL;
433
434         while((de = readdir(dir)))
435         {
436                 char *node;
437                 int duplicate = 0;
438
439                 /* Ignore . and .. entries. */
440                 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
441                         continue;
442
443                 snprintf(fnbuf, sizeof(fnbuf), "%s/%s", dirname, de->d_name);
444
445                 node = fnbuf;
446                 lstat(fnbuf, &st);
447                 if(S_ISLNK(st.st_mode))
448                 {
449                         int len;
450                         len = readlink(fnbuf, linkbuf, sizeof(linkbuf)-1);
451                         if(len!=-1)
452                         {
453                                 linkbuf[len] = 0;
454                                 node = linkbuf;
455                         }
456                 }
457
458                 /* There may be multiple symlinks to the same device.  Only include each
459                 device once in the returned array. */
460                 if(checked)
461                 {
462                         for(i=0; (!duplicate && i<n_checked); ++i)
463                                 if(strcmp(node, checked[i])==0)
464                                         duplicate = 1;
465                 }
466                 if(duplicate)
467                 {
468                         if(verbosity>=2)
469                                 printf("Device %s is a duplicate\n", fnbuf);
470                         continue;
471                 }
472
473                 checked = (char **)realloc(checked, (n_checked+1)*sizeof(char *));
474                 checked[n_checked] = strdup(node);
475                 ++n_checked;
476
477                 nodes = (char **)realloc(nodes, (n_nodes+2)*sizeof(char *));
478                 nodes[n_nodes] = strdup(fnbuf);
479                 ++n_nodes;
480         }
481
482         closedir(dir);
483         if(checked)
484         {
485                 for(i=0; i<n_checked; ++i)
486                         free(checked[i]);
487                 free(checked);
488         }
489
490         if(nodes)
491                 nodes[n_nodes] = NULL;
492
493         return nodes;
494 }
495
496 /** Returns an array of all mountable devices. */
497 Device *get_devices(void)
498 {
499         char **nodes = NULL;
500         Device *devices = NULL;
501         int n_devices = 0;
502         char **mounted = NULL;
503         char **fstab = NULL;
504         int i;
505
506         nodes = get_device_nodes("/dev/disk/by-id");
507         mounted = get_mounted_devices();
508         fstab = get_fstab_devices();
509
510         for(i=0; nodes[i]; ++i)
511         {
512                 Property *props;
513
514                 if(verbosity>=1)
515                         printf("Examining device %s\n", nodes[i]);
516
517                 props = get_device_properties(nodes[i]);
518                 if(!props)
519                 {
520                         if(verbosity>=2)
521                                 printf("  No properties\n");
522                         continue;
523                 }
524
525                 if(verbosity>=2)
526                 {
527                         int j;
528                         for(j=0; props[j].name; ++j)
529                                 printf("  %s = %s\n", props[j].name, props[j].value);
530                 }
531
532                 if(can_mount(props, fstab))
533                 {
534                         char *devname;
535                         char *label;
536                         char *vendor;
537                         char *model;
538                         char buf[256];
539                         char pos;
540                         struct stat st;
541
542                         if(verbosity>=1)
543                                 printf("  Using device\n");
544
545                         devname = get_property_value(props, "DEVNAME");
546
547                         /* Get a human-readable label for the device.  Use filesystem label,
548                         filesystem UUID or device node name in order of preference. */
549                         label = get_property_value(props, "ID_FS_LABEL");
550                         if(!label)
551                                 label = get_property_value(props, "ID_FS_UUID");
552                         if(!label)
553                         {
554                                 char *ptr;
555
556                                 label = devname;
557                                 for(ptr=label; *ptr; ++ptr)
558                                         if(*ptr=='/')
559                                                 label = ptr+1;
560                         }
561
562                         vendor = get_property_value(props, "ID_VENDOR");
563                         model = get_property_value(props, "ID_MODEL");
564
565                         pos = snprintf(buf, sizeof(buf), "%s", label);
566                         if(vendor && model)
567                                 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
568
569                         stat(nodes[i], &st);
570
571                         /* Reserve space for a sentinel entry. */
572                         devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
573                         devices[n_devices].node = nodes[i];
574                         devices[n_devices].label = strdup(label);
575                         devices[n_devices].description = strdup(buf);
576                         devices[n_devices].mounted = is_in_array(mounted, devname);
577                         devices[n_devices].time = st.st_mtime;
578                         ++n_devices;
579                 }
580                 else
581                         free(nodes[i]);
582                 free_properties(props);
583         }
584
585         free(nodes);
586         free_string_array(mounted);
587
588         if(devices)
589         {
590                 /* Terminate the array with NULL pointers. */
591                 devices[n_devices].node = NULL;
592                 devices[n_devices].label = NULL;
593                 devices[n_devices].description = NULL;
594         }
595
596         return devices;
597 }
598
599 /**
600 Frees an array of devices and all strings contained in it.
601 */
602 void free_devices(Device *devices)
603 {
604         int i;
605         if(!devices)
606                 return;
607         for(i=0; devices[i].node; ++i)
608         {
609                 free(devices[i].node);
610                 free(devices[i].label);
611                 free(devices[i].description);
612         }
613         free(devices);
614 }
615
616 /**
617 Mounts a device if it was not mounted, or unmounts if it was.
618 */
619 int toggle_device(Device *device, char *out_buf, int out_size)
620 {
621         int umount = !!device->mounted;
622         int pos = 0;
623         int status = 0;
624         fd_set fds;
625         struct timeval timeout;
626         int pid;
627         int pipe_fd[2];
628
629         out_buf[0] = 0;
630
631         pipe(pipe_fd);
632
633         pid = fork();
634         if(pid==0)
635         {
636                 /* Child process */
637                 if(verbosity>=1)
638                 {
639                         if(umount)
640                                 printf("Running pumount %s\n", device->node);
641                         else
642                                 printf("Running pmount %s %s\n", device->node, device->label);
643                 }
644
645                 close(pipe_fd[0]);
646                 dup2(pipe_fd[1], 1);
647                 dup2(pipe_fd[1], 2);
648
649                 if(umount)
650                         execl("/usr/bin/pumount", "pumount", device->node, NULL);
651                 else
652                         execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
653                 _exit(1);
654         }
655         else if(pid<0)
656                 return -1;
657
658         /* Parent process */
659
660         close(pipe_fd[1]);
661         FD_ZERO(&fds);
662         FD_SET(pipe_fd[0], &fds);
663         timeout.tv_sec = 0;
664         timeout.tv_usec = 200000;
665
666         while(1)
667         {
668                 /* The write fd for the pipe may be inherited by a fuse server
669                 process and stay open indefinitely. */
670                 if(select(pipe_fd[0]+1, &fds, NULL, NULL, &timeout))
671                 {
672                         int len;
673
674                         len = read(pipe_fd[0], out_buf+pos, out_size-pos-1);
675                         if(len<=0)
676                                 break;
677                         pos += len;
678                 }
679                 else if(waitpid(pid, &status, 0))
680                 {
681                         pid = 0;
682                         break;
683                 }
684         }
685
686         close(pipe_fd[0]);
687         if(pid)
688                 waitpid(pid, &status, 0);
689
690         out_buf[pos] = 0;
691
692         if(verbosity>=1)
693         {
694                 if(WIFEXITED(status))
695                 {
696                         if(WEXITSTATUS(status))
697                                 printf("Command exited successfully\n");
698                         else
699                                 printf("Command exited with status %d\n", WEXITSTATUS(status));
700                 }
701                 else if(WIFSIGNALED(status))
702                         printf("Command terminated with signal %d\n", WTERMSIG(status));
703                 else
704                         printf("Command exited with unknown result %04X\n", status);
705         }
706
707         if(!WIFEXITED(status) || WEXITSTATUS(status))
708                 return -1;
709
710         device->mounted = !device->mounted;
711
712         return 0;
713 }
714
715 /**
716 Callback for activating a row in the device list.  Mounts or unmounts the
717 device depending on operating mode.
718 */
719 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
720 {
721         GtkTreeModel *model;
722         GtkTreeIter iter;
723         Device *device;
724         int ret;
725         char output[1024];
726         int pid;
727
728         model = gtk_tree_view_get_model(list);
729
730         if(!gtk_tree_model_get_iter(model, &iter, path))
731                 return;
732
733         gtk_tree_model_get(model, &iter, 1, &device, -1);
734         ret = toggle_device(device, output, sizeof(output));
735         if(ret)
736         {
737                 GtkWidget *dialog;
738
739                 /* Pmount terminated with nonzero status or a signal.  Display an
740                 error to the user. */
741                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", output);
742                 g_signal_connect(dialog, "response", &gtk_main_quit, NULL);
743                 gtk_widget_show_all(dialog);
744                 return;
745         }
746
747         gtk_main_quit();
748
749         if(post_mount_command && device->mounted)
750         {
751                 char workdir[256];
752                 int len;
753                 len = snprintf(workdir, sizeof(workdir), "/media/%s", device->label);
754                 if(len<(int)sizeof(workdir))
755                 {
756                         if(verbosity>=1)
757                                 printf("Running %s in %s\n", post_mount_command, workdir);
758
759                         pid = fork();
760                         if(pid==0)
761                         {
762                                 chdir(workdir);
763                                 execlp(post_mount_command, post_mount_command, NULL);
764                                 _exit(1);
765                         }
766                 }
767         }
768
769         (void)column;
770         (void)user_data;
771 }
772
773 /**
774 Callback for the mount/unmount button.  Causes the selected row in the device
775 list to be activated.
776 */
777 void button_clicked(GtkButton *button, gpointer user_data)
778 {
779         GtkWidget *list = (GtkWidget *)user_data;
780         GtkTreeSelection *selection;
781         GtkTreeIter iter;
782         GtkTreeModel *model;
783         GtkTreePath *path;
784
785         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
786         gtk_tree_selection_get_selected(selection, &model, &iter);
787         path = gtk_tree_model_get_path(model, &iter);
788         gtk_tree_view_row_activated(GTK_TREE_VIEW(list), path, gtk_tree_view_get_column(GTK_TREE_VIEW(list), 0));
789         gtk_tree_path_free(path);
790
791         (void)button;
792 }
793
794 /**
795 Global key press callback for the window.
796 */
797 gboolean key_press(GtkWidget *widget, GdkEvent *event, gpointer user_data)
798 {
799         if(event->key.keyval==GDK_KEY_Escape)
800         {
801                 gtk_main_quit();
802                 return TRUE;
803         }
804
805         (void)widget;
806         (void)user_data;
807
808         return FALSE;
809 }
810
811 void show_help(void)
812 {
813         printf("pmount-gui\n"
814                 "Copyright (c) 2011-2015 Mikko Rasa, Mikkosoft Productions\n\n"
815                 "Usage: pmount-gui [-v] [-u] [-r <command>] [-h]\n\n"
816                 "Options:\n"
817                 "  -v  Increase verbosity\n"
818                 "  -u  Unmount a device (default is mount)\n"
819                 "  -r  Run a command after mounting\n"
820                 "  -h  Display this help\n");
821 }
822
823 int main(int argc, char **argv)
824 {
825         GtkWidget *window;
826         GtkWidget *box;
827         GtkWidget *viewport;
828         GtkWidget *list;
829         GtkListStore *store;
830         GtkTreeSelection *selection;
831         GtkWidget *button;
832         GtkTreeIter iter;
833         Device *devices;
834         int i;
835         time_t latest;
836         int opt;
837         int umount = 0;
838         int n_listed;
839
840         gtk_init(&argc, &argv);
841
842         while((opt = getopt(argc, argv, "vur:h"))!=-1) switch(opt)
843         {
844         case 'v':
845                 ++verbosity;
846                 break;
847         case 'u':
848                 umount = 1;
849                 break;
850         case 'r':
851                 post_mount_command = optarg;
852                 break;
853         case 'h':
854                 show_help();
855                 return 0;
856         }
857
858         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
859         gtk_container_set_border_width(GTK_CONTAINER(window), 5);
860         g_signal_connect(window, "destroy", G_CALLBACK(&gtk_main_quit), NULL);
861         g_signal_connect(window, "key-press-event", G_CALLBACK(&key_press), NULL);
862
863         box = gtk_vbox_new(FALSE, 5);
864         gtk_container_add(GTK_CONTAINER(window), box);
865
866         viewport = gtk_viewport_new(NULL, NULL);
867         gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
868         gtk_box_pack_start(GTK_BOX(box), viewport, TRUE, TRUE, 0);
869
870         list = gtk_tree_view_new();
871         gtk_container_add(GTK_CONTAINER(viewport), list);
872         g_signal_connect(list, "row-activated", G_CALLBACK(&row_activated), NULL);
873
874         store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
875         gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
876         gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
877                 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
878
879         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
880
881         button = gtk_button_new_with_label(umount ? "Unmount" : "Mount");
882         g_signal_connect(button, "clicked", G_CALLBACK(&button_clicked), list);
883         gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0);
884
885         devices = get_devices();
886         n_listed = 0;
887         if(devices)
888         {
889                 /* Populate the list with devices in appropriate state. */
890                 latest = 0;
891                 for(i=0; devices[i].node; ++i)
892                         if(!devices[i].mounted==!umount)
893                         {
894                                 gtk_list_store_append(store, &iter);
895                                 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
896                                 if(devices[i].time>latest)
897                                 {
898                                         /* Pre-select the device that appeared on the system most recently. */
899                                         latest = devices[i].time;
900                                         gtk_tree_selection_select_iter(selection, &iter);
901                                 }
902
903                                 ++n_listed;
904                         }
905
906         }
907
908         if(n_listed)
909                 gtk_widget_show_all(window);
910         else
911         {
912                 GtkWidget *dialog;
913
914                 /* Don't show the main window if no devices were listed. */
915                 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
916                         "No devices to %s", (umount ? "unmount" : "mount"));
917                 g_signal_connect(dialog, "response", G_CALLBACK(&gtk_main_quit), NULL);
918                 gtk_widget_show_all(dialog);
919         }
920
921         gtk_main();
922
923         free_devices(devices);
924
925         return 0;
926 }