void
mmcli_modem_messaging_run_synchronous (GDBusConnection *connection)
{
    GError *error = NULL;

    /* Initialize context */
    ctx = g_new0 (Context, 1);
    ctx->object = mmcli_get_modem_sync (connection,
                                        mmcli_get_common_modem_string (),
                                        &ctx->manager);
    ctx->modem_messaging = mm_object_get_modem_messaging (ctx->object);

    /* Setup operation timeout */
    if (ctx->modem_messaging)
        mmcli_force_operation_timeout (G_DBUS_PROXY (ctx->modem_messaging));

    ensure_modem_messaging ();

    /* Request to get location status? */
    if (status_flag) {
        g_debug ("Printing messaging status...");
        print_messaging_status ();
        return;
    }

    /* Request to list the SMS? */
    if (list_flag) {
        GList *result;

        g_debug ("Synchronously listing SMS messages...");
        result = mm_modem_messaging_list_sync (ctx->modem_messaging, NULL, &error);
        list_process_reply (result, error);
        return;
    }

    /* Request to create a new SMS? */
    if (create_str) {
        MMSms *sms;
        GError *error = NULL;
        MMSmsProperties *properties;

        properties = build_sms_properties_from_input (create_str,
                                                      create_with_data_str);
        g_debug ("Synchronously creating new SMS in modem...");
        sms = mm_modem_messaging_create_sync (ctx->modem_messaging,
                                              properties,
                                              NULL,
                                              &error);
        g_object_unref (properties);

        create_process_reply (sms, error);
        return;
    }

    /* Request to delete a given SMS? */
    if (delete_str) {
        gboolean result;
        MMSms *sms;
        MMObject *obj = NULL;

        sms = mmcli_get_sms_sync (connection,
                                  delete_str,
                                  NULL,
                                  &obj);
        if (!g_str_equal (mm_object_get_path (obj), mm_modem_messaging_get_path (ctx->modem_messaging))) {
            g_printerr ("error: SMS '%s' not owned by modem '%s'",
                        mm_sms_get_path (sms),
                        mm_modem_messaging_get_path (ctx->modem_messaging));
            exit (EXIT_FAILURE);
        }

        result = mm_modem_messaging_delete_sync (ctx->modem_messaging,
                                                 mm_sms_get_path (sms),
                                                 NULL,
                                                 &error);
        g_object_unref (sms);
        g_object_unref (obj);

        delete_process_reply (result, error);
        return;
    }

    g_warn_if_reached ();
}
Ejemplo n.º 2
0
MMSms *
mmcli_get_sms_sync (GDBusConnection *connection,
                    const gchar *path_or_index,
                    MMManager **o_manager,
                    MMObject **o_object)
{
    MMManager *manager;
    GList *modems;
    GList *l;
    MMSms *found = NULL;
    gchar *sms_path;

    sms_path = get_sms_path (path_or_index);

    manager = mmcli_get_manager_sync (connection);
    modems = g_dbus_object_manager_get_objects (G_DBUS_OBJECT_MANAGER (manager));
    if (!modems) {
        g_printerr ("error: couldn't find sms at '%s': 'no modems found'\n",
                    sms_path);
        exit (EXIT_FAILURE);
    }

    for (l = modems; !found && l; l = g_list_next (l)) {
        GError *error = NULL;
        MMObject *object;
        MMModemMessaging *modem;
        GList *sms_list;

        object = MM_OBJECT (l->data);
        modem = mm_object_get_modem_messaging (object);

        /* If this modem doesn't implement messaging, continue to next one */
        if (!modem)
            continue;

        sms_list = mm_modem_messaging_list_sync (modem, NULL, &error);
        if (error) {
            g_printerr ("error: couldn't list SMS at '%s': '%s'\n",
                        mm_modem_messaging_get_path (modem),
                        error->message);
            exit (EXIT_FAILURE);
        }

        found = find_sms_in_list (sms_list, sms_path);
        g_list_free_full (sms_list, (GDestroyNotify) g_object_unref);

        if (found && o_object)
            *o_object = g_object_ref (object);

        g_object_unref (modem);
    }

    if (!found) {
        g_printerr ("error: couldn't find SMS at '%s': 'not found in any modem'\n",
                    sms_path);
        exit (EXIT_FAILURE);
    }

    g_list_free_full (modems, (GDestroyNotify) g_object_unref);
    g_free (sms_path);

    if (o_manager)
        *o_manager = manager;
    else
        g_object_unref (manager);

    return found;
}