static gboolean gst_base_rtp_depayload_setcaps (GstPad * pad, GstCaps * caps) { GstBaseRTPDepayload *filter; GstBaseRTPDepayloadClass *bclass; GstBaseRTPDepayloadPrivate *priv; gboolean res; GstStructure *caps_struct; const GValue *value; filter = GST_BASE_RTP_DEPAYLOAD (gst_pad_get_parent (pad)); priv = filter->priv; bclass = GST_BASE_RTP_DEPAYLOAD_GET_CLASS (filter); GST_DEBUG_OBJECT (filter, "Set caps"); caps_struct = gst_caps_get_structure (caps, 0); /* get other values for newsegment */ value = gst_structure_get_value (caps_struct, "npt-start"); if (value && G_VALUE_HOLDS_UINT64 (value)) priv->npt_start = g_value_get_uint64 (value); else priv->npt_start = 0; GST_DEBUG_OBJECT (filter, "NPT start %" G_GUINT64_FORMAT, priv->npt_start); value = gst_structure_get_value (caps_struct, "npt-stop"); if (value && G_VALUE_HOLDS_UINT64 (value)) priv->npt_stop = g_value_get_uint64 (value); else priv->npt_stop = -1; GST_DEBUG_OBJECT (filter, "NPT stop %" G_GUINT64_FORMAT, priv->npt_stop); value = gst_structure_get_value (caps_struct, "play-speed"); if (value && G_VALUE_HOLDS_DOUBLE (value)) priv->play_speed = g_value_get_double (value); else priv->play_speed = 1.0; value = gst_structure_get_value (caps_struct, "play-scale"); if (value && G_VALUE_HOLDS_DOUBLE (value)) priv->play_scale = g_value_get_double (value); else priv->play_scale = 1.0; if (bclass->set_caps) res = bclass->set_caps (filter, caps); else res = TRUE; priv->negotiated = res; gst_object_unref (filter); return res; }
int ce_get_property_default (NMSetting *setting, const char *property_name) { GParamSpec *spec; GValue value = { 0, }; g_return_val_if_fail (NM_IS_SETTING (setting), -1); spec = g_object_class_find_property (G_OBJECT_GET_CLASS (setting), property_name); g_return_val_if_fail (spec != NULL, -1); g_value_init (&value, spec->value_type); g_param_value_set_default (spec, &value); if (G_VALUE_HOLDS_CHAR (&value)) return (int) g_value_get_schar (&value); else if (G_VALUE_HOLDS_INT (&value)) return g_value_get_int (&value); else if (G_VALUE_HOLDS_INT64 (&value)) return (int) g_value_get_int64 (&value); else if (G_VALUE_HOLDS_LONG (&value)) return (int) g_value_get_long (&value); else if (G_VALUE_HOLDS_UINT (&value)) return (int) g_value_get_uint (&value); else if (G_VALUE_HOLDS_UINT64 (&value)) return (int) g_value_get_uint64 (&value); else if (G_VALUE_HOLDS_ULONG (&value)) return (int) g_value_get_ulong (&value); else if (G_VALUE_HOLDS_UCHAR (&value)) return (int) g_value_get_uchar (&value); g_return_val_if_fail (FALSE, 0); return 0; }
/** * g_value_get_uint64: * @value: a valid #GValue of type %G_TYPE_UINT64 * * Get the contents of a %G_TYPE_UINT64 #GValue. * * Returns: unsigned 64bit integer contents of @value */ guint64 g_value_get_uint64 (const GValue *value) { g_return_val_if_fail (G_VALUE_HOLDS_UINT64 (value), 0); return value->data[0].v_uint64; }
/** * g_value_set_uint64: * @value: a valid #GValue of type %G_TYPE_UINT64 * @v_uint64: unsigned 64bit integer value to be set * * Set the contents of a %G_TYPE_UINT64 #GValue to @v_uint64. */ void g_value_set_uint64 (GValue *value, guint64 v_uint64) { g_return_if_fail (G_VALUE_HOLDS_UINT64 (value)); value->data[0].v_uint64 = v_uint64; }
static GVariant * g_settings_set_mapping_unsigned_int (const GValue *value, const GVariantType *expected_type) { GVariant *variant = NULL; guint64 u; if (G_VALUE_HOLDS_UINT (value)) u = g_value_get_uint (value); else if (G_VALUE_HOLDS_UINT64 (value)) u = g_value_get_uint64 (value); else return NULL; if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT16)) { if (u <= G_MAXINT16) variant = g_variant_new_int16 ((gint16) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT16)) { if (u <= G_MAXUINT16) variant = g_variant_new_uint16 ((guint16) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT32)) { if (u <= G_MAXINT32) variant = g_variant_new_int32 ((gint) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT32)) { if (u <= G_MAXUINT32) variant = g_variant_new_uint32 ((guint) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_INT64)) { if (u <= G_MAXINT64) variant = g_variant_new_int64 ((gint64) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_UINT64)) { if (u <= G_MAXUINT64) variant = g_variant_new_uint64 ((guint64) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_HANDLE)) { if (u <= G_MAXUINT32) variant = g_variant_new_handle ((guint) u); } else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_DOUBLE)) variant = g_variant_new_double ((gdouble) u); return variant; }
static gboolean g_settings_get_mapping_unsigned_int (GValue *value, GVariant *variant) { const GVariantType *type; guint64 u; type = g_variant_get_type (variant); if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT16)) u = g_variant_get_uint16 (variant); else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT32)) u = g_variant_get_uint32 (variant); else if (g_variant_type_equal (type, G_VARIANT_TYPE_UINT64)) u = g_variant_get_uint64 (variant); else if (g_variant_type_equal (type, G_VARIANT_TYPE_HANDLE)) u = g_variant_get_handle (variant); else return FALSE; if (G_VALUE_HOLDS_INT (value)) { g_value_set_int (value, u); return (u <= G_MAXINT32); } else if (G_VALUE_HOLDS_UINT (value)) { g_value_set_uint (value, u); return (u <= G_MAXUINT32); } else if (G_VALUE_HOLDS_INT64 (value)) { g_value_set_int64 (value, u); return (u <= G_MAXINT64); } else if (G_VALUE_HOLDS_UINT64 (value)) { g_value_set_uint64 (value, u); return (u <= G_MAXUINT64); } else if (G_VALUE_HOLDS_DOUBLE (value)) { g_value_set_double (value, u); return TRUE; } return FALSE; }
static gboolean g_settings_get_mapping_int (GValue *value, GVariant *variant) { const GVariantType *type; gint64 l; type = g_variant_get_type (variant); if (g_variant_type_equal (type, G_VARIANT_TYPE_INT16)) l = g_variant_get_int16 (variant); else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT32)) l = g_variant_get_int32 (variant); else if (g_variant_type_equal (type, G_VARIANT_TYPE_INT64)) l = g_variant_get_int64 (variant); else return FALSE; if (G_VALUE_HOLDS_INT (value)) { g_value_set_int (value, l); return (G_MININT32 <= l && l <= G_MAXINT32); } else if (G_VALUE_HOLDS_UINT (value)) { g_value_set_uint (value, l); return (0 <= l && l <= G_MAXUINT32); } else if (G_VALUE_HOLDS_INT64 (value)) { g_value_set_int64 (value, l); return (G_MININT64 <= l && l <= G_MAXINT64); } else if (G_VALUE_HOLDS_UINT64 (value)) { g_value_set_uint64 (value, l); return (0 <= l && l <= G_MAXUINT64); } else if (G_VALUE_HOLDS_DOUBLE (value)) { g_value_set_double (value, l); return TRUE; } return FALSE; }
static void cd_mpris2_get_time_elapsed (void) { GValue v = G_VALUE_INIT; cairo_dock_dbus_get_property_in_value_with_timeout (myData.dbus_proxy_player, "org.mpris.MediaPlayer2.Player", "Position", &v, 250); // will be call each second, 250ms is already a big timeout, we can wait the next call. if (G_VALUE_HOLDS_INT64 (&v)) myData.iCurrentTime = g_value_get_int64 (&v) / 1e6; else if (G_VALUE_HOLDS_UINT64 (&v)) myData.iCurrentTime = g_value_get_uint64 (&v) / 1e6; else if (G_VALUE_HOLDS_INT (&v)) myData.iCurrentTime = g_value_get_int (&v) / 1e6; else if (G_VALUE_HOLDS_STRING (&v)) // this is bad ! (gmusicbrowser v1.1.7) myData.iCurrentTime = atoi (g_value_get_string (&v)) / 1e6; else { if (G_IS_VALUE(&v)) // when changing song, we don't receive this value => no need to display a warning message each time cd_warning ("wrong type for the 'Position' property, please report this bug to the %s team", myData.pCurrentHandler->appclass); myData.iCurrentTime = -1; } }
/* This function really ought not to be part of Amanda. In my (Ian's) opinion, serialization and deserialization should be a part of the GValue interface. But it's not, and here we are. */ gboolean g_value_set_from_string(GValue * val, char * string) { g_return_val_if_fail(val != NULL, FALSE); g_return_val_if_fail(G_IS_VALUE(val), FALSE); if (G_VALUE_HOLDS_BOOLEAN(val)) { return g_value_set_boolean_from_string(val, string); } else if (G_VALUE_HOLDS_INT(val)) { return g_value_set_int_from_string(val, string); } else if (G_VALUE_HOLDS_UINT(val)) { return g_value_set_uint_from_string(val, string); } else if (G_VALUE_HOLDS_UINT64(val)) { return g_value_set_uint64_from_string(val, string); } else if (G_VALUE_HOLDS_STRING(val)) { g_value_set_string(val, string); return TRUE; } else if (G_VALUE_HOLDS_FLAGS(val)) { return g_value_set_flags_from_string(val, string); } return TRUE; }
static GtkCellEditable * parasite_property_cell_renderer_start_editing(GtkCellRenderer *renderer, GdkEvent *event, GtkWidget *widget, const gchar *path, GdkRectangle *background_area, GdkRectangle *cell_area, GtkCellRendererState flags) { PangoFontDescription *font_desc; GtkCellEditable *editable = NULL; GObject *object; const char *name; GValue gvalue = {0}; GParamSpec *prop; g_object_get(renderer, "object", &object, "name", &name, NULL); prop = g_object_class_find_property(G_OBJECT_GET_CLASS(object), name); if (!(prop->flags & G_PARAM_WRITABLE)) return NULL; g_value_init(&gvalue, prop->value_type); g_object_get_property(object, name, &gvalue); if (G_VALUE_HOLDS_ENUM(&gvalue) || G_VALUE_HOLDS_BOOLEAN(&gvalue)) { GtkWidget *combobox = gtk_combo_box_new_text(); gtk_widget_show(combobox); g_object_set(G_OBJECT(combobox), "has-frame", FALSE, NULL); GList *renderers; if (G_VALUE_HOLDS_BOOLEAN(&gvalue)) { gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), "FALSE"); gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), "TRUE"); gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), g_value_get_boolean(&gvalue) ? 1 : 0); } else if (G_VALUE_HOLDS_ENUM(&gvalue)) { gint value = g_value_get_enum(&gvalue); GEnumClass *enum_class = G_PARAM_SPEC_ENUM(prop)->enum_class; guint i; for (i = 0; i < enum_class->n_values; i++) { GEnumValue *enum_value = &enum_class->values[i]; gtk_combo_box_append_text(GTK_COMBO_BOX(combobox), enum_value->value_name); if (enum_value->value == value) gtk_combo_box_set_active(GTK_COMBO_BOX(combobox), i); } } renderers = gtk_cell_layout_get_cells(GTK_CELL_LAYOUT(combobox)); g_object_set(G_OBJECT(renderers->data), "scale", TREE_TEXT_SCALE, NULL); g_list_free(renderers); editable = GTK_CELL_EDITABLE(combobox); } else if (G_VALUE_HOLDS_STRING(&gvalue)) { GtkWidget *entry = gtk_entry_new(); gtk_widget_show(entry); gtk_entry_set_text(GTK_ENTRY(entry), g_value_get_string(&gvalue)); editable = GTK_CELL_EDITABLE(entry); } else if (G_VALUE_HOLDS_INT(&gvalue) || G_VALUE_HOLDS_UINT(&gvalue) || G_VALUE_HOLDS_INT64(&gvalue) || G_VALUE_HOLDS_UINT64(&gvalue) || G_VALUE_HOLDS_LONG(&gvalue) || G_VALUE_HOLDS_ULONG(&gvalue) || G_VALUE_HOLDS_DOUBLE(&gvalue)) { double min, max, value; GtkWidget *spinbutton; guint digits = 0; if (G_VALUE_HOLDS_INT(&gvalue)) { GParamSpecInt *paramspec = G_PARAM_SPEC_INT(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_int(&gvalue); } else if (G_VALUE_HOLDS_UINT(&gvalue)) { GParamSpecUInt *paramspec = G_PARAM_SPEC_UINT(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_uint(&gvalue); } else if (G_VALUE_HOLDS_INT64(&gvalue)) { GParamSpecInt64 *paramspec = G_PARAM_SPEC_INT64(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_int64(&gvalue); } else if (G_VALUE_HOLDS_UINT64(&gvalue)) { GParamSpecUInt64 *paramspec = G_PARAM_SPEC_UINT64(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_uint64(&gvalue); } else if (G_VALUE_HOLDS_LONG(&gvalue)) { GParamSpecLong *paramspec = G_PARAM_SPEC_LONG(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_long(&gvalue); } else if (G_VALUE_HOLDS_ULONG(&gvalue)) { GParamSpecULong *paramspec = G_PARAM_SPEC_ULONG(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_ulong(&gvalue); } else if (G_VALUE_HOLDS_DOUBLE(&gvalue)) { GParamSpecDouble *paramspec = G_PARAM_SPEC_DOUBLE(prop); min = paramspec->minimum; max = paramspec->maximum; value = g_value_get_double(&gvalue); digits = 2; } else { // Shouldn't really be able to happen. return NULL; } spinbutton = gtk_spin_button_new_with_range(min, max, 1); gtk_widget_show(spinbutton); gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbutton), value); gtk_spin_button_set_digits(GTK_SPIN_BUTTON(spinbutton), digits); editable = GTK_CELL_EDITABLE(spinbutton); } font_desc = pango_font_description_new(); pango_font_description_set_size(font_desc, 8 * PANGO_SCALE); gtk_widget_modify_font(GTK_WIDGET(editable), font_desc); pango_font_description_free(font_desc); g_value_unset(&gvalue); g_signal_connect(G_OBJECT(editable), "editing_done", G_CALLBACK(parasite_property_cell_renderer_stop_editing), renderer); g_object_set_data_full(G_OBJECT(editable), "_prop_name", g_strdup(name), g_free); g_object_set_data(G_OBJECT(editable), "_prop_object", object); return editable; }
static gboolean gst_rdt_depay_setcaps (GstPad * pad, GstCaps * caps) { GstStructure *structure; GstRDTDepay *rdtdepay; GstCaps *srccaps; gint clock_rate = 1000; /* default */ const GValue *value; GstBuffer *header; rdtdepay = GST_RDT_DEPAY (GST_PAD_PARENT (pad)); structure = gst_caps_get_structure (caps, 0); if (gst_structure_has_field (structure, "clock-rate")) gst_structure_get_int (structure, "clock-rate", &clock_rate); /* config contains the RealMedia header as a buffer. */ value = gst_structure_get_value (structure, "config"); if (!value) goto no_header; header = gst_value_get_buffer (value); if (!header) goto no_header; /* get other values for newsegment */ value = gst_structure_get_value (structure, "npt-start"); if (value && G_VALUE_HOLDS_UINT64 (value)) rdtdepay->npt_start = g_value_get_uint64 (value); else rdtdepay->npt_start = 0; GST_DEBUG_OBJECT (rdtdepay, "NPT start %" G_GUINT64_FORMAT, rdtdepay->npt_start); value = gst_structure_get_value (structure, "npt-stop"); if (value && G_VALUE_HOLDS_UINT64 (value)) rdtdepay->npt_stop = g_value_get_uint64 (value); else rdtdepay->npt_stop = -1; GST_DEBUG_OBJECT (rdtdepay, "NPT stop %" G_GUINT64_FORMAT, rdtdepay->npt_stop); value = gst_structure_get_value (structure, "play-speed"); if (value && G_VALUE_HOLDS_DOUBLE (value)) rdtdepay->play_speed = g_value_get_double (value); else rdtdepay->play_speed = 1.0; value = gst_structure_get_value (structure, "play-scale"); if (value && G_VALUE_HOLDS_DOUBLE (value)) rdtdepay->play_scale = g_value_get_double (value); else rdtdepay->play_scale = 1.0; /* caps seem good, configure element */ rdtdepay->clock_rate = clock_rate; /* set caps on pad and on header */ srccaps = gst_caps_new_empty_simple ("application/vnd.rn-realmedia"); gst_pad_set_caps (rdtdepay->srcpad, srccaps); gst_caps_unref (srccaps); if (rdtdepay->header) gst_buffer_unref (rdtdepay->header); rdtdepay->header = gst_buffer_ref (header); return TRUE; /* ERRORS */ no_header: { GST_ERROR_OBJECT (rdtdepay, "no header found in caps, no 'config' field"); return FALSE; } }
static gboolean gst_rtp_base_depayload_setcaps (GstRTPBaseDepayload * filter, GstCaps * caps) { GstRTPBaseDepayloadClass *bclass; GstRTPBaseDepayloadPrivate *priv; gboolean res; GstStructure *caps_struct; const GValue *value; priv = filter->priv; bclass = GST_RTP_BASE_DEPAYLOAD_GET_CLASS (filter); GST_DEBUG_OBJECT (filter, "Set caps %" GST_PTR_FORMAT, caps); if (priv->last_caps) { if (gst_caps_is_equal (priv->last_caps, caps)) { res = TRUE; goto caps_not_changed; } else { gst_caps_unref (priv->last_caps); priv->last_caps = NULL; } } caps_struct = gst_caps_get_structure (caps, 0); /* get other values for newsegment */ value = gst_structure_get_value (caps_struct, "npt-start"); if (value && G_VALUE_HOLDS_UINT64 (value)) priv->npt_start = g_value_get_uint64 (value); else priv->npt_start = 0; GST_DEBUG_OBJECT (filter, "NPT start %" G_GUINT64_FORMAT, priv->npt_start); value = gst_structure_get_value (caps_struct, "npt-stop"); if (value && G_VALUE_HOLDS_UINT64 (value)) priv->npt_stop = g_value_get_uint64 (value); else priv->npt_stop = -1; GST_DEBUG_OBJECT (filter, "NPT stop %" G_GUINT64_FORMAT, priv->npt_stop); value = gst_structure_get_value (caps_struct, "play-speed"); if (value && G_VALUE_HOLDS_DOUBLE (value)) priv->play_speed = g_value_get_double (value); else priv->play_speed = 1.0; value = gst_structure_get_value (caps_struct, "play-scale"); if (value && G_VALUE_HOLDS_DOUBLE (value)) priv->play_scale = g_value_get_double (value); else priv->play_scale = 1.0; value = gst_structure_get_value (caps_struct, "clock-base"); if (value && G_VALUE_HOLDS_UINT (value)) priv->clock_base = g_value_get_uint (value); else priv->clock_base = -1; if (bclass->set_caps) { res = bclass->set_caps (filter, caps); if (!res) { GST_WARNING_OBJECT (filter, "Subclass rejected caps %" GST_PTR_FORMAT, caps); } } else { res = TRUE; } priv->negotiated = res; if (priv->negotiated) priv->last_caps = gst_caps_ref (caps); return res; caps_not_changed: { GST_DEBUG_OBJECT (filter, "Caps did not change"); return res; } }
GVariant * g_settings_set_mapping (const GValue *value, const GVariantType *expected_type, gpointer user_data) { gchar *type_string; if (G_VALUE_HOLDS_BOOLEAN (value)) { if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BOOLEAN)) return g_variant_new_boolean (g_value_get_boolean (value)); } else if (G_VALUE_HOLDS_CHAR (value) || G_VALUE_HOLDS_UCHAR (value)) { if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BYTE)) { if (G_VALUE_HOLDS_CHAR (value)) return g_variant_new_byte (g_value_get_char (value)); else return g_variant_new_byte (g_value_get_uchar (value)); } } else if (G_VALUE_HOLDS_INT (value) || G_VALUE_HOLDS_INT64 (value)) return g_settings_set_mapping_int (value, expected_type); else if (G_VALUE_HOLDS_DOUBLE (value)) return g_settings_set_mapping_float (value, expected_type); else if (G_VALUE_HOLDS_UINT (value) || G_VALUE_HOLDS_UINT64 (value)) return g_settings_set_mapping_unsigned_int (value, expected_type); else if (G_VALUE_HOLDS_STRING (value)) { if (g_value_get_string (value) == NULL) return NULL; else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_STRING)) return g_variant_new_string (g_value_get_string (value)); else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_BYTESTRING)) return g_variant_new_bytestring (g_value_get_string (value)); else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_OBJECT_PATH)) return g_variant_new_object_path (g_value_get_string (value)); else if (g_variant_type_equal (expected_type, G_VARIANT_TYPE_SIGNATURE)) return g_variant_new_signature (g_value_get_string (value)); } else if (G_VALUE_HOLDS (value, G_TYPE_STRV)) { if (g_value_get_boxed (value) == NULL) return NULL; return g_variant_new_strv ((const gchar **) g_value_get_boxed (value), -1); } else if (G_VALUE_HOLDS_ENUM (value)) { GEnumValue *enumval; GEnumClass *eclass; /* GParamSpecEnum holds a ref on the class so we just peek... */ eclass = g_type_class_peek (G_VALUE_TYPE (value)); enumval = g_enum_get_value (eclass, g_value_get_enum (value)); if (enumval) return g_variant_new_string (enumval->value_nick); else return NULL; } else if (G_VALUE_HOLDS_FLAGS (value)) { GVariantBuilder builder; GFlagsValue *flagsval; GFlagsClass *fclass; guint flags; fclass = g_type_class_peek (G_VALUE_TYPE (value)); flags = g_value_get_flags (value); g_variant_builder_init (&builder, G_VARIANT_TYPE ("as")); while (flags) { flagsval = g_flags_get_first_value (fclass, flags); if (flagsval == NULL) { g_variant_builder_clear (&builder); return NULL; } g_variant_builder_add (&builder, "s", flagsval->value_nick); flags &= ~flagsval->value; } return g_variant_builder_end (&builder); } type_string = g_variant_type_dup_string (expected_type); g_critical ("No GSettings bind handler for type \"%s\".", type_string); g_free (type_string); return NULL; }
int bt_obex(int argc, char *argv[]) { GError *error = NULL; GOptionContext *context; /* Query current locale */ setlocale(LC_CTYPE, ""); g_type_init(); dbus_init(); context = g_option_context_new(" - a bluetooth OBEX client/server"); g_option_context_add_main_entries(context, entries, NULL); g_option_context_set_summary(context, "Version "PACKAGE_VERSION); g_option_context_set_description(context, "Server Options:\n" " -s, --server [<path>]\n" " Register self at OBEX server and use given `path` as OPP save directory\n" " If `path` does not specified - use current directory\n\n" "OPP Options:\n" " -p, --opp <name|mac> <file>\n" " Send `file` to remote device using Object Push Profile\n\n" //"Report bugs to <"PACKAGE_BUGREPORT">." "Project home page <"PACKAGE_URL">." ); if (!g_option_context_parse(context, &argc, &argv, &error)) { g_print("%s: %s\n", g_get_prgname(), error->message); g_print("Try `%s --help` for more information.\n", g_get_prgname()); exit(EXIT_FAILURE); } else if (!server_arg && !opp_arg && (!ftp_arg || strlen(ftp_arg) == 0)) { g_print("%s", g_option_context_get_help(context, FALSE, NULL)); exit(EXIT_FAILURE); } else if (server_arg && argc != 1 && (argc != 2 || strlen(argv[1]) == 0)) { g_print("%s: Invalid arguments for --server\n", g_get_prgname()); g_print("Try `%s --help` for more information.\n", g_get_prgname()); exit(EXIT_FAILURE); } else if (opp_arg && (argc != 3 || strlen(argv[1]) == 0 || strlen(argv[2]) == 0)) { g_print("%s: Invalid arguments for --opp\n", g_get_prgname()); g_print("Try `%s --help` for more information.\n", g_get_prgname()); exit(EXIT_FAILURE); } g_option_context_free(context); if (!dbus_system_connect(&error)) { g_printerr("Couldn't connect to DBus system bus: %s\n", error->message); exit(EXIT_FAILURE); } if (!dbus_session_connect(&error)) { g_printerr("Couldn't connect to DBus session bus: %s\n", error->message); exit(EXIT_FAILURE); } /* Check, that bluetooth daemon is running */ if (!intf_supported(BLUEZ_DBUS_NAME, MANAGER_DBUS_PATH, MANAGER_DBUS_INTERFACE)) { g_printerr("%s: bluez service is not found\n", g_get_prgname()); g_printerr("Did you forget to run bluetoothd?\n"); exit(EXIT_FAILURE); } /* Check, that obexd daemon is running */ if (!intf_supported(OBEXS_DBUS_NAME, OBEXMANAGER_DBUS_PATH, OBEXMANAGER_DBUS_INTERFACE)) { g_printerr("%s: obex service is not found\n", g_get_prgname()); g_printerr("Did you forget to run obexd?\n"); exit(EXIT_FAILURE); } if (server_arg) { if (argc == 2) { server_path_arg = argv[1]; } /* Check that `path` is valid */ gchar *root_folder = server_path_arg == NULL ? g_get_current_dir() : g_strdup(server_path_arg); if (!is_dir(root_folder, &error)) { exit_if_error(error); } server_transfers = g_hash_table_new(g_str_hash, g_str_equal); OBEXManager *manager = g_object_new(OBEXMANAGER_TYPE, NULL); g_signal_connect(manager, "SessionCreated", G_CALLBACK(obexmanager_session_created), NULL); g_signal_connect(manager, "SessionRemoved", G_CALLBACK(obexmanager_session_removed), NULL); g_signal_connect(manager, "TransferStarted", G_CALLBACK(obexmanager_transfer_started), NULL); g_signal_connect(manager, "TransferCompleted", G_CALLBACK(obexmanager_transfer_completed), NULL); OBEXAgent *agent = g_object_new(OBEXAGENT_TYPE, "RootFolder", root_folder, NULL); g_free(root_folder); obexmanager_register_agent(manager, OBEXAGENT_DBUS_PATH, &error); exit_if_error(error); mainloop = g_main_loop_new(NULL, FALSE); /* Add SIGTERM && SIGINT handlers */ struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = sigterm_handler; sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); g_main_loop_run(mainloop); /* Waiting for connections... */ g_main_loop_unref(mainloop); /* Stop active transfers */ GHashTableIter iter; gpointer key, value; g_hash_table_iter_init(&iter, server_transfers); while (g_hash_table_iter_next(&iter, &key, &value)) { OBEXTransfer *t = OBEXTRANSFER(value); obextransfer_cancel(t, NULL); // skip errors g_object_unref(t); g_hash_table_iter_remove(&iter); } g_hash_table_unref(server_transfers); obexmanager_unregister_agent(manager, OBEXAGENT_DBUS_PATH, &error); g_object_unref(agent); g_object_unref(manager); } else if (opp_arg) { opp_device_arg = argv[1]; opp_file_arg = argv[2]; /* Check that `file` is valid */ if (!is_file(opp_file_arg, &error)) { exit_if_error(error); } gchar * files_to_send[] = {NULL, NULL}; files_to_send[0] = g_path_is_absolute(opp_file_arg) ? g_strdup(opp_file_arg) : get_absolute_path(opp_file_arg); /* Get source address (address of adapter) */ Adapter *adapter = find_adapter(adapter_arg, &error); exit_if_error(error); gchar *src_address = g_strdup(adapter_get_address(adapter)); /* Get destination address (address of remote device) */ gchar *dst_address = NULL; if (g_regex_match_simple("^\\x{2}:\\x{2}:\\x{2}:\\x{2}:\\x{2}:\\x{2}$", opp_device_arg, 0, 0)) { dst_address = g_strdup(opp_device_arg); } else { Device *device = find_device(adapter, opp_device_arg, &error); exit_if_error(error); dst_address = g_strdup(device_get_address(device)); g_object_unref(device); } g_object_unref(adapter); /* Build arguments */ GHashTable *device_dict = g_hash_table_new(g_str_hash, g_str_equal); GValue src_v = {0}; GValue dst_v = {0}; g_value_init(&src_v, G_TYPE_STRING); g_value_init(&dst_v, G_TYPE_STRING); g_value_set_string(&src_v, src_address); g_value_set_string(&dst_v, dst_address); g_hash_table_insert(device_dict, "Source", &src_v); g_hash_table_insert(device_dict, "Destination", &dst_v); mainloop = g_main_loop_new(NULL, FALSE); OBEXClient *client = g_object_new(OBEXCLIENT_TYPE, NULL); OBEXAgent *agent = g_object_new(OBEXAGENT_TYPE, NULL); g_signal_connect(agent, "AgentReleased", G_CALLBACK(agent_released), mainloop); /* Sending file(s) */ obexclient_send_files(client, device_dict, files_to_send, OBEXAGENT_DBUS_PATH, &error); exit_if_error(error); /* Add SIGTERM && SIGINT handlers */ struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = sigterm_handler; sigaction(SIGTERM, &sa, NULL); sigaction(SIGINT, &sa, NULL); g_main_loop_run(mainloop); /* Sending files process here ?? */ g_main_loop_unref(mainloop); g_object_unref(agent); g_object_unref(client); g_value_unset(&src_v); g_value_unset(&dst_v); g_hash_table_unref(device_dict); g_free(src_address); g_free(dst_address); g_free(files_to_send[0]); files_to_send[0] = NULL; } else if (ftp_arg) { /* Get source address (address of adapter) */ Adapter *adapter = find_adapter(adapter_arg, &error); exit_if_error(error); gchar *src_address = g_strdup(adapter_get_address(adapter)); /* Get destination address (address of remote device) */ Device *device = find_device(adapter, ftp_arg, &error); exit_if_error(error); gchar *dst_address = g_strdup(device == NULL ? ftp_arg : device_get_address(device)); g_object_unref(device); g_object_unref(adapter); /* Build arguments */ GHashTable *device_dict = g_hash_table_new(g_str_hash, g_str_equal); GValue src_v = {0}; GValue dst_v = {0}; GValue target_v = {0}; g_value_init(&src_v, G_TYPE_STRING); g_value_init(&dst_v, G_TYPE_STRING); g_value_init(&target_v, G_TYPE_STRING); g_value_set_string(&src_v, src_address); g_value_set_string(&dst_v, dst_address); g_value_set_string(&target_v, "FTP"); g_hash_table_insert(device_dict, "Source", &src_v); g_hash_table_insert(device_dict, "Destination", &dst_v); g_hash_table_insert(device_dict, "Target", &target_v); OBEXClient *client = g_object_new(OBEXCLIENT_TYPE, NULL); OBEXAgent *agent = g_object_new(OBEXAGENT_TYPE, NULL); /* Create FTP session */ gchar *session_path = obexclient_create_session(client, device_dict, &error); exit_if_error(error); OBEXClientFileTransfer *ftp_session = g_object_new(OBEXCLIENT_FILE_TRANSFER_TYPE, "DBusObjectPath", session_path, NULL); g_free(session_path); g_print("FTP session opened\n"); while (TRUE) { gchar *cmd; /* cmd = readline("> "); if (cmd == NULL) { continue; } else { add_history(cmd); } */ gint f_argc; gchar **f_argv; /* Parsing command line */ if (!g_shell_parse_argv(cmd, &f_argc, &f_argv, &error)) { g_print("%s\n", error->message); g_error_free(error); error = NULL; g_free(cmd); continue; } /* Execute commands */ if (g_strcmp0(f_argv[0], "cd") == 0) { if (f_argc != 2 || strlen(f_argv[1]) == 0) { g_print("invalid arguments\n"); } else { obexclient_file_transfer_change_folder(ftp_session, f_argv[1], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } } else if (g_strcmp0(f_argv[0], "mkdir") == 0) { if (f_argc != 2 || strlen(f_argv[1]) == 0) { g_print("invalid arguments\n"); } else { obexclient_file_transfer_create_folder(ftp_session, f_argv[1], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } } else if (g_strcmp0(f_argv[0], "ls") == 0) { if (f_argc != 1) { g_print("invalid arguments\n"); } else { GPtrArray *folders = obexclient_file_transfer_list_folder(ftp_session, &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } else { for (int i = 0; i < folders->len; i++) { GHashTable *el = g_ptr_array_index(folders, i); g_print( "%s\t%llu\t%s\n", g_value_get_string(g_hash_table_lookup(el, "Type")), G_VALUE_HOLDS_UINT64(g_hash_table_lookup(el, "Size")) ? g_value_get_uint64(g_hash_table_lookup(el, "Size")) : 0, g_value_get_string(g_hash_table_lookup(el, "Name")) ); } } if (folders) g_ptr_array_unref(folders); /*obexclient_remove_session(client, obexclient_file_transfer_get_dbus_object_path(ftp_session), &error); exit_if_error(error); g_object_unref(ftp_session); session_path = obexclient_create_session(client, device_dict, &error); exit_if_error(error); ftp_session = g_object_new(OBEXCLIENT_FILE_TRANSFER_TYPE, "DBusObjectPath", session_path, NULL); g_free(session_path);*/ } } else if (g_strcmp0(f_argv[0], "get") == 0) { if (f_argc != 3 || strlen(f_argv[1]) == 0 || strlen(f_argv[2]) == 0) { g_print("invalid arguments\n"); } else { gchar *abs_dst_path = get_absolute_path(f_argv[2]); gchar *dir = g_path_get_dirname(abs_dst_path); if (!is_dir(dir, &error)) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } else { obexclient_file_transfer_get_file(ftp_session, abs_dst_path, f_argv[1], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } g_free(dir); g_free(abs_dst_path); } } else if (g_strcmp0(f_argv[0], "put") == 0) { if (f_argc != 3 || strlen(f_argv[1]) == 0 || strlen(f_argv[2]) == 0) { g_print("invalid arguments\n"); } else { gchar *abs_src_path = get_absolute_path(f_argv[1]); if (!is_file(abs_src_path, &error)) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } else { obexclient_file_transfer_put_file(ftp_session, abs_src_path, f_argv[2], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } g_free(abs_src_path); } } else if (g_strcmp0(f_argv[0], "cp") == 0) { if (f_argc != 3 || strlen(f_argv[1]) == 0 || strlen(f_argv[2]) == 0) { g_print("invalid arguments\n"); } else { obexclient_file_transfer_copy_file(ftp_session, f_argv[1], f_argv[2], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } } else if (g_strcmp0(f_argv[0], "mv") == 0) { if (f_argc != 3 || strlen(f_argv[1]) == 0 || strlen(f_argv[2]) == 0) { g_print("invalid arguments\n"); } else { obexclient_file_transfer_move_file(ftp_session, f_argv[1], f_argv[2], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } } else if (g_strcmp0(f_argv[0], "rm") == 0) { if (f_argc != 2 || strlen(f_argv[1]) == 0) { g_print("invalid arguments\n"); } else { obexclient_file_transfer_delete(ftp_session, f_argv[1], &error); if (error) { g_print("%s\n", error->message); g_error_free(error); error = NULL; } } } else if (g_strcmp0(f_argv[0], "help") == 0) { g_print( "help\t\t\tShow this message\n" "exit\t\t\tClose FTP session\n" "cd <folder>\t\tChange the current folder of the remote device\n" "mkdir <folder>\t\tCreate a new folder in the remote device\n" "ls\t\t\tList folder contents\n" "get <src> <dst>\t\tCopy the src file (from remote device) to the dst file (on local filesystem)\n" "put <src> <dst>\t\tCopy the src file (from local filesystem) to the dst file (on remote device)\n" "cp <src> <dst>\t\tCopy a file within the remote device from src file to dst file\n" "mv <src> <dst>\t\tMove a file within the remote device from src file to dst file\n" "rm <target>\t\tDeletes the specified file/folder\n" ); } else if (g_strcmp0(f_argv[0], "exit") == 0 || g_strcmp0(f_argv[0], "quit") == 0) { obexclient_remove_session(client, obexclient_file_transfer_get_dbus_object_path(ftp_session), &error); exit_if_error(error); g_strfreev(f_argv); g_free(cmd); break; } else { g_print("invalid command\n"); } g_strfreev(f_argv); g_free(cmd); } g_object_unref(agent); g_object_unref(client); g_object_unref(ftp_session); g_value_unset(&src_v); g_value_unset(&dst_v); g_value_unset(&target_v); g_hash_table_unref(device_dict); g_free(src_address); g_free(dst_address); } dbus_disconnect(); exit(EXIT_SUCCESS); }