private void LoadCallback()
        {
            try
            {
                document = fileType.Load(siphonStream);
            }

            catch
            {
                if (document != null)
                {
                    document.Dispose();
                    document = null;
                }

                throw;
            }
        }
Esempio n. 2
0
        private void UpdateFileSizeAndPreview(string tempFileName)
        {
            if (this.IsDisposed)
            {
                return;
            }

            if (tempFileName == null)
            {
                string error = PdnResources.GetString("SaveConfigDialog.FileSizeText.Text.Error");
                this.fileSizeText.Text = string.Format(this.fileSizeTextFormat, error);
            }
            else
            {
                FileInfo fi       = new FileInfo(tempFileName);
                long     fileSize = fi.Length;
                this.fileSizeText.Text    = string.Format(fileSizeTextFormat, Utility.SizeStringFromBytes(fileSize));
                this.documentView.Visible = true;

                // note: see comments for DocumentView.SuspendRefresh() for why we do these two backwards
                this.documentView.ResumeRefresh();

                Document disposeMe = null;
                try
                {
                    if (this.disposeDocument && this.documentView.Document != null)
                    {
                        disposeMe = this.documentView.Document;
                    }

                    if (this.fileType.IsReflexive(this.SaveConfigToken))
                    {
                        this.documentView.Document = this.Document;
                        this.documentView.Document.Invalidate();
                        this.disposeDocument = false;
                    }
                    else
                    {
                        FileStream stream = new FileStream(tempFileName, FileMode.Open, FileAccess.Read, FileShare.Read);

                        Document previewDoc;

                        try
                        {
                            Utility.GCFullCollect();
                            previewDoc = fileType.Load(stream);
                        }

                        catch
                        {
                            previewDoc = null;
                            TokenChangedHandler(this, EventArgs.Empty);
                        }

                        stream.Close();

                        if (previewDoc != null)
                        {
                            this.documentView.Document = previewDoc;
                            this.disposeDocument       = true;
                        }

                        Utility.GCFullCollect();
                    }

                    try
                    {
                        fi.Delete();
                    }

                    catch
                    {
                    }
                }

                finally
                {
                    this.documentView.SuspendRefresh();

                    if (disposeMe != null)
                    {
                        disposeMe.Dispose();
                    }
                }
            }
        }
Esempio n. 3
0
        public static Document LoadDocument(Control owner, string fileName, out FileType fileTypeResult, ProgressEventHandler progressCallback)
        {
            FileTypeCollection fileTypes;
            int ftIndex;
            FileType fileType;

            fileTypeResult = null;

            try
            {
                fileTypes = FileTypes.GetFileTypes();
                ftIndex = fileTypes.IndexOfExtension(Path.GetExtension(fileName));

                if (ftIndex == -1)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.ImageTypeNotRecognized"));
                    return null;
                }

                fileType = fileTypes[ftIndex];
                fileTypeResult = fileType;
            }

            catch (ArgumentException)
            {
                string format = PdnResources.GetString("LoadImage.Error.InvalidFileName.Format");
                string error = string.Format(format, fileName);
                Utility.ErrorBox(owner, error);
                return null;
            }

            Document document = null;

            using (new WaitCursorChanger(owner))
            {
                Utility.GCFullCollect();
                Stream stream = null;

                try
                {
                    try
                    {
                        stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                        long totalBytes = 0;

                        SiphonStream siphonStream = new SiphonStream(stream);

                        IOEventHandler ioEventHandler = null;
                        ioEventHandler =
                            delegate(object sender, IOEventArgs e)
                            {
                                if (progressCallback != null)
                                {
                                    totalBytes += (long)e.Count;
                                    double percent = Utility.Clamp(100.0 * ((double)totalBytes / (double)siphonStream.Length), 0, 100);
                                    progressCallback(null, new ProgressEventArgs(percent));
                                }
                            };

                        siphonStream.IOFinished += ioEventHandler;

                        using (new WaitCursorChanger(owner))
                        {
                            document = fileType.Load(siphonStream);

                            if (progressCallback != null)
                            {
                                progressCallback(null, new ProgressEventArgs(100.0));
                            }
                        }

                        siphonStream.IOFinished -= ioEventHandler;
                        siphonStream.Close();
                    }

                    catch (WorkerThreadException ex)
                    {
                        Type innerExType = ex.InnerException.GetType();
                        ConstructorInfo ci = innerExType.GetConstructor(new Type[] { typeof(string), typeof(Exception) });

                        if (ci == null)
                        {
                            throw;
                        }
                        else
                        {
                            Exception ex2 = (Exception)ci.Invoke(new object[] { "Worker thread threw an exception of this type", ex.InnerException });
                            throw ex2;
                        }
                    }
                }

                catch (ArgumentException)
                {
                    if (fileName.Length == 0)
                    {
                        Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.BlankFileName"));
                    }
                    else
                    {
                        Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.ArgumentException"));
                    }
                }

                catch (UnauthorizedAccessException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.UnauthorizedAccessException"));
                }

                catch (SecurityException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.SecurityException"));
                }

                catch (FileNotFoundException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.FileNotFoundException"));
                }

                catch (DirectoryNotFoundException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.DirectoryNotFoundException"));
                }

                catch (PathTooLongException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.PathTooLongException"));
                }

                catch (IOException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.IOException"));
                }

                catch (SerializationException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.SerializationException"));
                }

                catch (OutOfMemoryException)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.OutOfMemoryException"));
                }

                catch (Exception)
                {
                    Utility.ErrorBox(owner, PdnResources.GetString("LoadImage.Error.Exception"));
                }

                finally
                {
                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
                }
            }

            return document;
        }