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