NewBranch.bjs
[gitlive] / NewBranch.vala
1 static NewBranch  _NewBranch;
2
3 public class NewBranch : Object
4 {
5     public Gtk.Dialog el;
6     private NewBranch  _this;
7
8     public static NewBranch singleton()
9     {
10         if (_NewBranch == null) {
11             _NewBranch= new NewBranch();
12         }
13         return _NewBranch;
14     }
15     public Xcls_projectsel projectsel;
16     public Xcls_prcellrenderer prcellrenderer;
17     public Xcls_prmodel prmodel;
18     public Xcls_ticketsel ticketsel;
19     public Xcls_dbcellrenderer dbcellrenderer;
20     public Xcls_dbmodel dbmodel;
21     public Xcls_name name;
22     public Xcls_btn_not_me btn_not_me;
23     public Xcls_btn_closed btn_closed;
24     public Xcls_scrolled_window scrolled_window;
25     public Xcls_view view;
26
27         // my vars (def)
28     public GitRepo? repo;
29     public bool running;
30     public Gee.ArrayList<GitMonitorQueue> queue;
31
32     // ctor
33     public NewBranch()
34     {
35         _this = this;
36         this.el = new Gtk.Dialog();
37
38         // my vars (dec)
39         this.repo = null;
40         this.running = false;
41         this.queue = null;
42
43         // set gobject values
44         this.el.title = "Create a working branch ";
45         this.el.default_height = 500;
46         this.el.default_width = 500;
47         this.el.deletable = true;
48         this.el.modal = true;
49         var child_0 = new Xcls_Box2( _this );
50         child_0.ref();
51         this.el.get_content_area().add (  child_0.el  );
52
53         //listeners
54         this.el.delete_event.connect( (self, event) => {
55             this.el.hide();
56             return true; 
57             //test  
58         });
59         this.el.response.connect( (self, response_id) =>  { 
60           
61                 GLib.debug("got %d", (int) response_id);
62                 if (response_id < 1) {
63                     _this.el.hide();    
64                     this.running = false; 
65                         return;
66                 }
67                 
68                 
69                 
70                 
71                  
72                 // have they selected a ticket..
73                 // make that the current active ticket?
74                 // we really need to store locally what ticket is being worked on..
75                 // in theory we could be working on multiple project and not merging..
76                 // -- each repo would have their active ticket (only one per repo)
77                 // -- so we could just store that in there
78                 // -- initial load can check the contents of the ticket files on first scan.
79                 var ticket_id = _this.ticketsel.selectedTicketId();
80                 
81         
82                 
83             if (this.repo != null) {
84                 var bn = _this.name.el.get_text();
85         
86                         var res = this.repo.setActiveTicket(                    
87                                  RooTicket.singleton().getById(ticket_id != "" ? ticket_id : "-1"), bn
88                          );
89                          if (res) {
90                                 // start the monitoring..
91                                  GitMonitor.gitmonitor.start();
92                                  
93                          }
94             }
95             
96         
97             this.running = false; 
98         
99                 this.el.hide();
100                  
101                  
102         
103                  
104         });
105     }
106
107     // user defined functions
108     public   void show ( GitRepo repo, Gee.ArrayList<GitMonitorQueue> queue, string tid = "" ) 
109     {
110          // this.el.set_gravity(Gdk.Gravity.NORTH);
111         if (this.running) { // should not happen!!
112                 GLib.error("new branch show called, when already being displayed?");
113         }
114         this.queue = queue;
115         
116         this.running  = true;
117         GitMonitor.gitmonitor.stop();
118         
119         this.repo = repo;
120         
121          
122         
123         this.el.move((Gdk.Screen.width() / 2)- 250 ,0);
124         this.el.set_default_size( 500,200); // not sure why it grows..
125         GLib.debug("Loading tickets"); 
126          
127     
128         this.el.show_all();
129         this.el.set_keep_above(true);
130        
131         
132         var curproj = RooProject.getProjectByRepo(this.repo);
133          _this.prmodel.loadProjects(curproj == null ? "" : curproj.id);
134         
135         _this.dbmodel.loadTickets(curproj == null ? "": curproj.id, tid);
136     
137         if (tid != "") {
138                 var name = RooTicket.singleton().usernameLocal();
139                 var ticket = RooTicket.singleton().getById(tid);
140                 _this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
141     
142     
143         }
144         _this.view.loadTicket(tid);
145         this.el.run();
146     
147     }
148     public class Xcls_Box2 : Object
149     {
150         public Gtk.Box el;
151         private NewBranch  _this;
152
153
154             // my vars (def)
155
156         // ctor
157         public Xcls_Box2(NewBranch _owner )
158         {
159             _this = _owner;
160             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
161
162             // my vars (dec)
163
164             // set gobject values
165             var child_0 = new Xcls_Table3( _this );
166             child_0.ref();
167             this.el.pack_start (  child_0.el , false,false,0 );
168             var child_1 = new Xcls_scrolled_window( _this );
169             child_1.ref();
170             this.el.add (  child_1.el  );
171         }
172
173         // user defined functions
174     }
175     public class Xcls_Table3 : Object
176     {
177         public Gtk.Table el;
178         private NewBranch  _this;
179
180
181             // my vars (def)
182
183         // ctor
184         public Xcls_Table3(NewBranch _owner )
185         {
186             _this = _owner;
187             this.el = new Gtk.Table( 5, 5, true );
188
189             // my vars (dec)
190
191             // set gobject values
192             this.el.expand = false;
193             this.el.margin = 2;
194             this.el.column_spacing = 4;
195             this.el.vexpand = false;
196             var child_0 = new Xcls_Label4( _this );
197             child_0.ref();
198             this.el.attach_defaults (  child_0.el , 0,1,0,1 );
199             var child_1 = new Xcls_Label5( _this );
200             child_1.ref();
201             this.el.attach_defaults (  child_1.el , 1,2,0,1 );
202             var child_2 = new Xcls_projectsel( _this );
203             child_2.ref();
204             this.el.attach_defaults (  child_2.el , 0,1,1,2 );
205             var child_3 = new Xcls_ticketsel( _this );
206             child_3.ref();
207             this.el.attach_defaults (  child_3.el , 1,4,1,2 );
208             var child_4 = new Xcls_Button12( _this );
209             child_4.ref();
210             this.el.attach_defaults (  child_4.el , 4,5,1,2 );
211             var child_5 = new Xcls_Label13( _this );
212             child_5.ref();
213             this.el.attach_defaults (  child_5.el , 0,1,3,4 );
214             var child_6 = new Xcls_name( _this );
215             child_6.ref();
216             this.el.attach_defaults (  child_6.el , 0,4,4,5 );
217             var child_7 = new Xcls_Button15( _this );
218             child_7.ref();
219             this.el.attach_defaults (  child_7.el , 4,5,4,5 );
220             var child_8 = new Xcls_Box16( _this );
221             child_8.ref();
222             this.el.attach_defaults (  child_8.el , 0,5,2,3 );
223         }
224
225         // user defined functions
226     }
227     public class Xcls_Label4 : Object
228     {
229         public Gtk.Label el;
230         private NewBranch  _this;
231
232
233             // my vars (def)
234
235         // ctor
236         public Xcls_Label4(NewBranch _owner )
237         {
238             _this = _owner;
239             this.el = new Gtk.Label( "Project" );
240
241             // my vars (dec)
242
243             // set gobject values
244             this.el.halign = Gtk.Align.START;
245             this.el.justify = Gtk.Justification.RIGHT;
246             this.el.visible = true;
247         }
248
249         // user defined functions
250     }
251
252     public class Xcls_Label5 : Object
253     {
254         public Gtk.Label el;
255         private NewBranch  _this;
256
257
258             // my vars (def)
259
260         // ctor
261         public Xcls_Label5(NewBranch _owner )
262         {
263             _this = _owner;
264             this.el = new Gtk.Label( "Ticket" );
265
266             // my vars (dec)
267
268             // set gobject values
269             this.el.halign = Gtk.Align.START;
270             this.el.visible = true;
271         }
272
273         // user defined functions
274     }
275
276     public class Xcls_projectsel : Object
277     {
278         public Gtk.ComboBox el;
279         private NewBranch  _this;
280
281
282             // my vars (def)
283         public bool loading;
284
285         // ctor
286         public Xcls_projectsel(NewBranch _owner )
287         {
288             _this = _owner;
289             _this.projectsel = this;
290             this.el = new Gtk.ComboBox.with_entry();
291
292             // my vars (dec)
293             this.loading = false;
294
295             // set gobject values
296             var child_0 = new Xcls_prcellrenderer( _this );
297             child_0.ref();
298             this.el.pack_start (  child_0.el , true );
299             var child_1 = new Xcls_prmodel( _this );
300             child_1.ref();
301             this.el.set_model (  child_1.el  );
302
303             // init method
304
305             this.el.set_entry_text_column(1);
306
307             //listeners
308             this.el.changed.connect( () => {
309                 if (this.loading) {
310                         return;
311                 }
312                 var project_id = this.selectedProjectId();
313                 _this.dbmodel.loadTickets(project_id);
314                 
315                 
316                 /*if (this.loading) {
317                         return;
318                 }
319                 var ticket_id = this.selectedTicketId();
320                 
321                 var name = RooTicket.singleton().usernameLocal();
322                 
323                 if (ticket_id == "" || ticket_id == null) {
324                 
325                         var dt = new  DateTime.now_local();
326                         _this.name.el.set_text("wip_%s_%s".printf(name,dt.format("%Y_%b_%d")));
327                         return;
328                 }
329                 
330                 
331                 var ticket = RooTicket.singleton().getById(ticket_id);
332                
333                 _this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
334                 
335                 //GLib.debug (//"Selection: %s, %s\n", (string) val1, (string) val2);
336                 */
337             });
338         }
339
340         // user defined functions
341         public string selectedProjectId () {
342                 Gtk.TreeIter iter;
343                 Value val1;
344          
345          
346                 this.el.get_active_iter (out iter);
347                 _this.prmodel.el.get_value (iter, 0, out val1);
348          
349         
350                 return  (string) val1;
351                 
352                 
353                 
354                 
355         }
356     }
357     public class Xcls_prcellrenderer : Object
358     {
359         public Gtk.CellRendererText el;
360         private NewBranch  _this;
361
362
363             // my vars (def)
364
365         // ctor
366         public Xcls_prcellrenderer(NewBranch _owner )
367         {
368             _this = _owner;
369             _this.prcellrenderer = this;
370             this.el = new Gtk.CellRendererText();
371
372             // my vars (dec)
373
374             // set gobject values
375         }
376
377         // user defined functions
378     }
379
380     public class Xcls_prmodel : Object
381     {
382         public Gtk.ListStore el;
383         private NewBranch  _this;
384
385
386             // my vars (def)
387
388         // ctor
389         public Xcls_prmodel(NewBranch _owner )
390         {
391             _this = _owner;
392             _this.prmodel = this;
393             this.el = new Gtk.ListStore( 2, typeof(string),typeof(string) );
394
395             // my vars (dec)
396
397             // set gobject values
398         }
399
400         // user defined functions
401         public void loadProjects (string id) {
402         
403         
404             
405             _this.projectsel.loading = true;
406         
407             this.el.clear();                                    
408             Gtk.TreeIter iter;
409             var el = this.el;
410             
411             el.append(out iter);
412         
413             
414             el.set_value(iter, 0, "");
415             el.set_value(iter, 1, "-- select a project --");
416             if (id == "") {
417                     _this.projectsel.el.set_active_iter(iter);
418             }
419           
420             foreach(var project in RooProject.projects()) {
421             
422                 el.append(out iter);
423                 
424                 el.set_value(iter, 0, project.id);
425                 el.set_value(iter, 1,  project.name );
426                 if (id == project.id) {
427                            _this.projectsel.el.set_active_iter(iter);
428                     }   
429                 
430             }
431             
432             _this.projectsel.loading = false;
433              //this.el.set_sort_column_id(0, Gtk.SortType.ASCENDING);          
434                                              
435         }
436     }
437
438
439     public class Xcls_ticketsel : Object
440     {
441         public Gtk.ComboBox el;
442         private NewBranch  _this;
443
444
445             // my vars (def)
446         public bool loading;
447
448         // ctor
449         public Xcls_ticketsel(NewBranch _owner )
450         {
451             _this = _owner;
452             _this.ticketsel = this;
453             this.el = new Gtk.ComboBox.with_entry();
454
455             // my vars (dec)
456             this.loading = false;
457
458             // set gobject values
459             var child_0 = new Xcls_dbcellrenderer( _this );
460             child_0.ref();
461             this.el.pack_start (  child_0.el , true );
462             var child_1 = new Xcls_dbmodel( _this );
463             child_1.ref();
464             this.el.set_model (  child_1.el  );
465
466             // init method
467
468             //this.el.add_attribute(_this.dbcellrenderer.el , "markup", 1 );
469             this.el.set_entry_text_column(1);
470             this.el.get_child().set_sensitive(false);
471
472             //listeners
473             this.el.changed.connect( () => {
474                 if (this.loading) {
475                         return;
476                 }
477                 var ticket_id = this.selectedTicketId();
478                 
479                 var name = RooTicket.singleton().usernameLocal();
480                 
481                 if (ticket_id == "" || ticket_id == null) {
482                 
483                         var dt = new  DateTime.now_local();
484                         _this.name.el.set_text("wip_%s_%s".printf(name,dt.format("%Y_%b_%d")));
485                         _this.view.loadTicket("");              
486                         return;
487                 }
488                 
489                 
490                 var ticket = RooTicket.singleton().getById(ticket_id);
491                
492                 _this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
493                 _this.scrolled_window.el.show();
494                  
495                 _this.view.loadTicket(ticket.id);
496                 //GLib.debug (//"Selection: %s, %s\n", (string) val1, (string) val2);
497             });
498         }
499
500         // user defined functions
501         public string selectedTicketId () {
502         Gtk.TreeIter iter;
503                 Value val1;
504          
505          
506                 this.el.get_active_iter (out iter);
507                 _this.dbmodel.el.get_value (iter, 0, out val1);
508          
509         
510                 return  (string) val1;
511                 
512                 
513                 
514                 
515         }
516     }
517     public class Xcls_dbcellrenderer : Object
518     {
519         public Gtk.CellRendererText el;
520         private NewBranch  _this;
521
522
523             // my vars (def)
524
525         // ctor
526         public Xcls_dbcellrenderer(NewBranch _owner )
527         {
528             _this = _owner;
529             _this.dbcellrenderer = this;
530             this.el = new Gtk.CellRendererText();
531
532             // my vars (dec)
533
534             // set gobject values
535         }
536
537         // user defined functions
538     }
539
540     public class Xcls_dbmodel : Object
541     {
542         public Gtk.ListStore el;
543         private NewBranch  _this;
544
545
546             // my vars (def)
547
548         // ctor
549         public Xcls_dbmodel(NewBranch _owner )
550         {
551             _this = _owner;
552             _this.dbmodel = this;
553             this.el = new Gtk.ListStore( 2, typeof(string),typeof(string) );
554
555             // my vars (dec)
556
557             // set gobject values
558         }
559
560         // user defined functions
561         public void loadTickets (string project_id , string tid = "") {
562         
563         
564             
565             // fixme .. get project id from selection..
566             
567              RooTicket.singleton().loadTickets(
568                                 project_id,
569                                 _this.btn_not_me.el.active ? RooTicket.Who.ANYBODY :  RooTicket.Who.ME,
570                                  _this.btn_closed.el.active ? RooTicket.Status.ALL :  RooTicket.Status.ACTIVE
571                          );
572                 
573             _this.ticketsel.loading = true;
574         
575             this.el.clear();                                    
576             Gtk.TreeIter iter;
577             var el = this.el;
578             
579             el.append(out iter);
580         
581             
582             el.set_value(iter, 0, "");
583             el.set_value(iter, 1, "-- select a ticket --");
584             
585             _this.ticketsel.el.set_active_iter(iter);
586             var tickets = RooTicket.singleton().tickets;
587             foreach(var ticket in tickets) {
588             
589                 el.append(out iter);
590                 
591                 el.set_value(iter, 0, ticket.id);
592                 el.set_value(iter, 1, "#%s %s".printf( ticket.id, ticket.summary));
593                 
594                 if (ticket.id == tid) {
595                             _this.ticketsel.el.set_active_iter(iter);
596                 }
597                 
598             }
599             
600             _this.ticketsel.loading = false;
601              //this.el.set_sort_column_id(0, Gtk.SortType.ASCENDING);          
602                                              
603         }
604     }
605
606
607     public class Xcls_Button12 : Object
608     {
609         public Gtk.Button el;
610         private NewBranch  _this;
611
612
613             // my vars (def)
614
615         // ctor
616         public Xcls_Button12(NewBranch _owner )
617         {
618             _this = _owner;
619             this.el = new Gtk.Button();
620
621             // my vars (dec)
622
623             // set gobject values
624             this.el.expand = false;
625             this.el.label = "New Ticket";
626
627             //listeners
628             this.el.clicked.connect( () => {
629                 _this.el.response(-1);
630                 Ticket.singleton().show(_this.repo);
631             
632             });
633         }
634
635         // user defined functions
636     }
637
638     public class Xcls_Label13 : Object
639     {
640         public Gtk.Label el;
641         private NewBranch  _this;
642
643
644             // my vars (def)
645
646         // ctor
647         public Xcls_Label13(NewBranch _owner )
648         {
649             _this = _owner;
650             this.el = new Gtk.Label( "Use this as branch name" );
651
652             // my vars (dec)
653
654             // set gobject values
655             this.el.halign = Gtk.Align.START;
656             this.el.justify = Gtk.Justification.RIGHT;
657             this.el.xalign = 0.900000f;
658         }
659
660         // user defined functions
661     }
662
663     public class Xcls_name : Object
664     {
665         public Gtk.Entry el;
666         private NewBranch  _this;
667
668
669             // my vars (def)
670
671         // ctor
672         public Xcls_name(NewBranch _owner )
673         {
674             _this = _owner;
675             _this.name = this;
676             this.el = new Gtk.Entry();
677
678             // my vars (dec)
679
680             // set gobject values
681             this.el.visible = true;
682         }
683
684         // user defined functions
685     }
686
687     public class Xcls_Button15 : Object
688     {
689         public Gtk.Button el;
690         private NewBranch  _this;
691
692
693             // my vars (def)
694
695         // ctor
696         public Xcls_Button15(NewBranch _owner )
697         {
698             _this = _owner;
699             this.el = new Gtk.Button();
700
701             // my vars (dec)
702
703             // set gobject values
704             this.el.label = "Create Branch";
705
706             // init method
707
708             {
709                this.el.get_style_context().add_class("suggested-action");
710             }
711
712             //listeners
713             this.el.clicked.connect( () => {
714                 GLib.debug("fire response = 1");
715                 _this.el.response(1);
716             });
717         }
718
719         // user defined functions
720     }
721
722     public class Xcls_Box16 : Object
723     {
724         public Gtk.Box el;
725         private NewBranch  _this;
726
727
728             // my vars (def)
729
730         // ctor
731         public Xcls_Box16(NewBranch _owner )
732         {
733             _this = _owner;
734             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
735
736             // my vars (dec)
737
738             // set gobject values
739             var child_0 = new Xcls_btn_not_me( _this );
740             child_0.ref();
741             this.el.add (  child_0.el  );
742             var child_1 = new Xcls_btn_closed( _this );
743             child_1.ref();
744             this.el.add (  child_1.el  );
745             var child_2 = new Xcls_Label19( _this );
746             child_2.ref();
747             this.el.add (  child_2.el  );
748         }
749
750         // user defined functions
751     }
752     public class Xcls_btn_not_me : Object
753     {
754         public Gtk.CheckButton el;
755         private NewBranch  _this;
756
757
758             // my vars (def)
759
760         // ctor
761         public Xcls_btn_not_me(NewBranch _owner )
762         {
763             _this = _owner;
764             _this.btn_not_me = this;
765             this.el = new Gtk.CheckButton();
766
767             // my vars (dec)
768
769             // set gobject values
770             this.el.label = "Show tickets not assigned to me";
771
772             //listeners
773             this.el.toggled.connect( () => {
774                 var project_id = _this.projectsel.selectedProjectId();
775                 _this.dbmodel.loadTickets(project_id);  
776             });
777         }
778
779         // user defined functions
780     }
781
782     public class Xcls_btn_closed : Object
783     {
784         public Gtk.CheckButton el;
785         private NewBranch  _this;
786
787
788             // my vars (def)
789
790         // ctor
791         public Xcls_btn_closed(NewBranch _owner )
792         {
793             _this = _owner;
794             _this.btn_closed = this;
795             this.el = new Gtk.CheckButton();
796
797             // my vars (dec)
798
799             // set gobject values
800             this.el.label = "Show closed Tickets";
801
802             //listeners
803             this.el.toggled.connect( () => {
804                 var project_id = _this.projectsel.selectedProjectId();
805                 _this.dbmodel.loadTickets(project_id);  
806                  
807             });
808         }
809
810         // user defined functions
811     }
812
813     public class Xcls_Label19 : Object
814     {
815         public Gtk.Label el;
816         private NewBranch  _this;
817
818
819             // my vars (def)
820
821         // ctor
822         public Xcls_Label19(NewBranch _owner )
823         {
824             _this = _owner;
825             this.el = new Gtk.Label( "<a href=\"refresh\">Refresh Ticket list</a>" );
826
827             // my vars (dec)
828
829             // set gobject values
830             this.el.halign = Gtk.Align.END;
831             this.el.hexpand = true;
832             this.el.use_markup = true;
833
834             //listeners
835             this.el.activate_link.connect( (uri) => {
836                 if (uri == "refresh") {
837                         RooProject.reload();
838                         var curproj = RooProject.getProjectByRepo(_this.repo);
839                          _this.prmodel.loadProjects(curproj == null ? "": curproj.id);
840             
841                         _this.dbmodel.loadTickets(curproj == null ? "": curproj.id);            
842                 }
843                 return true;
844             });
845         }
846
847         // user defined functions
848     }
849
850
851
852     public class Xcls_scrolled_window : Object
853     {
854         public Gtk.ScrolledWindow el;
855         private NewBranch  _this;
856
857
858             // my vars (def)
859
860         // ctor
861         public Xcls_scrolled_window(NewBranch _owner )
862         {
863             _this = _owner;
864             _this.scrolled_window = this;
865             this.el = new Gtk.ScrolledWindow( null, null );
866
867             // my vars (dec)
868
869             // set gobject values
870             this.el.height_request = 500;
871             this.el.vexpand = true;
872             var child_0 = new Xcls_view( _this );
873             child_0.ref();
874             this.el.add (  child_0.el  );
875
876             // init method
877
878             {
879                 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
880             }
881         }
882
883         // user defined functions
884     }
885     public class Xcls_view : Object
886     {
887         public WebKit.WebView el;
888         private NewBranch  _this;
889
890
891             // my vars (def)
892
893         // ctor
894         public Xcls_view(NewBranch _owner )
895         {
896             _this = _owner;
897             _this.view = this;
898             this.el = new WebKit.WebView();
899
900             // my vars (dec)
901
902             // set gobject values
903         }
904
905         // user defined functions
906         public void loadTicket (string tid) {
907                 int h,w;
908                 _this.el.get_size(out w, out h);
909                 
910                 if (tid == "") {
911                         _this.scrolled_window.el.hide();
912                         _this.el.resize(w, 150);
913                 } else {
914                         _this.scrolled_window.el.show();
915                         _this.el.resize(w, 800);
916                 }
917                 
918                 var rs =  RooTicket.singleton();                
919                 var uri = new WebKit.URIRequest("https://roojs.com/admin.php/MTrack/View/" + tid);
920                 var hd = uri.get_http_headers();
921                 var authCode = Base64.encode ("%s:%s".printf(rs.username, rs.password).data);
922                 hd.append("Authorization", "Basic %s".printf(authCode));
923                 
924                  this.el.load_request(uri);
925         }
926     }
927
928
929
930 }