blob: 0a20dd6ade89750e0940810969d71780f236f16c [file] [log] [blame]
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "flutter/shell/platform/linux/public/flutter_linux/fl_dart_project.h"
#include <gmodule.h>
/**
* FlDartProject:
*
* #FlDartProject represents a Dart project. It is used provide information
* about the application when creating a #FlView.
*/
struct _FlDartProject {
GObject parent_instance;
gchar* path;
};
enum { PROP_ASSETS_PATH = 1, PROP_ICU_DATA_PATH, PROP_PATH, PROP_LAST };
G_DEFINE_TYPE(FlDartProject, fl_dart_project, G_TYPE_OBJECT)
static void fl_dart_project_set_path(FlDartProject* self, const gchar* path) {
g_free(self->path);
if (g_path_is_absolute(path))
self->path = g_strdup(path);
else {
g_autoptr(GError) error = NULL;
g_autofree gchar* exe_path = g_file_read_link("/proc/self/exe", &error);
if (exe_path == NULL)
g_critical("Failed to determine location of executable: %s",
error->message);
g_autofree gchar* dir = g_path_get_dirname(exe_path);
self->path = g_build_filename(dir, path, NULL);
}
}
static void fl_dart_project_set_property(GObject* object,
guint prop_id,
const GValue* value,
GParamSpec* pspec) {
FlDartProject* self = FL_DART_PROJECT(object);
switch (prop_id) {
case PROP_PATH:
fl_dart_project_set_path(self, g_value_get_string(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void fl_dart_project_get_property(GObject* object,
guint prop_id,
GValue* value,
GParamSpec* pspec) {
FlDartProject* self = FL_DART_PROJECT(object);
switch (prop_id) {
case PROP_ASSETS_PATH:
g_value_take_string(value, fl_dart_project_get_assets_path(self));
break;
case PROP_ICU_DATA_PATH:
g_value_take_string(value, fl_dart_project_get_icu_data_path(self));
break;
case PROP_PATH:
g_value_set_string(value, self->path);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void fl_dart_project_dispose(GObject* object) {
FlDartProject* self = FL_DART_PROJECT(object);
g_clear_pointer(&self->path, g_free);
G_OBJECT_CLASS(fl_dart_project_parent_class)->dispose(object);
}
static void fl_dart_project_class_init(FlDartProjectClass* klass) {
G_OBJECT_CLASS(klass)->set_property = fl_dart_project_set_property;
G_OBJECT_CLASS(klass)->get_property = fl_dart_project_get_property;
G_OBJECT_CLASS(klass)->dispose = fl_dart_project_dispose;
g_object_class_install_property(
G_OBJECT_CLASS(klass), PROP_ASSETS_PATH,
g_param_spec_string(
"assets-path", "assets-path", "Path to Flutter assets", nullptr,
static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
G_OBJECT_CLASS(klass), PROP_ICU_DATA_PATH,
g_param_spec_string(
"icu-data-path", "icu-data-path", "Path to ICU data", nullptr,
static_cast<GParamFlags>(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)));
g_object_class_install_property(
G_OBJECT_CLASS(klass), PROP_PATH,
g_param_spec_string(
"path", "path", "Path to Flutter project", nullptr,
static_cast<GParamFlags>(G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS)));
}
static void fl_dart_project_init(FlDartProject* self) {}
/**
* fl_dart_project_new:
* @path: a file path, e.g. "my_dart_project"
*
* Create a Flutter project. The project path should contain the following
* top-level items:
* - icudtl.dat (provided as a resource by the Flutter tool)
* - flutter_assets (as built by the Flutter tool)
*
* The path can either be absolute, or relative to the directory containing the
* running executable.
*
* Returns: a new #FlDartProject
*/
G_MODULE_EXPORT FlDartProject* fl_dart_project_new(const gchar* path) {
return static_cast<FlDartProject*>(
g_object_new(fl_dart_project_get_type(), "path", path, nullptr));
}
/**
* fl_dart_project_get_path:
* @view: a #FlDartProject
*
* Get the path to the directory containing the Flutter application.
*
* Returns: (type filename): a file path, e.g. "/projects/my_dart_project"
*/
G_MODULE_EXPORT const gchar* fl_dart_project_get_path(FlDartProject* self) {
g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
return self->path;
}
/**
* fl_dart_project_get_assets_path:
* @view: a #FlDartProject
*
* Get the path to the directory containing the assets used in the Flutter
* application.
*
* Returns: (type filename): a file path, e.g.
* "/projects/my_dart_project/assets"
*/
G_MODULE_EXPORT gchar* fl_dart_project_get_assets_path(FlDartProject* self) {
g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
return g_build_filename(self->path, "flutter_assets", NULL);
}
/**
* fl_dart_project_get_icu_data_path:
* @view: a #FlDartProject
*
* Get the path to the ICU data file in the Flutter application.
*
* Returns: (type filename): a file path, e.g.
* "/projects/my_dart_project/icudtl.dat"
*/
G_MODULE_EXPORT gchar* fl_dart_project_get_icu_data_path(FlDartProject* self) {
g_return_val_if_fail(FL_IS_DART_PROJECT(self), nullptr);
return g_build_filename(self->path, "icudtl.dat", NULL);
}