aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Hua <william.hua@canonical.com>2013-06-04 10:09:22 -0400
committerWilliam Hua <william.hua@canonical.com>2013-06-04 10:09:22 -0400
commit7f9e0c6f74e4d85db873f2d8782f6b3da2d74c85 (patch)
tree9531d9373259ac252e11a3556a54f7855db37ad9
parentaabf5155c14bc9a7c757d591c3a2192e8370b2b8 (diff)
downloadayatana-indicator-keyboard-7f9e0c6f74e4d85db873f2d8782f6b3da2d74c85.tar.gz
ayatana-indicator-keyboard-7f9e0c6f74e4d85db873f2d8782f6b3da2d74c85.tar.bz2
ayatana-indicator-keyboard-7f9e0c6f74e4d85db873f2d8782f6b3da2d74c85.zip
Factor out common code.
-rw-r--r--.bzrignore3
-rw-r--r--lib/Makefile.am3
-rw-r--r--lib/common.vala61
-rw-r--r--lib/main.vala68
4 files changed, 68 insertions, 67 deletions
diff --git a/.bzrignore b/.bzrignore
index f0055e0a..a259ba54 100644
--- a/.bzrignore
+++ b/.bzrignore
@@ -21,7 +21,8 @@ debian/tmp
indicator-keyboard-service
indicator-keyboard.indicator
indicator-keyboard.service
+lib/common.c
+lib/main.c
libtool
-main.c
po/POTFILES
po/stamp-it
diff --git a/lib/Makefile.am b/lib/Makefile.am
index b7fae014..80496827 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -5,7 +5,8 @@ AM_VALAFLAGS = --metadatadir $(top_srcdir)/deps \
libexec_PROGRAMS = indicator-keyboard-service
-indicator_keyboard_service_SOURCES = main.vala
+indicator_keyboard_service_SOURCES = main.vala \
+ common.vala
indicator_keyboard_service_VALAFLAGS = $(AM_VALAFLAGS) \
--pkg posix \
--pkg pangocairo \
diff --git a/lib/common.vala b/lib/common.vala
new file mode 100644
index 00000000..83652c8d
--- /dev/null
+++ b/lib/common.vala
@@ -0,0 +1,61 @@
+string get_abbreviation (string name) {
+ string abbreviation = null;
+
+ if (name != null) {
+ char letters[2];
+ var index = 0;
+
+ for (var i = 0; i < name.length && index < 2; i++) {
+ if (name[i].isupper ()) {
+ letters[index++] = name[i];
+ }
+ }
+
+ if (index < 2) {
+ index = 0;
+
+ for (var i = 0; i < name.length && index < 2; i++) {
+ if (name[i].isalpha () && (i == 0 || !name[i - 1].isalpha ())) {
+ letters[index++] = name[i++].toupper ();
+ }
+ }
+
+ if (index < 2) {
+ index = 0;
+
+ for (var i = 0; i < name.length && index < 2; i++) {
+ if (name[i].isalpha ()) {
+ letters[index++] = name[i];
+ }
+ }
+ }
+ }
+
+ if (index == 2) {
+ abbreviation = @"$(letters[0])$(letters[1])";
+ } else if (index == 1) {
+ abbreviation = @"$(letters[0])";
+ }
+ }
+
+ return abbreviation;
+}
+
+string? get_display_name (string? layout) {
+ if (layout == null) {
+ return null;
+ }
+
+ var language = Xkl.get_language_name (layout);
+ var country = Xkl.get_country_name (layout);
+
+ if (language != null && country != null) {
+ return @"$language ($country)";
+ } else if (language != null) {
+ return language;
+ } else if (country != null) {
+ return country;
+ } else {
+ return null;
+ }
+}
diff --git a/lib/main.vala b/lib/main.vala
index f6f823fc..d85a8c1b 100644
--- a/lib/main.vala
+++ b/lib/main.vala
@@ -126,50 +126,6 @@ public class Indicator.Keyboard.Service : Object {
}
[DBus (visible = false)]
- private string abbreviate_display_name (string display_name) {
- string abbreviation = null;
-
- if (display_name != null) {
- char letters[2];
- var index = 0;
-
- for (var i = 0; i < display_name.length && index < 2; i++) {
- if (display_name[i].isupper ()) {
- letters[index++] = display_name[i];
- }
- }
-
- if (index < 2) {
- index = 0;
-
- for (var i = 0; i < display_name.length && index < 2; i++) {
- if (display_name[i].isalpha () && (i == 0 || !display_name[i - 1].isalpha ())) {
- letters[index++] = display_name[i++].toupper ();
- }
- }
-
- if (index < 2) {
- index = 0;
-
- for (var i = 0; i < display_name.length && index < 2; i++) {
- if (display_name[i].isalpha ()) {
- letters[index++] = display_name[i];
- }
- }
- }
- }
-
- if (index == 1) {
- abbreviation = @"$(letters[0])";
- } else if (index == 2) {
- abbreviation = @"$(letters[0])$(letters[1])";
- }
- }
-
- return abbreviation;
- }
-
- [DBus (visible = false)]
private uint get_icon_string_subscript (uint index) {
uint icon_string_subscript = 0;
Variant array = null;
@@ -228,19 +184,10 @@ public class Indicator.Keyboard.Service : Object {
this.xkb_info.get_layout_info (name, out display_name, null, out layout_name, null);
if (display_name == null) {
- var language = Xkl.get_language_name (layout_name);
- var country = Xkl.get_country_name (layout_name);
-
- if (language != null && country != null) {
- display_name = @"$language ($country)";
- } else if (language != null) {
- display_name = language;
- } else if (country != null) {
- display_name = country;
- }
+ display_name = get_display_name (layout_name);
}
- this.icon_strings[index] = abbreviate_display_name (display_name);
+ this.icon_strings[index] = get_abbreviation (display_name);
icon_string = this.icon_strings[index];
}
}
@@ -407,16 +354,7 @@ public class Indicator.Keyboard.Service : Object {
this.xkb_info.get_layout_info (name, out display_name, null, out layout_name, null);
if (display_name == null) {
- var language = Xkl.get_language_name (layout_name);
- var country = Xkl.get_country_name (layout_name);
-
- if (language != null && country != null) {
- name = @"$language ($country)";
- } else if (language != null) {
- name = language;
- } else if (country != null) {
- name = country;
- }
+ name = get_display_name (layout_name);
} else {
name = display_name;
}