-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
425 lines (336 loc) · 9.65 KB
/
mainwindow.cpp
File metadata and controls
425 lines (336 loc) · 9.65 KB
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
#include <iostream>
#include <gtk/gtk.h>
#include "config.h"
#include "camsession.h"
#include "progressbar.h"
#include "pathsupport.h"
#include "prefsdialog.h"
#include "mainwindow.h"
#include "config.h"
#include "gettext.h"
#define _(x) gettext(x)
#define N_(x) gettext_noop(x)
using namespace std;
/* columns */
enum
{
ACTIVE_COLUMN = 0,
LABEL_COLUMN,
INFO_COLUMN,
TRUE_COLUMN,
CAMSESSION_COLUMN,
NUM_COLUMNS
};
static GtkTreeModel *create_model ()
{
GtkTreeStore *model;
/* create tree store */
model = gtk_tree_store_new (NUM_COLUMNS,
G_TYPE_BOOLEAN,
G_TYPE_STRING,
G_TYPE_STRING,
G_TYPE_BOOLEAN,
G_TYPE_POINTER
);
return GTK_TREE_MODEL (model);
}
static void
item_toggled (GtkCellRendererToggle *cell,gchar *path_str,gpointer data)
{
GtkTreeModel *model = (GtkTreeModel *)data;
GtkTreePath *path = gtk_tree_path_new_from_string (path_str);
GtkTreeIter iter;
gboolean toggle_item;
gint *column;
column = (gint *)g_object_get_data (G_OBJECT (cell), "column");
/* get toggled iter */
gtk_tree_model_get_iter (model, &iter, path);
gtk_tree_model_get (model, &iter, column, &toggle_item, -1);
/* do something with the value */
toggle_item ^= 1;
/* set new value */
gtk_tree_store_set (GTK_TREE_STORE (model), &iter, column,
toggle_item, -1);
gpointer p;
gtk_tree_model_get (model, &iter, CAMSESSION_COLUMN, &p, -1);
CamSession *s=(CamSession *)p;
s->SetIncluded(toggle_item);
/* clean up */
gtk_tree_path_free (path);
}
static int sortcomparefunc(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
{
char *name1,*name2;
gtk_tree_model_get(model,a,LABEL_COLUMN,&name1,-1);
gtk_tree_model_get(model,b,LABEL_COLUMN,&name2,-1);
if(!name1)
if(!name2)
return(0);
else
return(-1);
if(!name2)
return(1);
int r=strcmp(name1,name2);
g_free(name1);
g_free(name2);
return(r);
}
void MainWindow::add_columns (GtkTreeView *treeview)
{
gint col_offset;
GtkCellRenderer *renderer;
/* Active column */
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set_data (G_OBJECT (renderer), "column", (gint *)ACTIVE_COLUMN);
g_signal_connect (renderer, "toggled", G_CALLBACK (item_toggled), model);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, _("Include?"),
renderer,
"active",
ACTIVE_COLUMN,
"activatable",
TRUE_COLUMN,
NULL);
/* column for names */
renderer = gtk_cell_renderer_text_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
g_object_set (renderer, "xpad", 10, NULL);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, _("Date"),
renderer, "text",
LABEL_COLUMN,
NULL);
/* Info column */
renderer = gtk_cell_renderer_text_new ();
g_object_set (renderer, "xalign", 0.0, NULL);
col_offset = gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (treeview),
-1, _("Details"),
renderer,"text",
INFO_COLUMN,
NULL);
}
static void quit( GtkWidget *widget,
gpointer data )
{
gtk_main_quit ();
}
void MainWindow::srcdir_changed(GtkFileChooser *chooser,gpointer userdata)
{
MainWindow *mw=(MainWindow *)userdata;
if(mw->ignoresrcdirchanged)
return;
mw->ignoresrcdirchanged=true;
char *newsrc=gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (mw->sourcepathsel));
if(mw->src && (strcmp(newsrc,mw->src)==0))
cerr << "No change - ignoring" << endl;
else
{
mw->ClearList();
mw->SetSourceDir(newsrc);
}
mw->ignoresrcdirchanged=false;
}
#define RESPONSE_SETTINGS 1
MainWindow::MainWindow()
: sessionlist(NULL), configfilename(NULL),src(NULL), window(NULL), ignoresrcdirchanged(false)
{
GtkWidget *tmp;
sessionlist=new CamSessionList(&config);
bool rcfileexists=LoadSettings();
sessionlist->SetExtensions();
sessionlist->SetExclusions();
window=gtk_dialog_new_with_buttons(_("CamCardSync"),
NULL,GtkDialogFlags(0),
_("Settings..."),RESPONSE_SETTINGS,
GTK_STOCK_CANCEL,GTK_RESPONSE_CANCEL,
GTK_STOCK_OK,GTK_RESPONSE_OK,
NULL);
gtk_window_set_default_size (GTK_WINDOW (window), 650, 400);
g_signal_connect (window, "destroy",
G_CALLBACK (quit), &window);
// GtkWidget *vbox = GTK_DIALOG(window)->vbox;
GtkWidget *vbox=gtk_vbox_new(FALSE,8);
gtk_box_pack_start(GTK_BOX(GTK_DIALOG(window)->vbox),vbox,TRUE,TRUE,8);
GtkWidget *hbox=gtk_hbox_new(FALSE,0);
gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,5);
tmp=gtk_label_new(_("Copy files from: "));
gtk_box_pack_start(GTK_BOX(hbox),tmp,FALSE,FALSE,5);
sourcepathsel=gtk_file_chooser_button_new (_("Copy files to: "),GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
// Why do we receive multiple triggers from this signal?
g_signal_connect(sourcepathsel,"selection-changed",G_CALLBACK(srcdir_changed),this);
gtk_box_pack_start(GTK_BOX(hbox),sourcepathsel,TRUE,TRUE,0);
GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (sw),
GTK_SHADOW_ETCHED_IN);
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0);
/* create model */
model = create_model ();
GtkTreeSortable *sortable=GTK_TREE_SORTABLE(model);
gtk_tree_sortable_set_sort_func(sortable, LABEL_COLUMN, sortcomparefunc,0, NULL);
gtk_tree_sortable_set_sort_column_id(sortable, LABEL_COLUMN, GTK_SORT_ASCENDING);
/* create tree view */
GtkWidget *treeview = gtk_tree_view_new_with_model (model);
g_object_unref (model);
gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview), TRUE);
gtk_tree_selection_set_mode (gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview)),
GTK_SELECTION_MULTIPLE);
add_columns (GTK_TREE_VIEW (treeview));
gtk_container_add (GTK_CONTAINER (sw), treeview);
// End of UI definition.
// Run dialog
gtk_widget_show_all (window);
while(gtk_events_pending())
gtk_main_iteration_do(false);
if(!rcfileexists)
PrefsDialog(this);
}
MainWindow::~MainWindow()
{
if(sessionlist)
delete sessionlist;
if(configfilename)
free(configfilename);
if(src)
free(src);
}
void MainWindow::Run()
{
bool done=false;
while(!done)
{
gint result=gtk_dialog_run(GTK_DIALOG(window));
switch(result)
{
case RESPONSE_SETTINGS:
if(PrefsDialog(this))
{
// If prefs dialog returned true, then we
// must rebuild the file list, since the extensions
// could have changed.
ClearList();
ProgressBar pb("Scanning media for files...",true,window);
const char *src=sessionlist->FindString("DefaultSourcePath");
sessionlist->AddPath(src,&pb);
PopulateList();
}
break;
case GTK_RESPONSE_CANCEL:
cerr << "Cancelled" << endl;
done=true;
break;
case GTK_RESPONSE_OK:
Go();
cerr << "Clicked OK" << endl;
done=true;
break;
}
}
}
GtkWidget *MainWindow::GetWindow()
{
return(window);
}
void MainWindow::ClearList()
{
gtk_tree_store_clear(GTK_TREE_STORE(model));
CamSession *cs;
while((cs=sessionlist->FirstDay()))
delete cs;
}
void MainWindow::PopulateList()
{
GtkTreeIter iter;
/* add data to the tree store */
const char *destdir=sessionlist->FindString("DestPath");
sessionlist->SetIncludedDefaults(destdir);
CamSession *s=sessionlist->FirstDay();
while (s)
{
char *fmt=_("%.1f megabytes in %d image%c");
char *infostring=(char *)malloc(strlen(fmt)+16);
int mbc=(s->GetFileSize()*10+1023)/1024;
float mbc2=mbc/10.0;
int fc=s->GetFileCount();
sprintf(infostring,fmt,mbc2,fc,(fc==1) ? ' ':'s');
cerr << infostring << endl;
gtk_tree_store_append (GTK_TREE_STORE(model), &iter, NULL);
gtk_tree_store_set (GTK_TREE_STORE(model), &iter,
ACTIVE_COLUMN, s->GetIncluded(),
LABEL_COLUMN, s->GetDirName(),
INFO_COLUMN, infostring,
TRUE_COLUMN, TRUE,
CAMSESSION_COLUMN, s,
-1);
free(infostring);
s=s->NextDay();
}
}
void MainWindow::SetSourceDir(const char *srcdir)
{
if(src)
free(src);
if(srcdir)
src=strdup(srcdir);
else
{
srcdir=sessionlist->FindString("DefaultSourcePath");
// If the default path doesn't exist, use a current dir instead.
struct stat statbuf;
if(stat(srcdir,&statbuf)!=0)
src=g_get_current_dir();
else
src=strdup(srcdir);
}
cerr << "Setting path to: " << src << endl;
// If the source dir is being changed as a result of a signal from the source
// directory selector, then we don't want to update the directory selector.
if(!ignoresrcdirchanged)
{
ignoresrcdirchanged=true;
if(g_path_is_absolute(src))
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(sourcepathsel),src);
else
{
gchar *cd=g_get_current_dir();
gchar *path=g_build_filename(cd,src,NULL);
gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(sourcepathsel),path);
g_free(path);
g_free(cd);
}
}
ProgressBar pb("Scanning media for files...",true,window);
sessionlist->SetString("DefaultSourcePath",src);
sessionlist->AddPath(src,&pb);
PopulateList();
ignoresrcdirchanged=false;
}
void MainWindow::Go()
{
const char *dst=sessionlist->FindString("DestPath");
ImageCopyStats stats;
if(dst)
{
ProgressBar pb(_("Copying files..."),true,window);
sessionlist->Copy(dst,&stats,&pb);
}
// Display stats
float fsize=(10*stats.copiedsize+1023)/1024;
fsize/=10.0;
GtkWidget *dlg=gtk_message_dialog_new(GTK_WINDOW(window),GtkDialogFlags(0),
GTK_MESSAGE_INFO,GTK_BUTTONS_OK,
_("Copied %.1f mb in %d files.\n%d file%sskipped.\n"),fsize,stats.copied,stats.skipped,stats.skipped==1 ? " " : "s ");
gtk_dialog_run(GTK_DIALOG(dlg));
}
void MainWindow::SaveSettings()
{
config.SaveFile(configfilename);
}
bool MainWindow::LoadSettings()
{
if(!configfilename)
configfilename=substitute_homedir("$HOME/.camcardsyncrc");
return(config.ParseFile(configfilename));
}