📄 uploading.cpp
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Uploading.h"
#include "GiftCommand.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
namespace KCeasyEngine {
// class TUlSource
// public
unsigned int TUlSource::GetTimeLeft()
{
if(Throughput.GetBps() == 0)
return 0;
return (Size - Transmitted) / Throughput.GetBps();
}
// private
TUlSource::TUlSource()
: State(New),
Start(0),
Size(0),
Transmitted(0)
{
User = new TUser();
}
TUlSource::~TUlSource()
{
delete User;
}
bool TUlSource::SetStateStr(const string& StateStr)
{
if(StateStr == "Waiting") State = Waiting;
else if(StateStr == "Paused") State = Paused;
else if(StateStr == "Queued (Remotely)") State = QueuedRemote;
else if(StateStr == "Queued") State = QueuedLocal;
else if(StateStr == "Active") State = Active;
else if(StateStr == "Complete") State = Complete;
else if(StateStr == "Cancelled (Remotely)") State = Cancelled;
else if(StateStr == "Timed out") State = Timeout;
else {
State = Unused;
return false;
}
return true;
}
// class TUpload
// private
TUpload::TUpload(TEngine* ParentEngine)
: Engine(ParentEngine),
GiftId(0),
State(New),
Special(false),
FileSize(0),
Transmitted(0),
Source(NULL),
Hashes(NULL)
{
Hashes = new THashSet();
}
TUpload::~TUpload()
{
delete Hashes;
if(Source)
delete Source;
}
bool TUpload::ProcessAddUpload(TGiftCommand* Cmd)
{
if(Cmd->Name != "ADDUPLOAD")
return false;
Engine->LockUploads();
GiftId = string_to_int(Cmd->Value);
Hashes->AddGiftHash(Cmd->GetNode("hash").Value);
FileName = Cmd->GetNode("file").Value;
FileSize = string_to_int(Cmd->GetNode("size").Value);
Transmitted = string_to_int(Cmd->GetNode("transmit").Value);
// set mime type, may be overwritten later with data from shares
MimeType = Cmd->GetNode("mime").Value;
Path = Cmd->GetNode("path").Value;
if (Engine->IsUsingLocalGift())
Path = UnixToWindowsPath(Path);
// HACKHACK: match nodes file by name
if(string_to_int(Cmd->GetNode("shared").Value) != 1 || FileName == "nodes.serve")
Special = 1;
else
Special = 0;
// state
string StateStr = Cmd->GetNode("state").Value;
if(StateStr == "active")
State = TUpload::Uploading;
else if(StateStr == "queued")
State = TUpload::Queued;
// add source
TGiftCommand& SourceCmd = Cmd->GetNode("SOURCE");
Source = new TUlSource();
Source->SourceId = SourceCmd.GetNode("url").Value;
Source->GetUser()->SetId(SourceCmd.GetNode("user").Value);
// set Upload->Network too
Network = Source->Network = ProtocolFromUrl(Source->SourceId);
Source->Start = string_to_int(SourceCmd.GetNode("start").Value);
Source->Size = string_to_int(SourceCmd.GetNode("total").Value);
Source->Transmitted = string_to_int(SourceCmd.GetNode("transmit").Value);
Source->SetStateStr(SourceCmd.GetNode("statusgrl").Value);
Source->ProtoState = SourceCmd.GetNode("status").Value;
// lookup meta data from shares list
// FIXME: lookup by hash when hash set implemented?
Engine->GetShares()->LockFiles();
TSharedFile* Share = Engine->GetShares()->GetFileByPath(Path);
if(Share) {
MetaData = Share->MetaData;
MimeType = Share->MimeType;
}
Engine->GetShares()->ReleaseFiles();
Engine->ReleaseUploads();
// notify UI
Engine->SendCallback(CbcNewUpload,(void*)this);
return true;
}
bool TUpload::ProcessDelUpload(TGiftCommand* Cmd)
{
if(Cmd->Name != "DELUPLOAD" || string_to_int(Cmd->Value) != GiftId)
return false;
Engine->LockUploads();
// update state
if(Transmitted == FileSize)
State = Completed;
else
State = Cancelled;
GiftId = 0; // giFT reuses old IDs so drop it
Engine->ReleaseUploads();
// notify UI
Engine->SendCallback(CbcUploadUpdate,(void*)this);
return true;
}
bool TUpload::ProcessChgUpload(TGiftCommand* Cmd)
{
if(Cmd->Name != "CHGUPLOAD" || string_to_int(Cmd->Value) != GiftId)
return false;
Engine->LockUploads();
// update upload state
Throughput.AddSample(string_to_int(Cmd->GetNode("throughput").Value));
Transmitted = string_to_int(Cmd->GetNode("transmit").Value);
Path = Cmd->GetNode("path").Value;
if (Engine->IsUsingLocalGift())
Path = UnixToWindowsPath(Path);
// state
string StateStr = Cmd->GetNode("state").Value;
if(StateStr == "active")
State = Uploading;
else if(StateStr == "queued")
State = Queued;
// update sources
int TimeElapsed = string_to_int(Cmd->GetNode("elapsed").Value);
TGiftCommand& SourceCmd = Cmd->GetNode("SOURCE");
Source->Start = string_to_int(SourceCmd.GetNode("start").Value);
Source->Size = string_to_int(SourceCmd.GetNode("total").Value);
Source->Throughput.AddSample(string_to_int(SourceCmd.GetNode("transmit").Value)-Source->Transmitted,TimeElapsed);
Source->Transmitted = string_to_int(SourceCmd.GetNode("transmit").Value);
Source->SetStateStr(SourceCmd.GetNode("statusgrl").Value);
Source->ProtoState = SourceCmd.GetNode("status").Value;
Engine->ReleaseUploads();
// notify UI
Engine->SendCallback(CbcUploadUpdate,(void*)this);
return true;
}
// public
bool TUpload::Cancel()
{
if(State == New || State == Completed || State == Cancelled)
return false;
TGiftCommand* Cmd = new TGiftCommand("TRANSFER",int_to_string(GiftId));
Cmd->AddNode(TGiftCommand("action","cancel"));
Engine->QueueCommand(Cmd);
return true;
}
} // namespace KCeasyEngine
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -