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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
# -*- coding: utf-8 -*-
#
# Copyright (C) 2008 Andrew Resch <andrewresch@gmail.com>
#
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
from __future__ import division
import base64
import logging
import os.path
import gtk
from gobject import TYPE_UINT64, idle_add
from twisted.internet.threads import deferToThread
import deluge.component as component
from deluge.common import get_path_size, is_url, resource_filename
from deluge.configmanager import ConfigManager
from deluge.ui.client import client
from deluge.ui.gtkui.torrentview_data_funcs import cell_data_size
log = logging.getLogger(__name__)
class CreateTorrentDialog(object):
def __init__(self):
pass
def show(self):
self.builder = gtk.Builder()
# The main dialog
self.builder.add_from_file(resource_filename(
'deluge.ui.gtkui', os.path.join('glade', 'create_torrent_dialog.ui')
))
# The remote path dialog
self.builder.add_from_file(resource_filename(
'deluge.ui.gtkui', os.path.join('glade', 'create_torrent_dialog.remote_path.ui')
))
# The remote save dialog
self.builder.add_from_file(resource_filename(
'deluge.ui.gtkui', os.path.join('glade', 'create_torrent_dialog.remote_save.ui')
))
# The progress dialog
self.builder.add_from_file(resource_filename(
'deluge.ui.gtkui', os.path.join('glade', 'create_torrent_dialog.progress.ui')
))
self.config = ConfigManager('gtkui.conf')
self.dialog = self.builder.get_object('create_torrent_dialog')
self.dialog.set_transient_for(component.get('MainWindow').window)
self.builder.connect_signals({
'on_button_file_clicked': self._on_button_file_clicked,
'on_button_folder_clicked': self._on_button_folder_clicked,
'on_button_remote_path_clicked': self._on_button_remote_path_clicked,
'on_button_cancel_clicked': self._on_button_cancel_clicked,
'on_button_save_clicked': self._on_button_save_clicked,
'on_button_up_clicked': self._on_button_up_clicked,
'on_button_add_clicked': self._on_button_add_clicked,
'on_button_remove_clicked': self._on_button_remove_clicked,
'on_button_down_clicked': self._on_button_down_clicked
})
# path, icon, size
self.files_treestore = gtk.TreeStore(str, str, TYPE_UINT64)
column = gtk.TreeViewColumn(_('Filename'))
render = gtk.CellRendererPixbuf()
column.pack_start(render, False)
column.add_attribute(render, 'stock-id', 1)
render = gtk.CellRendererText()
column.pack_start(render, True)
column.add_attribute(render, 'text', 0)
column.set_expand(True)
self.builder.get_object('treeview_files').append_column(column)
column = gtk.TreeViewColumn(_('Size'))
render = gtk.CellRendererText()
column.pack_start(render, True)
column.set_cell_data_func(render, cell_data_size, 2)
self.builder.get_object('treeview_files').append_column(column)
self.builder.get_object('treeview_files').set_model(self.files_treestore)
self.builder.get_object('treeview_files').set_show_expanders(False)
# tier, url
self.trackers_liststore = gtk.ListStore(int, str)
self.builder.get_object('tracker_treeview').append_column(
gtk.TreeViewColumn(_('Tier'), gtk.CellRendererText(), text=0))
self.builder.get_object('tracker_treeview').append_column(
gtk.TreeViewColumn(_('Tracker'), gtk.CellRendererText(), text=1))
self.builder.get_object('tracker_treeview').set_model(self.trackers_liststore)
self.trackers_liststore.set_sort_column_id(0, gtk.SORT_ASCENDING)
if not client.is_localhost() and client.connected():
self.builder.get_object('button_remote_path').show()
else:
self.builder.get_object('button_remote_path').hide()
self.dialog.show()
def parse_piece_size_text(self, value):
psize, metric = value.split()
psize = int(psize)
if psize < 32:
# This is a MiB value
psize = psize * 1024 * 1024
else:
# This is a KiB value
psize = psize * 1024
return psize
def adjust_piece_size(self):
"""Adjusts the recommended piece based on the file/folder/path selected."""
size = self.files_treestore[0][2]
model = self.builder.get_object('combo_piece_size').get_model()
for index, value in enumerate(model):
psize = self.parse_piece_size_text(value[0])
pieces = size // psize
if pieces < 2048 or (index + 1) == len(model):
self.builder.get_object('combo_piece_size').set_active(index)
break
def _on_button_file_clicked(self, widget):
log.debug('_on_button_file_clicked')
# Setup the filechooserdialog
chooser = gtk.FileChooserDialog(_('Choose a file'),
self.dialog,
gtk.FILE_CHOOSER_ACTION_OPEN,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
chooser.set_transient_for(self.dialog)
chooser.set_select_multiple(False)
chooser.set_property('skip-taskbar-hint', True)
# Run the dialog
response = chooser.run()
if response == gtk.RESPONSE_OK:
result = chooser.get_filename()
else:
chooser.destroy()
return
path = result.decode('utf-8')
self.files_treestore.clear()
self.files_treestore.append(None, [result, gtk.STOCK_FILE, get_path_size(path)])
self.adjust_piece_size()
chooser.destroy()
def _on_button_folder_clicked(self, widget):
log.debug('_on_button_folder_clicked')
# Setup the filechooserdialog
chooser = gtk.FileChooserDialog(_('Choose a folder'),
self.dialog,
gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
chooser.set_transient_for(self.dialog)
chooser.set_select_multiple(False)
chooser.set_property('skip-taskbar-hint', True)
# Run the dialog
response = chooser.run()
if response == gtk.RESPONSE_OK:
result = chooser.get_filename()
else:
chooser.destroy()
return
path = result.decode('utf-8')
self.files_treestore.clear()
self.files_treestore.append(None, [result, gtk.STOCK_OPEN, get_path_size(path)])
self.adjust_piece_size()
chooser.destroy()
def _on_button_remote_path_clicked(self, widget):
log.debug('_on_button_remote_path_clicked')
dialog = self.builder.get_object('remote_path_dialog')
entry = self.builder.get_object('entry_path')
dialog.set_transient_for(self.dialog)
entry.set_text('/')
entry.grab_focus()
response = dialog.run()
if response == gtk.RESPONSE_OK:
result = entry.get_text()
def _on_get_path_size(size):
log.debug('size: %s', size)
if size > 0:
self.files_treestore.clear()
self.files_treestore.append(None, [result, gtk.STOCK_NETWORK, size])
self.adjust_piece_size()
client.core.get_path_size(result).addCallback(_on_get_path_size)
client.force_call(True)
dialog.hide()
def _on_button_cancel_clicked(self, widget):
log.debug('_on_button_cancel_clicked')
self.dialog.destroy()
def _on_button_save_clicked(self, widget):
log.debug('_on_button_save_clicked')
if len(self.files_treestore) == 0:
return
# Get the path
path = self.files_treestore[0][0].rstrip('\\/')
torrent_filename = '%s.torrent' % os.path.split(path)[-1]
is_remote = self.files_treestore[0][1] == gtk.STOCK_NETWORK
if is_remote:
# This is a remote path
dialog = self.builder.get_object('remote_save_dialog')
dialog.set_transient_for(self.dialog)
dialog_save_path = self.builder.get_object('entry_save_path')
dialog_save_path.set_text(path + '.torrent')
response = dialog.run()
if response == gtk.RESPONSE_OK:
result = dialog_save_path.get_text()
else:
dialog.hide()
return
dialog.hide()
else:
# Setup the filechooserdialog
chooser = gtk.FileChooserDialog(_('Save .torrent file'), self.dialog,
gtk.FILE_CHOOSER_ACTION_SAVE,
buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_SAVE, gtk.RESPONSE_OK))
chooser.set_transient_for(self.dialog)
chooser.set_select_multiple(False)
chooser.set_property('skip-taskbar-hint', True)
# Add .torrent and * file filters
file_filter = gtk.FileFilter()
file_filter.set_name(_('Torrent files'))
file_filter.add_pattern('*.' + 'torrent')
chooser.add_filter(file_filter)
file_filter = gtk.FileFilter()
file_filter.set_name(_('All files'))
file_filter.add_pattern('*')
chooser.add_filter(file_filter)
chooser.set_current_name(torrent_filename)
# Run the dialog
response = chooser.run()
if response == gtk.RESPONSE_OK:
result = chooser.get_filename()
else:
chooser.destroy()
return
chooser.destroy()
# Fix up torrent filename
if len(result) < 9:
result += '.torrent'
elif result[-8:] != '.torrent':
result += '.torrent'
# Get a list of trackers
trackers = []
if not len(self.trackers_liststore):
tracker = None
else:
# Create a list of lists [[tier0, ...], [tier1, ...], ...]
tier_dict = {}
for tier, tracker in self.trackers_liststore:
tier_dict.setdefault(tier, []).append(tracker)
trackers = [tier_dict[tier] for tier in sorted(tier_dict)]
# Get the first tracker in the first tier
tracker = trackers[0][0]
# Get a list of webseeds
webseeds = []
b = self.builder.get_object('textview_webseeds').get_buffer()
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split('\n')
for l in lines:
if is_url(l):
webseeds.append(l)
# Get the piece length in bytes
combo = self.builder.get_object('combo_piece_size')
piece_length = self.parse_piece_size_text(combo.get_model()[combo.get_active()][0])
author = self.builder.get_object('entry_author').get_text()
comment = self.builder.get_object('entry_comments').get_text()
private = self.builder.get_object('chk_private_flag').get_active()
add_to_session = self.builder.get_object('chk_add_to_session').get_active()
if is_remote:
def torrent_created():
self.builder.get_object('progress_dialog').hide_all()
client.deregister_event_handler('CreateTorrentProgressEvent', on_create_torrent_progress_event)
def on_create_torrent_progress_event(piece_count, num_pieces):
self._on_create_torrent_progress(piece_count, num_pieces)
if piece_count == num_pieces:
from twisted.internet import reactor
reactor.callLater(0.5, torrent_created)
client.register_event_handler('CreateTorrentProgressEvent', on_create_torrent_progress_event)
client.core.create_torrent(
path,
tracker,
piece_length,
comment,
result,
webseeds,
private,
author,
trackers,
add_to_session)
else:
def hide_progress(result):
self.builder.get_object('progress_dialog').hide_all()
deferToThread(self.create_torrent,
path.decode('utf-8'),
tracker,
piece_length,
self._on_create_torrent_progress,
comment,
result.decode('utf-8'),
webseeds,
private,
author,
trackers,
add_to_session).addCallback(hide_progress)
# Setup progress dialog
self.builder.get_object('progress_dialog').set_transient_for(component.get('MainWindow').window)
self.builder.get_object('progress_dialog').show_all()
self.dialog.destroy()
def create_torrent(self, path, tracker, piece_length, progress, comment, target,
webseeds, private, created_by, trackers, add_to_session):
import deluge.metafile
deluge.metafile.make_meta_file(
path,
tracker,
piece_length,
progress=progress,
comment=comment,
target=target,
webseeds=webseeds,
private=private,
created_by=created_by,
trackers=trackers)
if add_to_session:
with open(target, 'rb') as _file:
filedump = base64.encodestring(_file.read())
client.core.add_torrent_file(os.path.split(target)[-1], filedump,
{'download_location': os.path.split(path)[0]})
def _on_create_torrent_progress(self, value, num_pieces):
percent = value / num_pieces
def update_pbar_with_gobject(percent):
pbar = self.builder.get_object('progressbar')
pbar.set_text('%.2f%%' % (percent * 100))
pbar.set_fraction(percent)
return False
if percent >= 0 and percent <= 1.0:
# Make sure there are no threads race conditions that can
# crash the UI while updating it.
idle_add(update_pbar_with_gobject, percent)
def _on_button_up_clicked(self, widget):
log.debug('_on_button_up_clicked')
row = self.builder.get_object('tracker_treeview').get_selection().get_selected()[1]
if row is None:
return
if self.trackers_liststore[row][0] == 0:
return
else:
self.trackers_liststore[row][0] -= 1
def _on_button_down_clicked(self, widget):
log.debug('_on_button_down_clicked')
row = self.builder.get_object('tracker_treeview').get_selection().get_selected()[1]
if row is None:
return
self.trackers_liststore[row][0] += 1
def _on_button_add_clicked(self, widget):
log.debug('_on_button_add_clicked')
builder = gtk.Builder()
builder.add_from_file(resource_filename(
'deluge.ui.gtkui', os.path.join('glade', 'edit_trackers.add.ui')
))
dialog = builder.get_object('add_tracker_dialog')
dialog.set_transient_for(self.dialog)
textview = builder.get_object('textview_trackers')
if self.config['createtorrent.trackers']:
textview.get_buffer().set_text('\n'.join(self.config['createtorrent.trackers']))
else:
textview.get_buffer().set_text('')
textview.grab_focus()
response = dialog.run()
if response == gtk.RESPONSE_OK:
# Create a list of trackers from the textview buffer
trackers = []
b = textview.get_buffer()
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split('\n')
self.config['createtorrent.trackers'] = lines
log.debug('lines: %s', lines)
for l in lines:
if is_url(l):
trackers.append(l)
# We are going to add these trackers to the highest tier + 1
tier = 0
for row in self.trackers_liststore:
if row[0] > tier:
tier = row[0]
for tracker in trackers:
self.trackers_liststore.append([tier, tracker])
dialog.destroy()
def _on_button_remove_clicked(self, widget):
log.debug('_on_button_remove_clicked')
row = self.builder.get_object('tracker_treeview').get_selection().get_selected()[1]
if row is None:
return
self.trackers_liststore.remove(row)
|