summaryrefslogtreecommitdiffstats
path: root/plugins/partfileimport/importdialog.cpp
blob: ba43ac9ba013d013afc0c4cb93c1b7030181270a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
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 <kurl.h>
#include <klocale.h>
#include <kprogress.h>
#include <kurlrequester.h>
#include <kpushbutton.h>
#include <kmessagebox.h>
#include <kio/job.h>
#include <kio/jobclasses.h>
#include <util/log.h>
#include <util/error.h>
#include <util/file.h>
#include <util/fileops.h>
#include <util/functions.h>
#include <torrent/globals.h>
#include <torrent/torrent.h>
#include <torrent/chunkmanager.h>
#include <interfaces/coreinterface.h>
#include "importdialog.h"
#include <datachecker/singledatachecker.h>
#include <datachecker/multidatachecker.h>

using namespace bt;

namespace kt
{
	ImportDialog::ImportDialog(CoreInterface* core,TQWidget* 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,TQT_SIGNAL(clicked()),this,TQT_SLOT(onImport()));
		connect(m_cancel_btn,TQT_SIGNAL(clicked()),this,TQT_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,TQString());
		}
		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
		TQString 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())
			{
				TQValueList<Uint32> dnd_files;
				bool dnd = false;
				// first make tor_dir/cache/
				TQString cache_dir = tor_dir + "cache" + bt::DirSeparator();
				TQString 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;
				}
				
				TQString 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");
				TQString 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,TQT_SIGNAL(result(KIO::Job* )),this,TQT_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 TQString & 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 TQString & cache_dir,const TQString & dnd_dir,
								   const KURL & data_url,const TQString & fpath,bool & dnd)
	{
		TQStringList sl = TQStringList::split(bt::DirSeparator(),fpath);

		// create all necessary subdirs
		TQString ctmp = cache_dir;
		TQString otmp = data_url.path();
		if (!otmp.endsWith(bt::DirSeparator()))
			otmp += bt::DirSeparator();
		
		TQString 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();
		}

		TQString 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 TQString & stats_file,const KURL & data_url,Uint64 imported,bool custom_output_name)
	{
		TQFile fptr(stats_file);
		if (!fptr.open(IO_WriteOnly))
		{
			Out(SYS_PFI|LOG_IMPORTANT) << "Warning : can't create stats file" << endl;
			return;
		}

		TQTextStream 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 << TQString("MAX_RATIO=%1").arg(core->getGlobalMaxShareRatio(),0,'f',2) << ::endl;
		out << TQString("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 TQString & file_info_file,TQValueList<Uint32> & 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"