Add general impls for converting enum values to nicks and back.

Use this for service provider related enums.
This commit is contained in:
Michael Gratton 2018-09-05 23:10:30 +10:00 committed by Michael James Gratton
parent 1f00e67bf9
commit 9884faba84
6 changed files with 68 additions and 38 deletions

View file

@ -46,22 +46,15 @@ public enum Geary.TlsNegotiationMethod {
public static TlsNegotiationMethod for_value(string value)
throws EngineError {
switch (value.ascii_up()) {
case "NONE":
return NONE;
case "START_TLS":
return START_TLS;
case "TRANSPORT":
return TRANSPORT;
}
throw new EngineError.BAD_PARAMETERS(
"Unknown Protocol value: %s", value
return ObjectUtils.from_enum_nick<TlsNegotiationMethod>(
typeof(TlsNegotiationMethod), value.ascii_down()
);
}
public string to_value() {
string value = to_string();
return value.substring(value.last_index_of("_") + 1);
return ObjectUtils.to_enum_nick<TlsNegotiationMethod>(
typeof(TlsNegotiationMethod), this
);
}
}
@ -78,22 +71,15 @@ public enum Geary.SmtpCredentials {
public static SmtpCredentials for_value(string value)
throws EngineError {
switch (value.ascii_up()) {
case "NONE":
return Geary.SmtpCredentials.NONE;
case "IMAP":
return Geary.SmtpCredentials.IMAP;
case "CUSTOM":
return Geary.SmtpCredentials.CUSTOM;
}
throw new EngineError.BAD_PARAMETERS(
"Unknown SmtpCredentials value: %s", value
return ObjectUtils.from_enum_nick<SmtpCredentials>(
typeof(SmtpCredentials), value.ascii_down()
);
}
public string to_value() {
string value = to_string();
return value.substring(value.last_index_of("_") + 1);
return ObjectUtils.to_enum_nick<SmtpCredentials>(
typeof(SmtpCredentials), this
);
}
}

View file

@ -18,24 +18,15 @@ public enum Geary.ServiceProvider {
public static ServiceProvider for_value(string value)
throws EngineError {
switch (value.ascii_up()) {
case "GMAIL":
return GMAIL;
case "YAHOO":
return YAHOO;
case "OUTLOOK":
return OUTLOOK;
case "OTHER":
return OTHER;
}
throw new EngineError.BAD_PARAMETERS(
"Unknown Geary.ServiceProvider value: %s", value
return ObjectUtils.from_enum_nick<ServiceProvider>(
typeof(ServiceProvider), value.ascii_down()
);
}
public string to_value() {
string value = to_string();
return value.substring(value.last_index_of("_") + 1);
return ObjectUtils.to_enum_nick<ServiceProvider>(
typeof(ServiceProvider), this
);
}
public void setup_service(ServiceInformation service) {

View file

@ -42,5 +42,22 @@ public void unmirror_properties(Gee.List<Binding> bindings) {
bindings.clear();
}
/** Convenience method for getting an enum value's nick name. */
internal string to_enum_nick<E>(GLib.Type type, E value) {
GLib.EnumClass enum_type = (GLib.EnumClass) type.class_ref();
return enum_type.get_value((int) value).value_nick;
}
/** Convenience method for getting an enum value's from its nick name. */
internal E from_enum_nick<E>(GLib.Type type, string nick) throws EngineError {
GLib.EnumClass enum_type = (GLib.EnumClass) type.class_ref();
unowned GLib.EnumValue? e_value = enum_type.get_value_by_nick(nick);
if (e_value == null) {
throw new EngineError.BAD_PARAMETERS(
"Unknown %s enum value: %s", typeof(E).name(), nick
);
}
return (E) e_value.value;
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright 2018 Michael Gratton <mike@vee.net>
*
* This software is licensed under the GNU Lesser General Public License
* (version 2.1 or later). See the COPYING file in this distribution.
*/
class Geary.TlsNegotiationMethodTest : TestCase {
public TlsNegotiationMethodTest() {
base("Geary.TlsNegotiationMethodTest");
add_test("to_value", to_value);
add_test("for_value", for_value);
}
public void to_value() throws GLib.Error {
assert_string("start-tls", TlsNegotiationMethod.START_TLS.to_value());
}
public void for_value() throws GLib.Error {
assert_int(
TlsNegotiationMethod.START_TLS,
TlsNegotiationMethod.for_value("start-tls"),
"start-tls"
);
assert_int(
TlsNegotiationMethod.START_TLS,
TlsNegotiationMethod.for_value("Start-TLS"),
"Start-TLS"
);
}
}

View file

@ -24,6 +24,7 @@ geary_test_engine_sources = [
'engine/api/geary-account-information-test.vala',
'engine/api/geary-attachment-test.vala',
'engine/api/geary-engine-test.vala',
'engine/api/geary-service-information-test.vala',
'engine/app/app-conversation-test.vala',
'engine/app/app-conversation-monitor-test.vala',
'engine/app/app-conversation-set-test.vala',

View file

@ -27,6 +27,7 @@ int main(string[] args) {
engine.add_suite(new Geary.EngineTest().get_suite());
engine.add_suite(new Geary.IdleManagerTest().get_suite());
engine.add_suite(new Geary.TimeoutManagerTest().get_suite());
engine.add_suite(new Geary.TlsNegotiationMethodTest().get_suite());
engine.add_suite(new Geary.App.ConversationTest().get_suite());
engine.add_suite(new Geary.App.ConversationSetTest().get_suite());
// Depends on ConversationTest and ConversationSetTest passing