Esempio n. 1
0
        private static void ExecuteTaskCore(OperationListBaseModel Model)
        {
            CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                Model.UpdateStatus(OperationStatus.Processing);
            }).AsTask().Wait();

            switch (Model)
            {
            case OperationListCompressionModel CModel:
            {
                try
                {
                    switch (CModel.Type)
                    {
                    case CompressionType.Zip:
                    {
                        CompressionUtil.CreateZipAsync(CModel.FromPath, CModel.ToPath, (int)CModel.Level, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();

                        break;
                    }

                    case CompressionType.Tar:
                    {
                        CompressionUtil.CreateTarAsync(CModel.FromPath, CModel.ToPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();

                        break;
                    }

                    case CompressionType.Gzip:
                    {
                        if (CModel.FromPath.Length == 1)
                        {
                            CompressionUtil.CreateGzipAsync(CModel.FromPath.First(), CModel.ToPath, (int)CModel.Level, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                        }
                        else
                        {
                            throw new ArgumentException("Gzip could not contains more than one item");
                        }

                        break;
                    }
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedCompression_Content"));
                        }).AsTask().Wait();
                }
                catch (FileNotFoundException)
                {
                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_LocateFileFailure_Content"));
                        }).AsTask().Wait();
                }
                catch (Exception ex)
                {
                    LogTracer.Log(ex, "Compression error");

                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CompressionError_Content"));
                        }).AsTask().Wait();
                }

                break;
            }

            case OperationListDecompressionModel DModel:
            {
                try
                {
                    CompressionUtil.SetEncoding(DModel.Encoding);

                    if (Model.FromPath.All((Item) => Path.GetExtension(Item).Equals(".zip", StringComparison.OrdinalIgnoreCase)))
                    {
                        if (string.IsNullOrEmpty(Model.ToPath))
                        {
                            CompressionUtil.ExtractZipAsync(Model.FromPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();
                        }
                        else
                        {
                            CompressionUtil.ExtractZipAsync(Model.FromPath, Model.ToPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();
                        }
                    }
                    else if (Model.FromPath.All((Item) => Path.GetExtension(Item).Equals(".tar", StringComparison.OrdinalIgnoreCase)))
                    {
                        if (string.IsNullOrEmpty(Model.ToPath))
                        {
                            CompressionUtil.ExtractTarAsync(Model.FromPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();
                        }
                        else
                        {
                            CompressionUtil.ExtractTarAsync(Model.FromPath, Model.ToPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();
                        }
                    }
                    else if (Model.FromPath.All((Item) => Path.GetExtension(Item).Equals(".gz", StringComparison.OrdinalIgnoreCase)))
                    {
                        if (string.IsNullOrEmpty(Model.ToPath))
                        {
                            CompressionUtil.ExtractGZipAsync(Model.FromPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();
                        }
                        else
                        {
                            CompressionUtil.ExtractGZipAsync(Model.FromPath, Model.ToPath, (s, e) =>
                                {
                                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                    {
                                        Model.UpdateProgress(e.ProgressPercentage);
                                    }).AsTask().Wait();
                                }).Wait();
                        }
                    }
                    else
                    {
                        throw new Exception(Globalization.GetString("QueueDialog_FileTypeIncorrect_Content"));
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedDecompression_Content"));
                        }).AsTask().Wait();
                }
                catch (NotImplementedException)
                {
                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CanNotDecompressEncrypted_Content"));
                        }).AsTask().Wait();
                }
                catch (FileNotFoundException)
                {
                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_LocateFileFailure_Content"));
                        }).AsTask().Wait();
                }
                catch (Exception ex)
                {
                    LogTracer.Log(ex, "Decompression error");

                    CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                        {
                            Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_DecompressionError_Content"));
                        }).AsTask().Wait();
                }

                break;
            }
            }

            CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
            {
                if (Model.Status != OperationStatus.Error)
                {
                    Model.UpdateProgress(100);
                    Model.UpdateStatus(OperationStatus.Complete);
                }
            }).AsTask().Wait();
        }
Esempio n. 2
0
        private static void ExecuteSubTaskCore(OperationListBaseModel Model)
        {
            Interlocked.Increment(ref RunningTaskCounter);

            try
            {
                CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    Model.UpdateStatus(OperationStatus.Preparing);
                }).AsTask().Wait();

                Model.PrepareSizeDataAsync().Wait();

                CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    Model.UpdateStatus(OperationStatus.Processing);
                    Model.UpdateProgress(0);
                }).AsTask().Wait();

                switch (Model)
                {
                case OperationListRemoteModel:
                {
                    using (FullTrustProcessController.ExclusiveUsage Exclusive = FullTrustProcessController.GetAvailableController().Result)
                    {
                        if (!Exclusive.Controller.PasteRemoteFile(Model.ToPath).Result)
                        {
                            CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                {
                                    Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CopyFailUnexpectError_Content"));
                                }).AsTask().Wait();
                        }
                    }

                    break;
                }

                case OperationListCopyModel:
                {
                    try
                    {
                        using (FullTrustProcessController.ExclusiveUsage Exclusive = FullTrustProcessController.GetAvailableController().Result)
                        {
                            Exclusive.Controller.CopyAsync(Model.FromPath, Model.ToPath, ProgressHandler: (s, e) =>
                                {
                                    Model.UpdateProgress(e.ProgressPercentage);
                                }).Wait();
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CopyFailForNotExist_Content"));
                            }).AsTask().Wait();
                    }
                    catch (InvalidOperationException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedPaste_Content"));
                            }).AsTask().Wait();
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "Copy failed for unexpected error");

                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CopyFailUnexpectError_Content"));
                            }).AsTask().Wait();
                    }

                    break;
                }

                case OperationListMoveModel:
                {
                    try
                    {
                        using (FullTrustProcessController.ExclusiveUsage Exclusive = FullTrustProcessController.GetAvailableController().Result)
                        {
                            Exclusive.Controller.MoveAsync(Model.FromPath, Model.ToPath, ProgressHandler: (s, e) =>
                                {
                                    Model.UpdateProgress(e.ProgressPercentage);
                                }).Wait();
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_MoveFailForNotExist_Content"));
                            }).AsTask().Wait();
                    }
                    catch (FileCaputureException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_Item_Captured_Content"));
                            }).AsTask().Wait();
                    }
                    catch (InvalidOperationException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedPaste_Content"));
                            }).AsTask().Wait();
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "Move failed for unexpected error");

                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_MoveFailUnexpectError_Content"));
                            }).AsTask().Wait();
                    }

                    break;
                }

                case OperationListDeleteModel DeleteModel:
                {
                    try
                    {
                        using (FullTrustProcessController.ExclusiveUsage Exclusive = FullTrustProcessController.GetAvailableController().Result)
                        {
                            Exclusive.Controller.DeleteAsync(DeleteModel.FromPath, DeleteModel.IsPermanentDelete, ProgressHandler: (s, e) =>
                                {
                                    Model.UpdateProgress(e.ProgressPercentage);
                                }).Wait();
                        }
                    }
                    catch (FileNotFoundException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_DeleteItemError_Content"));
                            }).AsTask().Wait();
                    }
                    catch (FileCaputureException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_Item_Captured_Content"));
                            }).AsTask().Wait();
                    }
                    catch (InvalidOperationException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedDelete_Content"));
                            }).AsTask().Wait();
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "Delete failed for unexpected error");

                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_DeleteFailUnexpectError_Content"));
                            }).AsTask().Wait();
                    }

                    break;
                }

                case OperationListUndoModel UndoModel:
                {
                    try
                    {
                        using (FullTrustProcessController.ExclusiveUsage Exclusive = FullTrustProcessController.GetAvailableController().Result)
                        {
                            switch (UndoModel.UndoOperationKind)
                            {
                            case OperationKind.Copy:
                            {
                                Exclusive.Controller.DeleteAsync(Model.FromPath, true, true, (s, e) =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).Wait();

                                break;
                            }

                            case OperationKind.Move:
                            {
                                Exclusive.Controller.MoveAsync(Model.FromPath, Model.ToPath, true, (s, e) =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).Wait();

                                break;
                            }

                            case OperationKind.Delete:
                            {
                                if (!Exclusive.Controller.RestoreItemInRecycleBinAsync(Model.FromPath).Result)
                                {
                                    throw new Exception();
                                }

                                break;
                            }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "Undo failed for unexpected error");

                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UndoFailure_Content"));
                            }).AsTask().Wait();
                    }

                    break;
                }

                case OperationListCompressionModel CModel:
                {
                    try
                    {
                        switch (CModel.Type)
                        {
                        case CompressionType.Zip:
                        {
                            CompressionUtil.CreateZipAsync(CModel.FromPath, CModel.ToPath, (int)CModel.Level, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();

                            break;
                        }

                        case CompressionType.Tar:
                        {
                            CompressionUtil.CreateTarAsync(CModel.FromPath, CModel.ToPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();

                            break;
                        }

                        case CompressionType.Gzip:
                        {
                            if (CModel.FromPath.Length == 1)
                            {
                                CompressionUtil.CreateGzipAsync(CModel.FromPath.First(), CModel.ToPath, (int)CModel.Level, (s, e) =>
                                        {
                                            CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                            {
                                                Model.UpdateProgress(e.ProgressPercentage);
                                            }).AsTask().Wait();
                                        }).Wait();
                            }
                            else
                            {
                                throw new ArgumentException("Gzip could not contains more than one item");
                            }

                            break;
                        }
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedCompression_Content"));
                            }).AsTask().Wait();
                    }
                    catch (FileNotFoundException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_LocateFileFailure_Content"));
                            }).AsTask().Wait();
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "Compression error");

                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CompressionError_Content"));
                            }).AsTask().Wait();
                    }

                    break;
                }

                case OperationListDecompressionModel DModel:
                {
                    try
                    {
                        CompressionUtil.SetEncoding(DModel.Encoding);

                        if (Model.FromPath.All((Item) => Path.GetExtension(Item).Equals(".zip", StringComparison.OrdinalIgnoreCase)))
                        {
                            if (string.IsNullOrEmpty(Model.ToPath))
                            {
                                CompressionUtil.ExtractZipAsync(Model.FromPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                            }
                            else
                            {
                                CompressionUtil.ExtractZipAsync(Model.FromPath, Model.ToPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                            }
                        }
                        else if (Model.FromPath.All((Item) => Path.GetExtension(Item).Equals(".tar", StringComparison.OrdinalIgnoreCase)))
                        {
                            if (string.IsNullOrEmpty(Model.ToPath))
                            {
                                CompressionUtil.ExtractTarAsync(Model.FromPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                            }
                            else
                            {
                                CompressionUtil.ExtractTarAsync(Model.FromPath, Model.ToPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                            }
                        }
                        else if (Model.FromPath.All((Item) => Path.GetExtension(Item).Equals(".gz", StringComparison.OrdinalIgnoreCase)))
                        {
                            if (string.IsNullOrEmpty(Model.ToPath))
                            {
                                CompressionUtil.ExtractGZipAsync(Model.FromPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                            }
                            else
                            {
                                CompressionUtil.ExtractGZipAsync(Model.FromPath, Model.ToPath, (s, e) =>
                                    {
                                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                                        {
                                            Model.UpdateProgress(e.ProgressPercentage);
                                        }).AsTask().Wait();
                                    }).Wait();
                            }
                        }
                        else
                        {
                            throw new Exception(Globalization.GetString("QueueDialog_FileTypeIncorrect_Content"));
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_UnauthorizedDecompression_Content"));
                            }).AsTask().Wait();
                    }
                    catch (NotImplementedException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_CanNotDecompressEncrypted_Content"));
                            }).AsTask().Wait();
                    }
                    catch (FileNotFoundException)
                    {
                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_LocateFileFailure_Content"));
                            }).AsTask().Wait();
                    }
                    catch (Exception ex)
                    {
                        LogTracer.Log(ex, "Decompression error");

                        CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                            {
                                Model.UpdateStatus(OperationStatus.Error, Globalization.GetString("QueueDialog_DecompressionError_Content"));
                            }).AsTask().Wait();
                    }

                    break;
                }
                }

                CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Low, () =>
                {
                    if (Model.Status != OperationStatus.Error)
                    {
                        Model.UpdateProgress(100);
                        Model.UpdateStatus(OperationStatus.Complete);
                    }
                }).AsTask().Wait();
            }
            catch (Exception ex)
            {
                LogTracer.Log(ex, "A subthread in Task List threw an exception");
            }
            finally
            {
                Interlocked.Decrement(ref RunningTaskCounter);
            }
        }