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