示例#1
0
        /// <summary>
        /// Load the the new GRAMPS data.
        /// </summary>
        /// <returns>
        /// True if the load is successful or False if not.
        /// </returns>
        public async Task <bool> TriggerLoadGRAMPSFileAsync(bool deleteOld)
        {
            FileInfoEx fileGrampsDataInput = await StoreFolder.FolderGetFileAsync(DataStore.AD.CurrentDataFolder, CommonConstants.StorageGRAMPSFileName).ConfigureAwait(false);

            if (fileGrampsDataInput != null)
            {
                if (deleteOld)
                {
                    // TODO fix this
                    //await localStoreFile.DataStorageInitialiseAsync(DataStore.AD.CurrentDataFolder).ConfigureAwait(false);
                }

                await localStoreFile.DecompressGZIP(fileGrampsDataInput).ConfigureAwait(false);

                // Save the current Index File modified date for later checking
                //StoreFileNames.SaveFileModifiedSinceLastSave(CommonConstants.SettingsGPRAMPSFileLastDateTimeModified, CommonConstants.StorageGRAMPSFileName);
                StoreFileNames.SaveFileModifiedSinceLastSave(CommonConstants.SettingsGPRAMPSFileLastDateTimeModified, fileGrampsDataInput);
            }

            await DataStore.CN.MajorStatusDelete().ConfigureAwait(false);

            await DataStore.CN.ChangeLoadingMessage(null).ConfigureAwait(false);

            return(false);
        }
示例#2
0
        /// <summary>
        /// Loads the Gramps XML data.
        /// </summary>
        /// <param name="dataFolder">
        /// StorageFolder to load.
        /// </param>
        /// <returns>
        /// Flag if Gramps Data Only loaded successfully.
        /// </returns>
        public async Task <bool> DataStorageLoadXML()
        {
            try
            {
                FileInfoEx inputFile = StoreFolder.FolderGetFile(DataStore.AD.CurrentDataFolder, CommonConstants.StorageXMLFileName);

                await DataStore.CN.MajorStatusAdd("Loading existing local copy of the GRAMPS data").ConfigureAwait(false);

                {
                    Stream xmlReader = inputFile.FInfo.OpenRead();

                    try
                    {
                        // string t = inputFile.Path;
                        localGrampsXMLdoc = XDocument.Load(xmlReader);
                    }
                    catch (Exception ex)
                    {
                        DataStore.CN.NotifyException("Can not load the Gramps XML file. Error in basic XML load", ex);

                        throw;
                    }

                    int compareFlag = string.Compare(localGrampsXMLdoc.DocumentType.PublicId, CommonConstants.GrampsXMLPublicId, StringComparison.CurrentCulture);
                    if (compareFlag < 0)
                    {
                        DataStore.CN.NotifyError("The program can only load files with a Gramps XML version of " + CommonConstants.GrampsXMLPublicId + " or greater.  This file is version " + localGrampsXMLdoc.DocumentType.PublicId);
                        return(false);
                    }

                    var nameSpaceList = localGrampsXMLdoc.Root.Attributes().Where(
                        a => a.IsNamespaceDeclaration).GroupBy(
                        a => a.Name.Namespace == XNamespace.None ? string.Empty : a.Name.LocalName,
                        a => XNamespace.Get(a.Value)).ToDictionary(
                        g => g.Key,
                        g => g.FirstOrDefault());

                    ns = nameSpaceList.FirstOrDefault().Value.NamespaceName;

                    xmlReader.Dispose();
                }

                await DataStore.CN.MajorStatusDelete().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                DataStore.CN.NotifyException("trying to load Gramps data only", ex);
                throw;
            }

            return(true);
        }
示例#3
0
        /// <summary>
        /// Load the the new GRAMPS data.
        /// </summary>
        /// <returns>
        /// True if the load is successful or False if not.
        /// </returns>
        public async Task <bool> TriggerLoadGrampsUnZippedFolderAsync()
        {
            // HockeyClient.Current.TrackEvent("TriggerLoadGrampsUnZippedFolderAsync");
            await DataStore.CN.ChangeLoadingMessage("Loading GRAMPS XML unzipped data").ConfigureAwait(false);

            {
                ClearRepositories();

                bool tt = await localExternalStorage.DataStorageLoadXML().ConfigureAwait(false);

                if (tt)
                {
                    await localExternalStorage.LoadXMLDataAsync().ConfigureAwait(false);

                    await DataStore.CN.MajorStatusAdd("Finished loading GRAMPS XML data").ConfigureAwait(false);

                    FileInfoEx t = await StoreFolder.FolderGetFileAsync(DataStore.AD.CurrentDataFolder, CommonConstants.StorageXMLFileName).ConfigureAwait(false);

                    if (t.Valid)
                    {
                        // Save the current Index File modified date for later checking
                        StoreFileNames.SaveFileModifiedSinceLastSave(CommonConstants.SettingsXMLFileLastDateTimeModified, t);
                    }

                    UpdateSettings();

                    // save the data in a serial format for next time localEventAggregator.GetEvent<DataSaveSerialEvent>().Publish(null);

                    // let everybody know we have finished loading data
                    _EventAggregator.GetEvent <DataLoadXMLEvent>().Publish(null);

                    return(true);
                }
            }

            await DataStore.CN.MajorStatusDelete().ConfigureAwait(false);

            await DataStore.CN.ChangeLoadingMessage(null).ConfigureAwait(false);

            return(true);
        }
示例#4
0
        private StoreFolder GetFolder(string path, out StoreFolder parent)
        {
            string[] components = path.Split(Separators, StringSplitOptions.RemoveEmptyEntries);

            if(components.Length == 0) {
                parent = null;
                return _root;
            }

            parent = _root;
            StoreFolder folder = _root;

            // try to locate the folder
            int length = components.Length;
            for(int i = 0; i < length; i++) {
                parent = folder;

                if(folder.Subfolders.TryGetValue(components[i], out folder) == false) {
                    // not found
                    return null;
                }
            }

            return folder;
        }
示例#5
0
 /// <summary>
 /// Removes all files and folders from the store.
 /// </summary>
 private void Format()
 {
     _root = new StoreFolder("root");
     files.Clear();
 }
示例#6
0
        private bool DeserializeFolders(byte[] data)
        {
            MemoryStream stream = null;

            if(_encrypt) {
                // decrypt the folder data
                data = DecryptData(data);
            }

            try {
                stream = new MemoryStream(data);
                BinaryFormatter formatter = new BinaryFormatter();

                _root = (StoreFolder)formatter.Deserialize(stream);
                return true;
            }
            catch(Exception e) {
                return false;
            }
            finally {
                if(stream != null) {
                    stream.Close();
                }
            }
        }
示例#7
0
 public FileStore()
 {
     _root = new StoreFolder("root");
     files = new Dictionary<Guid, StoreFile>();
 }
示例#8
0
        private StoreFolder CreateFolderImpl(string path)
        {
            string[] components = path.Split(Separators, StringSplitOptions.RemoveEmptyEntries);

            if(components.Length == 0) {
                return null;
            }

            StoreFolder parent = _root;
            StoreFolder folder = null;

            // locate the parent
            int position = 0;
            int length = components.Length;

            while(position < (length - 1)) {
                if(parent.Subfolders.TryGetValue(components[position], out folder) == false) {
                    break;
                }

                parent = folder;
                position++;
            }

            // create the required folders
            while(position < length) {
                string name = components[position];
                folder = new StoreFolder(name);

                parent.Subfolders.Add(name, folder);
                parent = folder;

                position++;
            }

            return folder;
        }
示例#9
0
        public static async Task <HLinkMediaModel> CreateClippedMediaModel(HLinkLoadImageModel argHLinkLoadImageModel)
        {
            if (argHLinkLoadImageModel is null)
            {
                throw new ArgumentNullException(nameof(argHLinkLoadImageModel));
            }

            if (!argHLinkLoadImageModel.DeRef.Valid)
            {
                throw new ArgumentException("CreateClippedMediaModel argument is invalid", nameof(argHLinkLoadImageModel));
            }

            IMediaModel returnMediaModel = await MainThread.InvokeOnMainThreadAsync(() =>
            {
                // TODO cleanup code. Multiple copies of things in use

                IMediaModel theMediaModel = argHLinkLoadImageModel.DeRef;

                SKBitmap resourceBitmap = new SKBitmap();

                IMediaModel newMediaModel = new MediaModel();

                string newHLinkKey = argHLinkLoadImageModel.HLinkKey + "-" + argHLinkLoadImageModel.GCorner1X + argHLinkLoadImageModel.GCorner1Y + argHLinkLoadImageModel.GCorner2X + argHLinkLoadImageModel.GCorner2Y;
                string outFileName = Path.Combine("Cropped", newHLinkKey + ".png");

                string outFilePath = Path.Combine(DataStore.AD.CurrentDataFolder.FullName, outFileName);

                Debug.WriteLine(argHLinkLoadImageModel.DeRef.MediaStorageFilePath);

                // Check if already exists
                IMediaModel fileExists = DV.MediaDV.GetModelFromHLinkString(newHLinkKey);

                if ((!fileExists.Valid) && (theMediaModel.IsMediaStorageFileValid))
                {
                    // Needs clipping
                    using (StreamReader stream = new StreamReader(theMediaModel.MediaStorageFilePath))
                    {
                        resourceBitmap = SKBitmap.Decode(stream.BaseStream);
                    }

                    // Check for too large a bitmap
                    Debug.WriteLine("Image ResourceBitmap size: " + resourceBitmap.ByteCount);
                    if (resourceBitmap.ByteCount > int.MaxValue - 1000)
                    {
                        // TODO Handle this better. Perhaps resize? Delete for now
                        resourceBitmap = new SKBitmap();
                    }

                    float crleft   = (float)(argHLinkLoadImageModel.GCorner1X / 100d * theMediaModel.MetaDataWidth);
                    float crright  = (float)(argHLinkLoadImageModel.GCorner2X / 100d * theMediaModel.MetaDataWidth);
                    float crtop    = (float)(argHLinkLoadImageModel.GCorner1Y / 100d * theMediaModel.MetaDataHeight);
                    float crbottom = (float)(argHLinkLoadImageModel.GCorner2Y / 100d * theMediaModel.MetaDataHeight);

                    SKRect cropRect = new SKRect(crleft, crtop, crright, crbottom);

                    SKBitmap croppedBitmap = new SKBitmap(
                        (int)cropRect.Width,
                        (int)cropRect.Height
                        );

                    SKRect dest = new SKRect(
                        0,
                        0,
                        cropRect.Width,
                        cropRect.Height
                        );

                    SKRect source = new SKRect(
                        cropRect.Left,
                        cropRect.Top,
                        cropRect.Right,
                        cropRect.Bottom);

                    using (SKCanvas canvas = new SKCanvas(croppedBitmap))
                    {
                        canvas.DrawBitmap(resourceBitmap, source, dest);
                    }

                    // create an image COPY
                    SKImage image = SKImage.FromBitmap(croppedBitmap);

                    // encode the image (defaults to PNG)
                    SKData encoded = image.Encode();

                    // get a stream over the encoded data

                    using (Stream stream = File.Open(outFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                    {
                        encoded.SaveTo(stream);
                    }

                    croppedBitmap.Dispose();

                    // ------------ Save new MediaObject
                    newMediaModel          = theMediaModel.Copy();
                    newMediaModel.HLinkKey = newHLinkKey;

                    newMediaModel.OriginalFilePath = outFileName;
                    newMediaModel.MediaStorageFile = StoreFolder.FolderGetFile(DataStore.AD.CurrentDataFolder, outFileName);;
                    newMediaModel.IsClippedFile    = true;

                    newMediaModel.MetaDataHeight = cropRect.Height;
                    newMediaModel.MetaDataWidth  = cropRect.Width;

                    newMediaModel = SetHomeImage(newMediaModel);

                    DataStore.DS.MediaData.Add((MediaModel)newMediaModel);
                }
                else
                {
                    Dictionary <string, string> argErrorDetail = new Dictionary <string, string>
                    {
                        { "Original ID", theMediaModel.Id },
                        { "Original File", theMediaModel.MediaStorageFilePath },
                        { "Clipped Id", argHLinkLoadImageModel.DeRef.Id }
                    };

                    DataStore.CN.NotifyError("File not found when Region specified in ClipMedia", argErrorDetail);
                }

                resourceBitmap.Dispose();

                return(newMediaModel);
            }).ConfigureAwait(false);

            return(returnMediaModel.HLink);
        }
示例#10
0
        public async static Task <bool> FixSingleMediaFile(IMediaModel argMediaModel)
        {
            try
            {
                if (argMediaModel.IsOriginalFilePathValid)
                {
                    //_CL.LogVariable("tt.OriginalFilePath", argMediaModel.OriginalFilePath); //
                    //_CL.LogVariable("localMediaFolder.path", DataStore.AD.CurrentDataFolder.FullName); //
                    //_CL.LogVariable("path", DataStore.AD.CurrentDataFolder.FullName + "\\" + argMediaModel.OriginalFilePath);

                    DataStore.DS.MediaData[argMediaModel.HLinkKey].MediaStorageFile = StoreFolder.FolderGetFile(DataStore.AD.CurrentDataFolder, argMediaModel.OriginalFilePath);
                }
            }
            catch (FileNotFoundException ex)
            {
                DataStore.CN.NotifyError("File (" + argMediaModel.OriginalFilePath + ") not found while   loading media. Has the GRAMPS database been verified ? " + ex.ToString());

                DataStore.CN.NotifyException("Trying to  add media file pointer", ex);
            }
            catch (Exception ex)
            {
                CommonLocalSettings.DataSerialised = false;
                DataStore.CN.NotifyException("Trying to add media file pointer", ex);

                await DataStore.CN.MajorStatusDelete().ConfigureAwait(false);

                throw;
            }

            return(false);
        }
示例#11
0
        /// <summary>
        /// Starts the data load asynchronous. Order is:
        /// 1) UnTar new *.GPKG file
        /// 2) UnZip new data.GRAMPS file
        /// 3) Load new data.XML file
        /// 4) ELSE load Serial file.
        /// </summary>
        /// <returns>
        /// Returns a empty task.
        /// </returns>
        public async Task <bool> StartDataLoadAsync()
        {
            await DataStore.CN.ChangeLoadingMessage("Loading Data...").ConfigureAwait(false);

            if (DataStore.DS.IsDataLoaded)
            {
                return(true);
            }

            // Clear the repositories in case we had to restart after being interupted.
            ClearRepositories();

            DataStore.AD.LoadDataStore();

            if (DataStore.AD.CurrentDataFolderValid)
            {
                // 1) UnTar *.GPKG file DataStore.AD.CurrentInputFile = await StoreFileNames.FileGetFirstGPKG().ConfigureAwait(false);

                if (DataStore.AD.CurrentInputFile != null)
                {
                    //if (await StoreFileNames.FileModifiedSinceLastSaveAsync(CommonConstants.SettingsGPKGFileLastDateTimeModified, DataStore.AD.CurrentInputFile.).ConfigureAwait(false))
                    //{
                    await DataStore.CN.ChangeLoadingMessage("Later version of Gramps XML data plus Media  compressed file found. Loading it into the program").ConfigureAwait(false);

                    await TriggerLoadGPKGFileAsync(false).ConfigureAwait(false);

                    //await TriggerLoadGRAMPSFileAsync(false).ConfigureAwait(false);
                    //}
                }

                // 2) UnZip new data.GRAMPS file
                FileInfoEx GrampsFile = await StoreFolder.FolderGetFileAsync(DataStore.AD.CurrentDataFolder, CommonConstants.StorageGRAMPSFileName).ConfigureAwait(false);

                if (GrampsFile.Valid)
                {
                    if (StoreFileNames.FileModifiedSinceLastSaveAsync(CommonConstants.SettingsGPRAMPSFileLastDateTimeModified, GrampsFile))
                    {
                        await DataStore.CN.ChangeLoadingMessage("Later version of Gramps data file found. Loading it into the program").ConfigureAwait(false);

                        await TriggerLoadGRAMPSFileAsync(false).ConfigureAwait(false);
                    }
                }

                // 3) Load new data.XML file
                FileInfoEx dataXML = await StoreFolder.FolderGetFileAsync(DataStore.AD.CurrentDataFolder, CommonConstants.StorageXMLFileName).ConfigureAwait(false);

                if (dataXML.Valid)
                {
                    if (StoreFileNames.FileModifiedSinceLastSaveAsync(CommonConstants.SettingsXMLFileLastDateTimeModified, dataXML))
                    {
                        await DataStore.CN.ChangeLoadingMessage("Later version of Gramps XML data file found. Loading it into the program").ConfigureAwait(false);

                        // Load the new data
                        await TriggerLoadGrampsUnZippedFolderAsync().ConfigureAwait(false);

                        return(true);
                    }
                }

                if (CommonLocalSettings.DataSerialised)
                {
                    // 4) ELSE load Serial file
                    await TriggerLoadSerialDataAsync().ConfigureAwait(false);
                }

                await DataStore.CN.ChangeLoadingMessage(null).ConfigureAwait(false);

                return(true);
            }
            else
            {
                //if (!(DataStore.AD.CurrentDataFolder.Status.LatestException is null))
                //{
                //    DataStore.CN.NotifyException("StartDataLoadAsync", DataStore.AD.CurrentDataFolder.Status.LatestException);
                //}

                DataStore.CN.NotifyError("DataStorageFolder not valid.  It will need to be reloaded...");

                CommonLocalSettings.SetReloadDatabase();
            }

            // TODO Handle special messages if there is a problem
            await DataStore.CN.ChangeLoadingMessage("Unable to load Datafolder").ConfigureAwait(false);

            return(false);
        }
        public static async Task <HLinkMediaModel> CreateClippedMediaModel(HLinkLoadImageModel argHLinkLoadImageModel)
        {
            if (argHLinkLoadImageModel is null)
            {
                throw new ArgumentNullException(nameof(argHLinkLoadImageModel));
            }

            if (!argHLinkLoadImageModel.DeRef.Valid)
            {
                throw new ArgumentException("CreateClippedMediaModel argument is invalid", nameof(argHLinkLoadImageModel));
            }

            // TODO cleanup code. Multiple copies of things in use

            MediaModel theMediaModel = argHLinkLoadImageModel.DeRef;

            SKBitmap resourceBitmap = new SKBitmap();

            MediaModel newMediaModel = new MediaModel();

            string newHLinkKey = argHLinkLoadImageModel.HLinkKey + "-" + argHLinkLoadImageModel.GCorner1X + argHLinkLoadImageModel.GCorner1Y + argHLinkLoadImageModel.GCorner2X + argHLinkLoadImageModel.GCorner2Y;
            string outFileName = Path.Combine("Cropped", newHLinkKey + ".png");

            string outFilePath = Path.Combine(DataStore.AD.CurrentDataFolder.FullName, outFileName);

            Debug.WriteLine(argHLinkLoadImageModel.DeRef.MediaStorageFilePath);

            // Check if already exists
            MediaModel fileExists = DV.MediaDV.GetModelFromHLinkString(newHLinkKey);

            if (!fileExists.Valid)
            {
                // Needs clipping
                using (StreamReader stream = new StreamReader(theMediaModel.MediaStorageFilePath))
                {
                    resourceBitmap = SKBitmap.Decode(stream.BaseStream);
                }

                // Check for too large a bitmap
                Debug.WriteLine("Image ResourceBitmap size: " + resourceBitmap.ByteCount);
                if (resourceBitmap.ByteCount > int.MaxValue - 1000)
                {
                    // TODO Handle this better. Perhaps resize? Delete for now
                    resourceBitmap = new SKBitmap();
                }

                float crleft   = (float)(argHLinkLoadImageModel.GCorner1X / 100d * theMediaModel.MetaDataWidth);
                float crright  = (float)(argHLinkLoadImageModel.GCorner2X / 100d * theMediaModel.MetaDataWidth);
                float crtop    = (float)(argHLinkLoadImageModel.GCorner1Y / 100d * theMediaModel.MetaDataHeight);
                float crbottom = (float)(argHLinkLoadImageModel.GCorner2Y / 100d * theMediaModel.MetaDataHeight);

                SKRect cropRect = new SKRect(crleft, crtop, crright, crbottom);

                SKBitmap croppedBitmap = new SKBitmap(
                    (int)cropRect.Width,
                    (int)cropRect.Height
                    );

                SKRect dest = new SKRect(
                    0,
                    0,
                    cropRect.Width,
                    cropRect.Height
                    );

                SKRect source = new SKRect(
                    cropRect.Left,
                    cropRect.Top,
                    cropRect.Right,
                    cropRect.Bottom);

                using (SKCanvas canvas = new SKCanvas(croppedBitmap))
                {
                    canvas.DrawBitmap(resourceBitmap, source, dest);
                }

                // create an image COPY
                SKImage image = SKImage.FromBitmap(croppedBitmap);

                // encode the image (defaults to PNG)
                SKData encoded = image.Encode();

                // get a stream over the encoded data

                using (Stream stream = File.Open(outFilePath, FileMode.OpenOrCreate, System.IO.FileAccess.Write, FileShare.ReadWrite))
                {
                    encoded.SaveTo(stream);
                }

                croppedBitmap.Dispose();

                // ------------ Save new MediaObject
                newMediaModel          = theMediaModel.Copy();
                newMediaModel.HLinkKey = newHLinkKey;

                newMediaModel.HomeImageHLink.HLinkKey      = newHLinkKey;
                newMediaModel.HomeImageHLink.HomeImageType = CommonConstants.HomeImageTypeThumbNail;
                newMediaModel.HomeImageHLink.HomeSymbol    = CommonConstants.IconMedia;

                newMediaModel.OriginalFilePath = outFileName;
                newMediaModel.MediaStorageFile = await StoreFolder.FolderGetFileAsync(DataStore.AD.CurrentDataFolder, outFileName).ConfigureAwait(false);

                newMediaModel.HomeImageHLink.HomeImageType = CommonConstants.HomeImageTypeThumbNail;
                newMediaModel.IsClippedFile = true;

                newMediaModel.MetaDataHeight = cropRect.Height;
                newMediaModel.MetaDataWidth  = cropRect.Width;

                DataStore.DS.MediaData.Add(newMediaModel);
                //await StorePostLoad.fixMediaFile(newMediaModel).ConfigureAwait(false);
            }

            resourceBitmap.Dispose();

            return(newMediaModel.HLink);
        }