Add Education button programmed
[resumebuilder.git] / resumebuilder.go
1 package main
2
3 import (
4 "fmt"
5 "os"
6 "strconv"
7
8 "github.com/gotk3/gotk3/gtk"
9 )
10
11 type Applicant struct {
12 FirstName string
13 LastName string
14 DateOfBirth Date
15 Sex Sex
16 Address string
17 ContactNo string
18 Email string
19 Education []Education
20 }
21
22 type Date struct {
23 Year uint
24 Month uint
25 Day uint
26 }
27
28 type Education struct {
29 Qualification string
30 Institution string
31 YearOfPassing uint
32 GradeOrPercentage string
33 }
34
35 type Sex int
36
37 var ui *gtk.Builder
38 var selectedsex Sex
39 var selecteddate Date
40
41 const (
42 Male Sex = 1
43 Female = 2
44 Other = 3
45 )
46
47 /* func SaveToJson(a *Applicant) {
48 b, err := json.Marshal(a)
49
50 if err != nil {
51 fmt.Println(err.Error())
52 os.Exit(2)
53 }
54 err = ioutil.WriteFile("test.json", b, 0644)
55 if err != nil {
56 fmt.Println(err.Error())
57 os.Exit(3)
58 }
59 } */
60
61 func getLineText(fieldname string) (string, error) {
62 obj, err := ui.GetObject(fieldname)
63 if err != nil {
64 return "", err
65 }
66 field, ok := obj.(*gtk.Entry)
67 if !ok {
68 return "", err
69 }
70 fieldstr, err := field.GetText()
71 if err != nil {
72 return "", err
73 }
74 return fieldstr, nil
75 }
76
77 func getMultilineText(fieldname string) (string, error) {
78 obj, err := ui.GetObject(fieldname)
79 if err != nil {
80 return "", err
81 }
82 fieldctl, ok := obj.(*gtk.TextView)
83 if !ok {
84 return "", err
85 }
86 textbuf, err := fieldctl.GetBuffer()
87 if err != nil {
88 return "", err
89 }
90 start, end := textbuf.GetBounds()
91 textstr, err := textbuf.GetText(start, end, true)
92 if err != nil {
93 return "", err
94 }
95 return textstr, nil
96 }
97
98 func getBasicDetails() (*Applicant, error) {
99 // Retrieve all the fields.
100
101 firstname, err := getLineText("FirstName")
102 if err != nil {
103 return nil, err
104 }
105 lastname, err := getLineText("LastName")
106 if err != nil {
107 return nil, err
108 }
109
110 address, err := getMultilineText("Address")
111 if err != nil {
112 return nil, err
113 }
114
115 contact, err := getLineText("ContactNumber")
116 if err != nil {
117 return nil, err
118 }
119 email, err := getLineText("Email")
120 if err != nil {
121 return nil, err
122 }
123
124 // Store the contents in Applicant object
125 var applicant = new(Applicant)
126
127 applicant.FirstName = firstname
128 applicant.LastName = lastname
129 applicant.DateOfBirth = selecteddate
130 applicant.Sex = selectedsex
131 applicant.Address = address
132 applicant.ContactNo = contact
133 applicant.Email = email
134
135 return applicant, nil
136 }
137
138 func getWindow() (*gtk.Window, error) {
139 obj, err := ui.GetObject("MainWindow")
140 if err != nil {
141 fmt.Println(err.Error())
142 return nil, err
143 }
144 wnd, ok := obj.(*gtk.Window)
145 if !ok {
146 return nil, err
147 }
148 return wnd, nil
149 }
150
151 func LoadMain() {
152 gtk.Init(nil)
153 var err error
154 ui, err = gtk.BuilderNew()
155 if err != nil {
156 fmt.Println(err.Error())
157 os.Exit(1)
158 }
159 asset, err := Asset("resources/resumebuilder.glade")
160 if err != nil {
161 fmt.Println(err.Error())
162 os.Exit(1)
163 }
164 ui.AddFromString(string(asset))
165 ui.ConnectSignals(map[string]interface{}{"SexMaleSelected": sexMaleSelected,
166 "SexFemaleSelected": sexFemaleSelected, "SexOtherSelected": sexOtherSelected,
167 "SaveButtonClicked": saveButtonClicked, "DoBSelected": doBSelected,
168 "AddEducationClicked": addEducationClicked})
169
170 wnd, err := getWindow()
171 if err != nil {
172 os.Exit(1)
173 }
174
175 wnd.SetTitle("Resume Builder")
176 wnd.ShowAll()
177 selectedsex = Male
178
179 wnd.Connect("destroy", func() {
180 gtk.MainQuit()
181 })
182 gtk.Main()
183 }
184
185 func sexMaleSelected(male *gtk.RadioButton) {
186 if male.GetActive() {
187 selectedsex = Male
188 }
189 }
190
191 func sexFemaleSelected(female *gtk.RadioButton) {
192 if female.GetActive() {
193 selectedsex = Female
194 }
195 }
196
197 func sexOtherSelected(other *gtk.RadioButton) {
198 if other.GetActive() {
199 selectedsex = Other
200 }
201 }
202
203 func doBSelected(dob *gtk.Calendar) {
204 selecteddate.Year, selecteddate.Month, selecteddate.Day = dob.GetDate()
205 }
206
207 func getEducationFields() (*Education, error) {
208 ed := new(Education)
209 qual, err := getLineText("QualificationTitle")
210 if err != nil {
211 return nil, err
212 }
213 inst, err := getLineText("InstitutionName")
214 if err != nil {
215 return nil, err
216 }
217 yearp, err := getLineText("YearPassing")
218 if err != nil {
219 return nil, err
220 }
221 year, err := strconv.Atoi(yearp)
222 if err != nil {
223 return nil, err
224 }
225 gradep, err := getLineText("GradeOrPercentag")
226 if err != nil {
227 return nil, err
228 }
229 ed.Qualification = qual
230 ed.Institution = inst
231 ed.YearOfPassing = uint(year)
232 ed.GradeOrPercentage = gradep
233
234 return ed, nil
235 }
236
237 func addEducationClicked() {
238 ed, err := getEducationFields()
239 if err != nil {
240 return
241 }
242 lstore, err := ui.GetObject("EducationStore")
243 if err != nil {
244 return
245 }
246 edustore, ok := lstore.(*gtk.ListStore)
247 if !ok {
248 return
249 }
250 iter := edustore.Append()
251 edustore.SetValue(iter, 0, ed.Qualification)
252 edustore.SetValue(iter, 1, ed.Institution)
253 edustore.SetValue(iter, 2, ed.YearOfPassing)
254 edustore.SetValue(iter, 3, ed.GradeOrPercentage)
255 }
256
257 func saveButtonClicked() {
258 a, err := getBasicDetails()
259 if err != nil {
260 fmt.Println(err.Error())
261 os.Exit(1)
262 }
263 fmt.Println(*a)
264 }
265
266 func main() {
267 LoadMain()
268 }