File: action.c

package info (click to toggle)
tilp2 1.12-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 4,936 kB
  • ctags: 1,292
  • sloc: ansic: 9,842; sh: 9,147; cpp: 255; makefile: 184; sed: 16
file content (445 lines) | stat: -rw-r--r-- 11,550 bytes parent folder | download
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/* Hey EMACS -*- linux-c -*- */
/* $Id: action.c 4112 2007-12-30 23:31:59Z kevinkofler $ */

/*  TiLP - Tilp Is a Linking Program
 *  Copyright (C) 1999-2006  Romain Lievin
 *  Copyright (C) 2007  Kevin Kofler
 *
 *  This program is free software you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif				/*  */

#include <gtk/gtk.h>
#include <glade/glade.h>
#include <stdlib.h>
#include <string.h>

#include "action.h"
#include "support.h"
#include "tilp_core.h"
#include "ctree.h"
#include "labels.h"

static GtkWidget *clist;
static GtkListStore *list;

// be able to modify var attribute

enum 
{ 
	COLUMN_VAR, COLUMN_ATTR, COLUMN_FILE, COLUMN_ACTION,
	COLUMN_DATA_F, COLUMN_DATA_V, COLUMN_DATA_N, COLUMN_NUMBER
};

#define CLIST_NVCOLS	(4)		// 4 visible columns
#define CLIST_NCOLS		(8)		// 6 real columns

static gboolean select_function(GtkTreeSelection * selection,
				GtkTreeModel * model,
				GtkTreePath * path,
				gboolean path_currently_selected,
				gpointer data)
{
	GtkTreeIter iter;
	FileEntry *fe;
	int n;

	gtk_tree_model_get_iter(model, &iter, path);
	gtk_tree_model_get(model, &iter, COLUMN_DATA_F, &fe, COLUMN_DATA_N, &n, -1);

	if (gtk_tree_selection_path_is_selected(selection, path))
		fe->selected = 0;
	else
		fe->selected = !0;

	return TRUE;
}

static void create_clist(GtkWidget * clist_wnd)
{
	GtkTreeView *view = GTK_TREE_VIEW(clist_wnd);
	GtkTreeModel *model;
	GtkCellRenderer *renderer;
	GtkTreeSelection *sel;
	gint i;

	list = gtk_list_store_new(COLUMN_NUMBER, 
				G_TYPE_STRING, G_TYPE_STRING, 
				G_TYPE_STRING, G_TYPE_STRING, 
				G_TYPE_POINTER, G_TYPE_POINTER, G_TYPE_INT);
	model = GTK_TREE_MODEL(list);
	
	gtk_tree_view_set_model(view, model);
	gtk_tree_view_set_headers_visible(view, TRUE);
	gtk_tree_view_set_headers_clickable(view, TRUE);
	gtk_tree_view_set_rules_hint(view, FALSE);

	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(view, -1,
						    _("Varname"),
						    renderer, "text",
						    COLUMN_VAR, NULL);
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(view, -1,
						    _("Attribute"),
						    renderer, "text",
						    COLUMN_ATTR, NULL);
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(view, -1,
						    _("Filename"),
						    renderer, "text",
						    COLUMN_FILE, NULL);
	renderer = gtk_cell_renderer_text_new();
	gtk_tree_view_insert_column_with_attributes(view, -1,
						    _("Action"),
						    renderer, "text",
						    COLUMN_ACTION, NULL);

	for (i = 0; i < CLIST_NVCOLS - 1; i++) 
	{
	  GtkTreeViewColumn *col;
	  col = gtk_tree_view_get_column(view, i);
	  gtk_tree_view_column_set_resizable(col, TRUE);
	}
	
	sel = gtk_tree_view_get_selection(view);
	gtk_tree_selection_set_mode(sel, GTK_SELECTION_MULTIPLE);
	gtk_tree_selection_set_select_function(sel, select_function, NULL, NULL);
}

static const char* action2string(int action)
{
	switch(action)
	{
	case ACT_NONE:   return _("none"); break;
	case ACT_RENAME: return _("rename"); break;
	case ACT_OVER:   return _("overwrite"); break;
	case ACT_SKIP:   return _("skip"); break;
	}

	return "";
}

gint display_action_dbox(gchar *target)
{
	GladeXML *xml;
	GtkWidget *dbox;
	GtkWidget *data;
	GtkTreeIter iter;
	GList *sel;
	int button = 0;
	gint result;
	gboolean empty = TRUE;

	// update folder listing (if supported)
	if (!(ticalcs_calc_features(calc_handle) & FTS_SILENT) )
		return BUTTON1;
	else
	{
		if(remote.var_tree == NULL)
		{
			if (tilp_dirlist_remote())
				return BUTTON1;

			ctree_refresh();
			labels_refresh();
		}
	}

	// box creation
	xml = glade_xml_new(tilp_paths_build_glade("action-2.glade"), "action_dbox", PACKAGE);
	if (!xml)
		g_error(_("action.c: GUI loading failed !\n"));
	glade_xml_signal_autoconnect(xml);

	dbox = glade_xml_get_widget(xml, "action_dbox");
	gtk_dialog_set_alternative_button_order(GTK_DIALOG(dbox), GTK_RESPONSE_OK,
	                                        GTK_RESPONSE_CANCEL,-1);
	clist = data = glade_xml_get_widget(xml, "treeview1");

	// clist creation
	create_clist(data);

	// fill model
	for (sel = local.selection1; sel != NULL; sel = sel->next) 
	{
		FileEntry *f = (FileEntry *)sel->data;
		FileContent *c = (FileContent *)f->content1;

		if(f->content1 == NULL)	// file can't be loaded
			continue;

		// we have now 1 VarEntry per FileContent
		{
			VarEntry *v = c->entries[0];			
			VarEntry *w;
			gchar **row_text = g_malloc0(5 * sizeof(gchar *));
			char *trans;
			char full_name[19];
			int appv = (options.calc_model == CALC_TI83P || options.calc_model != CALC_TI84P) && (v->type == TI83p_APPV);

			// modify attr or folder if target is specified
			if(strcmp(target, ""))
			{
				if(!strcmp(target, "<FLASH>") && !appv)
				{
					v->attr = ATTRB_ARCHIVED;
				}
				else
				{
					strcpy(v->folder, target);
				}
			}

			// search for matching var
			tifiles_build_fullname(options.calc_model, full_name, v->folder, v->name);
			trans = ticonv_varname_to_utf8(options.calc_model, (const char *)full_name, v->type);

			w = ticalcs_dirlist_ve_exist(remote.var_tree, v);
			if (w == NULL)
				continue;

			if(w->attr == ATTRB_LOCKED || w->attr == ATTRB_ARCHIVED)
				v->action = ACT_SKIP;
			else
				v->action = ACT_SKIP;	//don't overwrite as default behaviour

			// file contains an already existing var: add it to the window
			row_text[0] = trans;
			row_text[1] = g_strdup(tifiles_attribute_to_string(v->attr));
			row_text[2] = g_strdup(f->name);
			row_text[3] = g_strdup(action2string(v->action));
			tilp_vars_translate(row_text[0]);

			gtk_list_store_append(list, &iter);
			gtk_list_store_set(list, &iter,
					   COLUMN_VAR,  row_text[0],
					   COLUMN_ATTR, row_text[1],
					   COLUMN_FILE, row_text[2],
					   COLUMN_ACTION, row_text[3],
					   COLUMN_DATA_F, (gpointer)f, 
					   COLUMN_DATA_V, (gpointer)v, 
					   -1);
			g_strfreev(row_text);

			empty = FALSE;
		}
	}		

	if (empty == TRUE) 
	{
		button = BUTTON1; // skip box as ok
		goto out_clean;
	}

	// select all vars
	{
		GtkTreeView *view = GTK_TREE_VIEW(clist);
		GtkTreeSelection *sel;

		sel = gtk_tree_view_get_selection(view);
		gtk_tree_selection_select_all(sel);
	}

	// box running
	gtk_dialog_set_default_response(GTK_DIALOG(dbox), GTK_RESPONSE_CANCEL);
	result = gtk_dialog_run(GTK_DIALOG(dbox));
	switch (result) 
	{
	case GTK_RESPONSE_OK:
		button = BUTTON1;
		break;
	case GTK_RESPONSE_CANCEL:
		button = BUTTON2;
	default:
         button = BUTTON2;
		break;
	}

out_clean:
	gtk_widget_destroy(dbox);

	return button;
}

GLADE_CB void action_overwrite_clicked(GtkButton * button, gpointer user_data)
{
	GtkTreeModel *model = GTK_TREE_MODEL(list);
	GtkTreeIter iter;
	gboolean valid;

	for (valid = gtk_tree_model_get_iter_first(model, &iter); valid;
	     valid = gtk_tree_model_iter_next(model, &iter)) 
	{
		FileEntry *f;
		VarEntry *v;
		int n;

		gtk_tree_model_get(model, &iter, 
			COLUMN_DATA_F, &f, COLUMN_DATA_V, &v, COLUMN_DATA_N, &n,
			-1);
		
		if (!f->selected)
			continue;

		if (v->attr != ATTRB_NONE)
			continue;

		v->action = ACT_OVER;
		gtk_list_store_set(list, &iter, COLUMN_ACTION, _("overwrite"), -1);
	}
}

GLADE_CB void action_rename_clicked(GtkButton * button, gpointer user_data)
{
	GtkTreeModel *model = GTK_TREE_MODEL(list);
	GtkTreeIter iter;
	gboolean valid;

	for (valid = gtk_tree_model_get_iter_first(model, &iter); valid;
	     valid = gtk_tree_model_iter_next(model, &iter)) 
	{
		FileEntry *f;
		FileContent *c;
		VarEntry *v;
		VarEntry *w;
		int n;
	
		gchar *new_name = NULL;
		gchar **row_text = g_malloc0(5 * sizeof(gchar *));
		char *trans;
		char full_name[19];

		gtk_tree_model_get(model, &iter, 
				COLUMN_DATA_F, &f, COLUMN_DATA_V, &v, COLUMN_DATA_N, &n,
				-1);

		if (!f->selected)
			continue;

		// get new name
		c = f->content1;
		new_name = gif->msg_entry(_("Rename the file"), _("New name: "), tifiles_get_varname(v->name));
		if (new_name == NULL)
			continue;

		// check that new varname does not exist
		tifiles_build_fullname(options.calc_model, full_name, v->folder, new_name);
		g_free(new_name);
		w = ticalcs_dirlist_ve_exist(remote.var_tree, v);

		// update action
		v->attr = (w != NULL) ? w->attr : ATTRB_NONE;
		v->action = (v->attr != ATTRB_NONE) ? ACT_SKIP : ACT_RENAME;

		// update var entry
		strncpy(v->folder, tifiles_get_fldname(full_name), 8); v->folder[8] = 0;
		strncpy(v->name,   tifiles_get_varname(full_name), 8); v->name[8] = 0;
		trans = ticonv_varname_to_utf8(options.calc_model, (const char *)v->name, v->type);

		// update entry
		row_text[0] = g_strdup(trans); g_free(trans);
		row_text[1] = g_strdup(tifiles_attribute_to_string(v->attr));
		row_text[3] = g_strdup(action2string(v->action));
		gtk_list_store_set(list, &iter, 
					COLUMN_VAR, row_text[0],
				   COLUMN_ATTR, row_text[1], 
				   COLUMN_ACTION, row_text[3], -1);
		g_strfreev(row_text);   //bug
	}
}

GLADE_CB void action_skip_clicked(GtkButton * button, gpointer user_data)
{
	GtkTreeModel *model = GTK_TREE_MODEL(list);
	GtkTreeIter iter;
	gboolean valid;

	for (valid = gtk_tree_model_get_iter_first(model, &iter); valid;
	     valid = gtk_tree_model_iter_next(model, &iter)) 
	{
		FileEntry *f;
		VarEntry *v;
		int n;

		gtk_tree_model_get(model, &iter, 
			COLUMN_DATA_F, &f, COLUMN_DATA_V, &v, COLUMN_DATA_N, &n,
			-1);
		
		if (!f->selected)
			continue;

		v->action = ACT_SKIP;
		gtk_list_store_set(list, &iter, COLUMN_ACTION, "skip", -1);
	}
}

GLADE_CB void action_select_all_clicked(GtkButton * button, gpointer user_data)
{
	GtkTreeView *view = GTK_TREE_VIEW(clist);
	GtkTreeSelection *sel;
	
	sel = gtk_tree_view_get_selection(view);
	gtk_tree_selection_select_all(sel);
} 

GLADE_CB void action_deselect_all_clicked(GtkButton * button, gpointer user_data)
{
	GtkTreeView *view = GTK_TREE_VIEW(clist);
	GtkTreeSelection *sel;
	
	sel = gtk_tree_view_get_selection(view);
	gtk_tree_selection_unselect_all(sel);
}

GLADE_CB gboolean
action_treeview1_button_press_event(GtkWidget* widget, GdkEventButton* event, gpointer user_data)
{
	GtkTreeView *view = GTK_TREE_VIEW(widget);
	GtkTreeModel *model = GTK_TREE_MODEL(list);
	GtkTreePath *path;
	GtkTreeViewColumn *column;
	GtkTreeIter iter;
	gint tx = (gint) event->x;
	gint ty = (gint) event->y;
	gint cx, cy;

	if(event->type == GDK_2BUTTON_PRESS)
	{
		VarEntry *ve;

		gtk_tree_view_get_path_at_pos(view, tx, ty, &path, &column, &cx, &cy);

		if (path == NULL)
			return FALSE;

		gtk_tree_model_get_iter(model, &iter, path);
		gtk_tree_model_get(model, &iter, COLUMN_DATA_V, &ve, -1);

		if(ve->attr == ATTRB_NONE)
			ve->attr = ATTRB_LOCKED;
		else if(ve->attr == ATTRB_LOCKED)
			ve->attr = ATTRB_ARCHIVED;
		else if(ve->attr == ATTRB_ARCHIVED)
			ve->attr = ATTRB_NONE;

		gtk_list_store_set(list, &iter, COLUMN_ATTR, tifiles_attribute_to_string(ve->attr), -1);
	}

	return FALSE;
}