PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Plugins/Gerrit/GerritPlugin.cs

https://github.com/eisnerd/gitextensions
C# | 217 lines | 156 code | 52 blank | 9 comment | 21 complexity | 64596e1049b35148bff66f4ce51012cb MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using GitUIPluginInterfaces;
  8. using System.Windows.Forms;
  9. using System.Drawing;
  10. using ResourceManager.Translation;
  11. namespace Gerrit
  12. {
  13. public class GerritPlugin : IGitPluginForRepository
  14. {
  15. private bool _initialized;
  16. private ToolStripItem[] _gerritMenuItems;
  17. private ToolStripMenuItem _gitReviewMenuItem;
  18. private Form _mainForm;
  19. private IGitUICommands _gitUiCommands;
  20. #region Translation
  21. private readonly TranslationString _pluginDescription = new TranslationString("Gerrit Code Review");
  22. private readonly TranslationString _editGitReview = new TranslationString("Edit .gitreview");
  23. private readonly TranslationString _downloadGerritChange = new TranslationString("Download Gerrit Change");
  24. private readonly TranslationString _publishGerritChange = new TranslationString("Publish Gerrit Change");
  25. #endregion
  26. public string Description
  27. {
  28. get { return _pluginDescription.Text; }
  29. }
  30. public IGitPluginSettingsContainer Settings { get; set; }
  31. public void Register(IGitUICommands gitUiCommands)
  32. {
  33. _gitUiCommands = gitUiCommands;
  34. gitUiCommands.PostBrowseInitialize += gitUiCommands_PostBrowseInitialize;
  35. }
  36. void gitUiCommands_PostBrowseInitialize(object sender, GitUIBaseEventArgs e)
  37. {
  38. if (!_initialized)
  39. Initialize((Form)e.OwnerForm);
  40. // Correct enabled/visibility of our menu/tool strip items.
  41. bool validWorkingDir = GitCommands.Settings.Module.ValidWorkingDir();
  42. _gitReviewMenuItem.Enabled = validWorkingDir;
  43. bool showGerritItems = validWorkingDir && File.Exists(GitCommands.Settings.WorkingDir + ".gitreview");
  44. foreach (var item in _gerritMenuItems)
  45. {
  46. item.Visible = showGerritItems;
  47. }
  48. }
  49. private void Initialize(Form form)
  50. {
  51. // Prevent initialize being called multiple times when we fail to
  52. // initialize.
  53. _initialized = true;
  54. // Take a reference to the main form. We use this for ownership.
  55. _mainForm = form;
  56. // Find the controls we're going to extend.
  57. var menuStrip = FindControl<MenuStrip>(form, p => true);
  58. var toolStrip = FindControl<ToolStrip>(form, p => p.Name == "ToolStrip");
  59. if (menuStrip == null)
  60. throw new Exception("Cannot find main menu");
  61. if (toolStrip == null)
  62. throw new Exception("Cannot find main tool strip");
  63. // Create the Edit .gitreview button.
  64. var settingsMenu = (ToolStripMenuItem)menuStrip.Items.Cast<ToolStripItem>().SingleOrDefault(p => p.Name == "settingsToolStripMenuItem1");
  65. if (settingsMenu == null)
  66. throw new Exception("Cannot find settings menu");
  67. var mailMapMenuItem = settingsMenu.DropDownItems.Cast<ToolStripItem>().SingleOrDefault(p => p.Name == "editmailmapToolStripMenuItem");
  68. if (mailMapMenuItem == null)
  69. throw new Exception("Cannot find mailmap menu item");
  70. _gitReviewMenuItem = new ToolStripMenuItem
  71. {
  72. Text = _editGitReview.Text
  73. };
  74. _gitReviewMenuItem.Click += gitReviewMenuItem_Click;
  75. settingsMenu.DropDownItems.Insert(
  76. settingsMenu.DropDownItems.IndexOf(mailMapMenuItem) + 1,
  77. _gitReviewMenuItem
  78. );
  79. // Create the toolstrip items.
  80. var pushMenuItem = toolStrip.Items.Cast<ToolStripItem>().SingleOrDefault(p => p.Name == "toolStripButtonPush");
  81. if (pushMenuItem == null)
  82. throw new Exception("Cannot find push menu item");
  83. int nextIndex = toolStrip.Items.IndexOf(pushMenuItem) + 1;
  84. var separator = new ToolStripSeparator();
  85. toolStrip.Items.Insert(nextIndex++, separator);
  86. var downloadMenuItem = new ToolStripButton
  87. {
  88. Text = _downloadGerritChange.Text,
  89. Image = Properties.Resources.GerritDownload,
  90. DisplayStyle = ToolStripItemDisplayStyle.Image,
  91. Visible = false
  92. };
  93. downloadMenuItem.Click += downloadMenuItem_Click;
  94. toolStrip.Items.Insert(nextIndex++, downloadMenuItem);
  95. var publishMenuItem = new ToolStripButton
  96. {
  97. Text = _publishGerritChange.Text,
  98. Image = Properties.Resources.GerritPublish,
  99. DisplayStyle = ToolStripItemDisplayStyle.Image,
  100. Visible = false
  101. };
  102. publishMenuItem.Click += publishMenuItem_Click;
  103. toolStrip.Items.Insert(nextIndex++, publishMenuItem);
  104. // Keep a list of all items so we can show/hide them based in the
  105. // presence of the .gitreview file.
  106. _gerritMenuItems = new ToolStripItem[]
  107. {
  108. separator,
  109. downloadMenuItem,
  110. publishMenuItem
  111. };
  112. }
  113. void publishMenuItem_Click(object sender, EventArgs e)
  114. {
  115. using (var form = new FormGerritPublish(_gitUiCommands))
  116. {
  117. form.ShowDialog(_mainForm);
  118. }
  119. _gitUiCommands.RaiseBrowseInitialize();
  120. }
  121. void downloadMenuItem_Click(object sender, EventArgs e)
  122. {
  123. using (var form = new FormGerritDownload(_gitUiCommands))
  124. {
  125. form.ShowDialog(_mainForm);
  126. }
  127. _gitUiCommands.RaiseBrowseInitialize();
  128. }
  129. void gitReviewMenuItem_Click(object sender, EventArgs e)
  130. {
  131. using (var form = new FormGitReview())
  132. {
  133. form.ShowDialog(_mainForm);
  134. }
  135. _gitUiCommands.RaiseBrowseInitialize();
  136. }
  137. private T FindControl<T>(Control form, Func<T, bool> predicate)
  138. where T : Control
  139. {
  140. return FindControl(form.Controls, predicate);
  141. }
  142. private T FindControl<T>(IEnumerable controls, Func<T, bool> predicate)
  143. where T : Control
  144. {
  145. foreach (Control control in controls)
  146. {
  147. var result = control as T;
  148. if (result != null && predicate(result))
  149. return result;
  150. result = FindControl(control.Controls, predicate);
  151. if (result != null)
  152. return result;
  153. }
  154. return null;
  155. }
  156. public bool Execute(GitUIBaseEventArgs gitUiCommands)
  157. {
  158. using (var form = new FormPluginInformation())
  159. {
  160. form.ShowDialog();
  161. }
  162. return false;
  163. }
  164. }
  165. }