add single file mode for children
[noxon-gateway.git] / src / mediatomb.php
index 74b7f8479fdaa5d07f16cdf1ae621b8840355a72..f5c49777687035e1c343832ae2160cadd3177127 100644 (file)
 <?php
 require_once 'Services/MediaTomb.php';
 
-function handleRequestMediatomb($fullPath, $prefix)
+function handleMediatomb($action, $fullPath, $prefix)
 {
     global $mediatomb;
 
     extract($mediatomb);
     try {
         $smt = new Services_MediaTomb($user, $pass, $host, $port);
+        if ($action == 'browse') {
+            mediatombBrowse($smt, $fullPath, $prefix);
+        } else if ($action == 'single') {
+            mediatombSingle($smt, $fullPath, $prefix);
+        }
+    } catch (Exception $e) {
+        sendMessage('Mediatomb error: ' . $e->getMessage());
+        return;
+    }
+}
 
-        $path = substr(urldecode($fullPath), strlen($prefix));
-        $container = $smt->getContainerByPath($path);
-        $listItems = array();
-        addPreviousItem($listItems, $fullPath);
+function mediatombBrowse(Services_MediaTomb $smt, $fullPath, $prefix)
+{
+    global $mediatomb;
+
+    $path = substr($fullPath, strlen($prefix));
+    $container = $smt->getContainerByPath($path);
+    $listItems = array();
+    addPreviousItem($listItems, $fullPath);
 
-        foreach ($container->getContainers() as $subContainer) {
+    $it = $container->getItemIterator(false);
+    $it->rewind();
+    $hasFiles = $it->valid();
+    if ($hasFiles && is_array($mediatomb['singleFileDirectories'])) {
+        $enableSingle = false;
+        foreach ($mediatomb['singleFileDirectories'] as $dir) {
+            if (substr($fullPath, 0, strlen($dir)) == $dir) {
+                $enableSingle = true;
+            }
+        }
+        if ($enableSingle) {
             $listItems[] = getDirItem(
-                $subContainer->title,
-                $fullPath . rawurlencode($subContainer->title) . '/'
+                'Einzeln',
+                '.mt-single/' . $path
             );
         }
+    }
+
+    foreach ($container->getContainers() as $subContainer) {
+        $listItems[] = getDirItem(
+            $subContainer->title,
+            pathEncode($fullPath . $subContainer->title) . '/'
+        );
+    }
 
+    foreach ($container->getItemIterator(false) as $item) {
+        mediatombAddFile($listItems, $item);
+    }
+
+    sendListItems($listItems);
+}
+
+function mediatombAddFile(&$listItems, $item)
+{
+    global $host1;
+
+    $di = $item->getDetailedItem();
+    $itemUrl = $item->url;
+    if ($di->mimetype !== 'audio/mpeg') {
+        //noxon iRadio cube does not want to play .ogg files
+        $itemUrl = $host1 . 'transcode-nocache.php'
+            . '?url=' . urlencode($itemUrl);
+    }
+    $listItems[] = getEpisodeItem(
+        $item->title,
+        $itemUrl,
+        '',
+        'MP3'
+    );
+}
+
+/**
+ * Single file mode - shows directories that only have a single file in them.
+ * Each audio file gets its own virtual directory, containing only the
+ * audio file itself.
+ *
+ * Useful children who want to listen a single story before sleeping,
+ * but the noxon's auto switch-off timer is not exactly at the end of
+ * the story. So the next story starts already, and the kid complains
+ * that it wanted to listen to that as well...
+ */
+function mediatombSingle(Services_MediaTomb $smt, $fullPath, $prefix)
+{
+    $path = substr($fullPath, strlen($prefix));
+
+    $parts = explode('/', $path);
+    $fileMode = false;
+    if (substr(end($parts), 0, 5) == 'file-') {
+        $fileMode = true;
+        $fileTitle = substr(end($parts), 5);
+        $path = substr($path, 0, -strlen($fileTitle) - 5);
+    }
+
+    $container = $smt->getContainerByPath($path);
+    $listItems = array();
+
+    if ($fileMode) {
+        //show single file to play
+        addPreviousItem($listItems, $fullPath);
+        $item = $smt->getSingleItem($container, $fileTitle, false);
+        mediatombAddFile($listItems, $item);
+    } else {
+        addPreviousItem($listItems, 'internetradio/' . $path . '/dummy');
+
+        //browse directory
         foreach ($container->getItemIterator(false) as $item) {
-            $di = $item->getDetailedItem();
-            if ($di->mimetype !== 'audio/mpeg') {
-                //noxon iRadio cube does not want to play .ogg files
-                //FIXME: convert to mp3
-                //$di->location (on the server)
-            }
-            $listItems[] = getEpisodeItem(
+            $listItems[] = getDirItem(
                 $item->title,
-                $item->url,
-                '',
-                'MP3'
+                $fullPath . 'file-' . $item->title
             );
         }
-    } catch (Exception $e) {
-        sendMessage('Mediatomb error: ' . $e->getMessage());
-        return;
     }
 
     sendListItems($listItems);