示例#1
0
def deserialize_image(register_buf, content_buf, iter, data, length,
                      create_tags, user_data):
    # Implementation note: we follow gtk_selection_get_pixbuf() in usage of
    # Gtk.PixbufLoader to capture clipboard data in a pixbuf object.
    # We could skip this, but it allows for on-the-fly conversion of the data
    # type.
    mimetype, notebook, path = user_data

    # capture image
    loader = GdkPixbuf.PixbufLoader()
    loader.write(data)
    loader.close()
    pixbuf = loader.get_pixbuf()

    # save it as an attachment
    dir = notebook.get_attachments_dir(path)
    if not dir.exists():
        logger.debug("Creating attachment dir: %s", dir)
        dir.touch()

    format, extension = _get_image_info(mimetype)
    if format is None or format == 'bmp':
        # default to png format
        # special casing bmp since many window apps use it internally
        # but is quite large to store, so compress by using png
        format, extension = 'png', 'png'

    file = dir.new_file('pasted_image.%s' % extension)
    logger.debug("Saving image from clipboard to %s", file)
    pixbuf.savev(file.path, format, [], [])
    FS.emit('path-created', file)  # notify version control

    # and insert it in the page
    links = [file.uri]
    tree = _link_tree(links, notebook, path)
    content_buf.insert_parsetree(iter, tree, interactive=True)
    return True
示例#2
0
def parsetree_from_selectiondata(selectiondata,
                                 notebook=None,
                                 path=None,
                                 text_format='plain'):
    '''Function to get a parsetree based on the selectiondata contents
	if at all possible. Used by both copy-paste and drag-and-drop
	methods.

	The 'notebook' and optional 'path' arguments are used to format
	links relative to the page which is the target for the pasting or
	drop operation.

	For image data, the parameters notebook and page are used
	to save the image to the correct attachment folder and return a
	parsetree with the correct image link.

	@param selectiondata: a C{Gtk.SelectionData} object
	@param notebook: a L{Notebook} object
	@param path: a L{Path} object
	@param text_format: format to parse pasted text, as a special case

		- "verbatim" will wrap content in VERBARIM_BLOCK or VERBATIM element based on the content
		- "verbatim-pre" will wrap the content in a VERBATIM_BLOCK element and
		- "verbatim-code" will wrap the content in a VERBATIM element

	@returns: a L{ParseTree} or C{None}
	'''
    # TODO: check relative linking for all parsetrees !!!

    targetname = selectiondata.get_target().name()
    if targetname == PARSETREE_TARGET_NAME:
        return ParseTree().fromstring(selectiondata.get_data())
    elif targetname in (INTERNAL_PAGELIST_TARGET_NAME, PAGELIST_TARGET_NAME) \
    or targetname in URI_TARGET_NAMES:
        links = selectiondata.get_uris()
        return _link_tree(links, notebook, path)
    elif targetname in TEXT_TARGET_NAMES:
        # plain text parser should highlight urls etc.
        # FIXME some apps drop text/uri-list as a text/plain mimetype
        # try to catch this situation by a check here
        text = selectiondata.get_text()
        if text:
            if text_format in ('verbatim', 'verbatim-pre', 'verbatim-code'):
                if text_format == 'verbatim':
                    tag_name = 'pre' if '\n' in text else 'code'
                else:
                    tag_name = text_format[9:]
                builder = ParseTreeBuilder(partial=True)
                builder.start('zim-tree', {})
                builder.start(tag_name, {})
                builder.text(text)
                builder.end(tag_name)
                builder.end('zim-tree')
                return builder.get_parsetree()
            else:
                return get_format(text_format).Parser().parse(text,
                                                              partial=True)
        else:
            return None
    elif targetname in IMAGE_TARGET_NAMES:
        # save image
        pixbuf = selectiondata.get_pixbuf()
        if not pixbuf:
            return None

        dir = notebook.get_attachments_dir(path)
        assert isinstance(dir, LocalFolder) or hasattr(
            dir, '_folder') and isinstance(dir._folder, LocalFolder)
        # XXX: assert we have local path  - HACK to deal with FilesAttachmentFolder
        if not dir.exists():
            logger.debug("Creating attachment dir: %s", dir)
            dir.touch()

        format, extension = _get_image_info(targetname)
        if format is None or format == 'bmp':
            # default to png format
            # special casing bmp since many window apps use it internally
            # but is quite large to store, so compress by using png
            format, extension = 'png', 'png'

        file = dir.new_file('pasted_image.%s' % extension)
        logger.debug("Saving image from clipboard to %s", file)
        pixbuf.savev(file.path, format, [], [])
        FS.emit('path-created', file)  # notify version control

        links = [file.uri]
        return _link_tree(links, notebook, path)
    else:
        return None
示例#3
0
def parsetree_from_selectiondata(selectiondata, notebook=None, path=None):
	'''Function to get a parsetree based on the selectiondata contents
	if at all possible. Used by both copy-paste and drag-and-drop
	methods.

	The 'notebook' and optional 'path' arguments are used to format
	links relative to the page which is the target for the pasting or
	drop operation.

	For image data, the parameters notebook and page are used
	to save the image to the correct attachment folder and return a
	parsetree with the correct image link.

	@param selectiondata: a C{gtk.SelectionData} object
	@param notebook: a L{Notebook} object
	@param path: a L{Path} object

	@returns: a L{ParseTree} or C{None}
	'''
	# TODO: check relative linking for all parsetrees !!!

	targetname = str(selectiondata.target)
	if targetname == PARSETREE_TARGET_NAME:
		return ParseTree().fromstring(selectiondata.data)
	elif targetname in (INTERNAL_PAGELIST_TARGET_NAME, PAGELIST_TARGET_NAME) \
	or targetname in URI_TARGET_NAMES:
		links = unpack_urilist(selectiondata.data)
		return _link_tree(links, notebook, path)
	elif targetname in TEXT_TARGET_NAMES:
		# plain text parser should highlight urls etc.
		# FIXME some apps drop text/uri-list as a text/plain mimetype
		# try to catch this situation by a check here
		text = selectiondata.get_text()
		if text:
			return get_format('plain').Parser().parse(text.decode('utf-8'), partial=True)
		else:
			return None
	elif targetname in IMAGE_TARGET_NAMES:
		# save image
		pixbuf = selectiondata.get_pixbuf()
		if not pixbuf:
			return None

		dir = notebook.get_attachments_dir(path)
		if not dir.exists():
			logger.debug("Creating attachment dir: %s", dir)
			dir.touch()

		format, extension = _get_image_info(targetname)
		if format is None or format == 'bmp':
			# default to png format
			# special casing bmp since many window apps use it internally
			# but is quite large to store, so compress by using png
			format, extension = 'png', 'png'

		file = dir.new_file('pasted_image.%s' % extension)
		logger.debug("Saving image from clipboard to %s", file)
		pixbuf.save(file.path, format)
		FS.emit('path-created', file) # notify version control

		links = [file.uri]
		return _link_tree(links, notebook, path)
	else:
		return None
示例#4
0
def parsetree_from_selectiondata(selectiondata, notebook=None, path=None):
	'''Function to get a parsetree based on the selectiondata contents
	if at all possible. Used by both copy-paste and drag-and-drop
	methods.

	The 'notebook' and optional 'path' arguments are used to format
	links relative to the page which is the target for the pasting or
	drop operation.

	For image data, the parameters notebook and page are used
	to save the image to the correct attachment folder and return a
	parsetree with the correct image link.

	@param selectiondata: a C{gtk.SelectionData} object
	@param notebook: a L{Notebook} object
	@param path: a L{Path} object

	@returns: a L{ParseTree} or C{None}
	'''
	# TODO: check relative linking for all parsetrees !!!

	targetname = str(selectiondata.target)
	if targetname == PARSETREE_TARGET_NAME:
		return ParseTree().fromstring(selectiondata.data)
	elif targetname in (INTERNAL_PAGELIST_TARGET_NAME, PAGELIST_TARGET_NAME) \
	or targetname in URI_TARGET_NAMES:
		links = unpack_urilist(selectiondata.data)
		return _link_tree(links, notebook, path)
	elif targetname in TEXT_TARGET_NAMES:
		# plain text parser should highlight urls etc.
		# FIXME some apps drop text/uri-list as a text/plain mimetype
		# try to catch this situation by a check here
		text = selectiondata.get_text()
		if text:
			return get_format('plain').Parser().parse(text.decode('utf-8'), partial=True)
		else:
			return None
	elif targetname in IMAGE_TARGET_NAMES:
		# save image
		pixbuf = selectiondata.get_pixbuf()
		if not pixbuf:
			return None

		dir = notebook.get_attachments_dir(path)
		if not dir.exists():
			logger.debug("Creating attachment dir: %s", dir)
			dir.touch()

		format, extension = _get_image_info(targetname)
		if format is None or format == 'bmp':
			# default to png format
			# special casing bmp since many window apps use it internally
			# but is quite large to store, so compress by using png
			format, extension = 'png', 'png'

		file = dir.new_file('pasted_image.%s' % extension)
		logger.debug("Saving image from clipboard to %s", file)
		pixbuf.save(file.path, format)
		FS.emit('path-created', file) # notify version control

		links = [file.uri]
		return _link_tree(links, notebook, path)
	else:
		return None