From 9ad5c7b5e23b4940e7a3ea3ca3a6fb77e6a8fab0 Mon Sep 17 00:00:00 2001 From: tpearson Date: Wed, 20 Jan 2010 02:37:40 +0000 Subject: Updated to final KDE3 ktorrent release (2.2.6) git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/ktorrent@1077377 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- plugins/partfileimport/Makefile.am | 27 ++ plugins/partfileimport/importdialog.cpp | 389 +++++++++++++++++++++ plugins/partfileimport/importdialog.h | 78 +++++ plugins/partfileimport/importdlgbase.ui | 163 +++++++++ .../partfileimport/ktpartfileimportplugin.desktop | 25 ++ plugins/partfileimport/ktpartfileimportpluginui.rc | 8 + plugins/partfileimport/partfileimportplugin.cpp | 78 +++++ plugins/partfileimport/partfileimportplugin.h | 52 +++ 8 files changed, 820 insertions(+) create mode 100644 plugins/partfileimport/Makefile.am create mode 100644 plugins/partfileimport/importdialog.cpp create mode 100644 plugins/partfileimport/importdialog.h create mode 100644 plugins/partfileimport/importdlgbase.ui create mode 100644 plugins/partfileimport/ktpartfileimportplugin.desktop create mode 100644 plugins/partfileimport/ktpartfileimportpluginui.rc create mode 100644 plugins/partfileimport/partfileimportplugin.cpp create mode 100644 plugins/partfileimport/partfileimportplugin.h (limited to 'plugins/partfileimport') diff --git a/plugins/partfileimport/Makefile.am b/plugins/partfileimport/Makefile.am new file mode 100644 index 0000000..f5aca48 --- /dev/null +++ b/plugins/partfileimport/Makefile.am @@ -0,0 +1,27 @@ +INCLUDES = -I$(top_builddir)/apps/ktorrent -I$(srcdir)/../../libktorrent \ + $(all_includes) +METASOURCES = AUTO +kde_module_LTLIBRARIES = ktpartfileimportplugin.la +noinst_HEADERS = partfileimportplugin.h importdialog.h +ktpartfileimportplugin_la_SOURCES = partfileimportplugin.cpp importdlgbase.ui importdialog.cpp + +# Libs needed by the plugin +ktpartfileimportplugin_la_LIBADD = $(LIB_KHTML) $(LIB_KPARTS) \ + ../../libktorrent/libktorrent.la \ + $(LIB_QT) $(LIB_KDECORE) $(LIB_KDEUI) $(LIB_KFILE) + +# LD flags for the plugin +# -module says: this is a module, i.e. something you're going to dlopen +# so e.g. it has no version number like a normal shared lib would have. +ktpartfileimportplugin_la_LDFLAGS = -module $(KDE_PLUGIN) $(all_libraries) + +# rc file containing the GUI for the plugin +rcdir = $(kde_datadir)/ktorrent +rc_DATA = ktpartfileimportpluginui.rc + +# Install the desktop file needed to detect the plugin +kde_services_DATA = ktpartfileimportplugin.desktop + +# kde_kcfg_DATA = ktpartfileimportplugin.kcfg + +KDE_CXXFLAGS = $(USE_EXCEPTIONS) $(USE_RTTI) diff --git a/plugins/partfileimport/importdialog.cpp b/plugins/partfileimport/importdialog.cpp new file mode 100644 index 0000000..22f9a4b --- /dev/null +++ b/plugins/partfileimport/importdialog.cpp @@ -0,0 +1,389 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.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. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "importdialog.h" +#include +#include + +using namespace bt; + +namespace kt +{ + ImportDialog::ImportDialog(CoreInterface* core,QWidget* parent, const char* name, bool modal, WFlags fl) + : ImportDlgBase(parent,name, modal,fl),DataCheckerListener(false),core(core) + { + KURLRequester* r = m_torrent_url; + r->setMode(KFile::File|KFile::LocalOnly); + r->setFilter("*.torrent|" + i18n("Torrent files") + "\n*|" + i18n("All files")); + + r = m_data_url; + r->setMode(KFile::File|KFile::Directory|KFile::LocalOnly); + + connect(m_import_btn,SIGNAL(clicked()),this,SLOT(onImport())); + connect(m_cancel_btn,SIGNAL(clicked()),this,SLOT(reject())); + m_progress->setEnabled(false); + } + + ImportDialog::~ImportDialog() + {} + + void ImportDialog::progress(Uint32 num,Uint32 total) + { + m_progress->setTotalSteps(total); + m_progress->setProgress(num); + } + + void ImportDialog::status(Uint32 ,Uint32 ) + { + // don't care + } + + void ImportDialog::finished() + { + // only used for check in separate thread, so does not apply for the import plugin + } + + void ImportDialog::import(Torrent & tor) + { + // get the urls + KURL tor_url = KURL::fromPathOrURL(m_torrent_url->url()); + KURL data_url = KURL::fromPathOrURL(m_data_url->url()); + + // now we need to check the data + DataChecker* dc = 0; + if (tor.isMultiFile()) + dc = new MultiDataChecker(); + else + dc = new SingleDataChecker(); + + try + { + dc->setListener(this); + dc->check(data_url.path(),tor,QString::null); + } + catch (Error & e) + { + delete dc; + KMessageBox::error(this,i18n("Cannot verify data : %1").arg(e.toString()),i18n("Error")); + reject(); + return; + } + + // find a new torrent dir and make it if necessary + QString tor_dir = core->findNewTorrentDir(); + if (!tor_dir.endsWith(bt::DirSeparator())) + tor_dir += bt::DirSeparator(); + + try + { + if (!bt::Exists(tor_dir)) + bt::MakeDir(tor_dir); + + // write the index file + writeIndex(tor_dir + "index",dc->getDownloaded()); + + // copy the torrent file + bt::CopyFile(tor_url.prettyURL(),tor_dir + "torrent"); + + Uint64 imported = calcImportedBytes(dc->getDownloaded(),tor); + + // make the cache + if (tor.isMultiFile()) + { + QValueList dnd_files; + bool dnd = false; + // first make tor_dir/cache/ + QString cache_dir = tor_dir + "cache" + bt::DirSeparator(); + QString dnd_dir = tor_dir + "dnd" + bt::DirSeparator(); + if (!bt::Exists(cache_dir)) + MakeDir(cache_dir); + if (!bt::Exists(dnd_dir)) + MakeDir(dnd_dir); + + + // make all sub symlinks + for (Uint32 i = 0;i < tor.getNumFiles();i++) + { + linkTorFile(cache_dir,dnd_dir,data_url,tor.getFile(i).getPath(),dnd); + if (dnd) + dnd_files.append(i); + dnd = false; + } + + QString durl = data_url.path(); + if (durl.endsWith(bt::DirSeparator())) + durl = durl.left(durl.length() - 1); + int ds = durl.findRev(bt::DirSeparator()); + if (durl.mid(ds+1) == tor.getNameSuggestion()) + { + durl = durl.left(ds); + saveStats(tor_dir + "stats",KURL::fromPathOrURL(durl),imported,false); + } + else + { + saveStats(tor_dir + "stats",KURL::fromPathOrURL(durl),imported,true); + } + saveFileInfo(tor_dir + "file_info",dnd_files); + } + else + { + // single file, just symlink the data_url to tor_dir/cache + bt::SymLink(data_url.path(),tor_dir + "cache"); + QString durl = data_url.path(); + int ds = durl.findRev(bt::DirSeparator()); + durl = durl.left(ds); + saveStats(tor_dir + "stats",durl,imported,false); + } + + // everything went OK, so load the whole shabang and start downloading + core->loadExistingTorrent(tor_dir); + } + catch (Error & e) + { + // delete tor_dir + bt::Delete(tor_dir,true); + delete dc; + KMessageBox::error(this,e.toString(),i18n("Error")); + reject(); + return; + } + + delete dc; + accept(); + } + + void ImportDialog::onTorrentGetReult(KIO::Job* j) + { + if (j->error()) + { + j->showErrorDialog(this); + reject(); + } + else + { + KIO::StoredTransferJob* stj = (KIO::StoredTransferJob*)j; + Torrent tor; + + // try to load the torrent + try + { + tor.load(stj->data(),false); + } + catch (Error & e) + { + KMessageBox::error(this,i18n("Cannot load the torrent file : %1").arg(e.toString()), + i18n("Error")); + reject(); + return; + } + import(tor); + } + } + + void ImportDialog::onImport() + { + m_progress->setEnabled(true); + m_import_btn->setEnabled(false); + m_cancel_btn->setEnabled(false); + m_torrent_url->setEnabled(false); + m_data_url->setEnabled(false); + + KURL tor_url = KURL::fromPathOrURL(m_torrent_url->url()); + if (!tor_url.isLocalFile()) + { + // download the torrent file + KIO::StoredTransferJob* j = KIO::storedGet(tor_url); + connect(j,SIGNAL(result(KIO::Job* )),this,SLOT(onTorrentGetReult(KIO::Job*))); + } + else + { + KURL tor_url = KURL::fromPathOrURL(m_torrent_url->url()); + Torrent tor; + + // try to load the torrent + try + { + tor.load(tor_url.path(),false); + } + catch (Error & e) + { + KMessageBox::error(this,i18n("Cannot load the torrent file : %1").arg(e.toString()), + i18n("Error")); + reject(); + return; + } + import(tor); + } + } + + void ImportDialog::writeIndex(const QString & file,const BitSet & chunks) + { + // first try to open it + File fptr; + if (!fptr.open(file,"wb")) + throw Error(i18n("Cannot open %1 : %2").arg(file).arg(fptr.errorString())); + + // write all chunks to the file + for (Uint32 i = 0;i < chunks.getNumBits();i++) + { + if (!chunks.get(i)) + continue; + + // we have the chunk so write a NewChunkHeader struct to the file + NewChunkHeader hdr; + hdr.index = i; + hdr.deprecated = 0; + fptr.write(&hdr,sizeof(NewChunkHeader)); + } + } + + void ImportDialog::linkTorFile(const QString & cache_dir,const QString & dnd_dir, + const KURL & data_url,const QString & fpath,bool & dnd) + { + QStringList sl = QStringList::split(bt::DirSeparator(),fpath); + + // create all necessary subdirs + QString ctmp = cache_dir; + QString otmp = data_url.path(); + if (!otmp.endsWith(bt::DirSeparator())) + otmp += bt::DirSeparator(); + + QString dtmp = dnd_dir; + for (Uint32 i = 0;i < sl.count() - 1;i++) + { + otmp += sl[i]; + ctmp += sl[i]; + dtmp += sl[i]; + // we need to make the same directory structure in the cache + // as the output dir + if (!bt::Exists(ctmp)) + MakeDir(ctmp); + if (!bt::Exists(otmp)) + MakeDir(otmp); + if (!bt::Exists(dtmp)) + MakeDir(dtmp); + otmp += bt::DirSeparator(); + ctmp += bt::DirSeparator(); + dtmp += bt::DirSeparator(); + } + + QString dfile = otmp + sl.last(); + if (!bt::Exists(dfile)) + { + // when we start the torrent the user will be asked what to do + // bt::SymLink(dfile,cache_dir + fpath); + dnd = false; + } + else + { + // just symlink the existing file + bt::SymLink(dfile,cache_dir + fpath); + dnd = false; + } + } + + void ImportDialog::saveStats(const QString & stats_file,const KURL & data_url,Uint64 imported,bool custom_output_name) + { + QFile fptr(stats_file); + if (!fptr.open(IO_WriteOnly)) + { + Out(SYS_PFI|LOG_IMPORTANT) << "Warning : can't create stats file" << endl; + return; + } + + QTextStream out(&fptr); + out << "OUTPUTDIR=" << data_url.path() << ::endl; + out << "UPLOADED=0" << ::endl; + out << "RUNNING_TIME_DL=0" << ::endl; + out << "RUNNING_TIME_UL=0" << ::endl; + out << "PRIORITY=0" << ::endl; + out << "AUTOSTART=1" << ::endl; + if (core->getGlobalMaxShareRatio() > 0) + out << QString("MAX_RATIO=%1").arg(core->getGlobalMaxShareRatio(),0,'f',2) << ::endl; + out << QString("IMPORTED=%1").arg(imported) << ::endl; + if (custom_output_name) + out << "CUSTOM_OUTPUT_NAME=1" << endl; + } + + Uint64 ImportDialog::calcImportedBytes(const bt::BitSet & chunks,const Torrent & tor) + { + Uint64 nb = 0; + Uint64 ls = tor.getFileLength() % tor.getChunkSize(); + if (ls == 0) + ls = tor.getChunkSize(); + + for (Uint32 i = 0;i < chunks.getNumBits();i++) + { + if (!chunks.get(i)) + continue; + + if (i == chunks.getNumBits() - 1) + nb += ls; + else + nb += tor.getChunkSize(); + } + return nb; + } + + void ImportDialog::saveFileInfo(const QString & file_info_file,QValueList & dnd) + { + // saves which TorrentFile's do not need to be downloaded + File fptr; + if (!fptr.open(file_info_file,"wb")) + { + Out(SYS_PFI|LOG_IMPORTANT) << "Warning : Can't save chunk_info file : " << fptr.errorString() << endl; + return; + } + + ; + + // first write the number of excluded ones + Uint32 tmp = dnd.count(); + fptr.write(&tmp,sizeof(Uint32)); + // then all the excluded ones + for (Uint32 i = 0;i < dnd.count();i++) + { + tmp = dnd[i]; + fptr.write(&tmp,sizeof(Uint32)); + } + fptr.flush(); + } +} + + + +#include "importdialog.moc" + diff --git a/plugins/partfileimport/importdialog.h b/plugins/partfileimport/importdialog.h new file mode 100644 index 0000000..b7617fc --- /dev/null +++ b/plugins/partfileimport/importdialog.h @@ -0,0 +1,78 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.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. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef IMPORTDIALOG_H +#define IMPORTDIALOG_H + +#include +#include +#include "importdlgbase.h" + +class KURL; + +namespace bt +{ + class BitSet; + class Torrent; +} + +namespace KIO +{ + class Job; +} + + +namespace kt +{ + class CoreInterface; + + class ImportDialog : public ImportDlgBase,public bt::DataCheckerListener + { + Q_OBJECT + + public: + ImportDialog(CoreInterface* core,QWidget* parent = 0, const char* name = 0, bool modal = FALSE, WFlags fl = 0 ); + virtual ~ImportDialog(); + + public slots: + void onImport(); + void onTorrentGetReult(KIO::Job* j); + + private: + void writeIndex(const QString & file,const bt::BitSet & chunks); + void linkTorFile(const QString & cache_dir,const QString & dnd_dir, + const KURL & data_url,const QString & fpath,bool & dnd); + void saveStats(const QString & stats_file,const KURL & data_url,bt::Uint64 imported,bool custom_output_name); + bt::Uint64 calcImportedBytes(const bt::BitSet & chunks,const bt::Torrent & tor); + void saveFileInfo(const QString & file_info_file,QValueList & dnd); + + virtual void progress(bt::Uint32 num,bt::Uint32 total); + virtual void status(bt::Uint32 num_failed,bt::Uint32 num_downloaded); + virtual void finished(); + + void import(bt::Torrent & tor); + + private: + CoreInterface* core; + }; +} + +#endif + diff --git a/plugins/partfileimport/importdlgbase.ui b/plugins/partfileimport/importdlgbase.ui new file mode 100644 index 0000000..e1cdd64 --- /dev/null +++ b/plugins/partfileimport/importdlgbase.ui @@ -0,0 +1,163 @@ + +ImportDlgBase + + + ImportDlgBase + + + + 0 + 0 + 473 + 196 + + + + Import an existing download + + + + unnamed + + + + layout6 + + + + unnamed + + + + layout5 + + + + unnamed + + + + textLabel1 + + + + 60 + 0 + + + + Torrent: + + + + + textLabel2 + + + + 60 + 0 + + + + Data: + + + + + + + layout4 + + + + unnamed + + + + m_torrent_url + + + + + m_data_url + + + + + + + + + kActiveLabel1 + + + Please specify the torrent and the data already downloaded for that torrent. + + + + + m_progress + + + + + layout3 + + + + unnamed + + + + spacer1 + + + Horizontal + + + Expanding + + + + 61 + 20 + + + + + + m_import_btn + + + &Import + + + + + m_cancel_btn + + + Ca&ncel + + + + + + + + + + + kurlrequester.h + klineedit.h + kpushbutton.h + kurlrequester.h + klineedit.h + kpushbutton.h + kactivelabel.h + kprogress.h + kpushbutton.h + kpushbutton.h + + diff --git a/plugins/partfileimport/ktpartfileimportplugin.desktop b/plugins/partfileimport/ktpartfileimportplugin.desktop new file mode 100644 index 0000000..5b28fe0 --- /dev/null +++ b/plugins/partfileimport/ktpartfileimportplugin.desktop @@ -0,0 +1,25 @@ +[Desktop Entry] +Name=PartialFileImportPlugin +Name[bg]=Приставка за частично импортиране на файлове +Name[de]=Teildatei-Import-Modul +Name[el]=Πρόσθετο εισαγωγής μερικής λήψης +Name[es]=Importación de ficheros parciales +Name[et]=Osalise faili impordi plugin +Name[it]=Plugin importazione file parziali +Name[nb]=Filimportmodul +Name[nds]=Importmoduul för Deeldateien +Name[nl]=Import gedeeltelijke bestanden +Name[pl]=Wtyczka importu części plików +Name[pt]=Importação de Ficheiros Parciais +Name[pt_BR]=Plugin de Importação Parcial de Arquivo +Name[sk]=PartialFileImport Plugin +Name[sr]=Прикључак за увоз недовршених фајлова +Name[sr@Latn]=Priključak za uvoz nedovršenih fajlova +Name[sv]=Insticksprogram för partiell filimport +Name[tr]=Tamamlanmamış Dosya Alma Eklentisi +Name[xx]=xxPartialFileImportPluginxx +Name[zh_CN]=部分文件导入插件 +Name[zh_TW]=PartialFileImport外掛程式 +ServiceTypes=KTorrent/Plugin +Type=Service +X-KDE-Library=ktpartfileimportplugin diff --git a/plugins/partfileimport/ktpartfileimportpluginui.rc b/plugins/partfileimport/ktpartfileimportpluginui.rc new file mode 100644 index 0000000..f8867e3 --- /dev/null +++ b/plugins/partfileimport/ktpartfileimportpluginui.rc @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/plugins/partfileimport/partfileimportplugin.cpp b/plugins/partfileimport/partfileimportplugin.cpp new file mode 100644 index 0000000..c154478 --- /dev/null +++ b/plugins/partfileimport/partfileimportplugin.cpp @@ -0,0 +1,78 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.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. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include "partfileimportplugin.h" +#include "importdialog.h" + +#define NAME "Import" +#define AUTHOR "Joris Guisson" +#define EMAIL "joris.guisson@gmail.com" + + + +K_EXPORT_COMPONENT_FACTORY(ktpartfileimportplugin,KGenericFactory("ktpartfileimportplugin")) + +namespace kt +{ + + PartFileImportPlugin::PartFileImportPlugin(QObject* parent, const char* name, const QStringList& args) + : Plugin(parent, name, args,NAME,i18n("Import"),AUTHOR,EMAIL,i18n("Imports partially or fully downloaded torrents from other clients"),"ktplugins") + { + setXMLFile("ktpartfileimportpluginui.rc"); + import_action = 0; + } + + + PartFileImportPlugin::~PartFileImportPlugin() + {} + + + void PartFileImportPlugin::load() + { + import_action = new KAction(i18n("Import existing download" ), 0, this, + SLOT(onImport()), actionCollection(), "partfileimport" ); + } + + void PartFileImportPlugin::unload() + { + delete import_action; + import_action = 0; + } + + void PartFileImportPlugin::onImport() + { + ImportDialog dlg(getCore(),0,0,true); + dlg.exec(); + } + + bool PartFileImportPlugin::versionCheck(const QString & version) const + { + return version == KT_VERSION_MACRO; + } + +} +#include "partfileimportplugin.moc" diff --git a/plugins/partfileimport/partfileimportplugin.h b/plugins/partfileimport/partfileimportplugin.h new file mode 100644 index 0000000..31f2d3a --- /dev/null +++ b/plugins/partfileimport/partfileimportplugin.h @@ -0,0 +1,52 @@ +/*************************************************************************** + * Copyright (C) 2005 by Joris Guisson * + * joris.guisson@gmail.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. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef KTPARTFILEIMPORTPLUGIN_H +#define KTPARTFILEIMPORTPLUGIN_H + +#include + +class KAction; + +namespace kt +{ + + /** + @author Joris Guisson + */ + class PartFileImportPlugin : public Plugin + { + Q_OBJECT + public: + PartFileImportPlugin(QObject* parent, const char* name, const QStringList& args); + virtual ~PartFileImportPlugin(); + + virtual void load(); + virtual void unload(); + virtual bool versionCheck(const QString& version) const; + public slots: + void onImport(); + + private: + KAction* import_action; + }; + +} + +#endif -- cgit v1.2.1