def get_files(self, file_descriptors): """download given list of file file_descriptor is a list: [ [split path], size ]""" if self.peer.server.current_state == self.peer.server.new_state: SecurityAlert(self.peer.peer_id, "Can't get files: peer's server not known yet") else: # display downlaod dialog if necessary if get_prefs("display_dl") \ and "wx" in sys.modules: print "xxx using wx" from solipsis.util.uiproxy import UIProxy from solipsis.services.profile.gui.DownloadDialog \ import DownloadDialog self.download_dlg = UIProxy(DownloadDialog( get_prefs("display_dl"), None, -1)) self.download_dlg.init() self.download_dlg.Show() # launch first download self.files = file_descriptors if self.files: split_path, size = self.files.pop() self.update_file(split_path[-1], size) self._connect(MESSAGE_FILES, format_data_file(split_path, size)) # create deferred to be called when all files downloaded deferred = defer.Deferred() self.files_deferred = deferred return self.files_deferred else: display_warning(_("Empty List"), _("No file selected to download"))
def __init__(self, options, parent, id, title, plugin=None, **kwds): self.plugin = plugin self.options = options args = (parent, id, title) # begin wxGlade: ViewerFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.viewer_book = wx.Notebook(self, -1, style=0) self.file_tab = wx.Panel(self.viewer_book, -1) self.blog_tab = wx.Panel(self.viewer_book, -1) self.view_tab = wx.Panel(self.viewer_book, -1) # Menu Bar self.viewer_menu = wx.MenuBar() self.SetMenuBar(self.viewer_menu) self.action_item = wx.Menu() self.export_item = wx.MenuItem(self.action_item, wx.NewId(), _("&Export HTML ...\tCtrl+E"), _("Write profile as HTML File"), wx.ITEM_NORMAL) self.action_item.AppendItem(self.export_item) self.quit_item = wx.MenuItem(self.action_item, wx.NewId(), _("&Close\tCtrl+W"), _("Close profile management"), wx.ITEM_NORMAL) self.action_item.AppendItem(self.quit_item) self.viewer_menu.Append(self.action_item, _("Action")) self.refresh_menu = wx.Menu() self.r_all_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("Refresh All"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_all_item) self.refresh_menu.AppendSeparator() self.r_profile_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("Profile\tCtrl+P"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_profile_item) self.r_blog_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("Blog\tCtrl+B"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_blog_item) self.r_files_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("List files\tCtrl+L"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_files_item) self.viewer_menu.Append(self.refresh_menu, _("Refresh")) self.status_item = wx.Menu() self.anonymous_item = wx.MenuItem(self.status_item, wx.NewId(), _("&Anonymous\tCtrl+A"), "", wx.ITEM_RADIO) self.status_item.AppendItem(self.anonymous_item) self.friend_item = wx.MenuItem(self.status_item, wx.NewId(), _("&Friend\tCtrl+F"), "", wx.ITEM_RADIO) self.status_item.AppendItem(self.friend_item) self.blacklisted_item = wx.MenuItem(self.status_item, wx.NewId(), _("&Ignore\tCtrl+I"), "", wx.ITEM_RADIO) self.status_item.AppendItem(self.blacklisted_item) self.viewer_menu.Append(self.status_item, _("Status")) self.help_menu = wx.Menu() self.about_item = wx.MenuItem(self.help_menu, wx.NewId(), _("About...\tCtrl+?"), "", wx.ITEM_NORMAL) self.help_menu.AppendItem(self.about_item) self.viewer_menu.Append(self.help_menu, _("Help")) # Menu Bar end self.statusbar = self.CreateStatusBar(1, 0) self.html_view = MyHtmlWindow(self.view_tab, -1) self.blog_panel = BlogPanel(self.blog_tab, -1) self.file_panel = FilePanel(self.file_tab, -1) self.__set_properties() self.__do_layout() # end wxGlade self.profile_dlg = UIProxy(ProfileDialog(parent, -1, plugin=self.plugin)) self.peer_dlg = UIProxy(BlogDialog(parent, -1, plugin=self.plugin)) self.file_dlg = UIProxy(FileDialog(parent, -1, plugin=self.plugin)) # quite different initialisation according to launched by navigator or not if self.options["standalone"]: self.import_item = wx.MenuItem(self.action_item, wx.NewId(), _("Import...\tCtrl+I"), _("Load a profile and add it in contact list"), wx.ITEM_NORMAL) self.action_item.AppendItem(self.import_item) self.bind_controls()
class ViewerFrame(wx.Frame): def __init__(self, options, parent, id, title, plugin=None, **kwds): self.plugin = plugin self.options = options args = (parent, id, title) # begin wxGlade: ViewerFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.viewer_book = wx.Notebook(self, -1, style=0) self.file_tab = wx.Panel(self.viewer_book, -1) self.blog_tab = wx.Panel(self.viewer_book, -1) self.view_tab = wx.Panel(self.viewer_book, -1) # Menu Bar self.viewer_menu = wx.MenuBar() self.SetMenuBar(self.viewer_menu) self.action_item = wx.Menu() self.export_item = wx.MenuItem(self.action_item, wx.NewId(), _("&Export HTML ...\tCtrl+E"), _("Write profile as HTML File"), wx.ITEM_NORMAL) self.action_item.AppendItem(self.export_item) self.quit_item = wx.MenuItem(self.action_item, wx.NewId(), _("&Close\tCtrl+W"), _("Close profile management"), wx.ITEM_NORMAL) self.action_item.AppendItem(self.quit_item) self.viewer_menu.Append(self.action_item, _("Action")) self.refresh_menu = wx.Menu() self.r_all_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("Refresh All"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_all_item) self.refresh_menu.AppendSeparator() self.r_profile_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("Profile\tCtrl+P"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_profile_item) self.r_blog_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("Blog\tCtrl+B"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_blog_item) self.r_files_item = wx.MenuItem(self.refresh_menu, wx.NewId(), _("List files\tCtrl+L"), "", wx.ITEM_NORMAL) self.refresh_menu.AppendItem(self.r_files_item) self.viewer_menu.Append(self.refresh_menu, _("Refresh")) self.status_item = wx.Menu() self.anonymous_item = wx.MenuItem(self.status_item, wx.NewId(), _("&Anonymous\tCtrl+A"), "", wx.ITEM_RADIO) self.status_item.AppendItem(self.anonymous_item) self.friend_item = wx.MenuItem(self.status_item, wx.NewId(), _("&Friend\tCtrl+F"), "", wx.ITEM_RADIO) self.status_item.AppendItem(self.friend_item) self.blacklisted_item = wx.MenuItem(self.status_item, wx.NewId(), _("&Ignore\tCtrl+I"), "", wx.ITEM_RADIO) self.status_item.AppendItem(self.blacklisted_item) self.viewer_menu.Append(self.status_item, _("Status")) self.help_menu = wx.Menu() self.about_item = wx.MenuItem(self.help_menu, wx.NewId(), _("About...\tCtrl+?"), "", wx.ITEM_NORMAL) self.help_menu.AppendItem(self.about_item) self.viewer_menu.Append(self.help_menu, _("Help")) # Menu Bar end self.statusbar = self.CreateStatusBar(1, 0) self.html_view = MyHtmlWindow(self.view_tab, -1) self.blog_panel = BlogPanel(self.blog_tab, -1) self.file_panel = FilePanel(self.file_tab, -1) self.__set_properties() self.__do_layout() # end wxGlade self.profile_dlg = UIProxy(ProfileDialog(parent, -1, plugin=self.plugin)) self.peer_dlg = UIProxy(BlogDialog(parent, -1, plugin=self.plugin)) self.file_dlg = UIProxy(FileDialog(parent, -1, plugin=self.plugin)) # quite different initialisation according to launched by navigator or not if self.options["standalone"]: self.import_item = wx.MenuItem(self.action_item, wx.NewId(), _("Import...\tCtrl+I"), _("Load a profile and add it in contact list"), wx.ITEM_NORMAL) self.action_item.AppendItem(self.import_item) self.bind_controls() def on_change_facade(self): self.blog_panel.on_change_facade() self.file_panel.on_change_facade() # EVENTS def bind_controls(self): """bind all controls with facade""" # actions self.Bind(wx.EVT_MENU, self.on_export, id=self.export_item.GetId()) self.Bind(wx.EVT_MENU, self.on_close, id=self.quit_item.GetId()) self.Bind(wx.EVT_CLOSE, self.on_close) if self.options["standalone"]: self.Bind(wx.EVT_MENU, self.on_add, id=self.import_item.GetId()) # refresh self.Bind(wx.EVT_MENU, self.on_get_all, id=self.r_all_item.GetId()) self.Bind(wx.EVT_MENU, self.on_get_profile, id=self.r_profile_item.GetId()) self.Bind(wx.EVT_MENU, self.on_get_blog, id=self.r_blog_item.GetId()) self.Bind(wx.EVT_MENU, self.on_get_files, id=self.r_files_item.GetId()) # change status self.Bind(wx.EVT_MENU, self.on_make_friend, id=self.friend_item.GetId()) self.Bind(wx.EVT_MENU, self.on_blacklist, id=self.blacklisted_item.GetId()) self.Bind(wx.EVT_MENU, self.on_anonymous, id=self.anonymous_item.GetId()) # about self.Bind(wx.EVT_MENU, self.on_about, id=self.about_item.GetId()) def on_export(self, evt): """export .html""" dlg = wx.FileDialog( self, message="Export HTML file as ...", defaultDir=get_prefs("profile_dir"), defaultFile="", wildcard="HTML File (*.html)|*.html", style=wx.SAVE) if dlg.ShowModal() == wx.ID_OK: path = dlg.GetPath() get_facade().export_profile(path) def on_close(self, evt): """hide application""" get_facade()._desc.save() if self.options["standalone"]: self._close() else: self.Hide() def _close(self): """termainate application""" self.profile_dlg.Destroy() self.peer_dlg.Destroy() self.file_dlg.Destroy() self.Destroy() sys.exit() def on_add(self, evt): dlg = wx.FileDialog( self, message="Add profile ...", defaultDir=get_prefs("profile_dir"), defaultFile="", wildcard="Solipsis file (*.prf)|*.prf", style=wx.OPEN) if dlg.ShowModal() == wx.ID_OK: directory, file_name = os.path.split(dlg.GetPath()[:-4]) peer_desc = PeerDescriptor(file_name, document=FileDocument()) peer_desc.load(directory=directory) blogs = peer_desc.blog loader = peer_desc.document get_facade().fill_data(peer_desc.node_id, loader) get_facade().fill_blog(peer_desc.node_id, blogs) get_facade().fill_shared_files(peer_desc.node_id, loader.get_shared_files()) self.display_profile(peer_desc) self.display_blog(peer_desc) self.display_files(peer_desc) def on_get_all(self, evt): """display peer's blog""" pass def on_get_profile(self, evt): """display peer's files""" peer_id = self.other_tab.get_peer_selected() if peer_id: peer_desc = get_facade().get_peer(peer_id) if peer_desc.connected: self.plugin.get_profile(peer_id) else: display_status(peer_desc.node_id, "not connected") self.display_profile(peer_desc) else: "no peer selected" def on_get_blog(self, evt): """display peer's blog""" peer_id = self.other_tab.get_peer_selected() if peer_id: peer_desc = get_facade().get_peer(peer_id) if peer_desc.connected: self.plugin.get_blog_file(peer_id) else: display_status(peer_desc.node_id, "not connected") self.display_blog(peer_desc) else: display_warning("no peer selected") def on_get_files(self, evt): """display peer's files""" peer_id = self.other_tab.get_peer_selected() if peer_id: peer_desc = get_facade().get_peer(peer_id) if peer_desc.connected: self.plugin.select_files(peer_id) else: display_status(peer_desc.node_id, "not connected") self.display_files(peer_desc) else: "no peer selected" def on_make_friend(self, evt): """end application""" peer_id = self.other_tab.get_peer_selected() if peer_id: get_facade().make_friend(peer_id) else: display_warning("no peer selected") def on_blacklist(self, evt): """end application""" peer_id = self.other_tab.get_peer_selected() if peer_id: get_facade().blacklist_peer(peer_id) else: display_warning("no peer selected") def on_anonymous(self, evt): """end application""" peer_id = self.other_tab.get_peer_selected() if peer_id: get_facade().unmark_peer(peer_id) else: display_warning("no peer selected") def on_about(self, evt): """display about""" # not modal because would freeze the wx thread while twisted # one goes on and initialize profile about_dlg = AboutDialog(get_prefs("disclaimer"), self, -1) about_dlg.Show() def display_profile(self, peer_desc): """display blog in dedicated window""" # profile dialog self.profile_dlg.Show(peer_desc) def display_blog(self, peer_desc): """display blog in dedicated window""" # blog dialog self.peer_dlg.SetTitle(peer_desc) # display self.peer_dlg.Show(peer_desc.blog) def display_files(self, peer_desc): """display blog in dedicated window""" # file dialog self.file_dlg.set_desc(peer_desc) self.file_dlg.SetTitle() # display files {repos: {names:tags}, } self.file_dlg.Show(files=peer_desc.document.get_shared_files()) def __set_properties(self): # begin wxGlade: ViewerFrame.__set_properties self.SetTitle(_("Profile Viewer")) self.SetSize((460, 500)) self.statusbar.SetStatusWidths([-1]) # statusbar fields statusbar_fields = [_("status")] for i in range(len(statusbar_fields)): self.statusbar.SetStatusText(statusbar_fields[i], i) # end wxGlade self.r_all_item.Enable(False) self.enable_peer_states(False) def __do_layout(self): # begin wxGlade: ViewerFrame.__do_layout viewer_sizer = wx.BoxSizer(wx.VERTICAL) file_sizer = wx.BoxSizer(wx.VERTICAL) blog_sizer = wx.BoxSizer(wx.VERTICAL) view_sizer = wx.BoxSizer(wx.HORIZONTAL) view_sizer.Add(self.html_view, 1, wx.EXPAND, 0) self.view_tab.SetAutoLayout(True) self.view_tab.SetSizer(view_sizer) view_sizer.Fit(self.view_tab) view_sizer.SetSizeHints(self.view_tab) blog_sizer.Add(self.blog_panel, 1, wx.EXPAND, 0) self.blog_tab.SetAutoLayout(True) self.blog_tab.SetSizer(blog_sizer) blog_sizer.Fit(self.blog_tab) blog_sizer.SetSizeHints(self.blog_tab) file_sizer.Add(self.file_panel, 1, wx.EXPAND, 0) self.file_tab.SetAutoLayout(True) self.file_tab.SetSizer(file_sizer) file_sizer.Fit(self.file_tab) file_sizer.SetSizeHints(self.file_tab) self.viewer_book.AddPage(self.view_tab, _("View")) self.viewer_book.AddPage(self.blog_tab, _("Blog")) self.viewer_book.AddPage(self.file_tab, _("Files")) viewer_sizer.Add(self.viewer_book, 1, wx.EXPAND, 0) self.SetAutoLayout(True) self.SetSizer(viewer_sizer) self.Layout() self.Centre() # end wxGlade def enable_peer_states(self, enable, status=PeerDescriptor.ANONYMOUS): """(Dis)Activate menu items""" # select correct status if status == PeerDescriptor.FRIEND: self.friend_item.Check(True) elif status == PeerDescriptor.BLACKLISTED: self.blacklisted_item.Check(True) else: self.anonymous_item.Check(True) # (dis)activate items self.r_all_item.Enable(enable) self.r_profile_item.Enable(enable) self.r_blog_item.Enable(enable) self.r_files_item.Enable(enable) self.anonymous_item.Enable(enable) self.blacklisted_item.Enable(enable) self.friend_item.Enable(enable)
class PeerClient(dict): """dictionary of all dowloads: {id(transport): }""" def __init__(self, peer, connect_method): dict.__init__(self) self.peer = peer self.connect = connect_method self.download_dlg = None # [ [split path], size] self.files = [] self.files_deferred = None def __setitem__(self, transport, download_msg): dict.__setitem__(self, id(transport), download_msg) def __getitem__(self, transport): try: return dict.__getitem__(self, id(transport)) except KeyError: raise SecurityAlert(transport.getPeer().host, _("Corrupted client")) # high level API # def auto_load(self): """download profile when meeting peer for the first time""" return self._connect(MESSAGE_HELLO) def get_profile(self): """download peer profile using self.get_file. Automatically called on client creation""" if self.peer.server.current_state == self.peer.server.new_state: SecurityAlert(self.peer.peer_id, "Can't get profile: peer's server not known yet") else: return self._connect(MESSAGE_PROFILE) def get_blog_file(self): """donload blog file using self.get_file""" if self.peer.server.current_state == self.peer.server.new_state: SecurityAlert(self.peer.peer_id, "Can't get blog: peer's server not known yet") else: return self._connect(MESSAGE_BLOG) def get_shared_files(self): """donload blog file using self.get_file""" if self.peer.server.current_state == self.peer.server.new_state: SecurityAlert(self.peer.peer_id, "Can't get list: peer's server not known yet") else: return self._connect(MESSAGE_SHARED) def get_files(self, file_descriptors): """download given list of file file_descriptor is a list: [ [split path], size ]""" if self.peer.server.current_state == self.peer.server.new_state: SecurityAlert(self.peer.peer_id, "Can't get files: peer's server not known yet") else: # display downlaod dialog if necessary if get_prefs("display_dl") \ and "wx" in sys.modules: print "xxx using wx" from solipsis.util.uiproxy import UIProxy from solipsis.services.profile.gui.DownloadDialog \ import DownloadDialog self.download_dlg = UIProxy(DownloadDialog( get_prefs("display_dl"), None, -1)) self.download_dlg.init() self.download_dlg.Show() # launch first download self.files = file_descriptors if self.files: split_path, size = self.files.pop() self.update_file(split_path[-1], size) self._connect(MESSAGE_FILES, format_data_file(split_path, size)) # create deferred to be called when all files downloaded deferred = defer.Deferred() self.files_deferred = deferred return self.files_deferred else: display_warning(_("Empty List"), _("No file selected to download")) # connection management # def _connect(self, command, data=None): # set download information message = self.peer.wrap_message(command, data) connector = self.connect(self.peer) deferred = defer.Deferred() download = DownloadMessage(connector.transport, deferred, message) self[connector.transport] = download # set callback if command == MESSAGE_HELLO: deferred.addCallback(self._on_hello) elif command == MESSAGE_PROFILE: deferred.addCallback(self._on_complete_profile) elif command == MESSAGE_BLOG: deferred.addCallback(self._on_complete_pickle) elif command == MESSAGE_SHARED: deferred.addCallback(self._on_complete_pickle) elif command == MESSAGE_FILES: deferred.addCallback(self._on_complete_file) else: raise ValueError("ERROR in _connect: %s not valid"% command) return deferred def rawDataReceived(self, transport, data): self[transport].write_data(data) self.update_download(self[transport].size) def _fail_client(self, transport, reason): display_warning("Action [%s] failed: %s"\ % (str(self[transport].message), reason)) self[transport].close(reason) def _on_connected(self, transport): self[transport].send_message() self[transport].setup_download() def _on_disconnected(self, transport, reason): self[transport].teardown_download() self[transport].close(reason) # callbacks # def _on_hello(self, donwload_msg): """callback when autoloading of profile successful""" document = read_document(donwload_msg.file) get_facade().set_data(self.peer.peer_id, document, flag_update=False) get_filter_facade().fill_data(self.peer.peer_id, document) def _on_complete_profile(self, donwload_msg): """callback when finished downloading profile""" return read_document(donwload_msg.file) def _on_complete_pickle(self, donwload_msg): """callback when finished downloading blog""" try: return pickle.load(donwload_msg.file) except Exception, err: display_error(_("Your version of Solipsis is not compatible " "with the peer'sone you wish to download from " "Make sure you both use the latest (%s)"\ % VERSION), title="Download error", error=err)