[mythtv] MythMusic patch to have onscreen tree mirror directory tree.

Chris Pinkham cpinkham at bc2va.org
Sat Feb 8 15:35:28 EST 2003


The attached patch modified MythMusic to allow the user to setup MythMusic
to show the online music tree in the same structure as the actual
directory tree.  I have added a possible keyword "directory" to the
TreeLevels variable in mythmusic-settings.txt.  If TreeLevels is set to
"directory" (minus the quotes of course), then MythMusic will create the
onscreen tree as a mirror of the directory tree on disk.  It does this by
parsing the filenames as they are read from the database.  The code is
only run if the user sets TreeLevels=directory, and only affects
databasebox.cpp.  Included patch modified mythmusic/databasebox.cpp and
mythmusic/mythmusic-settings.txt (to add a comment about the new
'directory' option as well as correct something in the Ignore_ID3
comments).

The patch works independently of the Ignore_ID3 option currently in CVS.
This make it nicer for people who have laid out their music in a directory
structure they like rather than forcing them into a structure based upon
the normal genre/artist/album/title format variations.

Chris

*****************************************************************************
** Chris Pinkham                  Linux v2.2.18, Sane v1.0.4, Cajun v3.0-8 **
** cpinkham at bc2va.org                          http://www.bc2va.org/chris/ **
*****************************************************************************
-------------- next part --------------
Index: mythmusic/databasebox.cpp
===================================================================
RCS file: /var/lib/cvs/mythmusic/mythmusic/databasebox.cpp,v
retrieving revision 1.14
diff -u -r1.14 databasebox.cpp
--- mythmusic/databasebox.cpp	5 Feb 2003 19:47:54 -0000	1.14
+++ mythmusic/databasebox.cpp	8 Feb 2003 20:24:13 -0000
@@ -6,12 +6,15 @@
 #include <qstringlist.h>
 #include <qheader.h>
 #include <qpixmap.h>
+#include <qregexp.h>
 
 #include "metadata.h"
 #include "databasebox.h"
 #include "treecheckitem.h"
 #include "cddecoder.h"
 
+#include <mythtv/mythcontext.h>
+
 DatabaseBox::DatabaseBox(QSqlDatabase *ldb, QString &paths, 
                          QValueList<Metadata> *playlist, 
                          QWidget *parent, const char *name)
@@ -100,43 +103,132 @@
     QString temptitle = "All My Music";
     TreeCheckItem *allmusic = new TreeCheckItem(listview, temptitle, templevel,
                                                 NULL);
-    
-    QStringList lines = QStringList::split(" ", paths);
 
-    QString first = lines.front();
+    if (( paths == "directory" ) ||
+        ( paths == "subdirectory" ))
+    {
+        // tree levels match directory tree
 
-    char thequery[1024];
-    sprintf(thequery, "SELECT DISTINCT %s FROM musicmetadata ORDER BY %s DESC;",
-                      first.ascii(), first.ascii());
+        QString base_dir = gContext->GetSetting("MusicLocation");
 
-    QSqlQuery query = db->exec(thequery);
+        char thequery[1024];
+        QString levels[10];
+        int current_level = 1;
+        int first = 1;
+        TreeCheckItem *node[10];
 
-    if (query.isActive() && query.numRowsAffected() > 0)
-    {
-        while (query.next())
-        {
-            QString current = query.value(0).toString();
+        node[0] = allmusic;
 
-            QString querystr = first;
-            QString matchstr = first + " = \"" + current + "\"";           
- 
-            QStringList::Iterator line = lines.begin();
-            ++line;
-            int num = 1;
+        sprintf(thequery, "SELECT filename, title, artist, album, tracknum "
+                               "FROM musicmetadata ORDER BY filename DESC;" );
 
-            QString level = *line;
+        QSqlQuery query = db->exec(thequery);
 
-            Metadata *mdata = new Metadata();
-            mdata->setField(first, current);
+        if (query.isActive() && query.numRowsAffected() > 0)
+        {
+            while (query.next())
+            {
+                int sub_level = 1;
+
+                QString filename = query.value(0).toString();
+                QString current = query.value(0).toString();
+                QString title = query.value(1).toString();
+                QString artist = query.value(2).toString();
+                QString album = query.value(3).toString();
+                QString disp_title = query.value(4).toString();
+                QString current_dir;
+
+                current.replace( QRegExp(base_dir), QString(""));
+                current.replace( QRegExp("/[^/]*$"), QString(""));
+
+                QStringList subdirs = QStringList::split("/", current);
+                QStringList::iterator subdir_it = subdirs.begin();
+
+                for( ; subdir_it != subdirs.end(); subdir_it++, sub_level++ )
+                {
+                    QString subdir = *subdir_it;
+                    QString subdir_txt = subdir;
+
+                    current_dir = current_dir + subdir + "/";
+
+                    subdir_txt.replace( QRegExp("_"), QString(" "));
+
+                    if (( first ) ||
+                        ( levels[sub_level] != subdir ))
+                    {
+                        Metadata *mdata = new Metadata();
+                        mdata->setField("filename", current_dir);
+                        levels[sub_level] = subdir;
+                        node[sub_level] = new TreeCheckItem( node[sub_level-1], 
+                                                             subdir_txt, subdir,
+                                                             mdata);
+                        if (plist->find(*mdata) != plist->end())
+                             node[sub_level]->setOn(true);
+
+                        checkParent(node[sub_level]);
+                        first = 0;
+                    }
+                    current_level = sub_level;
+                }
+
+                // now fill in the file entry and it's metadata
+                Metadata *mdata = new Metadata();
+                mdata->setField("title", title);
+                mdata->setField("artist", artist);
+                mdata->setField("album", album);
+                mdata->setField("filename", filename);
+                mdata->fillData(db);
+
+                disp_title += " - " + title;
+
+                TreeCheckItem *item = new TreeCheckItem(node[current_level], 
+                                                        disp_title, disp_title,
+                                                        mdata);
+
+                if (plist->find(*mdata) != plist->end())
+                    item->setOn(true);
+            }
+        }
+    } else {
+        // tree levels specified in paths variable
+        QStringList lines = QStringList::split(" ", paths);
 
-            TreeCheckItem *item = new TreeCheckItem(allmusic, current, first, 
-                                                    mdata);
+        QString first = lines.front();
 
-            fillNextLevel(level, num, querystr, matchstr, line, lines,
-                          item);
+        char thequery[1024];
+        sprintf(thequery, "SELECT DISTINCT %s FROM musicmetadata "
+                               "ORDER BY %s DESC;",
+                          first.ascii(), first.ascii());
 
-            if (plist->find(*mdata) != plist->end())
-                item->setOn(true);
+        QSqlQuery query = db->exec(thequery);
+
+        if (query.isActive() && query.numRowsAffected() > 0)
+        {
+            while (query.next())
+            {
+                QString current = query.value(0).toString();
+
+                QString querystr = first;
+                QString matchstr = first + " = \"" + current + "\"";           
+     
+                QStringList::Iterator line = lines.begin();
+                ++line;
+                int num = 1;
+
+                QString level = *line;
+
+                Metadata *mdata = new Metadata();
+                mdata->setField(first, current);
+
+                TreeCheckItem *item = new TreeCheckItem(allmusic,
+                                                        current, first, mdata);
+
+                fillNextLevel(level, num, querystr, matchstr, line, lines,
+                              item);
+
+                if (plist->find(*mdata) != plist->end())
+                    item->setOn(true);
+            }
         }
     }
 
Index: mythmusic/mythmusic-settings.txt
===================================================================
RCS file: /var/lib/cvs/mythmusic/mythmusic/mythmusic-settings.txt,v
retrieving revision 1.5
diff -u -r1.5 mythmusic-settings.txt
--- mythmusic/mythmusic-settings.txt	29 Jan 2003 23:39:52 -0000	1.5
+++ mythmusic/mythmusic-settings.txt	8 Feb 2003 20:24:13 -0000
@@ -12,9 +12,12 @@
 TemporarySpace=/tmp/
 
 # Order in which to sort the Music Selection Tree.
+# Possible values are space-separated list of genre, artist, album, and title
+#     OR the keyword "directory" to indicate that onscreen tree mirrors
+#        the actual directory tree.
 TreeLevels=artist album title
 
-# Ignore_ID3 TRUE says to skip checking ID3 tags in files and just try to
+# Ignore_ID3 = 1 says to skip checking ID3 tags in files and just try to
 # determine Genre, Artist, Album, and Track number and title from the
 # filename.  NonID3FileNameFormat specifies the format of the directory
 # structure for Genre, Artist, Album, etc..  Track number and Title can be


More information about the mythtv-dev mailing list