private void doTranslateFile(FileState state, IUOFTranslator converter) { if (converter == null) { state.IsSucceed = false; return; } try { if (File.Exists(state.SourceFileName)) { Directory.CreateDirectory(Path.GetDirectoryName(state.DestinationFileName)); if (state.TransType == TranslationType.UofToOox) { //TODO:现在的ComputeSize不好用 //converter.UofToOoxComputeSize(state.SourceFileName); ListView.Invoke(myWriteLog, new object[] { "Translating " + state.SourceFileName, false }); converter.UofToOox(state.SourceFileName, state.DestinationFileName); } else if (state.TransType == TranslationType.OoxToUof) { //TODO:现在的ComputeSize不好用 //converter.OoxToUofComputeSize(state.SourceFileName); ListView.Invoke(myWriteLog, new object[] { "Translating " + state.SourceFileName, false }); converter.OoxToUof(state.SourceFileName, state.DestinationFileName); } else { state.IsSucceed = false; return; } } state.IsSucceed = true; //TODO: Set Warning state.HasWarning = false; } catch (Exception e) { // ListView.Invoke(myWriteLog, new object[] { "转换中出现异常:" + e.ToString(), false }); state.IsSucceed = false; } }
private void doWork(object tState) { try { FileState totalState = (FileState)tState; IUOFTranslator converter = null; totalState.IsSucceed = false; totalState.HasWarning = false; int filesCount = 0; int currentCount = 0; ListView.Invoke(mySetProgressBarMove); if (Directory.Exists(totalState.SourceFileName)) { // Is Directory, Translate Each File // Use UUID to create top dir string srcroot = totalState.SourceFileName; string topdir = Guid.NewGuid().ToString() + Path.DirectorySeparatorChar + new DirectoryInfo(srcroot).Name; totalState.DestinationFileName = Path.GetTempPath() + topdir; List <string> files = new List <string>(); bool isAll = false; string searchpattern = Extensions.ALL_SEARCH; if (totalState.TransType == TranslationType.UofToOox) { //searchpattern = Extensions.UOF_SEARCH_PRECISION; if (totalState.DocType == DocumentType.Word) { searchpattern = Extensions.UOF_WORD_SEARCH; } else if (totalState.DocType == DocumentType.Excel) { searchpattern = Extensions.UOF_EXCEL_SEARCH; } else if (totalState.DocType == DocumentType.Powerpnt) { searchpattern = Extensions.UOF_POWERPNT_SEARCH; } else { files.AddRange(Directory.GetFiles(srcroot, Extensions.UOF_POWERPNT_SEARCH, SearchOption.AllDirectories)); files.AddRange(Directory.GetFiles(srcroot, Extensions.UOF_EXCEL_SEARCH, SearchOption.AllDirectories)); files.AddRange(Directory.GetFiles(srcroot, Extensions.UOF_WORD_SEARCH, SearchOption.AllDirectories)); isAll = true; } } else if (totalState.TransType == TranslationType.OoxToUof) { if (totalState.DocType == DocumentType.Word) { searchpattern = Extensions.OOX_WORD_SEARCH; } else if (totalState.DocType == DocumentType.Excel) { searchpattern = Extensions.OOX_EXCEL_SEARCH; } else if (totalState.DocType == DocumentType.Powerpnt) { searchpattern = Extensions.OOX_POWERPNT_SEARCH; } else { files.AddRange(Directory.GetFiles(srcroot, Extensions.OOX_WORD_SEARCH, SearchOption.AllDirectories)); files.AddRange(Directory.GetFiles(srcroot, Extensions.OOX_EXCEL_SEARCH, SearchOption.AllDirectories)); files.AddRange(Directory.GetFiles(srcroot, Extensions.OOX_POWERPNT_SEARCH, SearchOption.AllDirectories)); isAll = true; } } else { goto Finish; } if (!isAll) { files.AddRange(Directory.GetFiles(srcroot, searchpattern, SearchOption.AllDirectories)); } if (files.Count == 0) { goto Finish; } filesCount = files.Count; currentCount = 0; ListView.Invoke(mySetProgress, new object[] { filesCount, currentCount }); foreach (string file in files) { FileState fileState = new FileState(); fileState.SourceFileName = Path.GetFullPath(file); fileState.DestinationFileName = totalState.DestinationFileName + file.Substring(srcroot.Length); fileState.adjustDestFileExtension(); if (totalState.DocType != DocumentType.All) { if ((totalState.TransType == TranslationType.UofToOox) && (totalState.DocType != fileState.DocType)) { //跳过这个UOF文件 continue; } } converter = initConverter(fileState.SourceFileName, fileState.DocType); doTranslateFile(fileState, converter); if (fileState.IsSucceed) { totalState.IsSucceed = true; } else { totalState.HasWarning = true; ListView.Invoke(myWriteLog, new object[] { "Error:File" + fileState.SourceFileName + "translate false.", false }); } currentCount++; ListView.Invoke(mySetProgress, new object[] { filesCount, currentCount }); } } else { filesCount = 1; currentCount = 0; ListView.Invoke(mySetProgress, new object[] { filesCount, currentCount }); string topdir = Guid.NewGuid().ToString(); totalState.DestinationFileName = Path.GetTempPath() + topdir + Path.DirectorySeparatorChar + Path.GetFileName(totalState.SourceFileName); totalState.adjustDestFileExtension(); converter = initConverter(totalState.SourceFileName, totalState.DocType); doTranslateFile(totalState, converter); currentCount++; ListView.Invoke(mySetProgress, new object[] { filesCount, currentCount }); if (!totalState.IsSucceed) { ListView.Invoke(myWriteLog, new object[] { "Error:File" + totalState.SourceFileName + "translate false.", false }); } } Finish: ListView.Invoke(mySetProgressBarStop); ListView.Invoke(mySetProgress, new object[] { filesCount, currentCount }); totalState.StateValProgress = StateVal.STATE_DONE; ListView.Invoke(myTransferState, new object[] { totalState.clone() }); } catch (ThreadAbortException ex) { ListView.Invoke(mySetProgressBarStop); } }