private void LoadConfigurationFile() { try { // Check if configuration exist, if not create the default if (!File.Exists(_configFile)) { ActiveQLibrary.Global.CreateDefaultConfig(System.Environment.CurrentDirectory + @"\Config.xml", System.Environment.CurrentDirectory + @"\Pickup", false); } TextReader reader = new StreamReader(_configFile); XmlSerializer serialize = new XmlSerializer(typeof(ActiveQLibrary.Serialization.ConfigGlobal.Config)); ActiveQLibrary.Serialization.ConfigGlobal.Config config = (ActiveQLibrary.Serialization.ConfigGlobal.Config)serialize.Deserialize(reader); reader.Close(); this._tbWorker.Text = config.Threads.ToString(); this._tbActiveMailLicense.Text = config.ActiveMailLicense; this._tbIntervalMail.Text = config.Readers.MailPickUp.ToString(); this._tbIntervalTask.Text = config.Readers.XmlPickup.ToString(); if (config.DeleteMailWhenProcessed == true) { this._cbDeleteWhenProcessed.Checked = true; } else { this._cbDeleteWhenProcessed.Checked = false; } this._tbMaxBytesEvent.Text = config.LogFiles.MaxSizeEvent.ToString(); this._tbMaxBytesError.Text = config.LogFiles.MaxSizeError.ToString(); this._meMailDir.AddListItem(config.MailPickupDirectories); this._meXmlFile.AddListItem(config.XmlPickupFiles); foreach (SmtpServer smtpServer in config.SmtpServers) { if (smtpServer.Password.Trim() != "") { smtpServer.Password = Encryption.Decrypt(smtpServer.Password.Trim()); } } this._meSmtpServer.AddListItem(config.SmtpServers); } catch { this.Hide(); MessageBox.Show(string.Format("Unable to load '{0}'", _configFile), "Loading configuration file", MessageBoxButtons.OK, MessageBoxIcon.Error); Application.Exit(); } }
/// <summary> /// Create the default configuration file. /// </summary> /// <param name="defaultPickupDir">Default pickup dir</param> /// <param name="ConfigFile">The name of the config file.</param> /// <param name="useExecutingAssemblyPath">If the exectuting assembly path must be used.</param> public static void CreateDefaultConfig(string ConfigFile,string defaultPickupDir, bool useExecutingAssemblyPath) { try { Config cfg = new Config(); cfg.Threads = 10; cfg.Readers.MailPickUp = 15; cfg.Readers.XmlPickup = 60; cfg.DeleteMailWhenProcessed = false; cfg.LogFiles.MaxSizeEvent = 0; cfg.LogFiles.MaxSizeError = 0; if (defaultPickupDir != null) { cfg.MailPickupDirectories.Add(defaultPickupDir); Directory.CreateDirectory(defaultPickupDir); } TextWriter writer = null; if (useExecutingAssemblyPath == true) writer = new StreamWriter(GetImagePath(Assembly.GetExecutingAssembly().Location) + @"\" + _configFileName); else writer = new StreamWriter(ConfigFile); XmlSerializer serializer = new XmlSerializer(typeof(Config)); serializer.Serialize(writer, cfg); writer.Close(); } catch(Exception ex) { Global.Log.WriteError("[GLOBAL:DEFAULTCONFIG] " + ex.Message); Global.Log.WriteError("[GLOBAL:DEFAULTCONFIG] " + ex.StackTrace); } }
/// <summary> /// Load the config value from the config file. /// </summary> public static void LoadConfig() { try { string configFullPath = GetImagePath(Assembly.GetExecutingAssembly().Location) + @"\" + _configFileName; Global.Log.WriteEvent(LogType.normal,string.Format("[GLOBAL] Loading configuration value from '{0}'",configFullPath)); TextReader reader = new StreamReader(configFullPath); XmlSerializer serialize = new XmlSerializer(typeof(Config)); _config = (Config)serialize.Deserialize(reader); reader.Close(); _config.LogFiles.MaxSizeEvent *= 1000000; _config.LogFiles.MaxSizeError *= 1000000; if (_activeMail != null) foreach(SmtpServer smtp in _config.SmtpServers) { if (smtp.Host.Trim() != "" && smtp.Port > 0 && smtp.Password.Trim() == "") _smtpServers.GetType().GetMethod("Add", new Type[] {Type.GetType("System.String"),Type.GetType("System.Int32")}).Invoke(_smtpServers,new object[] {smtp.Host,smtp.Port}); else if (smtp.Host.Trim() != "" && smtp.Port > 0 && smtp.Password.Trim() != "") { Object server = Activator.CreateInstance(_activeCommon.GetType("ActiveUp.Net.Mail.Server",true),new object[] {smtp.Host,smtp.Port,smtp.Username,Encryption.Decrypt(smtp.Password)}); _smtpServers.GetType().GetMethod("Add", new Type[] {_activeCommon.GetType("ActiveUp.Net.Mail.Server",true)}).Invoke(_smtpServers,new object[] {server}); } } } catch(FileNotFoundException) { CreateDefaultConfig(); LoadConfig(); } catch(Exception ex) { Global.Log.WriteError("[GLOBAL] " + ex.Message); Global.Log.WriteError("[GLOBAL] " + ex.StackTrace); } }
/// <summary> /// Initialize the library. /// Be careful : this method is mandatory for the good work of the application. /// </summary> /// <param name="ConfigFile">The name of the config file</param> /// <param name="Prefix">The prefix of the config file</param> /// <param name="defaultLevel">The default level of loggin</param> public static void InitializeLibrary(string ConfigFile, string Prefix, LogType defaultLevel) { if (ConfigFile == null) throw new ArgumentNullException("ConfigFile"); if (ConfigFile.Trim() == "") throw new ArgumentException("Cannot be blank","ConfigFile"); _config = new Config(); _configFileName = ConfigFile; _log = new Logger(Prefix,defaultLevel); EventLog ev = new EventLog(); ev.Source = "ActiveQ"; string path = GetImagePath(Assembly.GetExecutingAssembly().Location); // Create the error directory _pathDirError = path + @"\Error"; Directory.CreateDirectory(_pathDirError); // Create the processed directory _pathDirProcessed = path + @"\Processed"; Directory.CreateDirectory(_pathDirProcessed); try { // loading library _activeMail = null; _activeCommon = null; _activeSmtp = null; try { _activeMail = Assembly.LoadFrom(_libraryMailFile); _activeCommon = Assembly.LoadFrom(_libraryCommonFile); _activeSmtp = Assembly.LoadFrom(_librarySmtpFile); } catch (FileNotFoundException fe) { } if (_activeMail == null) { String dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); _activeMail = Assembly.LoadFrom(Path.Combine(dir, _libraryMailFile)); } if (_activeCommon == null) { String dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); _activeCommon = Assembly.LoadFrom(Path.Combine(dir, _libraryCommonFile)); } if (_activeSmtp == null) { String dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); _activeSmtp = Assembly.LoadFrom(Path.Combine(dir, _librarySmtpFile)); } object _message = Activator.CreateInstance(_activeCommon.GetType("ActiveUp.Net.Mail.Message",true)); _smtpServers = Activator.CreateInstance(_activeCommon.GetType("ActiveUp.Net.Mail.ServerCollection",true)); LoadConfig(); //_activeMail.GetType("ActiveUp.Mail.Common.LicenseInfo").GetProperty("License").SetValue(typeof(string),_config.ActiveMailLicense,null); string imageDir = GetImagePath(Assembly.GetExecutingAssembly().Location); if (imageDir.Trim() != "") _libraryMailFile = imageDir + @"\" + _libraryMailFile; } catch(FileNotFoundException) { Log.WriteError(string.Format("[INITIALIZE] Unable to find '{0}'.",_libraryMailFile)); Log.WriteError(string.Format("[INITIALIZE] Mailing operation are disabled")); ev.WriteEntry(string.Format("Unable to find '{0}'.\nMailing operation are disabled.",_libraryMailFile),EventLogEntryType.Error); string keyString = @"SOFTWARE\Active Up\ActiveQ"; RegistryKey key = Registry.LocalMachine.OpenSubKey(keyString); if (key != null) { if ((string)key.GetValue("ActiveMailNotFound") == (string)"True") { PageActiveMailNotFound pageMailNotFound = new PageActiveMailNotFound(); pageMailNotFound.ShowDialog(); } key.Close(); } else { PageActiveMailNotFound pageMailNotFound = new PageActiveMailNotFound(); pageMailNotFound.ShowDialog(); } } catch(Exception ex) { Type trialExpired = _activeMail.GetType("ActiveUp.Licensing.TrialException",true); if (ex.InnerException.GetType() == trialExpired) { Log.WriteError(string.Format("[INITIALIZE] Trial version of ActiveMail has expired, please register at www.activeup.com.")); Log.WriteError(string.Format("[INITIALIZE] Mailing operation are disabled.")); ev.WriteEntry("Trial version of ActiveMail has expired, please register at www.activeup.com.\nMailing operation are disabled.",EventLogEntryType.Error); PageActiveMailExpired pageMailExpired = new PageActiveMailExpired(); pageMailExpired.ShowDialog(); _activeMail = null; } else { if (ex.InnerException != null) { Log.WriteError(string.Format("[INITIALIZE] Error loading '{0}'",_libraryMailFile)); Log.WriteError("[INITIALIZE] " + ex.InnerException.Message); Log.WriteError("[INITIALIZE] " + ex.InnerException.StackTrace); } else { Log.WriteError(string.Format("[INITIALIZE] Error loading '{0}'",_libraryMailFile)); Log.WriteError("[INITIALIZE] " + ex.Message); Log.WriteError("[INITIALIZE] " + ex.StackTrace); } } } // Initalization for the midnigth event DateTime now = DateTime.Now; DateTime midnight = new DateTime(now.Year,now.Month,now.Day,0,0,0,0); midnight = midnight.AddDays(1); TimeSpan t = midnight - now; Log.WriteEvent(LogType.normal,string.Format("[INITIALIZE] Midnigth event occurs in {0}",t)); _timeMidnight.Change(t,new TimeSpan(-1)); _reader = new Reader(); _processer = new Processer(); ActiveQLibrary.Form.ManageForm.Initialize(); }
private void ApplyChanges() { try { ActiveQLibrary.Serialization.ConfigGlobal.Config config = new ActiveQLibrary.Serialization.ConfigGlobal.Config(); config.Threads = Int32.Parse(_tbWorker.Text); config.Readers.MailPickUp = Int32.Parse(_tbIntervalMail.Text); config.Readers.XmlPickup = Int32.Parse(_tbIntervalTask.Text); config.DeleteMailWhenProcessed = this._cbDeleteWhenProcessed.Checked; config.LogFiles.MaxSizeEvent = Int32.Parse(this._tbMaxBytesEvent.Text); config.LogFiles.MaxSizeError = Int32.Parse(this._tbMaxBytesError.Text); config.ActiveMailLicense = _tbActiveMailLicense.Text; foreach (string dir in _meMailDir.ListBoxItem.Items) { config.MailPickupDirectories.Add(dir); } foreach (ListViewItem lvITem in _meSmtpServer.ListViewItem.Items) { int index = _meSmtpServer.IndexElement(lvITem.Text, Int32.Parse(lvITem.SubItems[1].Text)); if (index != -1) { if (((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Username.Trim() != "" && ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Password.Trim() != "") { config.SmtpServers.Add(new SmtpServer(((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Host, ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Port, ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Username, Encryption.Encrypt(((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Password))); } else { config.SmtpServers.Add(new SmtpServer(((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Host, ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Port, "", "")); } } } foreach (string xml in _meXmlFile.ListBoxItem.Items) { config.XmlPickupFiles.Add(xml); } XmlSerializer serialize = new XmlSerializer(typeof(ActiveQLibrary.Serialization.ConfigGlobal.Config)); TextWriter writer = new StreamWriter(_configFile); serialize.Serialize(writer, config); writer.Close(); try { ServiceController sc = new ServiceController("ActiveQ"); sc.ExecuteCommand(230); } catch { } } catch (Exception ex) { MessageBox.Show(string.Format("Unable write data in '{0}'\n{1}\n{2}", _configFile, ex.Source, ex.Message), "Writing configuration file", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void ApplyChanges() { try { ActiveQLibrary.Serialization.ConfigGlobal.Config config = new ActiveQLibrary.Serialization.ConfigGlobal.Config(); config.Threads = Int32.Parse(_tbWorker.Text); config.Readers.MailPickUp = Int32.Parse(_tbIntervalMail.Text); config.Readers.XmlPickup = Int32.Parse(_tbIntervalTask.Text); config.DeleteMailWhenProcessed = this._cbDeleteWhenProcessed.Checked; config.LogFiles.MaxSizeEvent = Int32.Parse(this._tbMaxBytesEvent.Text); config.LogFiles.MaxSizeError = Int32.Parse(this._tbMaxBytesError.Text); config.ActiveMailLicense = _tbActiveMailLicense.Text; foreach(string dir in _meMailDir.ListBoxItem.Items) { config.MailPickupDirectories.Add(dir); } foreach(ListViewItem lvITem in _meSmtpServer.ListViewItem.Items) { int index = _meSmtpServer.IndexElement(lvITem.Text,Int32.Parse(lvITem.SubItems[1].Text)); if (index != -1) { if (((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Username.Trim() != "" && ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Password.Trim() != "") config.SmtpServers.Add(new SmtpServer(((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Host, ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Port, ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Username, Encryption.Encrypt(((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Password))); else config.SmtpServers.Add(new SmtpServer(((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Host, ((SmtpServer)_meSmtpServer.ListItemSmtp[index]).Port, "", "")); } } foreach(string xml in _meXmlFile.ListBoxItem.Items) { config.XmlPickupFiles.Add(xml); } XmlSerializer serialize = new XmlSerializer( typeof(ActiveQLibrary.Serialization.ConfigGlobal.Config)); TextWriter writer = new StreamWriter(_configFile); serialize.Serialize( writer, config ); writer.Close(); try { ServiceController sc = new ServiceController("ActiveQ"); sc.ExecuteCommand(230); } catch { } } catch(Exception ex) { MessageBox.Show(string.Format("Unable write data in '{0}'\n{1}\n{2}",_configFile,ex.Source,ex.Message),"Writing configuration file",MessageBoxButtons.OK,MessageBoxIcon.Error); } }