Ejemplo n.º 1
0
        public async Task writeMetaDataAsync(MetaDataUpdateViewModelAsyncState state)
        {
            TotalProgressMax = state.ItemList.Count;
            ItemProgressMax  = 100;
            TotalProgress    = 0;

            await Task.Factory.StartNew(() =>
            {
                GlobalCommands.MetaDataUpdateCommand.Execute(state);
                writeMetaData(state);
            }, CancellationToken, TaskCreationOptions.None, PriorityScheduler.Lowest);

            OkCommand.IsExecutable     = true;
            CancelCommand.IsExecutable = false;
        }
        public async Task writeMetaDataAsync(MetaDataUpdateViewModelAsyncState state)
        {
            TotalProgressMax = state.ItemList.Count;
            ItemProgressMax = 100;
            TotalProgress = 0;

            await Task.Factory.StartNew(() =>
            {
                GlobalCommands.MetaDataUpdateCommand.Execute(state);
                writeMetaData(state);

            }, CancellationToken, TaskCreationOptions.None, PriorityScheduler.Lowest);

            OkCommand.IsExecutable = true;
            CancelCommand.IsExecutable = false;
        }
Ejemplo n.º 3
0
        void writeMetaData(MetaDataUpdateViewModelAsyncState state)
        {
            List <Counter> counters = new List <Counter>();
            String         oldPath = "", newPath = "";
            String         oldFilename = "", newFilename = "", ext = "";

            foreach (MediaFileItem item in state.ItemList)
            {
                if (CancellationToken.IsCancellationRequested)
                {
                    return;
                }

                ItemProgress = 0;
                ItemInfo     = "Opening: " + item.Location;
                bool isModified = false;


                // Update Metadata values
                item.EnterWriteLock();
                try
                {
                    isModified = updateMetadata(item, state);
                }
                catch (Exception e)
                {
                    string info = "Error updating Metadata: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Error(info, e);
                    MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                finally
                {
                    item.ExitWriteLock();
                }

                // Save Metadata to disk
                if (isModified)
                {
                    String info;
                    ItemInfo = "Saving MetaData: " + item.Location;

                    item.EnterUpgradeableReadLock();
                    try
                    {
                        item.writeMetadata_URLock(MetadataFactory.WriteOptions.AUTO, this);
                    }
                    catch (Exception e)
                    {
                        info = "Error saving Metadata: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Error(info, e);
                        MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                        // reload metaData, exceptions are caught in readMetadata
                        if (item.Metadata != null)
                        {
                            item.Metadata.clear();
                        }

                        item.readMetadata_URLock(MetadataFactory.ReadOptions.AUTO |
                                                 MetadataFactory.ReadOptions.GENERATE_THUMBNAIL, CancellationToken);

                        return;
                    }
                    finally
                    {
                        item.ExitUpgradeableReadLock();
                    }

                    info = "Completed updating Metadata for: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Info(info);
                }
                else
                {
                    string info = "Skipped updating Metadata (no changes) for: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Info(info);
                }

                // Export if requested
                if (state.ImportedEnabled == true && state.IsImported == false)
                {
                    bool success = MediaFileState.export(item, CancellationToken);

                    if (success)
                    {
                        string info = "Exported: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Info(info);
                    }
                }

                //rename and/or move
                item.EnterReadLock();
                try
                {
                    oldPath     = FileUtils.getPathWithoutFileName(item.Location);
                    oldFilename = Path.GetFileNameWithoutExtension(item.Location);
                    ext         = Path.GetExtension(item.Location);

                    newFilename = parseNewFilename(state.Filename, state.ReplaceFilename, state.IsRegexEnabled, oldFilename, item.Metadata);
                    newPath     = String.IsNullOrEmpty(state.Location) ? oldPath : state.Location;
                    newPath     = newPath.TrimEnd('\\');
                }
                finally
                {
                    item.ExitReadLock();
                }

                try
                {
                    MediaFileState.move(item, newPath + "\\" + newFilename + ext, this);
                }
                catch (Exception e)
                {
                    string info = "Error moving/renaming: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Error(info, e);
                    MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }

                // import if requested
                if (state.ImportedEnabled == true && state.IsImported == true)
                {
                    bool success = false;
                    try
                    {
                        success = MediaFileState.import(item, CancellationToken);
                    }
                    catch (Exception e)
                    {
                        string info = "Error importing media: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Error(info, e);
                        MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    if (success)
                    {
                        string info = "Imported: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Info(info);
                    }
                }

                ItemProgress = 100;
                TotalProgress++;
            }

            if (state.BatchMode == true)
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MiscUtils.insertIntoHistoryCollection(Settings.Default.FilenameHistory, state.Filename);
                }));
            }
            else if (!oldFilename.Equals(newFilename))
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MiscUtils.insertIntoHistoryCollection(Settings.Default.FilenameHistory, newFilename);
                }));
            }

            if (!oldPath.Equals(newPath))
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MiscUtils.insertIntoHistoryCollection(Settings.Default.MetaDataUpdateDirectoryHistory, newPath);
                }));
            }
        }
Ejemplo n.º 4
0
        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);
        }
        void writeMetaData(MetaDataUpdateViewModelAsyncState state)
        {
            List<Counter> counters = new List<Counter>();
            String oldPath = "", newPath = "";
            String oldFilename = "", newFilename = "", ext = "";

            foreach (MediaFileItem item in state.ItemList)
            {
                if (CancellationToken.IsCancellationRequested) return;

                ItemProgress = 0;
                ItemInfo = "Opening: " + item.Location;
                bool isModified = false;

                
                // Update Metadata values
                item.EnterWriteLock();
                try
                {
                    isModified = updateMetadata(item, state);
                }
                catch (Exception e)
                {
                    string info = "Error updating Metadata: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Error(info, e);
                    MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                finally
                {
                    item.ExitWriteLock();
                }

                // Save Metadata to disk
                if (isModified)
                {
                    String info;
                    ItemInfo = "Saving MetaData: " + item.Location;

                    item.EnterUpgradeableReadLock();
                    try
                    {
                        item.writeMetadata_URLock(MetadataFactory.WriteOptions.AUTO, this);
                    }
                    catch (Exception e)
                    {
                        info = "Error saving Metadata: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Error(info, e);
                        MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                        // reload metaData, exceptions are caught in readMetadata
                        if (item.Metadata != null)
                        {
                            item.Metadata.clear();
                        }
                           
                        item.readMetadata_URLock(MetadataFactory.ReadOptions.AUTO |
                            MetadataFactory.ReadOptions.GENERATE_THUMBNAIL, CancellationToken);

                        return;
                    }
                    finally
                    {
                        item.ExitUpgradeableReadLock();
                    }

                    info = "Completed updating Metadata for: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Info(info);
                }
                else
                {
                    string info = "Skipped updating Metadata (no changes) for: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Info(info);
                }

                // Export if requested
                if (state.ImportedEnabled == true && state.IsImported == false)
                {
                    bool success = MediaFileState.export(item, CancellationToken);

                    if (success)
                    {
                        string info = "Exported: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Info(info);
                    }
                }

                //rename and/or move   
                item.EnterReadLock();
                try
                {
                    oldPath = FileUtils.getPathWithoutFileName(item.Location);
                    oldFilename = Path.GetFileNameWithoutExtension(item.Location);
                    ext = Path.GetExtension(item.Location);

                    newFilename = parseNewFilename(state.Filename, state.ReplaceFilename, state.IsRegexEnabled, oldFilename, item.Metadata);
                    newPath = String.IsNullOrEmpty(state.Location) ? oldPath : state.Location;
                    newPath = newPath.TrimEnd('\\');
                }
                finally
                {
                    item.ExitReadLock();
                }

                try
                {
                    MediaFileState.move(item, newPath + "\\" + newFilename + ext, this);
                }
                catch (Exception e)
                {
                    string info = "Error moving/renaming: " + item.Location;

                    InfoMessages.Add(info);
                    Logger.Log.Error(info, e);
                    MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;

                }

                // import if requested
                if (state.ImportedEnabled == true && state.IsImported == true)
                {
                    bool success = false;
                    try
                    {
                        success = MediaFileState.import(item, CancellationToken);
                    }
                    catch (Exception e)
                    {
                        string info = "Error importing media: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Error(info, e);
                        MessageBox.Show(info + "\n\n" + e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                        return;
                    }

                    if (success)
                    {
                        string info = "Imported: " + item.Location;

                        InfoMessages.Add(info);
                        Logger.Log.Info(info);
                    }

                }

                ItemProgress = 100;
                TotalProgress++;
                                
            }

            if (state.BatchMode == true)
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MiscUtils.insertIntoHistoryCollection(Settings.Default.FilenameHistory, state.Filename);
                }));

            }
            else if (!oldFilename.Equals(newFilename))
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MiscUtils.insertIntoHistoryCollection(Settings.Default.FilenameHistory, newFilename);
                }));
            }

            if (!oldPath.Equals(newPath))
            {
                App.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    MiscUtils.insertIntoHistoryCollection(Settings.Default.MetaDataUpdateDirectoryHistory, newPath);
                }));
            }


        }
        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);
        }