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