MergeBranch.bjs
[gitlive] / MergeBranch.vala
1 static MergeBranch  _MergeBranch;
2
3 public class MergeBranch : Object
4 {
5     public Gtk.Dialog el;
6     private MergeBranch  _this;
7
8     public static MergeBranch singleton()
9     {
10         if (_MergeBranch == null) {
11             _MergeBranch= new MergeBranch();
12         }
13         return _MergeBranch;
14     }
15     public Xcls_table table;
16     public Xcls_actionsel actionsel;
17     public Xcls_actioncellrenderer actioncellrenderer;
18     public Xcls_actionmodel actionmodel;
19     public Xcls_ticketsel ticketsel;
20     public Xcls_dbcellrenderer dbcellrenderer;
21     public Xcls_dbmodel dbmodel;
22     public Xcls_name name;
23     public Xcls_scrolled_window scrolled_window;
24     public Xcls_view view;
25     public Xcls_spinner spinner;
26
27         // my vars (def)
28     public GitRepo repo;
29     public RooTicket? ticket;
30     public bool running;
31
32     // ctor
33     public MergeBranch()
34     {
35         _this = this;
36         this.el = new Gtk.Dialog();
37
38         // my vars (dec)
39         this.ticket = null;
40         this.running = false;
41
42         // set gobject values
43         this.el.title = "Merge Branch";
44         this.el.default_height = 200;
45         this.el.default_width = 500;
46         this.el.deletable = true;
47         this.el.modal = true;
48         var child_0 = new Xcls_Box2( _this );
49         child_0.ref();
50         this.el.get_content_area().add (  child_0.el  );
51         var child_1 = new Xcls_Button17( _this );
52         child_1.ref();
53         this.el.add_action_widget (  child_1.el , 0 );
54         var child_2 = new Xcls_Button18( _this );
55         child_2.ref();
56         this.el.add_action_widget (  child_2.el , 1 );
57
58         //listeners
59         this.el.delete_event.connect( (self, event) => {
60             this.el.hide();
61             return true; 
62             //test  
63         });
64         this.el.response.connect( (self, response_id) =>  { 
65           
66                 GLib.debug("got %d", (int) response_id);
67                 if (response_id < 1) {
68                     _this.el.hide();    
69                     this.running = false; 
70                 GitMonitor.gitmonitor.start();
71                         return;
72                 }
73                  /*
74                 // have they selected a ticket..
75                 // make that the current active ticket?
76                 // we really need to store locally what ticket is being worked on..
77                 // in theory we could be working on multiple project and not merging..
78                 // -- each repo would have their active ticket (only one per repo)
79                 // -- so we could just store that in there
80                 // -- initial load can check the contents of the ticket files on first scan.
81                 var ticket_id = _this.ticketsel.selectedTicketId();
82                 
83             if (this.repo != null) {
84                 var bn = _this.name.el.get_text();
85                 if (ticket_id != "" ) {
86                                 this.repo.setActiveTicket( RooTicket.singleton().getById(ticket_id), bn);
87                         } else {
88                                 this.repo.createBranchNamed(bn);
89                         }
90             }
91                 */
92                 
93                 if (this.repo != null) {
94                         repo.doMerge(
95                                 _this.actionsel.selectedAction(), 
96                                 _this.ticketsel.selectedTicketId(),
97                                 _this.name.el.get_text()
98                         );
99                 
100                 } else {
101                         GitRepo.doMerges(
102                                 _this.actionsel.selectedAction(), 
103                                 _this.ticketsel.selectedTicketId(),
104                                 _this.name.el.get_text()
105                         );
106                 }
107                 this.running = false; 
108         
109                  
110                 _this.el.hide();        
111                 GitMonitor.gitmonitor.start();
112         
113                  
114         });
115         this.el.show.connect( (self)  => {
116          
117         
118           //test
119         });
120     }
121
122     // user defined functions
123     public   void show (  RooTicket ticket, GitRepo? repo ) 
124     {
125          // this.el.set_gravity(Gdk.Gravity.NORTH);
126         if (this.running) {
127                 return;
128         }
129         GitMonitor.gitmonitor.stop();
130         
131         this.ticket = ticket;
132         this.repo = repo;
133         
134         this.el.move((Gdk.Screen.width() / 2)- 250 ,0);
135                 GLib.debug("Loading tickets"); 
136     
137     
138         this.el.show_all();
139         _this.dbmodel.loadTickets();
140         _this.actionmodel.loadActions();
141         _this.view.loadTicket(ticket.id);
142     
143     }
144     public class Xcls_Box2 : Object
145     {
146         public Gtk.Box el;
147         private MergeBranch  _this;
148
149
150             // my vars (def)
151
152         // ctor
153         public Xcls_Box2(MergeBranch _owner )
154         {
155             _this = _owner;
156             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
157
158             // my vars (dec)
159
160             // set gobject values
161             var child_0 = new Xcls_table( _this );
162             child_0.ref();
163             this.el.pack_start (  child_0.el , false,false,0 );
164             var child_1 = new Xcls_scrolled_window( _this );
165             child_1.ref();
166             this.el.add (  child_1.el  );
167             var child_2 = new Xcls_spinner( _this );
168             child_2.ref();
169             this.el.add (  child_2.el  );
170         }
171
172         // user defined functions
173     }
174     public class Xcls_table : Object
175     {
176         public Gtk.Table el;
177         private MergeBranch  _this;
178
179
180             // my vars (def)
181
182         // ctor
183         public Xcls_table(MergeBranch _owner )
184         {
185             _this = _owner;
186             _this.table = this;
187             this.el = new Gtk.Table( 2, 2, false );
188
189             // my vars (dec)
190
191             // set gobject values
192             this.el.margin = 2;
193             this.el.column_spacing = 2;
194             this.el.vexpand = false;
195             var child_0 = new Xcls_Label4( _this );
196             child_0.ref();
197             this.el.attach_defaults (  child_0.el , 0,1,0,1 );
198             var child_1 = new Xcls_actionsel( _this );
199             child_1.ref();
200             this.el.attach_defaults (  child_1.el , 1,2,0,1 );
201             var child_2 = new Xcls_Label8( _this );
202             child_2.ref();
203             this.el.attach_defaults (  child_2.el , 0,1,1,2 );
204             var child_3 = new Xcls_ticketsel( _this );
205             child_3.ref();
206             this.el.attach_defaults (  child_3.el , 1,2,1,2 );
207             var child_4 = new Xcls_Label12( _this );
208             child_4.ref();
209             this.el.attach_defaults (  child_4.el , 0,1,2,3 );
210             var child_5 = new Xcls_name( _this );
211             child_5.ref();
212             this.el.attach_defaults (  child_5.el , 1,2,2,3 );
213         }
214
215         // user defined functions
216     }
217     public class Xcls_Label4 : Object
218     {
219         public Gtk.Label el;
220         private MergeBranch  _this;
221
222
223             // my vars (def)
224
225         // ctor
226         public Xcls_Label4(MergeBranch _owner )
227         {
228             _this = _owner;
229             this.el = new Gtk.Label( "Do what?" );
230
231             // my vars (dec)
232
233             // set gobject values
234             this.el.justify = Gtk.Justification.RIGHT;
235             this.el.xalign = 0.900000f;
236             this.el.visible = true;
237         }
238
239         // user defined functions
240     }
241
242     public class Xcls_actionsel : Object
243     {
244         public Gtk.ComboBox el;
245         private MergeBranch  _this;
246
247
248             // my vars (def)
249         public bool loading;
250
251         // ctor
252         public Xcls_actionsel(MergeBranch _owner )
253         {
254             _this = _owner;
255             _this.actionsel = this;
256             this.el = new Gtk.ComboBox();
257
258             // my vars (dec)
259             this.loading = false;
260
261             // set gobject values
262             var child_0 = new Xcls_actioncellrenderer( _this );
263             child_0.ref();
264             this.el.pack_start (  child_0.el , true );
265             var child_1 = new Xcls_actionmodel( _this );
266             child_1.ref();
267             this.el.set_model (  child_1.el  );
268
269             // init method
270
271             this.el.add_attribute(_this.actioncellrenderer.el , "markup", 1 );
272
273             //listeners
274             this.el.changed.connect( () => {
275                 if (this.loading) {
276                         return;
277                 }
278                 _this.name.updateText();
279                 //GLib.debug (//"Selection: %s, %s\n", (string) val1, (string) val2);
280             });
281         }
282
283         // user defined functions
284         public string selectedAction () {
285                 Gtk.TreeIter iter;
286                 Value val1;
287          
288                 this.el.get_active_iter (out iter);
289                 _this.actionmodel.el.get_value (iter, 0, out val1);
290         
291                 return  (string) val1;
292         }
293     }
294     public class Xcls_actioncellrenderer : Object
295     {
296         public Gtk.CellRendererText el;
297         private MergeBranch  _this;
298
299
300             // my vars (def)
301
302         // ctor
303         public Xcls_actioncellrenderer(MergeBranch _owner )
304         {
305             _this = _owner;
306             _this.actioncellrenderer = this;
307             this.el = new Gtk.CellRendererText();
308
309             // my vars (dec)
310
311             // set gobject values
312         }
313
314         // user defined functions
315     }
316
317     public class Xcls_actionmodel : Object
318     {
319         public Gtk.ListStore el;
320         private MergeBranch  _this;
321
322
323             // my vars (def)
324
325         // ctor
326         public Xcls_actionmodel(MergeBranch _owner )
327         {
328             _this = _owner;
329             _this.actionmodel = this;
330             this.el = new Gtk.ListStore( 2, typeof(string),typeof(string) );
331
332             // my vars (dec)
333
334             // set gobject values
335         }
336
337         // user defined functions
338         public void loadActions () {
339         
340          
341             
342             _this.actionsel.loading = true;
343         
344             this.el.clear();                                    
345             Gtk.TreeIter iter;
346             var el = this.el;
347             
348             el.append(out iter);
349         
350             
351             el.set_value(iter, 0, "");
352             el.set_value(iter, 1, "-- select action  --");
353             
354             _this.actionsel.el.set_active_iter(iter);
355         
356             el.append(out iter);    
357             el.set_value(iter, 0, "CLOSE");
358             el.set_value(iter, 1, "Merge changes to master - and CLOSE ticket");
359                 
360                 
361             el.append(out iter);    
362             el.set_value(iter, 0, "LEAVE");
363             el.set_value(iter, 1, "Merge changes to master - and LEAVE ticket open");
364         
365             el.append(out iter);    
366             el.set_value(iter, 0, "MASTER");
367             el.set_value(iter, 1, "Merge changes FROM master into this branch");    
368             
369             el.append(out iter);    
370             el.set_value(iter, 0, "EXIT");
371             el.set_value(iter, 1, "Switch back to MASTER branch - no merge");    
372             
373             
374             
375             _this.actionsel.loading = false;
376              //this.el.set_sort_column_id(0, Gtk.SortType.ASCENDING);          
377                                              
378         }
379     }
380
381
382     public class Xcls_Label8 : Object
383     {
384         public Gtk.Label el;
385         private MergeBranch  _this;
386
387
388             // my vars (def)
389
390         // ctor
391         public Xcls_Label8(MergeBranch _owner )
392         {
393             _this = _owner;
394             this.el = new Gtk.Label( "Select Ticket" );
395
396             // my vars (dec)
397
398             // set gobject values
399             this.el.justify = Gtk.Justification.RIGHT;
400             this.el.xalign = 0.900000f;
401             this.el.visible = true;
402         }
403
404         // user defined functions
405     }
406
407     public class Xcls_ticketsel : Object
408     {
409         public Gtk.ComboBox el;
410         private MergeBranch  _this;
411
412
413             // my vars (def)
414         public bool loading;
415
416         // ctor
417         public Xcls_ticketsel(MergeBranch _owner )
418         {
419             _this = _owner;
420             _this.ticketsel = this;
421             this.el = new Gtk.ComboBox();
422
423             // my vars (dec)
424             this.loading = true;
425
426             // set gobject values
427             var child_0 = new Xcls_dbcellrenderer( _this );
428             child_0.ref();
429             this.el.pack_start (  child_0.el , true );
430             var child_1 = new Xcls_dbmodel( _this );
431             child_1.ref();
432             this.el.set_model (  child_1.el  );
433
434             // init method
435
436             this.el.add_attribute(_this.dbcellrenderer.el , "markup", 1 );
437
438             //listeners
439             this.el.changed.connect( () => {
440                 if (this.loading) {
441                         return;
442                 }
443                 _this.view.loadTicket(this.selectedTicketId());
444                  
445                 _this.name.updateText();
446                 //_this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
447                 
448                 //GLib.debug (//"Selection: %s, %s\n", (string) val1, (string) val2);
449             });
450         }
451
452         // user defined functions
453         public string selectedTicketId () {
454         Gtk.TreeIter iter;
455                 Value val1;
456          
457          
458                 this.el.get_active_iter (out iter);
459                 _this.dbmodel.el.get_value (iter, 0, out val1);
460          
461         
462                 return  (string) val1;
463                 
464                 
465                 
466                 
467         }
468     }
469     public class Xcls_dbcellrenderer : Object
470     {
471         public Gtk.CellRendererText el;
472         private MergeBranch  _this;
473
474
475             // my vars (def)
476
477         // ctor
478         public Xcls_dbcellrenderer(MergeBranch _owner )
479         {
480             _this = _owner;
481             _this.dbcellrenderer = this;
482             this.el = new Gtk.CellRendererText();
483
484             // my vars (dec)
485
486             // set gobject values
487         }
488
489         // user defined functions
490     }
491
492     public class Xcls_dbmodel : Object
493     {
494         public Gtk.ListStore el;
495         private MergeBranch  _this;
496
497
498             // my vars (def)
499
500         // ctor
501         public Xcls_dbmodel(MergeBranch _owner )
502         {
503             _this = _owner;
504             _this.dbmodel = this;
505             this.el = new Gtk.ListStore( 2, typeof(string),typeof(string) );
506
507             // my vars (dec)
508
509             // set gobject values
510         }
511
512         // user defined functions
513         public void loadTickets () {
514         
515             RooTicket.singleton().loadTickets("",RooTicket.Who.ME, RooTicket.Status.ACTIVE);
516             
517             _this.ticketsel.loading = true;
518         
519             this.el.clear();                                    
520             Gtk.TreeIter iter;
521             var el = this.el;
522             
523             el.append(out iter);
524             el.set_value(iter, 0, "");
525             el.set_value(iter, 1, "-- select a ticket --");
526             
527             _this.ticketsel.el.set_active_iter(iter);
528             
529             if (_this.ticket != null &&  _this.ticket.id == "-1") {
530                         el.append(out iter);
531                         el.set_value(iter, 0, "-1");
532                         el.set_value(iter, 1, "Temporary Branch - No ticket specified/relivant");
533                 _this.ticketsel.el.set_active_iter(iter);       
534             }
535             
536             
537             
538             
539             var tickets = RooTicket.singleton().tickets;
540             foreach(var ticket in tickets) {
541             
542                 el.append(out iter);
543         
544                 el.set_value(iter, 0, ticket.id);
545                 el.set_value(iter, 1, "#%s [%s] %s".printf( ticket.id, ticket.project_id_name , ticket.summary));
546                         if (_this.ticket != null && _this.ticket.id == ticket.id) {
547                             _this.ticketsel.el.set_active_iter(iter);
548                     }
549                 
550             }
551             
552             _this.ticketsel.loading = false;
553              //this.el.set_sort_column_id(0, Gtk.SortType.ASCENDING);          
554                                              
555         }
556     }
557
558
559     public class Xcls_Label12 : Object
560     {
561         public Gtk.Label el;
562         private MergeBranch  _this;
563
564
565             // my vars (def)
566
567         // ctor
568         public Xcls_Label12(MergeBranch _owner )
569         {
570             _this = _owner;
571             this.el = new Gtk.Label( "Use this commit message" );
572
573             // my vars (dec)
574
575             // set gobject values
576             this.el.justify = Gtk.Justification.RIGHT;
577             this.el.xalign = 0.900000f;
578         }
579
580         // user defined functions
581     }
582
583     public class Xcls_name : Object
584     {
585         public Gtk.Entry el;
586         private MergeBranch  _this;
587
588
589             // my vars (def)
590
591         // ctor
592         public Xcls_name(MergeBranch _owner )
593         {
594             _this = _owner;
595             _this.name = this;
596             this.el = new Gtk.Entry();
597
598             // my vars (dec)
599
600             // set gobject values
601             this.el.visible = true;
602         }
603
604         // user defined functions
605         public void updateText () {
606                 var ticket_id = _this.ticketsel.selectedTicketId();
607                 
608                 RooTicket ticket = null;
609                 if (ticket_id.length > 0){
610                         ticket = RooTicket.singleton().getById(ticket_id);
611                 }
612                 
613                 var name = RooTicket.singleton().usernameLocal();
614                 var action = _this.actionsel.selectedAction();
615                 this.el.set_editable(true); 
616                 switch (action) {
617                         case "CLOSE": // merge changes and close..
618                                 this.el.set_text("Fix #%s - %s".printf(ticket_id, ticket != null ? ticket.summary : ""));
619                                 break;
620                         case "LEAVE": // partial fix    
621                                 this.el.set_text("Partial Fix #%s - %s".printf(ticket_id,   ticket != null ? ticket.summary : ""));
622                                 break;
623                         case "MASTER": // merge master
624                         case "EXIT": // just switch back...             
625                                 this.el.set_editable(false);
626                                 this.el.set_text(""); // not relivant..                         
627                                 break;
628                         default:
629                         
630                                 break; /// don't fix the text value.
631                 }
632            
633         }
634     }
635
636
637     public class Xcls_scrolled_window : Object
638     {
639         public Gtk.ScrolledWindow el;
640         private MergeBranch  _this;
641
642
643             // my vars (def)
644
645         // ctor
646         public Xcls_scrolled_window(MergeBranch _owner )
647         {
648             _this = _owner;
649             _this.scrolled_window = this;
650             this.el = new Gtk.ScrolledWindow( null, null );
651
652             // my vars (dec)
653
654             // set gobject values
655             this.el.height_request = 500;
656             this.el.vexpand = true;
657             var child_0 = new Xcls_view( _this );
658             child_0.ref();
659             this.el.add (  child_0.el  );
660
661             // init method
662
663             {
664                 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
665             }
666         }
667
668         // user defined functions
669     }
670     public class Xcls_view : Object
671     {
672         public WebKit.WebView el;
673         private MergeBranch  _this;
674
675
676             // my vars (def)
677
678         // ctor
679         public Xcls_view(MergeBranch _owner )
680         {
681             _this = _owner;
682             _this.view = this;
683             this.el = new WebKit.WebView();
684
685             // my vars (dec)
686
687             // set gobject values
688         }
689
690         // user defined functions
691         public void loadTicket (string tid) {
692                 int h,w;
693                 _this.el.get_size(out w, out h);
694                 
695                 if (tid == "") {
696                         _this.scrolled_window.el.hide();
697                         _this.el.resize(w, 150);
698                 } else {
699                         _this.scrolled_window.el.show();
700                         _this.el.resize(w, 800);
701                 }
702                 
703                 var rs =  RooTicket.singleton();                
704                 var uri = new WebKit.URIRequest("https://roojs.com/admin.php/MTrack/View/" + tid);
705                 var hd = uri.get_http_headers();
706                 var authCode = Base64.encode ("%s:%s".printf(rs.username, rs.password).data);
707                 hd.append("Authorization", "Basic %s".printf(authCode));
708                 
709                  this.el.load_request(uri);
710         }
711     }
712
713
714     public class Xcls_spinner : Object
715     {
716         public Gtk.Spinner el;
717         private MergeBranch  _this;
718
719
720             // my vars (def)
721
722         // ctor
723         public Xcls_spinner(MergeBranch _owner )
724         {
725             _this = _owner;
726             _this.spinner = this;
727             this.el = new Gtk.Spinner();
728
729             // my vars (dec)
730
731             // set gobject values
732             this.el.hexpand = true;
733             this.el.vexpand = true;
734         }
735
736         // user defined functions
737     }
738
739
740     public class Xcls_Button17 : Object
741     {
742         public Gtk.Button el;
743         private MergeBranch  _this;
744
745
746             // my vars (def)
747
748         // ctor
749         public Xcls_Button17(MergeBranch _owner )
750         {
751             _this = _owner;
752             this.el = new Gtk.Button();
753
754             // my vars (dec)
755
756             // set gobject values
757             this.el.relief = Gtk.ReliefStyle.NONE;
758             this.el.label = "Cancel";
759         }
760
761         // user defined functions
762     }
763
764     public class Xcls_Button18 : Object
765     {
766         public Gtk.Button el;
767         private MergeBranch  _this;
768
769
770             // my vars (def)
771
772         // ctor
773         public Xcls_Button18(MergeBranch _owner )
774         {
775             _this = _owner;
776             this.el = new Gtk.Button();
777
778             // my vars (dec)
779
780             // set gobject values
781             this.el.label = "Do Merge";
782         }
783
784         // user defined functions
785     }
786
787 }