iriver OGG Alzheimer fix
diff --git a/ChangeLog b/ChangeLog
index de4dbe7..f2911ce 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-03-16  Linus Walleij <triad@df.lth.se>
+
+	* src/libusb-glue.h: introducing yet another device flag for the
+	  irivers bugged firmwares, this is for the Alzheimer disease that
+	  cause irivers to forget that OGG files are OGG files.
+	* src/libmtp.c: dito.
+	* src/libusb-glue.c: introduce this flag for all iriver devices.
+	* examples/pathutils.c: found a (small) bug.
+
 2007-03-12  Linus Walleij <triad@df.lth.se>
 
 	* Makefile.am: cleanup patch from Rafael Laboissiere.
diff --git a/examples/pathutils.c b/examples/pathutils.c
index f14033e..358e71a 100644
--- a/examples/pathutils.c
+++ b/examples/pathutils.c
@@ -20,8 +20,8 @@
  * Boston, MA 02111-1307, USA.
  */
 #include "common.h"
-#include "string.h"
 #include "pathutils.h"
+#include <string.h>
 #include <libgen.h>
 
 /* Find the folder_id of a given path
diff --git a/src/libmtp.c b/src/libmtp.c
index 2e41e80..fd8a401 100644
--- a/src/libmtp.c
+++ b/src/libmtp.c
@@ -2400,6 +2400,7 @@
   LIBMTP_track_t *retracks = NULL;
   LIBMTP_track_t *curtrack = NULL;
   PTPParams *params = (PTPParams *) device->params;
+  PTP_USB *ptp_usb = (PTP_USB*) device->usbinfo;
 
   // Get all the handles if we haven't already done that
   if (params->handles.Handler == NULL) {
@@ -2430,7 +2431,10 @@
 	   oi.ObjectFormat != PTP_OFC_MTP_AAC &&
 	   oi.ObjectFormat != PTP_OFC_MTP_M4A &&
 	   oi.ObjectFormat != PTP_OFC_MTP_MP4 &&
-	   oi.ObjectFormat != PTP_OFC_MTP_UndefinedAudio ) {
+	   oi.ObjectFormat != PTP_OFC_MTP_UndefinedAudio &&
+	   // This row lets through undefined files for examination since they may be forgotten OGG files.
+	   (oi.ObjectFormat != PTP_OFC_Undefined || !(ptp_usb->device_flags & DEVICE_FLAG_IRIVER_OGG_ALZHEIMER))
+	   ) {
 	// printf("Not a music track (format: %d), skipping...\n",oi.ObjectFormat);
 	continue;
       }
@@ -2452,6 +2456,30 @@
 
       get_track_metadata(device, oi.ObjectFormat, track);
 
+      /*
+       * A special quirk for iriver devices that doesn't quite
+       * remember that some files marked as "unknown" type are
+       * actually OGG files. We look at the filename extension
+       * and see if it happens that this was atleast named "ogg"
+       * and fall back on this heuristic approach in that case, 
+       * for these bugged devices only.
+       */
+      if (track->filetype == LIBMTP_FILETYPE_UNKNOWN &&
+	  ptp_usb->device_flags & DEVICE_FLAG_IRIVER_OGG_ALZHEIMER) {
+	// Repair forgotten OGG filetype
+	char *ptype;
+
+	ptype = rindex(track->filename,'.')+1;
+	if (ptype != NULL && !strcasecmp (ptype, "ogg")) {
+	  // Fix it.
+	  track->filetype = LIBMTP_FILETYPE_OGG;
+	} else {
+	  // This was not an OGG file so discard it and continue
+	  LIBMTP_destroy_track_t(track);
+	  continue;
+	}
+      }
+
       // Add track to a list that will be returned afterwards.
       if (retracks == NULL) {
 	retracks = track;
diff --git a/src/libusb-glue.c b/src/libusb-glue.c
index 2d6a972..c44c3b0 100644
--- a/src/libusb-glue.c
+++ b/src/libusb-glue.c
@@ -176,26 +176,53 @@
    * Please test on your device if you believe it isn't broken!
    * Some devices from http://www.mtp-ums.net/viewdeviceinfo.php
    */
-  { "iRiver Portable Media Center", 0x1006, 0x4002, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver Portable Media Center", 0x1006, 0x4003, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver Portable Media Center", 0x1006, 0x4002, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver Portable Media Center", 0x1006, 0x4003, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
   // From libgphoto2 source
-  { "iRiver T10", 0x4102, 0x1113, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver T20 FM", 0x4102, 0x1114, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver T10", 0x4102, 0x1113, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver T20 FM", 0x4102, 0x1114, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
   // This appears at the MTP-UMS site
-  { "iRiver T20", 0x4102, 0x1115, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver U10", 0x4102, 0x1116, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver T10", 0x4102, 0x1117, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver T20", 0x4102, 0x1118, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver T30", 0x4102, 0x1119, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver T20", 0x4102, 0x1115, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver U10", 0x4102, 0x1116, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver T10", 0x4102, 0x1117, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver T20", 0x4102, 0x1118, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver T30", 0x4102, 0x1119, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
   // Reported by David Wolpoff
-  { "iRiver T10 2GB", 0x4102, 0x1120, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver T10 2GB", 0x4102, 0x1120, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
   // Rough guess this is the MTP device ID...
-  { "iRiver N12", 0x4102, 0x1122, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver N12", 0x4102, 0x1122, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
   // Reported by Adam Torgerson
-  { "iRiver Clix", 0x4102, 0x112a, DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver Clix", 0x4102, 0x112a, 
+    DEVICE_FLAG_NO_ZERO_READS | DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
   // Reported by Scott Call
-  { "iRiver H10 20GB", 0x4102, 0x2101, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
-  { "iRiver H10", 0x4102, 0x2102, DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS },
+  { "iRiver H10 20GB", 0x4102, 0x2101, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
+  { "iRiver H10", 0x4102, 0x2102, 
+    DEVICE_FLAG_BROKEN_MTPGETOBJPROPLIST | DEVICE_FLAG_NO_ZERO_READS | 
+    DEVICE_FLAG_IRIVER_OGG_ALZHEIMER },
 
   /*
    * Dell
diff --git a/src/libusb-glue.h b/src/libusb-glue.h
index 1cadac8..10ccef9 100644
--- a/src/libusb-glue.h
+++ b/src/libusb-glue.h
@@ -75,6 +75,18 @@
  * 512 bytes, it will work with USB 1.1 and USB 2.0 alike.
  */
 #define DEVICE_FLAG_NO_ZERO_READS 0x00000008
+/**
+ * This flag means that the device is prone to forgetting the
+ * OGG container file type, so that libmtp must look at the
+ * filename extensions in order to determine that a file is
+ * actually OGG. This is a clear and present firmware bug, and
+ * while firmware bugs should be fixed in firmware, we like
+ * OGG so much that we back it by introducing this flag.
+ * The error has only been seen on iriver devices. Turning this
+ * flag on won't hurt anything, just that the check against
+ * filename extension will be done for files of "unknown" type.
+ */
+#define DEVICE_FLAG_IRIVER_OGG_ALZHEIMER 0x00000010
 
 /**
  * Internal USB struct.