#region License // Copyright (c) 2010, Jasper Yeh. // All rights reserved. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // * Neither the name of ClearCanvas Inc. nor the names of its contributors // may be used to endorse or promote products derived from this software without // specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE // GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY // OF SUCH DAMAGE. #endregion using System; using System.Collections.Generic; using System.IO; using ClearCanvas.Common; using ClearCanvas.Desktop; using ClearCanvas.Dicom; using ClearCanvas.ImageViewer; using Path = System.IO.Path; namespace Nullstack.ClearCanvasEx.ViewerEx.FileSetViewer { internal static class OpenImagesHelper { public static bool Open(IDesktopWindow desktopWindow, string path) { string title; var fileList = BuildFileList(desktopWindow, path, out title); if (fileList == null) return false; try { LaunchViewer(desktopWindow, fileList, title); return true; } catch (Exception ex) { ExceptionHandler.Report(ex, desktopWindow); return false; } } private static string[] BuildFileList(IDesktopWindow desktopWindow, string path, out string title) { title = null; if (File.Exists(path)) { if (IsValidDicomFileSet(path)) return new List(ResolveDicomFileSet(path, out title)).ToArray(); return new[] {path}; } if (Directory.Exists(path)) { var recursive = false; if (Directory.GetDirectories(path).Length > 0) { var result = desktopWindow.ShowMessageBox(string.Format(SR.msgConfirmRecursiveOpenDirectory, path), SR.szOpenDirectory, MessageBoxActions.YesNoCancel); if (result == DialogBoxAction.Yes) recursive = true; else if (result == DialogBoxAction.Cancel) return null; } return Directory.GetFiles(path, "*.*", recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly); } throw new FileNotFoundException("Specified path does not exist.", path); } private static void LaunchViewer(IDesktopWindow desktopWindow, string[] files, string title) { var component = new ImageViewerComponent(LayoutManagerCreationParameters.Extended, PriorStudyFinder.Null); try { bool cancelled; component.LoadImages(files, desktopWindow, out cancelled); if (cancelled) return; } catch (LoadSopsException ex) { ExceptionHandler.Report(ex, desktopWindow); if (!ex.AnyLoaded) return; } ImageViewerComponent.Launch(component, new LaunchImageViewerArgs(WindowBehaviour.Auto) {Title = title}); } private static bool IsValidDicomFileSet(string fileSetPath) { if (string.IsNullOrEmpty(fileSetPath) || !File.Exists(fileSetPath)) return false; try { var dicomFile = new DicomFile(fileSetPath); dicomFile.Load(); return dicomFile.MediaStorageSopClassUid == SopClass.MediaStorageDirectoryStorageUid; } catch (Exception) { return false; } } private static IEnumerable ResolveDicomFileSet(string dicomDirPath, out string fileSetId) { if (!File.Exists(dicomDirPath)) throw new FileNotFoundException("Specified file does not exist.", dicomDirPath); var fileList = new List(); using (var dicomDir = new DicomDirectory("AETITLE")) { dicomDir.Load(dicomDirPath); fileSetId = dicomDir.FileSetId; EnumerateFileRecords(dicomDir.RootDirectoryRecordCollection, fileList); } var fileSetRootPath = Path.GetDirectoryName(dicomDirPath); if (fileSetRootPath == null) return null; return fileList.ConvertAll(s => Path.Combine(fileSetRootPath, s)); } private static void EnumerateFileRecords(IEnumerable directoryRecords, ICollection fileList) { if (directoryRecords != null) { foreach (var directoryRecord in directoryRecords) { EnumerateFileRecords(directoryRecord.LowerLevelDirectoryRecordCollection, fileList); var fileId = directoryRecord[DicomTags.ReferencedFileId].ToString(); if (!string.IsNullOrEmpty(fileId)) fileList.Add(fileId); } } } } }