-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconflict-dialog.c
171 lines (145 loc) · 5.38 KB
/
conflict-dialog.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include "conflict-dialog.h"
#include "conflict.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef enum
{
NAME_COLUMN,
CHOSEN_STRING_COLUMN,
CONFLICT_COLUMN,
LIST_CONFLICT_MODEL,
N_MODEL_CONFLICTS
} MODEL_CONFLICTS_COLUMNS;
typedef enum
{
CHOICE_STRING_COLUMN,
CHOICE_ATTR_COLUMN,
N_MODEL_CHOICE
} MODEL_CHOICE;
static void ecm_conflict_dialog_choice_changed(GtkCellRendererCombo *combo, gchar *path_string, GtkTreeIter *new_iter, gpointer user_data)
{
printf("choice changed !\n");
ECMConflictDialog* dialog = (ECMConflictDialog*)user_data;
GtkTreeSelection* selection = gtk_tree_view_get_selection(dialog->view);
GtkTreeIter iter;
GtkTreeModel* model;
if (gtk_tree_selection_get_selected(selection, &model, &iter) == FALSE)
{
printf("strange ... no line is selected ...\n");
return;
}
GtkTreeModel* combo_model = NULL; ECMConflict* conflict = NULL;
gtk_tree_model_get(model, &iter, LIST_CONFLICT_MODEL, &combo_model, CONFLICT_COLUMN, &conflict, -1);
char* selected_string = NULL; EVCardAttribute* selected_attr = NULL;
gtk_tree_model_get(combo_model, new_iter, CHOICE_STRING_COLUMN, &selected_string, CHOICE_ATTR_COLUMN, &selected_attr, -1);
printf("selected element : %s\n", selected_string);
gtk_list_store_set((GtkListStore*)model, &iter, CHOSEN_STRING_COLUMN, selected_string, -1);
free(selected_string);
conflict->chosen_attr = selected_attr;
}
static void setup_treeview(ECMConflictDialog* dialog)
{
dialog->store = gtk_list_store_new(N_MODEL_CONFLICTS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER, GTK_TYPE_TREE_MODEL);
gtk_tree_view_set_model(dialog->view, (GtkTreeModel*)dialog->store);
GtkCellRenderer* renderer;
GtkTreeViewColumn* column;
renderer = gtk_cell_renderer_text_new();
column = gtk_tree_view_column_new_with_attributes("Name", renderer, "text", NAME_COLUMN, NULL);
gtk_tree_view_append_column(dialog->view, column);
renderer = gtk_cell_renderer_combo_new();
g_object_set(renderer, "text-column", CHOICE_STRING_COLUMN, "editable", TRUE, "has-entry", FALSE, NULL);
column = gtk_tree_view_column_new_with_attributes("Choice", renderer, "text", CHOSEN_STRING_COLUMN, "model", LIST_CONFLICT_MODEL, NULL);
gtk_tree_view_append_column(dialog->view, column);
g_signal_connect(renderer, "changed", G_CALLBACK(ecm_conflict_dialog_choice_changed), (void*)dialog);
gtk_tree_selection_set_mode(gtk_tree_view_get_selection(dialog->view), GTK_SELECTION_SINGLE);
}
ECMConflictDialog* ecm_conflict_dialog_new()
{
ECMConflictDialog* d = (ECMConflictDialog*)malloc(sizeof(ECMConflictDialog));
d->builder = gtk_builder_new();
char* filename = g_strconcat(PLUGIN_PATH, "/org-gnome-contacts-merge.ui", NULL);
printf("loading ui %s\n", filename);
GError *error = NULL;
unsigned int ret = gtk_builder_add_from_file(d->builder, filename, &error);
g_free(filename);
if (ret == 0)
{
printf("unable to load ui : %s\n", error->message);
g_error_free(error);
ecm_conflict_dialog_free(d);
return NULL;
}
d->dialog = (GtkDialog*)gtk_builder_get_object(d->builder, "dialog_conflicts");
d->view = (GtkTreeView*)gtk_builder_get_object(d->builder, "treeview_conflicts");
if (d->dialog == NULL || d->view == NULL)
{
printf("unable to find ids in ui file\n");
ecm_conflict_dialog_free(d);
return NULL;
}
setup_treeview(d);
gtk_builder_connect_signals(d->builder, d);
gtk_widget_show_all((GtkWidget*)d->dialog);
d->conflicts = NULL;
return d;
}
void ecm_conflict_dialog_free(ECMConflictDialog* dialog)
{
if (dialog->dialog != NULL)
{
gtk_widget_destroy((GtkWidget*)dialog->dialog);
}
if (dialog->builder != NULL)
{
g_object_unref((GObject*)dialog->builder);
}
if (dialog->store != NULL)
{
g_object_unref((GObject*)dialog->store);
}
free(dialog);
}
gboolean ecm_conflict_dialog_populate(ECMConflictDialog* dialog, GHashTable* conflicts)
{
unsigned int nb_conflicts = 0;
dialog->conflicts = conflicts;
GHashTableIter iter;
char* key_attr_name; ECMConflict* value_conflict;
g_hash_table_iter_init(&iter, dialog->conflicts);
while (g_hash_table_iter_next(&iter, (void**)&key_attr_name, (void**)&value_conflict))
{
if (value_conflict->is_unique == TRUE && value_conflict->attr_array->len > 1)
{
nb_conflicts++;
GtkListStore* list_values = gtk_list_store_new(N_MODEL_CHOICE, G_TYPE_STRING, G_TYPE_POINTER);
int i;
for (i = 0; i < value_conflict->attr_array->len; ++i)
{
GtkTreeIter storeIter;
gtk_list_store_append(list_values, &storeIter);
char* s = ecm_conflict_attr_value_string(g_ptr_array_index(value_conflict->attr_array, i));
gtk_list_store_set(list_values, &storeIter, CHOICE_STRING_COLUMN, s, CHOICE_ATTR_COLUMN, g_ptr_array_index(value_conflict->attr_array, i), -1);
free(s);
}
GtkTreeIter storeIter;
gtk_list_store_append(dialog->store, &storeIter);
EContactField ecf = e_contact_field_id_from_vcard(key_attr_name);
const char* pretty_name = e_contact_pretty_name(ecf);
if (strlen(pretty_name) < 1)
{
pretty_name = key_attr_name;
}
gtk_list_store_set(dialog->store, &storeIter, NAME_COLUMN, pretty_name, CHOSEN_STRING_COLUMN, "(default)", CONFLICT_COLUMN, value_conflict, LIST_CONFLICT_MODEL, list_values, -1);
g_object_unref(list_values);
}
}
if (nb_conflicts > 0)
{
return TRUE;
}
else
{
return FALSE;
}
}