Ticket #341: dbupdate-short.2.patch

File dbupdate-short.2.patch, 9.3 KB (added by eskil <myth@…>, 15 years ago)

fixed for style

  • mythplugins/mythmusic/mythmusic/main.cpp

    diff -ruw ../decoder-refactor/mythplugins/mythmusic/mythmusic/main.cpp mythplugins/mythmusic/mythmusic/main.cpp
    old new  
    66#include <qapplication.h>
    77#include <qsqldatabase.h>
    88#include <qregexp.h>
     9#include <sys/types.h>
     10#include <sys/stat.h>
    911#include <unistd.h>
    1012
    1113#include <cdaudio.h>
     
    9395    query.exec();
    9496}
    9597
     98void UpdateFileInDB(const QString &directory, const QString &filename)
     99{
     100    Decoder *decoder = getDecoder(filename);
     101   
     102    if (decoder)
     103    {
     104        Metadata *db_meta = decoder->getMetadata();
     105        Metadata *disk_meta = decoder->readMetadata();
     106       
     107        if (db_meta && disk_meta)
     108        {
     109            disk_meta->setID(db_meta->ID());
     110            disk_meta->updateDatabase(directory);
     111        }
     112       
     113        if (disk_meta)
     114            delete disk_meta;
     115       
     116        if (db_meta)
     117            delete db_meta;
     118       
     119        delete decoder;
     120    }
     121}
     122
    96123enum MusicFileLocation
    97124{
    98125    kFileSystem,
    99126    kDatabase,
     127    kNeedUpdate,
    100128    kBoth
    101129};
    102130
     
    142170    }
    143171}
    144172
     173bool HasFileChanged(const QString &filename, const QString &date_modified)
     174{
     175    struct stat sbuf;
     176    if (stat(filename.ascii(), &sbuf) == 0)
     177    {
     178        if (date_modified.isEmpty() ||
     179            sbuf.st_mtime >
     180            (time_t)QDateTime::fromString(date_modified,
     181                                          Qt::ISODate).toTime_t())
     182        {
     183            return true;
     184        }
     185    }
     186    return false;
     187}
     188
    145189void SavePending(int pending)
    146190{
    147191    //  Temporary Hack until mythmusic
     
    213257    delete busy;
    214258
    215259    MSqlQuery query(MSqlQuery::InitCon());
    216     query.exec("SELECT filename "
     260    query.exec("SELECT filename, date_modified "
    217261               "FROM musicmetadata "
    218262               "WHERE filename NOT LIKE ('%://%')");
    219263
     
    232276
    233277            if (name != QString::null)
    234278            {
    235                 if ((iter = music_files.find(name)) != music_files.end())
     279                if ((iter = music_files.find(name)) != music_files.end()) {
     280                    if (HasFileChanged(name, query.value(1).toString())) {
     281                        music_files[name] = kNeedUpdate;
     282                    } else {
    236283                    music_files.remove(iter);
    237                 else
     284                    }
     285                } else {
    238286                    music_files[name] = kDatabase;
    239287            }
     288            }
    240289            file_checking->setProgress(++counter);
    241290        }
    242291    }
     
    247296    file_checking = new MythProgressDialog(
    248297        QObject::tr("Updating music database"), music_files.size());
    249298
     299     /*
     300       This can be optimised quite a bit by consolidating all commands
     301       via a lot of refactoring.
     302       
     303       1) group all files of the same decoder type, and don't
     304       create/delete a Decoder pr. AddFileToDB. Or make Decoders be
     305       singletons, it should be a fairly simple change.
     306       
     307       2) RemoveFileFromDB should group the remove into one big SQL.
     308       
     309       3) UpdateFileInDB, same as 1.
     310     */
     311
    250312    counter = 0;
    251313    for (iter = music_files.begin(); iter != music_files.end(); iter++)
    252314    {
     
    254316            AddFileToDB(directory, iter.key());
    255317        else if (*iter == kDatabase)
    256318            RemoveFileFromDB(directory, iter.key ());
     319         else if (*iter == kNeedUpdate)
     320             UpdateFileInDB(directory, iter.key());
    257321
    258322        file_checking->setProgress(++counter);
    259323    }
     
    309373    AllMusic *all_music;
    310374};
    311375
     376void RebuildMusicTree(MusicData *mdata)
     377{
     378    MythBusyDialog busy(QObject::tr("Rebuilding music tree"));
     379    busy.start();
     380    mdata->all_music->startLoading();
     381    while (!mdata->all_music->doneLoading()) {
     382        qApp->processEvents();
     383        usleep(50000);
     384    }
     385    mdata->all_playlists->postLoad();
     386    busy.Close();
     387}
     388
    312389void MusicCallback(void *data, QString &selection)
    313390{
    314391    MusicData *mdata = (MusicData *)data;
     
    328405            //  Reconcile with the database
    329406            SearchDir(mdata->startdir);
    330407            //  Tell the metadata to reset itself
    331             mdata->all_music->resync();
    332             mdata->all_playlists->postLoad();
     408            RebuildMusicTree(mdata);
    333409        }
    334410    }
    335411    else if (sel == "settings_scan")
     
    337413        if ("" != mdata->startdir)
    338414        {
    339415            SearchDir(mdata->startdir);
    340             mdata->all_music->resync();
    341             mdata->all_playlists->postLoad();
     416            RebuildMusicTree(mdata);
    342417        }
    343418    }
    344419    else if (sel == "music_set_general")
     
    610685        // if startRipper returns true, then new files should be present
    611686        // so we should look for them.
    612687        SearchDir(mdata.startdir);
    613         mdata.all_music->resync();
    614         mdata.all_playlists->postLoad();
     688        RebuildMusicTree(&mdata);
    615689    }
    616690    postMusic(&mdata);
    617691}
  • mythplugins/mythmusic/mythmusic/metadata.cpp

    diff -ruw ../decoder-refactor/mythplugins/mythmusic/mythmusic/metadata.cpp mythplugins/mythmusic/mythmusic/metadata.cpp
    old new  
    11#include <iostream>
     2#include <qapplication.h>
    23#include <qregexp.h>
    34#include <qdatetime.h>
    45#include <qdir.h>
     
    194195        return;
    195196
    196197    query.prepare("INSERT INTO musicmetadata (artist,compilation_artist,album,title,"
    197                   "genre,year,tracknum,length,filename,compilation,date_added) VALUES "
     198                  "genre,year,tracknum,length,filename,compilation,date_added,"
     199                  "date_modified) VALUES "
    198200                  "(:ARTIST, :COMPILATION_ARTIST, :ALBUM, :TITLE, :GENRE, :YEAR, :TRACKNUM, "
    199                   ":LENGTH, :FILENAME, :COMPILATION, :DATE_ADDED );");
     201                  ":LENGTH, :FILENAME, :COMPILATION, :DATE_ADDED, :DATE_MODIFIED );");
    200202    query.bindValue(":ARTIST", artist.utf8());
    201203    query.bindValue(":COMPILATION_ARTIST", compilation_artist.utf8());
    202204    query.bindValue(":ALBUM", album.utf8());
     
    207209    query.bindValue(":LENGTH", length);
    208210    query.bindValue(":FILENAME", sqlfilename.utf8());
    209211    query.bindValue(":COMPILATION", compilation);
    210     query.bindValue(":DATE_ADDED", QDate::currentDate());
     212    query.bindValue(":DATE_ADDED", QDateTime::currentDateTime());
     213    query.bindValue(":DATE_MODIFIED", QDateTime::currentDateTime());
    211214   
    212215    query.exec();
    213216
     
    364367                  "compilation_artist = :COMPILATION_ARTIST, "
    365368                  "title = :TITLE, genre = :GENRE, year = :YEAR, "
    366369                  "tracknum = :TRACKNUM, rating = :RATING, "
    367                   "compilation = :COMPILATION "
     370                  "compilation = :COMPILATION, "
     371                  "date_modified= :DATE_MODIFIED "
    368372                  "WHERE intid = :ID;");
    369373    query.bindValue(":ARTIST", artist.utf8());
    370374    query.bindValue(":COMPILATION_ARTIST", compilation_artist.utf8());
     
    375379    query.bindValue(":TRACKNUM", tracknum);
    376380    query.bindValue(":RATING", rating);
    377381    query.bindValue(":COMPILATION", compilation);
     382    query.bindValue(":DATE_MODIFIED", QDateTime::currentDateTime());
    378383    query.bindValue(":ID", id);
    379384
    380385    if (!query.exec())
     
    683688    //  loading and sorting
    684689    //
    685690   
    686     metadata_loader = new MetadataLoadingThread(this);
    687     metadata_loader->start();
     691    metadata_loader = NULL;
     692    startLoading();
    688693
    689694    all_music.setAutoDelete(true);
    690695    top_nodes.setAutoDelete(true);
     
    718723    return false;
    719724}
    720725
     726bool AllMusic::startLoading(void)
     727{
     728    // Set this to false early rather than letting it be delayed till
     729    // the thread calls resync.
     730    done_loading = false;
     731
     732    if (metadata_loader) {
     733        cleanOutThreads();
     734        delete metadata_loader;
     735    }
     736
     737    metadata_loader = new MetadataLoadingThread(this);
     738    metadata_loader->start();
     739   
     740    return true;
     741}
     742
    721743void AllMusic::resync()
    722744{
    723745    done_loading = false;
     
    11611183        if( *it != "genre"  &&
    11621184            *it != "artist" &&
    11631185            *it != "splitartist" &&
     1186            *it != "splitartist1" &&
    11641187            *it != "album"  &&
    11651188            *it != "title")
    11661189        {
  • mythplugins/mythmusic/mythmusic/metadata.h

    diff -ruw ../decoder-refactor/mythplugins/mythmusic/mythmusic/metadata.h mythplugins/mythmusic/mythmusic/metadata.h
    old new  
    274274    Metadata*   getMetadata(int an_id);
    275275    bool        updateMetadata(int an_id, Metadata *the_track);
    276276    void        save();
     277        /** \brief Start loading metadata.
     278                Makes the AllMusic object run it's resync in a thread. Once done, it's
     279                doneLoading method will return true.
     280
     281                \note Alternatively, it could be made to emit a signal so the
     282                caller won't have to poll for completion.
     283
     284                \returns true if the loader thread was started
     285        */
     286        bool        startLoading (void);
    277287    void        resync();   //  After a CD rip, for example
    278288    void        clearCDData();
    279289    void        addCDTrack(Metadata *the_track);