Move reparent_iter to common

Add support for 'spliting' folders in the add torrent dialog
This commit is contained in:
Andrew Resch 2009-06-28 00:56:36 +00:00
parent 2ccebaca3b
commit a66910e50b
3 changed files with 55 additions and 26 deletions

View File

@ -52,6 +52,7 @@ from deluge.log import LOG as log
import deluge.common import deluge.common
import deluge.ui.common import deluge.ui.common
import dialogs import dialogs
import common
class AddTorrentDialog(component.Component): class AddTorrentDialog(component.Component):
def __init__(self): def __init__(self):
@ -859,7 +860,31 @@ class AddTorrentDialog(component.Component):
# we can construct the new proper paths # we can construct the new proper paths
if len(new_text) == 0 or new_text[-1] != "/": if len(new_text) == 0 or new_text[-1] != "/":
new_text += "/" new_text += "/"
self.files_treestore[itr][1] = new_text
# We need to check if this folder has been split
split_text = new_text[:-1].split("/")
if len(split_text) > 1:
# It's been split, so we need to add new folders and then reparent
# itr.
parent = self.files_treestore.iter_parent(itr)
for s in split_text[:-1]:
# We don't iterate over the last item because we'll just use
# the existing itr and change the text
parent = self.files_treestore.append(parent,
[True, s, 0, -1, False, gtk.STOCK_DIRECTORY])
self.files_treestore[itr][1] = split_text[-1]
# Now reparent itr to parent
common.reparent_iter(self.files_treestore, itr, parent)
# We need to re-expand the view because it might contracted
# if we change the root iter
self.listview_files.expand_row("0", False)
else:
# This was a simple folder rename without any splits, so just
# change the path for itr
self.files_treestore[itr][1] = new_text
# Walk through the tree from 'itr' and add all the new file paths # Walk through the tree from 'itr' and add all the new file paths
# to the 'mapped_files' option # to the 'mapped_files' option

View File

@ -181,3 +181,29 @@ def show_other_dialog(header, type_str, image_stockid=None, image_filename=None,
dialog.destroy() dialog.destroy()
return value return value
def reparent_iter(treestore, itr, parent, move_siblings=False):
"""
This effectively moves itr plus it's children to be a child of parent in treestore
:param treestore: gtkTreeStore, the treestore
:param itr: gtkTreeIter, the iter to move
:param parent: gtkTreeIter, the new parent for itr
:param move_siblings: bool. if True, it will move all itr's siblings to parent
"""
src = itr
def move_children(i, dest):
while i:
n = treestore.append(dest, treestore.get(i, *xrange(treestore.get_n_columns())))
to_remove = i
if treestore.iter_children(i):
move_children(treestore.iter_children(i), n)
if i != src:
i = treestore.iter_next(i)
else:
# This is the source iter, we don't want other iters in it's level
if not move_siblings:
i = None
treestore.remove(to_remove)
move_children(itr, parent)

View File

@ -46,6 +46,7 @@ from deluge.configmanager import ConfigManager
import deluge.configmanager import deluge.configmanager
import deluge.component as component import deluge.component as component
import deluge.common import deluge.common
import common
from deluge.log import LOG as log from deluge.log import LOG as log
@ -673,29 +674,6 @@ class FilesTab(Tab):
return path_iter return path_iter
def reparent_iter(self, itr, parent, move_siblings=False):
"""
This effectively moves itr plus it's children to be a child of parent
If move_siblings is True, it will move all itr's siblings to parent
"""
src = itr
def move_children(i, dest):
while i:
n = self.treestore.append(dest, self.treestore.get(i, *xrange(self.treestore.get_n_columns())))
to_remove = i
if self.treestore.iter_children(i):
move_children(self.treestore.iter_children(i), n)
if i != src:
i = self.treestore.iter_next(i)
else:
# This is the source iter, we don't want other iters in it's level
if not move_siblings:
i = None
self.treestore.remove(to_remove)
move_children(itr, parent)
def remove_childless_folders(self, itr): def remove_childless_folders(self, itr):
""" """
Goes up the tree removing childless itrs starting at itr Goes up the tree removing childless itrs starting at itr
@ -745,14 +723,14 @@ class FilesTab(Tab):
return return
if new_folder_iter: if new_folder_iter:
# This means that a folder by this name already exists # This means that a folder by this name already exists
self.reparent_iter(self.treestore.iter_children(old_folder_iter), new_folder_iter) common.reparent_iter(self.treestore, self.treestore.iter_children(old_folder_iter), new_folder_iter)
else: else:
parent = old_folder_iter_parent parent = old_folder_iter_parent
for ns in new_split[:-1]: for ns in new_split[:-1]:
parent = self.treestore.append(parent, [ns + "/", 0, "", 0, 0, -1, gtk.STOCK_DIRECTORY]) parent = self.treestore.append(parent, [ns + "/", 0, "", 0, 0, -1, gtk.STOCK_DIRECTORY])
self.treestore[old_folder_iter][0] = new_split[-1] + "/" self.treestore[old_folder_iter][0] = new_split[-1] + "/"
self.reparent_iter(old_folder_iter, parent) common.reparent_iter(self.treestore, old_folder_iter, parent)
# We need to check if the old_folder_iter_parent no longer has children # We need to check if the old_folder_iter_parent no longer has children
# and if so, we delete it # and if so, we delete it