Exemple #1
0
        /// <summary>
        /// Import the LIFT file into FieldWorks.
        /// </summary>
        /// <returns>the name of the exported LIFT file if successful, or null if an error occurs.</returns>
        /// <remarks>
        /// This method is called in a thread, during the export process.
        /// </remarks>
        private object ImportLiftLexicon(IProgress progressDialog, params object[] parameters)
        {
            var liftPathname = parameters[0].ToString();
            var mergeStyle   = (FlexLiftMerger.MergeStyle)parameters[1];
            // If we use true while importing changes from repo it will fail to copy any pix/aud files that have changed.
            var fTrustModTimes = mergeStyle != FlexLiftMerger.MergeStyle.MsKeepOnlyNew;

            if (_progressDlg == null)
            {
                _progressDlg = progressDialog;
            }
            progressDialog.Minimum  = 0;
            progressDialog.Maximum  = 100;
            progressDialog.Position = 0;
            string sLogFile = null;

            NonUndoableUnitOfWorkHelper.Do(Cache.ActionHandlerAccessor, () =>
            {
                string sFilename;
                var fMigrationNeeded = Migrator.IsMigrationNeeded(liftPathname);
                if (fMigrationNeeded)
                {
                    var sOldVersion        = Palaso.Lift.Validation.Validator.GetLiftVersion(liftPathname);
                    progressDialog.Message = String.Format(ResourceHelper.GetResourceString("kstidLiftVersionMigration"),
                                                           sOldVersion, Palaso.Lift.Validation.Validator.LiftVersion);
                    sFilename = Migrator.MigrateToLatestVersion(liftPathname);
                }
                else
                {
                    sFilename = liftPathname;
                }
                progressDialog.Message      = ResourceHelper.GetResourceString("kstidLoadingListInfo");
                var flexImporter            = new FlexLiftMerger(Cache, mergeStyle, fTrustModTimes);
                var parser                  = new LiftParser <LiftObject, CmLiftEntry, CmLiftSense, CmLiftExample>(flexImporter);
                parser.SetTotalNumberSteps += ParserSetTotalNumberSteps;
                parser.SetStepsCompleted   += ParserSetStepsCompleted;
                parser.SetProgressMessage  += ParserSetProgressMessage;
                flexImporter.LiftFile       = liftPathname;

                flexImporter.LoadLiftRanges(liftPathname + "-ranges");
                var cEntries = parser.ReadLiftFile(sFilename);

                if (fMigrationNeeded)
                {
                    // Try to move the migrated file to the temp directory, even if a copy of it
                    // already exists there.
                    var sTempMigrated = Path.Combine(Path.GetTempPath(),
                                                     Path.ChangeExtension(Path.GetFileName(sFilename), "." + Palaso.Lift.Validation.Validator.LiftVersion + ".lift"));
                    if (File.Exists(sTempMigrated))
                    {
                        File.Delete(sTempMigrated);
                    }
                    File.Move(sFilename, sTempMigrated);
                }
                progressDialog.Message = ResourceHelper.GetResourceString("kstidFixingRelationLinks");
                flexImporter.ProcessPendingRelations(progressDialog);
                sLogFile = flexImporter.DisplayNewListItems(liftPathname, cEntries);
            });
            return(sLogFile);
        }
Exemple #2
0
        private object ImportLift(IAdvInd4 progressDlg, params object[] parameters)
        {
            Debug.Assert(parameters.Length == 1);
            m_progressDlg.Message = LexTextControls.ksLoadingVariousLists;
            m_progressDlg.Value   = 0;
            FlexLiftMerger flexImporter = new FlexLiftMerger(m_cache, FlexLiftMerger.MergeStyle.msKeepNew, true);

            LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> parser =
                new LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample>(flexImporter);
            parser.SetTotalNumberSteps += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .StepsArgs>(parser_SetTotalNumberSteps);
            parser.SetStepsCompleted   += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .ProgressEventArgs>(parser_SetStepsCompleted);
            parser.SetProgressMessage  += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .MessageArgs>(parser_SetProgressMessage);
            flexImporter.LiftFile       = (string)parameters[0];
            int cEntries = parser.ReadLiftFile((string)parameters[0]);

            m_progressDlg.Message = LexTextControls.ksFixingRelationLinks;
            flexImporter.ProcessPendingRelations();
            return(flexImporter.DisplayNewListItems((string)parameters[0], cEntries));
        }
Exemple #3
0
        /// <summary>
        /// Import from a LIFT file.
        /// </summary>
        /// <param name="progressDlg">The progress dialog.</param>
        /// <param name="parameters">The parameters: 1) filename</param>
        /// <returns></returns>
        private object ImportLIFT(IThreadedProgress progressDlg, params object[] parameters)
        {
            m_progressDlg = progressDlg;
            Debug.Assert(parameters.Length == 1);
            string sOrigFile = (string)parameters[0];

            try
            {
                // Create a temporary directory %temp%\TempForLIFTImport. Migrate as necessary and import from this
                // directory. Directory is left after import is done in case it is needed, but will be deleted next time
                // if it exists.
                var sLIFTfolder     = Path.GetDirectoryName(sOrigFile);
                var sLIFTtempFolder = Path.Combine(Path.GetTempPath(), "TempForLIFTImport");
                if (Directory.Exists(sLIFTtempFolder) == true)
                {
                    Directory.Delete(sLIFTtempFolder, true);
                }
                LdmlFileBackup.CopyDirectory(sLIFTfolder, sLIFTtempFolder);
                // Older LIFT files had ldml files in root directory. If found, move them to WritingSystem folder.
                if (Directory.GetFiles(sLIFTtempFolder, "*.ldml").Length > 0)
                {
                    var sWritingSystems = Path.Combine(sLIFTtempFolder, "WritingSystems");
                    if (Directory.Exists(sWritingSystems) == false)
                    {
                        Directory.CreateDirectory(sWritingSystems);
                    }
                    foreach (string filePath in Directory.GetFiles(sLIFTtempFolder, "*.ldml"))
                    {
                        string file = Path.GetFileName(filePath);
                        if (!File.Exists(Path.Combine(sWritingSystems, file)))
                        {
                            File.Move(filePath, Path.Combine(sWritingSystems, file));
                        }
                    }
                }
                var    sTempOrigFile = Path.Combine(sLIFTtempFolder, sOrigFile.Substring(sLIFTfolder.Length + 1));
                string sFilename;
                //Do a LIFT Migration to the current version of LIFT if it is needed.
                bool fMigrationNeeded = Migrator.IsMigrationNeeded(sTempOrigFile);
                if (fMigrationNeeded)
                {
                    string sOldVersion = Validator.GetLiftVersion(sTempOrigFile);
                    m_progressDlg.Message = String.Format(LexTextControls.ksMigratingLiftFile,
                                                          sOldVersion, Validator.LiftVersion);
                    sFilename = Migrator.MigrateToLatestVersion(sTempOrigFile);
                }
                else
                {
                    sFilename = sTempOrigFile;
                }
                //Validate the LIFT file.
                if (!Validate(sFilename, sTempOrigFile))
                {
                    return(null);
                }

                //Import the LIFT file and ranges file.
                m_progressDlg.Message = LexTextControls.ksLoadingVariousLists;
                var flexImporter = new FlexLiftMerger(m_cache, m_msImport, m_chkTrustModTimes.Checked);
                var parser       = new LiftParser <LiftObject, CmLiftEntry, CmLiftSense, CmLiftExample>(flexImporter);
                parser.SetTotalNumberSteps += parser_SetTotalNumberSteps;
                parser.SetStepsCompleted   += parser_SetStepsCompleted;
                parser.SetProgressMessage  += parser_SetProgressMessage;

                flexImporter.LiftFile = sTempOrigFile;

                //Before imporing the LIFT files ensure the LDML (language definition files) have the correct writing system codes.
                flexImporter.LdmlFilesMigration(sLIFTtempFolder, sFilename, sTempOrigFile + "-ranges");
                //Import the Ranges file.
                flexImporter.LoadLiftRanges(sTempOrigFile + "-ranges");                 // temporary (?) fix for FWR-3869.
                //Import the LIFT data file.
                int cEntries = parser.ReadLiftFile(sFilename);

                if (fMigrationNeeded)
                {
                    // Try to move the migrated file to the temp directory, even if a copy of it
                    // already exists there.
                    string sTempMigrated = Path.Combine(Path.GetTempPath(),
                                                        Path.ChangeExtension(Path.GetFileName(sFilename), "." + Validator.LiftVersion + FwFileExtensions.ksLexiconInterchangeFormat));
                    if (File.Exists(sTempMigrated))
                    {
                        File.Delete(sTempMigrated);
                    }
                    File.Move(sFilename, sTempMigrated);
                }
                flexImporter.ProcessPendingRelations(m_progressDlg);
                return(flexImporter.DisplayNewListItems(sOrigFile, cEntries));
            }
            catch (Exception error)
            {
                string sMsg = String.Format(LexTextControls.ksLIFTImportProblem,
                                            sOrigFile, error.Message);
                try
                {
                    StringBuilder bldr = new StringBuilder();
                    // leave in English for programmer's sake...
                    bldr.AppendFormat("Something went wrong while FieldWorks was attempting to import {0}.",
                                      sOrigFile);
                    bldr.AppendLine();
                    bldr.AppendLine(error.Message);
                    bldr.AppendLine();
                    bldr.AppendLine(error.StackTrace);

                    if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
                    {
                        ClipboardUtils.SetDataObject(bldr.ToString(), true);
                    }
                    else
                    {
                        progressDlg.SynchronizeInvoke.Invoke(() => ClipboardUtils.SetDataObject(bldr.ToString(), true));
                    }
                    SIL.Utils.Logger.WriteEvent(bldr.ToString());
                }
                catch
                {
                }
                MessageBox.Show(sMsg, LexTextControls.ksProblemImporting,
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(null);
            }
        }
Exemple #4
0
		/// <summary>
		/// Import from a LIFT file.
		/// </summary>
		/// <param name="progressDlg">The progress dialog.</param>
		/// <param name="parameters">The parameters: 1) filename</param>
		/// <returns></returns>
		private object ImportLIFT(IThreadedProgress progressDlg, params object[] parameters)
		{
			m_progressDlg = progressDlg;
			Debug.Assert(parameters.Length == 1);
			string sOrigFile = (string)parameters[0];
			try
			{
				// Create a temporary directory %temp%\TempForLIFTImport. Migrate as necessary and import from this
				// directory. Directory is left after import is done in case it is needed, but will be deleted next time
				// if it exists.
				var sLIFTfolder = Path.GetDirectoryName(sOrigFile);
				var sLIFTtempFolder = Path.Combine(Path.GetTempPath(), "TempForLIFTImport");
				if (Directory.Exists(sLIFTtempFolder) == true)
					Directory.Delete(sLIFTtempFolder, true);
				LdmlFileBackup.CopyDirectory(sLIFTfolder, sLIFTtempFolder);
				// Older LIFT files had ldml files in root directory. If found, move them to WritingSystem folder.
				if (Directory.GetFiles(sLIFTtempFolder, "*.ldml").Length > 0)
				{
					var sWritingSystems = Path.Combine(sLIFTtempFolder, "WritingSystems");
					if (Directory.Exists(sWritingSystems) == false)
						Directory.CreateDirectory(sWritingSystems);
					foreach (string filePath in Directory.GetFiles(sLIFTtempFolder, "*.ldml"))
					{
						string file = Path.GetFileName(filePath);
						if (!File.Exists(Path.Combine(sWritingSystems, file)))
							File.Move(filePath, Path.Combine(sWritingSystems, file));
					}
				}
				var sTempOrigFile = Path.Combine(sLIFTtempFolder, sOrigFile.Substring(sLIFTfolder.Length + 1));
				string sFilename;
				//Do a LIFT Migration to the current version of LIFT if it is needed.
				bool fMigrationNeeded = Migrator.IsMigrationNeeded(sTempOrigFile);
				if (fMigrationNeeded)
				{
					string sOldVersion = Validator.GetLiftVersion(sTempOrigFile);
					m_progressDlg.Message = String.Format(LexTextControls.ksMigratingLiftFile,
						sOldVersion, Validator.LiftVersion);
					sFilename = Migrator.MigrateToLatestVersion(sTempOrigFile);
				}
				else
				{
					sFilename = sTempOrigFile;
				}
				//Validate the LIFT file.
				if (!Validate(sFilename, sTempOrigFile))
					return null;

				//Import the LIFT file and ranges file.
				m_progressDlg.Message = LexTextControls.ksLoadingVariousLists;
				var flexImporter = new FlexLiftMerger(m_cache, m_msImport, m_chkTrustModTimes.Checked);
				var parser = new LiftParser<LiftObject, CmLiftEntry, CmLiftSense, CmLiftExample>(flexImporter);
				parser.SetTotalNumberSteps += parser_SetTotalNumberSteps;
				parser.SetStepsCompleted += parser_SetStepsCompleted;
				parser.SetProgressMessage += parser_SetProgressMessage;

				flexImporter.LiftFile = sTempOrigFile;

				//Before imporing the LIFT files ensure the LDML (language definition files) have the correct writing system codes.
				flexImporter.LdmlFilesMigration(sLIFTtempFolder, sFilename, sTempOrigFile + "-ranges");
				//Import the Ranges file.
				flexImporter.LoadLiftRanges(sTempOrigFile + "-ranges");	// temporary (?) fix for FWR-3869.
				//Import the LIFT data file.
				int cEntries = parser.ReadLiftFile(sFilename);

				if (fMigrationNeeded)
				{
					// Try to move the migrated file to the temp directory, even if a copy of it
					// already exists there.
					string sTempMigrated = Path.Combine(Path.GetTempPath(),
						Path.ChangeExtension(Path.GetFileName(sFilename), "." + Validator.LiftVersion + FwFileExtensions.ksLexiconInterchangeFormat));
					if (File.Exists(sTempMigrated))
						File.Delete(sTempMigrated);
					File.Move(sFilename, sTempMigrated);
				}
				flexImporter.ProcessPendingRelations(m_progressDlg);
				return flexImporter.DisplayNewListItems(sOrigFile, cEntries);
			}
			catch (Exception error)
			{
				string sMsg = String.Format(LexTextControls.ksLIFTImportProblem,
					sOrigFile, error.Message);
				try
				{
					StringBuilder bldr = new StringBuilder();
					// leave in English for programmer's sake...
					bldr.AppendFormat("Something went wrong while FieldWorks was attempting to import {0}.",
						sOrigFile);
					bldr.AppendLine();
					bldr.AppendLine(error.Message);
					bldr.AppendLine();
					bldr.AppendLine(error.StackTrace);

					if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
						ClipboardUtils.SetDataObject(bldr.ToString(), true);
					else
						progressDlg.SynchronizeInvoke.Invoke(() => ClipboardUtils.SetDataObject(bldr.ToString(), true));
						SIL.Utils.Logger.WriteEvent(bldr.ToString());
				}
				catch
				{
				}
				MessageBox.Show(sMsg, LexTextControls.ksProblemImporting,
					MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return null;
			}
		}
Exemple #5
0
		/// <summary>
		/// Import the modified LIFT file given by the first (and only) parameter.
		/// </summary>
		/// <returns>the name of the log file for the import, or null if a major error occurs.</returns>
		protected object ImportLexicon(IAdvInd4 progressDialog, params object[] parameters)
		{
			if (m_progressDlg == null)
				m_progressDlg = progressDialog;
			progressDialog.SetRange(0, 100);
			progressDialog.Position = 0;
			string inPath = parameters[0].ToString();
			string sLogFile = null;
			PropChangedHandling oldPropChg = m_cache.PropChangedHandling;
			try
			{
				m_cache.PropChangedHandling = PropChangedHandling.SuppressAll;
				string sFilename;
				bool fMigrationNeeded = LiftIO.Migration.Migrator.IsMigrationNeeded(inPath);
				if (fMigrationNeeded)
				{
					string sOldVersion = LiftIO.Validation.Validator.GetLiftVersion(inPath);
					progressDialog.Message = String.Format("Migrating from LIFT version {0} to version {1}",
						sOldVersion, LiftIO.Validation.Validator.LiftVersion);
					sFilename = LiftIO.Migration.Migrator.MigrateToLatestVersion(inPath);
				}
				else
				{
					sFilename = inPath;
				}
				// TODO: validate input file?
				progressDialog.Message = "Loading various lists for lookup during import";
				FlexLiftMerger flexImporter = new FlexLiftMerger(m_cache, FlexLiftMerger.MergeStyle.msKeepOnlyNew, true);
				LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample> parser =
					new LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>(flexImporter);
				parser.SetTotalNumberSteps += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.StepsArgs>(parser_SetTotalNumberSteps);
				parser.SetStepsCompleted += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject,LiftEntry,LiftSense,LiftExample>.ProgressEventArgs>(parser_SetStepsCompleted);
				parser.SetProgressMessage += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.MessageArgs>(parser_SetProgressMessage);
				flexImporter.LiftFile = inPath;

				int cEntries = parser.ReadLiftFile(sFilename);

				if (fMigrationNeeded)
				{
					// Try to move the migrated file to the temp directory, even if a copy of it
					// already exists there.
					string sTempMigrated = Path.Combine(Path.GetTempPath(),
						Path.ChangeExtension(Path.GetFileName(sFilename), "." + LiftIO.Validation.Validator.LiftVersion + ".lift"));
					if (File.Exists(sTempMigrated))
						File.Delete(sTempMigrated);
					File.Move(sFilename, sTempMigrated);
				}
				progressDialog.Message = "Fixing relation links between imported entries";
				flexImporter.ProcessPendingRelations();
				sLogFile = flexImporter.DisplayNewListItems(inPath, cEntries);
			}
			catch (Exception error)
			{
				string sMsg = String.Format("Something went wrong trying to import {0} while merging...",
					inPath, error.Message);
				try
				{
					StringBuilder bldr = new StringBuilder();
					// leave in English for programmer's sake...
					bldr.AppendFormat("Something went wrong while FieldWorks was attempting to import {0}.",
						inPath);
					bldr.AppendLine();
					bldr.AppendLine(error.Message);
					bldr.AppendLine();
					bldr.AppendLine(error.StackTrace);
					if (System.Threading.Thread.CurrentThread.GetApartmentState() == System.Threading.ApartmentState.STA)
						Clipboard.SetDataObject(bldr.ToString(), true);
				}
				catch
				{
				}
				MessageBox.Show(sMsg, "Problem Merging",
					MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			finally
			{
				m_cache.PropChangedHandling = oldPropChg;
			}
			return sLogFile;
		}
		/// <summary>
		/// Import the LIFT file into FieldWorks.
		/// </summary>
		/// <returns>the name of the exported LIFT file if successful, or null if an error occurs.</returns>
		/// <remarks>
		/// This method is called in a thread, during the export process.
		/// </remarks>
		private object ImportLiftLexicon(IProgress progressDialog, params object[] parameters)
		{
			var liftPathname = parameters[0].ToString();
			var mergeStyle = (FlexLiftMerger.MergeStyle)parameters[1];
			// If we use true while importing changes from repo it will fail to copy any pix/aud files that have changed.
			var fTrustModTimes = mergeStyle != FlexLiftMerger.MergeStyle.MsKeepOnlyNew;
			if (_progressDlg == null)
				_progressDlg = progressDialog;
			progressDialog.Minimum = 0;
			progressDialog.Maximum = 100;
			progressDialog.Position = 0;
			string sLogFile = null;

			if (File.Exists(LiftNotesPath))
			{

				using (var reader = new StreamReader(LiftNotesPath, Encoding.UTF8))
				using (var writer = new StreamWriter(FlexNotesPath, false, Encoding.UTF8))
				{
					ConvertLiftNotesToFlex(reader, writer, Path.GetFileName(_liftPathname));
				}
			}

			NonUndoableUnitOfWorkHelper.Do(Cache.ActionHandlerAccessor, () =>
			{
				string sFilename;
				var fMigrationNeeded = Migrator.IsMigrationNeeded(liftPathname);
				if (fMigrationNeeded)
				{
					var sOldVersion = Palaso.Lift.Validation.Validator.GetLiftVersion(liftPathname);
					progressDialog.Message = String.Format(ResourceHelper.GetResourceString("kstidLiftVersionMigration"),
						sOldVersion, Palaso.Lift.Validation.Validator.LiftVersion);
					sFilename = Migrator.MigrateToLatestVersion(liftPathname);
				}
				else
				{
					sFilename = liftPathname;
				}
				progressDialog.Message = ResourceHelper.GetResourceString("kstidLoadingListInfo");
				var flexImporter = new FlexLiftMerger(Cache, mergeStyle, fTrustModTimes);
				var parser = new LiftParser<LiftObject, CmLiftEntry, CmLiftSense, CmLiftExample>(flexImporter);
				parser.SetTotalNumberSteps += ParserSetTotalNumberSteps;
				parser.SetStepsCompleted += ParserSetStepsCompleted;
				parser.SetProgressMessage += ParserSetProgressMessage;
				flexImporter.LiftFile = liftPathname;

				flexImporter.LoadLiftRanges(liftPathname + "-ranges");
				var cEntries = parser.ReadLiftFile(sFilename);

				if (fMigrationNeeded)
				{
					// Try to move the migrated file to the temp directory, even if a copy of it
					// already exists there.
					var sTempMigrated = Path.Combine(Path.GetTempPath(),
													 Path.ChangeExtension(Path.GetFileName(sFilename), "." + Palaso.Lift.Validation.Validator.LiftVersion + ".lift"));
					if (File.Exists(sTempMigrated))
						File.Delete(sTempMigrated);
					File.Move(sFilename, sTempMigrated);
				}
				progressDialog.Message = ResourceHelper.GetResourceString("kstidFixingRelationLinks");
				flexImporter.ProcessPendingRelations(progressDialog);
				sLogFile = flexImporter.DisplayNewListItems(liftPathname, cEntries);
			});
			return sLogFile;
		}
Exemple #7
0
		/// <summary>
		/// Import from a LIFT file.
		/// </summary>
		/// <param name="progressDlg">The progress dialog.</param>
		/// <param name="parameters">The parameters: 1) filename</param>
		/// <returns></returns>
		private object ImportLIFT(IAdvInd4 progressDlg, params object[] parameters)
		{
			m_progressDlg = progressDlg;
			Debug.Assert(parameters.Length == 1);
			string sOrigFile = (string)parameters[0];
			string sLogFile = null;
			PropChangedHandling oldPropChg = m_cache.PropChangedHandling;
			try
			{
				m_cache.PropChangedHandling = PropChangedHandling.SuppressAll;
				string sFilename;
				bool fMigrationNeeded = LiftIO.Migration.Migrator.IsMigrationNeeded(sOrigFile);
				if (fMigrationNeeded)
				{
					string sOldVersion = LiftIO.Validation.Validator.GetLiftVersion(sOrigFile);
					m_progressDlg.Message = String.Format(LexTextControls.ksMigratingLiftFile,
						sOldVersion, LiftIO.Validation.Validator.LiftVersion);
					sFilename = LiftIO.Migration.Migrator.MigrateToLatestVersion(sOrigFile);
				}
				else
				{
					sFilename = sOrigFile;
				}
				if (!Validate(sFilename, sOrigFile))
					return sLogFile;
				m_progressDlg.Message = LexTextControls.ksLoadingVariousLists;
				FlexLiftMerger flexImporter = new FlexLiftMerger(m_cache, m_msImport, m_chkTrustModTimes.Checked);
				LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample> parser =
					new LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>(flexImporter);
				parser.SetTotalNumberSteps += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.StepsArgs>(parser_SetTotalNumberSteps);
				parser.SetStepsCompleted += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.ProgressEventArgs>(parser_SetStepsCompleted);
				parser.SetProgressMessage += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.MessageArgs>(parser_SetProgressMessage);
				flexImporter.LiftFile = sOrigFile;

				int cEntries = parser.ReadLiftFile(sFilename);

				if (fMigrationNeeded)
				{
					// Try to move the migrated file to the temp directory, even if a copy of it
					// already exists there.
					string sTempMigrated = Path.Combine(Path.GetTempPath(),
						Path.ChangeExtension(Path.GetFileName(sFilename), "." + LiftIO.Validation.Validator.LiftVersion + ".lift"));
					if (File.Exists(sTempMigrated))
						File.Delete(sTempMigrated);
					File.Move(sFilename, sTempMigrated);
				}
				m_progressDlg.Message = LexTextControls.ksFixingRelationLinks;
				flexImporter.ProcessPendingRelations();
				sLogFile = flexImporter.DisplayNewListItems(sOrigFile, cEntries);
			}
			catch (Exception error)
			{
				string sMsg = String.Format(LexTextControls.ksLIFTImportProblem,
					sOrigFile, error.Message);
				try
				{
					StringBuilder bldr = new StringBuilder();
					// leave in English for programmer's sake...
					bldr.AppendFormat("Something went wrong while FieldWorks was attempting to import {0}.",
						sOrigFile);
					bldr.AppendLine();
					bldr.AppendLine(error.Message);
					bldr.AppendLine();
					bldr.AppendLine(error.StackTrace);

					if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
						Clipboard.SetDataObject(bldr.ToString(), true);
					else
						SIL.Utils.Logger.WriteEvent(bldr.ToString());
				}
				catch
				{
				}
				MessageBox.Show(sMsg, LexTextControls.ksProblemImporting,
					MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
			finally
			{
				m_cache.PropChangedHandling = oldPropChg;
			}
			return sLogFile;
		}
Exemple #8
0
        /// <summary>
        /// Import the modified LIFT file given by the first (and only) parameter.
        /// </summary>
        /// <returns>the name of the log file for the import, or null if a major error occurs.</returns>
        protected object ImportLexicon(IAdvInd4 progressDialog, params object[] parameters)
        {
            if (m_progressDlg == null)
            {
                m_progressDlg = progressDialog;
            }
            progressDialog.SetRange(0, 100);
            progressDialog.Position = 0;
            string inPath   = parameters[0].ToString();
            string sLogFile = null;
            PropChangedHandling oldPropChg = m_cache.PropChangedHandling;

            try
            {
                m_cache.PropChangedHandling = PropChangedHandling.SuppressAll;
                string sFilename;
                bool   fMigrationNeeded = LiftIO.Migration.Migrator.IsMigrationNeeded(inPath);
                if (fMigrationNeeded)
                {
                    string sOldVersion = LiftIO.Validation.Validator.GetLiftVersion(inPath);
                    progressDialog.Message = String.Format("Migrating from LIFT version {0} to version {1}",
                                                           sOldVersion, LiftIO.Validation.Validator.LiftVersion);
                    sFilename = LiftIO.Migration.Migrator.MigrateToLatestVersion(inPath);
                }
                else
                {
                    sFilename = inPath;
                }
                // TODO: validate input file?
                progressDialog.Message = "Loading various lists for lookup during import";
                FlexLiftMerger flexImporter = new FlexLiftMerger(m_cache, FlexLiftMerger.MergeStyle.msKeepOnlyNew, true);
                LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> parser =
                    new LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample>(flexImporter);
                parser.SetTotalNumberSteps += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .StepsArgs>(parser_SetTotalNumberSteps);
                parser.SetStepsCompleted   += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .ProgressEventArgs>(parser_SetStepsCompleted);
                parser.SetProgressMessage  += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .MessageArgs>(parser_SetProgressMessage);
                flexImporter.LiftFile       = inPath;

                int cEntries = parser.ReadLiftFile(sFilename);

                if (fMigrationNeeded)
                {
                    // Try to move the migrated file to the temp directory, even if a copy of it
                    // already exists there.
                    string sTempMigrated = Path.Combine(Path.GetTempPath(),
                                                        Path.ChangeExtension(Path.GetFileName(sFilename), "." + LiftIO.Validation.Validator.LiftVersion + ".lift"));
                    if (File.Exists(sTempMigrated))
                    {
                        File.Delete(sTempMigrated);
                    }
                    File.Move(sFilename, sTempMigrated);
                }
                progressDialog.Message = "Fixing relation links between imported entries";
                flexImporter.ProcessPendingRelations();
                sLogFile = flexImporter.DisplayNewListItems(inPath, cEntries);
            }
            catch (Exception error)
            {
                string sMsg = String.Format("Something went wrong trying to import {0} while merging...",
                                            inPath, error.Message);
                try
                {
                    StringBuilder bldr = new StringBuilder();
                    // leave in English for programmer's sake...
                    bldr.AppendFormat("Something went wrong while FieldWorks was attempting to import {0}.",
                                      inPath);
                    bldr.AppendLine();
                    bldr.AppendLine(error.Message);
                    bldr.AppendLine();
                    bldr.AppendLine(error.StackTrace);
                    if (System.Threading.Thread.CurrentThread.GetApartmentState() == System.Threading.ApartmentState.STA)
                    {
                        Clipboard.SetDataObject(bldr.ToString(), true);
                    }
                }
                catch
                {
                }
                MessageBox.Show(sMsg, "Problem Merging",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                m_cache.PropChangedHandling = oldPropChg;
            }
            return(sLogFile);
        }
		private string TryImport(string sOrigFile, string sOrigRangesFile, FlexLiftMerger.MergeStyle mergeStyle, int expectedCount, bool trustModificationTimes = true)
		{
			string logfile = null;

			IProgress progressDlg = new DummyProgressDlg();
			var fMigrationNeeded = Migrator.IsMigrationNeeded(sOrigFile);
			var sFilename = fMigrationNeeded
								? Migrator.MigrateToLatestVersion(sOrigFile)
								: sOrigFile;
			var flexImporter = new FlexLiftMerger(Cache, mergeStyle, trustModificationTimes);
			var parser = new LiftParser<LiftObject, CmLiftEntry, CmLiftSense, CmLiftExample>(flexImporter);
			flexImporter.LiftFile = sOrigFile;

			//The following are the calls to import the Ranges file and then the Data file.
			if (!String.IsNullOrEmpty(sOrigRangesFile))
				flexImporter.LoadLiftRanges(sOrigRangesFile);
			var cEntries = parser.ReadLiftFile(sFilename);

			Assert.AreEqual(expectedCount, cEntries);
			if (fMigrationNeeded)
				File.Delete(sFilename);
			flexImporter.ProcessPendingRelations(progressDlg);
			logfile = flexImporter.DisplayNewListItems(sOrigFile, cEntries);

			return logfile;
		}
		private object ImportLift(IAdvInd4 progressDlg, params object[] parameters)
		{
			Debug.Assert(parameters.Length == 1);
			m_progressDlg.Message = LexTextControls.ksLoadingVariousLists;
			m_progressDlg.Value = 0;
			FlexLiftMerger flexImporter = new FlexLiftMerger(m_cache, FlexLiftMerger.MergeStyle.msKeepNew, true);
			LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample> parser =
				new LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>(flexImporter);
			parser.SetTotalNumberSteps += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.StepsArgs>(parser_SetTotalNumberSteps);
			parser.SetStepsCompleted += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.ProgressEventArgs>(parser_SetStepsCompleted);
			parser.SetProgressMessage += new EventHandler<LiftIO.Parsing.LiftParser<LiftObject, LiftEntry, LiftSense, LiftExample>.MessageArgs>(parser_SetProgressMessage);
			flexImporter.LiftFile = (string)parameters[0];
			int cEntries = parser.ReadLiftFile((string)parameters[0]);
			m_progressDlg.Message = LexTextControls.ksFixingRelationLinks;
			flexImporter.ProcessPendingRelations();
			return flexImporter.DisplayNewListItems((string)parameters[0], cEntries);
		}
Exemple #11
0
        /// <summary>
        /// Import from a LIFT file.
        /// </summary>
        /// <param name="progressDlg">The progress dialog.</param>
        /// <param name="parameters">The parameters: 1) filename</param>
        /// <returns></returns>
        private object ImportLIFT(IAdvInd4 progressDlg, params object[] parameters)
        {
            m_progressDlg = progressDlg;
            Debug.Assert(parameters.Length == 1);
            string sOrigFile = (string)parameters[0];
            string sLogFile  = null;
            PropChangedHandling oldPropChg = m_cache.PropChangedHandling;

            try
            {
                m_cache.PropChangedHandling = PropChangedHandling.SuppressAll;
                string sFilename;
                bool   fMigrationNeeded = LiftIO.Migration.Migrator.IsMigrationNeeded(sOrigFile);
                if (fMigrationNeeded)
                {
                    string sOldVersion = LiftIO.Validation.Validator.GetLiftVersion(sOrigFile);
                    m_progressDlg.Message = String.Format(LexTextControls.ksMigratingLiftFile,
                                                          sOldVersion, LiftIO.Validation.Validator.LiftVersion);
                    sFilename = LiftIO.Migration.Migrator.MigrateToLatestVersion(sOrigFile);
                }
                else
                {
                    sFilename = sOrigFile;
                }
                if (!Validate(sFilename, sOrigFile))
                {
                    return(sLogFile);
                }
                m_progressDlg.Message = LexTextControls.ksLoadingVariousLists;
                FlexLiftMerger flexImporter = new FlexLiftMerger(m_cache, m_msImport, m_chkTrustModTimes.Checked);
                LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> parser =
                    new LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample>(flexImporter);
                parser.SetTotalNumberSteps += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .StepsArgs>(parser_SetTotalNumberSteps);
                parser.SetStepsCompleted   += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .ProgressEventArgs>(parser_SetStepsCompleted);
                parser.SetProgressMessage  += new EventHandler <LiftIO.Parsing.LiftParser <LiftObject, LiftEntry, LiftSense, LiftExample> .MessageArgs>(parser_SetProgressMessage);
                flexImporter.LiftFile       = sOrigFile;

                int cEntries = parser.ReadLiftFile(sFilename);

                if (fMigrationNeeded)
                {
                    // Try to move the migrated file to the temp directory, even if a copy of it
                    // already exists there.
                    string sTempMigrated = Path.Combine(Path.GetTempPath(),
                                                        Path.ChangeExtension(Path.GetFileName(sFilename), "." + LiftIO.Validation.Validator.LiftVersion + ".lift"));
                    if (File.Exists(sTempMigrated))
                    {
                        File.Delete(sTempMigrated);
                    }
                    File.Move(sFilename, sTempMigrated);
                }
                m_progressDlg.Message = LexTextControls.ksFixingRelationLinks;
                flexImporter.ProcessPendingRelations();
                sLogFile = flexImporter.DisplayNewListItems(sOrigFile, cEntries);
            }
            catch (Exception error)
            {
                string sMsg = String.Format(LexTextControls.ksLIFTImportProblem,
                                            sOrigFile, error.Message);
                try
                {
                    StringBuilder bldr = new StringBuilder();
                    // leave in English for programmer's sake...
                    bldr.AppendFormat("Something went wrong while FieldWorks was attempting to import {0}.",
                                      sOrigFile);
                    bldr.AppendLine();
                    bldr.AppendLine(error.Message);
                    bldr.AppendLine();
                    bldr.AppendLine(error.StackTrace);

                    if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
                    {
                        Clipboard.SetDataObject(bldr.ToString(), true);
                    }
                    else
                    {
                        SIL.Utils.Logger.WriteEvent(bldr.ToString());
                    }
                }
                catch
                {
                }
                MessageBox.Show(sMsg, LexTextControls.ksProblemImporting,
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                m_cache.PropChangedHandling = oldPropChg;
            }
            return(sLogFile);
        }