Skip to content

Commit

Permalink
Merge pull request #644 from meganz/feature/pre-warning
Browse files Browse the repository at this point in the history
Check if the available transfer quota is enough
  • Loading branch information
javiserrano authored Jul 27, 2017
2 parents 1b1141f + ba63006 commit 99ba62a
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 6 deletions.
13 changes: 9 additions & 4 deletions bindings/ios/MEGARequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ typedef NS_ENUM (NSInteger, MEGARequestType) {
MEGARequestTypeGetUserEmail,
MEGARequestTypeAppVersion,
MEGARequestTypeGetLocalSSLCertificate,
MEGARequestTypeSendSignupLink
MEGARequestTypeSendSignupLink,
MEGARequestTypeQueryDns,
MEGARequestTypeQueryGelb,
MEGARequestTypeChatStats,
MEGARequestTypeDownloadFile,
MEGARequestTypeQueryTransferQuota
};

typedef NS_ENUM (NSInteger, MEGANodeAccessLevel) {
Expand Down Expand Up @@ -364,9 +369,9 @@ typedef NS_ENUM (NSInteger, MEGANodeAccessLevel) {
/**
* @brief A flag related to the request.
*
* This value is valid for these requests:
* - [MEGASdk retryPendingConnections] - Returns if request are disconnected
* - [MEGASdk pauseTransfers:] - Returns the direction of the transfers to pause/resume
* This value is valid for these request in onRequestFinish when the
* error code is MEGAErrorTypeApiOk:
* - [MEGASdk queryTransferQuota] - YES if it is expected to get an overquota error, otherwise NO
*
*/
@property (readonly, nonatomic) BOOL flag;
Expand Down
33 changes: 33 additions & 0 deletions bindings/ios/MEGASdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -2274,6 +2274,39 @@ typedef NS_ENUM(NSUInteger, PushNotificationTokenType) {
*/
- (void)getAccountDetails;

/**
* @brief Check if the available bandwidth quota is enough to transfer an amount of bytes
*
* The associated request type with this request is MEGARequestTypeQueryTransferQuota
*
* Valid data in the MegaRequest object received on callbacks:
* - [MEGARequest number] - Returns the amount of bytes to be transferred
*
* Valid data in the MegaRequest object received in onRequestFinish when the error code
* is MEGAErrorTypeApiOk:
* - [MEGARequest flag] - YES if it is expected to get an overquota error, otherwise NO
*
* @param size Amount of bytes to be transferred
* @param delegate MEGARequestDelegate to track this request
*/
- (void)queryTransferQuotaWithSize:(long long)size delegate:(id<MEGARequestDelegate>)delegate;

/**
* @brief Check if the available bandwidth quota is enough to transfer an amount of bytes
*
* The associated request type with this request is MEGARequestTypeQueryTransferQuota
*
* Valid data in the MegaRequest object received on callbacks:
* - [MEGARequest number] - Returns the amount of bytes to be transferred
*
* Valid data in the MegaRequest object received in onRequestFinish when the error code
* is MEGAErrorTypeApiOk:
* - [MEGARequest flag] - YES if it is expected to get an overquota error, otherwise NO
*
* @param size Amount of bytes to be transferred
*/
- (void)queryTransferQuotaWithSize:(long long)size;

/**
* @brief Get the available pricing plans to upgrade a MEGA account.
*
Expand Down
8 changes: 8 additions & 0 deletions bindings/ios/MEGASdk.mm
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,14 @@ - (void)getAccountDetails {
self.megaApi->getAccountDetails();
}

- (void)queryTransferQuotaWithSize:(long long)size delegate:(id<MEGARequestDelegate>)delegate {
self.megaApi->queryTransferQuota(size, [self createDelegateMEGARequestListener:delegate singleListener:YES]);
}

- (void)queryTransferQuotaWithSize:(long long)size {
self.megaApi->queryTransferQuota(size);
}

- (void)getPricingWithDelegate:(id<MEGARequestDelegate>)delegate {
self.megaApi->getPricing([self createDelegateMEGARequestListener:delegate singleListener:YES]);
}
Expand Down
3 changes: 2 additions & 1 deletion bindings/wp8/MRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ namespace mega
TYPE_CHAT_UPDATE_PERMISSIONS, TYPE_CHAT_TRUNCATE, TYPE_CHAT_SET_TITLE, TYPE_SET_MAX_CONNECTIONS,
TYPE_PAUSE_TRANSFER, TYPE_MOVE_TRANSFER, TYPE_CHAT_PRESENCE_URL, TYPE_REGISTER_PUSH_NOTIFICATION,
TYPE_GET_USER_EMAIL, TYPE_APP_VERSION, TYPE_GET_LOCAL_SSL_CERT, TYPE_SEND_SIGNUP_LINK,
TOTAL_OF_REQUEST_TYPES
TYPE_QUERY_DNS, TYPE_QUERY_GELB, TYPE_CHAT_STATS, TYPE_DOWNLOAD_FILE,
TYPE_QUERY_TRANSFER_QUOTA, TOTAL_OF_REQUEST_TYPES
};

public ref class MRequest sealed
Expand Down
10 changes: 10 additions & 0 deletions bindings/wp8/MegaSDK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,6 +1843,16 @@ void MegaSDK::getExtendedAccountDetails(bool sessions, bool purchases, bool tran
megaApi->getExtendedAccountDetails(sessions, purchases, transactions);
}

void MegaSDK::queryTransferQuota(int64 size, MRequestListenerInterface^ listener)
{
megaApi->queryTransferQuota(size, createDelegateMRequestListener(listener));
}

void MegaSDK::queryTransferQuota(int64 size)
{
megaApi->queryTransferQuota(size);
}

void MegaSDK::getPricing(MRequestListenerInterface^ listener)
{
megaApi->getPricing(createDelegateMRequestListener(listener));
Expand Down
2 changes: 2 additions & 0 deletions bindings/wp8/MegaSDK.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ namespace mega
void fetchNodes();
void getAccountDetails(MRequestListenerInterface^ listener);
void getAccountDetails();
void queryTransferQuota(int64 size, MRequestListenerInterface^ listener);
void queryTransferQuota(int64 size);
void getExtendedAccountDetails(bool sessions, bool purchases, bool transactions, MRequestListenerInterface^ listener);
void getExtendedAccountDetails(bool sessions, bool purchases, bool transactions);
void getPricing(MRequestListenerInterface^ listener);
Expand Down
8 changes: 8 additions & 0 deletions include/mega/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,14 @@ class MEGA_API CommandGetUserQuota : public Command
CommandGetUserQuota(MegaClient*, AccountDetails*, bool, bool, bool);
};

class MEGA_API CommandQueryTransferQuota : public Command
{
public:
void procresult();

CommandQueryTransferQuota(MegaClient*, m_off_t size);
};

class MEGA_API CommandGetUserTransactions : public Command
{
AccountDetails* details;
Expand Down
3 changes: 3 additions & 0 deletions include/mega/megaapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ struct MEGA_API MegaApp
virtual void account_details(AccountDetails*, bool, bool, bool, bool, bool, bool) { }
virtual void account_details(AccountDetails*, error) { }

// query bandwidth quota result
virtual void querytransferquota_result(int) { }

// sessionid is undef if all sessions except the current were killed
virtual void sessions_killed(handle /*sessionid*/, error) { }

Expand Down
3 changes: 3 additions & 0 deletions include/mega/megaclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ class MEGA_API MegaClient
// retrieve user details
void getaccountdetails(AccountDetails*, bool, bool, bool, bool, bool, bool);

// check if the available bandwidth quota is enough to transfer an amount of bytes
void querytransferquota(m_off_t size);

// update node attributes
error setattr(Node*, const char* prevattr = NULL);

Expand Down
24 changes: 23 additions & 1 deletion include/megaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2005,7 +2005,7 @@ class MegaRequest
TYPE_PAUSE_TRANSFER, TYPE_MOVE_TRANSFER, TYPE_CHAT_PRESENCE_URL, TYPE_REGISTER_PUSH_NOTIFICATION,
TYPE_GET_USER_EMAIL, TYPE_APP_VERSION, TYPE_GET_LOCAL_SSL_CERT, TYPE_SEND_SIGNUP_LINK,
TYPE_QUERY_DNS, TYPE_QUERY_GELB, TYPE_CHAT_STATS, TYPE_DOWNLOAD_FILE,
TOTAL_OF_REQUEST_TYPES
TYPE_QUERY_TRANSFER_QUOTA, TOTAL_OF_REQUEST_TYPES
};

virtual ~MegaRequest();
Expand Down Expand Up @@ -2412,6 +2412,7 @@ class MegaRequest
* - MegaApi::moveTransferBefore - Returns the tag of the transfer with the target position
* - MegaApi::moveTransferBeforeByTag - Returns the tag of the transfer with the target position
* - MegaApi::setMaxConnections - Returns the number of connections
* - MegaApi::queryTransferQuota - Returns the amount of bytes to be transferred
*
* This value is valid for these request in onRequestFinish when the
* error code is MegaError::API_OK:
Expand Down Expand Up @@ -2445,6 +2446,10 @@ class MegaRequest
* - MegaApi::moveTransferBefore - Returns false (it means that it's a manual move)
* - MegaApi::moveTransferBeforeByTag - Returns false (it means that it's a manual move)
*
* This value is valid for these request in onRequestFinish when the
* error code is MegaError::API_OK:
* - MegaApi::queryTransferQuota - True if it is expected to get an overquota error, otherwise false
*
* @return Flag related to the request
*/
virtual bool getFlag() const;
Expand Down Expand Up @@ -6509,6 +6514,23 @@ class MegaApi
*/
void getExtendedAccountDetails(bool sessions = false, bool purchases = false, bool transactions = false, MegaRequestListener *listener = NULL);

/**
* @brief Check if the available transfer quota is enough to transfer an amount of bytes
*
* The associated request type with this request is MegaRequest::TYPE_QUERY_TRANSFER_QUOTA
*
* Valid data in the MegaRequest object received on callbacks:
* - MegaRequest::getNumber - Returns the amount of bytes to be transferred
*
* Valid data in the MegaRequest object received in onRequestFinish when the error code
* is MegaError::API_OK:
* - MegaRequest::getFlag - True if it is expected to get an overquota error, otherwise false
*
* @param size Amount of bytes to be transferred
* @param listener MegaRequestListener to track this request
*/
void queryTransferQuota(long long size, MegaRequestListener *listener = NULL);

/**
* @brief Get the available pricing plans to upgrade a MEGA account
*
Expand Down
2 changes: 2 additions & 0 deletions include/megaapi_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,7 @@ class MegaApiImpl : public MegaApp
void getUserData(MegaUser *user, MegaRequestListener *listener = NULL);
void getUserData(const char *user, MegaRequestListener *listener = NULL);
void getAccountDetails(bool storage, bool transfer, bool pro, bool sessions, bool purchases, bool transactions, MegaRequestListener *listener = NULL);
void queryTransferQuota(long long size, MegaRequestListener *listener = NULL);
void createAccount(const char* email, const char* password, const char* name, MegaRequestListener *listener = NULL);
void createAccount(const char* email, const char* password, const char* firstname, const char* lastname, MegaRequestListener *listener = NULL);
void fastCreateAccount(const char* email, const char *base64pwkey, const char* name, MegaRequestListener *listener = NULL);
Expand Down Expand Up @@ -1872,6 +1873,7 @@ class MegaApiImpl : public MegaApp
// account credentials, properties and history
virtual void account_details(AccountDetails*, bool, bool, bool, bool, bool, bool);
virtual void account_details(AccountDetails*, error);
virtual void querytransferquota_result(int);

virtual void setattr_result(handle, error);
virtual void rename_result(handle, error);
Expand Down
24 changes: 24 additions & 0 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3241,6 +3241,30 @@ void CommandGetUserQuota::procresult()
}
}

CommandQueryTransferQuota::CommandQueryTransferQuota(MegaClient* client, m_off_t size)
{
cmd("qbq");
arg("s", size);

tag = client->reqtag;
}

void CommandQueryTransferQuota::procresult()
{
if (!client->json.isnumeric())
{
LOG_err << "Unexpected response: " << client->json.pos;
client->json.storeobject();

// Returns 0 to not alarm apps and don't show overquota pre-warnings
// if something unexpected is received, following the same approach as
// in the webclient
return client->app->querytransferquota_result(0);
}

return client->app->querytransferquota_result(client->json.getint());
}

CommandGetUserTransactions::CommandGetUserTransactions(MegaClient* client, AccountDetails* ad)
{
cmd("utt");
Expand Down
5 changes: 5 additions & 0 deletions src/megaapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,11 @@ void MegaApi::getExtendedAccountDetails(bool sessions, bool purchases, bool tran
pImpl->getAccountDetails(true, true, true, sessions, purchases, transactions, listener);
}

void MegaApi::queryTransferQuota(long long size, MegaRequestListener *listener)
{
pImpl->queryTransferQuota(size, listener);
}

void MegaApi::getPricing(MegaRequestListener *listener)
{
pImpl->getPricing(listener);
Expand Down
27 changes: 27 additions & 0 deletions src/megaapi_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2719,6 +2719,7 @@ const char *MegaRequestPrivate::getRequestString() const
case TYPE_QUERY_GELB: return "QUERY_GELB";
case TYPE_CHAT_STATS: return "CHAT_STATS";
case TYPE_DOWNLOAD_FILE: return "DOWNLOAD_FILE";
case TYPE_QUERY_TRANSFER_QUOTA: return "QUERY_TRANSFER_QUOTA";
}
return "UNKNOWN";
}
Expand Down Expand Up @@ -5056,6 +5057,14 @@ void MegaApiImpl::getAccountDetails(bool storage, bool transfer, bool pro, bool
waiter->notify();
}

void MegaApiImpl::queryTransferQuota(long long size, MegaRequestListener *listener)
{
MegaRequestPrivate *request = new MegaRequestPrivate(MegaRequest::TYPE_QUERY_TRANSFER_QUOTA, listener);
request->setNumber(size);
requestQueue.push(request);
waiter->notify();
}

void MegaApiImpl::changePassword(const char *oldPassword, const char *newPassword, MegaRequestListener *listener)
{
MegaRequestPrivate *request = new MegaRequestPrivate(MegaRequest::TYPE_CHANGE_PW, listener);
Expand Down Expand Up @@ -10488,6 +10497,18 @@ void MegaApiImpl::account_details(AccountDetails*, error e)
fireOnRequestFinish(request, megaError);
}

void MegaApiImpl::querytransferquota_result(int code)
{
if(requestMap.find(client->restag) == requestMap.end()) return;
MegaRequestPrivate* request = requestMap.at(client->restag);
if(!request || (request->getType() != MegaRequest::TYPE_QUERY_TRANSFER_QUOTA)) return;

// pre-warn about a possible overquota for codes 2 and 3, like in the webclient
request->setFlag((code == 2 || code == 3) ? true : false);

fireOnRequestFinish(request, MegaError(API_OK));
}

void MegaApiImpl::removecontact_result(error e)
{
MegaError megaError(e);
Expand Down Expand Up @@ -13326,6 +13347,12 @@ void MegaApiImpl::sendPendingRequests()
client->getaccountdetails(request->getAccountDetails(), storage, transfer, pro, transactions, purchases, sessions);
break;
}
case MegaRequest::TYPE_QUERY_TRANSFER_QUOTA:
{
m_off_t size = request->getNumber();
client->querytransferquota(size);
break;
}
case MegaRequest::TYPE_CHANGE_PW:
{
const char* oldPassword = request->getPassword();
Expand Down
5 changes: 5 additions & 0 deletions src/megaclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8351,6 +8351,11 @@ void MegaClient::getaccountdetails(AccountDetails* ad, bool storage,
}
}

void MegaClient::querytransferquota(m_off_t size)
{
reqs.add(new CommandQueryTransferQuota(this, size));
}

// export node link
error MegaClient::exportnode(Node* n, int del, m_time_t ets)
{
Expand Down

0 comments on commit 99ba62a

Please sign in to comment.