You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

139 lines
3.1 KiB

#include "magnetize.h"
MainWindow::MainWindow()
{
filename=new QLineEdit;
filename->setMinimumWidth(400);
filenamelabel=new QLabel("File name:");
magnet=new QLineEdit;
magnet->setReadOnly(true);
magnet->setMinimumWidth(500);
magnetlabel=new QLabel("Magnet link:");
quitButton=new QPushButton("Quit");
openButton=new QPushButton("Open");
genButton=new QPushButton("Generate");
copyButton=new QPushButton("Copy");
copyButton->setEnabled(false);
copyButton->setMinimumHeight(40);
connect(quitButton,SIGNAL(clicked()),this,SLOT(quit()));
connect(openButton,SIGNAL(clicked()),this,SLOT(openfile()));
connect(genButton,SIGNAL(clicked()),this,SLOT(generate()));
connect(copyButton,SIGNAL(clicked()),this,SLOT(copy()));
QVBoxLayout* mainlayout=new QVBoxLayout;
QHBoxLayout* filenamelayout=new QHBoxLayout;
QHBoxLayout* magnetlayout=new QHBoxLayout;
filenamelayout->addWidget(filenamelabel);
filenamelayout->addWidget(filename);
filenamelayout->addWidget(openButton);
magnetlayout->addWidget(magnetlabel);
magnetlayout->addWidget(magnet);
mainlayout->addLayout(filenamelayout);
mainlayout->addWidget(genButton);
mainlayout->addLayout(magnetlayout);
mainlayout->addWidget(copyButton);
mainlayout->addWidget(quitButton);
setLayout(mainlayout);
setWindowTitle("Generate magnet link");
}
void MainWindow::quit()
{
qApp->quit();
}
void MainWindow::openfile()
{
QString fname=QFileDialog::getOpenFileName(this,"Open file","","Torrent files (*.torrent)");
setmagnet(fname);
}
void MainWindow::generate()
{
QString fname=filename->text();
setmagnet(fname);
}
void MainWindow::copy()
{
QString link=magnet->text();
qApp->clipboard()->setText(link);
}
void MainWindow::setmagnet(const QString& fname)
{
if(fname=="")
{
magnet->setText("");
copyButton->setEnabled(false);
return;
}
QString hash;
int ret;
bool priv;
filename->setText(fname);
ret=infohash(fname.toLocal8Bit().data(),hash,priv);
if(ret)
{
magnet->setText("Error!");
copyButton->setEnabled(false);
}
else
{
magnet->setText("magnet:?xt=urn:btih:"+hash);
if(priv) copyButton->setEnabled(false);
else copyButton->setEnabled(true);
}
}
void Byte2Hex(unsigned char ch, char* out)
{
static const char cnv[]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
out[0]=cnv[(ch&(15<<4))>>4];
out[1]=cnv[ch&15];
}
int infohash(const char* file, QString& Qinfohash, bool& priv)
{
int fd;
struct stat st;
off_t size;
void* map;
libtorrent::error_code ec;
char s[3];
Qinfohash="";
libtorrent::sha1_hash infohash;
unsigned char* i;
fd=open(file,O_RDONLY);
if(fd==-1) return 1;
if(fstat(fd,&st)) {close(fd); return 1;}
size=st.st_size;
map=mmap(0,size,PROT_READ,MAP_SHARED,fd,0);
if(map==(void*)-1) {close(fd); return 1;}
libtorrent::torrent_info tinfo((const char*)map,size,ec);
if(ec) {munmap(map,size); close(fd); return 1;}
infohash=tinfo.info_hash();
priv=tinfo.priv();
s[2]=0;
for(i=infohash.begin();i!=infohash.end();i++)
{
Byte2Hex(*i,s);
Qinfohash+=s;
}
munmap(map,size);
close(fd);
return 0;
}
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow w;
w.show();
return app.exec();
}