2010-07-10 08:37:58 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2010, Andrea Anzani. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Andrea Anzani, andrea.anzani@gmail.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <Messenger.h>
|
|
|
|
|
|
|
|
#include "DownloadManager.h"
|
|
|
|
#include "ActionDownload.h"
|
|
|
|
|
|
|
|
#define DEFAULT_ITEMS_DOWNLOAD 1
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
DownloadManager::DownloadManager(BLooper* target)
|
|
|
|
:
|
|
|
|
fTarget(target)
|
2010-07-10 08:37:58 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
void
|
|
|
|
DownloadManager::Enqueue(QueueType type, ActionDownload* ad)
|
|
|
|
{
|
|
|
|
if (!fQueue[type]) {
|
2013-01-04 07:52:14 -06:00
|
|
|
fQueue[type] = new QueueFileDownload("queue_downloads",
|
|
|
|
DEFAULT_ITEMS_DOWNLOAD, fTarget, DOWNLOAD_INFO);
|
2010-07-10 08:37:58 -05:00
|
|
|
}
|
|
|
|
ad->SetDownloadManager(this);
|
|
|
|
fQueue[type]->AddAction(ad);
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
void
|
|
|
|
DownloadManager::TryStopCurrentAction(QueueType type, BString key, BString value)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!fQueue[type]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fQueue[type]->Lock()) {
|
|
|
|
for (int i = 0; i < fQueue[type]->CountThreads(); i++) {
|
|
|
|
ActionDownload* ad = (ActionDownload*)fQueue[type]->CurrentAction(i);
|
|
|
|
if (ad && ad->GetKey(key).Compare(value.String()) == 0 )
|
|
|
|
ad->SetShouldStop(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
fQueue[type]->Unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
bool
|
|
|
|
DownloadManager::RemoveFromQueue(QueueType type , BString key, BString value)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!fQueue[type]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fQueue[type]->Lock()) {
|
|
|
|
|
|
|
|
for (int32 i = 0; i < fQueue[type]->CountActions(); i++) {
|
|
|
|
ActionDownload* ad = (ActionDownload*)fQueue[type]->ActionAt(i);
|
|
|
|
if (ad->GetKey(key).Compare(value.String()) == 0 ) {
|
|
|
|
fQueue[type]->RemoveActionAt(i);
|
|
|
|
fQueue[type]->Unlock();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < fQueue[type]->CountThreads(); i++) {
|
|
|
|
ActionDownload* ad = (ActionDownload*)fQueue[type]->CurrentAction(i);
|
|
|
|
|
|
|
|
if (ad && ad->GetKey(key).Compare(value.String()) == 0 )
|
|
|
|
ad->SetShouldStop(true);
|
|
|
|
}
|
|
|
|
fQueue[type]->Unlock();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
void
|
|
|
|
DownloadManager::RemoveQueue(QueueType type, BList* removed)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!fQueue[type]) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fQueue[type]->Lock();
|
|
|
|
|
|
|
|
while (fQueue[type]->CountActions()) {
|
|
|
|
// ActionDownload *ad = (ActionDownload*)fQueue[type]->ActionAt(0);
|
|
|
|
fQueue[type]->RemoveActionAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ***************************************
|
|
|
|
// we did not unlock
|
|
|
|
// so no one can add new item.
|
|
|
|
// ***************************************
|
|
|
|
|
|
|
|
for (int i = 0; i < fQueue[type]->CountThreads(); i++) {
|
|
|
|
if (fQueue[type]->CurrentAction(i))
|
|
|
|
removed->AddItem( (void*)fQueue[type]->CurrentAction(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
void
|
|
|
|
DownloadManager::LoadProxySetting(BMessage* data)
|
|
|
|
{
|
|
|
|
|
|
|
|
bool value;
|
|
|
|
|
|
|
|
fEnabled = false;
|
|
|
|
fAddress.SetTo("");
|
|
|
|
fUserpwd.SetTo("");
|
|
|
|
fPort = 0;
|
|
|
|
|
|
|
|
if (data->FindBool("enable", &value) == B_OK)
|
|
|
|
fEnabled = value;
|
|
|
|
|
|
|
|
if (!fEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
BString username, password;
|
|
|
|
data->FindString("username", &username);
|
|
|
|
data->FindInt32("port", &fPort);
|
|
|
|
data->FindString("address", &fAddress);
|
|
|
|
data->FindString("password", &password);
|
|
|
|
if (username != "" || password != "")
|
|
|
|
fUserpwd << username << ":" << password;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DownloadManager::LoadDownloadSetting(BMessage* data)
|
|
|
|
{
|
|
|
|
|
|
|
|
//FIXME: fQueue[XXX] can be null.
|
|
|
|
|
|
|
|
//here magic stuff! :=)
|
|
|
|
int32 number = 1;
|
|
|
|
if (data->FindInt32("max_downloads", &number) == B_OK) {
|
|
|
|
fQueue[DOWNLOADS_QUEUE]->SetDownloadCount(number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
DownloadManager::FinishCurl(CURL* curl)
|
|
|
|
{
|
|
|
|
if (!fEnabled)
|
|
|
|
return;
|
|
|
|
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXY, fAddress.String());
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYPORT, fPort);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYTYPE , CURLPROXY_HTTP);
|
|
|
|
|
|
|
|
if (fUserpwd != "")
|
|
|
|
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD , fUserpwd.String() );
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
thread_id
|
|
|
|
DownloadManager::SingleThreadAction(ActionDownload* action)
|
|
|
|
{
|
|
|
|
action->SetDownloadManager(this);
|
|
|
|
if (action->Looper() == NULL) {
|
|
|
|
action->SetLooper(fTarget, DOWNLOAD_INFO);
|
|
|
|
}
|
2013-01-04 07:52:14 -06:00
|
|
|
thread_id id = spawn_thread(DownloadManager::SingleThreadPerform,
|
|
|
|
"single_action_thread", B_NORMAL_PRIORITY, (void*)action);
|
2010-07-10 08:37:58 -05:00
|
|
|
resume_thread(id);
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
|
2010-07-10 08:37:58 -05:00
|
|
|
int32
|
|
|
|
DownloadManager::SingleThreadPerform(void* a)
|
|
|
|
{
|
|
|
|
|
|
|
|
ActionDownload* ad = (ActionDownload*)a;
|
|
|
|
|
|
|
|
// perform the action
|
|
|
|
BMessage err;
|
|
|
|
//status_t status =
|
|
|
|
ad->Perform(&err);
|
|
|
|
// do post-perform!
|
|
|
|
if (ad->Looper()) {
|
|
|
|
err.what = ad->MessageWhat();
|
|
|
|
BMessenger(ad->Looper()).SendMessage(&err);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|