summaryrefslogtreecommitdiffstats
path: root/apps/ktorrent/fileselectdlg.cpp
blob: a6dec2784556d738c88724913b7ac7d5fb0a2acc (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
/***************************************************************************
 *   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 <tdelocale.h>
#include <tdemessagebox.h>
#include <tdelistview.h>
#include <kstdguiitem.h>
#include <kpushbutton.h>
#include <kurlrequester.h>
#include <kiconloader.h>
#include <kmimetype.h>
#include <kurl.h>

#include <tqlabel.h>
#include <tqstring.h>
#include <tqbuttongroup.h>
#include <tqcombobox.h>
#include <tqcheckbox.h>
#include <tqdir.h>

#include <interfaces/torrentfileinterface.h>
#include <interfaces/torrentinterface.h>
#include "fileselectdlg.h"
#include <interfaces/filetreediritem.h>
#include <interfaces/filetreeitem.h>
#include <interfaces/functions.h>
#include <settings.h>
#include <util/functions.h>
#include <util/fileops.h>

#include <groups/group.h>
#include <groups/groupmanager.h>

using namespace kt;

FileSelectDlg::FileSelectDlg(GroupManager* gm, bool* user, bool* start, TQWidget* parent, const char* name, bool modal, WFlags fl)
		: FileSelectDlgBase(parent, name, modal, fl), m_gman(gm), m_user(user), m_start(start)
{
	root = 0;
	connect(m_select_all, TQ_SIGNAL(clicked()), this, TQ_SLOT(selectAll()));
	connect(m_select_none, TQ_SIGNAL(clicked()), this, TQ_SLOT(selectNone()));
	connect(m_invert_selection, TQ_SIGNAL(clicked()), this, TQ_SLOT(invertSelection()));
	connect(m_ok, TQ_SIGNAL(clicked()), this, TQ_SLOT(accept()));
	connect(m_cancel, TQ_SIGNAL(clicked()), this, TQ_SLOT(reject()));
	connect(m_downloadLocation, TQ_SIGNAL(textChanged (const TQString &)), this, TQ_SLOT(updateSizeLabels()));
	
	m_downloadLocation->setMode(KFile::Directory);
}

FileSelectDlg::~FileSelectDlg()

{}

int FileSelectDlg::execute(kt::TorrentInterface* tc)
{
	this->tc = tc;

	if (tc)
	{
		populateFields();
		
		if(tc->getStats().multi_file_torrent)
			setupMultifileTorrent();
		else
			setupSinglefileTorrent();		
		
		return exec();
	}

	return TQDialog::Rejected;
}

void FileSelectDlg::reject()
{
	TQDialog::reject();
}

void FileSelectDlg::accept()
{
	TQStringList pe_ex;
	
	TQString dn = m_downloadLocation->url();
	if (!dn.endsWith(bt::DirSeparator()))
		dn += bt::DirSeparator();

	for (Uint32 i = 0;i < tc->getNumFiles();i++)
	{
		kt::TorrentFileInterface & file = tc->getTorrentFile(i);

		// check for preexsting files
		TQString path = dn + tc->getStats().torrent_name + bt::DirSeparator() + file.getPath();
		if (bt::Exists(path))
			file.setPreExisting(true);
		
		if (file.doNotDownload() && file.isPreExistingFile())
		{
			// we have excluded a preexsting file
			pe_ex.append(file.getPath());
		}
	}

	if (pe_ex.count() > 0)
	{
		TQString msg = i18n("You have deselected the following existing files. "
						   "You will lose all data in these files, are you sure you want to do this ?");
		// better ask the user if (s)he wants to delete the already existing data
		int ret = KMessageBox::warningYesNoList(0, msg, pe_ex, TQString(),
												KGuiItem(i18n("Yes, delete the files")),
												KGuiItem(i18n("No, keep the files")));

		if (ret == KMessageBox::No)
		{
			for (Uint32 i = 0;i < tc->getNumFiles();i++)
			{
				kt::TorrentFileInterface & file = tc->getTorrentFile(i);

				if (file.doNotDownload() && file.isPreExistingFile())
					file.setDoNotDownload(false);
			}
		}
	}

	for (Uint32 i = 0;i < tc->getNumFiles();i++)
	{
		kt::TorrentFileInterface & file = tc->getTorrentFile(i);
		file.setEmitDownloadStatusChanged(true);
		// we don't need to emit the downloadStatusChanged signal, 
		// because tc->createFiles() in ktorrentcore.cpp will take care of everything
	}
	
	//Setup custom download location
	TQString ddir = tc->getDataDir();
	if (!ddir.endsWith(bt::DirSeparator()))
		ddir += bt::DirSeparator();
	
	if (dn != ddir)	// only change when absolutely necessary
		tc->changeOutputDir(dn, false);
	
	//Make it user controlled if needed
	*m_user = m_chkUserControlled->isChecked();
	*m_start = m_chkUserControlled->isChecked() && m_chkStartTorrent->isChecked();
	
	//Now add torrent to selected group
	if(m_cmbGroups->currentItem() != 0)
	{
		TQString groupName = m_cmbGroups->currentText();
		
		Group* group = m_gman->find(groupName);
		if(group)
		{
			group->addTorrent(tc);	
		}
	}
	
	// update the last save directory
	Settings::setLastSaveDir(dn);
	TQDialog::accept();
}

void FileSelectDlg::selectAll()
{
	if (root)
		root->setAllChecked(true);
}

void FileSelectDlg::selectNone()
{
	if (root)
		root->setAllChecked(false);
}

void FileSelectDlg::invertSelection()
{
	if (root)
		root->invertChecked();
}

void FileSelectDlg::updateSizeLabels()
{
	//calculate free disk space

	KURL sdir = KURL(m_downloadLocation -> url());
	while( sdir.isValid() && sdir.isLocalFile() && (!sdir.isEmpty())  && (! TQDir(sdir.path()).exists()) ) 
	{
		sdir = sdir.upURL();
	}
	
	Uint64 bytes_free = 0;
	if (!FreeDiskSpace(sdir.path(),bytes_free))
	{
		FreeDiskSpace(tc->getDataDir(),bytes_free);
	}
	
	Uint64 bytes_to_download = 0;
	if (root)
		bytes_to_download = root->bytesToDownload();
	else
		bytes_to_download = tc->getStats().total_bytes;

	lblFree->setText(kt::BytesToString(bytes_free));
	lblRequired->setText(kt::BytesToString(bytes_to_download));

	if (bytes_to_download > bytes_free)
		lblStatus->setText("<font color=\"#ff0000\">" + kt::BytesToString(-1*(long long)(bytes_free - bytes_to_download)) + i18n(" short!"));
	else
		lblStatus->setText(kt::BytesToString(bytes_free - bytes_to_download));
}

void FileSelectDlg::treeItemChanged()
{
	updateSizeLabels();
}

void FileSelectDlg::setupMultifileTorrent()
{
	m_file_view->clear();
	root = new kt::FileTreeDirItem(m_file_view, tc->getStats().torrent_name, this);

	for (Uint32 i = 0;i < tc->getNumFiles();i++)
	{
		kt::TorrentFileInterface & file = tc->getTorrentFile(i);
		file.setEmitDownloadStatusChanged(false);
		root->insert(file.getPath(), file);
	}

	root->setOpen(true);
	m_file_view->setRootIsDecorated(true);

	updateSizeLabels();
}

void FileSelectDlg::setupSinglefileTorrent()
{
	m_file_view->clear();
	TDEListViewItem* single_root = new TDEListViewItem(m_file_view);
	single_root->setText(0,tc->getStats().torrent_name);
	single_root->setText(1,BytesToString(tc->getStats().total_bytes));
	single_root->setText(2,i18n("Yes"));
	single_root->setPixmap(0,KMimeType::findByPath(tc->getStats().torrent_name)->pixmap(TDEIcon::Small));
	root = 0;
	updateSizeLabels();
	m_select_all->setEnabled(false);
	m_select_none->setEnabled(false);
	m_invert_selection->setEnabled(false);
}

void FileSelectDlg::populateFields()
{
	TQString dir = Settings::saveDir();
	if (!Settings::useSaveDir() || dir.isNull())
	{
		dir = Settings::lastSaveDir();
		if (dir.isNull())
			dir = TQDir::homeDirPath();
	}
	
	m_downloadLocation->setURL(dir);
	loadGroups();
}

void FileSelectDlg::loadGroups()
{
	GroupManager::iterator it = m_gman->begin();
	
	TQStringList grps;
	
	//First default group
	grps << i18n("All Torrents");
	
	//now custom ones
	while(it != m_gman->end())
	{
		grps << it->first;		
		++it;
	}
	
	m_cmbGroups->insertStringList(grps);
}

#include "fileselectdlg.moc"