Exemple #1
0
        private void ChangeAndVerify(SafeUri uri, Action <TrackInfo> change, Action <TrackInfo> verify)
        {
            TagLib.File file  = StreamTagger.ProcessUri(uri);
            TrackInfo   track = new TrackInfo();

            StreamTagger.TrackInfoMerge(track, file);
            file.Dispose();

            // Make changes
            change(track);

            // Save changes
            bool saved = StreamTagger.SaveToFile(track, true, true, true);

            Assert.IsTrue(saved);

            // Read changes
            file  = StreamTagger.ProcessUri(uri);
            track = new TrackInfo();
            StreamTagger.TrackInfoMerge(track, file, false, true, true);
            file.Dispose();

            // Verify changes
            verify(track);
        }
Exemple #2
0
        public void Open(SafeUri uri)
        {
            // Check if the uri exists
            if (uri == null || !File.Exists(uri.AbsolutePath))
            {
                return;
            }

            bool found = false;

            if (ServiceManager.DbConnection != null)
            {
                // Try to find uri in the library
                int track_id = DatabaseTrackInfo.GetTrackIdForUri(uri);
                if (track_id > 0)
                {
                    DatabaseTrackInfo track_db = DatabaseTrackInfo.Provider.FetchSingle(track_id);
                    found = true;
                    Open(track_db);
                }
            }

            if (!found)
            {
                // Not in the library, get info from the file
                TrackInfo track = new TrackInfo();
                using (var file = StreamTagger.ProcessUri(uri)) {
                    StreamTagger.TrackInfoMerge(track, file, false);
                }
                Open(track);
            }
        }
Exemple #3
0
        private void OnTrackFinished(object o, AudioCdRipperTrackFinishedArgs args)
        {
            if (user_job == null || ripper == null)
            {
                return;
            }

            AudioCdTrackInfo track = (AudioCdTrackInfo)args.Track;

            ripped_duration    += track.Duration;
            track.PrimarySource = ServiceManager.SourceManager.MusicLibrary;
            track.Uri           = args.Uri;

            track.FileSize          = Banshee.IO.File.GetSize(track.Uri);
            track.FileModifiedStamp = Banshee.IO.File.GetModifiedTime(track.Uri);
            track.LastSyncedStamp   = DateTime.Now;

            using (var file = StreamTagger.ProcessUri(track.Uri)) {
                StreamTagger.TrackInfoMerge(track, file, true);
            }

            track.Save();

            source.UnlockTrack(track);
            RipNextTrack();
        }
        public DatabaseTrackInfo ImportTrack(SafeUri uri)
        {
            if (!IsWhiteListedFile(uri.LocalPath))
            {
                return(null);
            }

            if (DatabaseTrackInfo.ContainsUri(uri, PrimarySourceIds))
            {
                // TODO add DatabaseTrackInfo.SyncedStamp property, and if the file has been
                // updated since the last sync, fetch its metadata into the db.
                return(null);
            }

            DatabaseTrackInfo track = null;

            // TODO note, there is deadlock potential here b/c of locking of shared commands and blocking
            // because of transactions.  Needs to be fixed in HyenaDatabaseConnection.
            ServiceManager.DbConnection.BeginTransaction();
            try {
                track     = new DatabaseTrackInfo();
                track.Uri = uri;
                StreamTagger.TrackInfoMerge(track, StreamTagger.ProcessUri(uri));

                track.PrimarySource = trackPrimarySourceChooser(track);

                bool save_track = true;
                if (track.PrimarySource is Banshee.Library.LibrarySource)
                {
                    save_track = track.CopyToLibraryIfAppropriate(force_copy);
                }

                if (save_track)
                {
                    track.Save(false);
                }

                ServiceManager.DbConnection.CommitTransaction();
            } catch (Exception) {
                ServiceManager.DbConnection.RollbackTransaction();
                throw;
            }

            counts[track.PrimarySourceId] = counts.ContainsKey(track.PrimarySourceId) ? counts[track.PrimarySourceId] + 1 : 1;

            // Reload every 20% or every 250 tracks, whatever is more (eg at most reload 5 times during an import)
            if (counts[track.PrimarySourceId] >= Math.Max(TotalCount / 5, 250))
            {
                counts[track.PrimarySourceId] = 0;
                track.PrimarySource.NotifyTracksAdded();
            }

            return(track);
        }
        private void SaveToID3(TrackInfo track, string lyrics)
        {
            TagLib.File file = StreamTagger.ProcessUri(track.Uri);
            if (file == null || lyrics.Equals(file.Tag.Lyrics))
            {
                return;
            }

            file.Tag.Lyrics = lyrics;
            file.Save();

            track.FileSize          = Banshee.IO.File.GetSize(track.Uri);
            track.FileModifiedStamp = Banshee.IO.File.GetModifiedTime(track.Uri);
            track.LastSyncedStamp   = DateTime.Now;
        }
 private string GetMetadataHash(QueueItem item)
 {
     if (item.ChangeType == WatcherChangeTypes.Created && item.MetadataHash == null)
     {
         var uri = new SafeUri(item.FullPath);
         if (DatabaseImportManager.IsWhiteListedFile(item.FullPath) && Banshee.IO.File.Exists(uri))
         {
             var track = new TrackInfo();
             using (var file = StreamTagger.ProcessUri(uri)) {
                 StreamTagger.TrackInfoMerge(track, file);
             }
             item.MetadataHash = track.MetadataHash;
         }
     }
     return(item.MetadataHash);
 }
Exemple #7
0
        public TagLib.File GetTaglibFile()
        {
            if (Uri.Scheme == "http" || Uri.Scheme == "https")
            {
                return(null);
            }

            try {
                return(StreamTagger.ProcessUri(Uri));
            } catch (Exception e) {
                if (Uri.Scheme == "file")
                {
                    Hyena.Log.Exception("Cannot load TagLib file", e);
                }
            }

            return(null);
        }
 private void UpdateTrack(string track)
 {
     using (var reader = ServiceManager.DbConnection.Query(
                DatabaseTrackInfo.Provider.CreateFetchCommand(
                    "CoreTracks.PrimarySourceID = ? AND CoreTracks.Uri = ? LIMIT 1"), library.DbId, new SafeUri(track).AbsoluteUri)) {
         if (reader.Read())
         {
             var track_info = DatabaseTrackInfo.Provider.Load(reader);
             if (Banshee.IO.File.GetModifiedTime(track_info.Uri) > track_info.FileModifiedStamp)
             {
                 using (var file = StreamTagger.ProcessUri(track_info.Uri)) {
                     StreamTagger.TrackInfoMerge(track_info, file, false);
                 }
                 track_info.LastSyncedStamp = DateTime.Now;
                 track_info.Save(false);
             }
         }
     }
 }
Exemple #9
0
 private void UpdateTrack(string track)
 {
     using (var reader = ServiceManager.DbConnection.Query(
                DatabaseTrackInfo.Provider.CreateFetchCommand(String.Format(
                                                                  "CoreTracks.PrimarySourceID = ? AND {0} = ? LIMIT 1",
                                                                  BansheeQuery.UriField.Column)),
                library.DbId, new SafeUri(track).AbsoluteUri)) {
         if (reader.Read())
         {
             var track_info = DatabaseTrackInfo.Provider.Load(reader);
             if (Banshee.IO.File.GetModifiedTime(track_info.Uri) > track_info.FileModifiedStamp)
             {
                 using (var file = StreamTagger.ProcessUri(track_info.Uri)) {
                     StreamTagger.TrackInfoMerge(track_info, file, false,
                                                 SaveTrackMetadataService.WriteRatingsEnabled.Value,
                                                 SaveTrackMetadataService.WritePlayCountsEnabled.Value);
                 }
                 track_info.LastSyncedStamp = DateTime.Now;
                 track_info.Save(false);
             }
         }
     }
 }
Exemple #10
0
 protected IPicture [] GetEmbeddedPictures(SafeUri uri)
 {
     using (var file = StreamTagger.ProcessUri(uri)) {
         return(file == null ? null : file.Tag.Pictures);
     }
 }
        public DatabaseTrackInfo ImportTrack(SafeUri uri)
        {
            if (!IsWhiteListedFile(uri.AbsoluteUri))
            {
                return(null);
            }

            if (DatabaseTrackInfo.ContainsUri(uri, PrimarySourceIds))
            {
                // TODO add DatabaseTrackInfo.SyncedStamp property, and if the file has been
                // updated since the last sync, fetch its metadata into the db.
                return(null);
            }

            if (!Banshee.IO.File.GetPermissions(uri).IsReadable)
            {
                throw new InvalidFileException(String.Format(
                                                   Catalog.GetString("File is not readable so it could not be imported: {0}"),
                                                   Path.GetFileName(uri.LocalPath)));
            }

            if (Banshee.IO.File.GetSize(uri) == 0)
            {
                throw new InvalidFileException(String.Format(
                                                   Catalog.GetString("File is empty so it could not be imported: {0}"),
                                                   Path.GetFileName(uri.LocalPath)));
            }

            DatabaseTrackInfo track = new DatabaseTrackInfo()
            {
                Uri = uri
            };

            using (var file = StreamTagger.ProcessUri(uri)) {
                StreamTagger.TrackInfoMerge(track, file, false, true, true);
            }

            track.Uri = uri;

            if (FindOutdatedDupe(track))
            {
                return(null);
            }

            track.PrimarySource = trackPrimarySourceChooser(track);

            // TODO note, there is deadlock potential here b/c of locking of shared commands and blocking
            // because of transactions.  Needs to be fixed in HyenaDatabaseConnection.
            ServiceManager.DbConnection.BeginTransaction();
            try {
                bool save_track = true;
                if (track.PrimarySource is Banshee.Library.LibrarySource)
                {
                    save_track = track.CopyToLibraryIfAppropriate(force_copy);
                }

                if (save_track)
                {
                    track.Save(false);
                }

                ServiceManager.DbConnection.CommitTransaction();
            } catch (Exception) {
                ServiceManager.DbConnection.RollbackTransaction();
                throw;
            }

            counts[track.PrimarySourceId] = counts.ContainsKey(track.PrimarySourceId) ? counts[track.PrimarySourceId] + 1 : 1;

            // Reload every 20% or every 250 tracks, whatever is more (eg at most reload 5 times during an import)
            if (counts[track.PrimarySourceId] >= Math.Max(TotalCount / 5, 250))
            {
                counts[track.PrimarySourceId] = 0;
                track.PrimarySource.NotifyTracksAdded();
            }

            return(track);
        }
        private void RefreshMetadataThread(object state)
        {
            int total = ServiceManager.DbConnection.Query <int> ("SELECT count(*) FROM CoreTracks");

            if (total <= 0)
            {
                return;
            }

            UserJob job = new UserJob(Catalog.GetString("Refreshing Metadata"));

            job.SetResources(Resource.Cpu, Resource.Disk, Resource.Database);
            job.PriorityHints = PriorityHints.SpeedSensitive;
            job.Status        = Catalog.GetString("Scanning...");
            job.IconNames     = new string [] { "system-search", "gtk-find" };
            job.Register();

            HyenaSqliteCommand select_command = new HyenaSqliteCommand(
                String.Format(
                    "SELECT {0} FROM {1} WHERE {2}",
                    DatabaseTrackInfo.Provider.Select,
                    DatabaseTrackInfo.Provider.From,
                    DatabaseTrackInfo.Provider.Where
                    )
                );

            int count = 0;

            using (System.Data.IDataReader reader = ServiceManager.DbConnection.Query(select_command)) {
                while (reader.Read())
                {
                    DatabaseTrackInfo track = null;
                    try {
                        track = DatabaseTrackInfo.Provider.Load(reader);

                        if (track != null && track.Uri != null && track.Uri.IsFile)
                        {
                            try {
                                TagLib.File file = StreamTagger.ProcessUri(track.Uri);
                                StreamTagger.TrackInfoMerge(track, file, true);
                            } catch (Exception e) {
                                Log.Warning(String.Format("Failed to update metadata for {0}", track),
                                            e.GetType().ToString(), false);
                            }

                            track.Save(false);
                            track.Artist.Save();
                            track.Album.Save();

                            job.Status = String.Format("{0} - {1}", track.DisplayArtistName, track.DisplayTrackTitle);
                        }
                    } catch (Exception e) {
                        Log.Warning(String.Format("Failed to update metadata for {0}", track), e.ToString(), false);
                    }

                    job.Progress = (double)++count / (double)total;
                }
            }

            if (ServiceManager.DbConnection.Query <int> ("SELECT count(*) FROM CoreConfiguration WHERE Key = 'MetadataVersion'") == 0)
            {
                Execute(String.Format("INSERT INTO CoreConfiguration (EntryID, Key, Value) VALUES (null, 'MetadataVersion', {0})", CURRENT_METADATA_VERSION));
            }
            else
            {
                Execute(String.Format("UPDATE CoreConfiguration SET Value = {0} WHERE Key = 'MetadataVersion'", CURRENT_METADATA_VERSION));
            }

            job.Finish();
            ServiceManager.SourceManager.MusicLibrary.NotifyTracksChanged();
        }
Exemple #13
0
 protected IPicture [] GetEmbeddedPictures(SafeUri uri)
 {
     TagLib.File file = StreamTagger.ProcessUri(uri);
     return(file == null ? null : file.Tag.Pictures);
 }