public LoadProgressDialog(Control owner, Stream stream, FileType fileType)
     : base(owner, 
            PdnInfo.GetBareProductName(),
            PdnResources.GetString("LoadProgressDialog.Description"))
 {
     this.fileType = fileType;
     this.siphonStream = new SiphonStream(stream);
     this.siphonStream.IOFinished += new IOEventHandler(siphonStream_IOFinished);
     this.Icon = Utility.ImageToIcon(PdnResources.GetImage("Icons.ImageFromDiskIcon.png"), Utility.TransparentKey);
 }
        protected override void OnCancelClick()
        {
            SiphonStream stream = siphonStream;

            if (stream != null)
            {
                stream.Abort(new ApplicationException("Aborted"));
            }

            base.OnCancelClick();
        }
Beispiel #3
0
        public void OnEnteredStateImpl()
        {
            FileStream zipFileRead = new FileStream(this.extractMe, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream exeFileWrite = null;

            try
            {
                ICSharpCode.SharpZipLib.Zip.ZipInputStream zipStream =
                    new ICSharpCode.SharpZipLib.Zip.ZipInputStream(zipFileRead);

                // Search for the first .msi file in the exe, and extract it
                ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry;
                bool foundExe = false;

                while (true)
                {
                    zipEntry = zipStream.GetNextEntry();

                    if (zipEntry == null)
                    {
                        break;
                    }

                    if (!zipEntry.IsDirectory &&
                        string.Compare(".exe", Path.GetExtension(zipEntry.Name), true, CultureInfo.InvariantCulture) == 0)
                    {
                        foundExe = true;
                        break;
                    }
                }

                if (!foundExe)
                {
                    this.exception = new FileNotFoundException();
                    StateMachine.QueueInput(PrivateInput.GoToError);
                }
                else
                {
                    int maxBytes = (int)zipEntry.Size;
                    int bytesSoFar = 0;

                    this.installerPath = Path.Combine(Path.GetDirectoryName(this.extractMe), zipEntry.Name);
                    exeFileWrite = new FileStream(this.installerPath, FileMode.Create, FileAccess.Write, FileShare.Read);
                    SiphonStream siphonStream2 = new SiphonStream(exeFileWrite, 4096);

                    this.abortMeStream = siphonStream2;

                    IOEventHandler ioFinishedDelegate =
                        delegate(object sender, IOEventArgs e)
                        {
                            bytesSoFar += e.Count;
                            double percent = 100.0 * ((double)bytesSoFar / (double)maxBytes);
                            OnProgress(percent);
                        };

                    OnProgress(0.0);

                    if (maxBytes > 0)
                    {
                        siphonStream2.IOFinished += ioFinishedDelegate;
                    }

                    Utility.CopyStream(zipStream, siphonStream2);

                    if (maxBytes > 0)
                    {
                        siphonStream2.IOFinished -= ioFinishedDelegate;
                    }

                    this.abortMeStream = null;
                    siphonStream2 = null;
                    exeFileWrite.Close();
                    exeFileWrite = null;
                    zipStream.Close();
                    zipStream = null;

                    StateMachine.QueueInput(PrivateInput.GoToReadyToInstall);
                }
            }

            catch (Exception ex)
            {
                if (this.AbortRequested)
                {
                    StateMachine.QueueInput(PrivateInput.GoToAborted);
                }
                else
                {
                    this.exception = ex;
                    StateMachine.QueueInput(PrivateInput.GoToError);
                }
            }

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

                if (zipFileRead != null)
                {
                    zipFileRead.Close();
                    zipFileRead = null;
                }

                if (this.exception != null || this.AbortRequested)
                {
                    if (this.installerPath != null)
                    {
                        bool result = FileSystem.TryDeleteFile(this.installerPath);
                    }
                }

                if (this.extractMe != null)
                {
                    bool result = FileSystem.TryDeleteFile(this.extractMe);
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Saves the Document to the given Stream with the default and given headers, and
        /// using the given IO completion callback.
        /// </summary>
        /// <param name="stream">The Stream to serialize the Document to.</param>
        /// <param name="callback">
        /// This can be used to keep track of the number of uncompressed bytes that are written. The 
        /// values reported through the IOEventArgs.Count+Offset will vary from 1 to approximately 
        /// Layers.Count*Width*Height*sizeof(ColorBgra). The final number will actually be higher 
        /// because of hierarchical overhead, so make sure to cap any progress reports to 100%. This
        /// callback will be wired to the IOFinished event of a SiphonStream. Events may be raised
        /// from any thread. May be null.
        /// </param>
        public void SaveToStream(Stream stream, IOEventHandler callback)
        {
            InitializeDpu();

            PrepareHeader();
            string headerText = this.HeaderXml.OuterXml;

            // Write the header
            byte[] magicBytes = Document.MagicBytes;
            stream.Write(magicBytes, 0, magicBytes.Length);
            byte[] headerBytes = Encoding.UTF8.GetBytes(headerText);
            stream.WriteByte((byte)(headerBytes.Length & 0xff));
            stream.WriteByte((byte)((headerBytes.Length & 0xff00) >> 8));
            stream.WriteByte((byte)((headerBytes.Length & 0xff0000) >> 16));
            stream.Write(headerBytes, 0, headerBytes.Length);
            stream.Flush();

            // Copy version info
            this.savedWith = PdnInfo.GetVersion();

            // Write 0x00, 0x01 to indicate normal .NET serialized data
            stream.WriteByte(0x00);
            stream.WriteByte(0x01);

            // Write the remainder of the file (gzip compressed)
            SiphonStream siphonStream = new SiphonStream(stream);
            BinaryFormatter formatter = new BinaryFormatter();
            DeferredFormatter deferred = new DeferredFormatter(true, null);
            SaveProgressRelay relay = new SaveProgressRelay(deferred, callback);
            formatter.Context = new StreamingContext(formatter.Context.State, deferred);
            formatter.Serialize(siphonStream, this);
            deferred.FinishSerialization(siphonStream);

            stream.Flush();
        }
        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;
        }
Beispiel #6
0
        public override void OnEnteredState()
        {
            this.zipTempName = Path.GetTempFileName() + ".zip";

            try
            {
                bool getFull;

                if (SystemLayer.OS.IsDotNetVersionInstalled(
                        downloadMe.NetFxMajorVersion,
                        downloadMe.NetFxMinorVersion,
                        downloadMe.NetFxServicePack,
                        true))
                {
                    getFull = false;
                }
                else
                {
                    getFull = true;
                }

                OnProgress(0.0);

                FileStream zipFileWrite = new FileStream(zipTempName, FileMode.Create, FileAccess.Write, FileShare.Read);

                try
                {
                    // we need to wrap the zipFileWrite in a SiphonStream so that we can
                    // Abort() it externally
                    SiphonStream monitorStream = new SiphonStream(zipFileWrite);
                    this.abortMeStream = monitorStream;

                    ProgressEventHandler progressCallback =
                        delegate(object sender, ProgressEventArgs e)
                        {
                            OnProgress(e.Percent);
                        };

                    string url;

                    url = downloadMe.ChooseDownloadUrl(getFull);
                    SystemLayer.Tracing.Ping("Chosen mirror url: " + url);

                    Utility.DownloadFile(new Uri(url), monitorStream, progressCallback);
                    monitorStream.Flush();

                    this.abortMeStream = null;
                    monitorStream = null;
                }

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

                StateMachine.QueueInput(PrivateInput.GoToExtracting);
            }

            catch (Exception ex)
            {
                this.exception = ex;

                if (this.AbortRequested)
                {
                    StateMachine.QueueInput(PrivateInput.GoToAborted);
                }
                else
                {
                    this.exception = ex;
                    StateMachine.QueueInput(PrivateInput.GoToError);
                }
            }
        }