// -------------------------------------------------------------------------------- // bool guDbCache::DoSetImage( const wxString &url, wxImage * img, const wxBitmapType imgtype, const int imagesize ) { wxMemoryOutputStream Outs; if( img->SaveFile( Outs, imgtype ) ) { wxSQLite3Statement stmt = m_Db->PrepareStatement( wxString::Format( wxT( "INSERT INTO cache( cache_id, cache_key, cache_data, cache_type, cache_time, cache_size ) " \ "VALUES( NULL, '%s', ?, %u, %lu, %u );" ), escape_query_str( url ).c_str(), ( int ) imgtype, wxDateTime::Now().GetTicks(), imagesize ) ); try { stmt.Bind( 1, ( const unsigned char * ) Outs.GetOutputStreamBuffer()->GetBufferStart(), Outs.GetSize() ); //guLogMessage( wxT( "%s" ), stmt.GetSQL().c_str() ); stmt.ExecuteQuery(); return true; } catch( wxSQLite3Exception &e ) { guLogError( wxT( "%u: %s" ), e.GetErrorCode(), e.GetMessage().c_str() ); } catch(...) { guLogError( wxT( "Other exception found while updating the image in cache" ) ); } } return false; }
// -------------------------------------------------------------------------------- // bool guDbCache::SetContent( const wxString &url, const wxString &content, const int type ) { wxString query = wxString::Format( wxT( "INSERT INTO cache( cache_id, cache_key, cache_data, " \ "cache_type, cache_time, cache_size ) VALUES( NULL, '%s', '%s', %u, %lu, %u );" ), escape_query_str( url ).c_str(), escape_query_str( content ).c_str(), type, wxDateTime::Now().GetTicks(), 0 ); ExecuteUpdate( query ); // delete the expired entries but call it only 2% of the times if( guRandom( 1000 ) < 7 ) ClearExpired(); return true; }
// -------------------------------------------------------------------------------- // int guDbRadios::SetRadioGenre( const int genreid, const wxString &name, const int source, const int flags ) { wxString query; query = wxString::Format( wxT( "UPDATE radiogenres SET radiogenre_name = '%s', radiogenre_source = %i, " \ "radiogenre_flags = %i WHERE radiogenre_id = %u;" ), escape_query_str( name ).c_str(), source, flags, genreid ); return ExecuteUpdate( query ); }
// -------------------------------------------------------------------------------- // int guDbRadios::SetRadioLabelName( const int LabelId, wxString LabelName ) { wxString query; escape_query_str( &LabelName ); query = query.Format( wxT( "UPDATE radiolabels SET radiolabel_name = '%s' WHERE radiolabel_id = %u;" ), LabelName.c_str(), LabelId ); if( ExecuteUpdate( query ) == 1 ) { return GetLastRowId(); } return 0; }
// -------------------------------------------------------------------------------- // void guJamendoLibrary::CreateNewSong( guTrack * track ) { wxString query; wxSQLite3ResultSet dbRes; track->m_Path = track->m_GenreName + wxT( "/" ) + track->m_ArtistName + wxT( "/" ) + track->m_AlbumName + wxT( "/" ); track->m_PathId = GetPathId( track->m_Path ); query = wxString::Format( wxT( "SELECT song_id FROM songs WHERE song_id = %i LIMIT 1;" ), track->m_SongId ); dbRes = ExecuteQuery( query ); if( !dbRes.NextRow() ) { wxString query = wxString::Format( wxT( "INSERT INTO songs( " \ "song_id, song_playcount, song_addedtime, " \ "song_name, song_genreid, song_genre, song_artistid, song_artist, " \ "song_albumid, song_album, song_pathid, song_path, song_filename, song_format, song_number, song_year, " \ "song_coverid, song_disk, song_length, song_offset, song_bitrate, song_rating, " \ "song_filesize ) VALUES( %u, 0, %lu, '%s', %u, '%s', %u, '%s', %u, '%s', " \ "%u, '%s', '%s', 'mp3,ogg', %u, %u, %u, '%s', %u, 0, 0, -1, 0 )" ), track->m_SongId, wxDateTime::GetTimeNow(), escape_query_str( track->m_SongName ).c_str(), track->m_GenreId, escape_query_str( track->m_GenreName ).c_str(), track->m_ArtistId, escape_query_str( track->m_ArtistName ).c_str(), track->m_AlbumId, escape_query_str( track->m_AlbumName ).c_str(), track->m_PathId, escape_query_str( track->m_Path ).c_str(), escape_query_str( track->m_FileName ).c_str(), track->m_Number, track->m_Year, track->m_CoverId, escape_query_str( track->m_Disk ).c_str(), track->m_Length ); //guLogMessage( wxT( "%s" ), query.c_str() ); ExecuteUpdate( query ); } // guLogMessage( wxT( "%s/%s/%s/%i - %s" ), // track->m_GenreName.c_str(), // track->m_ArtistName.c_str(), // track->m_AlbumName.c_str(), // track->m_Number, // track->m_SongName.c_str() ); }
// -------------------------------------------------------------------------------- // int guDbRadios::AddRadioLabel( wxString LabelName ) { wxString query; escape_query_str( &LabelName ); query = wxT( "INSERT INTO radiolabels( radiolabel_id, radiolabel_name ) VALUES( NULL, '" ) + LabelName + wxT( "');" ); if( ExecuteUpdate( query ) == 1 ) { return GetLastRowId(); } return 0; }
// -------------------------------------------------------------------------------- // wxString inline RadioTextFilterToSQL( const wxArrayString &textfilters, const int source ) { long count; long index; wxString RetVal = wxEmptyString; if( ( count = textfilters.Count() ) ) { for( index = 0; index < count; index++ ) { if( source == guRADIO_SOURCE_USER ) { RetVal += wxT( "radiostation_name LIKE '%" ) + escape_query_str( textfilters[ index ] ) + wxT( "%' AND " ); } else { RetVal += wxT( "( radiogenre_name LIKE '%" ) + escape_query_str( textfilters[ index ] ) + wxT( "%' OR " ); RetVal += wxT( " radiostation_name LIKE '%" ) + escape_query_str( textfilters[ index ] ) + wxT( "%' ) AND " ); } } RetVal = RetVal.RemoveLast( 4 ); } return RetVal; }
// -------------------------------------------------------------------------------- // int guDbRadios::AddRadioGenre( const wxString &GenreName, const int source, const int flags ) { wxString query; query = wxT( "INSERT INTO radiogenres( radiogenre_id, radiogenre_name, radiogenre_source, radiogenre_flags ) " ); query += wxString::Format( wxT( "VALUES( NULL, '%s', %i, %i );" ), escape_query_str( GenreName ).c_str(), source, flags ); if( ExecuteUpdate( query ) == 1 ) { return GetLastRowId(); } return 0; }
// -------------------------------------------------------------------------------- // wxImage * guDbCache::GetImage( const wxString &url, wxBitmapType &imgtype, const int imgsize ) { wxImage * Img = NULL; wxString query; wxSQLite3ResultSet dbRes; const unsigned char * Data; int DataLen = 0; query = wxString::Format( wxT( "SELECT cache_data, cache_type FROM cache WHERE cache_key = '%s' " \ "AND cache_size = %u LIMIT 1;" ), escape_query_str( url ).c_str(), imgsize ); dbRes = ExecuteQuery( query ); if( dbRes.NextRow() ) { Data = dbRes.GetBlob( 0, DataLen ); imgtype = wxBitmapType( dbRes.GetInt( 1 ) ); if( DataLen ) { wxMemoryInputStream Ins( Data, DataLen ); Img = new wxImage( Ins, imgtype ); if( Img ) { if( !Img->IsOk() ) { guLogMessage( wxT( "Image is not OK" ) ); delete Img; Img = NULL; } } // else // { // guLogMessage( wxT( "Could not create the image" ) ); // } } } // else // { // guLogMessage( wxT( "DbCache failed '%s'" ), url.c_str() ); // } dbRes.Finalize(); return Img; }
// -------------------------------------------------------------------------------- // bool guDbCache::SetContent( const wxString &url, const char * str, const int len ) { try { wxSQLite3Statement stmt = m_Db->PrepareStatement( wxString::Format( wxT( "INSERT INTO cache( cache_id, cache_key, cache_data, cache_type, cache_time, cache_size ) " \ "VALUES( NULL, '%s', ?, %u, %lu, %u );" ), escape_query_str( url ).c_str(), guDBCACHE_TYPE_TEXT, wxDateTime::Now().GetTicks(), 0 ) ); stmt.Bind( 1, ( const unsigned char * ) str, len ); //guLogMessage( wxT( "%s" ), stmt.GetSQL().c_str() ); stmt.ExecuteQuery(); return true; } catch( wxSQLite3Exception &e ) { guLogError( wxT( "%u: %s" ), e.GetErrorCode(), e.GetMessage().c_str() ); } return false; }
// -------------------------------------------------------------------------------- // wxString guDbCache::GetContent( const wxString &url ) { wxString RetVal = wxEmptyString; wxString query; wxSQLite3ResultSet dbRes; query = wxString::Format( wxT( "SELECT cache_data FROM cache WHERE cache_key = '%s' LIMIT 1;" ), escape_query_str( url ).c_str() ); dbRes = ExecuteQuery( query ); if( dbRes.NextRow() ) { RetVal = dbRes.GetString( 0 ); } dbRes.Finalize(); return RetVal; }
// -------------------------------------------------------------------------------- // void guDbRadios::SetRadioStation( const guRadioStation * radiostation ) { wxString query; if( radiostation->m_Id != wxNOT_FOUND ) { query = wxString::Format( wxT( "UPDATE radiostations SET " \ "radiostation_scid = %u, radiostation_source = %u, radiostation_genreid = %u, " \ "radiostation_name = '%s', radiostation_link = '%s', radiostation_type = '%s', " \ "radiostation_br = %u, radiostation_lc = %u, radiostation_ct = '%s' WHERE radiostation_id = %u;" ), radiostation->m_SCId, radiostation->m_Source, radiostation->m_GenreId, escape_query_str( radiostation->m_Name ).c_str(), escape_query_str( radiostation->m_Link ).c_str(), escape_query_str( radiostation->m_Type ).c_str(), radiostation->m_BitRate, radiostation->m_Listeners, escape_query_str( radiostation->m_NowPlaying ).c_str(), radiostation->m_Id ); } else { query = wxString::Format( wxT( "INSERT INTO radiostations( radiostation_id, radiostation_scid, radiostation_source, radiostation_genreid, " \ "radiostation_name, radiostation_link, radiostation_type, radiostation_br, radiostation_lc, radiostation_ct ) " \ "VALUES( NULL, %u, %u, %u, '%s', '%s', '%s', %u, %u, '%s' );" ), radiostation->m_SCId, radiostation->m_Source, radiostation->m_GenreId, escape_query_str( radiostation->m_Name ).c_str(), escape_query_str( radiostation->m_Link ).c_str(), escape_query_str( radiostation->m_Type ).c_str(), radiostation->m_BitRate, radiostation->m_Listeners, escape_query_str( radiostation->m_NowPlaying ).c_str() ); } //guLogMessage( wxT( "SetRadioStation:\n%s" ), query.c_str() ); ExecuteUpdate( query ); }
// -------------------------------------------------------------------------------- // void guDbRadios::SetRadioGenres( const wxArrayString &Genres ) { wxString query; wxSQLite3ResultSet dbRes; int index; int count; wxString GenreName; query = wxT( "DELETE FROM radiogenres;" ); ExecuteUpdate( query ); count = Genres.Count(); for( index = 0; index < count; index++ ) { GenreName = Genres[ index ]; escape_query_str( &GenreName ); query = wxString::Format( wxT( "INSERT INTO radiogenres( radiogenre_id, radiogenre_name ) "\ "VALUES( NULL, '%s' );" ), GenreName.c_str() ); ExecuteUpdate( query ); } }
// -------------------------------------------------------------------------------- // guLibUpdateThread::ExitCode guLibUpdateThread::Entry() { int Index; int Count; int LastIndex; wxCommandEvent evtup( wxEVT_MENU, ID_STATUSBAR_GAUGE_UPDATE ); evtup.SetInt( m_GaugeId ); wxCommandEvent evtmax( wxEVT_MENU, ID_STATUSBAR_GAUGE_SETMAX ); evtmax.SetInt( m_GaugeId ); if( m_ScanPath.IsEmpty() ) { Count = m_LibPaths.Count(); if( !Count ) { guLogError( wxT( "No library directories to scan" ) ); return 0; } // For every directory in the library scan for new files and add them to m_TrackFiles Index = 0; while( !TestDestroy() && ( Index < Count ) ) { guLogMessage( wxT( "Doing Library Update in %s" ), m_LibPaths[ Index ].c_str() ); ScanDirectory( m_LibPaths[ Index ] ); Index++; } } else { ScanDirectory( m_ScanPath, true ); } bool EmbeddMetadata = m_MediaViewer->GetMediaCollection()->m_EmbeddMetadata; // For every new track file update it in the database Count = m_TrackFiles.Count(); if( Count ) { m_Db->ExecuteUpdate( wxT( "BEGIN TRANSACTION;" ) ); Index = 0; evtmax.SetExtraLong( Count ); wxPostEvent( m_MainFrame, evtmax ); LastIndex = -1; while( !TestDestroy() ) { //guLogMessage( wxT( "%i - %i" ), Index, Count ); if( ( Index >= Count ) ) break; //guLogMessage( wxT( "Scanning: '%s'" ), m_TrackFiles[ Index ].c_str() ); m_Db->ReadFileTags( m_TrackFiles[ Index ], EmbeddMetadata ); //Sleep( 1 ); Index++; if( Index > LastIndex ) { evtup.SetExtraLong( Index ); wxPostEvent( m_MainFrame, evtup ); LastIndex = Index + 5; } } m_Db->ExecuteUpdate( wxT( "COMMIT TRANSACTION;" ) ); } Count = m_CueFiles.Count(); if( Count ) { m_Db->ExecuteUpdate( wxT( "BEGIN TRANSACTION;" ) ); evtmax.SetExtraLong( Count ); wxPostEvent( m_MainFrame, evtmax ); for( Index = 0; Index < Count; Index++ ) { // // Delete all files from the same cue files // wxString query = wxT( "DELETE FROM songs WHERE song_path = '" ); query += escape_query_str( wxPathOnly( m_CueFiles[ Index ] + wxT( "/" ) ) ); query += wxT( "' AND song_offset > 0" ); m_Db->ExecuteUpdate( query ); //guLogMessage( wxT( "DELETE:\n%s" ), query.c_str() ); } Index = 0; LastIndex = -1; while( !TestDestroy() ) { //guLogMessage( wxT( "%i - %i" ), Index, Count ); if( ( Index >= Count ) ) break; //guLogMessage( wxT( "Scanning: '%s'" ), m_TrackFiles[ Index ].c_str() ); m_Db->ReadFileTags( m_CueFiles[ Index ], EmbeddMetadata ); //Sleep( 1 ); Index++; if( Index > LastIndex ) { evtup.SetExtraLong( Index ); wxPostEvent( m_MainFrame, evtup ); LastIndex = Index + 5; } } m_Db->ExecuteUpdate( wxT( "COMMIT TRANSACTION;" ) ); } ProcessCovers(); Count = m_PlayListFiles.Count(); if( Count ) { m_Db->ExecuteUpdate( wxT( "BEGIN TRANSACTION;" ) ); Index = 0; evtmax.SetExtraLong( Count ); wxPostEvent( m_MainFrame, evtmax ); LastIndex = -1; while( !TestDestroy() ) { //guLogMessage( wxT( "%i - %i" ), Index, Count ); if( ( Index >= Count ) ) break; guPlaylistFile PlayList( m_PlayListFiles[ Index ] ); wxArrayInt PlayListIds; int ItemTrackId; int ItemIndex; int ItemCount; if( ( ItemCount = PlayList.Count() ) ) { for( ItemIndex = 0; ItemIndex < ItemCount; ItemIndex++ ) { if( wxFileExists( PlayList.GetItem( ItemIndex ).m_Location ) ) { ItemTrackId = m_Db->FindTrackFile( PlayList.GetItem( ItemIndex ).m_Location, NULL ); if( ItemTrackId ) { PlayListIds.Add( ItemTrackId ); } } } if( PlayListIds.Count() ) { if( m_Db->GetStaticPlayList( m_PlayListFiles[ Index ] ) == wxNOT_FOUND ) { m_Db->CreateStaticPlayList( m_PlayListFiles[ Index ], PlayListIds ); wxCommandEvent evt( wxEVT_MENU, ID_PLAYLIST_UPDATED ); evt.SetClientData( ( void * ) m_MediaViewer ); wxPostEvent( m_MainFrame, evt ); } } } Index++; if( Index > LastIndex ) { evtup.SetExtraLong( Index ); wxPostEvent( m_MainFrame, evtup ); LastIndex = Index + 5; } } m_Db->ExecuteUpdate( wxT( "COMMIT TRANSACTION;" ) ); } // // This cant be called here as wxBitmap do X11 calls and this can only be done from the main // thread. So we must call DoCleanUp from the main thread once this thread is finished. // in the OnLibraryUpdated Event handler // // delete all orphans entries // m_Db->DoCleanUp(); return 0; }