Пример #1
0
        private void WcfConnect(ProgressDialog prg)
        {
            // set up callback to track tasks loading progress:
            var progressCbk = new ProgressCallback(delegate(int from, int to, int current)
            {
                prg.SetProgress(20 + (current * 80) / (to - from), string.Format("Loading task {0} of {1}...", current + 1, to - from));
            });

            prg.SetProgress(10, "Connecting to service...");
            WcfDisconnect();

            try
            {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(C1ReportsSchedulerService.Constants.RegKey_Root))
                {
                    string baseAddr = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointBaseAddress) as string;
                    string relAddr  = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointRelAddress) as string;
                    // todo: check how base and rel addresses are combined:
#if wcf_http
                    WcfEndpointAddress = string.Format("{0}/{1}", baseAddr, relAddr);
#else
                    WcfEndpointAddress = string.Format("{0}/{1}", baseAddr, relAddr);
#endif
                }
                LoadTasks(new TaskListHolderClient(this, WcfEndpointAddress, progressCbk));
            }
            catch (Exception ex)
            {
                WinUtil.ShowError(string.Format("Could not get service endpoint address from registry: {0}", ex.Message));
            }
        }
Пример #2
0
        private void StartStopSchedule()
        {
            TaskBase task = CurrentTask;

            if (task == null)
            {
                return;
            }
            bool start = !task.Schedule.Enabled;

            if (start)
            {
                System.Diagnostics.Debug.Assert(task.State == TaskState.Ready);
                if (task.Check(true))
                {
                    task.Schedule.Enabled = true;
                }
                else
                {
                    WinUtil.ShowError(string.Format("Could not start schedule - task has errors: {0}", task.Message));
                }
            }
            else
            {
                task.Schedule.Enabled = false;
            }
        }
Пример #3
0
        public static void ShowError(string message)
        {
#if SERVICE
            C1ReportsSchedulerService.Logger.Log("ERROR", message);
#else
            WinUtil.ShowError(message);
#endif
        }
Пример #4
0
 private void ConnectToService(ProgressDialog prg)
 {
     System.Diagnostics.Debug.Assert(CanConnectToService(false));
     try
     {
         WcfConnect(prg);
     }
     catch (Exception ex)
     {
         WinUtil.ShowError(string.Format("Cannot connect to service {0}: {1}.", C1ReportsSchedulerService.Constants.Name, ex.Message));
     }
 }
Пример #5
0
 internal void FileOpen(string fileName)
 {
     try
     {
         LoadTasks(TaskListHolder.Load(fileName));
         C1rsconfFileName = fileName;
     }
     catch (Exception ex)
     {
         WinUtil.ShowError(ex.Message);
         C1rsconfFileName = null;
     }
 }
Пример #6
0
        static public void Load(MainForm mainForm)
        {
            UiSettings uiSettings = Properties.Settings.Default.UiSettings;

            if (uiSettings != null)
            {
                mainForm.UiSettings = uiSettings;
            }

            mainForm.WcfEndpointAddress = Properties.Settings.Default.EndpointAddress;

            if (Properties.Settings.Default.ClientMode)
            {
                if (!mainForm.ClientMode)
                {
                    mainForm.ConnectToServiceOnLoad();
                }
            }
            else
            {
                TaskListStore store = Properties.Settings.Default.TaskListStore;
                switch (store)
                {
                case TaskListStore.AppConfigFile:
                    string tasksXml = Properties.Settings.Default.C1ReportsSchedulerTasks;
                    if (!string.IsNullOrEmpty(tasksXml))
                    {
                        try
                        {
                            using (MemoryStream ms = new MemoryStream(UTF8Encoding.Default.GetBytes(tasksXml)))
                                mainForm.LoadTasks(TaskListHolder.Load(ms));
                        }
                        catch (Exception ex)
                        {
                            WinUtil.ShowError(string.Format("Error loading tasks from config file: {0}", ex.Message));
                        }
                    }
                    break;

                case TaskListStore.XmlFile:
                    mainForm.FileOpen(Properties.Settings.Default.C1rsconfFileName);
                    break;

                default:
                    System.Diagnostics.Debug.Assert(false);
                    break;
                }
            }
        }
Пример #7
0
        private bool UiTaskSelectFileName(TaskBase task)
        {
            try
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Filter =
                        "Report definition files (*.xml)|*.xml|C1DX documents(*.c1dx)|*.c1dx|C1D documents (*.c1d)|*.c1d|Executable files (*.exe)|*.exe|All files (*.*)|*.*";
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        task.FileName = ofd.FileName;
                        switch (Path.GetExtension(ofd.FileName).ToLowerInvariant())
                        {
                        case ".xml":
                            if (task.ReportKind == ReportKind.C1dDocument)
                            {
                                task.ReportKind = ReportKind.XmlReport;
                            }
                            break;

                        case ".c1d":
                        case ".c1dx":
                            if (task.ReportKind != ReportKind.C1dDocument)
                            {
                                task.ReportKind = ReportKind.C1dDocument;
                            }
                            break;

                        case ".exe":
                            if (task.ReportKind != ReportKind.Program)
                            {
                                task.ReportKind = ReportKind.Program;
                            }
                            break;
                        }
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
            }
            catch (Exception ex)
            {
                WinUtil.ShowError(ex.Message);
                return(false);
            }
        }
Пример #8
0
        private void ServiceCall(ServiceControllerStatus desiredStatus, string actionName, ProgressDialog prg)
        {
            prg.SetProgress(10, string.Format("{0} {1} service...", actionName, C1ReportsSchedulerService.Constants.Name));
            using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name))
            {
                try
                {
                    if (sc.Status == desiredStatus)
                    {
                        return;
                    }
                    switch (desiredStatus)
                    {
                    case ServiceControllerStatus.Paused:
                        sc.Pause();
                        break;

                    case ServiceControllerStatus.Running:
                        if (sc.Status == ServiceControllerStatus.Paused)
                        {
                            sc.Continue();
                        }
                        else
                        {
                            sc.Start();
                        }
                        break;

                    case ServiceControllerStatus.Stopped:
                        sc.Stop();
                        break;

                    default:
                        System.Diagnostics.Debug.Assert(false);
                        break;
                    }
                    WaitForStatus(sc, desiredStatus, prg);
                }
                catch (Exception ex)
                {
                    WinUtil.ShowError(string.Format("Error: {0}", ex.Message));
                }
                finally
                {
                    sc.Close();
                }
            }
        }
Пример #9
0
        private bool UiActionSelectFileName(ActionBase action)
        {
            try
            {
                switch (action.Kind)
                {
                case ActionKind.Export:
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "All files (*.*)|*.*";
                    if (!string.IsNullOrEmpty(action.ExportFormatKey))
                    {
                        sfd.Filter = string.Format("{0} (*.{1})|*.{1}|", action.ExportFormatName, action.ExportDefaultExtension) + sfd.Filter;
                    }
                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        action.OutputFileName = sfd.FileName;
                        return(true);
                    }
                    break;

                case ActionKind.Print:
                    PrintDialog pdlg = new PrintDialog();
                    if (pdlg.ShowDialog() == DialogResult.OK)
                    {
                        // todo: save all print settings
                        action.OutputFileName = pdlg.PrinterSettings.PrinterName;
                        return(true);
                    }
                    break;

                case ActionKind.Run:
                    break;

                default:
                    System.Diagnostics.Debug.Assert(false);
                    break;
                }
                return(false);
            }
            catch (Exception ex)
            {
                WinUtil.ShowError(ex.Message);
                return(false);
            }
        }
Пример #10
0
 void _taskGrid_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
 {
     try
     {
         for (int i = 0; i < e.RowCount; ++i)
         {
             TaskBase task = Tasks[e.RowIndex + i];
             task.Actions.Invoker = this;
             UpdateTaskGridReportList(e.RowIndex + i);
             OnTaskStateChanged(task);
             task.PropertyChanged += new PropertyChangedEventHandler(TaskPropertyChanged);
         }
     }
     catch (Exception ex)
     {
         WinUtil.ShowError(ex.Message);
     }
 }
Пример #11
0
 /// <summary>
 /// Set task grid row's properties according to task state.
 /// If the passed task is current, also updates actions and schedule panels.
 /// </summary>
 /// <param name="task"></param>
 private void OnTaskStateChanged(TaskBase task)
 {
     if (task == null)
     {
         return;
     }
     try
     {
         int taskIdx = Tasks.IndexOf(task);
         // ignore state change from task if it's not in grid - assume
         // that it was removed while executing:
         // System.Diagnostics.Debug.Assert(taskIdx >= 0);
         if (taskIdx < 0)
         {
             return;
         }
         bool ro = task.State != TaskState.Ready;
         _taskGrid.Rows[taskIdx].ReadOnly = ro;
         if (task == CurrentTask)
         {
             _actionGrid.ReadOnly = ro;
             foreach (ActionBase a in task.Actions)
             {
                 UpdateActionGridCellsReadonlyState(a);
             }
             SetReadOnlyRecursive(grpSchedule.Controls, ro);
             // for "one time" schedules, repeat freq/unit must be adjusted:
             numScheduleRepeatFreq.Enabled = chkScheduleRepeat.Checked;
             cmbScheduleRepeatUnit.Enabled = chkScheduleRepeat.Checked;
             SetReadOnlyRecursive(flwScheduleFrequency.Controls, ro);
             dtpScheduleDate.Enabled = !ro;
             dtpScheduleTime.Enabled = !ro;
         }
     }
     catch (Exception ex)
     {
         WinUtil.ShowError(ex.Message);
     }
 }
Пример #12
0
 private void FileOpen()
 {
     try
     {
         if (!ConfirmNewOrLoadTasks())
         {
             return;
         }
         using (OpenFileDialog ofd = new OpenFileDialog())
         {
             ofd.Filter = string.Format("{0} config files (*{1})|*{1}|All files (*.*)|*.*",
                                        C1ReportsSchedulerService.Constants.Name, C1ReportsSchedulerService.Defaults.C1rsconfFileExt);
             if (ofd.ShowDialog() == DialogResult.OK)
             {
                 FileOpen(ofd.FileName);
             }
         }
     }
     catch (Exception ex)
     {
         WinUtil.ShowError(string.Format("Could not load from the selected file: {0}", ex.Message));
         NewTasks();
     }
 }
Пример #13
0
 private bool CanConnectToService(bool showErrors, out bool serviceInstalled)
 {
     serviceInstalled = false;
     using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name))
     {
         try
         {
             if (sc.Status == ServiceControllerStatus.Running || sc.Status == ServiceControllerStatus.Paused)
             {
                 serviceInstalled = true;
                 return(true);
             }
             else
             {
                 if (showErrors)
                 {
                     WinUtil.ShowError(string.Format("Cannot connect to service {0}: it is not running.", C1ReportsSchedulerService.Constants.Name));
                 }
                 serviceInstalled = true;
                 return(false);
             }
         }
         catch (Exception ex)
         {
             if (showErrors)
             {
                 WinUtil.ShowError(string.Format("Could not access service {0}: {1}", C1ReportsSchedulerService.Constants.Name, ex.Message));
             }
             return(false);
         }
         finally
         {
             sc.Close();
         }
     }
 }
Пример #14
0
        private void Command_Click(object sender, EventArgs e)
        {
            // avoid recursive command calls:
            if (!_timer.Enabled)
            {
                return;
            }

            // we turn service pinging off for the duration of the command.
            _timer.Stop();
            try
            {
                Command cmd = _commandMap[sender];
                cmd();
            }
            catch (Exception ex)
            {
                WinUtil.ShowError(ex.Message);
            }
            finally
            {
                _timer.Start();
            }
        }
Пример #15
0
        private void ServiceStart(ProgressDialog progress, bool unconditionalTransfer)
        {
            using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name))
            {
                try
                {
                    progress.SetProgress(5, "Checking service status...");
                    sc.WaitForStatus(ServiceControllerStatus.Stopped, c_timeout);
                    ServiceControllerStatus status = sc.Status;
                    if (status != ServiceControllerStatus.Stopped)
                    {
                        WinUtil.ShowError("Service is running or paused!");
                        return;
                    }
                    string currentTaskListFile = null;
                    bool   transfer            = unconditionalTransfer;
                    if (!transfer && _tasksHolder.Tasks.Count > 0)
                    {
                        transfer = WinUtil.AskQuestion("Transfer current task list to service?", false);
                    }

                    if (transfer)
                    {
                        progress.SetProgress(10, "Stopping running schedules...");
                        _tasksHolder.StopSchedules();

                        progress.SetProgress(20, "Preparing current task list for transfer...");
                        currentTaskListFile = Path.GetTempFileName();
                        _tasksHolder.Save(currentTaskListFile);
                    }
                    this.Clear();

                    progress.SetProgress(30, string.Format("Starting {0} service...", C1ReportsSchedulerService.Constants.Name));

                    if (string.IsNullOrEmpty(currentTaskListFile))
                    {
                        sc.Start();
                    }
                    else
                    {
                        sc.Start(new string[] { currentTaskListFile });
                    }

                    WaitForStatus(sc, ServiceControllerStatus.Running, progress);

                    sc.ExecuteCommand((int)C1ReportsSchedulerService.CustomCommands.ResetC1rsconfFileToRegistry);
                    progress.SetProgress(90, "Connecting to service...");
                    ConnectToService(progress);

                    progress.SetProgress(90, "Service successfully started.");

                    if (!string.IsNullOrEmpty(currentTaskListFile))
                    {
                        File.Delete(currentTaskListFile);
                    }
                }
                catch (Exception ex)
                {
                    WinUtil.ShowError(string.Format("Error: {0}", ex.Message));
                }
                finally
                {
                    sc.Close();
                }
            }
        }
Пример #16
0
        private void ServiceSetup()
        {
            // find out whether the service is installed or not:
            bool serviceInstalled = false;
            ServiceControllerStatus serviceStatus = ServiceControllerStatus.ContinuePending; // some assigned value

            using (ServiceController sc = new ServiceController(C1ReportsSchedulerService.Constants.Name))
            {
                try
                {
                    serviceStatus    = sc.Status;
                    serviceInstalled = true;
                }
                catch
                {
                }
                finally
                {
                    sc.Close();
                }
            }

            // try getting values from registry, use defaults if unsuccessful:
            string sWcfEndpointBaseAddress = C1ReportsSchedulerService.Defaults.WcfEndpointBaseAddress;
            string sWcfEndpointRelAddress  = C1ReportsSchedulerService.Defaults.WcfEndpointRelAddress;
            string sC1rsconfFilePath       = string.Format(@"{0}\{1}{2}",
                                                           Application.CommonAppDataPath, C1ReportsSchedulerService.Defaults.C1rsconfFileName, C1ReportsSchedulerService.Defaults.C1rsconfFileExt);
            bool sAutoStart = serviceInstalled ?
                              ServiceInstaller.InstallService.GetAutoStartValue(C1ReportsSchedulerService.Constants.Name) : false;
            bool sLogTasks         = C1ReportsSchedulerService.Defaults.LogTasks;
            bool sLogActions       = C1ReportsSchedulerService.Defaults.LogActions;
            bool sLogProgramOutput = C1ReportsSchedulerService.Defaults.LogProgramOutput;
            bool sEnableMex        = C1ReportsSchedulerService.Defaults.EnableMex;

            try
            {
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(C1ReportsSchedulerService.Constants.RegKey_Root))
                {
                    sWcfEndpointBaseAddress = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointBaseAddress) as string;
                    sWcfEndpointRelAddress  = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointRelAddress) as string;
                    sC1rsconfFilePath       = key.GetValue(C1ReportsSchedulerService.Constants.RegKey_C1rsconfFilePath) as string;
                    sLogTasks         = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_LogTasks) != 0;
                    sLogActions       = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_LogActions) != 0;
                    sLogProgramOutput = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_LogProgramOutput) != 0;
                    sEnableMex        = (int)key.GetValue(C1ReportsSchedulerService.Constants.RegKey_EnableMex) != 0;
                }
            }
            catch
            {
                // eat errors here
            }

            // show dialog to set up service parameters (WCF comm address etc):
            using (ServiceInstallDialog sid = new ServiceInstallDialog())
            {
                sid.WcfEndpointBaseAddress = sWcfEndpointBaseAddress;
                sid.WcfEndpointRelAddress  = sWcfEndpointRelAddress;
                sid.C1rsconfFilePath       = sC1rsconfFilePath;
                sid.AutoStart        = sAutoStart;
                sid.LogTasks         = sLogTasks;
                sid.LogActions       = sLogActions;
                sid.LogProgramOutput = sLogProgramOutput;
                sid.EnableMex        = sEnableMex;
                if (sid.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                // copy back values from the dialog:
                sWcfEndpointBaseAddress = sid.WcfEndpointBaseAddress;
                sWcfEndpointRelAddress  = sid.WcfEndpointRelAddress;
                sC1rsconfFilePath       = sid.C1rsconfFilePath;
                sAutoStart        = sid.AutoStart;
                sLogTasks         = sid.LogTasks;
                sLogActions       = sid.LogActions;
                sLogProgramOutput = sid.LogProgramOutput;
                sEnableMex        = sid.EnableMex;
            }

            // install service if it is not installed, stop (so that the new settings will be applied) otherwise:
            using (ProgressDialog prg = new ProgressDialog())
            {
                if (!serviceInstalled)
                {
                    // install service:
                    prg.SetProgress(10, "Installing service...");
                    // find service executable (must be in same directory as current app):
                    string serviceExe = WinUtil.FindServiceExe();
                    if (string.IsNullOrEmpty(serviceExe))
                    {
                        WinUtil.ShowError(string.Format("Could not find the service executable {0}", WinUtil.ServiceExeName));
                        return;
                    }
                    // install service:
                    ServiceInstaller.InstallService.Install(serviceExe, C1ReportsSchedulerService.Constants.Name, C1ReportsSchedulerService.Constants.Name, sAutoStart);
                    ServiceInstaller.InstallService.SetDescription(C1ReportsSchedulerService.Constants.Name, C1ReportsSchedulerService.Constants.Description);
                }
                else
                {
                    DisconnectFromService();
                    // stop service (restart will pick up new settings):
                    ServiceCall(ServiceControllerStatus.Stopped, "Stopping", prg);
                    // update "autostart" parameter:
                    ServiceInstaller.InstallService.SetAutoStartValue(C1ReportsSchedulerService.Constants.Name, sAutoStart);
                }
                // set up service via additional registry keys:
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(C1ReportsSchedulerService.Constants.RegKey_Root, true))
                {
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointBaseAddress, sWcfEndpointBaseAddress, RegistryValueKind.String);
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_WcfEndpointRelAddress, sWcfEndpointRelAddress, RegistryValueKind.String);
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_C1rsconfFilePath, sC1rsconfFilePath, RegistryValueKind.String);
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_LogTasks, sLogTasks ? 1 : 0, RegistryValueKind.DWord);
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_LogActions, sLogActions ? 1 : 0, RegistryValueKind.DWord);
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_LogProgramOutput, sLogProgramOutput ? 1 : 0, RegistryValueKind.DWord);
                    key.SetValue(C1ReportsSchedulerService.Constants.RegKey_EnableMex, sEnableMex ? 1 : 0, RegistryValueKind.DWord);
                }
                // create event log:
                if (!EventLog.SourceExists(C1ReportsSchedulerService.Constants.EventLogSourceName))
                {
                    // optional but why not...
                    prg.SetProgress(prg.Complete + 5, "Creating service event log source...");
                    EventLog.CreateEventSource(C1ReportsSchedulerService.Constants.EventLogSourceName, C1ReportsSchedulerService.Constants.EventLogName);
                }
                // start service:
                ServiceStart(prg, false);
            }
        }
Пример #17
0
        private void TestClientModeOnStart()
        {
            if (Properties.Settings.Default.ClientMode)
            {
                return;
            }

            bool serviceInstalled;

            if (CanConnectToService(false, out serviceInstalled))
            {
                // connect to a running service:
                string text =
                    "You're about to start C1ReportsScheduler app in standalone mode.\r\n" +
                    "But, it appears that C1Reports scheduling service is installed and running.\r\n" +
                    "For full functionality it is highly recommended that you connect to the C1Reports scheduling service, " +
                    "and use the C1ReportsScheduler (this application) in client mode to communicate with the service.\r\n\r\n" +
                    "Do you want to connect to the service now?\r\n"
                ;
                if (WinUtil.AskQuestion(text, false))
                {
                    Properties.Settings.Default.ClientMode = true;
                }
            }
            else if (serviceInstalled)
            {
                // warn about a stopped service:
                string text =
                    "You're about to start C1ReportsScheduler app in standalone mode.\r\n" +
                    "But, it appears that C1Reports scheduling service is installed but is not running.\r\n" +
                    "For full functionality it is highly recommended that you start the C1Reports scheduling service, " +
                    "and use the C1ReportsScheduler (this application) in client mode to communicate with the service.\r\n\r\n" +
                    "Use menu Service | Start to start the service and connect to it.\r\n"
                ;
                WinUtil.ShowWarning(text);
            }
            else
            {
                // offer to install and start service:
                string text =
                    "You're about to start C1ReportsScheduler app in standalone mode.\r\n" +
                    "Standalone mode provides limited scheduling functionality, as scheduled reports will not run when the application is closed.\r\n" +
                    "For full functionality it is highly recommended that you install the C1Reports scheduling service, " +
                    "and use the C1ReportsScheduler (this application) in client mode to communicate with the service.\r\n\r\n" +
                    "Do you want to install the service now?\r\n\r\n" +
                    "(You can install or uninstall the service at any time via the \"Service\" menu.)"
                ;
                if (WinUtil.AskQuestion(text, false))
                {
                    try
                    {
                        ServiceSetup();
                        Properties.Settings.Default.ClientMode = true;
                    }
                    catch (Exception ex)
                    {
                        WinUtil.ShowError(ex.Message);
                    }
                }
            }
        }