PageRenderTime 64ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Forms/FormMain.cs

https://github.com/SkyFire/icechat
C# | 5902 lines | 4742 code | 622 blank | 538 comment | 1083 complexity | 8ee8a60ee574ed4551d6a56ec918084b MD5 | raw file
Possible License(s): AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /******************************************************************************\
  2. * IceChat 9 Internet Relay Chat Client
  3. *
  4. * Copyright (C) 2011 Paul Vanderzee <snerf@icechat.net>
  5. * <www.icechat.net>
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Please consult the LICENSE.txt file included with this project for
  21. * more details
  22. *
  23. \******************************************************************************/
  24. using System;
  25. using System.Collections.Generic;
  26. using System.Collections;
  27. using System.ComponentModel;
  28. using System.Data;
  29. using System.Drawing.Drawing2D;
  30. using System.Drawing;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using System.Windows.Forms;
  34. using System.Xml.Serialization;
  35. using System.IO;
  36. using System.Reflection;
  37. using System.Runtime.InteropServices;
  38. using IceChatPlugin;
  39. namespace IceChat
  40. {
  41. public partial class FormMain : Form
  42. {
  43. public static FormMain Instance;
  44. private string optionsFile;
  45. private string messagesFile;
  46. private string fontsFile;
  47. private string colorsFile;
  48. private string soundsFile;
  49. private string favoriteChannelsFile;
  50. private string serversFile;
  51. private string aliasesFile;
  52. private string popupsFile;
  53. private string pluginsFile;
  54. private string currentFolder;
  55. private string logsFolder;
  56. private string pluginsFolder;
  57. private string emoticonsFile;
  58. private string scriptsFolder;
  59. private string soundsFolder;
  60. private string picturesFolder;
  61. private List<LanguageItem> languageFiles;
  62. private LanguageItem currentLanguageFile;
  63. private StreamWriter errorFile;
  64. private IceChatOptions iceChatOptions;
  65. private IceChatColors iceChatColors;
  66. private IceChatMessageFormat iceChatMessages;
  67. private IceChatFontSetting iceChatFonts;
  68. private IceChatAliases iceChatAliases;
  69. private IceChatPopupMenus iceChatPopups;
  70. private IceChatEmoticon iceChatEmoticons;
  71. private IceChatLanguage iceChatLanguage;
  72. private IceChatSounds iceChatSounds;
  73. private IceChatPluginFile iceChatPlugins;
  74. //private System.Threading.Mutex mutex;
  75. private List<IPluginIceChat> loadedPlugins;
  76. private IdentServer identServer;
  77. private TabPage nickListTab;
  78. private TabPage serverListTab;
  79. private TabPage channelListTab;
  80. private TabPage buddyListTab;
  81. private delegate IceTabPage AddWindowDelegate(IRCConnection connection, string windowName, IceTabPage.WindowType windowType);
  82. private delegate void CurrentWindowDelegate(string data, int color);
  83. private delegate void WindowMessageDelegate(IRCConnection connection, string name, string data, int color, bool scrollToBottom);
  84. private delegate void CurrentWindowMessageDelegate(IRCConnection connection, string data, int color, bool scrollToBottom);
  85. private System.Timers.Timer flashTrayIconTimer;
  86. private int flashTrayCount;
  87. private System.Timers.Timer flashTaskBarIconTimer;
  88. private int flashTaskBarCount;
  89. private System.Media.SoundPlayer player;
  90. private bool muteAllSounds;
  91. [StructLayout(LayoutKind.Sequential)]
  92. private struct OSVERSIONINFOEX
  93. {
  94. public int dwOSVersionInfoSize;
  95. public int dwMajorVersion;
  96. public int dwMinorVersion;
  97. public int dwBuildNumber;
  98. public int dwPlatformId;
  99. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
  100. public string szCSDVersion;
  101. public short wServicePackMajor;
  102. public short wServicePackMinor;
  103. public short wSuiteMask;
  104. public byte wProductType;
  105. public byte wReserved;
  106. }
  107. [DllImport("kernel32.dll")]
  108. private static extern bool GetVersionEx(ref OSVERSIONINFOEX osVersionInfo);
  109. private const int VER_NT_WORKSTATION = 1;
  110. private const int VER_NT_DOMAIN_CONTROLLER = 2;
  111. private const int VER_NT_SERVER = 3;
  112. private const int VER_SUITE_SMALLBUSINESS = 1;
  113. private const int VER_SUITE_ENTERPRISE = 2;
  114. private const int VER_SUITE_TERMINAL = 16;
  115. private const int VER_SUITE_DATACENTER = 128;
  116. private const int VER_SUITE_SINGLEUSERTS = 256;
  117. private const int VER_SUITE_PERSONAL = 512;
  118. private const int VER_SUITE_BLADE = 1024;
  119. public const string ProgramID = "IceChat 9";
  120. public const string VersionID = "Release Candidate 2.5";
  121. /// <summary>
  122. /// All the Window Message Types used for Coloring the Tab Text for Different Events
  123. /// </summary>
  124. internal enum ServerMessageType
  125. {
  126. Default = 0,
  127. Message = 1,
  128. Action = 2,
  129. JoinChannel = 3,
  130. PartChannel = 4,
  131. QuitServer = 5,
  132. ServerMessage = 6,
  133. Other = 7
  134. }
  135. public FormMain(string[] args, Form splash)
  136. {
  137. FormMain.Instance = this;
  138. player = new System.Media.SoundPlayer();
  139. bool forceCurrentFolder = false;
  140. if (args.Length > 0)
  141. {
  142. string prevArg = "";
  143. foreach (string arg in args)
  144. {
  145. if (prevArg.Length == 0)
  146. prevArg = arg;
  147. else
  148. {
  149. switch (prevArg.ToLower())
  150. {
  151. case "-profile":
  152. currentFolder = arg;
  153. //check if the folder exists, ir not, create it
  154. if (!Directory.Exists(currentFolder))
  155. Directory.CreateDirectory(currentFolder);
  156. forceCurrentFolder = true;
  157. break;
  158. }
  159. prevArg = "";
  160. }
  161. }
  162. }
  163. //mutex = new System.Threading.Mutex(true, "IceChatMutex");
  164. #region Settings Files
  165. //check if the xml settings files exist in current folder
  166. if (currentFolder == null)
  167. currentFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  168. if (!File.Exists(currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatServer.xml") && !forceCurrentFolder)
  169. {
  170. if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "IceChat Networks" + Path.DirectorySeparatorChar + "IceChat"))
  171. Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "IceChat Networks" + Path.DirectorySeparatorChar + "IceChat");
  172. currentFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + Path.DirectorySeparatorChar + "IceChat Networks" + Path.DirectorySeparatorChar + "IceChat";
  173. }
  174. //load all files from the Local AppData folder, unless it exist in the current folder
  175. serversFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatServer.xml";
  176. optionsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatOptions.xml";
  177. messagesFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatMessages.xml";
  178. fontsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatFonts.xml";
  179. colorsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatColors.xml";
  180. soundsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatSounds.xml";
  181. favoriteChannelsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatChannels.xml";
  182. aliasesFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatAliases.xml";
  183. popupsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatPopups.xml";
  184. pluginsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "IceChatPlugins.xml";
  185. emoticonsFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "Emoticons" + System.IO.Path.DirectorySeparatorChar + "IceChatEmoticons.xml";
  186. logsFolder = currentFolder + System.IO.Path.DirectorySeparatorChar + "Logs";
  187. scriptsFolder = currentFolder + System.IO.Path.DirectorySeparatorChar + "Scripts";
  188. soundsFolder = currentFolder + System.IO.Path.DirectorySeparatorChar + "Sounds";
  189. picturesFolder = currentFolder + System.IO.Path.DirectorySeparatorChar + "Pictures";
  190. //pluginsFolder = currentFolder + System.IO.Path.DirectorySeparatorChar + "Plugins";
  191. pluginsFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + System.IO.Path.DirectorySeparatorChar + "Plugins";
  192. //pluginsFolder = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
  193. if (!Directory.Exists(pluginsFolder))
  194. Directory.CreateDirectory(pluginsFolder);
  195. if (!Directory.Exists(scriptsFolder))
  196. Directory.CreateDirectory(scriptsFolder);
  197. if (!Directory.Exists(soundsFolder))
  198. Directory.CreateDirectory(soundsFolder);
  199. if (!Directory.Exists(picturesFolder))
  200. Directory.CreateDirectory(picturesFolder);
  201. #endregion
  202. languageFiles = new List<LanguageItem>();
  203. DirectoryInfo languageDirectory = null;
  204. languageDirectory = new DirectoryInfo(currentFolder + System.IO.Path.DirectorySeparatorChar + "Languages");
  205. if (!Directory.Exists(currentFolder + System.IO.Path.DirectorySeparatorChar + "Languages"))
  206. Directory.CreateDirectory(currentFolder + System.IO.Path.DirectorySeparatorChar + "Languages");
  207. if (languageDirectory != null)
  208. {
  209. // scan the language directory for xml files and make LanguageItems for each file
  210. FileInfo[] langFiles = languageDirectory.GetFiles("*.xml");
  211. foreach (FileInfo fi in langFiles)
  212. {
  213. string langFile = languageDirectory.FullName + System.IO.Path.DirectorySeparatorChar + fi.Name;
  214. LanguageItem languageItem = LoadLanguageItem(langFile);
  215. if (languageItem != null) languageFiles.Add(languageItem);
  216. }
  217. if (languageFiles.Count == 0)
  218. {
  219. currentLanguageFile = new LanguageItem();
  220. languageFiles.Add(currentLanguageFile); // default language English
  221. }
  222. }
  223. LoadOptions();
  224. LoadColors();
  225. LoadSounds();
  226. // use the language saved in options if availlable,
  227. // if not (e.g. user deleted xml file) default is used
  228. foreach (LanguageItem li in languageFiles)
  229. {
  230. if (li.LanguageName == iceChatOptions.Language)
  231. {
  232. currentLanguageFile = li;
  233. break;
  234. }
  235. }
  236. LoadLanguage(); // The language class MUST be loaded before any GUI component is created
  237. //check if we have any servers/settings saved, if not, load firstrun
  238. if (!File.Exists(serversFile))
  239. {
  240. FormFirstRun firstRun = new FormFirstRun(currentFolder);
  241. firstRun.ShowDialog(this);
  242. }
  243. InitializeComponent();
  244. //load icons from Embedded Resources
  245. this.toolStripQuickConnect.Image = StaticMethods.LoadResourceImage("quick.png");
  246. this.toolStripSettings.Image = StaticMethods.LoadResourceImage("settings.png");
  247. this.toolStripColors.Image = StaticMethods.LoadResourceImage("colors.png");
  248. this.toolStripEditor.Image = StaticMethods.LoadResourceImage("editor.png");
  249. this.toolStripAway.Image = StaticMethods.LoadResourceImage("away.png");
  250. this.toolStripSystemTray.Image = StaticMethods.LoadResourceImage("system-tray.png");
  251. this.toolStripUpdate.Image = StaticMethods.LoadResourceImage("update.png");
  252. //disable this by default
  253. this.toolStripUpdate.Visible = false;
  254. this.minimizeToTrayToolStripMenuItem.Image = StaticMethods.LoadResourceImage("new-tray-icon.ico");
  255. this.debugWindowToolStripMenuItem.Image = StaticMethods.LoadResourceImage("window-icon.ico");
  256. this.exitToolStripMenuItem.Image = StaticMethods.LoadResourceImage("disconected.png");
  257. this.iceChatSettingsToolStripMenuItem.Image = StaticMethods.LoadResourceImage("settings.png");
  258. this.iceChatColorsToolStripMenuItem.Image = StaticMethods.LoadResourceImage("colors.png");
  259. this.iceChatEditorToolStripMenuItem.Image = StaticMethods.LoadResourceImage("editormenu.png");
  260. this.codePlexPageToolStripMenuItem.Image = StaticMethods.LoadResourceImage("codeplex.ico");
  261. this.forumsToolStripMenuItem.Image = StaticMethods.LoadResourceImage("smf.ico");
  262. this.facebookFanPageToolStripMenuItem.Image = StaticMethods.LoadResourceImage("facebook.png");
  263. this.checkForUpdateToolStripMenuItem.Image = StaticMethods.LoadResourceImage("update-menu.png");
  264. this.iceChatHomePageToolStripMenuItem.Image = StaticMethods.LoadResourceImage("home.png");
  265. this.downloadPluginsToolStripMenuItem.Image = StaticMethods.LoadResourceImage("plug-icon.png");
  266. this.pluginsToolStripMenuItem.Image = StaticMethods.LoadResourceImage("plug-icon.png");
  267. //this.muteAllSoundsToolStripMenuItem.Image = StaticMethods.LoadResourceImage("mute.png");
  268. //this.browseDataFolderToolStripMenuItem.Image = StaticMethods.LoadResourceImage("folder.ico");
  269. this.notifyIcon.Icon = System.Drawing.Icon.FromHandle(StaticMethods.LoadResourceImage("new-tray-icon.ico").GetHicon());
  270. this.Icon = System.Drawing.Icon.FromHandle(StaticMethods.LoadResourceImage("new-tray-icon.ico").GetHicon());
  271. this.toolStripMain.VisibleChanged += new EventHandler(toolStripMain_VisibleChanged);
  272. serverListToolStripMenuItem.Checked = iceChatOptions.ShowServerTree;
  273. panelDockLeft.Visible = serverListToolStripMenuItem.Checked;
  274. splitterLeft.Visible = serverListToolStripMenuItem.Checked;
  275. nickListToolStripMenuItem.Checked = iceChatOptions.ShowNickList;
  276. panelDockRight.Visible = nickListToolStripMenuItem.Checked;
  277. splitterRight.Visible = nickListToolStripMenuItem.Checked;
  278. statusBarToolStripMenuItem.Checked = iceChatOptions.ShowStatusBar;
  279. statusStripMain.Visible = statusBarToolStripMenuItem.Checked;
  280. toolBarToolStripMenuItem.Checked = iceChatOptions.ShowToolBar;
  281. toolStripMain.Visible = toolBarToolStripMenuItem.Checked;
  282. serverTree = new ServerTree();
  283. serverTree.Dock = DockStyle.Fill;
  284. this.Text = ProgramID + " :: " + VersionID + " :: September 14 2011";
  285. this.notifyIcon.Text = ProgramID + " :: " + VersionID;
  286. if (!Directory.Exists(logsFolder))
  287. Directory.CreateDirectory(logsFolder);
  288. try
  289. {
  290. errorFile = new StreamWriter(logsFolder + System.IO.Path.DirectorySeparatorChar + "errors.log");
  291. }
  292. catch (IOException io)
  293. {
  294. System.Diagnostics.Debug.WriteLine("Can not create errors.log:" + io.Message);
  295. }
  296. if (!iceChatOptions.TimeStamp.EndsWith(" "))
  297. iceChatOptions.TimeStamp += " ";
  298. if (iceChatOptions.WindowSize != null)
  299. {
  300. if (iceChatOptions.WindowSize.Width != 0)
  301. this.Size = iceChatOptions.WindowSize;
  302. else
  303. {
  304. Rectangle r = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea;
  305. this.Width = r.Width;
  306. this.Height = r.Height;
  307. }
  308. }
  309. if (iceChatOptions.WindowLocation != null)
  310. this.Location = iceChatOptions.WindowLocation;
  311. statusStripMain.Visible = iceChatOptions.ShowStatusBar;
  312. inputPanel.ShowColorPicker = iceChatOptions.ShowColorPicker;
  313. inputPanel.ShowEmoticonPicker = iceChatOptions.ShowEmoticonPicker;
  314. inputPanel.ShowSearchPanel = false;
  315. LoadAliases();
  316. LoadPopups();
  317. LoadEmoticons();
  318. LoadMessageFormat();
  319. LoadFonts();
  320. if (iceChatOptions.CurrentTheme == null)
  321. iceChatOptions.CurrentTheme = "Default";
  322. else
  323. {
  324. //load in the new color theme, if it not Default
  325. if (iceChatOptions.CurrentTheme != "Default")
  326. {
  327. string themeFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "Colors-" + iceChatOptions.CurrentTheme + ".xml";
  328. if (File.Exists(themeFile))
  329. {
  330. XmlSerializer deserializer = new XmlSerializer(typeof(IceChatColors));
  331. TextReader textReader = new StreamReader(themeFile);
  332. iceChatColors = (IceChatColors)deserializer.Deserialize(textReader);
  333. textReader.Close();
  334. textReader.Dispose();
  335. colorsFile = themeFile;
  336. }
  337. else
  338. {
  339. System.Diagnostics.Debug.WriteLine("Color Theme File not found:" + themeFile);
  340. }
  341. themeFile = currentFolder + System.IO.Path.DirectorySeparatorChar + "Messages-" + iceChatOptions.CurrentTheme + ".xml";
  342. if (File.Exists(themeFile))
  343. {
  344. XmlSerializer deserializer = new XmlSerializer(typeof(IceChatMessageFormat));
  345. TextReader textReader = new StreamReader(themeFile);
  346. iceChatMessages = (IceChatMessageFormat)deserializer.Deserialize(textReader);
  347. textReader.Close();
  348. textReader.Dispose();
  349. messagesFile = themeFile;
  350. }
  351. else
  352. {
  353. System.Diagnostics.Debug.WriteLine("Messages Theme File not found:" + themeFile);
  354. }
  355. }
  356. }
  357. if (iceChatOptions.Themes == null)
  358. {
  359. iceChatOptions.Themes = new string[1];
  360. iceChatOptions.Themes[0] = "Default";
  361. }
  362. channelList = new ChannelList();
  363. channelList.Dock = DockStyle.Fill;
  364. buddyList = new BuddyList();
  365. buddyList.Dock = DockStyle.Fill;
  366. toolStripMain.BackColor = IrcColor.colors[iceChatColors.ToolbarBackColor];
  367. menuMainStrip.BackColor = IrcColor.colors[iceChatColors.MenubarBackColor];
  368. statusStripMain.BackColor = IrcColor.colors[iceChatColors.StatusbarBackColor];
  369. toolStripStatus.ForeColor = IrcColor.colors[iceChatColors.StatusbarForeColor];
  370. inputPanel.SetInputBoxColors();
  371. channelList.SetListColors();
  372. buddyList.SetListColors();
  373. nickList = new NickList();
  374. nickList.Header = iceChatLanguage.consoleTabTitle;
  375. nickList.Dock = DockStyle.Fill;
  376. serverListTab = new TabPage("Favorite Servers");
  377. Panel serverPanel = new Panel();
  378. serverPanel.Dock = DockStyle.Fill;
  379. serverPanel.Controls.Add(serverTree);
  380. serverListTab.Controls.Add(serverPanel);
  381. serverListTab.BackColor = IrcColor.colors[iceChatColors.PanelHeaderBG1];
  382. serverListTab.ForeColor = IrcColor.colors[iceChatColors.PanelHeaderForeColor];
  383. this.panelDockLeft.TabControl.TabPages.Add(serverListTab);
  384. nickListTab = new TabPage("Nick List");
  385. Panel nickPanel = new Panel();
  386. nickPanel.Dock = DockStyle.Fill;
  387. nickPanel.Controls.Add(nickList);
  388. nickListTab.Controls.Add(nickPanel);
  389. nickListTab.BackColor = IrcColor.colors[iceChatColors.PanelHeaderBG1];
  390. nickListTab.ForeColor = IrcColor.colors[iceChatColors.PanelHeaderForeColor];
  391. this.panelDockRight.TabControl.TabPages.Add(nickListTab);
  392. channelListTab = new TabPage("Favorite Channels");
  393. Panel channelPanel = new Panel();
  394. channelPanel.Dock = DockStyle.Fill;
  395. channelPanel.Controls.Add(channelList);
  396. channelListTab.Controls.Add(channelPanel);
  397. channelListTab.BackColor = IrcColor.colors[iceChatColors.PanelHeaderBG1];
  398. channelListTab.ForeColor = IrcColor.colors[iceChatColors.PanelHeaderForeColor];
  399. this.panelDockRight.TabControl.TabPages.Add(channelListTab);
  400. buddyListTab = new TabPage("Buddy List");
  401. Panel buddyPanel = new Panel();
  402. buddyPanel.Dock = DockStyle.Fill;
  403. buddyPanel.Controls.Add(buddyList);
  404. buddyListTab.Controls.Add(buddyPanel);
  405. buddyListTab.BackColor = IrcColor.colors[iceChatColors.PanelHeaderBG1];
  406. buddyListTab.ForeColor = IrcColor.colors[iceChatColors.PanelHeaderForeColor];
  407. this.panelDockRight.TabControl.TabPages.Add(buddyListTab);
  408. panelDockLeft.Width = iceChatOptions.LeftPanelWidth;
  409. panelDockLeft.TabControl.Alignment = TabAlignment.Left;
  410. panelDockRight.Width = iceChatOptions.RightPanelWidth;
  411. panelDockRight.TabControl.Alignment = TabAlignment.Right;
  412. nickList.Font = new Font(iceChatFonts.FontSettings[3].FontName, iceChatFonts.FontSettings[3].FontSize);
  413. serverTree.Font = new Font(iceChatFonts.FontSettings[4].FontName, iceChatFonts.FontSettings[4].FontSize);
  414. inputPanel.OnCommand +=new InputPanel.OnCommandDelegate(inputPanel_OnCommand);
  415. inputPanel.InputBoxFont = new Font(iceChatFonts.FontSettings[5].FontName, iceChatFonts.FontSettings[5].FontSize);
  416. mainTabControl.SelectedIndexChanged += new IceTabControl.TabEventHandler(TabSelectedIndexChanged);
  417. mainTabControl.OnTabClosed += new IceTabControl.TabClosedDelegate(mainTabControl_OnTabClosed);
  418. panelDockLeft.Initialize();
  419. panelDockRight.Initialize();
  420. //menuMainStrip.Font = new Font(iceChatFonts.FontSettings[7].FontName, iceChatFonts.FontSettings[7].FontSize);
  421. serverTree.NewServerConnection += new NewServerConnectionDelegate(NewServerConnection);
  422. serverTree.SaveDefault += new ServerTree.SaveDefaultDelegate(OnDefaultServerSettings);
  423. CreateDefaultConsoleWindow();
  424. this.FormClosing += new FormClosingEventHandler(FormMainClosing);
  425. this.Resize += new EventHandler(FormMainResize);
  426. if (iceChatOptions.IdentServer)
  427. identServer = new IdentServer();
  428. loadedPlugins = new List<IPluginIceChat>();
  429. if (iceChatLanguage.LanguageName != "English") ApplyLanguage(); // ApplyLanguage can first be called after all child controls are created
  430. WindowMessage(null, "Console", "Data Folder: " + currentFolder, 4, true);
  431. WindowMessage(null, "Console", "Plugins Folder: " + pluginsFolder, 4, true);
  432. //check for an update
  433. System.Threading.Thread checkThread = new System.Threading.Thread(checkForUpdate);
  434. checkThread.Name = "CheckUpdateThread";
  435. checkThread.Start();
  436. //check for router ip
  437. if (iceChatOptions.DCCLocalIP == null || iceChatOptions.DCCLocalIP.Length == 0)
  438. {
  439. System.Threading.Thread thread = new System.Threading.Thread(getLocalIPAddress);
  440. thread.Name = "DCCIP";
  441. thread.Start();
  442. }
  443. splash.Close();
  444. splash.Dispose();
  445. //load any plugin addons
  446. LoadPlugins();
  447. //load the plugin settings file
  448. LoadPluginFiles();
  449. //set any plugins as disabled
  450. //add any items top the pluginsFile if they do not exist, or remove any that do not
  451. foreach (IPluginIceChat ipc in FormMain.Instance.IceChatPlugins)
  452. {
  453. bool found = false;
  454. for (int i = 0; i < iceChatPlugins.listPlugins.Count; i++)
  455. {
  456. if (iceChatPlugins.listPlugins[i].PluginFile.Equals(ipc.FileName))
  457. {
  458. found = true;
  459. //check if the plugin is enabled or not
  460. if (iceChatPlugins.listPlugins[i].Enabled == false)
  461. {
  462. WindowMessage(null, "Console", "Disabled Plugin - " + ipc.Name + " v" + ipc.Version, 4, true);
  463. foreach (ToolStripMenuItem t in pluginsToolStripMenuItem.DropDownItems)
  464. if (t.ToolTipText.ToLower() == ipc.FileName.ToLower())
  465. t.Image = StaticMethods.LoadResourceImage("CloseButton.png");
  466. ipc.Enabled = false;
  467. }
  468. }
  469. }
  470. if (found == false)
  471. {
  472. //plugin file not found in plugin Items file, add it
  473. PluginItem item = new PluginItem();
  474. item.Enabled = true;
  475. item.PluginFile = ipc.FileName;
  476. iceChatPlugins.AddPlugin(item);
  477. SavePluginFiles();
  478. }
  479. //fire the event that the program has fully loaded
  480. if (ipc.Enabled == true)
  481. ipc.MainProgramLoaded();
  482. }
  483. if (iceChatPlugins.listPlugins.Count != loadedPlugins.Count)
  484. {
  485. //find the file that is missing
  486. List<int> removeItems = new List<int>();
  487. for (int i = 0; i < iceChatPlugins.listPlugins.Count; i++)
  488. {
  489. bool found = false;
  490. foreach (IPluginIceChat ipc in FormMain.Instance.IceChatPlugins)
  491. {
  492. if (iceChatPlugins.listPlugins[i].PluginFile.Equals(ipc.FileName))
  493. found = true;
  494. }
  495. if (found == false)
  496. removeItems.Add(i);
  497. }
  498. if (removeItems.Count > 0)
  499. {
  500. try
  501. {
  502. foreach (int i in removeItems)
  503. iceChatPlugins.listPlugins.Remove(iceChatPlugins.listPlugins[i]);
  504. }
  505. catch { }
  506. SavePluginFiles();
  507. }
  508. }
  509. this.Activated += new EventHandler(FormMainActivated);
  510. nickList.ShowNickButtons = iceChatOptions.ShowNickButtons;
  511. serverTree.ShowServerButtons = iceChatOptions.ShowServerButtons;
  512. this.flashTrayIconTimer = new System.Timers.Timer(2000);
  513. this.flashTrayIconTimer.Enabled = false;
  514. this.flashTrayIconTimer.Elapsed += new System.Timers.ElapsedEventHandler(flashTrayIconTimer_Elapsed);
  515. this.notifyIcon.Tag = "off";
  516. this.flashTrayCount = 0;
  517. this.flashTaskBarIconTimer = new System.Timers.Timer(2000);
  518. this.flashTaskBarIconTimer.Enabled = false;
  519. this.flashTaskBarIconTimer.Elapsed += new System.Timers.ElapsedEventHandler(flashTaskBarIconTimer_Elapsed);
  520. this.Tag = "off";
  521. this.flashTrayCount = 0;
  522. ToolStripMenuItem closeWindow = new ToolStripMenuItem(StaticMethods.LoadResourceImage("CloseButton.png"));
  523. closeWindow.Alignment = ToolStripItemAlignment.Right;
  524. closeWindow.Click += new EventHandler(closeWindow_Click);
  525. menuMainStrip.Items.Add(closeWindow);
  526. }
  527. private void closeWindow_Click(object sender, EventArgs e)
  528. {
  529. //close the current window
  530. mainTabControl.CloseCurrentTab();
  531. }
  532. private void UpdateIcon(string iconName, string tag)
  533. {
  534. this.Invoke((MethodInvoker)delegate()
  535. {
  536. this.Icon = System.Drawing.Icon.FromHandle(StaticMethods.LoadResourceImage(iconName).GetHicon());
  537. this.Tag = tag;
  538. });
  539. }
  540. private void UpdateTrayIcon(string iconName, string tag)
  541. {
  542. this.Invoke((MethodInvoker)delegate()
  543. {
  544. this.notifyIcon.Icon = System.Drawing.Icon.FromHandle(StaticMethods.LoadResourceImage(iconName).GetHicon());
  545. this.notifyIcon.Tag = tag;
  546. });
  547. }
  548. private void flashTaskBarIconTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  549. {
  550. if (this.WindowState == FormWindowState.Minimized)
  551. {
  552. if (this.Tag.Equals("on"))
  553. {
  554. UpdateIcon("new-tray-icon.ico", "off");
  555. }
  556. else
  557. {
  558. UpdateIcon("tray-icon-flash.ico", "on");
  559. }
  560. flashTaskBarCount++;
  561. if (flashTaskBarCount == 10)
  562. {
  563. this.flashTaskBarIconTimer.Stop();
  564. UpdateIcon("new-tray-icon.ico", "off");
  565. flashTaskBarCount = 0;
  566. }
  567. }
  568. else
  569. {
  570. this.flashTaskBarIconTimer.Stop();
  571. UpdateIcon("new-tray-icon.ico", "off");
  572. }
  573. }
  574. private void flashTrayIconTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
  575. {
  576. if (this.notifyIcon.Visible == true)
  577. {
  578. if (this.notifyIcon.Tag.Equals("on"))
  579. {
  580. UpdateTrayIcon("new-tray-icon.ico", "off");
  581. }
  582. else
  583. {
  584. UpdateTrayIcon("tray-icon-flash.ico", "on");
  585. }
  586. flashTrayCount++;
  587. if (flashTrayCount == 10)
  588. {
  589. this.flashTrayIconTimer.Stop();
  590. UpdateTrayIcon("new-tray-icon.ico", "off");
  591. flashTrayCount = 0;
  592. }
  593. }
  594. else
  595. {
  596. this.flashTrayIconTimer.Stop();
  597. UpdateTrayIcon("new-tray-icon.ico", "off");
  598. }
  599. }
  600. private void FormMainActivated(object sender, EventArgs e)
  601. {
  602. if (iceChatOptions.IsOnTray == true)
  603. {
  604. minimizeToTrayToolStripMenuItem.PerformClick();
  605. }
  606. //auto start any Auto Connect Servers
  607. foreach (ServerSetting s in serverTree.ServersCollection.listServers)
  608. {
  609. if (s.AutoStart)
  610. {
  611. NewServerConnection(s);
  612. }
  613. }
  614. //remove the event handler, because it only needs to be run once, on startup
  615. this.Activated -= FormMainActivated;
  616. }
  617. private void FormMainResize(object sender, EventArgs e)
  618. {
  619. if (this.WindowState == FormWindowState.Minimized)
  620. {
  621. if (iceChatOptions.MinimizeToTray)
  622. {
  623. this.notifyIcon.Visible = true;
  624. this.Hide();
  625. }
  626. }
  627. }
  628. private void toolStripMain_VisibleChanged(object sender, EventArgs e)
  629. {
  630. toolBarToolStripMenuItem.Checked =toolStripMain.Visible;
  631. }
  632. /// <summary>
  633. /// Save Default Server Settings
  634. /// </summary>
  635. private void OnDefaultServerSettings()
  636. {
  637. SaveOptions();
  638. }
  639. #region Load Language File
  640. private LanguageItem LoadLanguageItem(string languageFileName)
  641. {
  642. if (File.Exists(languageFileName))
  643. {
  644. LanguageItem languageItem = null;
  645. XmlSerializer deserializer = new XmlSerializer(typeof(LanguageItem));
  646. TextReader textReader = new StreamReader(languageFileName);
  647. try
  648. {
  649. languageItem = (LanguageItem)deserializer.Deserialize(textReader);
  650. languageItem.LanguageFile = languageFileName;
  651. }
  652. catch
  653. {
  654. languageItem = null;
  655. }
  656. finally
  657. {
  658. textReader.Close();
  659. textReader.Dispose();
  660. }
  661. return languageItem;
  662. }
  663. else
  664. {
  665. return null;
  666. }
  667. }
  668. private void LoadLanguage()
  669. {
  670. if (File.Exists(currentLanguageFile.LanguageFile))
  671. {
  672. XmlSerializer deserializer = new XmlSerializer(typeof(IceChatLanguage));
  673. TextReader textReader = new StreamReader(currentLanguageFile.LanguageFile);
  674. iceChatLanguage = (IceChatLanguage)deserializer.Deserialize(textReader);
  675. textReader.Close();
  676. textReader.Dispose();
  677. }
  678. else
  679. {
  680. iceChatLanguage = new IceChatLanguage();
  681. //write the language file
  682. XmlSerializer serializer = new XmlSerializer(typeof(IceChatLanguage));
  683. TextWriter textWriter = new StreamWriter(currentFolder + Path.DirectorySeparatorChar + "Languages" + Path.DirectorySeparatorChar + "English.xml");
  684. serializer.Serialize(textWriter,iceChatLanguage);
  685. textWriter.Close();
  686. textWriter.Dispose();
  687. }
  688. }
  689. private void ApplyLanguage()
  690. {
  691. mainToolStripMenuItem.Text = iceChatLanguage.mainToolStripMenuItem;
  692. minimizeToTrayToolStripMenuItem.Text = iceChatLanguage.minimizeToTrayToolStripMenuItem;
  693. debugWindowToolStripMenuItem.Text = iceChatLanguage.debugWindowToolStripMenuItem;
  694. exitToolStripMenuItem.Text = iceChatLanguage.exitToolStripMenuItem;
  695. optionsToolStripMenuItem.Text = iceChatLanguage.optionsToolStripMenuItem;
  696. iceChatSettingsToolStripMenuItem.Text = iceChatLanguage.iceChatSettingsToolStripMenuItem;
  697. iceChatColorsToolStripMenuItem.Text = iceChatLanguage.iceChatColorsToolStripMenuItem;
  698. iceChatEditorToolStripMenuItem.Text = iceChatLanguage.iceChatEditorToolStripMenuItem;
  699. pluginsToolStripMenuItem.Text = iceChatLanguage.pluginsToolStripMenuItem;
  700. viewToolStripMenuItem.Text = iceChatLanguage.viewToolStripMenuItem;
  701. serverListToolStripMenuItem.Text = iceChatLanguage.serverListToolStripMenuItem;
  702. nickListToolStripMenuItem.Text = iceChatLanguage.nickListToolStripMenuItem;
  703. statusBarToolStripMenuItem.Text = iceChatLanguage.statusBarToolStripMenuItem;
  704. toolBarToolStripMenuItem.Text = iceChatLanguage.toolBarToolStripMenuItem;
  705. helpToolStripMenuItem.Text = iceChatLanguage.helpToolStripMenuItem;
  706. codePlexPageToolStripMenuItem.Text = iceChatLanguage.codePlexPageToolStripMenuItem;
  707. iceChatHomePageToolStripMenuItem.Text = iceChatLanguage.iceChatHomePageToolStripMenuItem;
  708. forumsToolStripMenuItem.Text = iceChatLanguage.forumsToolStripMenuItem;
  709. aboutToolStripMenuItem.Text = iceChatLanguage.aboutToolStripMenuItem;
  710. toolStripQuickConnect.Text = iceChatLanguage.toolStripQuickConnect;
  711. toolStripSettings.Text = iceChatLanguage.toolStripSettings;
  712. toolStripColors.Text = iceChatLanguage.toolStripColors;
  713. toolStripEditor.Text = iceChatLanguage.toolStripEditor;
  714. toolStripAway.Text = iceChatLanguage.toolStripAway;
  715. toolStripSystemTray.Text = iceChatLanguage.toolStripSystemTray;
  716. toolStripStatus.Text = iceChatLanguage.toolStripStatus;
  717. channelListTab.Text = iceChatLanguage.tabPageFaveChannels;
  718. nickListTab.Text = iceChatLanguage.tabPageNicks;
  719. serverListTab.Text = iceChatLanguage.serverTreeHeader;
  720. channelList.ApplyLanguage();
  721. nickList.ApplyLanguage();
  722. serverTree.ApplyLanguage();
  723. inputPanel.ApplyLanguage();
  724. mainTabControl.Invalidate(); // repaint tabs to apply changes to user drawn tabs
  725. }
  726. #endregion
  727. private void FormMainClosing(object sender, FormClosingEventArgs e)
  728. {
  729. //check if there are any connections open
  730. foreach (IRCConnection c in serverTree.ServerConnections.Values)
  731. {
  732. if (c.IsConnected)
  733. {
  734. DialogResult dr = MessageBox.Show("You are connected to a Server(s), are you sure you want to close IceChat?", "Close IceChat", MessageBoxButtons.OKCancel);
  735. if (e.CloseReason == CloseReason.UserClosing && dr == DialogResult.Cancel)
  736. {
  737. e.Cancel = true;
  738. return;
  739. }
  740. else
  741. break;
  742. }
  743. }
  744. if (identServer != null)
  745. {
  746. identServer.Stop();
  747. identServer = null;
  748. }
  749. foreach (IPluginIceChat ipc in loadedPlugins)
  750. ipc.Dispose();
  751. //unload and dispose of all the plugins
  752. foreach (IPluginIceChat ipc in loadedPlugins)
  753. {
  754. //AppDomain.Unload(ipc.domain);
  755. ipc.Dispose();
  756. }
  757. for (int i = 0; i < loadedPlugins.Count; i++)
  758. {
  759. loadedPlugins.RemoveAt(i);
  760. }
  761. //disconnect all the servers
  762. foreach (IRCConnection c in serverTree.ServerConnections.Values)
  763. {
  764. if (c.IsConnected)
  765. {
  766. c.AttemptReconnect = false;
  767. ParseOutGoingCommand(c, "//quit " + c.ServerSetting.QuitMessage);
  768. }
  769. }
  770. if (iceChatOptions.SaveWindowPosition)
  771. {
  772. //save the window position , as long as its not minimized
  773. if (this.WindowState != FormWindowState.Minimized && this.notifyIcon.Visible == false)
  774. {
  775. iceChatOptions.WindowLocation = this.Location;
  776. iceChatOptions.WindowSize = this.Size;
  777. if (!panelDockRight.IsDocked)
  778. iceChatOptions.RightPanelWidth = panelDockRight.Width;
  779. if (!panelDockLeft.IsDocked)
  780. iceChatOptions.LeftPanelWidth = panelDockLeft.Width;
  781. }
  782. iceChatOptions.IsOnTray = this.notifyIcon.Visible;
  783. SaveOptions();
  784. }
  785. //mutex.ReleaseMutex();
  786. if (errorFile != null)
  787. {
  788. errorFile.Close();
  789. errorFile.Dispose();
  790. }
  791. }
  792. /// <summary>
  793. /// Play the specified sound file (currently only supports WAV files)
  794. /// </summary>
  795. /// <param name="sound"></param>
  796. internal void PlaySoundFile(string key)
  797. {
  798. IceChatSounds.SoundEntry sound = IceChatSounds.getSound(key);
  799. if (sound != null && !muteAllSounds)
  800. {
  801. string file = sound.getSoundFile();
  802. if (file != null && file.Length > 0)
  803. {
  804. try
  805. {
  806. if (iceChatOptions.SoundUseExternalCommand && iceChatOptions.SoundExternalCommand.Length > 0)
  807. ParseOutGoingCommand(inputPanel.CurrentConnection, iceChatOptions.SoundExternalCommand + " " + file);
  808. else
  809. ParseOutGoingCommand(inputPanel.CurrentConnection, "/play " + file);
  810. //player.SoundLocation = @file;
  811. //player.Play();
  812. }
  813. catch { }
  814. }
  815. }
  816. }
  817. /// <summary>
  818. /// Create a Default Tab for showing Welcome Information
  819. /// </summary>
  820. private void CreateDefaultConsoleWindow()
  821. {
  822. IceTabPage p = new IceTabPage(IceTabPage.WindowType.Console, "Console");
  823. p.AddConsoleTab(iceChatLanguage.consoleTabWelcome);
  824. mainTabControl.TabPages.Add(p);
  825. WindowMessage(null, "Console", "\x00034Welcome to " + ProgramID + " " + VersionID, 1, false);
  826. WindowMessage(null, "Console", "\x00034** This is a Release Candidate version, fully functional, not all the options are added **", 1, false);
  827. WindowMessage(null, "Console", "\x00033If you want a fully working version of \x0002IceChat\x0002, visit http://www.icechat.net and download IceChat 7.70", 1, false);
  828. WindowMessage(null, "Console", "\x00034Please visit \x00030,4#icechat\x0003 on \x00030,2irc://irc.quakenet.org/icechat\x0003 if you wish to help with this project", 1, true);
  829. }
  830. #region Internal Properties
  831. /// <summary>
  832. /// Gets the instance of the Nick List
  833. /// </summary>
  834. internal NickList NickList
  835. {
  836. get { return nickList; }
  837. }
  838. /// <summary>
  839. /// Gets the instance of the Server Tree
  840. /// </summary>
  841. internal ServerTree ServerTree
  842. {
  843. get { return serverTree; }
  844. }
  845. /// <summary>
  846. /// Gets the instance of the Main Tab Control
  847. /// </summary>
  848. internal IceTabControl TabMain
  849. {
  850. get { return mainTabControl; }
  851. }
  852. /// <summary>
  853. /// Gets the instance of the InputPanel
  854. /// </summary>
  855. internal InputPanel InputPanel
  856. {
  857. get
  858. {
  859. return this.inputPanel;
  860. }
  861. }
  862. internal IceChatOptions IceChatOptions
  863. {
  864. get
  865. {
  866. return this.iceChatOptions;
  867. }
  868. }
  869. internal IceChatMessageFormat MessageFormats
  870. {
  871. get
  872. {
  873. return this.iceChatMessages;
  874. }
  875. }
  876. internal IceChatFontSetting IceChatFonts
  877. {
  878. get
  879. {
  880. return this.iceChatFonts;
  881. }
  882. }
  883. internal IceChatColors IceChatColors
  884. {
  885. get
  886. {
  887. return this.iceChatColors;
  888. }
  889. }
  890. internal IceChatSounds IceChatSounds
  891. {
  892. get
  893. {
  894. return this.iceChatSounds;
  895. }
  896. }
  897. internal IceChatAliases IceChatAliases
  898. {
  899. get
  900. {
  901. return iceChatAliases;
  902. }
  903. set
  904. {
  905. iceChatAliases = value;
  906. //save the aliases
  907. SaveAliases();
  908. }
  909. }
  910. internal IceChatPopupMenus IceChatPopupMenus
  911. {
  912. get
  913. {
  914. return iceChatPopups;
  915. }
  916. set
  917. {
  918. iceChatPopups = value;
  919. //save the popups
  920. SavePopups();
  921. }
  922. }
  923. internal IceChatEmoticon IceChatEmoticons
  924. {
  925. get
  926. {
  927. return iceChatEmoticons;
  928. }
  929. set
  930. {
  931. iceChatEmoticons = value;
  932. //save the Emoticons
  933. SaveEmoticons();
  934. }
  935. }
  936. internal IceChatLanguage IceChatLanguage
  937. {
  938. get
  939. {
  940. return iceChatLanguage;
  941. }
  942. }
  943. internal List<LanguageItem> IceChatLanguageFiles
  944. {
  945. get
  946. {
  947. return languageFiles;
  948. }
  949. }
  950. internal LanguageItem IceChatCurrentLanguageFile
  951. {
  952. get
  953. {
  954. return currentLanguageFile;
  955. }
  956. set
  957. {
  958. if (currentLanguageFile != value)
  959. {
  960. currentLanguageFile = value;
  961. LoadLanguage();
  962. ApplyLanguage();
  963. }
  964. }
  965. }
  966. internal string FavoriteChannelsFile
  967. {
  968. get
  969. {
  970. return favoriteChannelsFile;
  971. }
  972. }
  973. internal BuddyList BuddyList
  974. {
  975. get
  976. {
  977. return this.buddyList;
  978. }
  979. }
  980. internal string MessagesFile
  981. {
  982. get
  983. {
  984. return messagesFile;
  985. }
  986. set
  987. {
  988. messagesFile = value;
  989. }
  990. }
  991. internal string ColorsFile
  992. {
  993. get
  994. {
  995. return colorsFile;
  996. }
  997. set
  998. {
  999. colorsFile = value;
  1000. }
  1001. }
  1002. internal string ServersFile
  1003. {
  1004. get
  1005. {
  1006. return serversFile;
  1007. }
  1008. }
  1009. internal string AliasesFile
  1010. {
  1011. get
  1012. {
  1013. return aliasesFile;
  1014. }
  1015. }
  1016. internal string PopupsFile
  1017. {
  1018. get
  1019. {
  1020. return popupsFile;
  1021. }
  1022. }
  1023. internal List<IPluginIceChat> IceChatPlugins
  1024. {
  1025. get
  1026. {
  1027. return loadedPlugins;
  1028. }
  1029. }
  1030. public string LogsFolder
  1031. {
  1032. get
  1033. {
  1034. return logsFolder;
  1035. }
  1036. }
  1037. public string CurrentFolder
  1038. {
  1039. get
  1040. {
  1041. return currentFolder;
  1042. }
  1043. }
  1044. internal string EmoticonsFolder
  1045. {
  1046. get
  1047. {
  1048. return System.IO.Path.GetDirectoryName(emoticonsFile);
  1049. }
  1050. }
  1051. internal void StatusText(string data)
  1052. {
  1053. try
  1054. {
  1055. this.Invoke((MethodInvoker)delegate()
  1056. {
  1057. toolStripStatus.Text = "Status: " + data;
  1058. });
  1059. }
  1060. catch { }
  1061. }
  1062. #endregion
  1063. #region Private Properti

Large files files are truncated, but you can click here to view the full file