~vcs-imports/lince/trunk

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
//           LoadSearchedTorrentDialog.cc
//  Thu Nov 22 14:08:15 2007
//  Copyright  2007  Fernando TarĂ­n Morales
//  icemanf@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 3 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 Library 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 "libtorrent/entry.hpp"
#include "libtorrent/bencode.hpp"

#include "Utils.h"
#include "LoadOneTorrentDialog.h"

#include "../config.h"
#include <glibmm/i18n.h>
#include <glib/gstdio.h>

#include "Lince/Utils.h"

#include <fstream>

using namespace Lince;

LoadOneTorrentDialog::LoadOneTorrentDialog(
		const std::string & torrent_path, Glib::ustring *tpathsave,
		std::vector<int> *pfiles, std::vector<std::string> *tmfiles):
	Dialog(_("Open Torrent"), true)
{
	torrentpathsave = tpathsave;
	priorities = pfiles;
	newfilenames = tmfiles;

	Gtk::HBox *torrentNameHBox = Gtk::manage(new Gtk::HBox());
	Gtk::HBox *saveHBox = Gtk::manage(new Gtk::HBox());
	Gtk::Label *saveDirectoryLabel = Gtk::manage(new Gtk::Label(_("Save at: ")));
	Gtk::Label *torrentNameLabel = Gtk::manage(new Gtk::Label(_("Torrent: ")));
	Gtk::Label *torrentName = Gtk::manage(new Gtk::Label());
	Gtk::Image *saveDirectorySelectButtonImage = Gtk::manage(new Gtk::Image());
	Gtk::Button *saveDirectorySelectButton = Gtk::manage(new Gtk::Button());
	Gtk::Frame *torrentFilesFrame = Gtk::manage(new Gtk::Frame());
	Gtk::ScrolledWindow *scrolledWindow = Gtk::manage(new Gtk::ScrolledWindow());

	torrentNameHBox->pack_start(*torrentNameLabel, Gtk::PACK_SHRINK , 5);
	torrentNameHBox->pack_start(*torrentName, Gtk::PACK_SHRINK , 5);

	saveDirectoryEntry.set_text(*tpathsave);

	torrentFilesFrame->set_shadow_type(Gtk::SHADOW_OUT);
	torrentFilesFrame->set_label(_("Torrents' files"));

	saveDirectorySelectButton->signal_clicked().connect( sigc::mem_fun(*this, &LoadOneTorrentDialog::selectAndSetSaveDirectory) );
	dialogTorrentTreeView.signal_button_press_event().connect_notify(sigc::mem_fun(*this,  &LoadOneTorrentDialog::mouse_button_press_event));

	saveDirectorySelectButtonImage->set(Gtk::Stock::DIRECTORY, Gtk::IconSize(1));
	saveDirectorySelectButton->set_image(*saveDirectorySelectButtonImage);

	saveHBox->pack_start(*saveDirectoryLabel, Gtk::PACK_SHRINK , 5);
	saveHBox->pack_start(saveDirectoryEntry, Gtk::PACK_EXPAND_WIDGET, 0);
	saveHBox->pack_start(*saveDirectorySelectButton, Gtk::PACK_SHRINK , 5);

	scrolledWindow->add(dialogTorrentTreeView);
	scrolledWindow->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

	// Create the Tree model:
	refTreeModel = Gtk::ListStore::create(columnsDialogTorrent);
	dialogTorrentTreeView.set_model(refTreeModel);
	dialogTorrentTreeView.set_rules_hint();

	// Add the TreeView's view columnsDialogTorrent:
	dialogTorrentTreeView.append_column_editable(_("Download"), columnsDialogTorrent.m_download);
	dialogTorrentTreeView.append_column(_("Name"), columnsDialogTorrent.m_name);
	dialogTorrentTreeView.append_column(_("Size"), columnsDialogTorrent.m_size);

	// Priority Column
	Gtk::TreeView::Column *prColumn = Gtk::manage(new Gtk::TreeView::Column(_("Priority")) );
	Gtk::CellRendererCombo *pRenderer = Gtk::manage(new Gtk::CellRendererCombo());

	Utils::ModelColumnsIntString pr_Columns;
	pr_refTreeModel = Gtk::ListStore::create(pr_Columns);

	Gtk::TreeModel::Row row;

	prColumn->pack_start(*pRenderer);
	dialogTorrentTreeView.append_column(*prColumn);

	row = *(pr_refTreeModel->append());
	row[pr_Columns.m_col_id] = 1;
  	row[pr_Columns.m_col_name] = _("Normal");

	row = *(pr_refTreeModel->append());
	row[pr_Columns.m_col_id] = 2;
  	row[pr_Columns.m_col_name] = _("High");

	row = *(pr_refTreeModel->append());
	row[pr_Columns.m_col_id] = 3;
  	row[pr_Columns.m_col_name] = _("Maximum");

	prColumn->add_attribute(pRenderer->property_text(), columnsDialogTorrent.m_prio_choosen);
	pRenderer->property_model() = pr_refTreeModel;
	pRenderer->property_editable() = true;
	pRenderer->property_has_entry () = false;
	pRenderer->property_text_column() = 1;

	pRenderer->signal_edited().connect( sigc::mem_fun(*this, &LoadOneTorrentDialog::on_cellrenderer_choice_edited) );
	//End priority Column

	for(guint i = 0; i < 3; i++){
		Gtk::TreeView::Column* pColumn = dialogTorrentTreeView.get_column(i);

		pColumn->set_resizable();
	}
	Gtk::CellRendererToggle *toggleRenderer = dynamic_cast<Gtk::CellRendererToggle *>(dialogTorrentTreeView.get_column_cell_renderer(0));
	toggleRenderer->signal_toggled().connect( sigc::mem_fun(*this, &LoadOneTorrentDialog::downloadFileToggled) );

	m_refActionGroup = Gtk::ActionGroup::create();
	m_refActionGroup->add( Gtk::Action::create("ChangeName", _("Change Name")), sigc::mem_fun(*this, &LoadOneTorrentDialog::changeName) );

	refUIManager = Gtk::UIManager::create();
	refUIManager->insert_action_group(m_refActionGroup);

	Glib::ustring ui_info =
    "<ui>"
    "  <popup name='PopupMenu'>"
	"    <menuitem action='ChangeName'/>"
    "  </popup>"
    "</ui>";

#ifdef GLIBMM_EXCEPTIONS_ENABLED
	try{
		refUIManager->add_ui_from_string(ui_info);
	}
	catch(const Glib::Error& ex){
		std::cerr << "building menus failed: " <<  ex.what();
	}
#else
	std::auto_ptr<Glib::Error> ex;
	refUIManager->add_ui_from_string(ui_info, ex);
	if(ex.get()){
		std::cerr << "building menus failed: " <<  ex->what();
	}
#endif //GLIBMM_EXCEPTIONS_ENABLED

	pMenuPopup = Gtk::manage(dynamic_cast<Gtk::Menu*>(refUIManager->get_widget("/PopupMenu")));
	if(!pMenuPopup)
		g_warning("menu not found");

	torrentFilesFrame->add(*scrolledWindow);

	Gtk::Box *vbox = get_vbox();

	vbox->pack_start(*torrentNameHBox, Gtk::PACK_SHRINK , 5);
	vbox->pack_start(*torrentFilesFrame, Gtk::PACK_EXPAND_WIDGET, 5);
	vbox->pack_start(*saveHBox, Gtk::PACK_SHRINK , 5);

	add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
	add_button(Gtk::Stock::OK, Gtk::RESPONSE_OK);

	set_default_size(400, 300);
	set_gravity(Gdk::GRAVITY_CENTER);

	try{
		int pos = 0;
		libtorrent::torrent_info t(torrent_path.c_str());

		// Fill torrent's data
		torrentname = t.name();
		priorities->resize(t.num_files(), 1);
		for (int i=0; i<t.num_files(); i++){
			newfilenames->push_back(t.file_at(i).path);
			Glib::ustring fileName(t.file_at(i).path);
			addRow(pos, fileName, t.file_at(i).size);
			pos++;
		}

		torrentName->set_label(torrentname);
		// This has become obsolete in libtorrent 0.16.x
		/*for (libtorrent::torrent_info::file_iterator i = t.begin_files(); i != t.end_files(); ++i){
			Glib::ustring fileName(i->path.string());
			addRow(pos, fileName, i->size);
			pos++;
		}*/
	}
	catch (libtorrent::invalid_torrent_file&){
	}
	catch (libtorrent::invalid_encoding&){
	}

	show_all();
	hide();
}

void LoadOneTorrentDialog::addRow(int position, Glib::ustring &name, const libtorrent::size_type &size){
	Gtk::TreeModel::Row row;

	row = *(refTreeModel->append());

	row[columnsDialogTorrent.m_position] = position;
	row[columnsDialogTorrent.m_download] = true;
	row[columnsDialogTorrent.m_name] = name;
	row[columnsDialogTorrent.m_size] = size_to_string((float) size);
	row[columnsDialogTorrent.m_size_type] = size;
	row[columnsDialogTorrent.m_prio_choosen] = _("Normal");
	row[columnsDialogTorrent.m_priority] = pr_refTreeModel;
}

void LoadOneTorrentDialog::on_cellrenderer_choice_edited(const Glib::ustring& path_string, const Glib::ustring& new_text){
	int file_index;
	bool is_active;
	Gtk::TreeModel::Row row;

	row = *(refTreeModel->get_iter(Gtk::TreeModel::Path(path_string)));
	is_active = row[columnsDialogTorrent.m_download];
	file_index = to_int(path_string.c_str());

	if (!is_active)
		row[columnsDialogTorrent.m_download] = true;

	row[columnsDialogTorrent.m_prio_choosen] = new_text;
	if (new_text == _("Maximum"))
		(*priorities)[file_index] = 7;
	else if (new_text == _("High"))
		(*priorities)[file_index] = 2;
	else
		(*priorities)[file_index] = 1;
}

void LoadOneTorrentDialog::selectAndSetSaveDirectory(){
	Glib::ustring directory;

	directory = Utils::getDirectoryFromDialog(_("Select torrent save path"),
											  (*torrentpathsave));

	if (directory != ""){
		(*torrentpathsave) = directory.c_str();
		saveDirectoryEntry.set_text(directory);
	}
}

bool LoadOneTorrentDialog::isRowSelected(){
	Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = dialogTorrentTreeView.get_selection();
	Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();

	if(iter){
		return true;
	}

	return false;
}

void LoadOneTorrentDialog::mouse_button_press_event(GdkEventButton* event){
	if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) && isRowSelected() ){
		if(pMenuPopup)
			pMenuPopup->popup(event->button, event->time);
	}
}

void LoadOneTorrentDialog::changeName()
{
	int file_position;
	Glib::ustring new_name;
	Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = dialogTorrentTreeView.get_selection();
	Gtk::TreeModel::iterator iter = refTreeSelection->get_selected();
	Gtk::TreeModel::Row row = *iter;

	if (!row[columnsDialogTorrent.m_download]) return;

	file_position = row[columnsDialogTorrent.m_position];

	new_name = Utils::askNameDialog(_("Choose a new name"), row[columnsDialogTorrent.m_name]);
	if(new_name.empty()) return;

	if (row[columnsDialogTorrent.m_name] != new_name)
	{
		row[columnsDialogTorrent.m_name] = new_name;

/*#if defined(_WIN32) && defined(__MINGW32__)
		if (newmap->size() > 1)
			new_name = torrentname + "\\" + new_name;
#else
		if (newmap->size() > 1)
			new_name = torrentname + "/" + new_name;
#endif

		(*newmap)[file_position].path = boost::filesystem::path(new_name.c_str());
*/
		(*newfilenames)[file_position] = new_name;
	}
}

void LoadOneTorrentDialog::downloadFileToggled(const Glib::ustring &s)
{
	int file_position;
	Gtk::TreeModel::Row row;
	Glib::ustring old_prio;

	row = *(refTreeModel->get_iter(Gtk::TreeModel::Path(s)));
	file_position = row[columnsDialogTorrent.m_position];
	old_prio = row[columnsDialogTorrent.m_prio_choosen];

	if (row[columnsDialogTorrent.m_download])
	{
		(*priorities)[file_position] = 1;
		row[columnsDialogTorrent.m_prio_choosen] = _("Normal");
	}
	else
	{
		(*priorities)[file_position] = 0;
		row[columnsDialogTorrent.m_prio_choosen] = _("None");

		bool all_zero = true;
		for (int i=0; i<(*priorities).size(); i++)
		{
			if ((*priorities)[i] == 1)
			{
				all_zero = false;
				break;
			}
		}
		if (all_zero)
		{
			(*priorities)[file_position] = 1;
			row[columnsDialogTorrent.m_download] = true;
			row[columnsDialogTorrent.m_prio_choosen] = old_prio;
			Utils::warningDialog(_("Selecting files"), _("At least one file of the torrent must be selected"));
		}
	}
}

LoadOneTorrentDialog::~LoadOneTorrentDialog(){
}