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