add single file mode for children
authorChristian Weiske <cweiske@cweiske.de>
Sat, 28 Nov 2015 13:27:36 +0000 (14:27 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Sat, 28 Nov 2015 13:27:36 +0000 (14:27 +0100)
src/mediatomb.php
var/.mt-single/.keep [new file with mode: 0644]
www/index.php

index e582a46764cd875956b6a12ec66105b5367446b6..f5c49777687035e1c343832ae2160cadd3177127 100644 (file)
 <?php
 require_once 'Services/MediaTomb.php';
 
-function handleRequestMediatomb($fullPath, $prefix)
+function handleMediatomb($action, $fullPath, $prefix)
 {
-    global $mediatomb, $host1;
+    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($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,
-                pathEncode($fullPath . $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();
-            $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(
+            $listItems[] = getDirItem(
                 $item->title,
-                $itemUrl,
-                '',
-                'MP3'
+                $fullPath . 'file-' . $item->title
             );
         }
-    } catch (Exception $e) {
-        sendMessage('Mediatomb error: ' . $e->getMessage());
-        return;
     }
 
     sendListItems($listItems);
diff --git a/var/.mt-single/.keep b/var/.mt-single/.keep
new file mode 100644 (file)
index 0000000..e69de29
index 02dd4e89bed7ee01b621861f10f9b5fdaed42af3..d9b53f1e5677acdc6d20e089f3c2ba775dc4dbe2 100644 (file)
@@ -37,7 +37,11 @@ function handleRequest($path)
 
     if (substr($path, 0, 14) == 'internetradio/') {
         require_once 'mediatomb.php';
-        handleRequestMediatomb($path, 'internetradio/');
+        handleMediatomb('browse', $path, 'internetradio/');
+        return;
+    } else if (substr($path, 0, 11) == '.mt-single/') {
+        require_once 'mediatomb.php';
+        handleMediatomb('single', $path, '.mt-single/');
         return;
     }