Vala 1.7.7 类关系表

class vala.CodeVisitor
  class vala.FlowAnalyzer
  class vala.Genie.Parser
  class vala.GirParser
  class vala.Parser
  class vala.SemanticAnalyzer
  class vala.SymbolResolver
  class vala.CodeWriter
  class vala.CodeGenerator
    class codegen.CCodeBaseModule
  	  class codegen.CCodeStructModule
  	    class codegen.CCodeMethodModule
  	      class codegen.CCodeControlFlowModule
  	        class codegen.CCodeMemberAccessModule
  	          class codegen.CCodeAssignmentModule
  	            class codegen.CCodeMethodCallModule
  	              class codegen.CCodeArrayModule
  	                class codegen.CCodeDelegateModule
  	                  class codegen.GErrorModule
  	                    class codegen.GTypeModule
  	                      class codegen.GObjectModule
  	                        class codegen.GSignalModule
  	                          class codegen.GAsyncModule
  	                            class codegen.GVariantModule
  	                              class codegen.GDBusModule
  	                                class codegen.GDBusClientModule
  	                                  class codegen.GDBusServerModule
  class codegen.GIRWriter
 
class vala.AttributeCache
  class codegen.CCodeAttribute
 
class codegen.CCodeCompiler
 
class codegen.TypeRegisterFunction
  class codegen.ClassRegisterFunction
  class codegen.EnumRegisterFunction
  class codegen.InterfaceRegisterFunction
  class codegen.StructRegisterFunction
 
class vala.CodeNode
  class vala.Expression
    class vala.AddressofExpression
    class vala.ArrayCreationExpression
    class vala.Assignment
    class vala.BaseAccess
    class vala.BinaryExpression
    class vala.Literal
      class vala.BooleanLiteral
      class vala.CharacterLiteral
      class vala.IntegerLiteral
      class vala.NullLiteral
      class vala.RealLiteral
      class vala.RegexLiteral
      class vala.StringLiteral
    class vala.CastExpression
    class vala.ConditionalExpression
    class vala.ElementAccess
    class vala.InitializerList
    class vala.LambdaExpression
    class vala.MemberAccess
    class vala.MethodCall
    class vala.NamedArgument
    class vala.ObjectCreationExpression
    class vala.PointerIndirection
    class vala.ReferenceTransferExpression
    class vala.SizeofExpression
    class vala.SliceExpression
    class vala.Template
    class vala.Tuple
    class vala.TypeCheck
    class vala.TypeofExpression
    class vala.UnaryExpression
  class vala.DataType
    class codegen.CType
    class vala.ReferenceType
      class vala.ArrayType
      class vala.ClassType
      class vala.ErrorType
      class vala.InterfaceType
      class vala.NullType
      class vala.ObjectType
    class vala.ValueType
      class vala.BooleanType
      class vala.EnumValueType
      class vala.FloatingType
      class vala.IntegerType
      class vala.StructValueType
    class vala.DelegateType
    class vala.FieldPrototype
    class vala.GenericType
    class vala.InvalidType
    class vala.MethodType
    class vala.PointerType
    class vala.PostfixExpression
    class vala.SignalType
    class vala.UnresolvedType
    class vala.VoidType
  class vala.Symbol
    class vala.Variable
      class vala.Field : interface vala.Lockable
        class vala.ArrayLengthField
      class vala.LocalVariable
      class vala.Parameter
    class vala.Block : interface vala.Statement
      class vala.ForeachStatement
      class vala.SwitchSection
    class vala.Subroutine
      class vala.Method
        class vala.ArrayMoveMethod
        class vala.ArrayResizeMethod
        class vala.CreationMethod
      class vala.Constructor
      class vala.Destructor
      class vala.DynamicMethod
      class vala.PropertyAccessor
    class vala.TypeSymbol
      class vala.ObjectTypeSymbol
        class vala.Class
        class vala.Interface
      class vala.Delegate
      class vala.Enum
      class vala.ErrorCode
      class vala.ErrorDomain
      class vala.Struct
    class vala.Constant : interface vala.Lockable
      class vala.EnumValue
    class vala.Property : interface vala.Lockable
      class vala.DynamicProperty
    class vala.Signal : interface vala.Lockable
      class vala.DynamicSignal
    class vala.Namespace
    class vala.TypeParameter
    class vala.UnresolvedSymbol
  class vala.Attribute
  class vala.BreakStatement : interface vala.Statement
  class vala.CatchClause
  class vala.ContinueStatement : interface vala.Statement
  class vala.DeclarationStatement : interface vala.Statement
  class vala.DeleteStatement : interface vala.Statement
  class vala.DoStatement : interface vala.Statement
  class vala.EmptyStatement : interface vala.Statement
  class vala.ExpressionStatement : interface vala.Statement
  class vala.ForStatement : interface vala.Statement
  class vala.IfStatement : interface vala.Statement
  class vala.LockStatement : interface vala.Statement
  class vala.Loop : interface vala.Statement
  class vala.ReturnStatement : interface vala.Statement
  class vala.MemberInitializer
  class vala.StatementList : interface vala.Statement
  class vala.SwitchLabel
  class vala.SwitchStatement : interface vala.Statement
  class vala.ThrowStatement : interface vala.Statement
  class vala.TryStatement : interface vala.Statement
  class vala.UnlockStatement : interface vala.Statement
  class vala.UsingDirective
  class vala.WhileStatement : interface vala.Statement
  class vala.YieldStatement : interface vala.Statement
  interface vala.Statement
 
class vala.BasicBlock
 
class vala.CodeContext
 
class vala.Comment
  class vala.GirComment
 
class vala.Genie.Scanner
 
class vala.Scanner
 
class vala.Scope
 
class vala.SourceFile
 
class vala.SourceReference
 
class vala.TargetValue
 
class Object
  class vala.MarkupReader
  class vala.Report
 
class vala.PhiFunction
 
interface vala.Lockable
 
class ccode.CCodeNode
  class ccode.CCodeExpression
    class ccode.CCodeAssignment
    class ccode.CCodeBinaryExpression
    class ccode.CCodeCastExpression
    class ccode.CCodeCommaExpression
    class ccode.CCodeConditionalExpression
    class ccode.CCodeConstant
    class ccode.CCodeElementAccess
    class ccode.CCodeFunctionCall
    class ccode.CCodeIdentifier
    class ccode.CCodeInitializerList
    class ccode.CCodeInvalidExpression
    class ccode.CCodeMemberAccess
    class ccode.CCodeParenthesizedExpression
    class ccode.CCodeUnaryExpression
  class ccode.CCodeStatement
    class ccode.CCodeBlock
      class ccode.CCodeSwitchStatement
    class ccode.CCodeBreakStatement
    class ccode.CCodeCaseStatement
    class ccode.CCodeContinueStatement
    class ccode.CCodeDeclaration
    class ccode.CCodeDoStatement
    class ccode.CCodeEmptyStatement
    class ccode.CCodeExpressionStatement
    class ccode.CCodeForStatement
    class ccode.CCodeGotoStatement
    class ccode.CCodeIfStatement
    class ccode.CCodeLabel
    class ccode.CCodeReturnStatement
    class ccode.CCodeWhileStatement
  class ccode.CCodeComment
  class ccode.CCodeDeclarator
    class ccode.CCodeFunctionDeclarator
    class ccode.CCodeVariableDeclarator
  class ccode.CCodeEnum
  class ccode.CCodeEnumValue
  class ccode.CCodeFragment
    class ccode.CCodeOnceSection
  class ccode.CCodeFunction
  class ccode.CCodeIncludeDirective
  class ccode.CCodeLineDirective
  class ccode.CCodeMacroReplacement
  class ccode.CCodeNewline
  class ccode.CCodeParameter
  class ccode.CCodeStruct
  class ccode.CCodeTypeDefinition
 
class ccode.CCodeFile
 
enum ccode.CCodeModifiers
 
class ccode.CCodeWriter
 
class compiler.Compiler

Over!

[Vala] D-Bus 客户端例程:方法调用返回多个值

Vala D-Bus Client Examples 中描述了如何通过定义 D-Bus 接口至 Vala 的 Interface 的映射,并创建一个实现了此本地 Interface 的 Proxy,进行方法调用、属性访问与信号侦听。

但在最近的开发中遇到了 D-Bus 接口中方法返回两个或多个值的情况,这样的情况又如何定义接口方法?看下面的例子:

例如接口 org.freedesktop.Telepathy.Connection.FUTURE 存在一个方法 EnsureSidecar,其原型如下:

EnsureSidecar (s: Main_Interface) → o: Path, a{sv}: Properties

正确定义 1:

namespace Hev {
 
    [DBus (name = "org.freedesktop.Telepathy.Connection.FUTURE")]
    public interface Future : GLib.Object {
        public abstract void ensure_sidecar (string iface, out ObjectPath path,
                out HashTable<string,Variant> props) throws IOError;
    }
}

正确定义2:

namespace Hev {
 
    [DBus (name = "org.freedesktop.Telepathy.Connection.FUTURE")]
    public interface Future : GLib.Object {
        public abstract HashTable<string,Variant> ensure_sidecar (string iface,
                out ObjectPath path) throws IOError;
    }
}

错误定义:

namespace Hev {
 
    [DBus (name = "org.freedesktop.Telepathy.Connection.FUTURE")]
    public interface Future : GLib.Object {
        public abstract ObjectPath ensure_sidecar (string iface,
                out HashTable<string,Variant> props) throws IOError;
    }
}

从上面的例子中可以看出优先级是:out 参数 > 返回值,为了更直观,建议使用正确定义1的方式。

Over!

安装 Devhelp 版本的 Valadoc

Valadoc 是一个非常优秀的的在线 Vala 语言 API 参考手册,但是就国内的网络环境使用效果是很不爽的。建议安装 Devhelp 版本至本地,前提是你使用 Devhelp。

安装方法

mkdir -p ~/.local/share/devhelp/books/
mkdir tmp
cd tmp
wget http://heiher.info/sftp/files/valadoc-list.txt
for i in `cat valadoc-list.txt`; do
wget $i && tar -xf `basename $i` -C ~/.local/share/devhelp/books/
done

下载列表 (valadoc-list.txt)

http://www.valadoc.org/alsa/alsa.tar.bz2
http://www.valadoc.org/atasmart/atasmart.tar.bz2
http://www.valadoc.org/atk/atk.tar.bz2
http://www.valadoc.org/avahi-gobject/avahi-gobject.tar.bz2
http://www.valadoc.org/bzlib/bzlib.tar.bz2
http://www.valadoc.org/cairo/cairo.tar.bz2
http://www.valadoc.org/cairo-xcb/cairo-xcb.tar.bz2
http://www.valadoc.org/ccss-1/ccss-1.tar.bz2
http://www.valadoc.org/champlain-0.14/champlain-0.14.tar.bz2
http://www.valadoc.org/champlain-gtk-0.14/champlain-gtk-0.14.tar.bz2
http://www.valadoc.org/clutter-1.0/clutter-1.0.tar.bz2
http://www.valadoc.org/clutter-gst-1.0/clutter-gst-1.0.tar.bz2
http://www.valadoc.org/clutter-gtk-0.10/clutter-gtk-0.10.tar.bz2
http://www.valadoc.org/clutter-gtk-1.0/clutter-gtk-1.0.tar.bz2
http://www.valadoc.org/clutter-json-1.0/clutter-json-1.0.tar.bz2
http://www.valadoc.org/cogl-1.0/cogl-1.0.tar.bz2
http://www.valadoc.org/cogl-pango-1.0/cogl-pango-1.0.tar.bz2
http://www.valadoc.org/colord/colord.tar.bz2
http://www.valadoc.org/curses/curses.tar.bz2
http://www.valadoc.org/dbus-glib-1/dbus-glib-1.tar.bz2
http://www.valadoc.org/Dbusmenu-0.4/Dbusmenu-0.4.tar.bz2
http://www.valadoc.org/DbusmenuGtk3-0.4/DbusmenuGtk3-0.4.tar.bz2
http://www.valadoc.org/dee-1.0/dee-1.0.tar.bz2
http://www.valadoc.org/enchant/enchant.tar.bz2
http://www.valadoc.org/fuse/fuse.tar.bz2
http://www.valadoc.org/gconf-2.0/gconf-2.0.tar.bz2
http://www.valadoc.org/gdk-2.0/gdk-2.0.tar.bz2
http://www.valadoc.org/gdk-3.0/gdk-3.0.tar.bz2
http://www.valadoc.org/gdk-pixbuf-2.0/gdk-pixbuf-2.0.tar.bz2
http://www.valadoc.org/gdk-x11-2.0/gdk-x11-2.0.tar.bz2
http://www.valadoc.org/gdk-x11-3.0/gdk-x11-3.0.tar.bz2
http://www.valadoc.org/gdl-1.0/gdl-1.0.tar.bz2
http://www.valadoc.org/gdl-3.0/gdl-3.0.tar.bz2
http://www.valadoc.org/gdu/gdu.tar.bz2
http://www.valadoc.org/gdu-gtk/gdu-gtk.tar.bz2
http://www.valadoc.org/gedit/gedit.tar.bz2
http://www.valadoc.org/gedit-2.20/gedit-2.20.tar.bz2
http://www.valadoc.org/gio-2.0/gio-2.0.tar.bz2
http://www.valadoc.org/gio-unix-2.0/gio-unix-2.0.tar.bz2
http://www.valadoc.org/glib-2.0/glib-2.0.tar.bz2
http://www.valadoc.org/gmodule-2.0/gmodule-2.0.tar.bz2
http://www.valadoc.org/gnome-keyring-1/gnome-keyring-1.tar.bz2
http://www.valadoc.org/gobject-2.0/gobject-2.0.tar.bz2
http://www.valadoc.org/gssdp-1.0/gssdp-1.0.tar.bz2
http://www.valadoc.org/gstreamer-0.10/gstreamer-0.10.tar.bz2
http://www.valadoc.org/gstreamer-app-0.10/gstreamer-app-0.10.tar.bz2
http://www.valadoc.org/gstreamer-audio-0.10/gstreamer-audio-0.10.tar.bz2
http://www.valadoc.org/gstreamer-base-0.10/gstreamer-base-0.10.tar.bz2
http://www.valadoc.org/gstreamer-cdda-0.10/gstreamer-cdda-0.10.tar.bz2
http://www.valadoc.org/gstreamer-check-0.10/gstreamer-check-0.10.tar.bz2
http://www.valadoc.org/gstreamer-controller-0.10/gstreamer-controller-0.10.tar.bz2
http://www.valadoc.org/gstreamer-dataprotocol-0.10/gstreamer-dataprotocol-0.10.tar.bz2
http://www.valadoc.org/gstreamer-fft-0.10/gstreamer-fft-0.10.tar.bz2
http://www.valadoc.org/gstreamer-interfaces-0.10/gstreamer-interfaces-0.10.tar.bz2
http://www.valadoc.org/gstreamer-net-0.10/gstreamer-net-0.10.tar.bz2
http://www.valadoc.org/gstreamer-netbuffer-0.10/gstreamer-netbuffer-0.10.tar.bz2
http://www.valadoc.org/gstreamer-pbutils-0.10/gstreamer-pbutils-0.10.tar.bz2
http://www.valadoc.org/gstreamer-riff-0.10/gstreamer-riff-0.10.tar.bz2
http://www.valadoc.org/gstreamer-rtp-0.10/gstreamer-rtp-0.10.tar.bz2
http://www.valadoc.org/gstreamer-rtsp-0.10/gstreamer-rtsp-0.10.tar.bz2
http://www.valadoc.org/gstreamer-sdp-0.10/gstreamer-sdp-0.10.tar.bz2
http://www.valadoc.org/gstreamer-tag-0.10/gstreamer-tag-0.10.tar.bz2
http://www.valadoc.org/gstreamer-video-0.10/gstreamer-video-0.10.tar.bz2
http://www.valadoc.org/gtk+-2.0/gtk+-2.0.tar.bz2
http://www.valadoc.org/gtk+-3.0/gtk+-3.0.tar.bz2
http://www.valadoc.org/gtk-vnc-2.0/gtk-vnc-2.0.tar.bz2
http://www.valadoc.org/gtkmozembed/gtkmozembed.tar.bz2
http://www.valadoc.org/gtksourceview-2.0/gtksourceview-2.0.tar.bz2
http://www.valadoc.org/gtksourceview-3.0/gtksourceview-3.0.tar.bz2
http://www.valadoc.org/gudev-1.0/gudev-1.0.tar.bz2
http://www.valadoc.org/gupnp-1.0/gupnp-1.0.tar.bz2
http://www.valadoc.org/gupnp-av-1.0/gupnp-av-1.0.tar.bz2
http://www.valadoc.org/gupnp-dlna-1.0/gupnp-dlna-1.0.tar.bz2
http://www.valadoc.org/gvnc-1.0/gvnc-1.0.tar.bz2
http://www.valadoc.org/hal/hal.tar.bz2
http://www.valadoc.org/hildon-1/hildon-1.tar.bz2
http://www.valadoc.org/hildon-fm-2/hildon-fm-2.tar.bz2
http://www.valadoc.org/javascriptcoregtk-3.0/javascriptcoregtk-3.0.tar.bz2
http://www.valadoc.org/json-glib-1.0/json-glib-1.0.tar.bz2
http://www.valadoc.org/libarchive/libarchive.tar.bz2
http://www.valadoc.org/libbonoboui-2.0/libbonoboui-2.0.tar.bz2
http://www.valadoc.org/libcanberra/libcanberra.tar.bz2
http://www.valadoc.org/libcanberra-gtk/libcanberra-gtk.tar.bz2
http://www.valadoc.org/libdaemon/libdaemon.tar.bz2
http://www.valadoc.org/libepc-1.0/libepc-1.0.tar.bz2
http://www.valadoc.org/libesmtp/libesmtp.tar.bz2
http://www.valadoc.org/libftdi/libftdi.tar.bz2
http://www.valadoc.org/libgda-4.0/libgda-4.0.tar.bz2
http://www.valadoc.org/libgda-report-4.0/libgda-report-4.0.tar.bz2
http://www.valadoc.org/libgdata/libgdata.tar.bz2
http://www.valadoc.org/libglade-2.0/libglade-2.0.tar.bz2
http://www.valadoc.org/libgsf-1/libgsf-1.tar.bz2
http://www.valadoc.org/libgvc/libgvc.tar.bz2
http://www.valadoc.org/libmagic/libmagic.tar.bz2
http://www.valadoc.org/libnl-1/libnl-1.tar.bz2
http://www.valadoc.org/libnl-2.0/libnl-2.0.tar.bz2
http://www.valadoc.org/libnl-3.0/libnl-3.0.tar.bz2
http://www.valadoc.org/libnotify/libnotify.tar.bz2
http://www.valadoc.org/liboobs-1/liboobs-1.tar.bz2
http://www.valadoc.org/libosinfo-1.0/libosinfo-1.0.tar.bz2
http://www.valadoc.org/libosso/libosso.tar.bz2
http://www.valadoc.org/libpanelapplet-2.0/libpanelapplet-2.0.tar.bz2
http://www.valadoc.org/libpeas-1.0/libpeas-1.0.tar.bz2
http://www.valadoc.org/libpq/libpq.tar.bz2
http://www.valadoc.org/libproxy-1.0/libproxy-1.0.tar.bz2
http://www.valadoc.org/librsvg-2.0/librsvg-2.0.tar.bz2
http://www.valadoc.org/libsoup-2.2/libsoup-2.2.tar.bz2
http://www.valadoc.org/libsoup-2.4/libsoup-2.4.tar.bz2
http://www.valadoc.org/libusb/libusb.tar.bz2
http://www.valadoc.org/libusb-1.0/libusb-1.0.tar.bz2
http://www.valadoc.org/libvirt-gconfig-1.0/libvirt-gconfig-1.0.tar.bz2
http://www.valadoc.org/libvirt-glib-1.0/libvirt-glib-1.0.tar.bz2
http://www.valadoc.org/libvirt-gobject-1.0/libvirt-gobject-1.0.tar.bz2
http://www.valadoc.org/libwnck-1.0/libwnck-1.0.tar.bz2
http://www.valadoc.org/libwnck-3.0/libwnck-3.0.tar.bz2
http://www.valadoc.org/libxml-2.0/libxml-2.0.tar.bz2
http://www.valadoc.org/linux/linux.tar.bz2
http://www.valadoc.org/loudmouth-1.0/loudmouth-1.0.tar.bz2
http://www.valadoc.org/lua/lua.tar.bz2
http://www.valadoc.org/mx-1.0/mx-1.0.tar.bz2
http://www.valadoc.org/mysql/mysql.tar.bz2
http://www.valadoc.org/orc-0.4/orc-0.4.tar.bz2
http://www.valadoc.org/pango/pango.tar.bz2
http://www.valadoc.org/pangocairo/pangocairo.tar.bz2
http://www.valadoc.org/pixman-1/pixman-1.tar.bz2
http://www.valadoc.org/poppler-glib/poppler-glib.tar.bz2
http://www.valadoc.org/posix/posix.tar.bz2
http://www.valadoc.org/purple/purple.tar.bz2
http://www.valadoc.org/raptor/raptor.tar.bz2
http://www.valadoc.org/rasqal/rasqal.tar.bz2
http://www.valadoc.org/readline/readline.tar.bz2
http://www.valadoc.org/rest-0.6/rest-0.6.tar.bz2
http://www.valadoc.org/rest-0.7/rest-0.7.tar.bz2
http://www.valadoc.org/rest-extras-0.6/rest-extras-0.6.tar.bz2
http://www.valadoc.org/sdl/sdl.tar.bz2
http://www.valadoc.org/sdl-gfx/sdl-gfx.tar.bz2
http://www.valadoc.org/sdl-image/sdl-image.tar.bz2
http://www.valadoc.org/sdl-mixer/sdl-mixer.tar.bz2
http://www.valadoc.org/sdl-net/sdl-net.tar.bz2
http://www.valadoc.org/sdl-ttf/sdl-ttf.tar.bz2
http://www.valadoc.org/spice-client-glib-2.0/spice-client-glib-2.0.tar.bz2
http://www.valadoc.org/spice-client-gtk-3.0/spice-client-gtk-3.0.tar.bz2
http://www.valadoc.org/spice-protocol/spice-protocol.tar.bz2
http://www.valadoc.org/sqlite3/sqlite3.tar.bz2
http://www.valadoc.org/taglib_c/taglib_c.tar.bz2
http://www.valadoc.org/telepathy-glib/telepathy-glib.tar.bz2
http://www.valadoc.org/tiff/tiff.tar.bz2
http://www.valadoc.org/tracker-indexer-module-1.0/tracker-indexer-module-1.0.tar.bz2
http://www.valadoc.org/twitter-glib-1.0/twitter-glib-1.0.tar.bz2
http://www.valadoc.org/udisks2/udisks2.tar.bz2
http://www.valadoc.org/unique-1.0/unique-1.0.tar.bz2
http://www.valadoc.org/unity/unity.tar.bz2
http://www.valadoc.org/unity-trace/unity-trace.tar.bz2
http://www.valadoc.org/v4l2/v4l2.tar.bz2
http://www.valadoc.org/vte/vte.tar.bz2
http://www.valadoc.org/vte-2.90/vte-2.90.tar.bz2
http://www.valadoc.org/webkit-1.0/webkit-1.0.tar.bz2
http://www.valadoc.org/x11/x11.tar.bz2
http://www.valadoc.org/xcb/xcb.tar.bz2
http://www.valadoc.org/xtst/xtst.tar.bz2
http://www.valadoc.org/zeitgeist-1.0/zeitgeist-1.0.tar.bz2

Over!

A simple FSM in Vala

一个使用 Vala 语言实现的简单的有限状态机。

FsmState – 状态机状态基类

/*
 ============================================================================
 Name        : hev-fsm-state.vala
 Author      : Heiher <admin @heiher.info>
 Version     : 0.0.1
 Copyright   : Copyright (C) 2012 everyone.
 Description : 
 ============================================================================
 */
 
using GLib;
 
namespace Hev {
 
	public class FsmState<g ,H> : Object {
 
		public virtual signal void enter(G obj, H input) {
			message("enter");
		}
 
		public virtual signal void process(G obj, H input) {
			message("process");
		}
 
		public virtual signal void leave(G obj, H input) {
			message("leave");
		}
	}
}
</g></admin>

此类定义了三个信号 enter, process, leave 及虚默认实现。enter 信号在切换入此状态时触发,process 信号在进入信号触发后触发,leave 信号为离开此状态时触发。基类中的默认信号处理函数仅仅打印出一个 message。通常从此基类派生子类并重载三个信号的默认处理函数实现特定的状态逻辑。
Continue reading A simple FSM in Vala

Gedit 3 Plugin Sample

Required bindings not yet included in vala:
Gedit >= 3.0 (gedit-3.0.vapi)
GtkSource View >= 3.0 (gtksourceview-3.0.vapi)
PeasGtk-1.0 gobject introspection package of your distro
…and Vala >= 0.11

Source & support files
A Gedit plugin is composed of just two files: a library (.so) and the plugin file definition.

Our example plugin will be contained in just one vala soure file, so with all the dependencies in place this should be the directory listing:

$ ls
gedit-3-example.plugin
gedit-3-example-plugin.vala
gedit-3.0.vapi
gtksourceview-3.0.vapi

In order to make the example really simple we decided to implement a very basic function: the plugin will just close an xml tag upon writing the ‘>’. Eg. if you write it will add the corresponding close tag.

Source code for the file: gedit-3-example-plugin.vala

using GLib;
 
namespace GeditPluginExample
{
        /*
         * This class will be instantiated and activated for each Gedit View
         */
        public class View : Gedit.ViewActivatable, Peas.ExtensionBase
        {
                public View ()
                {
                        GLib.Object ();
                }
 
                public Gedit.View view {
                         get; construct;
                }
 
                public void activate ()
                {
                        print ("View: activated\n");
                        view.key_release_event.connect (this.on_key_release);
                }
 
                public void deactivate ()
                {
                        print ("View: deactivated\n");
                        view.key_release_event.disconnect (this.on_key_release);
                }
 
                private bool on_key_release (Gtk.Widget sender, Gdk.EventKey event)
                {
                        if (event.str == ">") {
                                // Close the tag
                                Gedit.View view = (Gedit.View)sender;
                                Gtk.TextBuffer buffer = view.get_buffer ();
                                Gtk.TextIter end, start;
 
                                buffer.get_iter_at_mark (out end, (Gtk.TextMark) buffer.get_insert ());
                                if (end.backward_char ()) {
                                        start = end;
                                        if (start.backward_word_start ()) {
                                                string tag = "</%s>".printf (buffer.get_text (start, end, false));
 
                                                // add the closing tag
                                                buffer.begin_user_action ();
                                                buffer.insert_interactive_at_cursor (tag, -1, true);
                                                buffer.end_user_action ();
 
                                                // move cursor back
                                                buffer.get_iter_at_mark (out end, (Gtk.TextMark) buffer.get_insert ());
                                                end.backward_chars (tag.length);
                                                buffer.place_cursor (end);
                                        }
                                }
                        }
                        return true;
                }
        }
 
        /*
         * Plugin config dialog
         */
        public class Config : Peas.ExtensionBase, PeasGtk.Configurable
        {
                public Config () 
                {
                        Object ();
                }
 
                public Gtk.Widget create_configure_widget () 
                {
                        return new Gtk.Label (" Gedit 3.0 Example Vala Plugin ");
                }
        }
}
 
[ModuleInit]
public void peas_register_types (TypeModule module) 
{
        var objmodule = module as Peas.ObjectModule;
 
        // Register my plugin extension
        objmodule.register_extension_type (typeof (Gedit.ViewActivatable), typeof (GeditPluginExample.View));
        // Register my config dialog
        objmodule.register_extension_type (typeof (PeasGtk.Configurable), typeof (GeditPluginExample.Config));
}

Contents of the plugin definition file: gedit-3-example.plugin

[Plugin]
Module=gedit-3-example-plugin.so
IAge=2
Name=Vala Example Plugin
Description=A simple Vala Example Plugin
Authors=Andrea Del Signore <sejerpz@tin.it>
Copyright=Copyright © 2011 Andrea Del Signore
Website=http://live.gnome.org/action/Vala/Gedit3PluginSample

Compiling & Installing

$ valac --vapidir . -C gedit-3-example-plugin.vala --pkg gtk+-3.0 --pkg gedit-3.0 --pkg PeasGtk-1.0 --pkg GtkSource-3.0
$ gcc --shared -o libgedit-3-example-plugin.so gedit-3-example-plugin.c `pkg-config --cflags --libs gedit gtk+-3.0 gtksourceview-3.0 libpeas-gtk-1.0`
$ cp libgedit-3-example-plugin.so gedit-3-example.plugin ~/.local/share/gedit/plugins/

Running
Start GEdit 3 and enable the plugin from the edit -> preference menu

Over!

From: https://live.gnome.org/Vala/Gedit3PluginSample

Vala D-Bus Examples

Vala supports D-Bus inter-process communication using the GDBus API that is part of GLib/GIO since version 2.26.

Vala automatically transforms Vala style lower_case_names to D-Bus style CamelCaseNames behind the scenes. You can use methods, signals, properties in D-Bus objects as if they were Vala objects.

Using GDBus
Server

/* Note: this attribute specifies the _interface_ name.  It
 * is called 'name =' for historical reasons.
 */
[DBus (name = "org.example.Demo")]
public class DemoServer : Object {
 
    private int counter;
 
    public int ping (string msg) {
        stdout.printf ("%s\n", msg);
        return counter++;
    }
 
    public int ping_with_signal (string msg) {
        stdout.printf ("%s\n", msg);
        pong(counter, msg);
        return counter++;
    }
 
    /* Including any parameter of type GLib.BusName won't be added to the
       interface and will return the dbus sender name (who is calling the method) */
    public int ping_with_sender (string msg, GLib.BusName sender) {
        stdout.printf ("%s, from: %s\n", msg, sender);
        return counter++;
    }
 
    public void ping_error () throws Error {
        throw new DemoError.SOME_ERROR ("There was an error!");
    }
 
    public signal void pong (int count, string msg);
}
 
[DBus (name = "org.example.DemoError")]
public errordomain DemoError
{
    SOME_ERROR
}
 
void on_bus_aquired (DBusConnection conn) {
    try {
        conn.register_object ("/org/example/demo", new DemoServer ());
    } catch (IOError e) {
        stderr.printf ("Could not register service\n");
    }
}
 
void main () {
    Bus.own_name (BusType.SESSION, "org.example.Demo", BusNameOwnerFlags.NONE,
                  on_bus_aquired,
                  () => {},
                  () => stderr.printf ("Could not aquire name\n"));
 
    new MainLoop ().run ();
}
valac --pkg gio-2.0 gdbus-demo-server.vala

Client
The methods of the client interface must be defined with throws IOError.

[DBus (name = "org.example.Demo")]
interface Demo : Object {
    public abstract int ping (string msg) throws IOError;
    public abstract int ping_with_sender (string msg) throws IOError;
    public abstract int ping_with_signal (string msg) throws IOError;
    public signal void pong (int count, string msg);
}
 
void main () {
    /* Needed only if your client is listening to signals; you can omit it otherwise */
    var loop = new MainLoop();
 
    /* Important: keep demo variable out of try/catch scope not lose signals! */
    Demo demo = null;
 
    try {
        demo = Bus.get_proxy_sync (BusType.SESSION, "org.example.Demo",
                                                    "/org/example/demo");
 
        /* Connecting to signal pong! */
        demo.pong.connect((c, m) => {
            stdout.printf ("Got pong %d for msg '%s'\n", c, m);
            loop.quit ();
        });
 
        int pong = demo.ping ("Hello from Vala");
        stdout.printf ("%d\n", pong);
 
        pong = demo.ping_with_sender ("Hello from Vala with sender");
        stdout.printf ("%d\n", pong);
 
        pong = demo.ping_with_signal ("Hello from Vala with signal");
        stdout.printf ("%d\n", pong);
 
    } catch (IOError e) {
        stderr.printf ("%s\n", e.message);
    }
    loop.run();
}
valac --pkg gio-2.0 gdbus-demo-client.vala

Type Table

D-Bus Vala Description Example
b bool Boolean
y uint8 Byte
i int Integer
u uint Unsigned Integer
n int16 16-bit Integer
q uint16 Unsigned 16-bit Integer
x int64 64-bit Integer
t uint64 Unsigned 64-bit Integer
d double Double
s string String
v GLib.Variant Variant
o GLib.ObjectPath Object Path
a [] Array ai maps to int[]
a{} GLib.HashTable<,> Dictionary a{sv} maps to HashTable<string, Variant>
() a struct type Struct a(ii) maps to Foo[] where Foo might be defined as
struct Foo { public int a; public int b };

Debugging D-Bus Applications
D-Feet
D-Feet is a graphical D-Bus debugger. This is what our little D-Bus service looks like in D-Feet:

dbus-monitor
Open a terminal and enter:

dbus-monitor

Excerpt from the output showing a property change notification:

signal sender=:1.454 -> dest=(null destination) serial=9 path=/org/example/demo; interface=org.freedesktop.DBus.Properties; member=PropertiesChanged
   string "org.example.Demo"
   array [
      dict entry(
         string "pubprop"
         variant             string "1018873421"
      )
   ]
   array [
   ]

Service with D-Bus property change notifications
This example will setup a D-Bus service that can send notifications on the change of properties. (example code partly by Faheem)

The timeout will change the property every few seconds. The notifications can be visualized by the terminal program ‘dbus-monitor’ that comes with most distributions.

[DBus (name = "org.example.Demo")]
public class DemoServer : Object {
 
    public string pubprop { owned get; set; }
 
    private weak DBusConnection conn;
 
    public DemoServer (DBusConnection conn) {
        this.conn = conn;
        this.notify.connect (send_property_change);
    }
 
    private void send_property_change (ParamSpec p) {
        var builder = new VariantBuilder (VariantType.ARRAY);
        var invalid_builder = new VariantBuilder (new VariantType ("as"));
 
        if (p.name == "pubprop") {
            Variant i = pubprop;
            builder.add ("{sv}", "pubprop", i);
        }
 
        try {
            conn.emit_signal (null, 
                              "/org/example/demo", 
                              "org.freedesktop.DBus.Properties", 
                              "PropertiesChanged", 
                              new Variant ("(sa{sv}as)", 
                                           "org.example.Demo", 
                                           builder, 
                                           invalid_builder)
                              );
        } catch (Error e) {
            stderr.printf ("%s\n", e.message);
        }
    }
}
 
public class NotificationsTest : Object {
 
    private DemoServer dserver;
 
    public NotificationsTest () {
        Bus.own_name (BusType.SESSION, "org.example.Demo", BusNameOwnerFlags.NONE,
                      on_bus_acquired, on_name_acquired, on_name_lost);
    }
 
    private void on_bus_acquired (DBusConnection conn) {
        print ("bus acquired\n");
        try {
            this.dserver = new DemoServer (conn);
            conn.register_object ("/org/example/demo", this.dserver);
        } catch (IOError e) {
            print ("%s\n", e.message);
        }
    }
 
    private void on_name_acquired () {
        print ("name acquired\n");
    }  
 
    private void on_name_lost () {
        print ("name_lost\n");
    }
 
    public void setup_timeout () {
        Timeout.add_seconds (4, () => {
            dserver.pubprop = Random.next_int ().to_string ();
            return true;
        });
    }
}
 
void main () {
    var nt = new NotificationsTest ();
    nt.setup_timeout ();
    new MainLoop ().run ();
}
valac --pkg gio-2.0 gdbus-change-notificationst.vala

Over!

From: https://live.gnome.org/Vala/DBusServerSample

HevSCGIServer 混合语言开发

HevSCGIServerLibrary 实现了 GObject Introspection 支持且生成了 Vala API。从此,HevSCGIServer 框架支持混合语言编程了,理论上只要支持 GObject Introspection 的语言都可以用来开发。

目前,存在一个 Vala 语言编写的实例程序 HevSCGIServerVala,功能与 C 语言实现的 HevSCGIServerHello 一样。

下一步将通过模块的形式实现一个 JavaScript 语言的解释器,将意味着 Web Application 可以使用 JavaScript 语言编写,要注意的是它可以使用几乎所有的系统库哦。(Gjs 通过 GObject Introspection 可调用几乎所有的库)

Over!

使用 Builder 构造自定义构件

Vala 源代码

/* test.vala
 * Heiher <admin@heiher.info>
 */
 
namespace Hev {
 
public class Window : Gtk.Window {
}
 
static int main(string[] args) {
	Gtk.init(ref args);
 
	try {
		var builder = new Gtk.Builder();
		builder.add_from_file("test.xml");
		builder.connect_signals(null);
 
		Gtk.main();
	} catch(Error e) {
		stderr.printf("Erro: %s\r\n", e.message);
	}
 
	return 0;
}
 
}

test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<interface>
	<object class="HevWindow" id="window_test">
		<property name="visible">TRUE</property>
		<property name="title">Test</property>
		<signal name="delete-event" handler="gtk_main_quit" />
	</object>
</interface>

编译&执行

valac --pkg gtk+-2.0 --pkg gmodule-2.0 test.vala  # gmodule-2.0 必需
./test

Over!

学习 Vala 了

Vala 是 GObject 类型系统编译器,官方的介绍是“Vala is a new programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C. “

Vala 使用类似 C# 的语法,属高级语言,比C语言易用不错。它的编译流程是先将 vala 源代码编译成同等的 C 源代码,再使用本地C编译器将C源代码编译中本地机器码。Vala 和 C 的程序执行效率相当,但是 Vala 的开发效率要高的多。

Vala 是面向对象的,默认基于 GObject 对象系统,但是不强制基于它,还可以基于其它的类型系统。很多程序库都有它的绑定且在增加。

Vala 的编译器系统也是使用 Vala 语言开发的,又基于 GLib 移植性是没有问题的。它编写的程序移植也是没有问题,这个项目实在是牛!相信它会给 GNOME 带来很大的变化的!

Over!