Exemple #1
0
    def AddFileList(self, tdef, selectedFiles, vSizer, index):
        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Name')
        self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

        # Add files
        def sort_by_size(a, b):
            return cmp(a[1], b[1])

        files = tdef.get_files_as_unicode_with_length()
        files.sort(sort_by_size, reverse=True)

        for filename, size in files:
            try:
                pos = self.listCtrl.InsertStringItem(sys.maxsize, filename)
            except:
                try:
                    pos = self.listCtrl.InsertStringItem(
                        sys.maxsize, filename.decode('utf-8', 'ignore'))
                except:
                    self._logger.error("Could not format filename %s",
                                       self.torrent.name)
            self.listCtrl.SetItemData(pos, pos)
            self.listCtrl.SetStringItem(pos, 1, size_format(size))

            if selectedFiles:
                self.listCtrl.CheckItem(pos, filename in selectedFiles)

        if selectedFiles is None:
            self.listCtrl.doSelectAll()

        self.listCtrl.setResizeColumn(0)
        self.listCtrl.SetColumnWidth(
            1, wx.LIST_AUTOSIZE)  # autosize only works after adding rows
        vSizer.Insert(index, self.listCtrl, 1, wx.EXPAND | wx.BOTTOM, 3)

        self.listCtrl.SetFocus()

        def OnChar(event):
            if event.GetKeyCode() == wx.WXK_RETURN:
                self.OnOk()
            else:
                event.Skip()

        self.listCtrl.Bind(wx.EVT_CHAR, OnChar)

        vSizer.Insert(
            index,
            wx.StaticText(
                self, -1,
                'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'
            ), 0, wx.BOTTOM, 3)

        firstLine = wx.StaticText(self, -1, "Content:")
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Insert(index, firstLine, 0, wx.BOTTOM, 3)

        vSizer.Insert(index, wx.StaticLine(self, -1), 0, wx.EXPAND | wx.BOTTOM,
                      10)
Exemple #2
0
    def AddFileList(self, tdef, selectedFiles, vSizer, index):
        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Name')
        self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

        # Add files
        def sort_by_size(a, b):
            return cmp(a[1], b[1])

        files = tdef.get_files_as_unicode_with_length()
        files.sort(sort_by_size, reverse=True)

        for filename, size in files:
            try:
                pos = self.listCtrl.InsertStringItem(sys.maxsize, filename)
            except:
                try:
                    pos = self.listCtrl.InsertStringItem(sys.maxsize, filename.decode('utf-8', 'ignore'))
                except:
                    self._logger.error("Could not format filename %s", self.torrent.name)
            self.listCtrl.SetItemData(pos, pos)
            self.listCtrl.SetStringItem(pos, 1, size_format(size))

            if selectedFiles:
                self.listCtrl.CheckItem(pos, filename in selectedFiles)

        if selectedFiles is None:
            self.listCtrl.doSelectAll()

        self.listCtrl.setResizeColumn(0)
        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)  # autosize only works after adding rows
        vSizer.Insert(index, self.listCtrl, 1, wx.EXPAND | wx.BOTTOM, 3)

        self.listCtrl.SetFocus()

        def OnChar(event):
            if event.GetKeyCode() == wx.WXK_RETURN:
                self.OnOk()
            else:
                event.Skip()
        self.listCtrl.Bind(wx.EVT_CHAR, OnChar)

        vSizer.Insert(index, wx.StaticText(
            self, -1, 'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'), 0, wx.BOTTOM, 3)

        firstLine = wx.StaticText(self, -1, "Content:")
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Insert(index, firstLine, 0, wx.BOTTOM, 3)

        vSizer.Insert(index, wx.StaticLine(self, -1), 0, wx.EXPAND | wx.BOTTOM, 10)
Exemple #3
0
class SaveAs(wx.Dialog):
    def __init__(self,
                 parent,
                 tdef,
                 defaultdir,
                 defaultname,
                 selectedFiles=None):
        self._logger = logging.getLogger(self.__class__.__name__)
        wx.Dialog.__init__(self,
                           parent,
                           -1,
                           'Please specify a target directory',
                           name="SaveAsDialog")

        self.guiutility = GUIUtility.getInstance()
        self.utility = self.guiutility.utility
        self.SetSize((600, 550))

        self.filehistory = []
        try:
            self.filehistory = json.loads(
                self.utility.read_config("recent_download_history",
                                         literal_eval=False))
        except:
            pass

        self.defaultdir = defaultdir
        self.listCtrl = None
        self.collected = tdef

        lastUsed = self.filehistory[0] if self.filehistory else defaultdir

        vSizer = wx.BoxSizer(wx.VERTICAL)

        if tdef:
            line = 'Please select a directory where to save:'
        else:
            line = 'Please select a directory where to save this torrent'

        firstLine = wx.StaticText(self, -1, line)
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND | wx.BOTTOM, 3)

        if tdef:
            torrentName = wx.StaticText(self, -1, tdef.get_name_as_unicode())
            torrentName.SetMinSize((1, -1))
            vSizer.Add(torrentName, 0, wx.EXPAND | wx.BOTTOM | wx.RIGHT, 3)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(wx.StaticText(self, -1, 'Save as:'), 0,
                   wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.BOTTOM, 3)

        choices = copy.copy(self.filehistory)
        if defaultdir not in choices:
            choices.append(defaultdir)

        if defaultname:
            default_path = os.path.join(lastUsed, defaultname)
            choices.insert(0, default_path)
        else:
            default_path = lastUsed

        self.dirTextCtrl = wx.ComboBox(self,
                                       -1,
                                       default_path,
                                       choices=choices,
                                       style=wx.CB_DROPDOWN)
        self.dirTextCtrl.Select(0)

        hSizer.Add(self.dirTextCtrl, 1, wx.EXPAND | wx.RIGHT | wx.BOTTOM, 3)

        browseButton = wx.Button(self, -1, 'Browse')
        browseButton.Bind(wx.EVT_BUTTON, self.OnBrowseDir)
        hSizer.Add(browseButton)

        vSizer.Add(hSizer, 0, wx.EXPAND | wx.BOTTOM, 3)

        self.cancel = wx.Button(self, wx.ID_CANCEL)
        self.cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        self.ok = wx.Button(self, wx.ID_OK)
        self.ok.Bind(wx.EVT_BUTTON, self.OnOk)

        self.anonymity_dialog = None
        self.anonymity_dialog = AnonymityDialog(self)
        vSizer.Add(self.anonymity_dialog, 0, wx.EXPAND, 3)

        self.Bind(EVT_COLLECTED, self.OnCollected)

        # Add file list
        if tdef and tdef.get_files():
            self.AddFileList(tdef, selectedFiles, vSizer,
                             len(vSizer.GetChildren()))

        elif isinstance(tdef, TorrentDefNoMetainfo):
            text = wx.StaticText(self, -1,
                                 "Attempting to retrieve .torrent...")
            _set_font(text, size_increment=1)
            ag = wx.animate.GIFAnimationCtrl(
                self, -1,
                os.path.join(self.guiutility.utility.getPath(), LIBRARYNAME,
                             'Main', 'vwxGUI', 'images', 'search_new.gif'))
            ag.Play()
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.AddStretchSpacer()
            sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
            sizer.Add(ag, 0, wx.ALIGN_CENTER_VERTICAL)
            sizer.AddStretchSpacer()
            vSizer.Add(sizer, 1, wx.EXPAND | wx.BOTTOM, 3)
            self.SetSize((600, 285))

            # convert tdef into guidbtuple, and collect it using torrentsearch_manager.downloadTorrentfileFromPeers
            torrent = Torrent.fromTorrentDef(tdef)
            torrentsearch_manager = self.guiutility.torrentsearch_manager

            def callback(saveas_id, infohash):
                saveas = wx.FindWindowById(saveas_id)
                if saveas:
                    tdef = TorrentDef.load_from_memory(
                        self.utility.session.lm.torrent_store.get(infohash))
                    event = CollectedEvent(tdef=tdef)
                    wx.PostEvent(saveas, event)

            cb = lambda torrent_filename, saveas_id=self.Id: callback(
                saveas_id, torrent_filename)
            torrentsearch_manager.downloadTorrentfileFromPeers(torrent, cb)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(self.cancel)
        bSizer.AddButton(self.ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND | wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND | wx.ALL, 10)
        self.SetSizer(sizer)

    def AddFileList(self, tdef, selectedFiles, vSizer, index):
        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Name')
        self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

        # Add files
        def sort_by_size(a, b):
            return cmp(a[1], b[1])

        files = tdef.get_files_as_unicode_with_length()
        files.sort(sort_by_size, reverse=True)

        for filename, size in files:
            try:
                pos = self.listCtrl.InsertStringItem(sys.maxsize, filename)
            except:
                try:
                    pos = self.listCtrl.InsertStringItem(
                        sys.maxsize, filename.decode('utf-8', 'ignore'))
                except:
                    self._logger.error("Could not format filename %s",
                                       self.torrent.name)
            self.listCtrl.SetItemData(pos, pos)
            self.listCtrl.SetStringItem(pos, 1, size_format(size))

            if selectedFiles:
                self.listCtrl.CheckItem(pos, filename in selectedFiles)

        if selectedFiles is None:
            self.listCtrl.doSelectAll()

        self.listCtrl.setResizeColumn(0)
        self.listCtrl.SetColumnWidth(
            1, wx.LIST_AUTOSIZE)  # autosize only works after adding rows
        vSizer.Insert(index, self.listCtrl, 1, wx.EXPAND | wx.BOTTOM, 3)

        self.listCtrl.SetFocus()

        def OnChar(event):
            if event.GetKeyCode() == wx.WXK_RETURN:
                self.OnOk()
            else:
                event.Skip()

        self.listCtrl.Bind(wx.EVT_CHAR, OnChar)

        vSizer.Insert(
            index,
            wx.StaticText(
                self, -1,
                'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'
            ), 0, wx.BOTTOM, 3)

        firstLine = wx.StaticText(self, -1, "Content:")
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Insert(index, firstLine, 0, wx.BOTTOM, 3)

        vSizer.Insert(index, wx.StaticLine(self, -1), 0, wx.EXPAND | wx.BOTTOM,
                      10)

    def OnCollected(self, event):
        tdef = event.tdef
        self.collected = tdef
        self.SetSize((600, 550))
        vSizer = self.GetSizer().GetItem(0).GetSizer()
        hsizer = vSizer.GetItem(len(vSizer.GetChildren()) - 2).GetSizer()
        self.Freeze()
        hsizer.Clear(deleteWindows=True)
        vSizer.Remove(hsizer)
        self.AddFileList(tdef, None, vSizer, len(vSizer.GetChildren()) - 1)

        if tdef.is_multifile_torrent():
            items = self.dirTextCtrl.GetItems()
            lastUsed = self.filehistory[
                0] if self.filehistory else self.defaultdir
            path = os.path.join(lastUsed, tdef.get_name_as_unicode())
            if path not in items:
                items.insert(0, path)
                self.dirTextCtrl.SetItems(items)
            self.dirTextCtrl.SetStringSelection(path)

        self.Layout()
        self.Refresh()
        self.Thaw()

    def GetCollected(self):
        return self.collected

    def GetPath(self):
        return self.dirTextCtrl.GetValue().strip().rstrip(os.path.sep)

    def GetSelectedFiles(self):
        if self.listCtrl:
            selected = self.listCtrl.GetSelectedItems()
            nrSelected = len(selected)

            if nrSelected > 0 and nrSelected < self.listCtrl.GetItemCount():
                files = []
                for index in selected:
                    files.append(self.listCtrl.GetItem(index, 0).GetText())
                return files
        return None

    def UseTunnels(self):
        return self.anonymity_dialog and self.anonymity_dialog.UseTunnels()

    def UseSafeSeeding(self):
        return self.anonymity_dialog and self.anonymity_dialog.UseSafeSeeding()

    def OnOk(self, event=None):
        if self.listCtrl:
            nrSelected = len(self.listCtrl.GetSelectedItems())
            if nrSelected == 0:
                dlg = wx.MessageDialog(
                    self,
                    "Please select at least one file to be downloaded using the checkboxes.",
                    "Please select a file to be downloaded", wx.ICON_ERROR)
                dlg.ShowModal()
                dlg.Destroy()
                return

        path = self.GetPath()
        history_path = os.path.split(path)[0] if (self.collected and self.collected.is_multifile_torrent()) or \
            not os.path.exists(path) or os.path.isfile(path) else path
        if history_path in self.filehistory:
            self.filehistory.remove(history_path)
        self.filehistory.insert(0, history_path)
        self.filehistory = self.filehistory[:25]

        self.utility.write_config("recent_download_history",
                                  json.dumps(self.filehistory))
        self.utility.flush_config()

        self.EndModal(wx.ID_OK)

    def OnCancel(self, event=None):
        self.EndModal(wx.ID_CANCEL)

    def OnBrowseDir(self, event):
        dlg = wx.DirDialog(None,
                           "Please select a directory to save this torrent",
                           style=wx.wx.DD_NEW_DIR_BUTTON)
        dlg.SetPath(self.defaultdir)

        if dlg.ShowModal() == wx.ID_OK and os.path.isdir(dlg.GetPath()):
            self.dirTextCtrl.SetValue(dlg.GetPath())
        dlg.Destroy()
Exemple #4
0
class SaveAs(wx.Dialog):

    def __init__(self, parent, tdef, defaultdir, defaultname, selectedFiles=None):
        self._logger = logging.getLogger(self.__class__.__name__)
        wx.Dialog.__init__(self, parent, -1, 'Please specify a target directory', name="SaveAsDialog")

        self.guiutility = GUIUtility.getInstance()
        self.utility = self.guiutility.utility
        self.SetSize((600, 550))

        self.filehistory = []
        try:
            self.filehistory = json.loads(self.utility.read_config("recent_download_history", literal_eval=False))
        except:
            pass

        self.defaultdir = defaultdir
        self.listCtrl = None
        self.collected = tdef

        lastUsed = self.filehistory[0] if self.filehistory else defaultdir

        vSizer = wx.BoxSizer(wx.VERTICAL)

        if tdef:
            line = 'Please select a directory where to save:'
        else:
            line = 'Please select a directory where to save this torrent'

        firstLine = wx.StaticText(self, -1, line)
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND | wx.BOTTOM, 3)

        if tdef:
            torrentName = wx.StaticText(self, -1, tdef.get_name_as_unicode())
            torrentName.SetMinSize((1, -1))
            vSizer.Add(torrentName, 0, wx.EXPAND | wx.BOTTOM | wx.RIGHT, 3)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(wx.StaticText(self, -1, 'Save as:'), 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.BOTTOM, 3)

        choices = copy.copy(self.filehistory)
        if defaultdir not in choices:
            choices.append(defaultdir)

        if defaultname:
            default_path = os.path.join(lastUsed, defaultname)
            choices.insert(0, default_path)
        else:
            default_path = lastUsed

        self.dirTextCtrl = wx.ComboBox(self, -1, default_path, choices=choices, style=wx.CB_DROPDOWN)
        self.dirTextCtrl.Select(0)

        hSizer.Add(self.dirTextCtrl, 1, wx.EXPAND | wx.RIGHT | wx.BOTTOM, 3)

        browseButton = wx.Button(self, -1, 'Browse')
        browseButton.Bind(wx.EVT_BUTTON, self.OnBrowseDir)
        hSizer.Add(browseButton)

        vSizer.Add(hSizer, 0, wx.EXPAND | wx.BOTTOM, 3)

        self.cancel = wx.Button(self, wx.ID_CANCEL)
        self.cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        self.ok = wx.Button(self, wx.ID_OK)
        self.ok.Bind(wx.EVT_BUTTON, self.OnOk)

        self.anonymity_dialog = None
        self.anonymity_dialog = AnonymityDialog(self)
        vSizer.Add(self.anonymity_dialog, 0, wx.EXPAND, 3)

        self.Bind(EVT_COLLECTED, self.OnCollected)

        # Add file list
        if tdef and tdef.get_files():
            self.AddFileList(tdef, selectedFiles, vSizer, len(vSizer.GetChildren()))

        elif isinstance(tdef, TorrentDefNoMetainfo):
            text = wx.StaticText(self, -1, "Attempting to retrieve .torrent...")
            _set_font(text, size_increment=1)
            ag = wx.animate.GIFAnimationCtrl(self, -1, os.path.join(
                self.guiutility.utility.getPath(), LIBRARYNAME, 'Main', 'vwxGUI', 'images', 'search_new.gif'))
            ag.Play()
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.AddStretchSpacer()
            sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
            sizer.Add(ag, 0, wx.ALIGN_CENTER_VERTICAL)
            sizer.AddStretchSpacer()
            vSizer.Add(sizer, 1, wx.EXPAND | wx.BOTTOM, 3)
            self.SetSize((600, 285))

            # convert tdef into guidbtuple, and collect it using torrentsearch_manager.downloadTorrentfileFromPeers
            torrent = Torrent.fromTorrentDef(tdef)
            torrentsearch_manager = self.guiutility.torrentsearch_manager

            def callback(saveas_id, infohash):
                saveas = wx.FindWindowById(saveas_id)
                if saveas:
                    tdef = TorrentDef.load_from_memory(self.utility.session.lm.torrent_store.get(infohash))
                    event = CollectedEvent(tdef=tdef)
                    wx.PostEvent(saveas, event)

            cb = lambda torrent_filename, saveas_id = self.Id: callback(saveas_id, torrent_filename)
            torrentsearch_manager.downloadTorrentfileFromPeers(torrent, cb)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(self.cancel)
        bSizer.AddButton(self.ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND | wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND | wx.ALL, 10)
        self.SetSizer(sizer)

    def AddFileList(self, tdef, selectedFiles, vSizer, index):
        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Name')
        self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

        # Add files
        def sort_by_size(a, b):
            return cmp(a[1], b[1])

        files = tdef.get_files_as_unicode_with_length()
        files.sort(sort_by_size, reverse=True)

        for filename, size in files:
            try:
                pos = self.listCtrl.InsertStringItem(sys.maxsize, filename)
            except:
                try:
                    pos = self.listCtrl.InsertStringItem(sys.maxsize, filename.decode('utf-8', 'ignore'))
                except:
                    self._logger.error("Could not format filename %s", self.torrent.name)
            self.listCtrl.SetItemData(pos, pos)
            self.listCtrl.SetStringItem(pos, 1, size_format(size))

            if selectedFiles:
                self.listCtrl.CheckItem(pos, filename in selectedFiles)

        if selectedFiles is None:
            self.listCtrl.doSelectAll()

        self.listCtrl.setResizeColumn(0)
        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)  # autosize only works after adding rows
        vSizer.Insert(index, self.listCtrl, 1, wx.EXPAND | wx.BOTTOM, 3)

        self.listCtrl.SetFocus()

        def OnChar(event):
            if event.GetKeyCode() == wx.WXK_RETURN:
                self.OnOk()
            else:
                event.Skip()
        self.listCtrl.Bind(wx.EVT_CHAR, OnChar)

        vSizer.Insert(index, wx.StaticText(
            self, -1, 'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'), 0, wx.BOTTOM, 3)

        firstLine = wx.StaticText(self, -1, "Content:")
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Insert(index, firstLine, 0, wx.BOTTOM, 3)

        vSizer.Insert(index, wx.StaticLine(self, -1), 0, wx.EXPAND | wx.BOTTOM, 10)

    def OnCollected(self, event):
        tdef = event.tdef
        self.collected = tdef
        self.SetSize((600, 550))
        vSizer = self.GetSizer().GetItem(0).GetSizer()
        hsizer = vSizer.GetItem(len(vSizer.GetChildren()) - 2).GetSizer()
        self.Freeze()
        hsizer.Clear(deleteWindows=True)
        vSizer.Remove(hsizer)
        self.AddFileList(tdef, None, vSizer, len(vSizer.GetChildren()) - 1)

        if tdef.is_multifile_torrent():
            items = self.dirTextCtrl.GetItems()
            lastUsed = self.filehistory[0] if self.filehistory else self.defaultdir
            path = os.path.join(lastUsed, tdef.get_name_as_unicode())
            if path not in items:
                items.insert(0, path)
                self.dirTextCtrl.SetItems(items)
            self.dirTextCtrl.SetStringSelection(path)

        self.Layout()
        self.Refresh()
        self.Thaw()

    def GetCollected(self):
        return self.collected

    def GetPath(self):
        return self.dirTextCtrl.GetValue().strip().rstrip(os.path.sep)

    def GetSelectedFiles(self):
        if self.listCtrl:
            selected = self.listCtrl.GetSelectedItems()
            nrSelected = len(selected)

            if nrSelected > 0 and nrSelected < self.listCtrl.GetItemCount():
                files = []
                for index in selected:
                    files.append(self.listCtrl.GetItem(index, 0).GetText())
                return files
        return None

    def UseTunnels(self):
        return self.anonymity_dialog and self.anonymity_dialog.UseTunnels()

    def UseSafeSeeding(self):
        return self.anonymity_dialog and self.anonymity_dialog.UseSafeSeeding()

    def OnOk(self, event=None):
        if self.listCtrl:
            nrSelected = len(self.listCtrl.GetSelectedItems())
            if nrSelected == 0:
                dlg = wx.MessageDialog(
                    self, "Please select at least one file to be downloaded using the checkboxes.", "Please select a file to be downloaded", wx.ICON_ERROR)
                dlg.ShowModal()
                dlg.Destroy()
                return

        path = self.GetPath()
        history_path = os.path.split(path)[0] if (self.collected and self.collected.is_multifile_torrent()) or \
            not os.path.exists(path) or os.path.isfile(path) else path
        if history_path in self.filehistory:
            self.filehistory.remove(history_path)
        self.filehistory.insert(0, history_path)
        self.filehistory = self.filehistory[:25]

        self.utility.write_config("recent_download_history", json.dumps(self.filehistory))
        self.utility.flush_config()

        self.EndModal(wx.ID_OK)

    def OnCancel(self, event=None):
        self.EndModal(wx.ID_CANCEL)

    def OnBrowseDir(self, event):
        dlg = wx.DirDialog(None, "Please select a directory to save this torrent", style=wx.wx.DD_NEW_DIR_BUTTON)
        dlg.SetPath(self.defaultdir)

        if dlg.ShowModal() == wx.ID_OK and os.path.isdir(dlg.GetPath()):
            self.dirTextCtrl.SetValue(dlg.GetPath())
        dlg.Destroy()
Exemple #5
0
class MoveTorrents(wx.Dialog):
    def __init__(self, parent, labels, download_states):
        wx.Dialog.__init__(self, parent, -1, 'Please select the torrents you want to move', size=(750,450))
        
        self.download_states = download_states
        vSizer = wx.BoxSizer(wx.VERTICAL)
        message = 'Please select all torrents which should be moved'
        message += "\nUse ctrl+a to select all/deselect all."
        
        firstLine = wx.StaticText(self, -1, message)
        _set_font(firstLine, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND|wx.BOTTOM, 3)
              
        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Torrent')
        self.listCtrl.InsertColumn(1, 'Current Location')
        
        self.listCtrl.setResizeColumn(0)
        
        for i, label in enumerate(labels):
            row = self.listCtrl.InsertStringItem(sys.maxint, label)
            
            download = download_states[i].get_download()
            self.listCtrl.SetStringItem(row, 1, download.get_dest_dir())

        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        vSizer.Add(self.listCtrl, 1, wx.EXPAND|wx.BOTTOM|wx.TOP, 3)

        self.destTextCtrl = wx.TextCtrl(self)
        self.browseButton = wx.Button(self, -1, 'Browse')
        self.browseButton.Bind(wx.EVT_BUTTON, self.OnBrowse)
        
        moveTo = wx.StaticText(self, -1, 'Move to:')
        _set_font(moveTo, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(moveTo)
        vSizer.Add(wx.StaticText(self, -1, 'Please note that all multi-file torrents create a directory themselves.\nYour new destination should specify the base dir for all torrents.'))
        
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(self.destTextCtrl, 1, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        hSizer.Add(self.browseButton, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        vSizer.Add(hSizer, 0, wx.EXPAND|wx.BOTTOM, 3)
        
        self.moveFiles = wx.CheckBox(self, -1, 'Move files from current destination to new destination')
        self.ignoreFiles = wx.CheckBox(self, -1, 'Do not overwrite files already existing at new destination')
        
        vSizer.Add(self.moveFiles)
        vSizer.Add(self.ignoreFiles)
        
        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
        
        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)
        
        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND|wx.BOTTOM, 3)
        
        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND|wx.ALL, 10)
        self.SetSizer(sizer)
        
    def OnBrowse(self, event = None):
        dlg = wx.DirDialog(self,"Choose a new destination directory", style = wx.DEFAULT_DIALOG_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.destTextCtrl.SetValue(dlg.GetPath())
                
        dlg.Destroy()
        
    def GetSettings(self):
        selectedDownloads = []
        for i in range(self.listCtrl.GetItemCount()):
            if self.listCtrl.IsSelected(i):
                selectedDownloads.append(self.download_states[i])
         
        new_dir = self.destTextCtrl.GetValue()
        moveFiles = self.moveFiles.GetValue()
        ignoreIfExists = self.ignoreFiles.GetValue()
        return selectedDownloads, new_dir, moveFiles, ignoreIfExists

    def OnOk(self, event = None):
        if self.destTextCtrl.GetValue() != '':
            self.EndModal(wx.ID_OK)
        
    def OnCancel(self, event = None):
        self.EndModal(wx.ID_CANCEL)
Exemple #6
0
    def __init__(self, parent, labels, download_states):
        wx.Dialog.__init__(self, parent, -1, 'Please select the torrents you want to move', size=(750,450))
        
        self.download_states = download_states
        vSizer = wx.BoxSizer(wx.VERTICAL)
        message = 'Please select all torrents which should be moved'
        message += "\nUse ctrl+a to select all/deselect all."
        
        firstLine = wx.StaticText(self, -1, message)
        _set_font(firstLine, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND|wx.BOTTOM, 3)
              
        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Torrent')
        self.listCtrl.InsertColumn(1, 'Current Location')
        
        self.listCtrl.setResizeColumn(0)
        
        for i, label in enumerate(labels):
            row = self.listCtrl.InsertStringItem(sys.maxint, label)
            
            download = download_states[i].get_download()
            self.listCtrl.SetStringItem(row, 1, download.get_dest_dir())

        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        vSizer.Add(self.listCtrl, 1, wx.EXPAND|wx.BOTTOM|wx.TOP, 3)

        self.destTextCtrl = wx.TextCtrl(self)
        self.browseButton = wx.Button(self, -1, 'Browse')
        self.browseButton.Bind(wx.EVT_BUTTON, self.OnBrowse)
        
        moveTo = wx.StaticText(self, -1, 'Move to:')
        _set_font(moveTo, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(moveTo)
        vSizer.Add(wx.StaticText(self, -1, 'Please note that all multi-file torrents create a directory themselves.\nYour new destination should specify the base dir for all torrents.'))
        
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(self.destTextCtrl, 1, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        hSizer.Add(self.browseButton, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        vSizer.Add(hSizer, 0, wx.EXPAND|wx.BOTTOM, 3)
        
        self.moveFiles = wx.CheckBox(self, -1, 'Move files from current destination to new destination')
        self.ignoreFiles = wx.CheckBox(self, -1, 'Do not overwrite files already existing at new destination')
        
        vSizer.Add(self.moveFiles)
        vSizer.Add(self.ignoreFiles)
        
        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)
        
        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)
        
        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND|wx.BOTTOM, 3)
        
        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND|wx.ALL, 10)
        self.SetSizer(sizer)
Exemple #7
0
class SaveAs(wx.Dialog):

    def __init__(self, parent, tdef, defaultdir, defaultname, configfile, selectedFiles=None):
        wx.Dialog.__init__(self, parent, -1, 'Please specify a target directory', size=(600, 450), name="SaveAsDialog")

        self.filehistory = wx.FileHistory(25)
        self.config = wx.FileConfig(appName="Tribler", localFilename=configfile)
        self.filehistory.Load(self.config)
        self.defaultdir = defaultdir
        self.guiutility = GUIUtility.getInstance()
        self.listCtrl = None
        self.collected = None

        if self.filehistory.GetCount() > 0:
            lastUsed = self.filehistory.GetHistoryFile(0)
        else:
            lastUsed = defaultdir

        vSizer = wx.BoxSizer(wx.VERTICAL)

        if tdef:
            line = 'Please select a directory where to save:'
        else:
            line = 'Please select a directory where to save this torrent'

        firstLine = wx.StaticText(self, -1, line)
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND | wx.BOTTOM, 3)

        if tdef:
            torrentName = wx.StaticText(self, -1, tdef.get_name_as_unicode())
            torrentName.SetMinSize((1, -1))
            vSizer.Add(torrentName, 0, wx.EXPAND | wx.BOTTOM | wx.RIGHT, 3)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(wx.StaticText(self, -1, 'Save as:'), 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.BOTTOM, 3)

        choices = [self.filehistory.GetHistoryFile(i) for i in range(self.filehistory.GetCount())]
        if defaultdir not in choices:
            choices.append(defaultdir)

        if defaultname:
            choices.insert(0, os.path.join(lastUsed, defaultname))
            self.dirTextCtrl = wx.ComboBox(self, -1, os.path.join(lastUsed, defaultname), choices=choices, style=wx.CB_DROPDOWN)
        else:
            self.dirTextCtrl = wx.ComboBox(self, -1, lastUsed, choices=choices, style=wx.CB_DROPDOWN)
        self.dirTextCtrl.Select(0)

        hSizer.Add(self.dirTextCtrl, 1, wx.EXPAND | wx.RIGHT | wx.BOTTOM, 3)

        browseButton = wx.Button(self, -1, 'Browse')
        browseButton.Bind(wx.EVT_BUTTON, self.OnBrowseDir)
        hSizer.Add(browseButton)

        vSizer.Add(hSizer, 0, wx.EXPAND | wx.BOTTOM, 3)

        if tdef and tdef.get_files():
            self.AddFileList(tdef, selectedFiles, vSizer, len(vSizer.GetChildren()))
        elif isinstance(tdef, TorrentDefNoMetainfo):
            text = wx.StaticText(self, -1, "Attempting to retrieve .torrent...")
            _set_font(text, size_increment=1)
            ag = wx.animate.GIFAnimationCtrl(self, -1, os.path.join(self.guiutility.utility.getPath(), LIBRARYNAME, 'Main', 'vwxGUI', 'images', 'search_new.gif'))
            ag.Play()
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.AddStretchSpacer()
            sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, 10)
            sizer.Add(ag, 0, wx.ALIGN_CENTER_VERTICAL)
            sizer.AddStretchSpacer()
            vSizer.Add(sizer, 1, wx.EXPAND | wx.BOTTOM, 3)
            self.SetSize((600, 150))

            url = tdef.get_url()
            if url and url.startswith("magnet:"):
                retrieve_from_magnet = lambda: TorrentDef.retrieve_from_magnet(url, lambda tdef: wx.CallAfter(self.SetCollected, tdef), timeout=300)
                startWorker(None, retrieve_from_magnet, retryOnBusy=True, workerType="guiTaskQueue")
            else:
                torrentsearch_manager = self.guiutility.torrentsearch_manager

                def do_collect(delayedResult):
                    torrent = delayedResult.get()
                    if torrent:
                        def callback():
                            torrent_filename = torrentsearch_manager.getCollectedFilename(torrent)
                            tdef = TorrentDef.load(torrent_filename)
                            wx.CallAfter(self.SetCollected, tdef)
                        torrentsearch_manager.getTorrent(torrent, callback)

                def do_db():
                    return torrentsearch_manager.getTorrentByInfohash(tdef.get_infohash())
                startWorker(do_collect, do_db, retryOnBusy=True, priority=GUI_PRI_DISPERSY)

        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND | wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND | wx.ALL, 10)
        self.SetSizer(sizer)

    def AddFileList(self, tdef, selectedFiles, vSizer, index=None):
        vSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND | wx.BOTTOM, 10)

        firstLine = wx.StaticText(self, -1, "Content:")
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.BOTTOM, 3)

        vSizer.Add(wx.StaticText(self, -1, 'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'), 0, wx.BOTTOM, 3)

        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Name')
        self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

        # Add files
        def sort_by_size(a, b):
            return cmp(a[1], b[1])

        files = tdef.get_files_as_unicode_with_length()
        files.sort(sort_by_size, reverse=True)

        for filename, size in files:
            try:
                pos = self.listCtrl.InsertStringItem(sys.maxsize, filename)
            except:
                try:
                    pos = self.listCtrl.InsertStringItem(sys.maxsize, filename.decode('utf-8', 'ignore'))
                except:
                    print >> sys.stderr, "Could not format filename", self.torrent.name
            self.listCtrl.SetItemData(pos, pos)
            self.listCtrl.SetStringItem(pos, 1, self.guiutility.utility.size_format(size))

            if selectedFiles:
                self.listCtrl.CheckItem(pos, filename in selectedFiles)

        if selectedFiles == None:
            self.listCtrl.doSelectAll()

        self.listCtrl.setResizeColumn(0)
        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)  # autosize only works after adding rows
        vSizer.Add(self.listCtrl, 1, wx.EXPAND | wx.BOTTOM, 3)

        self.listCtrl.SetFocus()

        def OnKeyUp(event):
            if event.GetKeyCode() == wx.WXK_RETURN:
                self.OnOk()
            else:
                event.Skip()
        self.listCtrl.Bind(wx.EVT_KEY_UP, OnKeyUp)

    def SetCollected(self, tdef):
        self.collected = tdef
        self.SetSize((600, 450))
        vSizer = self.GetSizer().GetItem(0).GetSizer()
        hsizer = vSizer.GetItem(len(vSizer.GetChildren()) - 2).GetSizer()
        self.Freeze()
        hsizer.Clear(deleteWindows=True)
        vSizer.Remove(hsizer)
        self.AddFileList(tdef, None, vSizer, len(vSizer.GetChildren()) - 1)
        self.Layout()
        self.Refresh()
        self.Thaw()

    def GetCollected(self):
        return self.collected

    def GetPath(self):
        return self.dirTextCtrl.GetValue().strip()

    def GetSelectedFiles(self):
        if self.listCtrl:
            selected = self.listCtrl.GetSelectedItems()
            nrSelected = len(selected)

            if nrSelected > 0 and nrSelected < self.listCtrl.GetItemCount():
                files = []
                for index in selected:
                    files.append(self.listCtrl.GetItem(index, 0).GetText())
                return files
        return None

    def OnOk(self, event=None):
        if self.listCtrl:
            nrSelected = len(self.listCtrl.GetSelectedItems())
            if nrSelected == 0:
                dlg = wx.MessageDialog(self, "Please select at least one file to be downloaded using the checkboxes.", "Please select a file to be downloaded", wx.ICON_ERROR)
                dlg.ShowModal()
                dlg.Destroy()
                return

        path = self.GetPath()
        if not os.path.exists(path) or os.path.isfile(path):
            path, _ = os.path.split(path)
        self.filehistory.AddFileToHistory(path)

        self.filehistory.Save(self.config)
        self.config.Flush()

        self.EndModal(wx.ID_OK)

    def OnCancel(self, event=None):
        self.EndModal(wx.ID_CANCEL)

    def OnBrowseDir(self, event):
        dlg = wx.DirDialog(self, "Please select a directory to save this torrent", style=wx.wx.DD_NEW_DIR_BUTTON)
        dlg.SetPath(self.defaultdir)

        if dlg.ShowModal() == wx.ID_OK and os.path.isdir(dlg.GetPath()):
            self.dirTextCtrl.SetValue(dlg.GetPath())
        dlg.Destroy()
class SaveAs(wx.Dialog):
    def __init__(self, parent, tdef, defaultdir, defaultname, configfile, selectedFiles = None):
        wx.Dialog.__init__(self, parent, -1, 'Please specify a target directory', size=(600,450))

        self.filehistory = wx.FileHistory(25)
        self.config = wx.FileConfig(appName = "Tribler", localFilename = configfile)
        self.filehistory.Load(self.config)
        self.defaultdir = defaultdir
        self.guiutility = GUIUtility.getInstance()
        self.listCtrl = None
        self.collected = None

        if self.filehistory.GetCount() > 0:
            lastUsed = self.filehistory.GetHistoryFile(0)
        else:
            lastUsed = defaultdir

        vSizer = wx.BoxSizer(wx.VERTICAL)

        if tdef:
            line = 'Please select a directory where to save:'
        else:
            line = 'Please select a directory where to save this torrent'

        firstLine = wx.StaticText(self, -1, line)
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND|wx.BOTTOM, 3)

        if tdef:
            torrentName = wx.StaticText(self, -1, tdef.get_name_as_unicode())
            torrentName.SetMinSize((1, -1))
            vSizer.Add(torrentName, 0, wx.EXPAND|wx.BOTTOM|wx.RIGHT, 3)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(wx.StaticText(self, -1, 'Save as:'), 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.BOTTOM, 3)

        choices = [self.filehistory.GetHistoryFile(i) for i in range(self.filehistory.GetCount())]
        if defaultdir not in choices:
            choices.append(defaultdir)

        if defaultname:
            choices.insert(0, os.path.join(lastUsed, defaultname))
            self.dirTextCtrl = wx.ComboBox(self, -1, os.path.join(lastUsed, defaultname), choices = choices, style = wx.CB_DROPDOWN)
        else:
            self.dirTextCtrl = wx.ComboBox(self, -1, lastUsed, choices = choices, style = wx.CB_DROPDOWN)
        self.dirTextCtrl.Select(0)

        hSizer.Add(self.dirTextCtrl, 1, wx.EXPAND|wx.RIGHT|wx.BOTTOM, 3)

        browseButton = wx.Button(self, -1, 'Browse')
        browseButton.Bind(wx.EVT_BUTTON, self.OnBrowseDir)
        hSizer.Add(browseButton)

        vSizer.Add(hSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        if tdef and tdef.get_files():
            self.AddFileList(tdef, selectedFiles, vSizer, len(vSizer.GetChildren()))
        elif isinstance(tdef, TorrentDefNoMetainfo):
            text = wx.StaticText(self, -1, "Attempting to retrieve .torrent...")
            _set_font(text, size_increment = 1)
            ag = wx.animate.GIFAnimationCtrl(self, -1, os.path.join(self.guiutility.utility.getPath(), LIBRARYNAME, 'Main', 'vwxGUI', 'images', 'search_new.gif'))
            ag.Play()
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.AddStretchSpacer()
            sizer.Add(text, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 10)
            sizer.Add(ag, 0, wx.ALIGN_CENTER_VERTICAL)
            sizer.AddStretchSpacer()
            vSizer.Add(sizer, 1, wx.EXPAND|wx.BOTTOM, 3)
            self.SetSize((600,150))

            url = tdef.get_url()
            if url and url.startswith("magnet:"):
                retrieve_from_magnet = lambda: TorrentDef.retrieve_from_magnet(url, lambda tdef: wx.CallAfter(self.SetCollected, tdef))
                startWorker(None, retrieve_from_magnet, retryOnBusy = True, priority = GUI_PRI_DISPERSY)
            else:
                torrentsearch_manager = self.guiutility.torrentsearch_manager
                def do_collect(delayedResult):
                    torrent = delayedResult.get()
                    if torrent:
                        def callback():
                            torrent_filename = torrentsearch_manager.getCollectedFilename(torrent)
                            tdef = TorrentDef.load(torrent_filename)
                            wx.CallAfter(self.SetCollected, tdef)
                        torrentsearch_manager.getTorrent(torrent, callback)
                def do_db():
                    return torrentsearch_manager.getTorrentByInfohash(tdef.get_infohash())
                startWorker(do_collect, do_db, retryOnBusy = True, priority = GUI_PRI_DISPERSY)

        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND|wx.ALL, 10)
        self.SetSizer(sizer)

    def AddFileList(self, tdef, selectedFiles, vSizer, index = None):
        vSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.BOTTOM, 10)

        firstLine = wx.StaticText(self, -1, "Content:")
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.BOTTOM, 3)

        vSizer.Add(wx.StaticText(self, -1, 'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'), 0, wx.BOTTOM, 3)

        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Name')
        self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

        #Add files
        def sort_by_size(a, b):
            return cmp(a[1],b[1])

        files = tdef.get_files_as_unicode_with_length()
        files.sort(sort_by_size, reverse = True)

        for filename, size in files:
            try:
                pos = self.listCtrl.InsertStringItem(sys.maxint, filename)
            except:
                try:
                    pos = self.listCtrl.InsertStringItem(sys.maxint, filename.decode('utf-8','ignore'))
                except:
                    print >> sys.stderr, "Could not format filename", self.torrent.name
            self.listCtrl.SetItemData(pos, pos)
            self.listCtrl.SetStringItem(pos, 1, self.guiutility.utility.size_format(size))

            if selectedFiles:
                self.listCtrl.CheckItem(pos, filename in selectedFiles)

        if selectedFiles == None:
            self.listCtrl.doSelectAll()

        self.listCtrl.setResizeColumn(0)
        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE) #autosize only works after adding rows
        vSizer.Add(self.listCtrl, 1, wx.EXPAND|wx.BOTTOM, 3)

        self.listCtrl.SetFocus()
        def OnKeyUp(event):
            if event.GetKeyCode() == wx.WXK_RETURN:
                self.OnOk()
            else:
                event.Skip()                
        self.listCtrl.Bind(wx.EVT_KEY_UP, OnKeyUp)

    def SetCollected(self, tdef):
        self.collected = tdef
        self.SetSize((600,450))
        vSizer = self.GetSizer().GetItem(0).GetSizer()
        hsizer = vSizer.GetItem(len(vSizer.GetChildren())-2).GetSizer()
        self.Freeze()
        hsizer.Clear(deleteWindows = True)
        vSizer.Remove(hsizer)
        self.AddFileList(tdef, None, vSizer, len(vSizer.GetChildren())-1)
        self.Layout()
        self.Refresh()
        self.Thaw()

    def GetCollected(self):
        return self.collected

    def GetPath(self):
        return self.dirTextCtrl.GetValue().strip()

    def GetSelectedFiles(self):
        if self.listCtrl:
            selected = self.listCtrl.GetSelectedItems()
            nrSelected = len(selected)

            if nrSelected > 0 and nrSelected < self.listCtrl.GetItemCount():
                files = []
                for index in selected:
                    files.append(self.listCtrl.GetItem(index, 0).GetText())
                return files
        return None

    def OnOk(self, event = None):
        if self.listCtrl:
            nrSelected = len(self.listCtrl.GetSelectedItems())
            if nrSelected == 0:
                dlg = wx.MessageDialog(self, "Please select at least one file to be downloaded using the checkboxes.", "Please select a file to be downloaded", wx.ICON_ERROR)
                dlg.ShowModal()
                dlg.Destroy()
                return

        path = self.GetPath()
        if not os.path.exists(path) or os.path.isfile(path):
            path, _ = os.path.split(path)
        self.filehistory.AddFileToHistory(path)

        self.filehistory.Save(self.config)
        self.config.Flush()

        self.EndModal(wx.ID_OK)

    def OnCancel(self, event = None):
        self.EndModal(wx.ID_CANCEL)

    def OnBrowseDir(self, event):
        dlg = wx.DirDialog(self, "Please select a directory to save this torrent", style = wx.wx.DD_NEW_DIR_BUTTON)
        dlg.SetPath(self.defaultdir)

        if dlg.ShowModal() == wx.ID_OK and os.path.isdir(dlg.GetPath()):
            self.dirTextCtrl.SetValue(dlg.GetPath())
        dlg.Destroy()
Exemple #9
0
    def __init__(self, parent, labels, download_states):
        wx.Dialog.__init__(self, parent, -1, 'Please select the torrents you want to move', size=(750,450))

        self.download_states = download_states
        vSizer = wx.BoxSizer(wx.VERTICAL)
        message = 'Please select all torrents which should be moved'
        message += "\nUse ctrl+a to select all/deselect all."

        firstLine = wx.StaticText(self, -1, message)
        _set_font(firstLine, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND|wx.BOTTOM, 3)

        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Torrent')
        self.listCtrl.InsertColumn(1, 'Current Location')

        self.listCtrl.setResizeColumn(0)

        for i, label in enumerate(labels):
            row = self.listCtrl.InsertStringItem(sys.maxint, label)

            download = download_states[i].get_download()
            self.listCtrl.SetStringItem(row, 1, download.get_dest_dir())

        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        vSizer.Add(self.listCtrl, 1, wx.EXPAND|wx.BOTTOM|wx.TOP, 3)

        self.destTextCtrl = wx.TextCtrl(self)
        self.browseButton = wx.Button(self, -1, 'Browse')
        self.browseButton.Bind(wx.EVT_BUTTON, self.OnBrowse)

        moveTo = wx.StaticText(self, -1, 'Move to:')
        _set_font(moveTo, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(moveTo)
        vSizer.Add(wx.StaticText(self, -1, 'Please note that all multi-file torrents create a directory themselves.\nYour new destination should specify the base dir for all torrents.'))

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(self.destTextCtrl, 1, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        hSizer.Add(self.browseButton, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        vSizer.Add(hSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        self.moveFiles = wx.CheckBox(self, -1, 'Move files from current destination to new destination')
        self.ignoreFiles = wx.CheckBox(self, -1, 'Do not overwrite files already existing at new destination')

        vSizer.Add(self.moveFiles)
        vSizer.Add(self.ignoreFiles)

        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND|wx.ALL, 10)
        self.SetSizer(sizer)
Exemple #10
0
class MoveTorrents(wx.Dialog):
    def __init__(self, parent, labels, download_states):
        wx.Dialog.__init__(self, parent, -1, 'Please select the torrents you want to move', size=(750,450))

        self.download_states = download_states
        vSizer = wx.BoxSizer(wx.VERTICAL)
        message = 'Please select all torrents which should be moved'
        message += "\nUse ctrl+a to select all/deselect all."

        firstLine = wx.StaticText(self, -1, message)
        _set_font(firstLine, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND|wx.BOTTOM, 3)

        self.listCtrl = CheckSelectableListCtrl(self)
        self.listCtrl.InsertColumn(0, 'Torrent')
        self.listCtrl.InsertColumn(1, 'Current Location')

        self.listCtrl.setResizeColumn(0)

        for i, label in enumerate(labels):
            row = self.listCtrl.InsertStringItem(sys.maxint, label)

            download = download_states[i].get_download()
            self.listCtrl.SetStringItem(row, 1, download.get_dest_dir())

        self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE)
        vSizer.Add(self.listCtrl, 1, wx.EXPAND|wx.BOTTOM|wx.TOP, 3)

        self.destTextCtrl = wx.TextCtrl(self)
        self.browseButton = wx.Button(self, -1, 'Browse')
        self.browseButton.Bind(wx.EVT_BUTTON, self.OnBrowse)

        moveTo = wx.StaticText(self, -1, 'Move to:')
        _set_font(moveTo, fontweight = wx.FONTWEIGHT_BOLD)
        vSizer.Add(moveTo)
        vSizer.Add(wx.StaticText(self, -1, 'Please note that all multi-file torrents create a directory themselves.\nYour new destination should specify the base dir for all torrents.'))

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(self.destTextCtrl, 1, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        hSizer.Add(self.browseButton, 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT, 3)
        vSizer.Add(hSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        self.moveFiles = wx.CheckBox(self, -1, 'Move files from current destination to new destination')
        self.ignoreFiles = wx.CheckBox(self, -1, 'Do not overwrite files already existing at new destination')

        vSizer.Add(self.moveFiles)
        vSizer.Add(self.ignoreFiles)

        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND|wx.ALL, 10)
        self.SetSizer(sizer)

    def OnBrowse(self, event = None):
        dlg = wx.DirDialog(self,"Choose a new destination directory", style = wx.DEFAULT_DIALOG_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
            self.destTextCtrl.SetValue(dlg.GetPath())

        dlg.Destroy()

    def GetSettings(self):
        selectedDownloads = []
        for i in range(self.listCtrl.GetItemCount()):
            if self.listCtrl.IsSelected(i):
                selectedDownloads.append(self.download_states[i])

        new_dir = self.destTextCtrl.GetValue()
        moveFiles = self.moveFiles.GetValue()
        ignoreIfExists = self.ignoreFiles.GetValue()
        return selectedDownloads, new_dir, moveFiles, ignoreIfExists

    def OnOk(self, event = None):
        if self.destTextCtrl.GetValue() != '':
            self.EndModal(wx.ID_OK)

    def OnCancel(self, event = None):
        self.EndModal(wx.ID_CANCEL)
Exemple #11
0
    def __init__(self, parent, tdef, defaultdir, defaultname, configfile, selectedFiles = None):
        wx.Dialog.__init__(self, parent, -1, 'Please specify a target directory', size=(600,450))

        self.filehistory = wx.FileHistory(25)
        self.config = wx.FileConfig(appName = "Tribler", localFilename = configfile)
        self.filehistory.Load(self.config)
        self.defaultdir = defaultdir
        self.guiutility = GUIUtility.getInstance()
        self.listCtrl = None

        if self.filehistory.GetCount() > 0:
            lastUsed = self.filehistory.GetHistoryFile(0)
        else:
            lastUsed = defaultdir

        vSizer = wx.BoxSizer(wx.VERTICAL)

        if tdef:
            line = 'Please select a directory where to save:'
        else:
            line = 'Please select a directory where to save this torrent'

        firstLine = wx.StaticText(self, -1, line)
        _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
        vSizer.Add(firstLine, 0, wx.EXPAND|wx.BOTTOM, 3)

        if tdef:
            torrentName = wx.StaticText(self, -1, tdef.get_name_as_unicode())
            torrentName.SetMinSize((1, -1))
            vSizer.Add(torrentName, 0, wx.EXPAND|wx.BOTTOM|wx.RIGHT, 3)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(wx.StaticText(self, -1, 'Save as:'), 0, wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.BOTTOM, 3)

        choices = [self.filehistory.GetHistoryFile(i) for i in range(self.filehistory.GetCount())]
        if defaultdir not in choices:
            choices.append(defaultdir)

        if defaultname:
            choices.insert(0, os.path.join(lastUsed, defaultname))
            self.dirTextCtrl = wx.ComboBox(self, -1, os.path.join(lastUsed, defaultname), choices = choices, style = wx.CB_DROPDOWN)
        else:
            self.dirTextCtrl = wx.ComboBox(self, -1, lastUsed, choices = choices, style = wx.CB_DROPDOWN)
        self.dirTextCtrl.Select(0)

        hSizer.Add(self.dirTextCtrl, 1, wx.EXPAND|wx.RIGHT|wx.BOTTOM, 3)

        browseButton = wx.Button(self, -1, 'Browse')
        browseButton.Bind(wx.EVT_BUTTON, self.OnBrowseDir)
        hSizer.Add(browseButton)

        vSizer.Add(hSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        if tdef and tdef.get_files():
            vSizer.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.BOTTOM, 10)

            firstLine = wx.StaticText(self, -1, "Content:")
            _set_font(firstLine, fontweight=wx.FONTWEIGHT_BOLD)
            vSizer.Add(firstLine, 0, wx.BOTTOM, 3)

            vSizer.Add(wx.StaticText(self, -1, 'Use the checkboxes to choose which files to download.\nUse ctrl+a to select all/deselect all.'), 0, wx.BOTTOM, 3)

            self.listCtrl = CheckSelectableListCtrl(self)
            self.listCtrl.InsertColumn(0, 'Name')
            self.listCtrl.InsertColumn(1, 'Size', wx.LIST_FORMAT_RIGHT)

            #Add files
            def sort_by_size(a, b):
                return cmp(a[1],b[1])

            files = tdef.get_files_as_unicode_with_length()
            files.sort(sort_by_size, reverse = True)

            for filename, size in files:
                try:
                    pos = self.listCtrl.InsertStringItem(sys.maxint, filename)
                except:
                    try:
                        pos = self.listCtrl.InsertStringItem(sys.maxint, filename.decode('utf-8','ignore'))
                    except:
                        print >> sys.stderr, "Could not format filename", self.torrent.name
                self.listCtrl.SetItemData(pos, pos)
                self.listCtrl.SetStringItem(pos, 1, self.guiutility.utility.size_format(size))

                if selectedFiles:
                    self.listCtrl.CheckItem(pos, filename in selectedFiles)

            if selectedFiles == None:
                self.listCtrl.doSelectAll()

            self.listCtrl.setResizeColumn(0)
            self.listCtrl.SetColumnWidth(1, wx.LIST_AUTOSIZE) #autosize only works after adding rows
            vSizer.Add(self.listCtrl, 1, wx.EXPAND|wx.BOTTOM, 3)

            self.listCtrl.SetFocus()
            def OnKeyUp(event):
                if event.GetKeyCode() == wx.WXK_RETURN:
                    self.OnOk()
                else:
                    event.Skip()
            self.listCtrl.Bind(wx.EVT_KEY_UP, OnKeyUp)
        else:
            self.SetSize((600,150))

        cancel = wx.Button(self, wx.ID_CANCEL)
        cancel.Bind(wx.EVT_BUTTON, self.OnCancel)

        ok = wx.Button(self, wx.ID_OK)
        ok.Bind(wx.EVT_BUTTON, self.OnOk)

        bSizer = wx.StdDialogButtonSizer()
        bSizer.AddButton(cancel)
        bSizer.AddButton(ok)
        bSizer.Realize()
        vSizer.Add(bSizer, 0, wx.EXPAND|wx.BOTTOM, 3)

        sizer = wx.BoxSizer()
        sizer.Add(vSizer, 1, wx.EXPAND|wx.ALL, 10)
        self.SetSizer(sizer)