Example #1
0
        public static async Task RenameFolder(this StorageLibraryChange item, IEnumerable <Mediafile> Library, LibraryService LibraryService)
        {
            int successCount = 0;
            List <Mediafile> ChangedMediafiles = new List <Mediafile>();

            //if it's a folder, get all elements in that folder and change their directory
            foreach (var mediaFile in await LibraryService.Query(item.PreviousPath.ToUpperInvariant()))
            {
                var libraryMediafile = Library.First(t => t.Path == mediaFile.Path);

                //change the folder path.
                //NOTE: this also works for subfolders
                mediaFile.FolderPath = mediaFile.FolderPath.Replace(item.PreviousPath, item.Path);
                mediaFile.Path       = mediaFile.Path.Replace(item.PreviousPath, item.Path);

                //verify that the new path exists before updating.
                if (SharedLogic.VerifyFileExists(mediaFile.Path, 200))
                {
                    successCount++;

                    //add to the list so we can update in bulk (it's faster that way.)
                    ChangedMediafiles.Add(mediaFile);

                    libraryMediafile.Path = mediaFile.Path;
                }
            }
            if (successCount > 0)
            {
                //update in bulk.
                LibraryService.UpdateMediafiles <Mediafile>(ChangedMediafiles);

                await SharedLogic.NotificationManager.ShowMessageAsync(string.Format("{0} Mediafiles Updated. Folder Path: {1}", successCount, item.Path), 5);
            }
        }
Example #2
0
        public static async Task RemoveFolder(this StorageLibraryChange change, ThreadSafeObservableCollection <Mediafile> Library, LibraryService LibraryService)
        {
            int successCount = 0;
            List <Mediafile> RemovedMediafiles = new List <Mediafile>();

            //iterate all the files in the library that were in
            //the deleted folder.
            foreach (var mediaFile in await LibraryService.Query(string.IsNullOrEmpty(change.PreviousPath) ? change.Path.ToUpperInvariant() : change.PreviousPath.ToUpperInvariant()))
            {
                //verify that the file was deleted because it can be a false call.
                //we do not want to delete a file that exists.
                if (!SharedLogic.VerifyFileExists(mediaFile.Path, 200))
                {
                    RemovedMediafiles.Add(mediaFile);
                    successCount++;
                }
            }
            if (successCount > 0)
            {
                Library.RemoveRange(RemovedMediafiles);
                await LibraryService.RemoveMediafiles(RemovedMediafiles);

                await SharedLogic.NotificationManager.ShowMessageAsync(string.Format("{0} Mediafiles Removed. Folder Path: {1}", successCount, change.Path), 5);
            }
        }
Example #3
0
 public static bool IsItemInLibrary(this StorageLibraryChange change, IEnumerable <Mediafile> Library, out Mediafile file)
 {
     if (Library.Any(t => t.Path == (string.IsNullOrEmpty(change.PreviousPath) ? change.Path : change.PreviousPath)))
     {
         file = Library.First(t => t.Path == (string.IsNullOrEmpty(change.PreviousPath) ? change.Path : change.PreviousPath));
         return(true);
     }
     file = null;
     return(false);
 }
Example #4
0
 public static async Task RemoveItem(this StorageLibraryChange change, IEnumerable <Mediafile> Library, LibraryService LibraryService)
 {
     if (change.IsOfType(StorageItemTypes.File))
     {
         if (IsItemInLibrary(change, Library, out Mediafile movedItem))
         {
             if (await SharedLogic.RemoveMediafile(movedItem))
             {
                 await SharedLogic.NotificationManager.ShowMessageAsync(string.Format("Mediafile Removed. File Path: {0}", movedItem.Path), 5);
             }
         }
     }
     else
     {
         await RemoveFolder(change, (ThreadSafeObservableCollection <Mediafile>) Library, LibraryService);
     }
 }
Example #5
0
        public static async Task AddNewItem(this StorageLibraryChange change)
        {
            if (change.IsOfType(StorageItemTypes.File))
            {
                if (await change.GetStorageItemAsync() == null)
                {
                    return;
                }
                if (IsItemPotentialMediafile(await change.GetStorageItemAsync()))
                {
                    var newFile = await SharedLogic.CreateMediafile((StorageFile)await change.GetStorageItemAsync());

                    newFile.FolderPath = Path.GetDirectoryName(newFile.Path);
                    if (SharedLogic.AddMediafile(newFile))
                    {
                        await SharedLogic.NotificationManager.ShowMessageAsync(string.Format("Mediafile Added. File Path: {0}", newFile.Path), 5);
                    }
                }
            }
        }
Example #6
0
        public static async Task UpdateChangedItem(this StorageLibraryChange change, IEnumerable <Mediafile> Library, LibraryService LibraryService)
        {
            if (change.IsOfType(StorageItemTypes.File))
            {
                if (IsItemInLibrary(change, Library, out Mediafile createdItem))
                {
                    var id = createdItem.Id;
                    createdItem = await SharedLogic.CreateMediafile((StorageFile)await change.GetStorageItemAsync());

                    createdItem.Id = id;
                    if (await LibraryService.UpdateMediafile(createdItem))
                    {
                        await SharedLogic.NotificationManager.ShowMessageAsync(string.Format("Mediafile Updated. File Path: {0}", createdItem.Path), 5);
                    }
                }
                else
                {
                    await AddNewItem(change);
                }
            }
        }
        private async Task ProcessFileChange(StorageLibraryChange change)
        {
            // Temp variable used for instantiating StorageFiles for sorting if needed later
            //StorageFile newFile = null;
            var extension = Path.GetExtension(change.Path);

            switch (change.ChangeType)
            {
            // New File in the Library
            case StorageLibraryChangeType.Created:
            case StorageLibraryChangeType.MovedIntoLibrary:
                if (FileTypes.Extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
                {
                    try
                    {
                        // Don't add file to gallery if it already exists.
                        if (MediaDatas.Any(data => data.MediaFile.Path == change.Path))
                        {
                            return;
                        }

                        var mediaFile = (StorageFile)(await change.GetStorageItemAsync());

                        if (mediaFile == null)
                        {
                            return;
                        }

                        await AddFileAsync(ImageSize, mediaFile);
                    }
                    catch (Exception e)
                    {
                        // Log Exception.
                        LifecycleLog.Exception(e);
                    }
                }
                break;

            // Renamed file
            case StorageLibraryChangeType.MovedOrRenamed:
                if (FileTypes.Extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
                {
                    try
                    {
                        var mediaData = GetMediaDataFromPath(change.PreviousPath, MediaDatas);
                        var newName   = Path.GetFileName(change.Path);

                        // If no MediaData could be found in Gallery for this renamed item AND there is an entry for it in App.MediaDatas (lostMediaData)
                        // that probably means it has been renamed while the application was off, so update its path and add it to the gallery.
                        var lostMediaFile = GetMediaFileFromPath(change.Path, _lostMediaFiles);
                        if (mediaData == null && lostMediaFile != null)
                        {
                            var lostMetaData = GetMediaMetaDataFromPath(change.PreviousPath, App.DatabaseMetaDatas);
                            // Update path on metadata.
                            lostMetaData.MediaFilePath = change.Path;
                            // Get current MediaData associated with metadata.
                            var lostMediaData = await MediaData.CreateFromMediaMetadataAsync(lostMetaData);

                            // If file can still not be found then return.
                            if (lostMediaData == null)
                            {
                                return;
                            }
                            // Add file to gallery, including the lost media data.
                            await AddFileAsync(ImageSize, lostMediaFile, false, lostMediaData);
                        }
                        else if (mediaData == null)
                        {
                            return;
                        }

                        // Don't rename file in gallery if it is already renamed
                        if (MediaDatas.Any(data => data.Title == newName))
                        {
                            return;
                        }

                        var mediaFile = (StorageFile)(await change.GetStorageItemAsync());
                        // Run on UI thread.
                        await App.Current.NavigationService.Frame.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            // Update MediaFile of mediaData.
                            mediaData.MediaFile = mediaFile;
                        });

                        await mediaData.UpdateFileNameValuesAsync(newName);
                    }
                    catch (Exception e)
                    {
                        // Log Exception.
                        LifecycleLog.Exception(e);
                    }
                }
                break;

            // File Removed From Library
            case StorageLibraryChangeType.Deleted:
            case StorageLibraryChangeType.MovedOutOfLibrary:
                if (FileTypes.Extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
                {
                    try
                    {
                        // Only remove file from gallery if it exists in gallery.
                        if (MediaDatas.Any(data => data.MediaFile.Path == change.Path))
                        {
                            RemoveFile(change.Path);
                        }
                    }
                    catch (Exception e)
                    {
                        // Log Exception.
                        LifecycleLog.Exception(e);
                    }
                }
                break;

            // Modified Contents
            case StorageLibraryChangeType.ContentsChanged:
                if (FileTypes.Extensions.Contains(extension, StringComparer.InvariantCultureIgnoreCase))
                {
                    /*newFile = (StorageFile)(await change.GetStorageItemAsync());
                     * var imageProps = await newFile.Properties.GetImagePropertiesAsync();
                     * var dateTaken = imageProps.DateTaken;
                     * var dateModified = newFile.DateCreated;
                     * if (DateTimeOffset.Compare(dateTaken.AddSeconds(70), dateModified) > 0)
                     * {
                     *  // File was modified by the user
                     * }*/
                }
                break;

            // Ignored Cases
            case StorageLibraryChangeType.EncryptionChanged:
            case StorageLibraryChangeType.ContentsReplaced:
            case StorageLibraryChangeType.IndexingStatusChanged:
            default:
                // These are safe to ignore in this application
                break;
            }
        }