| 1 | #include "include/cw_monero/cw_monero_plugin.h" |
| 2 | |
| 3 | #include <flutter_linux/flutter_linux.h> |
| 4 | #include <gtk/gtk.h> |
| 5 | #include <sys/utsname.h> |
| 6 | |
| 7 | #include <cstring> |
| 8 | |
| 9 | #define CW_MONERO_PLUGIN(obj) \ |
| 10 | (G_TYPE_CHECK_INSTANCE_CAST((obj), cw_monero_plugin_get_type(), \ |
| 11 | CwMoneroPlugin)) |
| 12 | |
| 13 | struct _CwMoneroPlugin { |
| 14 | GObject parent_instance; |
| 15 | }; |
| 16 | |
| 17 | G_DEFINE_TYPE(CwMoneroPlugin, cw_monero_plugin, g_object_get_type()) |
| 18 | |
| 19 | // Called when a method call is received from Flutter. |
| 20 | static void cw_monero_plugin_handle_method_call( |
| 21 | CwMoneroPlugin* self, |
| 22 | FlMethodCall* method_call) { |
| 23 | g_autoptr(FlMethodResponse) response = nullptr; |
| 24 | |
| 25 | const gchar* method = fl_method_call_get_name(method_call); |
| 26 | |
| 27 | if (strcmp(method, "getPlatformVersion") == 0) { |
| 28 | struct utsname uname_data = {}; |
| 29 | uname(&uname_data); |
| 30 | g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version); |
| 31 | g_autoptr(FlValue) result = fl_value_new_string(version); |
| 32 | response = FL_METHOD_RESPONSE(fl_method_success_response_new(result)); |
| 33 | } else { |
| 34 | response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new()); |
| 35 | } |
| 36 | |
| 37 | fl_method_call_respond(method_call, response, nullptr); |
| 38 | } |
| 39 | |
| 40 | static void cw_monero_plugin_dispose(GObject* object) { |
| 41 | G_OBJECT_CLASS(cw_monero_plugin_parent_class)->dispose(object); |
| 42 | } |
| 43 | |
| 44 | static void cw_monero_plugin_class_init(CwMoneroPluginClass* klass) { |
| 45 | G_OBJECT_CLASS(klass)->dispose = cw_monero_plugin_dispose; |
| 46 | } |
| 47 | |
| 48 | static void cw_monero_plugin_init(CwMoneroPlugin* self) {} |
| 49 | |
| 50 | static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call, |
| 51 | gpointer user_data) { |
| 52 | CwMoneroPlugin* plugin = CW_MONERO_PLUGIN(user_data); |
| 53 | cw_monero_plugin_handle_method_call(plugin, method_call); |
| 54 | } |
| 55 | |
| 56 | void cw_monero_plugin_register_with_registrar(FlPluginRegistrar* registrar) { |
| 57 | CwMoneroPlugin* plugin = CW_MONERO_PLUGIN( |
| 58 | g_object_new(cw_monero_plugin_get_type(), nullptr)); |
| 59 | |
| 60 | g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new(); |
| 61 | g_autoptr(FlMethodChannel) channel = |
| 62 | fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar), |
| 63 | "cw_monero", |
| 64 | FL_METHOD_CODEC(codec)); |
| 65 | fl_method_channel_set_method_call_handler(channel, method_call_cb, |
| 66 | g_object_ref(plugin), |
| 67 | g_object_unref); |
| 68 | |
| 69 | g_object_unref(plugin); |
| 70 | } |