TrackGit
SwitchBranchWindow.cpp
Go to the documentation of this file.
1 
8 #include "SwitchBranchWindow.h"
9 #include "../GitCommand/SwitchBranch.h"
10 
11 #include <stdio.h>
12 #include <git2.h>
13 
14 #include <LayoutBuilder.h>
15 
16 #include <vector>
17 
18 
24  :
25  TrackGitWindow(repo, BRect(0, 0, 300, 100), "TrackGit - Switch Branch",
26  B_DOCUMENT_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
27 {
28  vector<BString> branches;
29 
30  if (SwitchBranch::GetBranches(fRepo, branches) != 0) {
31  const git_error* err = giterr_last();
32  printf("Error %d : %s\n", err->klass, err->message);
33  BString buffer("Error : %s");
34  buffer.ReplaceFirst("%s", err->message);
35  BAlert* alert = new BAlert("", buffer.String(), "Cancel",
36  0, 0, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
37  alert->Go();
38  Quit();
39  }
40 
41  fSwitchMenu = new BPopUpMenu(branches[0]);
42  fSelectedBranch = branches[0];
43 
44  BButton* fSwitch = new BButton("switch", "Switch",
45  new BMessage(kDoSwitchBranch));
46  BButton* fCancel = new BButton("cancel", "Cancel",
47  new BMessage(kCancelSwitchBranch));
48 
49  for (int i=0; i<branches.size(); i++) {
50  BMessage *msg = new BMessage(kBranchName);
51  msg->AddString("branch", branches[i]);
52  fSwitchMenu->AddItem(new BMenuItem(branches[i].String(), msg));
53  }
54 
55  BMenuField* switchMenuField = new BMenuField("Branch: ", fSwitchMenu);
56 
57  BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
58  .SetInsets(10)
59  .Add(switchMenuField)
60  .AddGroup(B_HORIZONTAL, 0)
61  .AddGlue()
62  .Add(fCancel)
63  .Add(fSwitch)
64  .End();
65 
66  CenterOnScreen();
67  Show();
68 }
69 
70 
75 void
77 {
78  BAlert* alert = NULL;
79  BString branchName;
80  switch (msg->what) {
81  case kDoSwitchBranch:
83  const git_error* err = giterr_last();
84 
85  BString buffer("Error : %s");
86  buffer.ReplaceFirst("%s", err->message);
87  alert = new BAlert("", buffer.String(), "Cancel",
88  0, 0, B_WIDTH_AS_USUAL, B_WARNING_ALERT);
89  } else {
90  BString buffer("Switched to branch %b.");
91  buffer.ReplaceFirst("%b", fSelectedBranch.String());
92  alert = new BAlert("", buffer.String(), "OK",
93  0, 0, B_WIDTH_AS_USUAL);
94  }
95  alert->Go();
96  Quit();
97  break;
99  Quit();
100  break;
101  case kBranchName:
102  if (msg->FindString("branch", &branchName) == B_OK)
103  fSelectedBranch = branchName;
104  break;
105  default:
106  BWindow::MessageReceived(msg);
107  }
108 }
BString fSelectedBranch
The branch to be switched to.
static int GetBranches(BString, vector< BString > &)
This function gets all the branche names in given repository.
virtual void Quit()
The function to Quit the window.
SwitchBranchWindow(BString)
SwitchBranchWindow Constructor.
virtual void MessageReceived(BMessage *)
Handler to received messages.
static int DoSwitchBranch(BString, BString)
This function creates a new branch in repo with given name.
Header file of Switch Branch window.
BPopUpMenu * fSwitchMenu
The Switch Branch pop up menu.
The TrackGit Window class.
BString fRepo
The repo/directory where the command is called.