private void NotifyForSave(string filePath)
        {
            int hr;
            IVsQueryEditQuerySave2 queryEditQuerySave = (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));
            uint result;

            hr = queryEditQuerySave.QuerySaveFile(filePath, 0, null, out result);
        }
Beispiel #2
0
        private int QuerySave(out tagVSQuerySaveResult qsResult)
        {
            uint result;
            IVsQueryEditQuerySave2 querySave =
                (IVsQueryEditQuerySave2)serviceProvider.GetService(typeof(SVsQueryEditQuerySave));
            int hr = querySave.QuerySaveFile(fileName, 0, null, out result);

            qsResult = (tagVSQuerySaveResult)result;
            return(hr);
        }
        /// <summary>
        /// Saves the diagram.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="serializationResult">The serialization result.</param>
        /// <param name="modelRoot">The model root.</param>
        /// <param name="modelFileName">Name of the model file.</param>
        /// <param name="diagram">The diagram.</param>
        /// <param name="diagramFileName">Name of the diagram file.</param>
        /// <param name="encoding">The encoding.</param>
        /// <param name="writeOptionalPropertiesWithDefaultValue">if set to <c>true</c> [write optional properties with default value].</param>
        public void SaveDiagram <TModel>(DslModeling::SerializationResult serializationResult, TModel modelRoot, string modelFileName, ComponentModelDiagram diagram, string diagramFileName, global::System.Text.Encoding encoding, bool writeOptionalPropertiesWithDefaultValue)
            where TModel : ModelElement
        {
            if (serializationResult.Failed)
            {
                return;
            }

            // MonikerResolver shouldn't be required in Save operation, so not calling SetupMonikerResolver() here.

            using (global::System.IO.MemoryStream diagramFileContent = new global::System.IO.MemoryStream())
            {
                DslModeling::DomainClassXmlSerializer diagramSerializer = this.Directory.GetSerializer(diagram.GetDomainClass().Id);
                global::System.Diagnostics.Debug.Assert(diagramSerializer != null, "Cannot find serializer for " + diagram.GetDomainClass().Name + "!");
                if (diagramSerializer != null)
                {
                    DslModeling::SerializationContext serializationContext = new DslModeling::SerializationContext(this.Directory, diagramFileName, serializationResult);
                    serializationContext.WriteOptionalPropertiesWithDefaultValue = writeOptionalPropertiesWithDefaultValue;
                    global::System.Xml.XmlWriterSettings settings = new global::System.Xml.XmlWriterSettings();
                    settings.Indent   = true;
                    settings.Encoding = encoding;
                    using (global::System.IO.StreamWriter streamWriter = new global::System.IO.StreamWriter(diagramFileContent, encoding))
                    {
                        using (global::System.Xml.XmlWriter writer = global::System.Xml.XmlWriter.Create(streamWriter, settings))
                        {
                            diagramSerializer.WriteRootElement(serializationContext, diagram, writer);
                        }
                    }
                }
                if (!serializationResult.Failed)
                {       // Only write the contents if there's no error encountered during serialization.
                    if (diagramFileContent != null)
                    {
                        IVsQueryEditQuerySave2 scc = ServiceLocator.Instance.GetService <IVsQueryEditQuerySave2>(typeof(SVsQueryEditQuerySave));
                        if (scc != null)
                        {
                            uint result;
                            if (scc.QuerySaveFile(diagramFileName, 0, null, out result) != (int)tagVSQuerySaveResult.QSR_SaveOK)
                            {
                                return;
                            }
                        }
                        using (global::System.IO.FileStream fileStream = new global::System.IO.FileStream(diagramFileName, global::System.IO.FileMode.Create, global::System.IO.FileAccess.Write, global::System.IO.FileShare.None))
                        {
                            using (global::System.IO.BinaryWriter writer = new global::System.IO.BinaryWriter(fileStream, encoding))
                            {
                                writer.Write(diagramFileContent.ToArray());
                            }
                        }
                    }
                }
            }
        }
Beispiel #4
0
        private void SaveOptionsExternal()
        {
            Dispatcher.CurrentDispatcher.VerifyAccess();

            SolutionManager solMgr = GetSolutionManager();

            if (solMgr != null)
            {
                SolutionInfo           info = solMgr.GetSolutionInfo();
                IVsQueryEditQuerySave2 queryEditQuerySave = (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));

                string targetPath = Path.ChangeExtension(info.SolutionFile, ".projectSets.json");
                if (m_repository.ProjectSets.Any() || File.Exists(targetPath))
                {
                    string tempPath = Path.GetTempFileName();
                    using (FileStream fs = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        m_repository.SaveJson(fs);
                    }

                    if (!AreFilesIdentical(tempPath, targetPath))
                    {
                        tagVSQuerySaveResult querySaveResult = tagVSQuerySaveResult.QSR_SaveOK;
                        if (queryEditQuerySave != null)
                        {
                            uint result;
                            ErrorHandler.ThrowOnFailure(queryEditQuerySave.QuerySaveFile(targetPath, 0, null, out result));
                            querySaveResult = (tagVSQuerySaveResult)result;
                        }

                        if (querySaveResult == tagVSQuerySaveResult.QSR_SaveOK)
                        {
                            var settings = GetSettings();
                            using (FileStream fin = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.None))
                                using (FileStream fout = new FileStream(targetPath, FileMode.Create, FileAccess.Write, FileShare.None))
                                {
                                    fin.CopyTo(fout);
                                }
                        }
                    }
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Saves the document
        /// </summary>
        /// <param name="saveFlag">Save flags</param>
        /// <param name="newFilePath">File path</param>
        /// <param name="saveCanceled">True if save was not successuful</param>
        public int SaveDocData(VSSAVEFLAGS saveFlag, out string newFilePath, out int saveCanceled)
        {
            newFilePath  = null;
            saveCanceled = 0;
            int hr = VSConstants.S_OK;

            switch (saveFlag)
            {
            case VSSAVEFLAGS.VSSAVE_Save:
            case VSSAVEFLAGS.VSSAVE_SilentSave: {
                IVsQueryEditQuerySave2 queryEditQuerySave = (IVsQueryEditQuerySave2)Package.GetGlobalService(typeof(SVsQueryEditQuerySave));

                // Call QueryEditQuerySave
                uint result = 0;
                hr = queryEditQuerySave.QuerySaveFile(
                    FileName,
                    // filename
                    0,
                    // flags
                    null,
                    // file attributes
                    out result);         // result
                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }

                // Process according to result from QuerySave
                switch ((tagVSQuerySaveResult)result)
                {
                case tagVSQuerySaveResult.QSR_NoSave_Cancel:
                    // Note that this is also case tagVSQuerySaveResult.QSR_NoSave_UserCanceled because these
                    // two tags have the same value.
                    saveCanceled = ~0;
                    break;

                case tagVSQuerySaveResult.QSR_SaveOK: {
                    // Call the shell to do the save for us
                    IVsUIShell uiShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
                    hr = uiShell.SaveDocDataToFile(saveFlag, (IPersistFileFormat)this, FileName, out newFilePath, out saveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_ForceSaveAs: {
                    // Call the shell to do the SaveAS for us
                    IVsUIShell uiShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
                    hr = uiShell.SaveDocDataToFile(VSSAVEFLAGS.VSSAVE_SaveAs, (IPersistFileFormat)this, FileName, out newFilePath, out saveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_NoSave_Continue:
                    // In this case there is nothing to do.
                    break;

                default:
                    throw new COMException("Invalid QuerySave result.");
                }
                break;
            }

            case VSSAVEFLAGS.VSSAVE_SaveAs:
            case VSSAVEFLAGS.VSSAVE_SaveCopyAs: {
                // Call the shell to do the save for us
                IVsUIShell uiShell = (IVsUIShell)Package.GetGlobalService(typeof(SVsUIShell));
                hr = uiShell.SaveDocDataToFile(saveFlag, (IPersistFileFormat)this, FileName, out newFilePath, out saveCanceled);
                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }
                break;
            }

            default:
                throw new ArgumentException("Invalid VSSAVEFLAGS.");
            }


            return(VSConstants.S_OK);
        }
Beispiel #6
0
        int IVsPersistDocData.SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            pbstrMkDocumentNew = null;
            pfSaveCanceled     = 0;
            int hr = VSConstants.S_OK;

            switch (dwSave)
            {
            case VSSAVEFLAGS.VSSAVE_Save:
            case VSSAVEFLAGS.VSSAVE_SilentSave:
            {
                IVsQueryEditQuerySave2 queryEditQuerySave = (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));

                // Call QueryEditQuerySave
                uint result = 0;
                hr = queryEditQuerySave.QuerySaveFile(
                    fileName,                               // filename
                    0,                                      // flags
                    null,                                   // file attributes
                    out result);                            // result

                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }

                // Process according to result from QuerySave
                switch ((tagVSQuerySaveResult)result)
                {
                case tagVSQuerySaveResult.QSR_NoSave_Cancel:
                    // Note that this is also case tagVSQuerySaveResult.QSR_NoSave_UserCanceled because these
                    // two tags have the same value.
                    pfSaveCanceled = ~0;
                    break;

                case tagVSQuerySaveResult.QSR_SaveOK:
                {
                    // Call the shell to do the save for us
                    IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
                    hr = uiShell.SaveDocDataToFile(dwSave, this, fileName, out pbstrMkDocumentNew, out pfSaveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_ForceSaveAs:
                {
                    // Call the shell to do the SaveAS for us
                    IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
                    hr = uiShell.SaveDocDataToFile(VSSAVEFLAGS.VSSAVE_SaveAs, this, fileName, out pbstrMkDocumentNew, out pfSaveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_NoSave_Continue:
                    // In this case there is nothing to do.
                    break;

                default:
                    throw new COMException(Resources.ExceptionMessageQEQS);
                }
                break;
            }

            case VSSAVEFLAGS.VSSAVE_SaveAs:
            case VSSAVEFLAGS.VSSAVE_SaveCopyAs:
            {
                // Make sure the file name as the right extension
                if (string.Compare(Constants.fileExtension, System.IO.Path.GetExtension(fileName), true, CultureInfo.CurrentCulture) != 0)
                {
                    fileName += Constants.fileExtension;
                }
                // Call the shell to do the save for us
                IVsUIShell uiShell = (IVsUIShell)GetService(typeof(SVsUIShell));
                hr = uiShell.SaveDocDataToFile(dwSave, this, fileName, out pbstrMkDocumentNew, out pfSaveCanceled);
                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }
                break;
            }

            default:
                throw new ArgumentException(Resources.ExceptionMessageSaveFlag);
            }
            ;

            return(VSConstants.S_OK);
        }
Beispiel #7
0
        int IVsPersistDocData.SaveDocData(VSSAVEFLAGS dwSave, out string pbstrMkDocumentNew, out int pfSaveCanceled)
        {
            pbstrMkDocumentNew = null;
            pfSaveCanceled     = 0;
            int hr;

            switch (dwSave)
            {
            case VSSAVEFLAGS.VSSAVE_Save:
            case VSSAVEFLAGS.VSSAVE_SilentSave:
            {
                IVsQueryEditQuerySave2 queryEditQuerySave =
                    (IVsQueryEditQuerySave2)GetService(typeof(SVsQueryEditQuerySave));

                uint result;
                hr = queryEditQuerySave.QuerySaveFile(
                    _FileName,
                    0,
                    null,
                    out result);

                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }

                switch ((tagVSQuerySaveResult)result)
                {
                case tagVSQuerySaveResult.QSR_NoSave_Cancel:
                    pfSaveCanceled = ~0;
                    break;

                case tagVSQuerySaveResult.QSR_SaveOK:
                {
                    hr = ServiceLocator.GetGlobalService <SVsUIShell, IVsUIShell>().SaveDocDataToFile(
                        dwSave, this, _FileName, out pbstrMkDocumentNew, out pfSaveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_ForceSaveAs:
                {
                    hr = ServiceLocator.GetGlobalService <SVsUIShell, IVsUIShell>().SaveDocDataToFile(
                        VSSAVEFLAGS.VSSAVE_SaveAs, this, _FileName, out pbstrMkDocumentNew,
                        out pfSaveCanceled);
                    if (ErrorHandler.Failed(hr))
                    {
                        return(hr);
                    }
                }
                break;

                case tagVSQuerySaveResult.QSR_NoSave_Continue:
                    break;

                default:
                    throw new InvalidOperationException("Unsupported result from Query Edit/Query Save");
                }
                break;
            }

            case VSSAVEFLAGS.VSSAVE_SaveAs:
            case VSSAVEFLAGS.VSSAVE_SaveCopyAs:
            {
                if (String.Compare(FileExtensionUsed, Path.GetExtension(_FileName), true, CultureInfo.CurrentCulture) != 0)
                {
                    _FileName += FileExtensionUsed;
                }
                hr = ServiceLocator.GetGlobalService <SVsUIShell, IVsUIShell>().SaveDocDataToFile(dwSave,
                                                                                                  this, _FileName, out pbstrMkDocumentNew, out pfSaveCanceled);
                if (ErrorHandler.Failed(hr))
                {
                    return(hr);
                }
                break;
            }

            default:
                throw new ArgumentException("Unsupported save flag value");
            }
            return(VSConstants.S_OK);
        }