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