PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/FD4/External/Plugins/StartPage/PluginMain.cs

https://bitbucket.org/kkszysiu/flashdevelop
C# | 290 lines | 192 code | 35 blank | 63 comment | 28 complexity | ef1d8d53086b78cf1cad3f12f67d4b1f MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Web;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.ComponentModel;
  7. using WeifenLuo.WinFormsUI.Docking;
  8. using ProjectManager.Projects;
  9. using PluginCore.Localization;
  10. using PluginCore.Utilities;
  11. using PluginCore.Managers;
  12. using PluginCore.Helpers;
  13. using StartPage.Controls;
  14. using PluginCore;
  15. namespace StartPage
  16. {
  17. public class PluginMain : IPlugin
  18. {
  19. private String pluginName = "StartPage";
  20. private String pluginGuid = "e4246322-bc55-4f4a-99c8-aaeeed0a7b9a";
  21. private String pluginHelp = "www.flashdevelop.org/community/";
  22. private String pluginDesc = "Adds a start page to FlashDevelop.";
  23. private String pluginAuth = "FlashDevelop Team";
  24. private Boolean justOpened = true;
  25. private String defaultRssUrl = "";
  26. private String defaultStartPageUrl = "";
  27. private StartPageWebBrowser startPageWebBrowser;
  28. private String settingFilename;
  29. private Settings settingObject;
  30. private DockContent startPage;
  31. private Image pluginImage;
  32. #region Required Properties
  33. /// <summary>
  34. /// Api level of the plugin
  35. /// </summary>
  36. public Int32 Api
  37. {
  38. get { return 1; }
  39. }
  40. /// <summary>
  41. /// Name of the plugin
  42. /// </summary>
  43. public String Name
  44. {
  45. get { return this.pluginName; }
  46. }
  47. /// <summary>
  48. /// GUID of the plugin
  49. /// </summary>
  50. public String Guid
  51. {
  52. get { return this.pluginGuid; }
  53. }
  54. /// <summary>
  55. /// Author of the plugin
  56. /// </summary>
  57. public String Author
  58. {
  59. get { return this.pluginAuth; }
  60. }
  61. /// <summary>
  62. /// Description of the plugin
  63. /// </summary>
  64. public String Description
  65. {
  66. get { return this.pluginDesc; }
  67. }
  68. /// <summary>
  69. /// Web address for help
  70. /// </summary>
  71. public String Help
  72. {
  73. get { return this.pluginHelp; }
  74. }
  75. /// <summary>
  76. /// Object that contains the settings
  77. /// </summary>
  78. public Object Settings
  79. {
  80. get { return this.settingObject; }
  81. }
  82. #endregion
  83. #region Required Methods
  84. /// <summary>
  85. /// Initializes the plugin
  86. /// </summary>
  87. public void Initialize()
  88. {
  89. this.InitBasics();
  90. this.LoadSettings();
  91. this.AddEventHandlers();
  92. this.CreateMenuItem();
  93. }
  94. /// <summary>
  95. /// Disposes the plugin
  96. /// </summary>
  97. public void Dispose()
  98. {
  99. this.SaveSettings();
  100. }
  101. /// <summary>
  102. /// Handles the incoming events
  103. /// </summary>
  104. public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority prority)
  105. {
  106. switch (e.Type)
  107. {
  108. case EventType.Command :
  109. DataEvent de = (DataEvent)e;
  110. switch (de.Action)
  111. {
  112. case ProjectManager.ProjectManagerEvents.Project :
  113. // Close pluginPanel if the user has the setting checked and a project is opened
  114. if (de.Data != null & this.startPage != null)
  115. {
  116. if (this.settingObject.CloseOnProjectOpen) this.startPage.Close();
  117. if (this.startPage != null)
  118. {
  119. // The project manager does not update recent projects until after
  120. // it broadcasts this event so we'll wait a little bit before refreshing
  121. Timer timer = new Timer();
  122. timer.Interval = 100;
  123. timer.Tick += delegate { timer.Stop(); if (!this.startPageWebBrowser.IsDisposed) this.startPageWebBrowser.SendProjectInfo(); };
  124. timer.Start();
  125. }
  126. }
  127. break;
  128. }
  129. break;
  130. case EventType.FileEmpty :
  131. if ((this.justOpened & (this.settingObject.ShowStartPageOnStartup == ShowStartPageOnStartupEnum.Always || this.settingObject.ShowStartPageOnStartup == ShowStartPageOnStartupEnum.NotRestoringSession)) || this.settingObject.ShowStartPageInsteadOfUntitled)
  132. {
  133. this.ShowStartPage();
  134. this.justOpened = false;
  135. e.Handled = true;
  136. }
  137. break;
  138. case EventType.RestoreSession:
  139. ISession session = ((DataEvent)e).Data as ISession;
  140. if (session.Type != SessionType.Startup) return; // handle only startup sessions
  141. if (this.settingObject.ShowStartPageOnStartup == ShowStartPageOnStartupEnum.Always) e.Handled = true;
  142. else if (session.Files.Count > 0) this.justOpened = false;
  143. break;
  144. }
  145. }
  146. #endregion
  147. #region Custom Methods
  148. private String CurrentPageUrl { get { return this.settingObject.UseCustomStartPage ? this.settingObject.CustomStartPage : this.defaultStartPageUrl; } }
  149. private String CurrentRssUrl { get { return this.settingObject.UseCustomRssFeed ? this.settingObject.CustomRssFeed : this.defaultRssUrl; } }
  150. /// <summary>
  151. /// Initializes important variables
  152. /// </summary>
  153. public void InitBasics()
  154. {
  155. String dataDir = Path.Combine(PathHelper.DataDir, "StartPage");
  156. String startPageDir = Path.Combine(PathHelper.AppDir, "StartPage");
  157. String localeName = PluginBase.MainForm.Settings.LocaleVersion.ToString();
  158. String version = Application.ProductName.Substring(13, Application.ProductName.IndexOf(" for") - 13);
  159. String fileWithArgs = "index.html?l=" + localeName + "&v=" + HttpUtility.HtmlEncode(version);
  160. this.defaultStartPageUrl = Path.Combine(startPageDir, fileWithArgs);
  161. this.defaultRssUrl = "http://www.flashdevelop.org/community/rss.php?f=15";
  162. if (!Directory.Exists(dataDir)) Directory.CreateDirectory(dataDir);
  163. this.settingFilename = Path.Combine(dataDir, "Settings.fdb");
  164. this.pluginDesc = TextHelper.GetString("Info.Description");
  165. this.pluginImage = PluginBase.MainForm.FindImage("224");
  166. }
  167. /// <summary>
  168. /// Adds the required event handlers
  169. /// </summary>
  170. public void AddEventHandlers()
  171. {
  172. EventManager.AddEventHandler(this, EventType.Command);
  173. EventManager.AddEventHandler(this, EventType.FileEmpty);
  174. EventManager.AddEventHandler(this, EventType.RestoreSession);
  175. }
  176. /// <summary>
  177. /// Loads the plugin settings
  178. /// </summary>
  179. public void LoadSettings()
  180. {
  181. this.settingObject = new Settings();
  182. if (!File.Exists(this.settingFilename)) this.SaveSettings();
  183. else
  184. {
  185. Object obj = ObjectSerializer.Deserialize(this.settingFilename, this.settingObject);
  186. this.settingObject = (Settings)obj;
  187. }
  188. }
  189. /// <summary>
  190. /// Saves the plugin settings
  191. /// </summary>
  192. public void SaveSettings()
  193. {
  194. ObjectSerializer.Serialize(this.settingFilename, this.settingObject);
  195. }
  196. /// <summary>
  197. /// Creates a menu item for the plugin
  198. /// </summary>
  199. public void CreateMenuItem()
  200. {
  201. String title = TextHelper.GetString("Label.ViewMenuItem");
  202. ToolStripMenuItem viewMenu = (ToolStripMenuItem)PluginBase.MainForm.FindMenuItem("ViewMenu");
  203. ToolStripMenuItem viewItem = new ToolStripMenuItem(title, this.pluginImage, new EventHandler(this.ViewMenuClick));
  204. PluginBase.MainForm.RegisterShortcutItem("ViewMenu.ShowStartPage", viewItem);
  205. viewMenu.DropDownItems.Add(viewItem);
  206. }
  207. /// <summary>
  208. /// Creates the Start Page Tab
  209. /// </summary>
  210. public void CreateStartPage()
  211. {
  212. this.startPageWebBrowser = new StartPageWebBrowser(this.CurrentPageUrl, this.CurrentRssUrl);
  213. this.startPage = PluginBase.MainForm.CreateCustomDocument(this.startPageWebBrowser);
  214. this.startPage.Icon = ImageKonverter.ImageToIcon(this.pluginImage);
  215. this.startPage.Disposed += new EventHandler(this.PluginPanelDisposed);
  216. this.startPage.Closing += new CancelEventHandler(this.PluginPanelClosing);
  217. this.startPage.Text = TextHelper.GetString("Title.StartPage");
  218. }
  219. /// <summary>
  220. /// Shows the Start Page Tab and creates if necessary.
  221. /// </summary>
  222. public void ShowStartPage()
  223. {
  224. if (this.startPage == null) this.CreateStartPage();
  225. else this.startPage.Show();
  226. }
  227. #endregion
  228. #region Event Handlers
  229. /// <summary>
  230. /// Shows the start page.
  231. /// </summary>
  232. private void ViewMenuClick(Object sender, System.EventArgs e)
  233. {
  234. this.ShowStartPage();
  235. }
  236. /// <summary>
  237. /// Some internal event handling for closing.
  238. /// </summary>
  239. private void PluginPanelClosing(Object sender, CancelEventArgs e)
  240. {
  241. if (this.settingObject.ShowStartPageInsteadOfUntitled && PluginBase.MainForm.Documents.Length == 1)
  242. {
  243. e.Cancel = true;
  244. }
  245. }
  246. /// <summary>
  247. /// Reset the start page reference.
  248. /// </summary>
  249. private void PluginPanelDisposed(Object sender, EventArgs e)
  250. {
  251. this.startPage = null;
  252. }
  253. #endregion
  254. }
  255. }