Beispiel #1
0
        public HResult CloseAllDocuments(NiSaveAllMode mode)
        {
            try
            {
                var hr = SaveAllDocuments(mode, true);

                if (ErrorUtil.Failure(hr) || hr == HResult.False)
                {
                    return(hr);
                }

                foreach (var windowFrame in ((INiShell)GetService(typeof(INiShell))).GetDocumentWindows())
                {
                    // We specifically request the frame not to save, because
                    // the SaveAllDocuments should already have taken care of this
                    // and we may have dirty windows because the user said the
                    // documents shouldn't be saved.

                    ErrorUtil.ThrowOnFailure(windowFrame.Close(NiFrameCloseMode.NoSave));
                }

                return(HResult.OK);
            }
            catch (Exception ex)
            {
                return(ErrorUtil.GetHResult(ex));
            }
        }
Beispiel #2
0
        private List <Tuple <INiHierarchy, INiPersistDocData> > GetDirtyDocuments(NiSaveAllMode mode)
        {
            var dirtyDocuments = new List <Tuple <INiHierarchy, INiPersistDocData> >();

            if (mode == NiSaveAllMode.VisibleOnly)
            {
                foreach (var windowFrame in ((INiShell)GetService(typeof(INiShell))).GetDocumentWindows())
                {
                    var docData = (INiPersistDocData)windowFrame.GetPropertyEx(NiFrameProperty.DocData);

                    if (docData != null)
                    {
                        bool isDirty;
                        ErrorUtil.ThrowOnFailure(docData.IsDocDataDirty(out isDirty));

                        if (isDirty)
                        {
                            var hier = (INiHierarchy)windowFrame.GetPropertyEx(NiFrameProperty.Hierarchy);

                            Debug.Assert(hier != null);

                            if (hier != null)
                            {
                                dirtyDocuments.Add(Tuple.Create(hier, docData));
                            }
                        }
                    }
                }
            }
            else
            {
                var runningDocumentTable = (INiRunningDocumentTable)GetService(typeof(INiRunningDocumentTable));

                foreach (int cookie in runningDocumentTable.GetDocuments())
                {
                    string            document;
                    INiHierarchy      hier;
                    INiPersistDocData docData;
                    ErrorUtil.ThrowOnFailure(runningDocumentTable.GetDocumentInfo(
                                                 cookie, out document, out hier, out docData
                                                 ));

                    bool isDirty;
                    ErrorUtil.ThrowOnFailure(docData.IsDocDataDirty(out isDirty));

                    if (isDirty)
                    {
                        dirtyDocuments.Add(Tuple.Create(hier, docData));
                    }
                }
            }

            return(dirtyDocuments);
        }
Beispiel #3
0
        public HResult SaveAllDocuments(NiSaveAllMode mode, bool prompt)
        {
            try
            {
                var dirtyDocuments = GetDirtyDocuments(mode);

                if (dirtyDocuments.Count == 0)
                {
                    return(HResult.OK);
                }

                NiQuerySaveResult querySaveResult;
                ErrorUtil.ThrowOnFailure(((INiQuerySave)GetService(typeof(INiQuerySave))).QuerySaveViaDialog(
                                             dirtyDocuments.Select(p => p.Item1).ToArray(),
                                             out querySaveResult
                                             ));

                switch (querySaveResult)
                {
                case NiQuerySaveResult.Save:
                    foreach (var docData in dirtyDocuments.Select(p => p.Item2))
                    {
                        string document;
                        bool   saved;
                        ErrorUtil.ThrowOnFailure(docData.SaveDocData(
                                                     NiSaveMode.SilentSave,
                                                     out document,
                                                     out saved
                                                     ));

                        if (!saved)
                        {
                            Log.Warn("Document {0} was not saved during SaveAll; ignoring");
                        }
                    }
                    return(HResult.OK);

                case NiQuerySaveResult.DoNotSave:
                    return(HResult.OK);

                default:
                    return(HResult.False);
                }
            }
            catch (Exception ex)
            {
                return(ErrorUtil.GetHResult(ex));
            }
        }
Beispiel #4
0
        private List<Tuple<INiHierarchy, INiPersistDocData>> GetDirtyDocuments(NiSaveAllMode mode)
        {
            var dirtyDocuments = new List<Tuple<INiHierarchy, INiPersistDocData>>();

            if (mode == NiSaveAllMode.VisibleOnly)
            {
                foreach (var windowFrame in ((INiShell)GetService(typeof(INiShell))).GetDocumentWindows())
                {
                    var docData = (INiPersistDocData)windowFrame.GetPropertyEx(NiFrameProperty.DocData);

                    if (docData != null)
                    {
                        bool isDirty;
                        ErrorUtil.ThrowOnFailure(docData.IsDocDataDirty(out isDirty));

                        if (isDirty)
                        {
                            var hier = (INiHierarchy)windowFrame.GetPropertyEx(NiFrameProperty.Hierarchy);

                            Debug.Assert(hier != null);

                            if (hier != null)
                                dirtyDocuments.Add(Tuple.Create(hier, docData));
                        }
                    }
                }
            }
            else
            {
                var runningDocumentTable = (INiRunningDocumentTable)GetService(typeof(INiRunningDocumentTable));

                foreach (int cookie in runningDocumentTable.GetDocuments())
                {
                    string document;
                    INiHierarchy hier;
                    INiPersistDocData docData;
                    ErrorUtil.ThrowOnFailure(runningDocumentTable.GetDocumentInfo(
                        cookie, out document, out hier, out docData
                    ));

                    bool isDirty;
                    ErrorUtil.ThrowOnFailure(docData.IsDocDataDirty(out isDirty));

                    if (isDirty)
                        dirtyDocuments.Add(Tuple.Create(hier, docData));
                }
            }

            return dirtyDocuments;
        }
Beispiel #5
0
        public HResult SaveAllDocuments(NiSaveAllMode mode, bool prompt)
        {
            try
            {
                var dirtyDocuments = GetDirtyDocuments(mode);

                if (dirtyDocuments.Count == 0)
                    return HResult.OK;

                NiQuerySaveResult querySaveResult;
                ErrorUtil.ThrowOnFailure(((INiQuerySave)GetService(typeof(INiQuerySave))).QuerySaveViaDialog(
                    dirtyDocuments.Select(p => p.Item1).ToArray(),
                    out querySaveResult
                ));

                switch (querySaveResult)
                {
                    case NiQuerySaveResult.Save:
                        foreach (var docData in dirtyDocuments.Select(p => p.Item2))
                        {
                            string document;
                            bool saved;
                            ErrorUtil.ThrowOnFailure(docData.SaveDocData(
                                NiSaveMode.SilentSave,
                                out document,
                                out saved
                            ));

                            if (!saved)
                                Log.Warn("Document {0} was not saved during SaveAll; ignoring");
                        }
                        return HResult.OK;

                    case NiQuerySaveResult.DoNotSave:
                        return HResult.OK;

                    default:
                        return HResult.False;
                }
            }
            catch (Exception ex)
            {
                return ErrorUtil.GetHResult(ex);
            }
        }
Beispiel #6
0
        public HResult CloseAllDocuments(NiSaveAllMode mode)
        {
            try
            {
                var hr = SaveAllDocuments(mode, true);

                if (ErrorUtil.Failure(hr) || hr == HResult.False)
                    return hr;

                foreach (var windowFrame in ((INiShell)GetService(typeof(INiShell))).GetDocumentWindows())
                {
                    // We specifically request the frame not to save, because
                    // the SaveAllDocuments should already have taken care of this
                    // and we may have dirty windows because the user said the
                    // documents shouldn't be saved.

                    ErrorUtil.ThrowOnFailure(windowFrame.Close(NiFrameCloseMode.NoSave));
                }

                return HResult.OK;
            }
            catch (Exception ex)
            {
                return ErrorUtil.GetHResult(ex);
            }
        }