Esempio n. 1
0
/**
    funzione di callback applicata da init_db() (usabile anche esternamente,
    ovvio, ma aggiunge al db solo file locali!)

    prende un percorso (si assume che punti ad un file valido). Se questo è un
    file supportato da taglib, ne analizza i tag e inserisce i dati nel database
    \attention le query *non* sono generiche, nè usano il file constants.h!
            inoltre, poiché questi dati saranno usati per generare i percorsi
            virtuali, non sono ammessi metadati col carattere '/': in tal caso
            saranno sostituiti col carattere '_'

    \param db database (di tipo sqlite*, ma castato a void* per questioni di
            compatibilità)
    \param path percorso del file da analizzare e, se supportato, aggiungere al
            database
    \return 0 se non ci sono errori, -1 altrimenti
    \sa init_db(), esegui_query(), preprocessing()
*/
int add_local_file_in_db(void* db, const char* path) {
    warn("[add_local_file_in_db] path = `%s'\n", path);
    TagLib_File* tlf = taglib_file_new(path);
    if (!tlf)
        return -1;
    TagLib_Tag* tlt = taglib_file_tag(tlf); // automaticamente free`d
    char* titolo = preprocessing(taglib_tag_title(tlt));
    char* artista = preprocessing(taglib_tag_artist(tlt));
    char* album = preprocessing(taglib_tag_album(tlt));
    char* genere = preprocessing(taglib_tag_genre(tlt));
    unsigned int anno = taglib_tag_year(tlt);
    unsigned int traccia = taglib_tag_track(tlt);

    char* anno_s = calloc(5, 1);    // andrà bene fino all'anno 9999
    char* traccia_s = calloc(3, 1); // andrà bene fino alla traccia 99

    snprintf(anno_s, 5, "%04d", anno);
    snprintf(traccia_s, 3, "%02d", traccia);

    if (esegui_query(db, "INSERT OR REPLACE INTO artista(nome_artista) values "
            "(%Q)", artista) == -1)
        return -1;
    if (esegui_query(db, "INSERT OR REPLACE INTO musica(titolo, nome_album,"
            " traccia, genere, artista_nome_artista, lavoro_anno) values "
            "(%Q, %Q, %Q, %Q, %Q, %Q)", titolo, album, traccia_s, genere,
            artista, anno_s) == -1)
        return -1;
    if (esegui_query(db, "INSERT OR REPLACE INTO file(host, path, permessi,"
            " formato, dimensioni, data_ultimo_aggiornamento, musica_titolo"
            ", musica_nome_album, musica_traccia, basename) values ("
            "'127.0.0.1', %Q, %d, %Q, %d, NULL, %Q, %Q, %Q, %Q)", path, 6,
            extract_extension(path), extract_size(path), titolo, album,
            traccia_s, extract_basename(path)) == -1)
        return -1;
    taglib_tag_free_strings();
    taglib_file_free(tlf);
    return 0;
}
Esempio n. 2
0
int BC_FileBox::create_tables()
{
	delete_tables();
	char string[BCTEXTLEN];
	BC_ListBoxItem *new_item;

	fs->set_sort_order(sort_order);
	fs->set_sort_field(column_type[sort_column]);

// Directory is entered before this from a random source
	fs->update(0);
	for(int i = 0; i < fs->total_files(); i++)
	{
		FileItem *file_item = fs->get_entry(i);
		int is_dir = file_item->is_dir;
		BC_Pixmap* icon = get_icon(file_item->name, is_dir);

// Name entry
		new_item = new BC_ListBoxItem(file_item->name,
			icon, 
			is_dir ? get_resources()->directory_color : get_resources()->file_color);
		if(is_dir) new_item->set_searchable(0);
		list_column[column_of_type(FILEBOX_NAME)].append(new_item);
	
// Size entry
// 		if(!want_directory)
// 		{
			if(!is_dir)
			{
				sprintf(string, "%lld", file_item->size);
				new_item = new BC_ListBoxItem(string, get_resources()->file_color);
			}
			else
			{
				new_item = new BC_ListBoxItem("", get_resources()->directory_color);
			}

	 		list_column[column_of_type(FILEBOX_SIZE)].append(new_item);
//		}

// Date entry
		if(!is_dir || 1)
		{
			static const char *month_text[13] = 
			{
				"Null",
				"Jan",
				"Feb",
				"Mar",
				"Apr",
				"May",
				"Jun",
				"Jul",
				"Aug",
				"Sep",
				"Oct",
				"Nov",
				"Dec"
			};
			sprintf(string, 
				"%s %d, %d", 
				month_text[file_item->month],
				file_item->day,
				file_item->year);
			new_item = new BC_ListBoxItem(string, get_resources()->file_color);
		}
		else
		{
			new_item = new BC_ListBoxItem("", get_resources()->directory_color);
		}

		list_column[column_of_type(FILEBOX_DATE)].append(new_item);

// Extension entry
//		if(!want_directory)
//		{
			if(!is_dir)
			{
				extract_extension(string, file_item->name);
				new_item = new BC_ListBoxItem(string, get_resources()->file_color);
			}
			else
			{
				new_item = new BC_ListBoxItem("", get_resources()->directory_color);
			}
			list_column[column_of_type(FILEBOX_EXTENSION)].append(new_item);
//		}
	}

	return 0;
}