Showing accounts
Showing accounts in a GtkComboBox
The basic idea
Using the platform factory you ask for the account store. With the account store you fill up a TnyList instance. That TnyList can be a TnyGtkAccountListModel. You can create it using tny_gtk_account_list_model_new. This type in Tinymail implements both GtkTreeModel and TnyList. Because it implements GtkTreeModel you can use it with widgets like GtkTreeView and GtkComboBox.
Sample code for displaying and filling the combo box
static void
create_ui_in (GtkContainer *container)
{
TnyList *model;
GtkComboBox *view;
GtkCellRenderer *renderer;
TnyPlatformFactory *platfact;
TnyAccountStore *account_store;
platfact = tny_gnome_platform_factory_get_instance ();
account_store = tny_platform_factory_new_account_store (platfact);
renderer = gtk_cell_renderer_text_new();
view = GTK_COMBO_BOX (gtk_combo_box_new ());
gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (view), renderer, TRUE);
gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (view), renderer,
"text", 0, NULL);
model = tny_gtk_account_list_model_new ();
tny_account_store_get_accounts (account_store, model,
TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
gtk_combo_box_set_model (view, GTK_TREE_MODEL (model));
g_object_unref (account_store);
g_object_unref (platfact);
gtk_widget_show (GTK_WIDGET (view));
gtk_container_add (container, GTK_WIDGET (view));
return;
}
Sample code for getting the selected account
You can use the GtkComboBox and GtkTreeModel API on top of the TnyList API with the TnyGtkAccountListModel. Note however that the GtkTreeModel and TnyList APIs are not guaranteed to be auto synchronized w.r.t. each other.
Important to know is that when requesting the instance using gtk_tree_model_get on a G_TYPE_OBJECT columns, a reference gets added. The something_INSTANCE_COLUMNs in Tinymail are all G_TYPE_OBJECT columns. You must remove that reference once you are finished with the instance.
It's a good practice to test the retrieved value for being not-NULL. Under normal circumstances it should never be NULL in this case. Else gtk_combo_box_get_active_iter would have returned FALSE.
static void
on_account_selected (GtkComboBox *widget, gpointer user_data)
{
GtkTreeIter iter;
GtkTreeModel *model = gtk_combo_box_get_model (widget);
if (gtk_combo_box_get_active_iter (widget, &iter)) {
TnyAccount *account = NULL;
gtk_tree_model_get (model, &iter,
TNY_GTK_ACCOUNT_LIST_MODEL_INSTANCE_COLUMN,
&account, -1);
if (account) {
g_print ("Account %s selected\n",
tny_account_get_name (account));
g_object_unref (account);
}
}
}
static void
create_ui_in (GtkContainer *container)
{
...
gtk_widget_show (GTK_WIDGET (view));
+ g_signal_connect (G_OBJECT (view), "changed",
+ G_CALLBACK (on_account_selected), view);
gtk_container_add (container, GTK_WIDGET (view));
return;
}
Using store accounts as TnyFolderStore instances
Some account instances also implement the TnyFolderStore API. Because we specified to get just the TNY_ACCOUNT_STORE_STORE_ACCOUNTS accounts, we are certain that we will only get account types that are also store accounts. An account that is a store account type must implement the TnyFolderStore interface. Therefore you are certain that the instances in your combo box are all instances that can cope with the TnyFolderStore API.
How to use and show folder stores is explained in showing folder stores
