summaryrefslogtreecommitdiff
path: root/camlibs/canon/canon.c
blob: f57c4c308dee290494ab3caded29264443a62d66 (plain)
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
/***************************************************************************
 *
 * canon.c
 *
 *   Canon Camera library for the gphoto project,
 *   (c) 1999 Wolfgang G. Reissnegger
 *   Developed for the Canon PowerShot A50
 *   Additions for PowerShot A5 by Ole W. Saastad
 *   (c) 2000 : Other additions  by Edouard Lafargue, Philippe Marzouk.
 *
 * $Header$
 ****************************************************************************/


/****************************************************************************
 *
 * include files
 *
 ****************************************************************************/

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#include <time.h>
//#include <ctype.h>

#include <gphoto2.h>

#ifdef ENABLE_NLS
#  include <libintl.h>
#  undef _
#  define _(String) dgettext (PACKAGE, String)
#  ifdef gettext_noop
#    define N_(String) gettext_noop (String)
#  else
#    define N_(String) (String)
#  endif
#else
#  define textdomain(String) (String)
#  define gettext(String) (String)
#  define dgettext(Domain,Message) (Message)
#  define dcgettext(Domain,Message,Type) (Message)
#  define bindtextdomain(Domain,Directory) (Domain)
#  define _(String) (String)
#  define N_(String) (String)
#endif

#include "util.h"
#include "library.h"
#include "canon.h"
#include "serial.h"
#include "usb.h"

#ifndef HAVE_SNPRINTF
#warning You do not seem to have a snprintf() function. Using sprintf instead.
#warning Note that this leads to SECURITY RISKS!
#define snprintf(buf,size,format,arg) sprintf(buf,format,arg)
#endif

static struct
{
	char *name;
	unsigned short idVendor;
	unsigned short idProduct;
	char serial;
}
models[] =
{
	{
	"Canon PowerShot A5", 0, 0, 1}
	, {
	"Canon PowerShot A5 Zoom", 0, 0, 1}
	, {
	"Canon PowerShot A50", 0, 0, 1}
	, {
	"Canon PowerShot Pro70", 0, 0, 1}
	, {
	"Canon PowerShot S10", 0x04A9, 0x3041, 1}
	, {
	"Canon PowerShot S20", 0x04A9, 0x3043, 1}
	, {
	"Canon EOS D30", 0x04A9, 0x3044, 0}
	, {
	"Canon PowerShot S100", 0x04A9, 0x3045, 0}
	, {
	"Canon IXY DIGITAL", 0x04A9, 0x3046, 0}
	, {
	"Canon Digital IXUS", 0x04A9, 0x3047, 0}
	, {
	"Canon PowerShot G1", 0x04A9, 0x3048, 1}
	, {
	"Canon PowerShot G2", 0x04A9, 0x3055, 0}
	, {
	"Canon PowerShot Pro90 IS", 0x04A9, 0x3049, 1}
	, {
	"Canon IXY DIGITAL 300", 0x04A9, 0x304B, 0}
	, {
	"Canon PowerShot S300", 0x04A9, 0x304C, 0}
	, {
	"Canon Digital IXUS 300", 0x04A9, 0x304D, 0}
	, {
	"Canon PowerShot A20", 0x04A9, 0x304E, 0}
	, {
	"Canon PowerShot A10", 0x04A9, 0x304F, 0}
	, {
	"Canon PowerShot S110", 0x04A9, 0x3051, 0}
	, {
	"Canon DIGITAL IXUS v", 0x04A9, 0x3052, 0}
	, {
	NULL, 0, 0, 0}
};

int
camera_id (CameraText *id)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_id()");

	strcpy (id->text, "canon");

	return GP_OK;
}

static int
camera_manual (Camera *camera, CameraText *manual)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_manual()");

	strcpy (manual->text, _("For the A50, 115200 may not be faster than 57600\n"
				"Folders are NOT supported\n"
				"if you experience a lot of transmissions errors,"
				" try to have you computer as idle as possible (ie: no disk activity)"));

	return GP_OK;
}

int
camera_abilities (CameraAbilitiesList *list)
{
	int i;
	CameraAbilities a;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_abilities()");

	for (i = 0; models[i].name; i++) {
		a.status = GP_DRIVER_STATUS_PRODUCTION;
		strcpy (a.model, models[i].name);
		a.port = 0;
		if (models[i].idProduct) {
			a.port |= GP_PORT_USB;
			a.usb_vendor = models[i].idVendor;
			a.usb_product = models[i].idProduct;
		}
		if (models[i].serial) {
			a.port |= GP_PORT_SERIAL;
			a.speed[0] = 9600;
			a.speed[1] = 19200;
			a.speed[2] = 38400;
			a.speed[3] = 57600;
			a.speed[4] = 115200;
			a.speed[5] = 0;
		}
		a.operations = GP_OPERATION_CONFIG;
		a.folder_operations = GP_FOLDER_OPERATION_PUT_FILE |
			GP_FOLDER_OPERATION_MAKE_DIR | GP_FOLDER_OPERATION_REMOVE_DIR;
		a.file_operations = GP_FILE_OPERATION_DELETE | GP_FILE_OPERATION_PREVIEW;
		gp_abilities_list_append (list, a);
	}

	return GP_OK;
}

void
clear_readiness (Camera *camera)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "clear_readiness()");

	camera->pl->cached_ready = 0;
}

static int
check_readiness (Camera *camera)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "check_readiness() cached_ready == %i",
			 camera->pl->cached_ready);

	if (camera->pl->cached_ready)
		return 1;
	if (canon_int_ready (camera) == GP_OK) {
		gp_debug_printf (GP_DEBUG_LOW, "canon", "Camera type:  %d\n",
				 camera->pl->model);
		camera->pl->cached_ready = 1;
		return 1;
	}
	gp_camera_status (camera, _("Camera unavailable"));
	return 0;
}

static void
switch_camera_off (Camera *camera)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "switch_camera_off()");


	gp_camera_status (camera, _("Switching Camera Off"));

	canon_serial_off (camera);
	clear_readiness (camera);
}

static int
camera_exit (Camera *camera)
{
	if (camera->pl) {
		if (camera->port->type == GP_PORT_SERIAL)
			switch_camera_off (camera);
		free (camera->pl);
		camera->pl = NULL;
	}

	return GP_OK;
}

static int
canon_get_batt_status (Camera *camera, int *pwr_status, int *pwr_source)
{
	GP_DEBUG ("canon_get_batt_status() called");

	if (!check_readiness (camera))
		return -1;

	return canon_int_get_battery (camera, pwr_status, pwr_source);
}

static int
update_disk_cache (Camera *camera)
{
	char root[10];		/* D:\ or such */

	gp_debug_printf (GP_DEBUG_LOW, "canon", "update_disk_cache()");

	if (camera->pl->cached_disk)
		return 1;
	if (!check_readiness (camera))
		return 0;
	camera->pl->cached_drive = canon_int_get_disk_name (camera);
	if (!camera->pl->cached_drive) {
		gp_camera_status (camera, _("No response"));
		return 0;
	}
	snprintf (root, sizeof (root), "%s\\", camera->pl->cached_drive);
	if (!canon_int_get_disk_name_info (camera, root,
					   &camera->pl->cached_capacity,
					   &camera->pl->cached_available)) {
		gp_camera_status (camera, _("No response"));
		return 0;
	}
	camera->pl->cached_disk = 1;

	return 1;
}

/* This function is only used by A5 */

static int
recurse (Camera *camera, const char *name)
{
	struct canon_dir *dir, *walk;
	char buffer[300];	/* longest path, etc. */
	int count, curr;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "recurse() name '%s'", name);

	dir = canon_int_list_directory (camera, name);
	if (!dir)
		return 1;	/* assume it's empty @@@ */
	count = 0;
	for (walk = dir; walk->name; walk++)
		if (walk->size && (is_image (walk->name) || is_movie (walk->name)))
			count++;
	camera->pl->cached_paths =
		realloc (camera->pl->cached_paths,
			 sizeof (char *) * (camera->pl->cached_images + count + 1));
	memset (camera->pl->cached_paths + camera->pl->cached_images + 1, 0,
		sizeof (char *) * count);
	if (!camera->pl->cached_paths) {
		perror ("realloc");
		return 0;
	}
	curr = camera->pl->cached_images;
	camera->pl->cached_images += count;
	for (walk = dir; walk->name; walk++) {
		sprintf (buffer, "%s\\%s", name, walk->name);
		if (!walk->size) {
			if (!recurse (camera, buffer))
				return 0;
		} else {
			if ((!is_image (walk->name)) && (!is_movie (walk->name)))
				continue;
			curr++;
			camera->pl->cached_paths[curr] = strdup (buffer);
			if (!camera->pl->cached_paths[curr]) {
				perror ("strdup");
				return 0;
			}
		}
	}
	free (dir);
	return 1;
}



/* This function is only used by A50 */

static struct canon_dir *
dir_tree (Camera *camera, const char *path)
{
	struct canon_dir *dir, *walk;
	char buffer[300];	/* longest path, etc. */

	gp_debug_printf (GP_DEBUG_LOW, "canon", "dir_tree() path '%s'", path);

	dir = canon_int_list_directory (camera, path);
	if (!dir)
		return NULL;	/* assume it's empty @@@ */
	for (walk = dir; walk->name; walk++) {
		if (walk->is_file) {
			if (is_image (walk->name) || is_movie (walk->name)
			    || is_thumbnail (walk->name))
				camera->pl->cached_images++;
		} else {
			sprintf (buffer, "%s\\%s", path, walk->name);
			walk->user = dir_tree (camera, buffer);
		}
	}
	qsort (dir, walk - dir, sizeof (*dir), comp_dir);
	return dir;
}


static void
clear_dir_cache (Camera *camera)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "clear_dir_cache()");

	canon_int_free_dir (camera, camera->pl->cached_tree);
}


/* A5 only: sort THB_ and AUT_ into their proper arrangement. */
static int
compare_a5_paths (const void *p1, const void *p2)
{
	const char *s1 = *((const char **) p1);
	const char *s2 = *((const char **) p2);
	const char *ptr, *base1, *base2;
	int n1 = 0, n2 = 0;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "compare_a5_paths()");

	gp_debug_printf (GP_DEBUG_LOW, "canon", _("Comparing %s to %s\n"), s1, s2);

	ptr = strrchr (s1, '_');
	if (ptr)
		n1 = strtol (ptr + 1, 0, 10);
	ptr = strrchr (s2, '_');
	if (ptr)
		n2 = strtol (ptr + 1, 0, 10);

	gp_debug_printf (GP_DEBUG_LOW, "canon", _("Numbers are %d and %d\n"), n1, n2);

	if (n1 < n2)
		return -1;
	else if (n1 > n2)
		return 1;
	else {
		base1 = strrchr (s1, '\\');
		base2 = strrchr (s2, '\\');
		gp_debug_printf (GP_DEBUG_LOW, "canon", _("Base 1 is %s and base 2 is %s\n"),
				 base1, base2);
		return strcmp (base1, base2);
	}
}


static int
update_dir_cache (Camera *camera)
{
	int i;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "update_dir_cache() "
			 "cached_dir = %i", camera->pl->cached_dir);

	if (camera->pl->cached_dir)
		return 1;
	if (!update_disk_cache (camera))
		return 0;
	if (!check_readiness (camera))
		return 0;
	camera->pl->cached_images = 0;
	switch (camera->pl->model) {
		case CANON_PS_A5:
		case CANON_PS_A5_ZOOM:
			if (recurse (camera, camera->pl->cached_drive)) {
				gp_debug_printf (GP_DEBUG_LOW, "canon", _("Before sort:\n"));
				for (i = 1; i < camera->pl->cached_images; i++) {
					gp_debug_printf (GP_DEBUG_LOW, "canon", "%d: %s\n", i,
							 camera->pl->cached_paths[i]);
				}
				qsort (camera->pl->cached_paths + 1, camera->pl->cached_images,
				       sizeof (char *), compare_a5_paths);
				gp_debug_printf (GP_DEBUG_LOW, "canon", _("After sort:\n"));
				for (i = 1; i < camera->pl->cached_images; i++) {
					printf ("%d: %s\n", i, camera->pl->cached_paths[i]);
				}
				camera->pl->cached_dir = 1;
				return 1;
			}
			clear_dir_cache (camera);
			return 0;
			break;

		default:	/* A50 or S10 or other */
			camera->pl->cached_tree = dir_tree (camera, camera->pl->cached_drive);
			if (!camera->pl->cached_tree)
				return 0;
			camera->pl->cached_dir = 1;
			return 1;
			break;
	}
}

static int
_canon_file_list (struct canon_dir *tree, const char *folder, CameraList *list)
{

	if (!tree) {
		return GP_ERROR;
	}

	while (tree->name) {
		if (is_image (tree->name) || is_movie (tree->name)) {
			gp_list_append (list, (char *) tree->name, NULL);
		} else if (!tree->is_file) {
			_canon_file_list (tree->user, folder, list);
		}
		tree++;
	}

	return GP_OK;
}

static int
canon_file_list (Camera *camera, const char *folder, CameraList *list)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "canon_file_list()");

	if (!update_dir_cache (camera)) {
		gp_camera_status (camera, _("Could not obtain directory listing"));
		return GP_ERROR;
	}

	_canon_file_list (camera->pl->cached_tree, folder, list);
	return GP_OK;
}


static int
file_list_func (CameraFilesystem *fs, const char *folder, CameraList *list, void *data)
{
	Camera *camera = data;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_file_list()");

	return canon_file_list (camera, folder, list);
}

/****************************************************************************
 *
 * gphoto library interface calls
 *
 ****************************************************************************/

static int
canon_get_picture (Camera *camera, char *filename, char *path, int thumbnail,
		   unsigned char **data, int *size)
{
	unsigned char attribs;
	char file[300];
	int res;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "canon_get_picture()");

	if (!check_readiness (camera)) {
		return GP_ERROR;
	}
	switch (camera->pl->model) {
		case CANON_PS_A5:
		case CANON_PS_A5_ZOOM:
#if 0
			picture_number = picture_number * 2 - 1;
			if (thumbnail)
				picture_number += 1;
			gp_debug_printf (GP_DEBUG_LOW, "canon", _("Picture number %d\n"),
					 picture_number);

			if (!picture_number || picture_number > cached_images) {
				gp_camera_status (camera, _("Invalid index"));
				return GP_ERROR;
			}
			gp_camera_status (camera, cached_paths[picture_number]);
			if (!check_readiness (camera)) {
				return GP_ERROR;
			}
			res = canon_int_get_file (cached_paths[picture_number], size);
			if (res != GP_OK)
				return res;
#else
			gp_debug_printf (GP_DEBUG_LOW, "canon",
					 "canon_get_picture: downloading "
					 "pictures disabled for cameras: PowerShot A5, "
					 "Powershot A5 ZOOM");

			return GP_ERROR_NOT_SUPPORTED;
#endif /* 0 */
			break;
		default:
			/* For A50 or others */
			/* clear_readiness(); */
			if (!update_dir_cache (camera)) {
				gp_camera_status (camera,
						  _("Could not obtain directory listing"));
				return GP_ERROR;
			}

			snprintf (file, sizeof (file), "%s%s", path, filename);
			gp_debug_printf (GP_DEBUG_LOW, "canon",
					 "canon_get_picture: file = %s\n\tpath=%s, filename=%s\n",
					 file, path, filename);
			attribs = 0;
			if (!check_readiness (camera)) {
				return GP_ERROR;
			}
			if (thumbnail) {
				/* The thumbnail of a movie in on a file called MVI_XXXX.THM
				 * we replace .AVI by .THM to download the thumbnail (jpeg format)
				 */
				if (is_movie (filename)) {
					strcpy (file + (strlen (file) - 3), "THM");
					/* XXX check that this works */
					gp_debug_printf (GP_DEBUG_LOW, "canon",
							 "canon_get_picture: movie thumbnail: %s\n",
							 file);
					return canon_int_get_file (camera, file, data, size);
				} else {
					*data = canon_int_get_thumbnail (camera, file, size);
					if (*data)
						return GP_OK;
					else {
						gp_debug_printf (GP_DEBUG_LOW, "canon",
								 "canon_get_picture: ",
								 "canon_int_get_thumbnail() '%s' %d failed!",
								 file, size);
						return GP_ERROR;
					}
				}
			} else {
				res = canon_int_get_file (camera, file, data, size);
				if (res != GP_OK) {
					gp_debug_printf (GP_DEBUG_LOW, "canon",
							 "canon_get_picture: canon_int_get_file() failed! returned %i",
							 res);
					return res;
				}

				/* find rightmost \ in path and terminate the string there */
				if (strrchr (path, '\\') == NULL) {
					gp_debug_printf (GP_DEBUG_LOW, "canon",
							 "camera_file_get: "
							 "Could not determine directory part of path '%s'",
							 path);
					return GP_ERROR;
				}
				/* trucate path at the position of the last \ (not after) */
				path[strrchr (path, '\\') - path] = '\0';

				gp_debug_printf (GP_DEBUG_LOW, "canon",
						 "canon_get_picture: We now have to set the \"downloaded\" flag on the picture\n");
				gp_debug_printf (GP_DEBUG_LOW, "canon",
						 "canon_get_picture: The old file attributes were: %#x\n",
						 attribs);
				attribs &= ~CANON_ATTR_DOWNLOADED;
				res = canon_int_set_file_attributes (camera, filename, path,
								     attribs);
				if (res != GP_OK) {
					/* warn but continue since we allready have the downloaded picture */
					gp_debug_printf (GP_DEBUG_LOW, "canon",
							 "canon_get_picture: WARNING: canon_int_set_file_attributes on "
							 "'%s' '%s' to 0x%x failed! returned %d.",
							 path, filename, attribs, res);
				}
			}
			return GP_OK;
			break;
	}
	/* NOT REACHED */
	return GP_ERROR;
}

/**
 * _get_file_path:
 * @camera: a #Camera to work with
 * @canon_dir: seems to be a reference to the current
 * @filename: seems to be the file name to look for
 * @path: returns (?!) the full path to the file given by @filename
 *
 * _get_file_path seems to find a given filename anywhere in the
 * directory tree and returns the complete path to the file in @path.
 *
 * _get_file_path is an internal function and is only called from #get_file_path.
 *
 * This is a strange function. Its name suggests it would return the path to a file
 * of a known name, but it returns *path = the whole filename (the filename argument
 * we are called with). So in effect, this function only does a lot of is_*() checking
 * and if we are not using USB it prepends a \ to the name. Prepending the \ should
 * be made somewhere else, IMO.
 *
 * Return value: a gphoto2 error code.
 **/
int
_get_file_path (Camera *camera, struct canon_dir *tree, const char *filename, char *path)
{
	GP_DEBUG ("_get_file_path() called: filename '%s' path '%s'", filename, path);

	if (tree == NULL)
		return GP_ERROR;

	if (camera->pl->canon_comm_method != CANON_USB) {
		path = strchr (path, 0);
		*path = '\\';
	}

	while (tree->name) {
		if (!is_image (tree->name) && !is_movie (tree->name)
		    && !is_thumbnail (tree->name)) {
			switch (camera->pl->canon_comm_method) {
				case CANON_USB:
					strcpy (path, tree->name);
					break;
				case CANON_SERIAL_RS232:
				default:
					strcpy (path + 1, tree->name);
					break;
			}
		}
		if ((is_image (tree->name)
		     || (is_movie (tree->name) || is_thumbnail (tree->name)))
		    && strcmp (tree->name, filename) == 0) {
			GP_DEBUG ("_get_file_path() returns with "
				  "filename '%s' path '%s'", filename, path);
			return GP_OK;
		} else if (!tree->is_file) {
			if (_get_file_path (camera, tree->user, filename, path) == GP_OK) {
				GP_DEBUG ("_get_file_path() returns with "
					  "filename '%s' path '%s'", filename, path);
				return GP_OK;
			}
		}
		tree++;
	}

	return GP_ERROR;
}

static int
get_file_path (Camera *camera, const char *filename, char *path)
{
	return _get_file_path (camera, camera->pl->cached_tree, filename, path);
}

static int
get_file_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileType type, CameraFile *file, void *user_data)
{
	Camera *camera = user_data;
	unsigned char *data = NULL;
	int buflen, size, ret;
	char path[300], tempfilename[300];

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_file_get() "
			 "folder '%s' filename '%s'", folder, filename);

	if (check_readiness (camera) != 1)
		return GP_ERROR;

	strncpy (path, camera->pl->cached_drive, sizeof (path) - 1);

	/* update file cache (if necessary) first */
	if (!update_dir_cache (camera)) {
		gp_camera_status (camera, _("Could not obtain directory listing"));
		return GP_ERROR;
	}

	/* update_dir_cache() loads all file names into cache, now call
	 * get_file_path() to determine in which folder on flash the file
	 * is located
	 */
	if (get_file_path (camera, filename, path) == GP_ERROR) {
		gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_file_get: "
				 "Filename '%s' path '%s' not found!", filename, path);
		return GP_ERROR;
	}

	gp_debug_printf (GP_DEBUG_HIGH, "canon", "camera_file_get: "
			 "Found picture, '%s'/'%s'", path, filename);

	switch (camera->pl->canon_comm_method) {
		case CANON_USB:
			/* add trailing backslash */
			if (path[strlen (path) - 1] != '\\')
				strncat (path, "\\", sizeof (path) - 1);
			break;
		case CANON_SERIAL_RS232:
		default:
			/* find rightmost \ in path */
			if (strrchr (path, '\\') == NULL) {
				gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_file_get: "
						 "Could not determine directory part of path '%s'",
						 path);
				return GP_ERROR;
			}
			/* truncate path after the last \ */
			path[strrchr (path, '\\') - path + 1] = '\0';

			break;
	}

	switch (type) {
		case GP_FILE_TYPE_NORMAL:
			ret = canon_get_picture (camera, (char *) filename,
						 (char *) path, 0, &data, &buflen);
			break;
		case GP_FILE_TYPE_PREVIEW:
			ret = canon_get_picture (camera, (char *) filename,
						 (char *) path, 1, &data, &buflen);
			break;
		default:
			return (GP_ERROR_NOT_SUPPORTED);
	}

	if (ret != GP_OK) {
		gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_file_get: "
				 "canon_get_picture() failed, returned %i", ret);
		return ret;
	}

	/* 256 is picked out of the blue, I figured no JPEG with EXIF header
	 * (not all canon cameras produces EXIF headers I think, but still)
	 * should be less than 256 bytes long.
	 */
	if (!data || buflen < 256)
		return GP_ERROR;

	switch (type) {
		case GP_FILE_TYPE_PREVIEW:
			/* we count the byte returned until the end of the jpeg data
			   which is FF D9 */
			/* It would be prettier to get that info from the exif tags */
			for (size = 1; size < buflen; size++)
				if ((data[size - 1] == JPEG_ESC) && (data[size] == JPEG_END))
					break;
			buflen = size + 1;
			gp_file_set_data_and_size (file, data, buflen);
			gp_file_set_mime_type (file, "image/jpeg");	/* always */
			strcpy (tempfilename, filename);
			strcat (tempfilename, "\0");
			strcpy (tempfilename + strlen ("IMG_XXXX"), ".JPG\0");
			gp_file_set_name (file, tempfilename);
			break;
		case GP_FILE_TYPE_NORMAL:
			if (is_movie (filename))
				gp_file_set_mime_type (file, "video/x-msvideo");
			else
				gp_file_set_mime_type (file, "image/jpeg");
			gp_file_set_data_and_size (file, data, buflen);
			gp_file_set_name (file, filename);
			break;
		default:
			return (GP_ERROR_NOT_SUPPORTED);
	}

	return GP_OK;
}

/****************************************************************************/


static void
pretty_number (int number, char *buffer)
{
	int len, tmp, digits;
	char *pos;

	len = 0;
	tmp = number;
	do {
		len++;
		tmp /= 10;
	}
	while (tmp);
	len += (len - 1) / 3;
	pos = buffer + len;
	*pos = 0;
	digits = 0;
	do {
		*--pos = (number % 10) + '0';
		number /= 10;
		if (++digits == 3) {
			*--pos = '\'';
			digits = 0;
		}
	}
	while (number);
}

static int
camera_summary (Camera *camera, CameraText *summary)
{
	char a[20], b[20];
	char *model;
	int pwr_source, pwr_status;
	char power_stats[48], cde[16];

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_summary()");

	if (check_readiness (camera) != 1)
		return GP_ERROR;

	/*clear_readiness(); */
	if (!update_disk_cache (camera))
		return GP_ERROR;

	pretty_number (camera->pl->cached_capacity, a);
	pretty_number (camera->pl->cached_available, b);

	model = "Canon Powershot";
	switch (camera->pl->model) {
		case CANON_PS_A5:
			model = "Canon Powershot A5";
			break;
		case CANON_PS_A5_ZOOM:
			model = "Canon Powershot A5 Zoom";
			break;
		case CANON_PS_A50:
			model = "Canon Powershot A50";
			break;
		case CANON_PS_A70:
			model = "Canon Powershot A70";
			break;
		case CANON_PS_S10:
			model = "Canon Powershot S10";
			break;
		case CANON_PS_S20:
			model = "Canon Powershot S20";
			break;
		case CANON_PS_G1:
			model = "Canon Powershot G1";
			break;
		case CANON_PS_G2:
			model = "Canon Powershot G2";
			break;
		case CANON_PS_S100:
			model = "Canon Powershot S100 / Digital IXUS / IXY DIGITAL";
			break;
		case CANON_PS_S300:
			model = "Canon Powershot S300 / Digital IXUS 300 / IXY DIGITAL 300";
			break;
		case CANON_PS_A10:
			model = "Canon Powershot A10";
			break;
		case CANON_PS_A20:
			model = "Canon Powershot A20";
			break;
		case CANON_EOS_D30:
			model = "Canon EOS D30";
			break;
		case CANON_PS_PRO90_IS:
			model = "Canon Pro90 IS";
			break;
	}

	canon_get_batt_status (camera, &pwr_status, &pwr_source);
	if ((pwr_source & CAMERA_MASK_BATTERY) == 0) {
		strcpy (power_stats, _("AC adapter "));
	} else {
		strcpy (power_stats, _("on battery "));
	}

	switch (pwr_status) {
		case CAMERA_POWER_OK:
			strcat (power_stats, _("(power OK)"));
			break;
		case CAMERA_POWER_BAD:
			strcat (power_stats, _("(power low)"));
			break;
		default:
			strcat (power_stats, cde);
			sprintf (cde, " - %i)", pwr_status);
			break;
	}

	sprintf (summary->text,
		 _("%s\n%s\n%s\nDrive %s\n%11s bytes total\n%11s bytes available\n"), model,
		 camera->pl->owner, power_stats, camera->pl->cached_drive, a, b);
	return GP_OK;
}

/****************************************************************************/

static int
camera_about (Camera *camera, CameraText *about)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_about()");

	strcpy (about->text,
		_("Canon PowerShot series driver by\n"
		  "Wolfgang G. Reissnegger,\n"
		  "Werner Almesberger,\n"
		  "Edouard Lafargue,\n"
		  "Philippe Marzouk,\n" "A5 additions by Ole W. Saastad\n" "Holger Klemm\n")
		);

	return GP_OK;
}

/****************************************************************************/

static int
delete_file_func (CameraFilesystem *fs, const char *folder, const char *filename, void *data)
{
	Camera *camera = data;
	char path[300], thumbname[300];
	int j;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_file_delete()");

	// initialize memory to avoid problems later
	for (j = 0; j < sizeof (path); j++)
		path[j] = '\0';
	memset (thumbname, 0, sizeof (thumbname));

	if (check_readiness (camera) != 1)
		return GP_ERROR;

	if (!(camera->pl->model == CANON_PS_A5 || camera->pl->model == CANON_PS_A5_ZOOM)) {	/* Tested only on powershot A50 */

		if (!update_dir_cache (camera)) {
			gp_camera_status (camera, _("Could not obtain directory listing"));
			return 0;
		}
		strcpy (path, camera->pl->cached_drive);

		if (get_file_path (camera, filename, path) == GP_ERROR) {
			gp_debug_printf (GP_DEBUG_LOW, "canon", "Filename not found!\n");
			return GP_ERROR;
		}
		if (camera->pl->canon_comm_method != CANON_USB) {
			j = strrchr (path, '\\') - path;
			path[j] = '\0';
		} else {
			j = strchr (path, '\0') - path;
			path[j] = '\0';
//                      path[j+1] = '\0';
		}

		gp_debug_printf (GP_DEBUG_LOW, "canon", "filename: %s\n path: %s\n", filename,
				 path);
		if (canon_int_delete_file (camera, filename, path)) {
			gp_camera_status (camera, _("error deleting file"));
			return GP_ERROR;
		} else {
			/* If we have a movie, delete its thumbnail as well */
			if (is_movie (filename)) {
				strcpy (thumbname, filename);
				strcpy (thumbname + strlen ("MVI_XXXX"), ".THM\0");
				if (canon_int_delete_file (camera, thumbname, path)) {
					gp_camera_status (camera,
							  _("error deleting thumbnail"));
					return GP_ERROR;
				}
			}
			return GP_OK;
		}
	}
	return GP_ERROR;
}

/****************************************************************************/

static int
_get_last_dir (Camera *camera, struct canon_dir *tree, char *path, char *temppath)
{

	if (tree == NULL)
		return GP_ERROR;

	if (camera->pl->canon_comm_method != CANON_USB) {
		path = strchr (path, 0);
		*path = '\\';
	}

	while (tree->name) {
		if (!is_image (tree->name) && !is_movie (tree->name)
		    && !is_thumbnail (tree->name)) {
			switch (camera->pl->canon_comm_method) {
				case CANON_USB:
					strcpy (path, tree->name);
					break;
				case CANON_SERIAL_RS232:
				default:
					strcpy (path + 1, tree->name);
					break;
			}
		}

		if (!tree->is_file) {
			if ((strcmp (path + 4, "CANON") == 0) && (strcmp (temppath, path) < 0))
				strcpy (temppath, path);
			_get_last_dir (camera, tree->user, path, temppath);
		}
		tree++;
	}

	strcpy (path, temppath);

	return GP_OK;
}

/*
 * get from the cache the name of the highest numbered directory
 * 
 */
static int
get_last_dir (Camera *camera, char *path)
{
	char temppath[300];

	gp_debug_printf (GP_DEBUG_LOW, "canon", "get_last_dir()");

	strncpy (temppath, path, sizeof (temppath));

	return _get_last_dir (camera, camera->pl->cached_tree, path, temppath);
}

static int
_get_last_picture (struct canon_dir *tree, char *directory, char *filename)
{

	if (tree == NULL)
		return GP_ERROR;

	while (tree->name) {

		if (is_image (tree->name) || is_movie (tree->name)
		    || is_thumbnail (tree->name)) {
			if (strcmp (tree->name, filename) > 0)
				strcpy (filename, tree->name);
		}

		if (!tree->is_file) {
			if ((strcmp (tree->name, "DCIM") == 0)
			    || (strcmp (tree->name, directory) == 0)) {
				_get_last_picture (tree->user, directory, filename);
			}
		}

		tree++;
	}

	return GP_OK;
}

/*
 * get from the cache the name of the highest numbered picture
 * 
 */
static int
get_last_picture (Camera *camera, char *directory, char *filename)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "get_last_picture()");

	return _get_last_picture (camera->pl->cached_tree, directory, filename);
}


static int
put_file_func (CameraFilesystem *fs, const char *folder, CameraFile *file, void *data)
{
	Camera *camera = data;
	char destpath[300], destname[300], dir[300], dcf_root_dir[10];
	int j, dirnum = 0, r;
	char buf[10];
	CameraAbilities a;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_folder_put_file()");

	if (check_readiness (camera) != 1)
		return GP_ERROR;

	gp_camera_get_abilities (camera, &a);
	if (camera->pl->speed > 57600 &&
	    (!strcmp (a.model, "Canon PowerShot A50") ||
	     !strcmp (a.model, "Canon PowerShot Pro70"))) {
		gp_camera_message (camera,
				   _
				   ("Speeds greater than 57600 are not supported for uploading to this camera"));
		return GP_ERROR_NOT_SUPPORTED;
	}

	if (!check_readiness (camera)) {
		return GP_ERROR;
	}

	for (j = 0; j < sizeof (destpath); j++) {
		destpath[j] = '\0';
		dir[j] = '\0';
		destname[j] = '\0';
	}

	if (!update_dir_cache (camera)) {
		gp_camera_status (camera, _("Could not obtain directory listing"));
		return GP_ERROR;
	}

	sprintf (dcf_root_dir, "%s\\DCIM", camera->pl->cached_drive);

	if (get_last_dir (camera, dir) == GP_ERROR)
		return GP_ERROR;

	if (strlen (dir) == 0) {
		sprintf (dir, "\\100CANON");
		sprintf (destname, "AUT_0001.JPG");
	} else {
		if (get_last_picture (camera, dir + 1, destname) == GP_ERROR)
			return GP_ERROR;

		if (strlen (destname) == 0) {
			sprintf (destname, "AUT_%c%c01.JPG", dir[2], dir[3]);
		} else {
			sprintf (buf, "%c%c", destname[6], destname[7]);
			j = 1;
			j = atoi (buf);
			if (j == 99) {
				j = 1;
				sprintf (buf, "%c%c%c", dir[1], dir[2], dir[3]);
				dirnum = atoi (buf);
				if (dirnum == 999) {
					gp_camera_message (camera,
							   _
							   ("Could not upload, no free folder name available!\n"
							    "999CANON folder name exists and has an AUT_9999.JPG picture in it."));
					return GP_ERROR;
				} else {
					dirnum++;
					sprintf (dir, "\\%03iCANON", dirnum);
				}
			} else
				j++;

			sprintf (destname, "AUT_%c%c%02i.JPG", dir[2], dir[3], j);
		}

		sprintf (destpath, "%s%s", dcf_root_dir, dir);

		gp_debug_printf (GP_DEBUG_LOW, "canon", "destpath: %s destname: %s\n",
				 destpath, destname);
	}

	r = canon_int_directory_operations (camera, dcf_root_dir, DIR_CREATE);
	if (r < 0) {
		gp_camera_message (camera, "could not create \\DCIM directory");
		return (r);
	}

	r = canon_int_directory_operations (camera, destpath, DIR_CREATE);
	if (r < 0) {
		gp_camera_message (camera, "could not create destination directory");
		return (r);
	}


	j = strlen (destpath);
	destpath[j] = '\\';
	destpath[j + 1] = '\0';

	clear_readiness (camera);

	return canon_int_put_file (camera, file, destname, destpath);
}

/****************************************************************************/

static int
camera_get_config (Camera *camera, CameraWidget **window)
{
	CameraWidget *t, *section;
	char power_stats[48], firm[64];
	int pwr_status, pwr_source;
	struct tm *camtm;
	time_t camtime;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_get_config()");

	gp_widget_new (GP_WIDGET_WINDOW, "Canon PowerShot Configuration", window);

	gp_widget_new (GP_WIDGET_SECTION, _("Configure"), &section);
	gp_widget_append (*window, section);

	gp_widget_new (GP_WIDGET_TEXT, _("Camera Model"), &t);
	gp_widget_set_value (t, camera->pl->ident);
	gp_widget_append (section, t);

	gp_widget_new (GP_WIDGET_TEXT, _("Owner name"), &t);
	gp_widget_set_value (t, camera->pl->owner);
	gp_widget_append (section, t);

	gp_widget_new (GP_WIDGET_TEXT, "date", &t);
	if (camera->pl->cached_ready == 1) {
		camtime = canon_int_get_time (camera);
		if (camtime != GP_ERROR) {
			camtm = gmtime (&camtime);
			gp_widget_set_value (t, asctime (camtm));
		} else
			gp_widget_set_value (t, _("Error"));
	} else
		gp_widget_set_value (t, _("Unavailable"));
	gp_widget_append (section, t);

	gp_widget_new (GP_WIDGET_TOGGLE, _("Set camera date to PC date"), &t);
	gp_widget_append (section, t);

	gp_widget_new (GP_WIDGET_TEXT, _("Firmware revision"), &t);
	sprintf (firm, "%i.%i.%i.%i", camera->pl->firmwrev[3],
		 camera->pl->firmwrev[2], camera->pl->firmwrev[1], camera->pl->firmwrev[0]);
	gp_widget_set_value (t, firm);
	gp_widget_append (section, t);

	if (camera->pl->cached_ready == 1) {
		canon_get_batt_status (camera, &pwr_status, &pwr_source);
		if ((pwr_source & CAMERA_MASK_BATTERY) == 0) {
			strcpy (power_stats, _("AC adapter "));
		} else {
			strcpy (power_stats, _("on battery "));
		}

		switch (pwr_status) {
				char cde[16];

			case CAMERA_POWER_OK:
				strcat (power_stats, _("(power OK)"));
				break;
			case CAMERA_POWER_BAD:
				strcat (power_stats, _("(power low)"));
				break;
			default:
				strcat (power_stats, cde);
				sprintf (cde, " - %i)", pwr_status);
				break;
		}
	} else
		strcpy (power_stats, _("Power: camera unavailable"));

	gp_widget_new (GP_WIDGET_TEXT, _("Power"), &t);
	gp_widget_set_value (t, power_stats);
	gp_widget_append (section, t);

	gp_widget_new (GP_WIDGET_SECTION, _("Debug"), &section);
	gp_widget_append (*window, section);

	return GP_OK;
}

static int
camera_set_config (Camera *camera, CameraWidget *window)
{
	CameraWidget *w;
	char *wvalue;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "camera_set_config()");

	gp_widget_get_child_by_label (window, _("Owner name"), &w);
	if (gp_widget_changed (w)) {
		gp_widget_get_value (w, &wvalue);
		if (!check_readiness (camera)) {
			gp_camera_status (camera, _("Camera unavailable"));
		} else {
			if (canon_int_set_owner_name (camera, wvalue) == GP_OK)
				gp_camera_status (camera, _("Owner name changed"));
			else
				gp_camera_status (camera, _("could not change owner name"));
		}
	}

	gp_widget_get_child_by_label (window, _("Set camera date to PC date"), &w);
	if (gp_widget_changed (w)) {
		gp_widget_get_value (w, &wvalue);
		if (!check_readiness (camera)) {
			gp_camera_status (camera, _("Camera unavailable"));
		} else {
			if (canon_int_set_time (camera)) {
				gp_camera_status (camera, _("time set"));
			} else {
				gp_camera_status (camera, _("could not set time"));
			}
		}
	}

	gp_debug_printf (GP_DEBUG_LOW, "canon", _("done configuring camera.\n"));

	return GP_OK;
}

static int
get_info_func (CameraFilesystem *fs, const char *folder, const char *filename,
	       CameraFileInfo * info, void *data)
{
	gp_debug_printf (GP_DEBUG_LOW, "canon", "canon get_info_func() "
			 "called for '%s'/'%s'", folder, filename);

	info->preview.fields = GP_FILE_INFO_TYPE;

	/* thumbnails are always jpeg on Canon Cameras */
	strcpy (info->preview.type, GP_MIME_JPEG);

	/* FIXME GP_FILE_INFO_PERMISSIONS to add */
	info->file.fields = GP_FILE_INFO_NAME | GP_FILE_INFO_TYPE;
	// | GP_FILE_INFO_PERMISSIONS | GP_FILE_INFO_SIZE;
	//info->file.fields.permissions = 

	if (is_movie (filename))
		strcpy (info->file.type, GP_MIME_AVI);
	else if (is_image (filename))
		strcpy (info->file.type, GP_MIME_JPEG);
	else
		/* May no be correct behaviour ... */
		strcpy (info->file.type, "unknown");

	strcpy (info->file.name, filename);

	return GP_OK;
}

static int
make_dir_func (CameraFilesystem *fs, const char *folder, const char *name, void *data)
{
	Camera *camera = data;
	char path[2048];
	int r;

	strncpy (path, folder, sizeof (path));
	if (strlen (folder) > 1)
		strncat (path, "/", sizeof (path));
	strncat (path, name, sizeof (path));

	r = canon_int_directory_operations (camera, path, DIR_CREATE);
	if (r < 0)
		return (r);

	return (GP_OK);
}

static int
remove_dir_func (CameraFilesystem *fs, const char *folder, const char *name, void *data)
{
	Camera *camera = data;
	char path[2048];
	int r;

	strncpy (path, folder, sizeof (path));
	if (strlen (folder) > 1)
		strncat (path, "/", sizeof (path));
	strncat (path, name, sizeof (path));

	r = canon_int_directory_operations (camera, path, DIR_REMOVE);
	if (r < 0)
		return (r);

	return (GP_OK);
}

/****************************************************************************/

/**
 * camera_init:
 * @camera: the camera to initialize
 *
 * This routine initializes the serial/USB port and also load the
 * camera settings. Right now it is only the speed that is
 * saved.
 **/
int
camera_init (Camera *camera)
{
	GPPortSettings settings;

	gp_debug_printf (GP_DEBUG_LOW, "canon", "canon camera_init()");

	/* First, set up all the function pointers */
	camera->functions->exit = camera_exit;
	camera->functions->get_config = camera_get_config;
	camera->functions->set_config = camera_set_config;
	camera->functions->summary = camera_summary;
	camera->functions->manual = camera_manual;
	camera->functions->about = camera_about;

	/* Set up the CameraFilesystem */
	gp_filesystem_set_list_funcs (camera->fs, file_list_func, NULL, camera);
	gp_filesystem_set_info_funcs (camera->fs, get_info_func, NULL, camera);
	gp_filesystem_set_file_funcs (camera->fs, get_file_func, delete_file_func, camera);
	gp_filesystem_set_folder_funcs (camera->fs, put_file_func, NULL,
					make_dir_func, remove_dir_func, camera);

	camera->pl = malloc (sizeof (CameraPrivateLibrary));
	if (!camera->pl)
		return (GP_ERROR_NO_MEMORY);
	memset (camera->pl, 0, sizeof (CameraPrivateLibrary));
	camera->pl->first_init = 1;
	camera->pl->seq_tx = 1;
	camera->pl->seq_rx = 1;

	switch (camera->port->type) {
		case GP_PORT_USB:
			gp_debug_printf (GP_DEBUG_LOW, "canon",
					 "GPhoto tells us that we should use a USB link.\n");
			camera->pl->canon_comm_method = CANON_USB;

			return canon_usb_init (camera);
			break;
		case GP_PORT_SERIAL:
		default:
			gp_debug_printf (GP_DEBUG_LOW, "canon",
					 "GPhoto tells us that we should use a RS232 link.\n");

			/* Figure out the speed (and set to default speed if 0) */
			gp_port_get_settings (camera->port, &settings);
			camera->pl->speed = settings.serial.speed;

			if (camera->pl->speed == 0)
				camera->pl->speed = 9600;

			gp_debug_printf (GP_DEBUG_LOW, "canon",
					 "Camera transmission speed : %i\n",
					 camera->pl->speed);
			camera->pl->canon_comm_method = CANON_SERIAL_RS232;

			return canon_serial_init (camera);
			break;
	}

	/* NOT REACHED */
	return GP_ERROR;
}