commit 0d0701e01f32bb31f21f3e2bab115d4d39000a5d Author: Alex 'AdUser' Z Date: Mon Jul 7 16:37:19 2014 +1100 + initial diff --git a/magnetize.cpp b/magnetize.cpp new file mode 100644 index 0000000..510021d --- /dev/null +++ b/magnetize.cpp @@ -0,0 +1,139 @@ +#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(); +} \ No newline at end of file diff --git a/magnetize.h b/magnetize.h new file mode 100644 index 0000000..60eca96 --- /dev/null +++ b/magnetize.h @@ -0,0 +1,34 @@ +#include +//#define BOOST_FILESYSTEM_NARROW_ONLY +#include +#include +#include +#include +#include +#include +#include + +class MainWindow:public QWidget +{ + Q_OBJECT + + public: + MainWindow(); +private slots: + void quit(); + void openfile(); + void copy(); + void generate(); +private: + void setmagnet(const QString& fname); + QLineEdit *filename; + QPushButton *quitButton; + QLabel *filenamelabel; + QPushButton *openButton; + QLineEdit *magnet; + QLabel* magnetlabel; + QPushButton* genButton; + QPushButton* copyButton; +}; + +int infohash(const char* file, QString& Qinfohash, bool& priv); diff --git a/magnetize.pro b/magnetize.pro new file mode 100644 index 0000000..7fff9c6 --- /dev/null +++ b/magnetize.pro @@ -0,0 +1,5 @@ +HEADERS = magnetize.h +SOURCES = magnetize.cpp +LIBS += `pkg-config --libs libtorrent-rasterbar` -lboost_system-mt +QMAKE_CFLAGS += `pkg-config --cflags libtorrent-rasterbar` +QMAKE_CXXFLAGS += `pkg-config --cflags libtorrent-rasterbar` \ No newline at end of file