def open_menu(self, menu): # always work on a copy menu = menu[:] if (menu): menu.append(MenuItem("/__separator")) menu += self.__DISPLAY_MENU mainmenu = gtk.Menu() tree = {} for entry in menu: if (not entry.label): item = gtk.SeparatorMenuItem() elif (entry.icon): if (gtk.stock_lookup(entry.icon)): item = gtk.ImageMenuItem(entry.icon) if (entry.label): item.get_children()[0].set_text_with_mnemonic(entry.label) else: item = gtk.ImageMenuItem(entry.label) try: img = Tiling() data = vfs.read_entire_file(self.get_full_path(entry.icon)) img.set_from_data(data) img.render(16, 16, 1, 1) img.show() item.set_image(img) except: import traceback; traceback.print_exc() else: item = gtk.MenuItem(entry.label) if (entry.callback): item.connect("activate", lambda src, cb, args: cb(*args), entry.callback, entry.args) item.show() if (not entry.active): item.set_sensitive(False) menupath = "/".join(entry.path.split("/")[:-1]) if (menupath): parentitem = tree.get(menupath) if (not parentitem.get_submenu()): m = gtk.Menu() parentitem.set_submenu(m) else: m = parentitem.get_submenu() else: m = mainmenu m.append(item) tree["/".join(entry.path.split("/"))] = item mainmenu.popup(None, None, None, 0, 0)
def _setp_uri(self, key, value): uri = self._get_display().get_full_path(value) try: data = vfs.read_entire_file(uri) except: log("Couldn't read file %s.\n" % uri) return self.__render(data) self._setp(key, value)
def __read_meta(self): """ Reads the meta information in the file. """ data = vfs.read_entire_file(self.__file) #open(self.__file).read() meta = self.__find_meta(data)[1] if (meta): self.__values = self.__parse_meta(meta) if self.KEY_NAME not in self.__values: self.__values[self.KEY_NAME] = \ os.path.splitext(os.path.basename(self.__file))[0]
def __set_border(self, args): cnt = 0 for uri in args: uri = self._get_display().get_full_path(uri) if (vfs.exists(uri)): try: data = vfs.read_entire_file(uri) except: return self.__images[cnt].set_from_data(data) cnt += 1 self.__redraw_frame()
def __load_image(self, uri): if (not uri): return try: data = vfs.read_entire_file(uri) except: log("Couldn't read file %s.\n" % uri) return try: self.__widget.set_from_data(data) except RuntimeError, exc: log(`exc`)
def set_loading(self, path): from MetaData import MetaData try: meta = MetaData(path) preview = meta.get(meta.KEY_PREVIEW) except Exception: preview = "" dirname = os.path.dirname(path) icon = os.path.join(dirname, preview) self.__label.set_markup("<big><b>Loading:</b>\n%s</big>" % path) self.__icon.hide() if preview: try: # for him who loves small try blocks: you don't gain anything # from splitting up this block except for badly readable code ;) from utils import vfs data = vfs.read_entire_file(icon) loader = gtk.gdk.PixbufLoader() loader.write(data, len(data)) loader.close() pbuf = loader.get_pixbuf() # scale icon down while preserving aspect ratio width = pbuf.get_width() height = pbuf.get_height() scale = 48 / float(height) width *= scale if width > 96: width = 96 if abs(scale - 1.0) > 0.001: pbuf = pbuf.scale_simple(int(width), 48, 3) self.__icon.set_from_pixbuf(pbuf) self.__icon.show() except Exception: pass # end if self.resize(10, 10) self.set_size_request(-1, -1)
def __create_display(self, ident, path): try: data = vfs.read_entire_file(path) except Exception: log("Could not open desklet file \"%s\"." % (path,)) raise UserError(_("Could not open desklet file \"%s\"") % (path,), _("The desklet file could not be opened because " "the file was not readable."), show_details = False) display = self.__factory.create_display(ident, data, path) return display
def __set_background(self, uri): if (not uri): self.__set_color("#00000000") return from utils import vfs if (vfs.exists(uri)): try: data = vfs.read_entire_file(uri) except: return w, h = self.__layout.size_request() self.__image.set_from_data(data) self.__image.tile(w, h)
def __set_icon(self, uri): if (not uri): return uri = self._get_display().get_full_path(uri) loader = gtk.gdk.PixbufLoader() try: data = vfs.read_entire_file(uri) except: return try: loader.write(data, len(data)) except: log("Invalid image format.") return loader.close() pbuf = loader.get_pixbuf() self.__icon.set_from_pixbuf(pbuf)
def set_banner(self, icon, text): self.__banner = gtk.Frame() self.__banner.set_shadow_type(gtk.SHADOW_IN) self.__banner.show() b_box = gtk.HBox(spacing = 12) b_box.set_border_width(6) b_box.show() self.__banner.add(b_box) if (icon): try: # for him who loves small try blocks: you don't gain anything # from splitting up this block except for badly readable code ;) from utils import vfs data = vfs.read_entire_file(icon) loader = gtk.gdk.PixbufLoader() loader.write(data, len(data)) loader.close() pbuf = loader.get_pixbuf() # scale icon down while preserving aspect ratio width = pbuf.get_width() height = pbuf.get_height() scale = 48 / float(height) width *= scale if (abs(scale - 1.0) > 0.001): pbuf = pbuf.scale_simple(int(width), 48, 3) b_icon = gtk.Image() b_icon.set_from_pixbuf(pbuf) b_icon.show() b_box.pack_start(b_icon, False, False) except: pass #end if b_label = gtk.Label(text) b_label.set_use_markup(True) b_label.show() b_box.pack_start(b_label, True, True)
def set_prop(self, key, value): if (key == "window-flags"): self.update_observer(self.OBS_FLAGS, value) elif (key == "desktop-borders"): self.update_observer(self.OBS_BORDERS, value) elif (key == "title"): self.update_observer(self.OBS_TITLE, value) elif (key == "icon"): filename = self.get_full_path(value) loader = gtk.gdk.PixbufLoader() try: data = vfs.read_entire_file(filename) except: return try: loader.write(data, len(data)) except: log("Warning: Invalid image format.") return loader.close() pixbuf = loader.get_pixbuf() self.update_observer(self.OBS_ICON, pixbuf) elif (key == "shape"): if (value.lstrip().startswith("<")): from utils.DOM import DOM try: from utils import svg except ImportError: log("Could not import svg module!") return w, h, = self.size_request() if (not w or not h): return root = DOM(value).get_root() root["width"] = `w` root["height"] = `h` img = gtk.Image() svg.render(img, w, h, str(root)) pixbuf = img.get_pixbuf() else: filename = self.get_full_path(value) loader = gtk.gdk.PixbufLoader() try: data = vfs.read_entire_file(filename) except: log("Couldn't read file %s", (filename,)) return try: loader.write(data, len(data)) except: log("Warning: Invalid image format.") return loader.close() pixbuf = loader.get_pixbuf() pix, mask = pixbuf.render_pixmap_and_mask(1) self.update_observer(self.OBS_SHAPE, mask)
def set_metadata(self, metadata): pbuf = None fbuf = None rbuf = None preview = metadata.get("preview", "") name = metadata.get("name", "") version = metadata.get("version", "") copyright = metadata.get("copyright", "") comments = metadata.get("comments", "") license = metadata.get("license", "") website = metadata.get("website", "") author = metadata.get("author", "") category = metadata.get("category", "") dependency = metadata.get("dependency", "") description = metadata.get("description", "") + "\n" # a banner without the display's name would look crappy if (name): if (preview): preview = self.get_full_path(preview) data = vfs.read_entire_file(preview) loader = gtk.gdk.PixbufLoader() loader.write(data, len(data)) loader.close() pbuf = loader.get_pixbuf() self.__configurator.set_banner(preview, "<big>%s</big> %s\n" "<small>%s</small>" % (name, version, author)) # include the LICENSE file if available for elem in ("COPYING", "LICENSE"): try: fbuf = open(self.get_full_path(elem), "r") except: pass if (fbuf): license = fbuf.read() else: log("Warning: COPYING or LICENSE not included in desklet \"%s %s\".\nPlease contact the author!" % (name, version)) # include the README file if available try: rbuf = open(self.get_full_path("README"), "r") except: log("README file not included in desklet!") if (rbuf): self.__readme_button = \ self.__about.add_button(_("_Readme"), 2003) self.__about.action_area.reorder_child(self.__readme_button, gtk.PACK_START) self.__readme_buffer.set_text(rbuf.read()) # add comments to the description if (comments): description = "%s\n%s\n" % (description, comments) # feed the About Window with the Metadata self.__about.set_name(name) self.__about.set_version(version) self.__about.set_copyright(copyright) self.__about.set_comments(description) if (license): self.__about.set_license(license) self.__about.set_website(website) self.__about.set_authors([author]) self.__about.set_logo(pbuf)
def endElement(self, name): oname, attrs = self.__nesting_stack.pop() state = self.__state[-1] if (not name == oname): # nesting errors in XML are detected by the SAX parser; if we # get here, it means our parser is buggy, not the XML input log("Nesting error: expected %s, got %s." % (oname, name)) return elif (state == _STATE_DISPLAY): settings = self.__create_settings(attrs) # if there is no ID given, guess a unique one ident = attrs.get("id", Display.make_id()) settings["id"] = ident # build the tree of children from bottom up children = self.__children_stack.pop() self.__children_stack[-1].append((name, settings, children)) elif (state == _STATE_META): self.__metadata = attrs elif (state == _STATE_SCRIPT): # Some distros ship a broken SAX parser and # their users are too lazy to reportbug. (doesn't have __contains__) # So we have to use has_key() # if ((name == "script") and ("uri" in attrs)): if (name == "script" and attrs.has_key("uri")): path = attrs["uri"] filename = os.path.join(self.__path, path) try: self.__script += "\n" + vfs.read_entire_file(filename) except: log("File doesn't exist or couldn't be loaded: %s" % (path,)) dialog.warning(_("File doesn't exist or couldn't be " "loaded"), _("A file which contains a script isn't " "available for further execution.")) else: filename = self.__filename # collect script by script to be able to execute them one by one # later; we would get problems with different indentation otherwise self.__scripts.append((self.__script, filename)) # deprecated sensor stuff elif (state == _STATE_SENSOR): from utils import typeconverter ident = attrs["id"] moduledata = typeconverter.str2type(TYPE_LIST, attrs["module"]) module = moduledata[0] args = moduledata[1:] sensorctrl = self.__control_factory.get_control(_SENSOR_CONTROL) if (sensorctrl): sensorctrl.sensor = (module, args) sensorctrl.config_id = self.__id + ident self.__sensors.append((ident, sensorctrl)) else: raise RuntimeError(_("Could not load sensor")) elif (state == _STATE_CONTROL): ident = attrs["id"] interface = attrs["interface"] if not attrs.has_key("length"): attrs["length"] = "0" length = int(attrs["length"]) script = "%s = get_control('%s', %d)" % \ (ident, interface, length) self.__scripts.append((script, self.__filename)) elif (state == _STATE_PREFS): if (name == "prefs"): pass else: if (name in ("boolean", "color", "date", "enum", "float", "font", "integer", "list", "radio", "string", "toggle", "uri") and not attrs.has_key("bind")): dialog.warning(_("<%s> needs a bind attribute") % \ (name,), _("The <prefs> section of this " "desklet file is broken.")) raise AttributeError("<%s> needs a bind attribute" % \ (name,)) # use global prefs callback if no callback was given if (self.__prefs_callback and not attrs.has_key("callback")): attrs["callback"] = self.__prefs_callback self.__config_items.append((name, attrs)) if (name in ("display", "sensor", "control", "meta", "prefs", "script")): self.__state.pop()