Example #1
0
static gchar *get_file_contents(const gchar *filename, gsize *len)
{
    gchar *content = NULL;
    GError *file_error = NULL;
    if(filename != NULL) {
        if(g_file_get_contents(filename, &content, len, &file_error) == FALSE) {
            glyr_message(-1, NULL, "Error while reading file '%s': %s\n", filename, file_error->message);
            g_error_free(file_error);
        }
    }
    return content;
}
Example #2
0
File: generic.c Project: meh/glyr
static GList * async_dl_callback(cb_object * capo, void * userptr, bool * stop_download, gint * add_item)
{
	if(capo->cache != NULL && userptr != NULL)
	{
		/* Sanitize this */
		struct callback_save_struct * saver = userptr;
		GHashTable * prov_url_table = saver->table;
		capo->cache->is_image = true;

		if(prov_url_table != NULL)
		{
			GlyrMemCache * old_cache = g_hash_table_lookup(prov_url_table,capo->cache->dsrc);
			GLYR_ERROR response = GLYRE_OK;
			if(old_cache != NULL)
			{
				update_md5sum(capo->cache);
				if(is_in_result_list(capo->cache,saver->results) == FALSE)
				{
					capo->cache->prov       = (old_cache->prov!=NULL) ? g_strdup(old_cache->prov) : NULL;
					capo->cache->img_format = (old_cache->img_format) ? g_strdup(old_cache->img_format) : NULL;

					if(capo->cache->type == GLYR_TYPE_NOIDEA)
					{
						capo->cache->type = saver->type;
					}

					if(capo->s->callback.download != NULL)
					{
						response = capo->s->callback.download(capo->cache,capo->s);
					}

					*add_item = (response != GLYRE_SKIP && response != GLYRE_STOP_PRE);
				}
				else
				{
					capo->s->itemctr--;
					*add_item = FALSE;
				}
			}

			if(response == GLYRE_STOP_POST || response == GLYRE_STOP_PRE)
			{
				*stop_download = TRUE;
			}
		}
		else
		{
			glyr_message(-1,NULL,"glyr: Warn: Hashtable is empty im image_callback!\n");
		}
	}
	return NULL;
}
Example #3
0
/* Check if a cache is already in the db, by cheskum or source_url  */
gboolean db_contains(GlyrDatabase * db, GlyrMemCache * cache)
{
	gboolean result = FALSE;
	if(db && cache)
	{
		gchar * sql = sqlite3_mprintf(
				"SELECT source_url,data_checksum,data_size,data_type FROM metadata AS m      "
				"WHERE (m.data_type = %d AND m.data_size = %d AND m.data_checksum = '?')     "
				"OR (m.source_url LIKE '%q' AND m.source_url IS NOT NULL AND data_type = %d) "
                "LIMIT 1;                                                                    ",
				cache->type,
				cache->size,
				cache->dsrc,
				cache->type);

		if(sql != NULL)
		{
			sqlite3_stmt * stmt = NULL;
			sqlite3_prepare_v2(db->db_handle, sql, strlen(sql) + 1, &stmt, NULL);
			sqlite3_bind_blob(stmt, 1, cache->md5sum, sizeof cache->md5sum, SQLITE_TRANSIENT);
	
			int err = sqlite3_step(stmt);	
			if(err == SQLITE_ROW)
			{
				result = TRUE;
			}
			else if(err != SQLITE_DONE) 
			{
				glyr_message(-1,NULL,"db_contains: error message: %s\n", sqlite3_errmsg(db->db_handle));
			}
	
			sqlite3_finalize(stmt);
			sqlite3_free(sql);
		}
	}
	return result;
}
Example #4
0
static void foreach_file(const gchar *song_dir_path, const GRegex *cRegex, GlyrQuery *query, GList **retv_list)
{
    if(song_dir_path != NULL) {
        GError *dir_error = NULL;
        GDir *song_dir = g_dir_open(song_dir_path, 0, &dir_error);
        if(song_dir != NULL) {
            const gchar *entry = NULL;
            while((entry = g_dir_read_name(song_dir)) != NULL) {
                if(regex_match_compiled(entry, cRegex) == TRUE) {

                    gsize size = 0;
                    gchar *absolute_path = g_strdup_printf("%s%c%s", song_dir_path, G_DIR_SEPARATOR, entry);

                    if(absolute_path != NULL) {
                        /* Read file */
                        gchar *file_path = g_filename_from_utf8(absolute_path, -1, NULL, NULL, NULL);
                        gchar *retv = get_file_contents(file_path, &size);
                        g_free(file_path);
                        g_free(absolute_path);

                        /* Add file to result list if not null */
                        file_to_cache(retv_list, query, retv, size);

                        /* Not requested more? */
                        if(g_list_length(*retv_list) >= (gsize) query->number) {
                            break;
                        }
                    }
                }
            }
            g_dir_close(song_dir);
        } else {
            glyr_message(-1, NULL, "Opening %s failed: %s\n", song_dir_path, dir_error->message);
            g_error_free(dir_error);
        }
    }
}
Example #5
0
const gchar * get_mbid_from_xml (GlyrQuery * s, GlyrMemCache * c, gint * offset)
{
    if (c==NULL || s==NULL || offset==NULL)
        return NULL;

    const gchar * searchterm  = NULL;
    const gchar * checkstring = NULL;
    const gchar * comparestr  = NULL;

    switch (please_what_type (s) )
    {
    case GLYR_TYPE_TAG_TITLE:
        checkstring = "<title>";
        searchterm  = "<track ";
        comparestr  = s->title;
        break;
    case GLYR_TYPE_TAG_ALBUM:
        checkstring = "<title>";
        searchterm  = "<release ";
        comparestr  = s->album;
        break;
    case GLYR_TYPE_TAG_ARTIST:
        checkstring = "<name>";
        searchterm  = "<artist ";
        comparestr  = s->artist;
        break;
    default:
        glyr_message (1,s,"Warning: (tags/musicbrainz.c) Unable to determine type.\n");
    }

    const gchar * mbid = NULL;
    if (searchterm != NULL)
    {
        gchar * node = c->data + *offset;
        gchar * search_check = NULL;
        gsize nlen = (sizeof ID_BEGIN) - 1;
        gsize clen = strlen (checkstring);

        while (node && (node = strstr (node,searchterm) ) && mbid == NULL)
        {
            if (! (node = strstr (node,ID_BEGIN) ) )
                break;

            if (! (search_check = strstr (node,checkstring) ) )
                break;

            search_check += clen;
            gchar * to_compare = copy_value (search_check,strstr (search_check,"</") );
            if (to_compare != NULL)
            {
                if (levenshtein_strnormcmp (s,to_compare,comparestr) <= s->fuzzyness)
                {
                    mbid = (gchar*) copy_value (node+nlen,strchr (node+nlen,'"') );
                }
                g_free (to_compare);
            }
            node += (sizeof ID_BEGIN) - 1;
        }
        *offset = node - c->data;
    }
    return mbid;
}