working on it ...

Filters

Explore Public Snippets

Sort by

Found 57 snippets

    public by marceloviana modified Nov 14, 2016  1367  14  4  0

    Csharp consumindo Rest + lendo JSON

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.IO;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    namespace ExemploChatBot
    {
        public partial class Chat : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void enviar_Click(object sender, EventArgs e)
            {
                string txt_campo_texto = campo_texto.Text;
    
                var url = "http://128.0.0.1:5000/site?id=2542&texto_digitado="+ txt_campo_texto + "";
    
                var syncClient = new WebClient();
                var content = syncClient.DownloadString(url);            
    
                dynamic registro = JObject.Parse(content);
                campo_texto.Text = registro.pergunta;
    
            }
        }
    }

    external by BZHDeveloper modified Saturday at 1:19:07 PM  3  0  1  0

    drag & drop Gtk.FlowBoxChild

    drag & drop Gtk.FlowBoxChild: drag-box-child.vala
    public abstract class DragBoxChild : Gtk.FlowBoxChild {
    	construct {
    		Gtk.TargetEntry te = { "STRING",        0, 0 };
    		Gtk.TargetEntry ue = { "text/plain",    0, 0 };
    		Gtk.TargetEntry ve = { "text/uri-list", 0, 1 };
    		
    		Gtk.TargetEntry targetentries[] = { te, ue, ve };
    		
    		Gtk.drag_dest_set (this, Gtk.DestDefaults.ALL, targetentries, Gdk.DragAction.COPY);
    		drag_data_received.connect ((context, x, y, data, info, time) => {
    			if (data.get_text() != null)
    				uris_received (new string[] { data.get_text() });
    			else
    				uris_received (data.get_uris());
    		});
    	}
    	
    	public signal void uris_received (string[] uris);
    }
    
    public class LabelChild : DragBoxChild {
    	Gtk.Label label;
    	
    	construct {
    		this.label = new Gtk.Label (this.text);
    		add (this.label);
    		uris_received.connect (uris => {
    			this.label.label = string.joinv ("; ", uris);
    		});
    	}
    	
    	public LabelChild (string text) {
    		GLib.Object (text : text);
    	}
    	
    	public string text { get; construct; }
    }
    
    public static void main (string[] args) {
    	Gtk.init (ref args);
    	var win = new Gtk.Window();
    	var box = new Gtk.FlowBox();
    	box.add (new LabelChild ("toto"));
    	box.add (new LabelChild ("tutu"));
    	box.add (new LabelChild ("titi"));
    	win.add (box);
    	win.show_all();
    	Gtk.main();
    }
    
    
    

    external by BZHDeveloper modified Saturday at 5:20:43 AM  2  0  2  0

    Drag & Drop data

    Drag & Drop data: drag-data-sample.vala
    public static void main (string[] args) {
    	Gtk.init (ref args);
    	var win = new Gtk.Window();
    	var box = new Gtk.EventBox();
    	var label = new Gtk.Label ("drag data here");
    	
    	Gtk.TargetEntry te = { "STRING",        0, 0 };
    	Gtk.TargetEntry ue = { "text/plain",    0, 0 };
    	Gtk.TargetEntry ve = { "text/uri-list", 0, 1 };
    	
    	Gtk.TargetEntry targetentries[] = { te, ue, ve };
    	
    	Gtk.drag_dest_set (box, Gtk.DestDefaults.ALL, targetentries, Gdk.DragAction.COPY);
    	box.drag_data_received.connect ((context, x, y, data, info, time) => {
    		print ("text : %s\n", data.get_text());
    		foreach (string uri in data.get_uris())
    			print ("uri : %s\n", uri);
    	});
    	box.add (label);
    	win.add (box);
    	win.show_all();
    	Gtk.main();
    }
    
    
    

    external by BZHDeveloper modified Saturday at 5:05:17 AM  1  0  1  0

    Gee.Collection deserialization

    Gee.Collection deserialization: gee-collection-deserialization.vala
    public class Tutu : GLib.Object {
    	Gee.ArrayList<int> list;
    	
    	construct {
    		this.list = new Gee.ArrayList<int>();
    	}
    	
    	public Gee.List<int> array {
    		get {
    			return this.list;
    		}
    	}
    	
    	public string chaine { get; set; }
    	public int64 valeur { get; set; }
    }
    
    public class Tata : GLib.Object {
    	Gee.ArrayList<Tutu> list;
    	
    	construct {
    		this.list = new Gee.ArrayList<Tutu>();
    	}
    	
    	public Gee.List<Tutu> tutus {
    		get {
    			return this.list;
    		}
    	}
    	
    	public string name { get; set; }
    }
    
    public class Tugudu : GLib.Object {
    	string str;
    	
    	public Tugudu (Value val) {
    		this.value = val;
    	}
    	
    	public GLib.Value value {
    		get {
    			return this.str;
    		}
    		set {
    			this.str = value.get_string();
    		}
    	}
    }
    
    public static void main (string[] args) {
    	string json = """{
    	"name" : "toto",
    	"tutus" : [
    		{ "chaine" : "pojposdgfj", "valeur" : 3968 },
    		{ "chaine" : "oifghsioii", "valeur" : 3096 },
    		{
    			"chaine" : "qsxziufhze",
    			"valeur" : 1932,
    			"array" : [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ]
    		}
    	]
    }""";
    	var object = GJson.Object.parse (json);
    	var tata = (Tata)GJson.deserialize_object (typeof (Tata), object);
    	print ("%s\n", tata.name);
    	tata.tutus.foreach (tutu => {
    		print ("\t%s : %lld\n", tutu.chaine, tutu.valeur);
    		foreach (int i in tutu.array)
    			print ("\t\t%d\n", i);
    		return true;
    	});
    }
    
    
    

    external by Kelvin modified Nov 4, 2017  3  0  1  0

    GPU Provider for Ubuntu indicator-multiload

    GPU Provider for Ubuntu indicator-multiload: gpuprovider.vala
    /******************************************************************************
     * Copyright (C) 2017 Yukai Miao <tjumyk@gmail.com>                           *
     *                                                                            *
     * This program is free software; you can redistribute it and/or modify       *
     * it under the terms of the GNU General Public License as published by       *
     * the Free Software Foundation; either version 3 of the License, or          *
     * (at your option) any later version.                                        *
     *                                                                            *
     * This program is distributed in the hope that it will be useful,            *
     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
     * GNU General Public License for more details.                               *
     *                                                                            *
     * You should have received a copy of the GNU General Public License along    *
     * with this program; if not, write to the Free Software Foundation, Inc.,    *
     * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
     ******************************************************************************/
    
    public class GpuProvider : Provider {
        public GpuProvider() {
            base("gpu", {"util", "memtotal", "memused", "memfree"}, 'p');
        }
    
        public override void update() {
    	    try {
                string[] spawn_args = {"nvidia-smi", "-i", "0", "--query-gpu=utilization.gpu,memory.total,memory.used,memory.free", "--format=csv,noheader,nounits"};
                string spawn_stdout;
                string spawn_stderr;
                int spawn_status;
                Process.spawn_sync(null, spawn_args, null, SpawnFlags.SEARCH_PATH, null, out spawn_stdout, out spawn_stderr, out spawn_status);
                string[] columns = spawn_stdout.split(", ");
                this.values[0] = double.parse(columns[0]) / 100.0;
                for(int i = 1 ; i < 4; ++i)
                    this.values[i] = double.parse(columns[i]) * 1000000;
            } catch (SpawnError e) {
                stdout.printf ("Error: %s\n", e.message);
            }
        }
    }
    
    
    

    external by robertsanseries modified Oct 16, 2017  3  0  1  0

    valac --pkg gtk+-3.0 EntryNumber.vala

    valac --pkg gtk+-3.0 EntryNumber.vala: EntryNumber.vala
    using Gtk;
    using Gdk;
    
    public static int main (string[] args) {
        Gtk.init (ref args);
    
        Application app = new Application ();
        app.show_all ();
        Gtk.main ();
        return 0;
    }
    
    public class Application : Gtk.Window {
        
        public Application () {
            this.title = "My Gtk.Window";
            this.window_position = Gtk.WindowPosition.CENTER;
            this.set_default_size (350, 300);
            this.hide_titlebar_when_maximized = false;
    
            EntryNumber entry_number = new EntryNumber (Gtk.InputPurpose.DIGITS);
            entry_number.include_icon_clean ();
            entry_number.changed.connect (() => {
                entry_number.input_digits(); 
            });
    
            this.add (entry_number);
        }
    }
    
    public class EntryNumber : Gtk.Entry {
    
        private string old = "";
    
        public EntryNumber (Gtk.InputPurpose purpose) {
            this.input_purpose = purpose;
        }   
    
        public  void input_digits () {
            try {
                this.text = this.text.replace (" ", "");
                
                var regex = new Regex ("""^[0-9]{1,}$""");
                var regex_match = regex.match (this.text);
                
                if (regex_match) {
                    this.old = this.text;
                } else if (this.text != "") {
                    this.text = this.old;
                }    
            } catch (Error e) {
                message (e.message);
            }    
        }
    
        public void include_icon_clean () {
            this.set_icon_from_icon_name (Gtk.EntryIconPosition.SECONDARY, "edit-clear");
            this.icon_press.connect ((pos, event) => {
                if (pos == Gtk.EntryIconPosition.SECONDARY) {
                    this.set_text ("");
                }
            });
        }
    }
    
    

    external by BZHDeveloper modified Oct 1, 2017  4  0  1  0

    Custom GStreamer playbin

    Custom GStreamer playbin: custom-playbin.vala
    namespace Vod {
    	public class Player : GLib.Object {
    		Gst.Pipeline pipeline;
    		Gst.Element decodebin;
    		Gst.Element audio_queue;
    		Gst.Element video_queue;
    		Gst.Element audio_convert;
    		Gst.Element video_convert;
    		Gst.Element asink;
    		Gst.Element vsink;
    		
    		construct {
    			this.pipeline = new Gst.Pipeline (name);
    			this.decodebin = Gst.ElementFactory.make ("decodebin", "decodebin");
    			this.audio_queue = Gst.ElementFactory.make ("queue", "audio-queue");
    			this.video_queue = Gst.ElementFactory.make ("queue", "video-queue");
    			this.audio_convert = Gst.ElementFactory.make ("audioconvert", "audio-convert");
    			this.video_convert = Gst.ElementFactory.make ("videoconvert", "video-convert");
    			this.asink = Gst.ElementFactory.make ("autoaudiosink", "audio-sink");
    			this.vsink = Gst.ElementFactory.make ("autovideosink", "video-sink");
    			
    			this.pipeline.add_many (this.decodebin, 
    				this.audio_queue, this.audio_convert, this.asink, 
    				this.video_queue, this.video_convert, this.vsink);
    			
    			this.audio_queue.link (this.audio_convert);
    			this.audio_convert.link (this.asink);
    			this.video_queue.link (this.video_convert);
    			this.video_convert.link (this.vsink);
    			
    			this.decodebin.pad_added.connect (pad => {
    				if (pad.caps.to_string().has_prefix ("audio"))
    					pad.link (this.audio_queue.get_static_pad ("sink"));
    				else
    					pad.link (this.video_queue.get_static_pad ("sink"));
    			});
    			
    			notify["source"].connect (() => {
    				this.pipeline.add (this.source);
    				this.source.link (this.decodebin);
    			});
    			
    			notify["audio-sink"].connect (() => {
    				this.audio_convert.unlink (this.asink);
    				this.pipeline.remove (this.asink);
    				this.asink = audio_sink;
    				this.pipeline.add (this.asink);
    				this.audio_convert.link (this.asink);
    			});
    			
    			notify["video-sink"].connect (() => {
    				this.video_convert.unlink (this.vsink);
    				this.pipeline.remove (this.vsink);
    				this.vsink = video_sink;
    				this.pipeline.add (this.vsink);
    				this.video_convert.link (this.vsink);
    			});
    			
    			notify["uri"].connect (() => {
    				if (this.source != null) {
    					this.source.unlink (this.decodebin);
    					this.pipeline.remove (this.source);
    				}
    				if (ReplaySrc.uri_is_valid (uri)) {
    					var src = new ReplaySrc();
    					src.location = uri;
    					this.source = src;
    				}
    				else
    					this.source = Gst.Element.make_from_uri (Gst.URIType.SRC, uri, null);
    			});
    		}
    		
    		public Player (string name) {
    			GLib.Object (name : name);
    		}
    		
    		public Gst.StateChangeReturn set_state (Gst.State state) {
    			return this.pipeline.set_state (state);
    		}
    		
    		public string name { get; construct; }
    		
    		public string uri { get; set; } 
    		
    		public Gst.Element audio_sink { get; set; }
    		
    		public Gst.Element video_sink { get; set; }
    		
    		public Gst.Element source { get; set; }
    	}
    }
    
    
    

    external by BZHDeveloper modified Aug 19, 2017  6  0  1  0

    test utilities

    test utilities: ubuntu-utils.vala
    /* utils.vapi generated by valac UNKNOWN, do not modify. */
    
    namespace System {
    	[CCode (cheader_filename = "utils.h")]
    	public class Distribution : GLib.Object {
    		public Distribution ();
    		public string codename { get; construct; }
    		public string description { get; construct; }
    		public string id { get; construct; }
    		public string release { get; construct; }
    	}
    	[CCode (cheader_filename = "utils.h")]
    	public class Kernel : GLib.Object {
    		public class Version : GLib.Object {
    			public string to_string ();
    			public string kernel_type { get; private set; }
    			public uint major { get; private set; }
    			public uint micro { get; private set; }
    			public uint minor { get; private set; }
    			public uint revision { get; private set; }
    		}
    		public Kernel ();
    		public string to_string ();
    		public string architecture { owned get; }
    		public string name { owned get; }
    		public System.Distribution operating_system { get; private set; }
    		public string pc_name { owned get; }
    		public System.Kernel.Version version { owned get; }
    	}
    	[CCode (cheader_filename = "utils.h")]
    	public class Source : GLib.Object, Gee.Hashable<System.Source> {
    		public Source (System.SourceType source_type, string uri, string suite);
    		public string to_string ();
    		public bool active { get; set construct; }
    		public Gee.Set<string> components { get; }
    		public System.SourceType source_type { get; construct; }
    		public string suite { get; construct; }
    		public string uri { get; construct; }
    	}
    	[CCode (cheader_filename = "utils.h")]
    	public class SourceList : Gee.AbstractSet<System.Source> {
    		public SourceList ();
    		public override bool add (System.Source source);
    		public override void clear ();
    		public override bool contains (System.Source source);
    		public override Gee.Iterator<System.Source> iterator ();
    		public static System.SourceList open (string path) throws GLib.Error;
    		public override bool remove (System.Source source);
    		public override bool read_only { get; }
    		public override int size { get; }
    	}
    	[CCode (cheader_filename = "utils.h")]
    	public enum SourceType {
    		DEB,
    		DEB_SRC;
    		public string get_nick ();
    		public static System.SourceType from_nick (string nick);
    	}
    }
    
    

    external by Mario Daniel Ruiz Saavedra modified Aug 13, 2017  4  0  1  0

    Emit custom notify in Vala

    Emit custom notify in Vala: custom-emit-notify.vala
    using GLib;
    
    public class TestClass : GLib.Object {
    	public string test1 {get;set;default = "test1";}
    	//Ths one won't emit on assignment
    	[CCode (notify = false)]
    	public string test2 {get;set;default = "test2";}
    
    	public static void main (string[] args) {
    		var test = new TestClass ();
    		test.notify["test1"].connect (() => GLib.print ("test1 notification\n"));
    		test.notify["test2"].connect (() => GLib.print ("test2 notification\n"));
    		test.test1 = "Ahoy";
    		test.test2 = "Újale";
    		test.test2 = "Ajúa";
    		ParamSpec pspec = ((ObjectClass) typeof (TestClass).class_ref ()).find_property ("test2");
    		GLib.print ("First try, will not work %s\n", pspec.name);
    		test.notify(pspec);
    		GLib.print ("second one, this is how it works");
    		test.notify["test2"] (pspec);
    		return;
    	}
    }
    
    

    external by ???????? modified Jul 8, 2017  6  0  2  0

    Reads all bytes from an InputStream (Vala)

    Reads all bytes from an InputStream (Vala): read_all_bytes_from_stream.vala
    public uint8[] read_all_bytes_from_stream (InputStream stream, ulong buf_size) throws Error {
    	uint8[] data = {};
    	uint8[] buf = new uint8[buf_size];
    	ssize_t bytes;
    	ulong total = 0;
    	while (true) {
    		bytes = stream.read(buf);
    		if (bytes <= 0) break;
    		
    		ulong size = (ulong) (total + bytes);
    		if (size > data.length) {
    			ulong new_len = MathUtils.next_pot(size);
    			uint8[] prev = data;
    			data = new uint8[new_len];
    			Memory.copy(&data[0], &prev[0], total);
    			prev = null;
    		}
    		Memory.copy(&data[0] + total, &buf[0], bytes);
    		total += (ulong) bytes;
    	}
    	return data[0:total];
    }
    
    
    • Public Snippets
    • Channels Snippets