Ticket #341: dbupdate.patch

File dbupdate.patch, 16.2 KB (added by eskil <myth@…>, 19 years ago)

patch that fixes the problem

  • mythplugins/mythmusic/mythmusic/aacdecoder.cpp

     
    614614    deinit();
    615615}
    616616
    617 Metadata* aacDecoder::getMetadata()
     617Metadata* aacDecoder::readMetadata()
    618618{
    619 
    620     Metadata *mdata = new Metadata(filename);
    621     if (mdata->isInDatabase(musiclocation))
    622     {
    623       return mdata;
    624     }
    625    
    626     delete mdata;
    627 
     619    Metadata *mdata = NULL;
    628620    MetaIOMP4* p_tagger = new MetaIOMP4;
    629621    if (ignore_id3) {
    630622        mdata = p_tagger->readFromFilename(filename);
     
    634626   
    635627    delete p_tagger;
    636628
    637     if (mdata)
    638         mdata->dumpToDatabase(musiclocation);
    639     else
     629    if (!mdata)
    640630      error(QString("aacdecoder.o: Could not read metadata from \"%1\"").arg(filename.local8Bit()));
    641631
    642632    return mdata;
  • mythplugins/mythmusic/mythmusic/decoder.h

     
    7979    static Decoder *create(const QString &, QIODevice *, AudioOutput *,
    8080                           bool = FALSE);
    8181
    82     virtual Metadata *getMetadata() = 0;
     82    virtual Metadata *readMetadata() = 0;
     83    virtual Metadata *getMetadata();
    8384    virtual void commitMetadata(Metadata *mdata) = 0;
    8485
    8586    static void SetLocationFormatUseTags(void);
  • mythplugins/mythmusic/mythmusic/metadata.cpp

     
    148148        playcount = query.value(10).toInt();
    149149        lastplay = query.value(11).toString();
    150150        compilation = (query.value(12).toInt() > 0);
    151        
     151
    152152        retval = true;
    153153    }
    154154
     
    194194        return;
    195195
    196196    query.prepare("INSERT INTO musicmetadata (artist,compilation_artist,album,title,"
    197                   "genre,year,tracknum,length,filename,compilation,date_added) VALUES "
     197                  "genre,year,tracknum,length,filename,compilation,date_added,"
     198                  "date_modified) VALUES "
    198199                  "(:ARTIST, :COMPILATION_ARTIST, :ALBUM, :TITLE, :GENRE, :YEAR, :TRACKNUM, "
    199                   ":LENGTH, :FILENAME, :COMPILATION, :DATE_ADDED );");
     200                  ":LENGTH, :FILENAME, :COMPILATION, :DATE_ADDED, :DATE_MODIFIED );");
    200201    query.bindValue(":ARTIST", artist.utf8());
    201202    query.bindValue(":COMPILATION_ARTIST", compilation_artist.utf8());
    202203    query.bindValue(":ALBUM", album.utf8());
     
    207208    query.bindValue(":LENGTH", length);
    208209    query.bindValue(":FILENAME", sqlfilename.utf8());
    209210    query.bindValue(":COMPILATION", compilation);
    210     query.bindValue(":DATE_ADDED", QDate::currentDate());
     211    query.bindValue(":DATE_ADDED", QDateTime::currentDateTime());
     212    query.bindValue(":DATE_MODIFIED", QDateTime::currentDateTime());
    211213   
    212214    query.exec();
    213215
     
    364366                  "compilation_artist = :COMPILATION_ARTIST, "
    365367                  "title = :TITLE, genre = :GENRE, year = :YEAR, "
    366368                  "tracknum = :TRACKNUM, rating = :RATING, "
    367                   "compilation = :COMPILATION "
     369                  "compilation = :COMPILATION, "
     370                  "date_modified= :DATE_MODIFIED "
    368371                  "WHERE intid = :ID;");
    369372    query.bindValue(":ARTIST", artist.utf8());
    370373    query.bindValue(":COMPILATION_ARTIST", compilation_artist.utf8());
     
    375378    query.bindValue(":TRACKNUM", tracknum);
    376379    query.bindValue(":RATING", rating);
    377380    query.bindValue(":COMPILATION", compilation);
     381    query.bindValue(":DATE_MODIFIED", QDateTime::currentDateTime());
    378382    query.bindValue(":ID", id);
    379383
    380384    if (!query.exec())
     
    11931197        if( *it != "genre"  &&
    11941198            *it != "artist" &&
    11951199            *it != "splitartist" &&
     1200            *it != "splitartist1" &&
    11961201            *it != "album"  &&
    11971202            *it != "title")
    11981203        {
  • mythplugins/mythmusic/mythmusic/cddecoder.h

     
    2525    int getNumTracks(void);
    2626    int getNumCDAudioTracks(void);
    2727
     28    Metadata *readMetadata();
    2829    Metadata *getMetadata(int track);
    2930    Metadata *getMetadata();
    3031    Metadata *getLastMetadata();
  • mythplugins/mythmusic/mythmusic/decoder.cpp

     
    66
    77#include "decoder.h"
    88#include "constants.h"
     9#include "metadata.h"
    910#include <mythtv/output.h>
    1011#include <mythtv/visual.h>
    1112
     
    8788    listeners.remove(object);
    8889}
    8990
     91Metadata* Decoder::getMetadata()
     92{
    9093
     94    Metadata *mdata = new Metadata(filename);
     95    if (mdata->isInDatabase(musiclocation))
     96    {
     97      return mdata;
     98    }
     99   
     100    delete mdata;
     101
     102    return readMetadata ();
     103}   
     104
    91105// static methods
    92106
    93107int Decoder::ignore_id3 = 0;
  • mythplugins/mythmusic/mythmusic/aacdecoder.h

     
    2929    void seek(double);
    3030    void stop();
    3131
    32     Metadata *getMetadata();
     32    Metadata *readMetadata();
    3333    void commitMetadata(Metadata *mdata);
    3434
    3535    bool     initializeMP4();
  • mythplugins/mythmusic/mythmusic/vorbisdecoder.cpp

     
    293293    deinit();
    294294}
    295295
    296 Metadata *VorbisDecoder::getMetadata()
     296Metadata *VorbisDecoder::readMetadata()
    297297{
    298     Metadata *mdata = new Metadata(filename);
    299     if (mdata->isInDatabase(musiclocation))
    300     {
    301         return mdata;
    302     }
    303 
    304     delete mdata;
    305 
    306 
     298    Metadata *mdata = NULL;
    307299    MetaIOOggVorbisComment* p_tagger = new MetaIOOggVorbisComment;
    308300    if (ignore_id3)
    309301        mdata = p_tagger->readFromFilename(filename);
     
    312304
    313305    delete p_tagger;
    314306
    315     if (mdata)
    316         mdata->dumpToDatabase(musiclocation);
    317     else
     307    if (!mdata)
    318308        cerr << "vorbisdecoder.o: Could not read metadata from " << filename.local8Bit() << endl;   
    319309
    320310    return mdata;
  • mythplugins/mythmusic/mythmusic/flacdecoder.h

     
    2222    void doWrite(const FLAC__Frame *frame, const FLAC__int32 * const buffer[]);
    2323    void setFlacMetadata(const FLAC__StreamMetadata *metadata);
    2424
    25     Metadata *getMetadata();
     25    Metadata *readMetadata();
    2626    void commitMetadata(Metadata *mdata);
    2727
    2828  private:
  • mythplugins/mythmusic/mythmusic/avfdecoder.cpp

     
    357357    deinit();
    358358}
    359359
    360 Metadata* avfDecoder::getMetadata()
     360Metadata* avfDecoder::readMetadata()
    361361{
    362     Metadata *mdata = new Metadata(filename);
    363     if (mdata->isInDatabase(musiclocation))
    364     {
    365         return mdata;
    366     }
    367 
    368     delete mdata;
    369 
    370 
     362    Metadata *mdata = NULL;
    371363    MetaIOAVFComment* p_tagger = new MetaIOAVFComment;
    372364    if (ignore_id3)
    373365        mdata = p_tagger->readFromFilename(filename);
     
    376368   
    377369    delete p_tagger;
    378370
    379     if (mdata)
    380         mdata->dumpToDatabase(musiclocation);
    381     else
     371    if (!mdata)
    382372        cerr << "avfdecoder.o: Could not read metadata from " << filename << endl;
    383373
    384374    return mdata;
  • mythplugins/mythmusic/mythmusic/main.cpp

     
    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>
     
    6567    return decoder;
    6668}
    6769
    68 void CheckFile(const QString &filename)
     70// Add a file to the database
     71void CheckFile(const QString &directory, const QString &filename)
    6972{
    7073    Decoder *decoder = getDecoder(filename);
    7174
    7275    if (decoder)
    7376    {
    7477        Metadata *data = decoder->getMetadata();
    75         if (data)
     78        if (data)
     79        {
     80            data->dumpToDatabase(directory);
    7681            delete data;
     82        }
     83        delete decoder;
     84    }
     85}
    7786
     87// Remove a file from the database
     88void RemoveFile (const QString &directory, const QString &filename)
     89{
     90    QString name(filename);
     91    name.remove(0, directory.length());
     92    MSqlQuery query(MSqlQuery::InitCon());
     93    query.prepare("DELETE FROM musicmetadata WHERE "
     94                  "filename = :NAME ;");
     95    query.bindValue(":NAME", name.utf8());
     96    query.exec();
     97}
     98
     99// Update a file's metadata in the database, preserving it's id
     100void UpdateFile (const QString &directory, const QString &filename)
     101{
     102    Decoder *decoder = getDecoder (filename);
     103
     104    if (decoder)
     105    {
     106        Metadata *dbmeta = decoder->getMetadata ();
     107        Metadata *fmeta = decoder->readMetadata ();
     108
     109        if (dbmeta && fmeta)
     110        {
     111            fmeta->setID (dbmeta->ID ());
     112            fmeta->updateDatabase (directory);
     113        }
     114
     115        if (fmeta)
     116            delete fmeta;
     117
     118        if (dbmeta)
     119            delete dbmeta;
     120
    78121        delete decoder;
    79122    }
    80123}
     
    83126{
    84127    kFileSystem,
    85128    kDatabase,
    86     kBoth
     129    kNeedUpdate,
    87130};
    88131
    89132typedef QMap <QString, MusicFileLocation> MusicLoadedMap;
     
    115158    }
    116159}
    117160
     161/* Check if the file's mdate is after the date_modified */
     162bool HasFileChanged (const QString &filename, const QString &date_modified)
     163{
     164    struct stat sbuf;
     165    if (stat (filename.ascii (), &sbuf) == 0) {
     166        if (sbuf.st_mtime > (time_t)QDateTime::fromString (date_modified, Qt::ISODate).toTime_t ()) {
     167            return true;
     168        }
     169    }
     170    return false;
     171}
     172
    118173void SavePending(int pending)
    119174{
    120175    //  Temporary Hack until mythmusic
     
    180235    BuildFileList(directory, music_files);
    181236
    182237    MSqlQuery query(MSqlQuery::InitCon());
    183     query.exec("SELECT filename FROM musicmetadata "
     238    query.exec("SELECT filename, date_modified FROM musicmetadata "
    184239                    "WHERE filename NOT LIKE ('%://%');");
    185240
    186241    int counter = 0;
     
    196251            QString name = directory + QString::fromUtf8(query.value(0).toString());
    197252            if (name != QString::null)
    198253            {
    199                 if ((iter = music_files.find(name)) != music_files.end())
    200                     music_files.remove(iter);
    201                 else
     254                if ((iter = music_files.find(name)) != music_files.end()) {
     255                    if (HasFileChanged (name, query.value (1).toString ())) {
     256                        music_files[name] = kNeedUpdate;
     257                    } else {
     258                        music_files.remove(iter);
     259                    }
     260                } else {
    202261                    music_files[name] = kDatabase;
     262                }
    203263            }
    204264            file_checking->setProgress(++counter);
    205265        }
     
    214274    QRegExp quote_regex("\"");
    215275    for (iter = music_files.begin(); iter != music_files.end(); iter++)
    216276    {
    217         if (*iter == kFileSystem)
    218         {
    219             CheckFile(iter.key());
     277        switch (*iter) {
     278        case kFileSystem:
     279            CheckFile(directory, iter.key());
     280            break;
     281        case kDatabase:
     282            RemoveFile (directory, iter.key());
     283            break;
     284        case kNeedUpdate:
     285            UpdateFile (directory, iter.key ());
     286            break;
    220287        }
    221         else if (*iter == kDatabase)
    222         {
    223             QString name(iter.key());
    224             name.remove(0, directory.length());
    225 
    226             query.prepare("DELETE FROM musicmetadata WHERE "
    227                           "filename = :NAME ;");
    228             query.bindValue(":NAME", name.utf8());
    229             query.exec();
    230         }
    231 
    232288        file_checking->setProgress(++counter);
    233289    }
    234290    file_checking->Close();
  • mythplugins/mythmusic/mythmusic/flacdecoder.cpp

     
    385385        char *field_value;
    386386} Argument_VcField;
    387387
    388 Metadata *FlacDecoder::getMetadata()
     388Metadata *FlacDecoder::readMetadata()
    389389{
    390     Metadata *mdata = new Metadata(filename);
    391     if (mdata->isInDatabase(musiclocation))
    392     {
    393         return mdata;
    394     }
    395 
    396     delete mdata;
    397 
     390    Metadata *mdata = NULL;
    398391    MetaIOFLACVorbisComment* p_tagger = new MetaIOFLACVorbisComment;
    399392    if (ignore_id3)
    400393        mdata = p_tagger->readFromFilename(filename);
     
    403396
    404397    delete p_tagger;
    405398
    406     if (mdata)
    407         mdata->dumpToDatabase(musiclocation);
    408     else
     399    if (!mdata)
    409400        cerr << "flacdecoder.o: Could not read metadata from " << filename.local8Bit() << endl;
    410401
    411402    return mdata;
  • mythplugins/mythmusic/mythmusic/maddecoder.h

     
    2424    static const int maxFrameCheck;
    2525    static const int initialFrameSize;
    2626
    27     Metadata *getMetadata();
     27    Metadata *readMetadata();
    2828    void commitMetadata(Metadata *mdata);
    2929
    3030private:
  • mythplugins/mythmusic/mythmusic/vorbisdecoder.h

     
    1818    void seek(double);
    1919    void stop();
    2020
    21     Metadata *getMetadata();
     21    Metadata *readMetadata();
    2222    void commitMetadata(Metadata *mdata);
    2323
    2424  private:
  • mythplugins/mythmusic/mythmusic/avfdecoder.h

     
    1818    void seek(double);
    1919    void stop();
    2020
    21     Metadata *getMetadata();
     21    Metadata *readMetadata();
    2222    void commitMetadata(Metadata *mdata);
    2323
    2424  private:
  • mythplugins/mythmusic/mythmusic/cddecoder.cpp

     
    331331    return NULL;
    332332}
    333333
     334Metadata *CdDecoder::readMetadata()
     335{
     336    return NULL;
     337}
     338
    334339Metadata *CdDecoder::getMetadata()
    335340{
    336341
  • mythplugins/mythmusic/mythmusic/maddecoder.cpp

     
    512512    return MAD_FLOW_STOP;
    513513}
    514514
    515 Metadata *MadDecoder::getMetadata()
     515Metadata *MadDecoder::readMetadata()
    516516{
    517     Metadata *mdata = new Metadata(filename);
    518     if (mdata->isInDatabase(musiclocation))
    519     {
    520         return mdata;
    521     }
    522 
    523     delete mdata;
    524 
    525 
     517    Metadata *mdata = NULL;
    526518    MetaIOID3v2* p_tagger = new MetaIOID3v2;
    527519    if (ignore_id3)
    528520        mdata = p_tagger->readFromFilename(filename);
     
    531523
    532524    delete p_tagger;
    533525
    534     if (mdata)
    535         mdata->dumpToDatabase(musiclocation);
    536     else
     526    if (!mdata)
    537527        cerr << "maddecoder.o: Could not read metadata from " << filename.local8Bit() << endl;
    538528
    539529    return mdata;