Пример #1
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Update the properties of a CmPicture with the given file, caption, and folder.
        /// </summary>
        /// <param name="srcFilename">The full path to the original filename (an internal copy
        /// will be made in this method)</param>
        /// <param name="captionTss">The caption</param>
        /// <param name="sFolder">The name of the CmFolder where picture should be stored</param>
        /// <param name="ws">The WS for the location in the caption MultiUnicode to put the
        /// caption</param>
        /// ------------------------------------------------------------------------------------
        public void UpdatePicture(string srcFilename, ITsString captionTss, string sFolder, int ws)
        {
            // Locate CmFolder with given name or create it, if neccessary
            ICmFolder folder = CmFolder.FindOrCreateFolder(m_cache, (int)LangProject.LangProjectTags.kflidPictures, sFolder);
            ICmFile   file   = PictureFileRA;

            if (file == null)
            {
                file          = folder.FilesOC.Add(new CmFile());
                PictureFileRA = file;
            }
            // (The case-independent comparison is valid only for Microsoft Windows.)
            string sInternalAbsPath = file.AbsoluteInternalPath;

            if (srcFilename != null &&
                !srcFilename.Equals(sInternalAbsPath, StringComparison.InvariantCultureIgnoreCase))
            {
                ((CmFile)file).SetInternalPath(srcFilename);
            }
            Caption.SetAlternative(captionTss, ws);
            m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, Hvo,
                                (int)CmPicture.CmPictureTags.kflidCaption, ws, 0, 0);
            m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, file.Hvo,
                                (int)CmFile.CmFileTags.kflidInternalPath, 0, 1, 1);
        }
Пример #2
0
        ///// ------------------------------------------------------------------------------------
        ///// <summary>
        ///// Append a picture to the end of the paragraph using the given writing system.
        ///// </summary>
        ///// <param name="ws">given writing system</param>
        ///// <param name="strBldr">The string builder for the paragraph being composed</param>
        ///// ------------------------------------------------------------------------------------
        //public void AppendPicture(int ws, ITsStrBldr strBldr)
        //{
        //    // Make a TsTextProps with the relevant object data and the same ws as its
        //    // context.
        //    byte[] objData = MiscUtils.GetObjData(this.Guid,
        //        (byte)FwObjDataTypes.kodtGuidMoveableObjDisp);
        //    ITsPropsBldr propsBldr = TsStringUtils.MakePropsBldr();
        //    propsBldr.SetStrPropValueRgch((int)FwTextPropType.ktptObjData,
        //        objData, objData.Length);
        //    propsBldr.SetIntPropValues((int)FwTextPropType.ktptWs, 0, ws);

        //    // Insert the orc with the resulting properties.
        //    strBldr.Replace(strBldr.Length, strBldr.Length,
        //        new string(TsStringUtils.kChObject, 1), propsBldr.GetTextProps());
        //}

        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Update the properties of a CmPicture with the given file, caption, and folder.
        /// </summary>
        /// <param name="srcFilename">The full path to the filename (this might be an "internal"
        /// copy of the original the user chose)</param>
        /// <param name="captionTss">The caption</param>
        /// <param name="sFolder">The name of the CmFolder where picture should be stored</param>
        /// <param name="ws">The WS for the location in the caption MultiUnicode to put the
        /// caption</param>
        /// ------------------------------------------------------------------------------------
        public void UpdatePicture(string srcFilename, ITsString captionTss, string sFolder, int ws)
        {
            // Set the caption first since creating the CmFile will throw if srcFilename is empty.
            if (ws != 0)
            {
                Caption.set_String(ws, captionTss);
            }

            ICmFile file = PictureFileRA;

            if (file == null)
            {
                ICmFolder folder = DomainObjectServices.FindOrCreateFolder(m_cache, LangProjectTags.kflidPictures, sFolder);
                PictureFileRA = DomainObjectServices.FindOrCreateFile(folder, srcFilename);
            }
            else
            {
                Debug.Assert(sFolder == CmFolderTags.LocalPictures,
                             "TODO: If we ever actually support use of different folders, we need to handle folder changes.");
                if (srcFilename != null && !FileUtils.PathsAreEqual(srcFilename, file.AbsoluteInternalPath))
                {
                    file.InternalPath = srcFilename;
                }
            }
            // We shouldn't need to this in the new LCM.
            //m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, Hvo,
            //    (int)CmPicture.CmPictureTags.kflidCaption, ws, 0 , 0);
            //m_cache.PropChanged(null, PropChangeType.kpctNotifyAll, file.Hvo,
            //    (int)CmFile.CmFileTags.kflidInternalPath, 0, 1, 1);
        }
Пример #3
0
        public void CmFileFinder_OrigFilesMatch()
        {
            // Setup
            ICmFolder folder = DomainObjectServices.FindOrCreateFolder(Cache,
                                                                       LangProjectTags.kflidPictures, CmFolderTags.LocalPictures);

            ICmFile file = DomainObjectServices.FindOrCreateFile(folder, m_internalPath);

            Assert.AreEqual(m_pict.PictureFileRA, file);
        }
Пример #4
0
        public void CmFileFinder_OrigFileMissing()
        {
            // Setup
            ICmFolder folder = DomainObjectServices.FindOrCreateFolder(Cache,
                                                                       LangProjectTags.kflidPictures, CmFolderTags.LocalPictures);

            FileUtils.Delete(m_internalPath);
            Assert.IsFalse(FileUtils.IsFileReadable(m_internalPath), "Test cannot proceed. Unable to delete Original file.");

            // Test
            Assert.DoesNotThrow(() => DomainObjectServices.FindOrCreateFile(folder, m_internalPath));
        }
Пример #5
0
 private void GetCmFilePathsInCmFolder(ICmFolder cmfolder, HashSet <string> filePathsInCmFiles)
 {
     if (cmfolder == null)
     {
         return;
     }
     foreach (var file in cmfolder.FilesOC)
     {
         string sFilepath = file.InternalPath;
         if (!Path.IsPathRooted(sFilepath))
         {
             filePathsInCmFiles.Add(file.AbsoluteInternalPath);
         }
     }
 }
Пример #6
0
        private void SetupCacheToTestAgainst()
        {
            var lp = Cache.LangProject;

            lp.LinkedFilesRootDir = m_linkedFilesRootDir;
            ICmFolder picturesfolder  = DomainObjectServices.FindOrCreateFolder(Cache, LangProjectTags.kflidPictures, CmFolderTags.LocalPictures);
            ICmFolder mediafolder     = DomainObjectServices.FindOrCreateFolder(Cache, LangProjectTags.kflidMedia, CmFolderTags.LocalMedia);
            ICmFolder tsStringsfolder = Cache.ServiceLocator.GetInstance <ICmFolderFactory>().Create();

            lp.FilePathsInTsStringsOA = tsStringsfolder;
            tsStringsfolder.Name.AnalysisDefaultWritingSystem = Cache.TsStrFactory.MakeString(CmFolderTags.LocalFilePathsInTsStrings,
                                                                                              WritingSystemServices.FallbackUserWs(Cache));

            AddCmFileToCmFolder(picturesfolder, Path.Combine("Pictures", "JudeAndMeWithBeard.jpg"));
            AddCmFileToCmFolder(tsStringsfolder, Path.Combine("Others", "Chic Skype.png"));
            AddCmFileToCmFolder(mediafolder, Path.Combine("AudioVisual", "Untitled0.WMV"));
        }
Пример #7
0
        public void CmFileFinder_OrigFilesMatch()
        {
            CheckDisposed();

            // Setup
            ICmFolder folder = CmFolder.FindOrCreateFolder(Cache, (int)LangProject.LangProjectTags.kflidPictures,
                                                           StringUtils.LocalPictures);

            using (DummyFileMaker maker = new DummyFileMaker("garbage.jpg", true))
            {
                ICmFile fileOrig = new CmFile();
                folder.FilesOC.Add(fileOrig);
                fileOrig.InternalPath = maker.Filename;

                ICmFile file = CmFile.FindOrCreateFile(folder, maker.Filename);
                Assert.AreEqual(fileOrig, file);
            }
        }
Пример #8
0
        public void CmFileFinder_NoPreExistingCmFile()
        {
            CheckDisposed();

            // Setup
            ICmFolder folder = CmFolder.FindOrCreateFolder(Cache, (int)LangProject.LangProjectTags.kflidPictures,
                                                           StringUtils.LocalPictures);

            using (DummyFileMaker maker = new DummyFileMaker("junk56.jpg", true))
            {
                ICmFile file = CmFile.FindOrCreateFile(folder, maker.Filename);
                Assert.IsNotNull(file, "null CmFile returned");
                Assert.IsNotNull(file.InternalPath, "Internal path not set correctly");
                Assert.IsTrue(file.AbsoluteInternalPath == file.InternalPath, "Files outside LangProject.ExtLinkRootDir are stored as absolute paths");
                m_internalFilesToDelete.Add(file.AbsoluteInternalPath);
                Assert.IsTrue(m_pict.PictureFileRAHvo != file.Hvo);
            }
        }
Пример #9
0
        public void CmFileFinder_NoPreExistingCmFile()
        {
            // Setup
            string sNewFile = Path.DirectorySeparatorChar + Path.GetRandomFileName();

            m_fileOs.AddFile(sNewFile, "456", Encoding.Default);

            ICmFolder folder = DomainObjectServices.FindOrCreateFolder(Cache,
                                                                       LangProjectTags.kflidPictures, CmFolderTags.LocalPictures);

            // Test
            ICmFile file = DomainObjectServices.FindOrCreateFile(folder, sNewFile);

            Assert.IsNotNull(file, "null CmFile returned");
            Assert.AreEqual(sNewFile, file.InternalPath, "Internal path not set correctly");
            Assert.AreEqual(sNewFile, file.AbsoluteInternalPath, "Files outside LangProject.LinkedFilesRootDir are stored as absolute paths");
            Assert.AreNotEqual(m_pict.PictureFileRA, file);
            FileUtils.Delete(sNewFile);
        }
Пример #10
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Create a picture (no caption set).
        /// </summary>
        /// <param name="para">Paragraph to insert picture into</param>
        /// <param name="ichPos">The 0-based character offset into the paragraph</param>
        /// <returns></returns>
        /// ------------------------------------------------------------------------------------
        protected ICmPicture InsertTestPicture(StTxtPara para, int ichPos)
        {
            // Create the picture
            ICmFolder folder = m_fdoCache.LangProject.PicturesOC.Add(new CmFolder());
            ICmFile   file   = folder.FilesOC.Add(new CmFile());

            file.InternalPath = "there";
            int        newHvo  = m_fdoCache.CreateObject(CmPicture.kClassId);
            ICmPicture picture = new CmPicture(m_fdoCache, newHvo);

            picture.PictureFileRA = file;

            // Update the paragraph contents to include the footnote marker
            ITsStrBldr tsStrBldr = para.Contents.UnderlyingTsString.GetBldr();

            (picture as CmPicture).InsertOwningORCIntoPara(tsStrBldr, ichPos, 0);             // Don't care about ws
            para.Contents.UnderlyingTsString = tsStrBldr.GetString();

            return(picture);
        }
Пример #11
0
        public void CmFileFinder_OrigFileMissing()
        {
            CheckDisposed();

            // Setup
            ICmFolder folder = CmFolder.FindOrCreateFolder(Cache, (int)LangProject.LangProjectTags.kflidPictures,
                                                           StringUtils.LocalPictures);
            string origFile = m_pict.PictureFileRA.AbsoluteInternalPath;

            try
            {
                File.Delete(origFile);
            }
            catch
            {
            }
            Assert.IsFalse(File.Exists(origFile),
                           "Test cannot proceed. Unable to delete Original file.");

            // Test
            ICmFile file = CmFile.FindOrCreateFile(folder, origFile);
        }
Пример #12
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Initialize a new CmPicture by creating a copy of the file in the given folder and
        /// hooking everything up.
        /// </summary>
        /// <param name="srcFilename">The path to the original filename (an internal copy will
        /// be made in this method)</param>
        /// <param name="captionTss">The caption (in the given Writing System)</param>
        /// <param name="description">Illustration description in English. This is not
        /// published.</param>
        /// <param name="layoutPos">Indication of where in the column/page the picture is to be
        /// laid out.</param>
        /// <param name="scaleFactor">Integral percentage by which picture is grown or shrunk.</param>
        /// <param name="locationRangeType">Indicates the type of data contained in LocationMin
        /// and LocationMax.</param>
        /// <param name="locationMin">The minimum Scripture reference at which this picture can
        /// be laid out.</param>
        /// <param name="locationMax">The maximum Scripture reference at which this picture can
        /// be laid out.</param>
        /// <param name="copyright">Publishable information about the copyright that should
        /// appear on the copyright page of the publication.</param>
        /// <param name="sFolder">The name of the CmFolder where picture should be stored
        /// </param>
        /// <param name="ws">The WS of the caption and copyright</param>
        /// ------------------------------------------------------------------------------------
        public void InitializeNewPicture(string srcFilename, ITsString captionTss,
                                         string description, PictureLayoutPosition layoutPos, int scaleFactor,
                                         PictureLocationRangeType locationRangeType, int locationMin, int locationMax,
                                         string copyright, string sFolder, int ws)
        {
            // Set the caption first since creating the CmFile will throw if srcFilename is empty.
            if (captionTss != null)
            {
                Caption.SetAlternative(captionTss, ws);
            }
            // Locate CmFolder with given name or create it, if neccessary
            ICmFolder folder = CmFolder.FindOrCreateFolder(m_cache, (int)LangProject.LangProjectTags.kflidPictures, sFolder);

            PictureFileRA = CmFile.FindOrCreateFile(folder, srcFilename);
            int wsEn = Cache.LanguageWritingSystemFactoryAccessor.GetWsFromStr("en");

            if (!String.IsNullOrEmpty(description) && wsEn > 0)
            {
                Description.SetAlternative(description, wsEn);
            }
            LayoutPos         = layoutPos;
            ScaleFactor       = scaleFactor;
            LocationRangeType = locationRangeType;
            LocationMin       = locationMin;
            LocationMax       = locationMax;
            if (!string.IsNullOrEmpty(copyright))
            {
                string sExistingCopyright = PictureFileRA.Copyright.GetAlternative(ws).Text;
                if (sExistingCopyright != null && sExistingCopyright != copyright)
                {
                    Logger.WriteEvent("Could not update copyright for picture " +
                                      PictureFileRA.AbsoluteInternalPath + " to '" + copyright + "'");
                    return;
                }
                PictureFileRA.Copyright.SetAlternative(copyright, ws);
            }
        }
Пример #13
0
		/// <summary>
		/// Expand the internal paths from relative to absolute as needed, since the user
		/// doesn't want to move (or copy) them.
		/// </summary>
		/// <param name="folder"></param>
		/// <param name="sOldRootDir"></param>
		/// <param name="sNewRootDir"></param>
		private static void ExpandToFullPath(ICmFolder folder,
			string sOldRootDir, string sNewRootDir)
		{
			foreach (var file in folder.FilesOC)
			{
				string sFilepath = file.InternalPath;
				if (!Path.IsPathRooted(sFilepath))
				{
					if (FileUtils.SimilarFileExists(Path.Combine(sOldRootDir, sFilepath)) &&
						!FileUtils.SimilarFileExists(Path.Combine(sNewRootDir, sFilepath)))
					{
						file.InternalPath = Path.Combine(sOldRootDir, sFilepath);
					}
				}
			}
			foreach (var sub in folder.SubFoldersOC)
				ExpandToFullPath(sub, sOldRootDir, sNewRootDir);
		}
Пример #14
0
		/// <summary>
		/// Build a list of files that can be moved (or copied) to the new external links root
		/// directory.
		/// </summary>
		/// <param name="folder"></param>
		/// <param name="rgFilesToMove"></param>
		/// <param name="sOldRootDir"></param>
		/// <param name="sNewRootDir"></param>
		private static void CollectMovableFilesFromFolder(ICmFolder folder,
			List<string> rgFilesToMove, string sOldRootDir, string sNewRootDir)
		{
			foreach (ICmFile file in folder.FilesOC)
			{
				string sFilepath = file.InternalPath;
				if (!Path.IsPathRooted(sFilepath))
				{
					if (File.Exists(Path.Combine(sOldRootDir, sFilepath)) &&
						!File.Exists(Path.Combine(sNewRootDir, sFilepath)))
					{
						rgFilesToMove.Add(sFilepath);
					}
				}
			}
			foreach (ICmFolder sub in folder.SubFoldersOC)
				CollectMovableFilesFromFolder(sub, rgFilesToMove, sOldRootDir, sNewRootDir);
		}
Пример #15
0
		/// ------------------------------------------------------------------------------------
		/// <summary>
		/// Finds another CmFile object whose AbsoluteInternalPath is the same as srcFile. If
		/// one is found then the CmFile object is returned. Otherwise a new one is created for
		/// the srcFile.
		/// </summary>
		/// <param name="folder">CmFolder whose CmFile collection will be searched.</param>
		/// <param name="srcFile">Full path of the picture file being searched for.</param>
		/// <returns>A CmFile object</returns>
		/// ------------------------------------------------------------------------------------
		public static ICmFile FindOrCreateFile(ICmFolder folder, string srcFile)
		{
			if (String.IsNullOrEmpty(srcFile))
				throw new ArgumentException("File path not specified.", "srcFile");

			char[] bad = Path.GetInvalidPathChars();
			int idx = srcFile.IndexOfAny(bad);
			if (idx >= 0)
				throw new ArgumentException("File path (" + srcFile + ") contains at least one invalid character.", "srcFile");

			if (!Path.IsPathRooted(srcFile))
				throw new ArgumentException("File does not have a rooted pathname: " + srcFile, "srcFile");

			string newName = Path.GetFileName(srcFile);
			foreach (ICmFile file in folder.FilesOC)
			{
				string internalName = Path.GetFileName(file.AbsoluteInternalPath);
				if (internalName == newName)
				{
					if (FileUtils.AreFilesIdentical(srcFile, file.AbsoluteInternalPath))
						return file;
				}
			}

			ICmFile cmFile = folder.FilesOC.Add(new CmFile());
			((CmFile)cmFile).SetInternalPath(srcFile);

			return cmFile;
		}
Пример #16
0
		/// <summary>
		/// Build a list of files that can be moved (or copied) to the new LinkedFiles root
		/// directory.
		/// </summary>
		/// <param name="folder"></param>
		/// <param name="rgFilesToMove"></param>
		/// <param name="sOldRootDir"></param>
		/// <param name="sNewRootDir"></param>
		private static void CollectMovableFilesFromFolder(ICmFolder folder,
			List<string> rgFilesToMove, string sOldRootDir, string sNewRootDir)
		{
			foreach (var file in folder.FilesOC)
			{
				string sFilepath = file.InternalPath;
				//only select files which have relative paths so they are in the LinkedFilesRootDir
				if (!Path.IsPathRooted(sFilepath))
				{
					// Don't put the same file in more than once!
					if (rgFilesToMove.Contains(sFilepath))
						continue;
					var sOldFilePath = Path.Combine(sOldRootDir, sFilepath);
					if (FileUtils.TrySimilarFileExists(sOldFilePath, out sOldFilePath))
					{
						var sNewFilePath= Path.Combine(sNewRootDir, sFilepath);
						if (FileUtils.TrySimilarFileExists(sNewFilePath, out sNewFilePath))
						{
							//if the file exists in the destination LinkedFiles location, then only copy/move it if
							//file in the source location is newer.
							var dateTimeOfFileSourceFile = File.GetLastWriteTime(sOldFilePath);
							var dateTimeOfFileDestinationFile = File.GetLastWriteTime(sNewFilePath);
							if (dateTimeOfFileSourceFile > dateTimeOfFileDestinationFile)
								rgFilesToMove.Add(sFilepath);
						}
						else
						{
							//if the file does not exist in the destination LinkeFiles location then copy/move it.
							rgFilesToMove.Add(sFilepath);
						}
					}
				}
			}
			foreach (var sub in folder.SubFoldersOC)
				CollectMovableFilesFromFolder(sub, rgFilesToMove, sOldRootDir, sNewRootDir);
		}
Пример #17
0
 private void AddCmFileToCmFolder(ICmFolder cmFolder, string fileInsideLinkedFiles)
 {
     var file = DomainObjectServices.FindOrCreateFile(cmFolder, fileInsideLinkedFiles);
 }
		private void AddCmFileToCmFolder(ICmFolder cmFolder, string fileInsideLinkedFiles)
		{
			var file = DomainObjectServices.FindOrCreateFile(cmFolder, fileInsideLinkedFiles);
		}
Пример #19
0
		private void GetCmFilePathsInCmFolder(ICmFolder cmfolder, HashSet<string> filePathsInCmFiles)
		{
			if (cmfolder == null)
				return;
			foreach (var file in cmfolder.FilesOC)
			{
				string sFilepath = file.InternalPath;
				if (!Path.IsPathRooted(sFilepath))
				{
					filePathsInCmFiles.Add(file.AbsoluteInternalPath);
				}
			}
		}