Beispiel #1
0
            public static MediaFileItem findInDictionary(string location)
            {
                rwLock.EnterWriteLock();
                try
                {
                    WeakReference <MediaFileItem> reference = null;
                    MediaFileItem item = null;

                    bool success = dictionary.TryGetValue(location, out reference);

                    if (success == true)
                    {
                        bool exists = reference.TryGetTarget(out item);

                        if (exists == false)
                        {
                            return(null);
                        }
                    }

                    return(item);
                }
                finally
                {
                    rwLock.ExitWriteLock();
                }
            }
 public void move(MediaFileItem item, String location, CancellableOperationProgressBase progress)
 {
     move(new List <MediaFileItem> {
         item
     }, new List <String> {
         location
     }, progress);
 }
        // returns true on success
        public bool export(MediaFileItem item, CancellationToken token)
        {
            List <MediaFileItem> dummy = new List <MediaFileItem>();

            dummy.Add(item);
            int nrExported = export(dummy, token);

            return(nrExported == 0 ? false : true);
        }
Beispiel #4
0
            public static MediaFileItem create(string location, BaseMetadata metadata = null)
            {
                rwLock.EnterWriteLock();
                try
                {
                    WeakReference <MediaFileItem> reference = null;
                    MediaFileItem item = null;

                    bool success = dictionary.TryGetValue(location, out reference);

                    if (success == true)
                    {
                        bool exists = reference.TryGetTarget(out item);

                        if (exists == false)
                        {
                            // item has been garbage collected, recreate
                            item = new MediaFileItem(location);
                            if (metadata != null)
                            {
                                item.Metadata = metadata;
                                item.checkVariables(metadata);
                                item.ItemState = MediaItemState.LOADED;
                            }

                            reference = new WeakReference <MediaFileItem>(item);
                            dictionary.Remove(location);
                            dictionary.Add(location, reference);
                        }
                    }
                    else
                    {
                        // item did not exist yet
                        item = new MediaFileItem(location);
                        if (metadata != null)
                        {
                            item.Metadata = metadata;
                            item.checkVariables(metadata);
                            item.ItemState = MediaItemState.LOADED;
                        }

                        reference = new WeakReference <MediaFileItem>(item);
                        dictionary.Add(location, reference);
                    }

                    return(item);
                }
                finally
                {
                    rwLock.ExitWriteLock();
                }
            }
Beispiel #5
0
            public static void renameInDictionary(String oldLocation, String newLocation)
            {
                rwLock.EnterWriteLock();
                try
                {
                    WeakReference<MediaFileItem> reference = null;
                    MediaFileItem item = null;

                    bool potentialFailure = dictionary.TryGetValue(newLocation, out reference);

                    if (potentialFailure == true)
                    {
                        if (reference.TryGetTarget(out item))
                        {
                            if (item.ItemState == MediaItemState.DELETED)
                            {
                                // the mediafileitem in the hash has been deleted on disk
                                dictionary.Remove(newLocation);
                            }
                            else
                            {
                                // there is already a mediafileitem with newLocation in the dictionary      
                                Logger.Log.Warn("Trying to rename item to existing item in media dictionary: " + oldLocation + " to " + newLocation);                                            
                                throw new InvalidOperationException("Trying to rename item to existing item in media dictionary: " + oldLocation + " to " + newLocation);
                            }
                        }
                        else
                        {
                            // the mediafileitem in the hash is already dead
                            dictionary.Remove(newLocation);
                        }
                    }

                    bool success = dictionary.TryGetValue(oldLocation, out reference);
                    
                    if (success == true)
                    {
                        bool exists = reference.TryGetTarget(out item);

                        if (exists == false)
                        {
                            // item has been garbage collected, recreate
                            dictionary.Remove(oldLocation);
                            item = new MediaFileItem(newLocation);
                            reference = new WeakReference<MediaFileItem>(item);

                            dictionary.Add(newLocation, reference);                          
                        }
                        else
                        {
                            dictionary.Remove(oldLocation);
                            dictionary.Add(newLocation, reference);
                        }
                    }
                    else
                    {
                        // item does not exist
                        throw new InvalidOperationException("Trying to rename non-existing item in media dictionary: " + oldLocation + " to " + newLocation);
                       
                    }
                 
                }
                finally
                {
                    rwLock.ExitWriteLock();
                }
            }
Beispiel #6
0
            public static MediaFileItem create(string location, BaseMetadata metadata = null)
            {                               
                rwLock.EnterWriteLock();
                try
                {  
                    WeakReference<MediaFileItem> reference = null;
                    MediaFileItem item = null;

                    bool success = dictionary.TryGetValue(location, out reference);

                    if (success == true)
                    {
                        bool exists = reference.TryGetTarget(out item);

                        if (exists == false) {
                      
                            // item has been garbage collected, recreate
                            item = new MediaFileItem(location);
                            if (metadata != null)
                            {
                                item.Metadata = metadata;                              
                                item.checkVariables(metadata);
                                item.ItemState = MediaItemState.LOADED;
                            }
                            
                            reference = new WeakReference<MediaFileItem>(item);
                            dictionary.Remove(location);
                            dictionary.Add(location, reference);
                            
                        }
                    }
                    else
                    {
                        // item did not exist yet
                        item = new MediaFileItem(location);
                        if (metadata != null)
                        {
                            item.Metadata = metadata;                           
                            item.checkVariables(metadata);
                            item.ItemState = MediaItemState.LOADED;
                        }

                        reference = new WeakReference<MediaFileItem>(item);
                        dictionary.Add(location, reference);
                  
                    }

                    return (item);
                }
                finally
                {
                    rwLock.ExitWriteLock();                   
                }
            }
Beispiel #7
0
            public static void renameInDictionary(String oldLocation, String newLocation)
            {
                rwLock.EnterWriteLock();
                try
                {
                    WeakReference <MediaFileItem> reference = null;
                    MediaFileItem item = null;

                    bool potentialFailure = dictionary.TryGetValue(newLocation, out reference);

                    if (potentialFailure == true)
                    {
                        if (reference.TryGetTarget(out item))
                        {
                            if (item.ItemState == MediaItemState.DELETED)
                            {
                                // the mediafileitem in the hash has been deleted on disk
                                dictionary.Remove(newLocation);
                            }
                            else
                            {
                                // there is already a mediafileitem with newLocation in the dictionary
                                Logger.Log.Warn("Trying to rename item to existing item in media dictionary: " + oldLocation + " to " + newLocation);
                                throw new InvalidOperationException("Trying to rename item to existing item in media dictionary: " + oldLocation + " to " + newLocation);
                            }
                        }
                        else
                        {
                            // the mediafileitem in the hash is already dead
                            dictionary.Remove(newLocation);
                        }
                    }

                    bool success = dictionary.TryGetValue(oldLocation, out reference);

                    if (success == true)
                    {
                        bool exists = reference.TryGetTarget(out item);

                        if (exists == false)
                        {
                            // item has been garbage collected, recreate
                            dictionary.Remove(oldLocation);
                            item      = new MediaFileItem(newLocation);
                            reference = new WeakReference <MediaFileItem>(item);

                            dictionary.Add(newLocation, reference);
                        }
                        else
                        {
                            dictionary.Remove(oldLocation);
                            dictionary.Add(newLocation, reference);
                        }
                    }
                    else
                    {
                        // item does not exist
                        throw new InvalidOperationException("Trying to rename non-existing item in media dictionary: " + oldLocation + " to " + newLocation);
                    }
                }
                finally
                {
                    rwLock.ExitWriteLock();
                }
            }
        public void navigateToImageView(MediaFileItem item)
        {
            Uri imageViewUri = new Uri(typeof(MediaFileBrowserImagePanelView).FullName, UriKind.Relative);

            NavigationParameters navigationParams = new NavigationParameters();
            navigationParams.Add("item", item);
            navigationParams.Add("viewModel", ImageViewModel);
           
            RegionManager.RequestNavigate(RegionNames.MediaFileBrowserContentRegion, imageViewUri, (result) =>
            {
                CurrentViewModel = ImageViewModel;               
       
            }, navigationParams);

            ImageMediaStackPanelViewModel.MediaStateCollectionView.FilterModes.MoveCurrentTo(MediaFilterMode.Images);
            Shell.ShellViewModel.navigateToMediaStackPanelView(imageMediaStackPanelViewModel, item != null ? item.Location : null);
        }
        public void navigateToVideoView(MediaFileItem item)
        {
            Uri VideoViewUri = new Uri(typeof(VideoView).FullName, UriKind.Relative);

            NavigationParameters navigationParams = new NavigationParameters();
            navigationParams.Add("item", item);           
            navigationParams.Add("viewModel", VideoViewModel);

            RegionManager.RequestNavigate(RegionNames.MediaFileBrowserContentRegion, VideoViewUri, (result) =>
            {
                CurrentViewModel = VideoViewModel;                
          
            }, navigationParams);

            VideoMediaStackPanelViewModel.MediaStateCollectionView.FilterModes.MoveCurrentTo(MediaFilterMode.Video);
            Shell.ShellViewModel.navigateToMediaStackPanelView(videoMediaStackPanelViewModel, item.Location);
           
        }
Beispiel #10
0
        public void move(IEnumerable <MediaFileItem> items, IEnumerable <String> locations,
                         CancellableOperationProgressBase progress)
        {
            List <String>        deletedImportedLocations = new List <String>();
            List <MediaFileItem> addedImportedItems       = new List <MediaFileItem>();

            try
            {
                var itemsEnum     = items.GetEnumerator();
                var locationsEnum = locations.GetEnumerator();

                while (itemsEnum.MoveNext() && locationsEnum.MoveNext())
                {
                    if (progress.CancellationToken.IsCancellationRequested)
                    {
                        return;
                    }

                    MediaFileItem item     = itemsEnum.Current;
                    String        location = locationsEnum.Current;

                    if (!item.Location.Equals(location))
                    {
                        String oldLocation = item.Location;

                        bool isImported = false;

                        item.EnterUpgradeableReadLock();
                        try
                        {
                            isImported = item.move_URLock(location, progress);
                        }
                        finally
                        {
                            item.ExitUpgradeableReadLock();
                        }

                        if (MediaFileWatcher.Instance.IsWatcherEnabled &&
                            !FileUtils.getPathWithoutFileName(location).Equals(MediaFileWatcher.Instance.Path))
                        {
                            List <MediaFileItem> removeList = new List <MediaFileItem>();
                            removeList.Add(item);

                            removeUIState(removeList);
                        }

                        if (isImported)
                        {
                            deletedImportedLocations.Add(oldLocation);
                            addedImportedItems.Add(item);
                        }
                    }
                }
            }
            finally
            {
                if (deletedImportedLocations.Count > 0)
                {
                    OnNrImportedItemsChanged(new MediaStateChangedEventArgs(
                                                 MediaStateChangedAction.Replace, addedImportedItems, deletedImportedLocations));
                }
            }
        }
Beispiel #11
0
 public void move(MediaFileItem item, String location, CancellableOperationProgressBase progress)
 {
     
     move(new List<MediaFileItem>{item}, new List<String>{location}, progress);
 }
Beispiel #12
0
        // returns true on success
        public bool export(MediaFileItem item, CancellationToken token)
        {
            List<MediaFileItem> dummy = new List<MediaFileItem>();
            dummy.Add(item);
            int nrExported = export(dummy, token);

            return nrExported == 0 ? false : true;
        }
 private void selectedMediaDataGridView_RowDoubleClick(object sender, MediaFileItem e)
 {
     MediaFileBrowserViewModel.ExpandCommand.Execute(e);
 }
        void generatePreview(MediaFileItem item)
        {
            ItemProgressMax = 100;
            ItemProgress = 0;
            ItemInfo = "Creating video preview image for: " + System.IO.Path.GetFileName(item.Location);

            FileStream outputFile = null;
            RenderTargetBitmap bitmap = null;

            mediaProbe.open(item.Location, CancellationToken);
            try
            {

                int nrFrames = 0;

                if (asyncState.IsCaptureIntervalSecondsEnabled == false)
                {
                    nrFrames = asyncState.NrRows * asyncState.NrColumns;
                }
                else
                {
                    nrFrames = mediaProbe.DurationSeconds / asyncState.CaptureIntervalSeconds;
                    nrFrames = calcNrRowsNrColumns(nrFrames);
                }

                int thumbWidth = asyncState.MaxPreviewImageWidth / asyncState.NrColumns;

                ItemProgressMax = nrFrames;

                double captureIntervalSeconds = asyncState.IsCaptureIntervalSecondsEnabled ? asyncState.CaptureIntervalSeconds : 0;

                List<MediaThumb> thumbs = mediaProbe.grabThumbnails(thumbWidth, 0, captureIntervalSeconds
                       ,nrFrames, 0.01, CancellationToken, 0, grabThumbnails_UpdateProgressCallback);
             
                if (thumbs.Count == 0 || CancellationToken.IsCancellationRequested) return;

                nrFrames = Math.Min(thumbs.Count, nrFrames);

                if (asyncState.IsCaptureIntervalSecondsEnabled == true)
                {
                    nrFrames = calcNrRowsNrColumns(nrFrames);
                }                               

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();                
                BitmapMetadata metaData = new BitmapMetadata("jpg");
                metaData.ApplicationName = App.getAppInfoString();
                metaData.DateTaken = DateTime.Now.ToString("R");

                if (item.Metadata.Tags.Count > 0)
                {

                    List<String> tags = new List<string>();

                    foreach (Tag tag in item.Metadata.Tags)
                    {
                        tags.Add(tag.Name);
                    }

                    metaData.Keywords = new ReadOnlyCollection<string>(tags);
                }

                if (item.Metadata.Title != null)
                {
                    metaData.Title = item.Metadata.Title;
                }

                if (item.Metadata.Copyright != null)
                {
                    metaData.Copyright = item.Metadata.Copyright;
                }

                if (item.Metadata.Description != null)
                {
                    metaData.Subject = item.Metadata.Description;
                }

                if (item.Metadata.Author != null)
                {
                    List<String> author = new List<string>();
                    author.Add(item.Metadata.Author);

                    metaData.Author = new ReadOnlyCollection<string>(author);
                }

                if (item.Metadata.Rating != null)
                {
                    metaData.Rating = (int)item.Metadata.Rating.Value;
                }

                // rendertargetbitmap has to be executed on the UI thread
                // if it's run on a non-UI thread there will be a memory leak
                App.Current.Dispatcher.Invoke(() =>
                    {
                        VideoGridImage gridImage = new VideoGridImage(item.Metadata as VideoMetadata, asyncState, thumbs);
                        bitmap = gridImage.createGridImage(asyncState.MaxPreviewImageWidth);
                    });

                BitmapFrame frame = BitmapFrame.Create(bitmap, null, metaData, null);

                encoder.Frames.Add(frame);               

                String outputFileName = Path.GetFileNameWithoutExtension(item.Location) + ".jpg";              

                outputFile = new FileStream(asyncState.OutputPath + "/" + outputFileName, FileMode.Create);
                encoder.QualityLevel = asyncState.JpegQuality;
                encoder.Save(outputFile);

                ItemProgressMax = nrFrames;
                ItemProgress = nrFrames;
                InfoMessages.Add("Finished video preview image: " + asyncState.OutputPath + "/" + outputFileName);
                                               
                
            }
            catch (Exception e)
            {
                InfoMessages.Add("Error creating video preview image for: " + item.Location + " " + e.Message);
                Logger.Log.Error("Error creating preview image for: " + item.Location, e);
            }
            finally
            {
                if (bitmap != null)
                {
                    bitmap.Clear();
                    bitmap = null;                            
                }

                if (outputFile != null)
                {
                    outputFile.Close();
                    outputFile.Dispose();
                }
                
                mediaProbe.close();

                // because of the extreme amount of memory used by rendertargetbitmap 
                // make sure we have it all back before moving on to prevent out of memory spikes
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
            }
        }
        bool updateMetadata(MediaFileItem item, MetaDataUpdateViewModelAsyncState state)
        {
            bool isModified = false;

            if (item.Metadata == null || item.Metadata is UnknownMetadata)
            {
                throw new Exception("Missing or invalid metadata in media");
            }

            isModified = item.Metadata.IsModified;

            BaseMetadata media = item.Metadata;

            if (state.RatingEnabled)
            {
                Nullable<double> oldValue = media.Rating;

                media.Rating = state.Rating.HasValue == false ? null : state.Rating * 5;

                if (media.Rating != oldValue)
                {
                    isModified = true;
                }
            }

            if (state.TitleEnabled && !EqualityComparer<String>.Default.Equals(media.Title, state.Title))
            {
                media.Title = state.Title;
                isModified = true;
            }

            if (state.DescriptionEnabled && !EqualityComparer<String>.Default.Equals(media.Description, state.Description))
            {
                media.Description = state.Description;
                isModified = true;
            }

            if (state.AuthorEnabled && !EqualityComparer<String>.Default.Equals(media.Author, state.Author))
            {
                media.Author = state.Author;
                isModified = true;
            }

            if (state.CopyrightEnabled && !EqualityComparer<String>.Default.Equals(media.Copyright, state.Copyright))
            {
                media.Copyright = state.Copyright;
                isModified = true;
            }

            if (state.CreationEnabled && !(Nullable.Compare<DateTime>(media.CreationDate, state.Creation) == 0))
            {
                media.CreationDate = state.Creation;
                isModified = true;
            }

            if (state.IsGeoTagEnabled && !(Nullable.Compare<double>(media.Latitude, state.Latitude) == 0))
            {
                media.Latitude = state.Latitude;
                isModified = true;
            }

            if (state.IsGeoTagEnabled && !(Nullable.Compare<double>(media.Longitude, state.Longitude) == 0))
            {
                media.Longitude = state.Longitude;
                isModified = true;
            }

            if (state.BatchMode == false && !state.Tags.SequenceEqual(media.Tags))
            {
                media.Tags.Clear();
                foreach (Tag tag in state.Tags)
                {
                    media.Tags.Add(tag);
                }
                isModified = true;
            }
            else if (state.BatchMode == true)
            {
                bool addedTag = false;
                bool removedTag = false;

                foreach (Tag tag in state.AddTags)
                {
                    // Hashset compares items using their gethashcode function
                    // which can be different for the same database entities created at different times
                    if (!media.Tags.Contains(tag, EqualityComparer<Tag>.Default))
                    {
                        media.Tags.Add(tag);
                        addedTag = true;
                    }
                }

                foreach (Tag tag in state.RemoveTags)
                {
                    Tag removeTag = media.Tags.FirstOrDefault((t) => t.Name.Equals(tag.Name));

                    if (removeTag != null)
                    {
                        media.Tags.Remove(removeTag);
                        removedTag = true;
                    }

                }

                if (removedTag || addedTag)
                {
                    isModified = true;
                }

            }

            return (isModified);
        }