Exemplo n.º 1
0
        /// <summary>
        /// Saves the changes.
        /// </summary>
        /// <returns></returns>
        private bool SaveChanges()
        {
            // Validate the name is not empty
            if (!FormHelper.ValidateNotEmpty(txtName, Resources.MsgUniqueNameRequired))
            {
                return(false);
            }

            string gatewayId = txtName.Text.Trim();

            if (!this.IsUpdate)
            {
                // The gateway name must be unique
                if (GatewayConfig.Exists(g => g.Id.ToLower() == gatewayId.ToLower()))
                {
                    FormHelper.ShowError(txtName, string.Format(Resources.MsgGatewayNameAlreadyExists, gatewayId));
                    return(false);
                }

                // The port name should not be in use
                if (GatewayConfig.Exists(g => g.ComPort.ToLower() == cboPort.Text.ToLower()))
                {
                    DialogResult dialogResult = FormHelper.Confirm(string.Format(Resources.MsgDuplicateComPort, cboPort.Text));
                    if (dialogResult == DialogResult.No)
                    {
                        return(false);
                    }
                }
            }

            // If forwarded is checked, make sure email address is not empty
            if (chkForwardArchivedMessageLogAsEmail.Checked)
            {
                if (!FormHelper.ValidateNotEmpty(txtEmailAddress, Resources.MsgEmailAddressRequired))
                {
                    return(false);
                }
            }

            // If auto response is required, make sure the text is not empty
            if (chkAutoResponseCall.Checked)
            {
                if (!FormHelper.ValidateNotEmpty(txtAutoResponseText, Resources.MsgAutoResponseTextRequired))
                {
                    return(false);
                }
            }

            try
            {
                // Save the gateway configuration
                GatewayConfig gatewayConfig = new GatewayConfig();

                if (this.IsUpdate)
                {
                    gatewayConfig = GatewayConfig.SingleOrDefault(g => g.Id == gatewayId);
                }

                // General
                gatewayConfig.Id          = gatewayId;
                gatewayConfig.OwnNumber   = txtPhoneNo.Text;
                gatewayConfig.AutoConnect = chkConnectAtStartup.Checked;
                gatewayConfig.Functions   = 0;
                if (chkSendMessage.Checked)
                {
                    gatewayConfig.Functions += (int)GatewayFunction.SendMessage;
                }
                if (chkReceiveMessage.Checked)
                {
                    gatewayConfig.Functions += (int)GatewayFunction.ReceiveMessage;
                }
                gatewayConfig.LogSettings       = cboLogType.Text;
                gatewayConfig.ClearLogOnConnect = chkClearLogOnConnect.Checked;
                gatewayConfig.Initialize        = false;

                // Communication
                gatewayConfig.ComPort        = cboPort.Text;
                gatewayConfig.BaudRate       = cboBaudRate.Text;
                gatewayConfig.DataBits       = cboDataBits.Text;
                gatewayConfig.Parity         = cboParity.Text;
                gatewayConfig.StopBits       = cboStopBits.Text;
                gatewayConfig.Handshake      = cboHandshake.Text;
                gatewayConfig.CommandTimeout = Convert.ToInt32(updCommandTimeout.Value);
                gatewayConfig.CommandDelay   = Convert.ToInt32(updCommandDelay.Value);
                gatewayConfig.ReadTimeout    = Convert.ToInt32(updReadIntervalTimeout.Value);


                // Message Settings
                gatewayConfig.Smsc                = txtSmsc.Text;
                gatewayConfig.UseSimSmsc          = chkUseSmscFromSim.Checked;
                gatewayConfig.MessageMemory       = cboMessageMemory.Text;
                gatewayConfig.MessageValidity     = cboMessageValidity.Text;
                gatewayConfig.SendRetry           = Convert.ToInt32(updSendRetry.Value);
                gatewayConfig.SendDelay           = Convert.ToInt32(updSendDelay.Value);
                gatewayConfig.RequestStatusReport = chkRequestDeliveryStatusReport.Checked;
                gatewayConfig.DeleteAfterRetrieve = chkDeleteAfterRetrieve.Checked;


                // Message log
                gatewayConfig.AutoArchiveLog            = chkAutoArchiveMessageLog.Checked;
                gatewayConfig.AutoArchiveLogInterval    = Convert.ToInt32(updArchiveMessageLogDay.Value);
                gatewayConfig.ArchiveOldMessageInterval = Convert.ToInt32(updArchiveMessageOlderThanDay.Value);
                gatewayConfig.ForwardArchivedMessage    = chkForwardArchivedMessageLogAsEmail.Checked;
                gatewayConfig.ForwardEmail                  = txtEmailAddress.Text;
                gatewayConfig.DeleteArchivedMessage         = chkDeleteArchivedMessageOlderThan.Checked;
                gatewayConfig.DeleteArchivedMessageInterval = Convert.ToInt32(updDeleteArchivedMessageOlderThanDay.Value);

                // Other settings
                gatewayConfig.SignalRefreshInterval = Convert.ToInt32(updRefreshSignalInterval.Value);
                gatewayConfig.Pin = txtPin.Text;
                gatewayConfig.AutoResponseCall     = chkAutoResponseCall.Checked;
                gatewayConfig.AutoResponseCallText = txtAutoResponseText.Text;

                if (!this.IsUpdate)
                {
                    gatewayConfig.Save();
                }
                else
                {
                    gatewayConfig.Update();
                }

                if (GatewayAdded != null)
                {
                    // Raise the event
                    GatewayEventHandlerArgs arg = new GatewayEventHandlerArgs(gatewayId);
                    this.GatewayAdded.BeginInvoke(this, arg, new AsyncCallback(this.AsyncCallback), null);
                }

                if (GatewayUpdated != null)
                {
                    // Raise the event
                    GatewayEventHandlerArgs arg = new GatewayEventHandlerArgs(gatewayId);
                    this.GatewayUpdated.BeginInvoke(this, arg, new AsyncCallback(this.AsyncCallback), null);
                }
            }
            catch (Exception ex)
            {
                FormHelper.ShowError(ex.Message);
                return(false);
            }

            // Reset to false
            isFormChanged = false;

            // Show successful save message
            FormHelper.ShowInfo(Resources.MsgGatewayConfigSaved);

            // Return true as saving is successful
            return(true);
        }