示例#1
0
static void mlp_hook_playback_begin(gpointer hook_data, gpointer user_data)
{
	gint playlist = mlp_playlist_get_playing();
	gint pos = mlp_playlist_get_position(playlist);

	if (mlp_playlist_entry_get_length (playlist, pos, FALSE) < 30)
	{
		AUDDBG(" *** not submitting due to entry->length < 30");
		return;
	}

	gchar * filename = mlp_playlist_entry_get_filename (playlist, pos);
	if (ishttp (filename))
	{
		AUDDBG(" *** not submitting due to HTTP source");
		str_unref (filename);
		return;
	}
	str_unref (filename);

	sc_idle(m_scrobbler);

	if (submit_tuple)
		tuple_unref (submit_tuple);
	submit_tuple = mlp_playlist_entry_get_tuple (playlist, pos, FALSE);
	if (! submit_tuple)
		return;

	sc_addentry(m_scrobbler, submit_tuple, tuple_get_int(submit_tuple, FIELD_LENGTH, NULL) / 1000);

	if (!track_timeout)
		track_timeout = g_timeout_add_seconds(1, sc_timeout, NULL);
}
示例#2
0
/**
Download a file

Pass the URL of the file to url

To specify an update function that is called after each buffer is read, pass a
pointer to that function as the third parameter. If no update function is
desired, then let the third parameter default to null.
*/
bool Download::download(char *url, bool reload, void (*update)(unsigned long, unsigned long), char *name)
{
    ofstream fout;              // output stream
    unsigned char buf[BUF_SIZE];// input buffer
    unsigned long numrcved;     // number of bytes read
    unsigned long filelen;      // length of the file on disk
    HINTERNET hIurl, hInet;     // internet handles
    unsigned long contentlen;   // length of content
    unsigned long len;          // length of contentlen
    unsigned long total = 0;    // running total of bytes received
    char header[80];            // holds Range header

    try
    {
        if(!ishttp(url))
            throw DLExc("Must be HTTP url");

        /*
        Open the file spcified by url.
        The open stream will be returned in fout. If reload is true, then any
        preexisting file will be truncated. The length of any preexisting file
        (after possible truncation) is returned.
        */
        filelen = openfile(url, reload, fout, name);

        // See if internet connection is available
        if(InternetAttemptConnect(0) != ERROR_SUCCESS)
            throw DLExc("Can't connect");

        // Open internet connection
        hInet = InternetOpen(TEXT("downloader"), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
        if(hInet == NULL)
            throw DLExc("Can't open connection");

        // Construct header requesting range of data
        sprintf(header, "Range:bytes=%d-", filelen);

        // Open the URL and request range
        //hIurl = InternetOpenUrl(hInet, url, header, -1, INTERNET_FLAG_NO_CACHE_WRITE, 0);
        BSTR header_w = Ui::toWString(header);
        BSTR url_w = Ui::toWString(url);
        DWORD flags = (INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID);
        hIurl = InternetOpenUrl(hInet, url_w, header_w, strlen(header), flags, 0);
        if(hIurl == NULL)
            throw DLExc("Can't open url");

        // Confirm that HTTP/1.1 or greater is supported
        if(!httpverOK(hIurl))
            throw DLExc("HTTP/1.1 not supported");

        // Get content length
        len = sizeof contentlen;
        if(!HttpQueryInfo(hIurl, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &contentlen, &len, NULL))
            throw DLExc("File or content length not found");

        // If existing file (if any) is not complete, then finish downloading
        if(filelen != contentlen && contentlen)
        {
            do
            {
                if(!Work::runWork())
                    throw DLExc("Canceled");

                // Read a buffer of info
                if(!InternetReadFile(hIurl, &buf, BUF_SIZE, &numrcved))
                    throw DLExc("Error occurred during download");

                // Write buffer to disk
                fout.write((const char *) buf, numrcved);
                if(!fout.good())
                    throw DLExc("Error writing file");

                // update running total
                total += numrcved;

                // Call update function, if specified
                if(update && numrcved > 0)
                    update(contentlen + filelen, total + filelen);
            } while (numrcved > 0);
        }
        else
        {
            if(update)
                update(filelen, filelen);
        }
        ::SysFreeString(header_w);
        ::SysFreeString(url_w);
    }
    catch (DLExc)
    {
        if(fout.is_open())
        {
            fout.close();
            InternetCloseHandle(hIurl);
            InternetCloseHandle(hInet);
        }

        // rethrow the exception for use by the caller
        throw;
    }

    fout.close();
    InternetCloseHandle(hIurl);
    InternetCloseHandle(hInet);

    return true;
}