📄 magnetdialog.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.
*/
//---------------------------------------------------------------------------
#include <vcl.h>
#include <KCeasy.h>
#pragma hdrstop
#include "SearchPage.h"
#include "PlayerPage.h"
#include "MagnetDialog.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//TMagnetForm *MagnetForm;
//---------------------------------------------------------------------------
__fastcall TMagnetForm::TMagnetForm(TComponent* Owner, string MagnetString)
: TForm(Owner),
MagnetStr(MagnetString),
Magnet(NULL),
Hashes(NULL)
{
TranslateComponent(this);
Caption = Brand->ReplaceAppName(Caption);
StreamBtn->Enabled = false;
MagnetStrEdit->Text = MagnetStr.c_str();
Magnet = new TMagnet();
if(!Magnet->Parse(MagnetStr)) {
// not a valid magnet
SearchBtn->Enabled = false;
DownloadBtn->Enabled = false;
NetworksListView->Enabled = false;
DescrStatic->Font->Color = clRed;
DescrStatic->Font->Style = TFontStyles() << fsBold;
// TRANSLATOR: Error in Magnet dialog.
DescrStatic->Caption = _("The link you tried to open is not a valid magnet.");
return;
}
FileNameEdit->Text = Magnet->GetDisplayName().c_str();
// FIXME: diabled until proper support from giFT is available
DownloadBtn->Enabled = false;
// make sure the user is connected
if(!Engine || !Engine->IsOnline()) {
SearchBtn->Enabled = false;
DownloadBtn->Enabled = false;
NetworksListView->Enabled = false;
DescrStatic->Font->Color = clRed;
DescrStatic->Font->Style = TFontStyles() << fsBold;
// TRANSLATOR: Error in Magnet dialog.
DescrStatic->Caption = _("You need to be connected for {AppName} to process magnet links.");
return;
}
// create hash set from magnet
Hashes = new THashSet();
THashSet* Topics = Magnet->GetExactTopicHashes();
Hashes->JoinHashSet(Topics);
delete Topics;
Topics = Magnet->GetExactSubstitudeHashes();
Hashes->JoinHashSet(Topics);
delete Topics;
Topics = Magnet->GetAcceptableSubstitudeHashes();
Hashes->JoinHashSet(Topics);
delete Topics;
Engine->GetNetworks()->Lock();
// populate network list view with networks supporting these hashes
TNetworks::TNetworkIterator itr = Engine->GetNetworks()->GetBegin();
for(; itr != Engine->GetNetworks()->GetEnd(); ++itr) {
// check if network supports any of the hashes in magnet
for(THashSet::Iterator hitr=Hashes->Begin(); hitr != Hashes->End(); ++hitr) {
THashAlgo* Algo = (*itr)->GetAlgos()->GetAlgo((*hitr)->GetName());
if(Algo && Algo->Equals((*hitr)->GetAlgo())) {
TListItem* Item = NetworksListView->Items->Add();
Item->Caption = (*itr)->GetName().c_str();
Item->ImageIndex = MainForm->IconManager->GetNetworkIconIndex((*itr)->GetName());
Item->Checked = true;
break;
}
}
}
// if we don't support any of the hashes add all networks for keyword search if possible
if(NetworksListView->Items->Count == 0 && Magnet->GetKeywordTopic() != "") {
TNetworks::TNetworkIterator itr = Engine->GetNetworks()->GetBegin();
for(; itr != Engine->GetNetworks()->GetEnd(); ++itr) {
TListItem* Item = NetworksListView->Items->Add();
Item->Caption = (*itr)->GetName().c_str();
Item->ImageIndex = MainForm->IconManager->GetNetworkIconIndex((*itr)->GetName());
Item->Checked = true;
}
// we can only search with keywords, not download directly
DownloadBtn->Enabled = false;
NetworksListView->Enabled = true;
}
Engine->GetNetworks()->Release();
// do we have anything to stream?
StreamBtn->Enabled = false;
if(Magnet->IsStreamable() && (Magnet->GetMediaType() == "audio" || Magnet->GetMediaType() == "video"))
{
// http url?
list<string> Topics = Magnet->GetExactTopics();
list<string> ESubs = Magnet->GetExactSubstitudes();
list<string> ASubs = Magnet->GetAcceptableSubstitudes();
Topics.splice(Topics.end(),ESubs);
Topics.splice(Topics.end(),ASubs);
for(list<string>::iterator itr = Topics.begin(); itr != Topics.end(); ++itr) {
if((*itr).substr(0,7) == "http://") {
// found http url, assume it's the streamable one
StreamUrl = *itr;
StreamBtn->Enabled = true;
break;
}
}
}
// if there are still no networks we cannot do anything at all
if(NetworksListView->Items->Count == 0) {
SearchBtn->Enabled = false;
DownloadBtn->Enabled = false;
NetworksListView->Enabled = false;
DescrStatic->Font->Color = clRed;
DescrStatic->Font->Style = TFontStyles() << fsBold;
// TRANSLATOR: Error in Magnet dialog.
DescrStatic->Caption = _("{AppName} cannot handle this magnet link because there are no matching networks.");
return;
}
}
//---------------------------------------------------------------------------
__fastcall TMagnetForm::~TMagnetForm()
{
delete Magnet;
delete Hashes;
}
//---------------------------------------------------------------------------
void __fastcall TMagnetForm::SearchBtnClick(TObject *Sender)
{
int StartedSearches = 0;
Engine->GetNetworks()->Lock();
// loop through all hashes in the magnet
for(THashSet::Iterator hitr=Hashes->Begin(); hitr != Hashes->End(); ++hitr) {
// collect all selected networks for this hash
string Nets;
for(int i=0; i<NetworksListView->Items->Count; i++) {
TNetwork* Network = Engine->GetNetworks()->GetNetwork(NetworksListView->Items->Item[i]->Caption.c_str());
if(!NetworksListView->Items->Item[i]->Checked || !Network)
continue;
THashAlgo* Algo = Network->GetAlgos()->GetAlgo((*hitr)->GetName());
if(Algo && Algo->Equals((*hitr)->GetAlgo()))
Nets += Network->GetName() + " ";
}
// run search if there was at least one network selected
if(Nets != "") {
SearchForm->RunSearch((*hitr)->GetGiftHash().c_str(), SRSource, Nets.c_str());
StartedSearches++;
}
}
Engine->GetNetworks()->Release();
// if there where no hash searches try to search for keywords
if(StartedSearches == 0 && Magnet->GetKeywordTopic() != "") {
string Keywords = Magnet->GetKeywordTopic();
// sanitize keywords
if(Keywords.length() > 100)
Keywords.resize(100);
TSearchRealm Realm = SRAny;
if(Magnet->GetMediaType() == "audio") Realm = SRAudio;
else if(Magnet->GetMediaType() == "video") Realm = SRVideo;
else if(Magnet->GetMediaType() == "image") Realm = SRImage;
else if(Magnet->GetMediaType() == "text") Realm = SRDocument;
// collect networks to search on
string Nets;
for(int i=0; i<NetworksListView->Items->Count; i++) {
TNetwork* Network = Engine->GetNetworks()->GetNetwork(NetworksListView->Items->Item[i]->Caption.c_str());
if(Network && NetworksListView->Items->Item[i]->Checked)
Nets += Network->GetName() + " ";
}
SearchForm->RunSearch(Keywords.c_str(), Realm, Nets.c_str());
StartedSearches++;
}
// switch to search page if a search was started
if(StartedSearches > 0)
MainForm->SetVisiblePage(SearchForm);
ModalResult = mrOk;
}
//---------------------------------------------------------------------------
void __fastcall TMagnetForm::DownloadBtnClick(TObject *Sender)
{
string FileName = FileNameEdit->Text.c_str();
if(FileName == "") {
DescrStatic->Font->Color = clRed;
DescrStatic->Font->Style = TFontStyles() << fsBold;
// TRANSLATOR: Error in Magnet dialog when user didn't specify filename for download.
DescrStatic->Caption = _("You need to specify a name for the file!");
return;
}
// start downloads for checked networks
for(int i=0; i<NetworksListView->Items->Count; i++) {
if(!NetworksListView->Items->Item[i]->Checked)
continue;
string Network = NetworksListView->Items->Item[i]->Caption.c_str();
// start download
Engine->NewDownload(Hashes,Network,FileName);
}
ModalResult = mrOk;
}
//---------------------------------------------------------------------------
void __fastcall TMagnetForm::StreamBtnClick(TObject *Sender)
{
// TODO: don't forget to switch pages
if(StreamUrl == "")
return;
// use media player to stream
PlayerForm->PlayStream(StreamUrl);
// switch page if video
if(Magnet->GetMediaType() == "video")
MainForm->SetVisiblePage(PlayerForm);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -