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 = 200;
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                 // have they selected a ticket..
69                 // make that the current active ticket?
70                 // we really need to store locally what ticket is being worked on..
71                 // in theory we could be working on multiple project and not merging..
72                 // -- each repo would have their active ticket (only one per repo)
73                 // -- so we could just store that in there
74                 // -- initial load can check the contents of the ticket files on first scan.
75                 var ticket_id = _this.ticketsel.selectedTicketId();
76                 
77             if (this.repo != null) {
78                 var bn = _this.name.el.get_text();
79         
80                         var res = this.repo.setActiveTicket(                    
81                                  RooTicket.singleton().getById(ticket_id != "" ? ticket_id : "-1"), bn
82                          );
83                          if (res) {
84                                 // start the monitoring..
85                                  GitMonitor.gitmonitor.start();
86                                  
87                          }
88             }
89             
90         
91             this.running = false; 
92         
93                 this.el.hide();
94                  
95                  
96         
97                  
98         });
99     }
100
101     // user defined functions
102     public   void show ( GitRepo repo, Gee.ArrayList<GitMonitorQueue> queue, string tid = "" ) 
103     {
104          // this.el.set_gravity(Gdk.Gravity.NORTH);
105         if (this.running) { // should not happen!!
106                 GLib.error("new branch show called, when already being displayed?");
107         }
108         this.queue = queue;
109         
110         this.running  = true;
111         GitMonitor.gitmonitor.stop();
112         
113         this.repo = repo;
114         
115          
116         
117         this.el.move((Gdk.Screen.width() / 2)- 250 ,0);
118         this.el.set_default_size( 500,200); // not sure why it grows..
119         GLib.debug("Loading tickets"); 
120          
121     
122         this.el.show_all();
123         this.el.set_keep_above(true);
124        
125         
126         var curproj = RooTicket.singleton().getProjectByRepo(this.repo);
127          _this.prmodel.loadProjects(curproj == null ? "" : curproj.id);
128         
129         _this.dbmodel.loadTickets(curproj == null ? "": curproj.id, tid);
130         if (tid != "") {
131                 var name = RooTicket.singleton().usernameLocal();
132                 var ticket = RooTicket.singleton().getById(tid);
133                 _this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
134         }
135     
136         this.el.run();
137     
138     }
139     public class Xcls_Box2 : Object
140     {
141         public Gtk.Box el;
142         private NewBranch  _this;
143
144
145             // my vars (def)
146
147         // ctor
148         public Xcls_Box2(NewBranch _owner )
149         {
150             _this = _owner;
151             this.el = new Gtk.Box( Gtk.Orientation.VERTICAL, 0 );
152
153             // my vars (dec)
154
155             // set gobject values
156             var child_0 = new Xcls_Table3( _this );
157             child_0.ref();
158             this.el.pack_start (  child_0.el , false,false,0 );
159             var child_1 = new Xcls_scrolled_window( _this );
160             child_1.ref();
161             this.el.add (  child_1.el  );
162         }
163
164         // user defined functions
165     }
166     public class Xcls_Table3 : Object
167     {
168         public Gtk.Table el;
169         private NewBranch  _this;
170
171
172             // my vars (def)
173
174         // ctor
175         public Xcls_Table3(NewBranch _owner )
176         {
177             _this = _owner;
178             this.el = new Gtk.Table( 5, 5, true );
179
180             // my vars (dec)
181
182             // set gobject values
183             this.el.expand = false;
184             this.el.margin = 2;
185             this.el.column_spacing = 4;
186             this.el.vexpand = false;
187             var child_0 = new Xcls_Label4( _this );
188             child_0.ref();
189             this.el.attach_defaults (  child_0.el , 0,1,0,1 );
190             var child_1 = new Xcls_Label5( _this );
191             child_1.ref();
192             this.el.attach_defaults (  child_1.el , 1,2,0,1 );
193             var child_2 = new Xcls_projectsel( _this );
194             child_2.ref();
195             this.el.attach_defaults (  child_2.el , 0,1,1,2 );
196             var child_3 = new Xcls_ticketsel( _this );
197             child_3.ref();
198             this.el.attach_defaults (  child_3.el , 1,4,1,2 );
199             var child_4 = new Xcls_Button12( _this );
200             child_4.ref();
201             this.el.attach_defaults (  child_4.el , 4,5,1,2 );
202             var child_5 = new Xcls_Label13( _this );
203             child_5.ref();
204             this.el.attach_defaults (  child_5.el , 0,1,3,4 );
205             var child_6 = new Xcls_name( _this );
206             child_6.ref();
207             this.el.attach_defaults (  child_6.el , 0,4,4,5 );
208             var child_7 = new Xcls_Button15( _this );
209             child_7.ref();
210             this.el.attach_defaults (  child_7.el , 4,5,4,5 );
211             var child_8 = new Xcls_Box16( _this );
212             child_8.ref();
213             this.el.attach_defaults (  child_8.el , 0,5,2,3 );
214         }
215
216         // user defined functions
217     }
218     public class Xcls_Label4 : Object
219     {
220         public Gtk.Label el;
221         private NewBranch  _this;
222
223
224             // my vars (def)
225
226         // ctor
227         public Xcls_Label4(NewBranch _owner )
228         {
229             _this = _owner;
230             this.el = new Gtk.Label( "Project" );
231
232             // my vars (dec)
233
234             // set gobject values
235             this.el.halign = Gtk.Align.START;
236             this.el.justify = Gtk.Justification.RIGHT;
237             this.el.visible = true;
238         }
239
240         // user defined functions
241     }
242
243     public class Xcls_Label5 : Object
244     {
245         public Gtk.Label el;
246         private NewBranch  _this;
247
248
249             // my vars (def)
250
251         // ctor
252         public Xcls_Label5(NewBranch _owner )
253         {
254             _this = _owner;
255             this.el = new Gtk.Label( "Ticket" );
256
257             // my vars (dec)
258
259             // set gobject values
260             this.el.halign = Gtk.Align.START;
261             this.el.visible = true;
262         }
263
264         // user defined functions
265     }
266
267     public class Xcls_projectsel : Object
268     {
269         public Gtk.ComboBox el;
270         private NewBranch  _this;
271
272
273             // my vars (def)
274         public bool loading;
275
276         // ctor
277         public Xcls_projectsel(NewBranch _owner )
278         {
279             _this = _owner;
280             _this.projectsel = this;
281             this.el = new Gtk.ComboBox.with_entry();
282
283             // my vars (dec)
284             this.loading = false;
285
286             // set gobject values
287             var child_0 = new Xcls_prcellrenderer( _this );
288             child_0.ref();
289             this.el.pack_start (  child_0.el , true );
290             var child_1 = new Xcls_prmodel( _this );
291             child_1.ref();
292             this.el.set_model (  child_1.el  );
293
294             // init method
295
296             this.el.set_entry_text_column(1);
297
298             //listeners
299             this.el.changed.connect( () => {
300                 if (this.loading) {
301                         return;
302                 }
303                 var project_id = this.selectedProjectId();
304                 _this.dbmodel.loadTickets(project_id);
305                 
306                 
307                 /*if (this.loading) {
308                         return;
309                 }
310                 var ticket_id = this.selectedTicketId();
311                 
312                 var name = RooTicket.singleton().usernameLocal();
313                 
314                 if (ticket_id == "" || ticket_id == null) {
315                 
316                         var dt = new  DateTime.now_local();
317                         _this.name.el.set_text("wip_%s_%s".printf(name,dt.format("%Y_%b_%d")));
318                         return;
319                 }
320                 
321                 
322                 var ticket = RooTicket.singleton().getById(ticket_id);
323                
324                 _this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
325                 
326                 //GLib.debug (//"Selection: %s, %s\n", (string) val1, (string) val2);
327                 */
328             });
329         }
330
331         // user defined functions
332         public string selectedProjectId () {
333                 Gtk.TreeIter iter;
334                 Value val1;
335          
336          
337                 this.el.get_active_iter (out iter);
338                 _this.prmodel.el.get_value (iter, 0, out val1);
339          
340         
341                 return  (string) val1;
342                 
343                 
344                 
345                 
346         }
347     }
348     public class Xcls_prcellrenderer : Object
349     {
350         public Gtk.CellRendererText el;
351         private NewBranch  _this;
352
353
354             // my vars (def)
355
356         // ctor
357         public Xcls_prcellrenderer(NewBranch _owner )
358         {
359             _this = _owner;
360             _this.prcellrenderer = this;
361             this.el = new Gtk.CellRendererText();
362
363             // my vars (dec)
364
365             // set gobject values
366         }
367
368         // user defined functions
369     }
370
371     public class Xcls_prmodel : Object
372     {
373         public Gtk.ListStore el;
374         private NewBranch  _this;
375
376
377             // my vars (def)
378
379         // ctor
380         public Xcls_prmodel(NewBranch _owner )
381         {
382             _this = _owner;
383             _this.prmodel = this;
384             this.el = new Gtk.ListStore( 2, typeof(string),typeof(string) );
385
386             // my vars (dec)
387
388             // set gobject values
389         }
390
391         // user defined functions
392         public void loadProjects (string id) {
393         
394             var rt = RooTicket.singleton();
395             rt.loadProjects();
396             
397             _this.projectsel.loading = true;
398         
399             this.el.clear();                                    
400             Gtk.TreeIter iter;
401             var el = this.el;
402             
403             el.append(out iter);
404         
405             
406             el.set_value(iter, 0, "");
407             el.set_value(iter, 1, "-- select a project --");
408             if (id == "") {
409                     _this.projectsel.el.set_active_iter(iter);
410             }
411             var projects = rt.projects;
412             foreach(var project in projects) {
413             
414                 el.append(out iter);
415                 
416                 el.set_value(iter, 0, project.id);
417                 el.set_value(iter, 1,  project.name );
418                 if (id == project.id) {
419                            _this.projectsel.el.set_active_iter(iter);
420                     }   
421                 
422             }
423             
424             _this.projectsel.loading = false;
425              //this.el.set_sort_column_id(0, Gtk.SortType.ASCENDING);          
426                                              
427         }
428     }
429
430
431     public class Xcls_ticketsel : Object
432     {
433         public Gtk.ComboBox el;
434         private NewBranch  _this;
435
436
437             // my vars (def)
438         public bool loading;
439
440         // ctor
441         public Xcls_ticketsel(NewBranch _owner )
442         {
443             _this = _owner;
444             _this.ticketsel = this;
445             this.el = new Gtk.ComboBox.with_entry();
446
447             // my vars (dec)
448             this.loading = false;
449
450             // set gobject values
451             var child_0 = new Xcls_dbcellrenderer( _this );
452             child_0.ref();
453             this.el.pack_start (  child_0.el , true );
454             var child_1 = new Xcls_dbmodel( _this );
455             child_1.ref();
456             this.el.set_model (  child_1.el  );
457
458             // init method
459
460             //this.el.add_attribute(_this.dbcellrenderer.el , "markup", 1 );
461             this.el.set_entry_text_column(1);
462
463             //listeners
464             this.el.changed.connect( () => {
465                 if (this.loading) {
466                         return;
467                 }
468                 var ticket_id = this.selectedTicketId();
469                 
470                 var name = RooTicket.singleton().usernameLocal();
471                 
472                 if (ticket_id == "" || ticket_id == null) {
473                 
474                         var dt = new  DateTime.now_local();
475                         _this.name.el.set_text("wip_%s_%s".printf(name,dt.format("%Y_%b_%d")));
476                         return;
477                 }
478                 
479                 
480                 var ticket = RooTicket.singleton().getById(ticket_id);
481                
482                 _this.name.el.set_text("wip_%s_T%s_%s".printf(name,ticket.id, ticket.summaryToBranchName()));
483                 
484                 //GLib.debug (//"Selection: %s, %s\n", (string) val1, (string) val2);
485             });
486         }
487
488         // user defined functions
489         public string selectedTicketId () {
490         Gtk.TreeIter iter;
491                 Value val1;
492          
493          
494                 this.el.get_active_iter (out iter);
495                 _this.dbmodel.el.get_value (iter, 0, out val1);
496          
497         
498                 return  (string) val1;
499                 
500                 
501                 
502                 
503         }
504     }
505     public class Xcls_dbcellrenderer : Object
506     {
507         public Gtk.CellRendererText el;
508         private NewBranch  _this;
509
510
511             // my vars (def)
512
513         // ctor
514         public Xcls_dbcellrenderer(NewBranch _owner )
515         {
516             _this = _owner;
517             _this.dbcellrenderer = this;
518             this.el = new Gtk.CellRendererText();
519
520             // my vars (dec)
521
522             // set gobject values
523         }
524
525         // user defined functions
526     }
527
528     public class Xcls_dbmodel : Object
529     {
530         public Gtk.ListStore el;
531         private NewBranch  _this;
532
533
534             // my vars (def)
535
536         // ctor
537         public Xcls_dbmodel(NewBranch _owner )
538         {
539             _this = _owner;
540             _this.dbmodel = this;
541             this.el = new Gtk.ListStore( 2, typeof(string),typeof(string) );
542
543             // my vars (dec)
544
545             // set gobject values
546         }
547
548         // user defined functions
549         public void loadTickets (string project_id , string tid = "") {
550         
551         
552             
553             // fixme .. get project id from selection..
554             
555              RooTicket.singleton().loadTickets(
556                                 project_id,
557                                 _this.btn_not_me.el.active ? RooTicket.Who.ANYBODY :  RooTicket.Who.ME,
558                                  _this.btn_closed.el.active ? RooTicket.Status.ALL :  RooTicket.Status.ACTIVE
559                          );
560                 
561             _this.ticketsel.loading = true;
562         
563             this.el.clear();                                    
564             Gtk.TreeIter iter;
565             var el = this.el;
566             
567             el.append(out iter);
568         
569             
570             el.set_value(iter, 0, "");
571             el.set_value(iter, 1, "-- select a ticket --");
572             
573             _this.ticketsel.el.set_active_iter(iter);
574             var tickets = RooTicket.singleton().tickets;
575             foreach(var ticket in tickets) {
576             
577                 el.append(out iter);
578                 
579                 el.set_value(iter, 0, ticket.id);
580                 el.set_value(iter, 1, "#%s [%s] %s".printf( ticket.id, ticket.project_id_name , ticket.summary));
581                 
582                 if (ticket.id == tid) {
583                             _this.ticketsel.el.set_active_iter(iter);
584                 }
585                 
586             }
587             
588             _this.ticketsel.loading = false;
589              //this.el.set_sort_column_id(0, Gtk.SortType.ASCENDING);          
590                                              
591         }
592     }
593
594
595     public class Xcls_Button12 : Object
596     {
597         public Gtk.Button el;
598         private NewBranch  _this;
599
600
601             // my vars (def)
602
603         // ctor
604         public Xcls_Button12(NewBranch _owner )
605         {
606             _this = _owner;
607             this.el = new Gtk.Button();
608
609             // my vars (dec)
610
611             // set gobject values
612             this.el.expand = false;
613             this.el.label = "New Ticket";
614
615             //listeners
616             this.el.clicked.connect( () => {
617                 _this.el.response(-1);
618                 Ticket.singleton().show(_this.repo);
619             
620             });
621         }
622
623         // user defined functions
624     }
625
626     public class Xcls_Label13 : Object
627     {
628         public Gtk.Label el;
629         private NewBranch  _this;
630
631
632             // my vars (def)
633
634         // ctor
635         public Xcls_Label13(NewBranch _owner )
636         {
637             _this = _owner;
638             this.el = new Gtk.Label( "Use this as branch name" );
639
640             // my vars (dec)
641
642             // set gobject values
643             this.el.halign = Gtk.Align.START;
644             this.el.justify = Gtk.Justification.RIGHT;
645             this.el.xalign = 0.900000f;
646         }
647
648         // user defined functions
649     }
650
651     public class Xcls_name : Object
652     {
653         public Gtk.Entry el;
654         private NewBranch  _this;
655
656
657             // my vars (def)
658
659         // ctor
660         public Xcls_name(NewBranch _owner )
661         {
662             _this = _owner;
663             _this.name = this;
664             this.el = new Gtk.Entry();
665
666             // my vars (dec)
667
668             // set gobject values
669             this.el.visible = true;
670         }
671
672         // user defined functions
673     }
674
675     public class Xcls_Button15 : Object
676     {
677         public Gtk.Button el;
678         private NewBranch  _this;
679
680
681             // my vars (def)
682
683         // ctor
684         public Xcls_Button15(NewBranch _owner )
685         {
686             _this = _owner;
687             this.el = new Gtk.Button();
688
689             // my vars (dec)
690
691             // set gobject values
692             this.el.label = "Create Branch";
693
694             // init method
695
696             {
697                this.el.get_style_context().add_class("suggested-action");
698             }
699
700             //listeners
701             this.el.clicked.connect( () => {
702                 GLib.debug("fire response = 1");
703                 _this.el.response(1);
704             });
705         }
706
707         // user defined functions
708     }
709
710     public class Xcls_Box16 : Object
711     {
712         public Gtk.Box el;
713         private NewBranch  _this;
714
715
716             // my vars (def)
717
718         // ctor
719         public Xcls_Box16(NewBranch _owner )
720         {
721             _this = _owner;
722             this.el = new Gtk.Box( Gtk.Orientation.HORIZONTAL, 0 );
723
724             // my vars (dec)
725
726             // set gobject values
727             var child_0 = new Xcls_btn_not_me( _this );
728             child_0.ref();
729             this.el.add (  child_0.el  );
730             var child_1 = new Xcls_btn_closed( _this );
731             child_1.ref();
732             this.el.add (  child_1.el  );
733             var child_2 = new Xcls_Label19( _this );
734             child_2.ref();
735             this.el.add (  child_2.el  );
736         }
737
738         // user defined functions
739     }
740     public class Xcls_btn_not_me : Object
741     {
742         public Gtk.CheckButton el;
743         private NewBranch  _this;
744
745
746             // my vars (def)
747
748         // ctor
749         public Xcls_btn_not_me(NewBranch _owner )
750         {
751             _this = _owner;
752             _this.btn_not_me = this;
753             this.el = new Gtk.CheckButton();
754
755             // my vars (dec)
756
757             // set gobject values
758             this.el.label = "Show tickets not assigned to me";
759
760             //listeners
761             this.el.toggled.connect( () => {
762                 var project_id = _this.projectsel.selectedProjectId();
763                 _this.dbmodel.loadTickets(project_id);  
764             });
765         }
766
767         // user defined functions
768     }
769
770     public class Xcls_btn_closed : Object
771     {
772         public Gtk.CheckButton el;
773         private NewBranch  _this;
774
775
776             // my vars (def)
777
778         // ctor
779         public Xcls_btn_closed(NewBranch _owner )
780         {
781             _this = _owner;
782             _this.btn_closed = this;
783             this.el = new Gtk.CheckButton();
784
785             // my vars (dec)
786
787             // set gobject values
788             this.el.label = "Show closed Tickets";
789
790             //listeners
791             this.el.toggled.connect( () => {
792                 var project_id = _this.projectsel.selectedProjectId();
793                 _this.dbmodel.loadTickets(project_id);  
794                  
795             });
796         }
797
798         // user defined functions
799     }
800
801     public class Xcls_Label19 : Object
802     {
803         public Gtk.Label el;
804         private NewBranch  _this;
805
806
807             // my vars (def)
808
809         // ctor
810         public Xcls_Label19(NewBranch _owner )
811         {
812             _this = _owner;
813             this.el = new Gtk.Label( "<a href=\"refresh\">Refresh Ticket list</a>" );
814
815             // my vars (dec)
816
817             // set gobject values
818             this.el.halign = Gtk.Align.END;
819             this.el.hexpand = true;
820             this.el.use_markup = true;
821
822             //listeners
823             this.el.activate_link.connect( (uri) => {
824                 if (uri == "refresh") {
825                         var curproj = RooTicket.singleton().getProjectByRepo(_this.repo);
826                          _this.prmodel.loadProjects(curproj == null ? "": curproj.id);
827             
828                         _this.dbmodel.loadTickets(curproj == null ? "": curproj.id);            
829                 }
830                 return true;
831             });
832         }
833
834         // user defined functions
835     }
836
837
838
839     public class Xcls_scrolled_window : Object
840     {
841         public Gtk.ScrolledWindow el;
842         private NewBranch  _this;
843
844
845             // my vars (def)
846
847         // ctor
848         public Xcls_scrolled_window(NewBranch _owner )
849         {
850             _this = _owner;
851             _this.scrolled_window = this;
852             this.el = new Gtk.ScrolledWindow( null, null );
853
854             // my vars (dec)
855
856             // set gobject values
857             var child_0 = new Xcls_view( _this );
858             child_0.ref();
859             this.el.add (  child_0.el  );
860
861             // init method
862
863             {
864                 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
865             }
866         }
867
868         // user defined functions
869     }
870     public class Xcls_view : Object
871     {
872         public WebKit.WebView el;
873         private NewBranch  _this;
874
875
876             // my vars (def)
877
878         // ctor
879         public Xcls_view(NewBranch _owner )
880         {
881             _this = _owner;
882             _this.view = this;
883             this.el = new WebKit.WebView();
884
885             // my vars (dec)
886
887             // init method
888
889             {
890                         this.load_try = 0;
891             
892             #if GTK3
893                          this.el.load_changed.connect( (ev ) => {
894                                 if (ev != WebKit.LoadEvent.FINISHED) {
895                                         return;  
896                                   }
897                                   
898                            this.printit(); 
899                           
900                          
901                         });
902                         
903                                 this.el.resource_load_started.connect( (resource, request) => {
904                                         print("Adding referrer header %s\n" , request.get_uri());
905                                         
906                                         if (request.get_http_headers() != null) {
907                                                 request.get_http_headers().remove("Referer");           
908                                                 request.get_http_headers().append("Referer", request.get_uri());
909                                         }
910                         });
911                         var settings = this.el.get_settings();
912                                 settings.set_user_agent( "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36" );
913                                         
914                        
915             #else
916              //listeners
917                       //  this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
918                         //  print("resource req. started %s\n", netreq.uri);
919                              
920                       //  });
921                         
922                         this.el.load_finished.connect( ( ) => {
923                                 
924                                 
925                                this.printit(); 
926                          
927                         });
928                         WebKit.get_default_session().request_started.connect((message, socket) => {
929                             Soup.URI uri = message.get_uri();
930                                 
931                                         var host = uri.get_host ();
932             
933                                         print("Adding header http://%s/\n", host);
934                                         message.request_headers.remove("Referer");                              
935                                         message.request_headers.append("Referer", "http://"+host+"/");
936                                         
937                         
938                         });
939                         var sess = WebKit.get_default_session();
940                                 sess.user_agent= "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36";
941                                 
942                         
943             #endif
944             }
945                         this.cookie_file = "";
946                         if (webkitpdf.opt_cookies != null) {
947                                 print("setting cookie %s\n",webkitpdf.opt_cookies);
948             
949                                 var uri = new Soup.URI (webkitpdf.opt_url);
950                                 
951                                 string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
952             
953                                 if(cookies.length > 1 ) {
954                                                      
955                                 #if GTK3        
956                                         var context = WebKit.WebContext.get_default();           
957                                         var cookie_manager = context.get_cookie_manager();
958                                         cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);
959                                         string scheme = uri.get_scheme ();
960                                 
961                                         try {            
962                                             FileIOStream iostream;                            
963                                             File file = File.new_tmp ("cookie-XXXXXX.txt", out iostream);
964                                             this.cookie_file = file.get_path ();        
965                                             print("cookie tmp file name: %s\n", file.get_path ());
966                                  
967                                             OutputStream ostream = iostream.output_stream;
968                                             DataOutputStream dostream = new DataOutputStream (ostream);                                      
969                                             dostream.put_string ("%s\tTRUE\t/\t%s\t2147483647\t%s\t%s".printf(uri.get_host(),scheme == "http" ? "FALSE" : "TRUE",cookies[0],cookies[1]));                             
970                                             cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);                              
971                                         } catch (Error e) {
972                                             stdout.printf ("Error: %s\n", e.message);
973                                         }               
974                                 #else
975                                                 var cookie_jar = new Soup.CookieJar();          
976                                         var cookie = new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1);
977                                         if(uri.get_scheme() =="https") {
978                                             cookie.set_secure(true);                    
979                                         }           
980                                         cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);                          
981                                         cookie_jar.add_cookie(cookie);                      
982                                         WebKit.get_default_session().add_feature(cookie_jar);
983                                 #endif
984                                                                                                             
985                                 }
986             
987                         } 
988                         // init method
989         }
990
991         // user defined functions
992         public void printit () {
993            print("load_finsihed\n");
994            
995            if (this.print_started) {
996                print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
997            
998               
999               
1000               
1001               return;
1002           }
1003            
1004          
1005             this.print_started = true; // flag to stop double call.
1006             
1007             
1008                 // inject any javascript if needed..
1009                 if (webkitpdf.opt_inject_js != null) {
1010                 
1011                         print("injecting javascript files \n");
1012                         foreach(var inject_js in  webkitpdf.opt_inject_js) {
1013                         print("inject? %s\n", inject_js);
1014                                 string str;
1015                                 try {
1016                                         FileUtils.get_contents( inject_js, out str);
1017                                 } catch(FileError e) {
1018                                         print("Failed to inject %s\n",  inject_js);
1019                                         continue;
1020                                 }
1021         #if GTK3
1022                                 GLib.MainLoop mainloop = new GLib.MainLoop();
1023                                 this.el.run_javascript.begin(str, null, (obj,res) => {
1024                                         mainloop.quit();
1025                                 });
1026                                 mainloop.run();
1027                                 
1028         #else                   
1029                                 this.el.execute_script(str);
1030         #endif
1031                         }
1032                 }
1033                   
1034                 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
1035                 if (webkitpdf.opt_target_pdf != null) {
1036                         GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
1037                                 print("delay done URL: %s\n", this.el.uri);
1038                         
1039                                  
1040                                 this.printpdf();
1041                                  return false;
1042                         }, GLib.Priority.DEFAULT);
1043                         return;
1044                 }
1045                         
1046         
1047         #if GTK3
1048                 print("PNG not supported in webkit2\n");
1049                 return;
1050         #else
1051                         
1052             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
1053            
1054                         
1055                         // ----------------- JPEG 
1056                         // resize the window...
1057                         // window.document.documentElement.scrollHeight
1058                         var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
1059                         print("Scroll height %d\n", scroll_height);
1060                         if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
1061                                 var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
1062                                 var h = int.min(scroll_height, 6000);
1063                                 _this.scrolled_window.el.set_size_request( w, h); 
1064                                 _this.el.resize (w+50, h+50);
1065                                 print("Resize to %d, %d\n", w,h);
1066                             GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
1067                                         this.printpng();
1068                                         return false;
1069                                   }, GLib.Priority.DEFAULT);
1070                             return false;
1071                         }
1072                         this.printpng();                
1073                         return false;
1074                   }, GLib.Priority.DEFAULT);
1075         
1076         
1077         #endif    
1078             
1079         }
1080         public bool printpdf () {
1081         
1082         
1083         
1084                         var list = Gtk.PaperSize.get_paper_sizes(false);
1085                     var psetup = new Gtk.PageSetup();
1086                     for(var i = 0; i < list.length(); i++ ) {
1087                         var entry = list.nth_data(i).copy();
1088                         if (entry.get_name() == "iso_a2") {
1089                             psetup.set_paper_size(entry);
1090                         }
1091                     }
1092                     
1093         
1094         #if GTK3         
1095         
1096                         var  pe = new  WebKit.PrintOperation(this.el);
1097                         pe.set_page_setup(psetup);
1098                         pe.finished.connect( () => {
1099                                 print("print completed\n");
1100                     this.delete_cookiefile(); 
1101                                  Gtk.main_quit();
1102                         
1103                         });
1104                         
1105                         var ps = new Gtk.PrintSettings();
1106                     ps.set_printer("Print to File");
1107                     ps.set("output-file-format", "pdf");
1108                     ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
1109         
1110                         
1111                         pe.set_print_settings(ps);
1112                         
1113         
1114                         pe.print();
1115                                   
1116         
1117         #else       
1118                         var  pe = new  Gtk.PrintOperation();
1119                         pe.export_filename = webkitpdf.opt_target_pdf;
1120                         print("got callback for print\n");
1121                         pe.ref();
1122                         /*pe.failed.connect(() => {
1123                                 print("print failed\n");
1124                                  Gtk.main_quit();
1125                         
1126                         })
1127                         */;
1128                          pe.done.connect(() => {
1129                                 print("print completed\n");
1130                     this.delete_cookiefile(); 
1131                                  Gtk.main_quit();
1132                         
1133                         });
1134                         
1135                         //pe.run_dialog(_this.el);
1136                  
1137                         
1138                         
1139         
1140         
1141                     pe.set_default_page_setup(psetup);
1142                     print("Calling Print?\n");
1143                     //pe.run_dialog(_this.el);
1144                     
1145                     
1146         
1147         
1148         
1149                     try {
1150                             this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
1151                     } catch (Error e) {
1152                                 // print failed...
1153                         }
1154         #endif      
1155                     
1156                      
1157                         return false;
1158         }
1159         public void printpng () {
1160             //var filename = "/tmp/test.pdf";
1161             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
1162         
1163             //if (le != LoadEvent.FINISHED) {
1164             //    return;
1165            // }
1166            
1167         // what size is the documet.
1168         
1169              print("making screenshot\n");
1170         #if GTK3
1171         
1172         #else     
1173             // fix vapi - get_snapshot add '?' to all null.
1174             var pixmap = _this.view.el.get_snapshot( null );
1175         
1176             
1177             
1178             int w,h;
1179             
1180             // add out to gdkpixmap.get_size
1181                 pixmap.get_size( out  w , out   h);
1182                 var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
1183                 print("pixbuf size: %d x %d\n ", w,h);
1184                  
1185             
1186             
1187              pixbuf.save(webkitpdf.opt_target_png, "png");
1188              print("Saved to %s\n", webkitpdf.opt_target_png);
1189             //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
1190             //    var sf = view.get_snapshot.end(res);
1191         
1192             //    sf.write_to_png(Browser.opt_target_png);
1193             //});
1194            this.delete_cookiefile(); 
1195             Gtk.main_quit();
1196              
1197         #endif   
1198             
1199             
1200         
1201         }
1202         public void delete_cookiefile () {
1203                 if(this.cookie_file.length < 1 ){
1204                         return;
1205                 }
1206             print("deleting tmp file %s\n",this.cookie_file);
1207             File file = File.new_for_path (this.cookie_file);
1208             try {
1209                 if (file.query_exists () == true) {
1210                    file.delete ();                   
1211                 }
1212             } catch (Error e) {
1213                 stdout.printf ("Error: %s\n", e.message);
1214             }
1215         
1216         }
1217     }
1218
1219
1220
1221 }