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