Chat-O-Matic/libs/libdownload/ActionDownload.h

131 lines
2.2 KiB
C
Raw Normal View History

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
};
ActionDownload(BString URL, entry_ref dest,
2010-07-10 08:37:58 -05:00
bool allowResume, BLooper* target = NULL, uint32 msg = 0);
virtual ~ActionDownload();
2010-07-10 08:37:58 -05:00
status_t Perform(BMessage* errors);
BString GetDescription();
BString GetLocalPath();
void
SetLooper(BLooper* loop, uint32 msg)
{
2010-07-10 08:37:58 -05:00
fTarget = loop;
fMsg = msg;
};
BLooper*
Looper()
{
2010-07-10 08:37:58 -05:00
return fTarget;
}
uint32
MessageWhat()
{
2010-07-10 08:37:58 -05:00
return fMsg;
}
2010-07-10 08:37:58 -05:00
// For compatibility:
void
SetExtraData(BString extra)
{
2010-07-10 08:37:58 -05:00
extraInfo.AddString("extra", extra);
}
2010-07-10 08:37:58 -05:00
void
SetKey(BString key, BString data)
{
2010-07-10 08:37:58 -05:00
extraInfo.AddString(key.String(), data);
}
2010-07-10 08:37:58 -05:00
void
SetRef(BString key, entry_ref* ref)
{
2010-07-10 08:37:58 -05:00
extraInfo.AddRef(key.String(), ref);
}
2010-07-10 08:37:58 -05:00
status_t
GetRef(BString key, entry_ref* ref)
{
2010-07-10 08:37:58 -05:00
return extraInfo.FindRef(key.String(), ref);
}
2010-07-10 08:37:58 -05:00
BString GetKey(BString key)
{
2010-07-10 08:37:58 -05:00
BString data;
extraInfo.FindString(key.String(), &data);
return data;
}
2010-07-10 08:37:58 -05:00
void
SetShouldStop(bool stop)
{
2010-07-10 08:37:58 -05:00
fShouldStop = stop;
}
bool
ShouldStop()
{
2010-07-10 08:37:58 -05:00
return fShouldStop;
}
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);
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