2010-07-10 08:37:58 -05:00
|
|
|
/*
|
|
|
|
* Copyright 2010, Andrea Anzani. All rights reserved.
|
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _ACTION_DOWNLOAD_H
|
|
|
|
#define _ACTION_DOWNLOAD_H
|
|
|
|
|
|
|
|
#include <curl/curl.h>
|
|
|
|
|
|
|
|
#include <Entry.h>
|
|
|
|
#include <Looper.h>
|
|
|
|
#include <File.h>
|
|
|
|
#include <SupportDefs.h>
|
|
|
|
#include <DownloadManager.h>
|
|
|
|
|
|
|
|
#include "Action.h"
|
|
|
|
|
|
|
|
class ActionDownload : public Action
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum Status {
|
|
|
|
ERROR_OPENFILE, // 0
|
|
|
|
ERROR_CURL_INIT, // 1
|
|
|
|
ERROR_PERFORMING, // 2
|
|
|
|
OK_CONNECTING, // 3
|
|
|
|
OK_PROGRESS, // 4
|
|
|
|
OK_DOWNLOADED // 5
|
|
|
|
};
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
ActionDownload(BString URL, entry_ref dest,
|
2010-07-10 08:37:58 -05:00
|
|
|
bool allowResume, BLooper* target = NULL, uint32 msg = 0);
|
2013-01-04 07:52:14 -06:00
|
|
|
virtual ~ActionDownload();
|
2010-07-10 08:37:58 -05:00
|
|
|
|
|
|
|
status_t Perform(BMessage* errors);
|
|
|
|
BString GetDescription();
|
|
|
|
BString GetLocalPath();
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
void
|
|
|
|
SetLooper(BLooper* loop, uint32 msg)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
fTarget = loop;
|
|
|
|
fMsg = msg;
|
2013-01-04 07:52:14 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
BLooper*
|
|
|
|
Looper()
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
return fTarget;
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32
|
|
|
|
MessageWhat()
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
return fMsg;
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
|
|
|
// For compatibility:
|
2013-01-04 07:52:14 -06:00
|
|
|
void
|
|
|
|
SetExtraData(BString extra)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
extraInfo.AddString("extra", extra);
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
void
|
|
|
|
SetKey(BString key, BString data)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
extraInfo.AddString(key.String(), data);
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
void
|
|
|
|
SetRef(BString key, entry_ref* ref)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
extraInfo.AddRef(key.String(), ref);
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
status_t
|
|
|
|
GetRef(BString key, entry_ref* ref)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
return extraInfo.FindRef(key.String(), ref);
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
BString GetKey(BString key)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
BString data;
|
|
|
|
extraInfo.FindString(key.String(), &data);
|
|
|
|
return data;
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
|
|
|
|
2013-01-04 07:52:14 -06:00
|
|
|
void
|
|
|
|
SetShouldStop(bool stop)
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
fShouldStop = stop;
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ShouldStop()
|
|
|
|
{
|
2010-07-10 08:37:58 -05:00
|
|
|
return fShouldStop;
|
2013-01-04 07:52:14 -06:00
|
|
|
}
|
2010-07-10 08:37:58 -05:00
|
|
|
|
|
|
|
void SetDownloadManager(DownloadManager* downloadManager);
|
|
|
|
|
|
|
|
private:
|
|
|
|
status_t openFile(BMessage*);
|
|
|
|
|
|
|
|
static void fillMessage(ActionDownload*, BMessage*);
|
|
|
|
static void sendProgressX(ActionDownload*, double max, double current);
|
|
|
|
|
|
|
|
//curl callbacks:
|
|
|
|
static size_t save_file ( void* ptr, size_t size, size_t nmemb, void* stream);
|
2013-01-04 07:52:14 -06:00
|
|
|
static int callback (void* clientp, double dltotal,
|
|
|
|
double dlnow, double , double);
|
2010-07-10 08:37:58 -05:00
|
|
|
|
|
|
|
CURL* curl;
|
|
|
|
BString fUrl;
|
|
|
|
entry_ref fDest;
|
|
|
|
BLooper* fTarget;
|
|
|
|
uint32 fMsg;
|
|
|
|
BMessage extraInfo;
|
|
|
|
bool fShouldStop;
|
|
|
|
BFile* file;
|
|
|
|
double resuming;
|
|
|
|
bool fAllowResume;
|
|
|
|
DownloadManager* fDownloadManager;
|
|
|
|
|
|
|
|
BString fFileType;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _ACTION_DOWNLOAD_H
|