/// <summary> /// Opens a new document for the given client</summary> /// <param name="client">Document client</param> /// <returns>Document, opened by the given client, or null if the user cancelled or /// there was a problem</returns> /// <remarks>Exceptions during opening are caught and reported via OnOpenException.</remarks> public virtual IDocument OpenNewDocument(IDocumentClient client) { if (client == null) { throw new ArgumentNullException("client"); } IDocument result = null; Uri uri = GetNewDocumentUri(client); if (uri != null) { result = SafeOpen(client, uri); // Consider the document untitled unless its file has been created by the client or // unless the user has already chosen a filename. if (result != null) { if (!m_newDocumentPaths.Contains(result.Uri.LocalPath) && !FileDialogService.PathExists(result.Uri.LocalPath)) { m_untitledDocuments.Add(result); } } } return(result); }
/// <summary> /// Creates a Uri under a specified directory, with a file name containing the specified string and extension, /// which currently doesn't exist on the file system</summary> /// <param name="directoryName">The base directory path under which the new Uri should reside</param> /// <param name="fileName">The base file name to be included in the new Uri, e.g., "Untitled".</param> /// <param name="extension">The file extension to be appended to the new Uri</param> /// <returns>A Uri under 'directoryName', with a file name containing 'fileName'</returns> /// <remarks>The default is to append "(#)" to 'fileName' if necessary, where '#' is a numeric suffix /// starting with 2 that is always incremented (independently for each file extension) for the /// life of this app.</remarks> protected virtual Uri GenerateUniqueUri(string directoryName, string fileName, string extension) { Uri uri; int suffix; m_extensionSuffixes.TryGetValue(extension, out suffix); // check the name to make sure there is no existing file with the same name while (true) { string fullFileName = fileName; if (suffix > 0) { fullFileName += "(" + (suffix + 1) + ")"; } suffix++; fullFileName += extension; string fullPath = Path.Combine(directoryName, fullFileName); if (!FileDialogService.PathExists(fullPath)) { uri = new Uri(fullPath, UriKind.RelativeOrAbsolute); break; } } m_extensionSuffixes[extension] = suffix; return(uri); }
/// <summary> /// Gets the path name for a new document</summary> /// <param name="client">Document client</param> /// <returns>URI representing the new path, or null if the user cancelled</returns> protected virtual Uri GetNewDocumentUri(IDocumentClient client) { Uri uri = null; string fileName = client.Info.NewDocumentName; string extension = null; string directoryName = null; if (client.Info.Extensions.Length > 1) { if (string.IsNullOrEmpty(client.Info.DefaultExtension)) { // Since there are multiple possible extensions, ask the user to pick a filename. string path = PromptUserForNewFilePath(client, fileName, null); if (path != null) { try { if (File.Exists(path)) { File.Delete(path); } } catch (Exception e) { string message = string.Format( "Failed to delete: {0}. Exception: {1}", path, e); Outputs.WriteLine(OutputMessageType.Warning, message); } m_newDocumentPaths.Add(path); uri = new Uri(path, UriKind.RelativeOrAbsolute); return(uri); } } else { directoryName = client.Info.InitialDirectory; if (directoryName == null) { directoryName = FileDialogService.InitialDirectory; } extension = client.Info.DefaultExtension; } } if (client.Info.Extensions.Length >= 1) { // Since there is only one possible extension, we can choose the new name (e.g., "Untitled.xml"). directoryName = client.Info.InitialDirectory; if (directoryName == null) { directoryName = FileDialogService.InitialDirectory; } extension = client.Info.Extensions[0]; if (directoryName != null && extension != null) { int suffix; m_extensionSuffixes.TryGetValue(extension, out suffix); // check the name to make sure there is no existing file with the same name while (true) { string fullFileName = fileName; if (suffix > 0) { fullFileName += "(" + (suffix + 1) + ")"; } suffix++; fullFileName += extension; string fullPath = Path.Combine(directoryName, fullFileName); if (!FileDialogService.PathExists(fullPath)) { uri = new Uri(fullPath, UriKind.RelativeOrAbsolute); break; } } m_extensionSuffixes[extension] = suffix; } } return(uri); }