PageRenderTime 119ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/FD4/External/Plugins/SourceControl/PluginMain.cs

https://bitbucket.org/kkszysiu/flashdevelop
C# | 361 lines | 278 code | 35 blank | 48 comment | 13 complexity | c1c744692cf3a5b1f66bae4d1f688976 MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.ComponentModel;
  6. using WeifenLuo.WinFormsUI.Docking;
  7. using PluginCore.Localization;
  8. using PluginCore.Utilities;
  9. using PluginCore.Managers;
  10. using PluginCore.Helpers;
  11. using SourceControl.Actions;
  12. using ProjectManager.Projects;
  13. using ProjectManager.Actions;
  14. using ProjectManager;
  15. using PluginCore;
  16. namespace SourceControl
  17. {
  18. public class PluginMain : IPlugin
  19. {
  20. private String pluginName = "SourceControl";
  21. private String pluginGuid = "42ac7fab-421b-1f38-a985-5735468ac489";
  22. private String pluginHelp = "www.flashdevelop.org/community/";
  23. private String pluginDesc = "Source Control integration for FlashDevelop.";
  24. private String pluginAuth = "FlashDevelop Team";
  25. private static Settings settingObject;
  26. private String settingFilename;
  27. private Image pluginImage;
  28. private Boolean ready;
  29. #region Required Properties
  30. /// <summary>
  31. /// Api level of the plugin
  32. /// </summary>
  33. public Int32 Api
  34. {
  35. get { return 1; }
  36. }
  37. /// <summary>
  38. /// Name of the plugin
  39. /// </summary>
  40. public String Name
  41. {
  42. get { return this.pluginName; }
  43. }
  44. /// <summary>
  45. /// GUID of the plugin
  46. /// </summary>
  47. public String Guid
  48. {
  49. get { return this.pluginGuid; }
  50. }
  51. /// <summary>
  52. /// Author of the plugin
  53. /// </summary>
  54. public String Author
  55. {
  56. get { return this.pluginAuth; }
  57. }
  58. /// <summary>
  59. /// Description of the plugin
  60. /// </summary>
  61. public String Description
  62. {
  63. get { return this.pluginDesc; }
  64. }
  65. /// <summary>
  66. /// Web address for help
  67. /// </summary>
  68. public String Help
  69. {
  70. get { return this.pluginHelp; }
  71. }
  72. /// <summary>
  73. /// Object that contains the settings
  74. /// </summary>
  75. [Browsable(false)]
  76. public Object Settings
  77. {
  78. get { return settingObject; }
  79. }
  80. #endregion
  81. #region Required Methods
  82. /// <summary>
  83. /// Initializes the plugin
  84. /// </summary>
  85. public void Initialize()
  86. {
  87. this.InitBasics();
  88. this.LoadSettings();
  89. this.AddEventHandlers();
  90. }
  91. /// <summary>
  92. /// Disposes the plugin
  93. /// </summary>
  94. public void Dispose()
  95. {
  96. ProjectWatcher.Dispose();
  97. this.SaveSettings();
  98. }
  99. /// <summary>
  100. /// Handles the incoming events
  101. /// </summary>
  102. public void HandleEvent(Object sender, NotifyEvent e, HandlingPriority prority)
  103. {
  104. switch (e.Type)
  105. {
  106. case EventType.UIStarted:
  107. ProjectWatcher.Init();
  108. this.ready = true;
  109. break;
  110. // Catches Project change event and display the active project path
  111. case EventType.Command:
  112. if (!this.ready) return;
  113. DataEvent de = e as DataEvent;
  114. String cmd = de.Action;
  115. if (!cmd.StartsWith("ProjectManager.")) return;
  116. switch (cmd)
  117. {
  118. case ProjectManagerEvents.Project:
  119. ProjectWatcher.SetProject(de.Data as Project);
  120. break;
  121. case ProjectManagerEvents.TreeSelectionChanged:
  122. ProjectWatcher.SelectionChanged();
  123. break;
  124. case ProjectManagerEvents.UserRefreshTree:
  125. ProjectWatcher.ForceRefresh();
  126. break;
  127. case ProjectFileActionsEvents.FileBeforeRename:
  128. try
  129. {
  130. de.Handled = ProjectWatcher.HandleFileBeforeRename(de.Data as String);
  131. }
  132. catch (Exception ex)
  133. {
  134. ErrorManager.ShowError(ex);
  135. de.Handled = true;
  136. }
  137. break;
  138. case ProjectFileActionsEvents.FileRename:
  139. try
  140. {
  141. de.Handled = ProjectWatcher.HandleFileRename(de.Data as String[]);
  142. }
  143. catch (Exception ex)
  144. {
  145. ErrorManager.ShowError(ex);
  146. de.Handled = true;
  147. }
  148. break;
  149. case ProjectFileActionsEvents.FileDeleteSilent:
  150. try
  151. {
  152. de.Handled = ProjectWatcher.HandleFileDelete(de.Data as String[], false);
  153. }
  154. catch (Exception ex)
  155. {
  156. ErrorManager.ShowError(ex);
  157. de.Handled = true;
  158. }
  159. break;
  160. case ProjectFileActionsEvents.FileDelete:
  161. try
  162. {
  163. de.Handled = ProjectWatcher.HandleFileDelete(de.Data as String[], true);
  164. }
  165. catch (Exception ex)
  166. {
  167. ErrorManager.ShowError(ex);
  168. de.Handled = true;
  169. }
  170. break;
  171. case ProjectFileActionsEvents.FileMove:
  172. try
  173. {
  174. de.Handled = ProjectWatcher.HandleFileMove(de.Data as String[]);
  175. }
  176. catch (Exception ex)
  177. {
  178. ErrorManager.ShowError(ex);
  179. de.Handled = true;
  180. }
  181. break;
  182. case ProjectManagerEvents.BuildProject:
  183. try
  184. {
  185. de.Handled = ProjectWatcher.HandleBuildProject();
  186. }
  187. catch (Exception ex)
  188. {
  189. ErrorManager.ShowError(ex);
  190. de.Handled = true;
  191. }
  192. break;
  193. case ProjectManagerEvents.TestProject:
  194. try
  195. {
  196. de.Handled = ProjectWatcher.HandleTestProject();
  197. }
  198. catch (Exception ex)
  199. {
  200. ErrorManager.ShowError(ex);
  201. de.Handled = true;
  202. }
  203. break;
  204. case ProjectManagerEvents.BeforeSave:
  205. try
  206. {
  207. de.Handled = ProjectWatcher.HandleSaveProject((string)de.Data);
  208. }
  209. catch (Exception ex)
  210. {
  211. ErrorManager.ShowError(ex);
  212. de.Handled = true;
  213. }
  214. break;
  215. }
  216. break;
  217. case EventType.FileOpen:
  218. try
  219. {
  220. e.Handled = ProjectWatcher.HandleFileOpen((e as TextEvent).Value);
  221. }
  222. catch (Exception ex)
  223. {
  224. ErrorManager.ShowError(ex);
  225. e.Handled = true;
  226. }
  227. break;
  228. case EventType.FileReload:
  229. try
  230. {
  231. e.Handled = ProjectWatcher.HandleFileReload((e as TextEvent).Value);
  232. }
  233. catch (Exception ex)
  234. {
  235. ErrorManager.ShowError(ex);
  236. e.Handled = true;
  237. }
  238. break;
  239. case EventType.FileModifyRO:
  240. try
  241. {
  242. e.Handled = ProjectWatcher.HandleFileModifyRO((e as TextEvent).Value);
  243. }
  244. catch (Exception ex)
  245. {
  246. ErrorManager.ShowError(ex);
  247. e.Handled = true;
  248. }
  249. break;
  250. case EventType.FileNew:
  251. case EventType.FileTemplate:
  252. try
  253. {
  254. e.Handled = ProjectWatcher.HandleFileNew((e as TextEvent).Value);
  255. }
  256. catch (Exception ex)
  257. {
  258. ErrorManager.ShowError(ex);
  259. e.Handled = true;
  260. }
  261. break;
  262. }
  263. }
  264. #endregion
  265. #region Custom Methods
  266. /// <summary>
  267. /// Acessor got the settings object
  268. /// </summary>
  269. public static Settings SCSettings
  270. {
  271. get { return settingObject; }
  272. }
  273. /// <summary>
  274. /// Initializes important variables
  275. /// </summary>
  276. public void InitBasics()
  277. {
  278. String dataPath = Path.Combine(PathHelper.DataDir, "SourceControl");
  279. if (!Directory.Exists(dataPath)) Directory.CreateDirectory(dataPath);
  280. this.settingFilename = Path.Combine(dataPath, "Settings.fdb");
  281. this.pluginDesc = TextHelper.GetString("Info.Description");
  282. this.pluginImage = PluginBase.MainForm.FindImage("100");
  283. }
  284. /// <summary>
  285. /// Adds the required event handlers
  286. /// </summary>
  287. public void AddEventHandlers()
  288. {
  289. EventManager.AddEventHandler(this, EventType.UIStarted | EventType.Command | EventType.FileModifyRO | EventType.FileOpen | EventType.FileReload | EventType.FileNew | EventType.FileTemplate);
  290. }
  291. /// <summary>
  292. /// Loads the plugin settings
  293. /// </summary>
  294. public void LoadSettings()
  295. {
  296. settingObject = new Settings();
  297. if (!File.Exists(this.settingFilename)) this.SaveSettings();
  298. else
  299. {
  300. Object obj = ObjectSerializer.Deserialize(this.settingFilename, settingObject);
  301. settingObject = (Settings)obj;
  302. }
  303. // Try to find svn path from: Tools/sliksvn/
  304. if (settingObject.SVNPath == "svn.exe")
  305. {
  306. String svnCmdPath = @"Tools\sliksvn\bin\svn.exe";
  307. if (PathHelper.ResolvePath(svnCmdPath) != null) settingObject.SVNPath = svnCmdPath;
  308. }
  309. // Try to find TortoiseProc path from program files
  310. if (settingObject.TortoiseSVNProcPath == "TortoiseProc.exe")
  311. {
  312. String programFiles = Environment.GetEnvironmentVariable("ProgramFiles");
  313. String torProcPath = Path.Combine(programFiles, @"TortoiseSVN\bin\TortoiseProc.exe");
  314. if (File.Exists(torProcPath)) settingObject.TortoiseSVNProcPath = torProcPath;
  315. }
  316. }
  317. /// <summary>
  318. /// Saves the plugin settings
  319. /// </summary>
  320. public void SaveSettings()
  321. {
  322. ObjectSerializer.Serialize(this.settingFilename, settingObject);
  323. }
  324. #endregion
  325. }
  326. }