コード例 #1
0
ファイル: TaskManager.cs プロジェクト: wazerstar/ShareX
        private static void task_TaskCompleted(WorkerTask task)
        {
            try
            {
                task.KeepImage = false;

                if (task != null)
                {
                    if (task.RequestSettingUpdate)
                    {
                        Program.MainForm.UpdateCheckStates();
                    }

                    TaskInfo info = task.Info;

                    if (info != null && info.Result != null)
                    {
                        ListViewItem lvi = FindListViewItem(task);

                        if (task.Status == TaskStatus.Stopped)
                        {
                            DebugHelper.WriteLine($"Task stopped. Filename: {info.FileName}");

                            if (lvi != null)
                            {
                                lvi.Text             = info.FileName;
                                lvi.SubItems[1].Text = info.Status;
                                lvi.ImageIndex       = 2;
                            }
                        }
                        else if (task.Status == TaskStatus.Failed)
                        {
                            string errors = string.Join("\r\n\r\n", info.Result.Errors.ToArray());

                            DebugHelper.WriteLine($"Task failed. Filename: {info.FileName}, Errors:\r\n{errors}");

                            if (lvi != null)
                            {
                                lvi.SubItems[1].Text = info.Status;
                                lvi.SubItems[6].Text = "";
                                lvi.ImageIndex       = 1;
                            }

                            if (!info.TaskSettings.AdvancedSettings.DisableNotifications)
                            {
                                if (info.TaskSettings.GeneralSettings.PlaySoundAfterUpload)
                                {
                                    TaskHelpers.PlayErrorSound(info.TaskSettings);
                                }

                                if (info.TaskSettings.GeneralSettings.PopUpNotification != PopUpNotificationType.None && !string.IsNullOrEmpty(errors) &&
                                    (!info.TaskSettings.AdvancedSettings.DisableNotificationsOnFullscreen || !CaptureHelpers.IsActiveWindowFullscreen()))
                                {
                                    TaskHelpers.ShowBalloonTip(errors, ToolTipIcon.Error, 5000, "ShareX - " + Resources.TaskManager_task_UploadCompleted_Error);
                                }
                            }
                        }
                        else
                        {
                            DebugHelper.WriteLine($"Task completed. Filename: {info.FileName}, Duration: {(long)info.TaskDuration.TotalMilliseconds} ms");

                            string result = info.Result.ToString();

                            if (string.IsNullOrEmpty(result) && !string.IsNullOrEmpty(info.FilePath))
                            {
                                result = info.FilePath;
                            }

                            if (lvi != null)
                            {
                                lvi.Text             = info.FileName;
                                lvi.SubItems[1].Text = info.Status;
                                lvi.ImageIndex       = 2;

                                if (!string.IsNullOrEmpty(result))
                                {
                                    lvi.SubItems[6].Text = result;
                                }
                            }

                            if (!task.StopRequested && !string.IsNullOrEmpty(result))
                            {
                                if (Program.Settings.HistorySaveTasks && (!Program.Settings.HistoryCheckURL ||
                                                                          (!string.IsNullOrEmpty(info.Result.URL) || !string.IsNullOrEmpty(info.Result.ShortenedURL))))
                                {
                                    HistoryItem historyItem = info.GetHistoryItem();
                                    AppendHistoryItemAsync(historyItem);
                                }

                                RecentManager.Add(task);

                                if (!info.TaskSettings.AdvancedSettings.DisableNotifications && info.Job != TaskJob.ShareURL)
                                {
                                    if (info.TaskSettings.GeneralSettings.PlaySoundAfterUpload)
                                    {
                                        TaskHelpers.PlayTaskCompleteSound(info.TaskSettings);
                                    }

                                    if (!string.IsNullOrEmpty(info.TaskSettings.AdvancedSettings.BalloonTipContentFormat))
                                    {
                                        result = new UploadInfoParser().Parse(info, info.TaskSettings.AdvancedSettings.BalloonTipContentFormat);
                                    }

                                    if (!string.IsNullOrEmpty(result) &&
                                        (!info.TaskSettings.AdvancedSettings.DisableNotificationsOnFullscreen || !CaptureHelpers.IsActiveWindowFullscreen()))
                                    {
                                        switch (info.TaskSettings.GeneralSettings.PopUpNotification)
                                        {
                                        case PopUpNotificationType.BalloonTip:
                                            BalloonTipAction action = new BalloonTipAction()
                                            {
                                                ClickAction = BalloonTipClickAction.OpenURL,
                                                Text        = result
                                            };

                                            TaskHelpers.ShowBalloonTip(result, ToolTipIcon.Info, 5000,
                                                                       "ShareX - " + Resources.TaskManager_task_UploadCompleted_ShareX___Task_completed, action);
                                            break;

                                        case PopUpNotificationType.ToastNotification:
                                            task.KeepImage = true;

                                            NotificationFormConfig toastConfig = new NotificationFormConfig()
                                            {
                                                LeftClickAction   = info.TaskSettings.AdvancedSettings.ToastWindowClickAction,
                                                RightClickAction  = info.TaskSettings.AdvancedSettings.ToastWindowRightClickAction,
                                                MiddleClickAction = info.TaskSettings.AdvancedSettings.ToastWindowMiddleClickAction,
                                                FilePath          = info.FilePath,
                                                Image             = task.Image,
                                                Text = "ShareX - " + Resources.TaskManager_task_UploadCompleted_ShareX___Task_completed + "\r\n" + result,
                                                URL  = result
                                            };
                                            NotificationForm.Show((int)(info.TaskSettings.AdvancedSettings.ToastWindowDuration * 1000),
                                                                  (int)(info.TaskSettings.AdvancedSettings.ToastWindowFadeDuration * 1000),
                                                                  info.TaskSettings.AdvancedSettings.ToastWindowPlacement,
                                                                  info.TaskSettings.AdvancedSettings.ToastWindowSize, toastConfig);
                                            break;
                                        }

                                        if (info.TaskSettings.AfterUploadJob.HasFlag(AfterUploadTasks.ShowAfterUploadWindow) && info.IsUploadJob)
                                        {
                                            AfterUploadForm dlg = new AfterUploadForm(info);
                                            NativeMethods.ShowWindow(dlg.Handle, (int)WindowShowStyle.ShowNoActivate);
                                        }
                                    }
                                }
                            }
                        }

                        if (lvi != null)
                        {
                            lvi.EnsureVisible();

                            if (Program.Settings.AutoSelectLastCompletedTask)
                            {
                                ListViewControl.SelectSingle(lvi);
                            }
                        }

                        if (Program.Settings.TaskViewMode == TaskViewMode.ThumbnailView)
                        {
                            TaskView.UpdateProgressVisible(task, false);
                        }
                    }
                }
            }
            finally
            {
                if (!IsBusy && Program.CLI.IsCommandExist("AutoClose"))
                {
                    Application.Exit();
                }
                else
                {
                    StartTasks();
                    UpdateProgressUI();

                    if (Program.Settings.SaveSettingsAfterTaskCompleted && !IsBusy)
                    {
                        SettingManager.SaveAllSettingsAsync();
                    }
                }
            }
        }