+/* Connect to a virtual machine. This callback is called from the
+ * connect menu. It searches the notebook of gtk-vnc widgets to see
+ * if we have already connected to this machine, and if not it
+ * makes a new connection.
+ */
+static void
+connect_to_vm (GtkWidget *widget, gpointer _vm)
+{
+ struct vm *vm = (struct vm *) _vm;
+ int n = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
+ int i, uuidlen, len;
+ GtkWidget *child;
+ const char *label;
+ char *label2;
+
+ DEBUG ("searching tabs for uuid %s", vm->uuid);
+
+ uuidlen = strlen (vm->uuid);
+
+ /* Search the tabs for this UUID, and if found, switch to it and return. */
+ for (i = 0; i < n; ++i) {
+ child = gtk_notebook_get_nth_page (GTK_NOTEBOOK (notebook), i);
+ label = gtk_notebook_get_tab_label_text (GTK_NOTEBOOK (notebook), child);
+ len = strlen (label);
+ if (len >= uuidlen &&
+ STREQ (label + len - uuidlen, vm->uuid)) {
+ DEBUG ("found on tab %d", i);
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), i);
+ return;
+ }
+ }
+
+ DEBUG ("not found, creating new tab");
+
+ /* This VM isn't in the notebook already, so create a new console. */
+ len = strlen (vm->description) + 1 + strlen (vm->uuid) + 1;
+ label2 = g_alloca (len);
+ snprintf (label2, len, "%s %s", vm->description, vm->uuid);
+
+ child = gtk_label_new (label2); /* XXX */
+
+ /* NB. We have to do this before adding it to the notebook. */
+ gtk_widget_show (child);
+
+ i = gtk_notebook_append_page (GTK_NOTEBOOK (notebook), child, NULL);
+ gtk_notebook_set_tab_label_text (GTK_NOTEBOOK (notebook), child, label2);
+ gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), i);
+
+ DEBUG ("finished creating new tab");
+}
+