summaryrefslogtreecommitdiff
path: root/windowPatches.go
blob: bcb41bb253ad70b8493157e61de3581a67d80d59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main

import (
	"strings"
	"sync"

	"go.wit.com/lib/gadgets"
	"go.wit.com/lib/protobuf/forgepb"
	"go.wit.com/log"

	"go.wit.com/gui"
)

type patchesWindow struct {
	once  sync.Once            // only init() the window once
	win   *gadgets.BasicWindow // the patches window
	stack *gui.Node            // the top box set as vertical
	// shelf   *gui.Node                    // the first box in the stack, set as horizontal
	grid    *gui.Node                    // the list of available patches
	summary *patchSummary                // summary of current patches
	setgrid *gui.Node                    // the list of each patchset
	setlist map[string]*forgepb.Patchset // a map of the patch names to the protobuf
}

func (r *patchesWindow) Hidden() bool {
	return r.win.Hidden()
}

func (r *patchesWindow) Toggle() {
	if r.Hidden() {
		r.Show()
	} else {
		r.Hide()
	}
}

func (r *patchesWindow) Show() {
	r.win.Show()
}

func (r *patchesWindow) Hide() {
	r.win.Hide()
}

func (r *patchesWindow) Disable() {
	r.stack.Disable()
}

func (r *patchesWindow) Enable() {
	r.stack.Enable()
}

// you can only have one of these
func (r *patchesWindow) initWindow() {
	// sync.Once()
	r.win = gadgets.RawBasicWindow("Forge Patchesets")
	r.win.Make()

	r.stack = r.win.Box().NewBox("bw vbox", false)
	// me.reposwin.Draw()
	r.win.Custom = func() {
		log.Warn("Patchset Window close. setting hidden=true")
		// sets the hidden flag to false so Toggle() works
		r.win.Hide()
	}

	r.grid = r.stack.NewGrid("", 0, 0)

	/*
			r.shelf = r.initGroup()
			group1 := r.stack.NewGroup("stuff")
			vbox := group1.Box()
		      vbox.Vertical()
	*/

	r.summary = r.submitPatchesBox(r.stack)

	// update the stats about the repos and patches
	r.summary.Update()

	g := r.stack.NewGroup("PatchSet List")

	// add the grid
	r.setgrid = g.NewGrid("", 0, 0)
	r.setlist = make(map[string]*forgepb.Patchset)

	// query for current patchsets
	lines, err := listPatches()
	if err != nil {
		log.Info(err)
		return
	}
	for i, line := range lines {
		log.Info(i, line)
		r.addPatchset(line)
	}
}

func (r *patchesWindow) addPatchset(line string) {
	parts := strings.Split(line, "Author:")
	author := parts[1]
	parts = strings.Fields(parts[0])
	name := parts[0]
	subject := strings.Join(parts[1:], " ")
	r.setgrid.NewLabel(name)
	r.setgrid.NewLabel(subject)
	r.setgrid.NewLabel(author)
	r.setgrid.NewButton("Download", func() {
		pset, err := getPatch(name)
		if err != nil {
			log.Info(name, "failed to download", err)
			return
		}
		r.setlist[name] = pset
	})
	r.setgrid.NewButton("Dump", func() {
		pset := r.setlist[name]
		if pset == nil {
			log.Info(name, "was nil")
			return
		}
		if !dumpPatchset(pset) {
			log.Info("Dump: some patches are bad", name)
			return
		}
	})
	r.setgrid.NewButton("Save", func() {
		pset := r.setlist[name]
		if pset == nil {
			log.Info(name, "was nil")
			return
		}
		if !savePatchset(pset) {
			log.Info("Save: some patches are bad", name)
			return
		}
	})
	r.setgrid.NewButton("Apply", func() {
		pset := r.setlist[name]
		if pset == nil {
			log.Info(name, "was nil")
			return
		}
		if _, _, _, err := IsEverythingOnDevel(); err != nil {
			log.Info("You can only apply patches to the devel branch")
			return
		}
		if IsAnythingDirty() {
			log.Info("You can't apply patches when repos are dirty")
			me.forge.PrintHumanTable(me.found)
			return
		}
		applyPatchset(pset)
	})
	r.setgrid.NextRow()
}