Example #1
0
        private void refreshScheduleTable()
        {
            Microsoft.Win32.TaskScheduler.Task[] tasks = routineScheduler.getSchedule();
            tabPage2.Controls.Clear();
            tasks.OrderBy(i => i.NextRunTime.Ticks).ToArray();
            Point baseLocation         = new Point(8, 34);
            Point panelSize            = new Point(416, 35);
            Point routineNameLocation  = new Point(3, 11);
            Point runtimeLocation      = new Point(114, 11);
            Point startTimeLocation    = new Point(201, 11);
            Point deleteButtonLocation = new Point(363, 3);
            Panel outerPanel           = new Panel();

            outerPanel.Size       = new Size(426, 215);
            outerPanel.Location   = baseLocation;
            outerPanel.AutoScroll = true;
            for (int j = 0; j < tasks.Length; j++)
            {
                Panel innerPanel = new Panel();
                innerPanel.Size     = new Size(416, 35);
                innerPanel.Location = new Point(0, 40 * j);

                Label  taskName = new Label();
                string taskDesc = tasks[j].Definition.RegistrationInfo.Description;
                taskName.Text      = taskDesc.Substring(taskDesc.LastIndexOf("\\") + 1);
                taskName.Location  = routineNameLocation;
                taskName.ForeColor = Color.White;

                Label taskRuntimeLabel = new Label();
                taskRuntimeLabel.AutoSize = true;
                string taskRoutinePath = routineScheduler.getRoutinePathFromTask(tasks[j]);
                double taskRuntime     = routineScheduler.getRoutineRuntime(taskRoutinePath);
                taskRuntimeLabel.Location = runtimeLocation;
                int minutes = (int)(taskRuntime / 60000);
                int seconds = (int)(taskRuntime % 60000) / 1000;
                taskRuntimeLabel.Text      = minutes + "m " + seconds + "s";
                taskRuntimeLabel.ForeColor = Color.White;

                Label taskStartTimeLabel = new Label();
                taskStartTimeLabel.AutoSize  = true;
                taskStartTimeLabel.Location  = startTimeLocation;
                taskStartTimeLabel.Text      = tasks[j].NextRunTime.ToString();
                taskStartTimeLabel.ForeColor = Color.White;
                //  taskStartTimeLabel.Anchor = AnchorStyles.Left;

                Button deleteTaskButton = new Button();
                Microsoft.Win32.TaskScheduler.Task taskToDelete = tasks[j];
                deleteTaskButton.Click += new EventHandler((object sender, EventArgs e) =>
                {
                    routineScheduler.deleteTask(taskToDelete);
                    refreshScheduleTable();
                }
                                                           );
                deleteTaskButton.Location  = deleteButtonLocation;
                deleteTaskButton.Size      = new Size(50, 29);
                deleteTaskButton.ForeColor = Color.White;
                deleteTaskButton.BackColor = Color.FromArgb(255, 89, 0);
                deleteTaskButton.Text      = "Delete";
                deleteTaskButton.FlatStyle = FlatStyle.Flat;


                innerPanel.Controls.Add(taskName);
                innerPanel.Controls.Add(taskRuntimeLabel);
                innerPanel.Controls.Add(taskStartTimeLabel);
                innerPanel.Controls.Add(deleteTaskButton);

                outerPanel.Controls.Add(innerPanel);
            }
            tabPage2.Controls.Add(outerPanel);
        }
        /// <summary>
        /// Validates the Auto Dark Mode autostart entries for correctness
        /// </summary>
        /// <returns>true if the state is valid, false if the state was invalid</returns>
        public static ApiResponse Validate(bool alwaysValidate)
        {
            if (!builder.Config.Autostart.Validate && !alwaysValidate)
            {
                return(new()
                {
                    StatusCode = StatusCode.Ok,
                    Message = "validation disabled"
                });
            }
            //check if a logon task if used, needs to be reset if the folder is empty or the path differs from the execution base directory
            if (builder.Config.Tunable.UseLogonTask)
            {
                try
                {
                    string autostartPath = RegistryHandler.GetAutostartPath();
                    using Microsoft.Win32.TaskScheduler.Task logonTask = TaskSchdHandler.GetLogonTask();
                    if (logonTask == null)
                    {
                        Logger.Warn("autostart validation failed, missing logon task folder. fixing autostart");
                        ApiResponse result = AddAutostart(modified: true);
                        result.Details += "\nvalidation mode: recreate task (missing folder)";
                        return(result);
                    }
                    else if (logonTask.Definition.Actions.First().ToString().Trim() != Extensions.ExecutionPath)
                    {
                        Logger.Warn("autostart validation failed, wrong execution path. fixing autostart");
                        ApiResponse result = AddAutostart(modified: true);
                        result.Details += "\nvalidation mode: recreate task (wrong path)";
                        return(result);
                    }
                    else if (autostartPath != null)
                    {
                        Logger.Warn("autostart validation failed, wrong execution path. fixing autostart");
                        ApiResponse result = AddAutostart(modified: true);
                        result.Details += "\nvalidation mode: recreate task (regkey still active)";
                        return(result);
                    }
                }
                catch (Exception ex)
                {
                    string msg = "error during autostart validation:";
                    Logger.Error(ex, msg);
                    return(new()
                    {
                        StatusCode = StatusCode.Err,
                        Message = msg,
                        Details = $"Exception:\nMessage: {ex.Message}\nSource:{ex.Source}"
                    });
                }
            }
            //check if registry keys are used, needs to be reset if the key is missing or the path differs from the execution base directory
            else
            {
                bool   recreate = false;
                string reason   = "";
                try
                {
                    using Microsoft.Win32.TaskScheduler.Task logonTask = TaskSchdHandler.GetLogonTask();
                    if (logonTask != null)
                    {
                        recreate = true;
                        reason   = "logon task active in registry mode";
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "could not determine task scheduler autostart state:");
                }
                if (!recreate)
                {
                    string autostartPath = RegistryHandler.GetAutostartPath();
                    if (autostartPath == null)
                    {
                        reason   = "missing entry";
                        recreate = true;
                    }
                    else
                    {
                        autostartPath = autostartPath.Replace("\"", "");
                        if (!autostartPath.Contains(Extensions.ExecutionPath))
                        {
                            reason   = "wrong path";
                            recreate = true;
                        }
                    }
                }

                if (recreate)
                {
                    Logger.Warn($"autostart validation failed ({reason}), recreate autostart registry keys");
                    ApiResponse result = AddAutostart(modified: true);
                    result.Details += $"\nvalidation mode: recreate regkey ({reason})";
                    return(result);
                }
            }
            return(new()
            {
                StatusCode = StatusCode.Ok,
                Message = "autostart entries valid"
            });
        }
Example #3
0
        private void btnSaveProfile_Click(object sender, EventArgs e)
        {
            if (cbProfiles.Text == "")
            {
                MessageBox.Show("Please enter a profile name before attempting to save the profile.");
                cbProfiles.Focus();

                return;
            }

            if (tbECID.Text.Length == 0)
            {
                MessageBox.Show("You must enter a device ECID before attempting to save the profile.");
                tbECID.Focus();

                return;
            }


            if (chkSchedulingEnabled.Checked)
            {
                using (Microsoft.Win32.TaskScheduler.TaskService ts = new Microsoft.Win32.TaskScheduler.TaskService()) {
                    Microsoft.Win32.TaskScheduler.Task t = ts.GetTask(frmAutosaves.taskName);

                    if (t == null)
                    {
                        MessageBox.Show("You selected to automatically check and save blobs for this profile, but you haven't set up autochecking. If you would like to use the autocheck feature, please enable it now.");

                        frmAutosaves formAutosaves = new frmAutosaves();
                        formAutosaves.ShowDialog();
                    }
                }
            }


            try {
                string identifier = "";

                if (cbModel.Text.Length > 0)
                {
                    identifier = cbModel.Text.Substring(cbModel.Text.IndexOf("(") + 1);
                    identifier = identifier.Substring(0, identifier.LastIndexOf(")"));
                }


                StringBuilder sb = new StringBuilder();

                sb.AppendLine("SchedulingEnabled:" + chkSchedulingEnabled.Checked);

                sb.AppendLine("ECID:" + tbECID.Text);
                sb.AppendLine("DeviceType:" + cbDevice.Text);
                sb.AppendLine("Identifier:" + identifier);
                sb.AppendLine("InternalName:" + cbBoardConfig.Text);
                sb.AppendLine("Apnonce:" + cbApnonce.Text);

                sb.Append("ApnonceList:");

                for (int i = 0; i < cbApnonce.Items.Count; i++)
                {
                    string apnonce = cbApnonce.GetItemText(cbApnonce.Items[i]);

                    if (apnonce.Length == 0)
                    {
                        continue;
                    }

                    sb.Append(";" + apnonce); // Preceeding the apnonce with a semicolon will handle adding the first blank item automatically
                }


                File.WriteAllText(Path.Combine(pathProfiles, cbProfiles.Text + ".txt"), sb.ToString());


                PopupNotifier popup = PopupHelper.GeneratePopup("The profile was saved successfully.");
                popup.Popup();
            } catch (Exception ex) {
                MessageBox.Show("Error: " + ex.Message + "\n\n" + ex.StackTrace);
            }


            RefreshProfiles();
        }
 public static ApiResponse GetAutostartState()
 {
     if (builder.Config.Tunable.UseLogonTask)
     {
         try
         {
             using Microsoft.Win32.TaskScheduler.Task logonTask = TaskSchdHandler.GetLogonTask();
             if (logonTask != null)
             {
                 ApiResponse response = new()
                 {
                     StatusCode = StatusCode.AutostartTask,
                     Message    = $"Enabled: {logonTask.Enabled}",
                     Details    = logonTask.Definition.Actions.ToString()
                 };
                 return(response);
             }
         }
         catch (Exception ex)
         {
             string msg = "error while getting logon task state:";
             Logger.Error(ex, msg);
             return(new()
             {
                 StatusCode = StatusCode.Err,
                 Message = msg,
                 Details = $"Exception:\nMessage: {ex.Message}\nSource:{ex.Source}"
             });
         }
     }
     else
     {
         string autostartPath = RegistryHandler.GetAutostartPath();
         if (autostartPath != null)
         {
             bool approved = RegistryHandler.IsAutostartApproved();
             if (approved)
             {
                 return(new()
                 {
                     StatusCode = StatusCode.AutostartRegistryEntry,
                     Message = $"Enabled",
                     Details = autostartPath.Replace("\"", "")
                 });
             }
             else
             {
                 return(new()
                 {
                     StatusCode = StatusCode.AutostartRegistryEntry,
                     Message = $"Disabled",
                     Details = autostartPath.Replace("\"", "")
                 });
             }
         }
     }
     return(new ApiResponse()
     {
         StatusCode = StatusCode.Disabled,
         Message = "no auto start entries found"
     });
 }