Ticket #303: DetectMediaType.diff

File DetectMediaType.diff, 3.7 KB (added by anonymous, 19 years ago)
  • libs/libmyth/mythcdrom.cpp

     
    99#include <cstdlib>
    1010#include <cerrno>
    1111
     12#include <qdir.h>
     13
    1214using namespace std;
    1315
    1416// end for testing
     
    99101        m_Status = MEDIASTAT_USEABLE;
    100102    }
    101103
     104    //
     105    // Try to make an educated guess about the type of data present on the
     106    // cd/dvd.
     107    //
     108    DetectMediaType(m_MountPath);
     109    if (ExtensionMap.isEmpty ())
     110    {
     111        VERBOSE(VB_GENERAL,
     112           QString("No files with extension found in '%1'").arg (m_MountPath));
     113    } else {
     114        struct {
     115            int     type;
     116            char    *extensions[16];
     117        } TypeMapping[] = {
     118                    { MEDIATYPE_MVIDEO,
     119                        { "wmv", "mpg", "avi", "vob", "mpeg", "nuv", NULL }},
     120                    { MEDIATYPE_MMUSIC,
     121                        { "mp3", "ogg", "flac", NULL }},
     122                    { MEDIATYPE_MGALLERY,
     123                        { "tif", "jpg", "png", NULL }},
     124                    { 0 , {NULL} }};
     125
     126        int  score = 0, highscore = 0, type = 0;
     127        int  i; char **p;
     128
     129        for (i = 0, score = 0; TypeMapping[i].type != 0; i++)
     130        {
     131            for (p = TypeMapping[i].extensions; *p != NULL; p++)
     132            {
     133                if (ExtensionMap.contains(*p))
     134                    score += ExtensionMap[*p];
     135            }
     136            if (score > highscore)
     137                type = TypeMapping[i].type; highscore = score;
     138        }
     139        // cerr << "Cdtype: " << type << "Score: " << highscore << endl;
     140    }
     141    ExtensionMap.clear();
     142 
    102143    if (m_AllowEject)
    103144        unlock();
    104145}
    105146
     147
     148//
     149// Recursively scan directories and create an associative array with
     150// key: 'tolower(extension)' and value the number of times we've
     151// seen it.
     152//
     153void MythCDROM::DetectMediaType(const QString& directory)
     154{
     155    QDir d(directory);
     156
     157    d.setSorting(QDir::DirsFirst | QDir::Name | QDir::IgnoreCase);
     158    if (!d.exists())
     159            return;
     160
     161    const QFileInfoList *list = d.entryInfoList();
     162    if (!list)                       
     163        return;
     164
     165    QFileInfoListIterator it(*list);
     166    QFileInfo *fi;
     167
     168    while ((fi = it.current()) != 0)
     169    {
     170        ++it;
     171        if (fi->fileName() == "." || fi->fileName() == ".." )
     172                continue;
     173        if (!fi->isDir())
     174        {
     175            QString extension = fi->extension(false);
     176            if (!extension.isEmpty())
     177            {
     178                extension = extension.lower();
     179                if (ExtensionMap.contains(extension))
     180                    ExtensionMap[extension]++;
     181                else
     182                    ExtensionMap[extension] = 1;
     183            }
     184        }
     185        else DetectMediaType(fi->absFilePath());
     186    }
     187}
  • libs/libmyth/mythcdrom.h

     
    2828
    2929  protected:
    3030    virtual void onDeviceMounted();
     31    void DetectMediaType(const QString& directory);
     32
     33  private:
     34     QMap<QString, int> ExtensionMap;
    3135};
    3236
    3337class MythCDROMLinux: public MythCDROM
  • libs/libmyth/mythmedia.h

     
    1919    MEDIATYPE_MIXED=4,
    2020    MEDIATYPE_AUDIO=8,
    2121    MEDIATYPE_DVD=16,
    22     MEDIATYPE_VCD=32
     22    MEDIATYPE_VCD=32,
     23    MEDIATYPE_MMUSIC=64,
     24    MEDIATYPE_MVIDEO=128,
     25    MEDIATYPE_MGALLERY=256
    2326} MediaType;
    2427
    2528typedef enum {