Cleanup the vala code of gtk3 panel and tool by:
authorDaiki Ueno <ueno@unixuser.org>
Mon, 3 Sep 2012 03:53:18 +0000 (12:53 +0900)
committerDaiki Ueno <ueno@unixuser.org>
Mon, 3 Sep 2012 03:53:18 +0000 (12:53 +0900)
- using config.vapi
- omitting unnecessary "using X"

Also localize ibus tool and make the argument handling better.

BUG=none

Review URL: https://codereview.appspot.com/6496066

16 files changed:
bindings/vala/Makefile.am
bindings/vala/config.vapi [new file with mode: 0644]
tools/Makefile.am
tools/main.vala
ui/gtk3/Makefile.am
ui/gtk3/application.vala
ui/gtk3/candidatearea.vala
ui/gtk3/candidatepanel.vala
ui/gtk3/handle.vala
ui/gtk3/iconwidget.vala
ui/gtk3/keybindingmanager.vala
ui/gtk3/panel.vala
ui/gtk3/pango.vala
ui/gtk3/property.vala
ui/gtk3/separator.vala
ui/gtk3/switcher.vala

index 074140e..faa92a6 100644 (file)
@@ -44,6 +44,7 @@ ibus-@IBUS_API_VERSION@.vapi: $(vapi_deps)
 EXTRA_DIST = \
        IBus-1.0.metadata \
        IBus-1.0-custom.vala \
+       config.vapi \
        $(NULL)
 
 MAINTAINERCLEANFILES = ibus-@IBUS_API_VERSION@.vapi
diff --git a/bindings/vala/config.vapi b/bindings/vala/config.vapi
new file mode 100644 (file)
index 0000000..f2195da
--- /dev/null
@@ -0,0 +1,12 @@
+[CCode (cprefix = "", lower_case_cprefix = "", cheader_filename = "config.h")]
+namespace Config
+{
+    public const string PACKAGE_DATADIR;
+    public const string PACKAGE_NAME;
+    public const string PACKAGE_VERSION;
+    public const string BINDIR;
+    public const string DATADIR;
+    public const string PKGDATADIR;
+    public const string GETTEXT_PACKAGE;
+    public const string GLIB_LOCALE_DIR;
+}
index c0cacd6..830fc4f 100644 (file)
@@ -29,17 +29,16 @@ INCLUDES = \
        -I$(top_builddir)/src \
        $(NULL)
 
+# force include config.h before gi18n.h.
+AM_CPPFLAGS = -include $(CONFIG_HEADER)
+
 AM_CFLAGS = \
        @GLIB2_CFLAGS@ \
        @GIO2_CFLAGS@ \
        @GTHREAD2_CFLAGS@ \
        $(INCLUDES) \
        -DG_LOG_DOMAIN=\"IBUS\" \
-       -DPKGDATADIR=\"$(pkgdatadir)\" \
-       -DLIBEXECDIR=\"$(libexecdir)\" \
-       -DBINDIR=\"@bindir@\" \
-       -DIBUS_VERSION=\"@IBUS_VERSION@\" \
-    -DIBUS_DISABLE_DEPRECATED \
+       -DIBUS_DISABLE_DEPRECATED \
        -Wno-unused-variable \
        -Wno-unused-but-set-variable \
        -Wno-unused-function \
@@ -55,7 +54,10 @@ AM_LDADD = \
 
 AM_VALAFLAGS = \
        --vapidir=$(top_builddir)/bindings/vala \
+       --vapidir=$(top_srcdir)/bindings/vala \
        --pkg=ibus-1.0 \
+       --pkg=posix \
+       --pkg=config \
        $(NULL)
 
 bin_PROGRAMS = ibus
index 0c51c4b..ce3fe47 100644 (file)
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  * Boston, MA  02111-1307  USA
  */
-using GLib;
-using IBus;
-
-
-public extern const string IBUS_VERSION;
 
 bool name_only = false;
 
 class EngineList {
-    public EngineDesc[] data = {};
+    public IBus.EngineDesc[] data = {};
 }
 
-IBus.Bus get_bus() {
+IBus.Bus? get_bus() {
     IBus.init();
     var bus = new IBus.Bus();
+    if (!bus.is_connected ())
+        return null;
     return bus;
 }
 
 int list_engine(string[] argv) {
-    const OptionEntry[] options =  {
-        { "name-only", 0, 0, OptionArg.NONE, out name_only, "engine name only", "engine name only" },
+    const OptionEntry[] options = {
+        { "name-only", 0, 0, OptionArg.NONE, out name_only,
+          N_("List engine name only"), null },
         { null }
     };
 
-    var option = new OptionContext("command [OPTIONS]");
+    var option = new OptionContext(_("command [OPTIONS]"));
     option.add_main_entries(options, "ibus");
 
     try {
         option.parse(ref argv);
     } catch (OptionError e) {
+        stderr.printf("%s\n", e.message);
+        return Posix.EXIT_FAILURE;
     }
 
     var bus = get_bus();
+    if (bus == null) {
+        stderr.printf(_("Can't connect to IBus.\n"));
+        return Posix.EXIT_FAILURE;
+    }
 
     var engines = bus.list_engines();
 
@@ -59,7 +63,7 @@ int list_engine(string[] argv) {
         foreach (var engine in engines) {
             print("%s\n", engine.get_name());
         }
-        return 0;
+        return Posix.EXIT_SUCCESS;
     }
 
     var map = new HashTable<string, EngineList>(GLib.str_hash, GLib.str_equal);
@@ -75,13 +79,13 @@ int list_engine(string[] argv) {
 
     foreach (var language in map.get_keys()) {
         var list = map.get(language);
-        print("language: %s\n", IBus.get_language_name(language));
+        print(_("language: %s\n"), IBus.get_language_name(language));
         foreach (var engine in list.data) {
             print("  %s - %s\n", engine.get_name(), engine.get_longname());
         }
     }
 
-    return 0;
+    return Posix.EXIT_SUCCESS;
 }
 
 int get_set_engine(string[] argv) {
@@ -92,48 +96,70 @@ int get_set_engine(string[] argv) {
 
     if (engine == null) {
         var desc = bus.get_global_engine();
-        if (desc == null)
-            return -1;
+        if (desc == null) {
+            stderr.printf(_("No engine is set.\n"));
+            return Posix.EXIT_FAILURE;
+        }
         print("%s\n", desc.get_name());
-        return 0;
+        return Posix.EXIT_SUCCESS;
     }
 
-    if(!bus.set_global_engine(engine))
-        return -1;
+    if(!bus.set_global_engine(engine)) {
+        stderr.printf(_("Set global engine failed.\n"));
+        return Posix.EXIT_FAILURE;
+    }
     var desc = bus.get_global_engine();
-    if (desc == null)
-        return -1;
+    if (desc == null) {
+        stderr.printf(_("Get global engine failed.\n"));
+        return Posix.EXIT_FAILURE;
+    }
+
     string cmdline = "setxkbmap %s".printf(desc.get_layout());
     try {
         if (!GLib.Process.spawn_command_line_sync(cmdline)) {
-            warning("Switch xkb layout to %s failed.",
-                desc.get_layout());
+            stderr.printf(_("Switch xkb layout to %s failed."),
+                          desc.get_layout());
+            return Posix.EXIT_FAILURE;
         }
     } catch (GLib.SpawnError e) {
-        warning("execute setxkblayout failed");
+        stderr.printf("Execute setxkbmap failed: %s", e.message);
+        return Posix.EXIT_FAILURE;
     }
-    return 0;
+    return Posix.EXIT_SUCCESS;
 }
 
 int message_watch(string[] argv) {
-    return 0;
+    return Posix.EXIT_SUCCESS;
 }
 
 int restart_daemon(string[] argv) {
     var bus = get_bus();
+    if (bus == null) {
+        stderr.printf(_("Can't connect to IBus.\n"));
+        return Posix.EXIT_FAILURE;
+    }
     bus.exit(true);
-    return 0;
+    return Posix.EXIT_SUCCESS;
 }
 
 int exit_daemon(string[] argv) {
     var bus = get_bus();
+    if (bus == null) {
+        stderr.printf(_("Can't connect to IBus.\n"));
+        return Posix.EXIT_FAILURE;
+    }
     bus.exit(false);
-    return 0;
+    return Posix.EXIT_SUCCESS;
+}
+
+int print_version(string[] argv) {
+    print("IBus %s\n", Config.PACKAGE_VERSION);
+    return Posix.EXIT_SUCCESS;
 }
 
-int print_version (string[] argv) {
-    print("IBus %s\n", IBUS_VERSION);
-    return 0;
+int print_help(string[] argv) {
+    print_usage(stdout);
+    return Posix.EXIT_SUCCESS;
 }
 
 delegate int EntryFunc(string[] argv);
@@ -143,31 +169,43 @@ struct CommandEntry {
     EntryFunc entry;
 }
 
+static const CommandEntry commands[]  = {
+    { "engine", get_set_engine },
+    { "exit", exit_daemon },
+    { "list-engine", list_engine },
+    { "watch", message_watch },
+    { "restart", restart_daemon },
+    { "version", print_version },
+    { "help", print_help }
+};
+
+static string program_name;
+
+void print_usage(FileStream stream) {
+    stream.printf(_("Usage: %s COMMAND [OPTION...]\n\n"), program_name);
+    stream.printf(_("Commands:\n"));
+    foreach (var command in commands) {
+        stream.printf("  %s\n", command.name);
+    }
+}
+
 public int main(string[] argv) {
-    const CommandEntry commands[]  = {
-        { "engine", get_set_engine },
-        { "exit", exit_daemon },
-        { "list-engine", list_engine },
-        { "watch", message_watch },
-        { "restart", restart_daemon },
-        { "version", print_version }
-    };
+    GLib.Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.GLIB_LOCALE_DIR);
+    GLib.Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
 
-    if (argv.length >= 2) {
-        if (argv[1] == "--help" || argv[1] == "help") {
-            foreach (var command in commands) {
-                print ("  %s\n", command.name);
-            }
-            return 0;
-        }
-        string[] new_argv = argv[1:argv.length];
-        foreach (var command in commands) {
-            if (command.name == argv[1])
-                return command.entry(new_argv);
-        }
-        warning("%s is unknown command!", argv[1]);
+    program_name = Path.get_basename(argv[0]);
+    if (argv.length < 2) {
+        print_usage(stderr);
+        return Posix.EXIT_FAILURE;
     }
 
-    return -1;
-}
+    string[] new_argv = argv[1:argv.length];
+    foreach (var command in commands) {
+        if (command.name == argv[1])
+            return command.entry(new_argv);
+    }
 
+    stderr.printf(_("%s is unknown command!\n"), argv[1]);
+    print_usage(stderr);
+    return Posix.EXIT_FAILURE;
+}
index 639e3ea..8d362bc 100644 (file)
@@ -44,6 +44,9 @@ INCLUDES = \
 
 USE_SYMBOL_ICON = FALSE
 
+# force include config.h before gi18n.h.
+AM_CPPFLAGS = -include $(CONFIG_HEADER)
+
 AM_CFLAGS = \
        @GLIB2_CFLAGS@ \
        @GIO2_CFLAGS@ \
@@ -51,14 +54,9 @@ AM_CFLAGS = \
        @GTK3_CFLAGS@ \
        @X11_CFLAGS@ \
        $(INCLUDES) \
-       -DGETTEXT_PACKAGE=\"@GETTEXT_PACKAGE@\" \
-       -DGLIB_LOCALE_DIR=\"@GLIB_LOCALE_DIR@\" \
        -DG_LOG_DOMAIN=\"IBUS\" \
-       -DPKGDATADIR=\"$(pkgdatadir)\" \
-       -DLIBEXECDIR=\"$(libexecdir)\" \
-       -DBINDIR=\"@bindir@\" \
-    -DIBUS_DISABLE_DEPRECATED \
-       -DIBUS_VERSION=\"@IBUS_VERSION@\" \
+       -DBINDIR=\"$(bindir)\" \
+       -DIBUS_DISABLE_DEPRECATED \
        -DSWITCHER_USE_SYMBOL_ICON=$(USE_SYMBOL_ICON) \
        -Wno-unused-variable \
        -Wno-unused-but-set-variable \
@@ -78,10 +76,12 @@ AM_LDADD = \
 
 AM_VALAFLAGS = \
        --vapidir=$(top_builddir)/bindings/vala \
+       --vapidir=$(top_srcdir)/bindings/vala \
        --pkg=posix \
        --pkg=gtk+-3.0 \
        --pkg=gdk-x11-3.0 \
        --pkg=ibus-1.0 \
+       --pkg=config \
        $(NULL)
 
 libexec_PROGRAMS = ibus-ui-gtk3
index b54821c..fd9ade7 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using IBus;
-using GLib;
-using Gtk;
-
-public extern const string GETTEXT_PACKAGE;
-public extern const string GLIB_LOCALE_DIR;
-
 class Application {
     private IBus.Bus m_bus;
     private Panel m_panel;
     private IBus.Config m_config;
 
     public Application(string[] argv) {
-        GLib.Intl.bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
-        GLib.Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
+        GLib.Intl.bindtextdomain(Config.GETTEXT_PACKAGE,
+                                 Config.GLIB_LOCALE_DIR);
+        GLib.Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8");
         IBus.init();
         Gtk.init(ref argv);
 
index 85a830d..611f810 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using Gtk;
-using IBus;
-using Pango;
-
 class CandidateArea : Gtk.Box {
     private bool m_vertical;
     private Gtk.Label[] m_labels;
index 0cf3c57..ae40b5b 100644 (file)
@@ -20,9 +20,6 @@
  * Boston, MA  02111-1307  USA
  */
 
-using Gtk;
-using Pango;
-
 public class CandidatePanel : Gtk.HBox{
     private bool m_vertical = true;
     private Gtk.Window m_toplevel;
index 4fe9a7f..08e6ef3 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using Cairo;
-using Gdk;
-using Gtk;
-
 class Handle : Gtk.EventBox {
     private bool m_move_begined;
     private Gdk.Rectangle m_workarea;
index d73cb6b..4dfcfea 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using Gdk;
-using GLib;
-using Gtk;
-
 class IconWidget: Gtk.Image {
     public IconWidget(string icon, int size) {
         Gdk.Pixbuf pixbuf = null;
index 2e1be4a..9b53b09 100644 (file)
@@ -10,11 +10,6 @@ valac --pkg gtk+-2.0 --pkg x11 --pkg gdk-x11-2.0 --pkg gee-1.0 keybinding-manage
  * @author Oliver Sauder <os@esite.ch>
  */
 
-using Gdk;
-using GLib;
-using Gtk;
-using X;
-
 extern bool grab_keycode (Gdk.Display display,
                           uint keyval,
                           uint modifiers);
index 25d705d..40079ec 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using IBus;
-using GLib;
-using Gtk;
-using Posix;
-
-public extern const string IBUS_VERSION;
-public extern const string BINDIR;
-
 class Panel : IBus.PanelService {
     private IBus.Bus m_bus;
     private IBus.Config m_config;
@@ -240,7 +232,7 @@ class Panel : IBus.PanelService {
                     engine.get_layout());
             }
         } catch (GLib.SpawnError e) {
-            warning("execute setxkblayout failed");
+            warning("Execute setxkbmap failed: %s", e.message);
         }
     }
 
@@ -338,7 +330,7 @@ class Panel : IBus.PanelService {
             m_setup_pid = 0;
         }
 
-        string binary = GLib.Path.build_filename(BINDIR, "ibus-setup");
+        string binary = GLib.Path.build_filename(Config.BINDIR, "ibus-setup");
         try {
             GLib.Process.spawn_async(null,
                                      {binary, "ibus-setup"},
@@ -363,7 +355,7 @@ class Panel : IBus.PanelService {
         if (m_about_dialog == null) {
             m_about_dialog = new Gtk.AboutDialog();
             m_about_dialog.set_program_name("IBus");
-            m_about_dialog.set_version(IBUS_VERSION);
+            m_about_dialog.set_version(Config.PACKAGE_VERSION);
 
             string copyright = _(
                 "Copyright (c) 2007-2012 Peng Huang\n" +
@@ -404,7 +396,7 @@ class Panel : IBus.PanelService {
             item.activate.connect((i) => show_about_dialog());
             m_sys_menu.append(item);
 
-            m_sys_menu.append(new SeparatorMenuItem());
+            m_sys_menu.append(new Gtk.SeparatorMenuItem());
 
             item = new Gtk.ImageMenuItem.from_stock(Gtk.Stock.REFRESH, null);
             item.set_label(_("Restart"));
@@ -431,7 +423,7 @@ class Panel : IBus.PanelService {
         // Show properties and IME switching menu
         m_property_manager.create_menu_items(m_ime_menu);
 
-        m_ime_menu.append(new SeparatorMenuItem());
+        m_ime_menu.append(new Gtk.SeparatorMenuItem());
 
         int width, height;
         Gtk.icon_size_lookup(Gtk.IconSize.MENU, out width, out height);
index adcce43..9f8f065 100644 (file)
@@ -20,9 +20,6 @@
  * Boston, MA  02111-1307  USA
  */
 
-using IBus;
-using Pango;
-
 Pango.AttrList get_pango_attr_list_from_ibus_text(IBus.Text text) {
     Pango.AttrList pango_attrs = new Pango.AttrList();
     unowned IBus.AttrList attrs = text.get_attributes();
index eb35636..718ba94 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using IBus;
-using GLib;
-using Gtk;
-
 public class PropertyManager {
     private IBus.PropList m_props;
 
index 0917f4c..0e1dbc8 100644 (file)
@@ -20,8 +20,6 @@
  * Boston, MA  02111-1307  USA
  */
 
-using Gtk;
-
 class HSeparator : Gtk.HSeparator {
     public HSeparator() {
         GLib.Object(
index b543a8f..7566466 100644 (file)
  * Boston, MA  02111-1307  USA
  */
 
-using Atk;
-using Cairo;
-using Gdk;
-using GLib;
-using Gtk;
-using IBus;
-using Pango;
-
 class Switcher : Gtk.Window {
     public extern const bool USE_SYMBOL_ICON;
     private const int DEFAULT_FONT_SIZE = 16;