Esempio n. 1
0
static void normalize_path (char * path)
{
#ifdef _WIN32
    string_replace_char (path, '/', '\\');
#endif
    int len = strlen (path);
#ifdef _WIN32
    if (len > 3 && path[len - 1] == '\\') /* leave "C:\" */
#else
    if (len > 1 && path[len - 1] == '/') /* leave leading "/" */
#endif
        path[len - 1] = 0;
}
Esempio n. 2
0
EXPORT char * uri_to_display (const char * uri)
{
    if (! strncmp (uri, "cdda://?", 8))
        return g_strdup_printf (_("Audio CD, track %s"), uri + 8);

    char buf[strlen (uri) + 1];

#ifdef _WIN32
    if (! strncmp (uri, "file:///", 8))
    {
        str_decode_percent (uri + 8, -1, buf);
        string_replace_char (buf, '/', '\\');
    }
#else
    if (! strncmp (uri, "file://", 7))
        str_decode_percent (uri + 7, -1, buf);
#endif
    else
        str_decode_percent (uri, -1, buf);

    return g_strdup (buf);
}
Esempio n. 3
0
char * construct_uri (const char * string, const char * playlist_name)
{
    /* URI */
    if (strstr (string, "://"))
        return strdup (string);

    /* absolute filename (assumed UTF-8) */
#ifdef _WIN32
    if (string[0] && string[1] == ':' && string[2] == '\\')
#else
    if (string[0] == '/')
#endif
        return filename_to_uri (string);

    /* relative filename (assumed UTF-8) */
    const char * slash = strrchr (playlist_name, '/');
    if (! slash)
        return NULL;

    int pathlen = slash + 1 - playlist_name;
    int rellen = strlen (string);

    char buf[pathlen + 3 * rellen + 1];
    memcpy (buf, playlist_name, pathlen);

    if (get_bool (NULL, "convert_backslash"))
    {
        char tmp[rellen + 1];
        strcpy (tmp, string);
        string_replace_char (tmp, '\\', '/');
        str_encode_percent (tmp, -1, buf + pathlen);
    }
    else
        str_encode_percent (string, -1, buf + pathlen);

    return strdup (buf);
}
Esempio n. 4
0
EXPORT char * uri_to_filename (const char * uri)
{
#ifdef _WIN32
    g_return_val_if_fail (! strncmp (uri, "file:///", 8), NULL);
    char buf[strlen (uri + 8) + 1];
    str_decode_percent (uri + 8, -1, buf);
#else
    g_return_val_if_fail (! strncmp (uri, "file://", 7), NULL);
    char buf[strlen (uri + 7) + 1];
    str_decode_percent (uri + 7, -1, buf);
#endif
#ifdef _WIN32
    string_replace_char (buf, '/', '\\');
#endif

    char * name = g_locale_from_utf8 (buf, -1, NULL, NULL, NULL);
    if (! name)
    {
        const char * locale = setlocale (LC_ALL, NULL);
        fprintf (stderr, "Cannot convert filename to system locale (%s): %s\n", locale, buf);
    }

    return name;
}
Esempio n. 5
0
EXPORT char * filename_to_uri (const char * name)
{
    char * utf8 = g_locale_to_utf8 (name, -1, NULL, NULL, NULL);
    if (! utf8)
    {
        const char * locale = setlocale (LC_ALL, NULL);
        fprintf (stderr, "Cannot convert filename from system locale (%s): %s\n", locale, name);
        return NULL;
    }

#ifdef _WIN32
    string_replace_char (utf8, '\\', '/');
#endif
    char enc[3 * strlen (utf8) + 1];
    str_encode_percent (utf8, -1, enc);

    g_free (utf8);

#ifdef _WIN32
    return g_strdup_printf ("file:///%s", enc);
#else
    return g_strdup_printf ("file://%s", enc);
#endif
}
Esempio n. 6
0
/**
 * Swap one apartment (unique) map for another.
 * @param mapold
 * Old map path.
 * @param mapnew
 * Map to switch for.
 * @param x
 * X position where player's items from old map will go to.
 * @param y
 * Y position where player's items from old map will go to.
 * @param op
 * Player we're doing the switching for.
 * @return
 * 1 on success, 0 on failure.
 */
int swap_apartments(const char *mapold, const char *mapnew, int x, int y, object *op)
{
    char *cleanpath, *path;
    int i, j;
    object *ob, *tmp, *tmp2;
    mapstruct *oldmap, *newmap;

    /* So we can transfer our items from the old apartment. */
    cleanpath = estrdup(mapold);
    string_replace_char(cleanpath, "/", '$');
    path = player_make_path(op->name, cleanpath);
    oldmap = ready_map_name(path, NULL, MAP_PLAYER_UNIQUE);
    efree(path);
    efree(cleanpath);

    if (!oldmap) {
        LOG(BUG, "Could not get oldmap using ready_map_name().");
        return 0;
    }

    /* Our new map. */
    cleanpath = estrdup(mapnew);
    string_replace_char(cleanpath, "/", '$');
    path = player_make_path(op->name, cleanpath);
    newmap = ready_map_name(path, NULL, MAP_PLAYER_UNIQUE);
    efree(path);
    efree(cleanpath);

    if (!newmap) {
        LOG(BUG, "Could not get newmap using ready_map_name().");
        return 0;
    }

    /* Go through every square on old apartment map, looking for things
     * to transfer. */
    for (i = 0; i < MAP_WIDTH(oldmap); i++) {
        for (j = 0; j < MAP_HEIGHT(oldmap); j++) {
            for (ob = GET_MAP_OB(oldmap, i, j); ob; ob = tmp2) {
                tmp2 = ob->above;

                /* We teleport any possible players here to emergency map. */
                if (ob->type == PLAYER) {
                    object_enter_map(ob, NULL, NULL, 0, 0, false);
                    continue;
                }

                /* If it's sys_object 1, there's no need to transfer it. */
                if (QUERY_FLAG(ob, FLAG_SYS_OBJECT)) {
                    continue;
                }

                /* A pickable item... Tranfer it */
                if (!QUERY_FLAG(ob, FLAG_NO_PICK)) {
                    object_remove(ob, 0);
                    ob->x = x;
                    ob->y = y;
                    object_insert_map(ob, newmap, NULL, INS_NO_MERGE | INS_NO_WALK_ON);
                } else {
                    /* Fixed part of map */

                    /* Now we test for containers, because player
                     * can have items stored in it. So, go through
                     * the container and look for things to transfer. */
                    for (tmp = ob->inv; tmp; tmp = tmp2) {
                        tmp2 = tmp->below;

                        if (QUERY_FLAG(tmp, FLAG_SYS_OBJECT) || QUERY_FLAG(tmp, FLAG_NO_PICK)) {
                            continue;
                        }

                        object_remove(tmp, 0);
                        tmp->x = x;
                        tmp->y = y;
                        object_insert_map(tmp, newmap, NULL, INS_NO_MERGE | INS_NO_WALK_ON);
                    }
                }
            }
        }
    }

    /* Save the map */
    new_save_map(newmap, 0);

    /* Check for old save bed */
    if (strcmp(oldmap->path, CONTR(op)->savebed_map) == 0) {
        strcpy(CONTR(op)->savebed_map, EMERGENCY_MAPPATH);
        CONTR(op)->bed_x = EMERGENCY_X;
        CONTR(op)->bed_y = EMERGENCY_Y;
    }

    unlink(oldmap->path);

    /* Free the maps */
    free_map(newmap, 1);
    free_map(oldmap, 1);

    return 1;
}
Esempio n. 7
0
/*
 * Initialize the given notification and add it to
 * the queue. Replace notification with id if id > 0.
 */
int notification_init(notification * n, int id)
{

    if (n == NULL)
        return -1;

    if (strcmp("DUNST_COMMAND_PAUSE", n->summary) == 0) {
        pause_display = true;
        return 0;
    }

    if (strcmp("DUNST_COMMAND_RESUME", n->summary) == 0) {
        pause_display = false;
        return 0;
    }

    n->script = NULL;
    n->text_to_render = NULL;

    n->format = settings.format;

    rule_apply_all(n);

    n->urls = notification_extract_markup_urls(&(n->body));

    n->msg = string_replace("%a", n->appname, g_strdup(n->format));
    n->msg = string_replace("%s", n->summary, n->msg);
    if (n->icon) {
        n->msg = string_replace("%I", basename(n->icon), n->msg);
        n->msg = string_replace("%i", n->icon, n->msg);
    }
    n->msg = string_replace("%b", n->body, n->msg);
    if (n->progress) {
        char pg[10];
        sprintf(pg, "[%3d%%]", n->progress - 1);
        n->msg = string_replace("%p", pg, n->msg);
    } else {
        n->msg = string_replace("%p", "", n->msg);
    }

    if (!settings.allow_markup)
        n->msg = notification_fix_markup(n->msg);
    else if (!settings.ignore_newline) {
        n->msg = string_replace("<br>", "\n", n->msg);
        n->msg = string_replace("<br />", "\n", n->msg);
    }

    while (strstr(n->msg, "\\n") != NULL)
        n->msg = string_replace("\\n", "\n", n->msg);

    if (settings.ignore_newline)
        while (strstr(n->msg, "\n") != NULL)
            n->msg = string_replace("\n", " ", n->msg);

    n->msg = g_strstrip(n->msg);

    n->dup_count = 0;

    /* check if n is a duplicate */
    if (settings.stack_duplicates) {
        for (GList * iter = g_queue_peek_head_link(queue); iter;
                iter = iter->next) {
            notification *orig = iter->data;
            if (strcmp(orig->appname, n->appname) == 0
                    && strcmp(orig->summary, n->summary) == 0
                    && strcmp(orig->body, n->body) == 0) {
                /* If the progress differs this was probably intended to replace the notification
                 * but notify-send was used. So don't increment dup_count in this case
                 */
                if (orig->progress == n->progress) {
                    orig->dup_count++;
                } else {
                    orig->progress = n->progress;
                }
                /* notifications that differ only in progress hints should be expected equal,
                 * but we want the latest message, with the latest hint value
                 */
                free(orig->msg);
                orig->msg = strdup(n->msg);
                notification_free(n);
                wake_up();
                return orig->id;
            }
        }

        for (GList * iter = g_queue_peek_head_link(displayed); iter;
                iter = iter->next) {
            notification *orig = iter->data;
            if (strcmp(orig->appname, n->appname) == 0
                    && strcmp(orig->summary, n->summary) == 0
                    && strcmp(orig->body, n->body) == 0) {
                /* notifications that differ only in progress hints should be expected equal,
                 * but we want the latest message, with the latest hint value
                 */
                free(orig->msg);
                orig->msg = strdup(n->msg);
                /* If the progress differs this was probably intended to replace the notification
                 * but notify-send was used. So don't increment dup_count in this case
                 */
                if (orig->progress == n->progress) {
                    orig->dup_count++;
                } else {
                    orig->progress = n->progress;
                }
                orig->start = time(NULL);
                notification_free(n);
                wake_up();
                return orig->id;
            }
        }
    }

    /* urgency > CRIT -> array out of range */
    n->urgency = n->urgency > CRIT ? CRIT : n->urgency;

    if (!n->color_strings[ColFG]) {
        n->color_strings[ColFG] = xctx.color_strings[ColFG][n->urgency];
    }

    if (!n->color_strings[ColBG]) {
        n->color_strings[ColBG] = xctx.color_strings[ColBG][n->urgency];
    }

    n->timeout =
        n->timeout == -1 ? settings.timeouts[n->urgency] : n->timeout;
    n->start = 0;

    if (n->icon == NULL) {
        n->icon = settings.icons[n->urgency];
    }
    else if (strlen(n->icon) <= 0) {
        free(n->icon);
        n->icon = settings.icons[n->urgency];
    }

    if (n->category == NULL) {
        n->category = "";
    }

    n->timestamp = time(NULL);

    n->redisplayed = false;

    n->first_render = true;

    if (id == 0) {
        n->id = ++next_notification_id;
    } else {
        notification_close_by_id(id, -1);
        n->id = id;
    }

    if (strlen(n->msg) == 0) {
        notification_close(n, 2);
        printf("skipping notification: %s %s\n", n->body, n->summary);
    } else {
        g_queue_insert_sorted(queue, n, notification_cmp_data, NULL);
    }

    char *tmp = g_strconcat(n->summary, " ", n->body, NULL);

    char *tmp_urls = extract_urls(tmp);
    if (tmp_urls != NULL) {
        if (n->urls != NULL) {
            n->urls = string_append(n->urls, tmp_urls, "\n");
            free(tmp_urls);
        } else {
            n->urls = tmp_urls;
        }
    }

    if (n->actions) {
        n->actions->dmenu_str = NULL;
        for (int i = 0; i < n->actions->count; i += 2) {
            char *human_readable = n->actions->actions[i + 1];
            string_replace_char('[', '(', human_readable); // kill square brackets
            string_replace_char(']', ')', human_readable);

            n->actions->dmenu_str =
                string_append(n->actions->dmenu_str,
                              g_strdup_printf("#%s [%s]", human_readable,
                                              n->appname),
                              "\n");
        }
    }

    free(tmp);

    if (settings.print_notifications)
        notification_print(n);

    return n->id;
}
Esempio n. 8
0
static gint file_open(gint fmt, gint rate, gint nch)
{
    gchar *filename = NULL, *temp = NULL;
    gchar * directory;
    gint pos;
    gint rv;
    gint playlist;

    input.format = fmt;
    input.frequency = rate;
    input.channels = nch;

    playlist = aud_playlist_get_playing ();
    if (playlist < 0)
        return 0;

    pos = aud_playlist_get_position(playlist);
    tuple = aud_playlist_entry_get_tuple (playlist, pos, FALSE);
    if (tuple == NULL)
        return 0;

    if (filenamefromtags)
    {
        gchar * utf8 = aud_playlist_entry_get_title (playlist, pos, FALSE);
        string_replace_char (utf8, '/', ' ');

        gchar buf[3 * strlen (utf8) + 1];
        str_encode_percent (utf8, -1, buf);
        str_unref (utf8);

        filename = g_strdup (buf);
    }
    else
    {
        temp = aud_playlist_entry_get_filename (playlist, pos);
        gchar * original = strrchr (temp, '/');
        g_return_val_if_fail (original != NULL, 0);
        filename = g_strdup (original + 1);
        str_unref (temp);

        if (!use_suffix)
            if ((temp = strrchr(filename, '.')) != NULL)
                *temp = '\0';
    }

    if (prependnumber)
    {
        gint number = tuple_get_int(tuple, FIELD_TRACK_NUMBER, NULL);
        if (!tuple || !number)
            number = pos + 1;

        temp = g_strdup_printf ("%d%%20%s", number, filename);
        g_free(filename);
        filename = temp;
    }

    if (save_original)
    {
        temp = aud_playlist_entry_get_filename (playlist, pos);
        directory = g_strdup (temp);
        str_unref (temp);
        temp = strrchr (directory, '/');
        g_return_val_if_fail (temp != NULL, 0);
        temp[1] = 0;
    }
    else
    {
        g_return_val_if_fail (file_path[0], 0);
        if (file_path[strlen (file_path) - 1] == '/')
            directory = g_strdup (file_path);
        else
            directory = g_strdup_printf ("%s/", file_path);
    }

    temp = g_strdup_printf ("%s%s.%s", directory, filename, fileext_str[fileext]);
    g_free (directory);
    g_free (filename);
    filename = temp;

    output_file = safe_create (filename);
    g_free (filename);

    if (output_file == NULL)
        return 0;

    convert_init (fmt, plugin->format_required (fmt), nch);

    rv = (plugin->open)();

    samples_written = 0;

    return rv;
}