[译] 给XMind脑图文件添加Gnome缩略图显示 [Ubuntu]

Felix Yan | 2010-07-21 | 471 views

由于 XMind (脑图软件) 使用一种类似 OpenOffice.org 的文件格式, 我调整了那些脚本, 让它们也能为我的 XMind 脑图文件创建缩略图. 由于 XMind 文件尚无 mime 类型定义, 我们还需要花一些额外的功夫来解决这一问题.

1, 为 XMind 文件设置一个 mime 类型定义.
默认情况下, XMind 文件被识别为可执行/压缩文件(application/zip), 因此我们需要为 XMind 文件指定 mime 类型:

sudo gedit /usr/share/mime/packages/x-xmind.xml

我仅仅把一些东西简单的放在了一起, 让系统把 “.xmind” 为扩展名的文件认为 mime 类型 应用程序/x-xmind(application/x-xmind)

<?xml version="1.0" encoding="UTF-8"?>
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
   <mime-type type="application/x-xmind">
     <comment>XMind mindmap</comment>
     <glob pattern="*.xmind"/>
   </mime-type>
</mime-info>

保存这个文件之后, 更新 mime 类型的数据库:

sudo update-mime-database /usr/share/mime


2, 创建生成 XMind 文件缩略图的脚本

sudo gedit /usr/bin/xmind-thumbnailer

贴入下面的脚本, 然后记得检查一下 ICON_PATH 环境变量, 确认图标文件存在.
这个缩略图生成脚本和 OpenOffice.org 的差别很小: XMind 把它的缩略图文件用一个高分辨率的 JPEG 文件存储(我的测试文件生成的缩略图分辨率为 2091×973). 为了让这个缩略图更像图标一些, 我随手选取了 200px 作为长/宽的上限. (程序图标文件为 36×37 分辨率, 而且应当能在缩略图中被认出来)

#!/usr/bin/env python
 
import gnomevfs
import os
import sys
import zipfile
from PIL import Image, ImageEnhance
 
# Alter these varibles to change thumbnail look
ICON_PATH = "/usr/local/xmind/xmind-logo-36.png" # Change this path to alter icons
ICON_OPACITY = 0.6 #Opacity of the icon (between 0.0 and 1.0)
THUMBNAIL_BACKGROUND_COLOR = "white" # Color of the background
 
in_file_path = gnomevfs.get_local_path_from_uri(sys.argv[1])
out_file_path = sys.argv[2]
path_without_thumbs = os.getenv("HOME")+"/Templates" 
 
def get_icon(thumbnail_size):
	#Load icon
	icon = Image.open(ICON_PATH).convert("RGBA")
	#Set it's opacity
	icon = set_icon_opacity(icon,ICON_OPACITY)
	#And set it's position in thumbnail
	icon_posx=thumbnail_size[0]-icon.size[0]
	icon_posy=thumbnail_size[1]-icon.size[1]
	icon_width=thumbnail_size[0]
	icon_height=thumbnail_size[1]
	return {"image":icon,"position":(icon_posx,icon_posy,icon_width,icon_height)}	
 
def get_basic_thumbnail():
	#Find out if the file is not in Templates directory
	if in_file_path.find(path_without_thumbs)!=0:
		try:
			#Extract thumbnail from Xmind file and save it
			zip=zipfile.ZipFile(in_file_path,mode="r")
			picture=zip.read("Thumbnails/thumbnail.jpg")
			zip.close()
			thumbnail=open(out_file_path,"w")
			thumbnail.write(picture)
			thumbnail.write("/n")
			thumbnail.close()
			#Open saved thumbnail
			image=Image.open(out_file_path).convert("RGBA")
			if image.size[0]>200:
				image = image.resize((200,image.size[1]*200/image.size[0]))
			if image.size[1]>200:
				image = image.resize((image.size[0]*200/image.size[1],200))
			return {"suceeded":True,"image":image,"size":(image.size[0],image.size[1])}
 
		except:
			return {"suceeded":False}
	else:
		return {"suceeded":False}
 
# Nicked from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/362879
def set_icon_opacity(icon,opacity):
	#Returns an image with reduced opacity.
	assert opacity >= 0 and opacity <= 1
	if icon.mode != 'RGBA':
		icon = icon.convert('RGBA')
	else:
		icon = icon.copy()
	alpha = icon.split()[3]
	alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
	icon.putalpha(alpha)
	return icon
 
thumbnail=get_basic_thumbnail()
if thumbnail["suceeded"]:
	background=Image.new("RGB", thumbnail["size"], THUMBNAIL_BACKGROUND_COLOR)
	icon=get_icon(thumbnail["size"])
	thumbnail=thumbnail["image"]
	# Add thumbnail
	background.paste(thumbnail, None, thumbnail)
	# Add icon
	background.paste(icon["image"],icon["position"],icon["image"])
	# Save thumbnail
	background.save(out_file_path,"PNG")

给脚本加上可执行权限:

sudo chmod +x /usr/bin/xmind-thumbnailer

3, 设置 mime 类型使用这个缩略图生成脚本来生成缩略图:
创建 schema 文件来定义缩略图生成脚本的工作

sudo gedit /usr/share/gconf/schemas/xmind.schemas

贴入下面的内容:

<gconfschemafile>
    <schemalist>
 
        <schema>
            <key>/schemas/desktop/gnome/thumbnailers/application@x-xmind/enable</key>
            <applyto>/desktop/gnome/thumbnailers/application@x-xmind/enable</applyto>
            <owner>xmind-thumb</owner>
            <type>bool</type>
            <default>true</default>
            <locale name="C">
                <short></short>
                <long></long>
            </locale>
        </schema>
 
 
        <schema>
            <key>/schemas/desktop/gnome/thumbnailers/application@x-xmind/command</key>
            <applyto>/desktop/gnome/thumbnailers/application@x-xmind/command</applyto>
            <owner>xmind-thumb</owner>
            <type>string</type>
            <default>/usr/bin/xmind-thumbnailer %u %o</default>
            <locale name="C">
                <short></short>
                <long></long>
            </locale>
        </schema>
 
    </schemalist>
</gconfschemafile>

4, 激活新功能!
重启计算机即可使新的缩略图生成器生效, 或者你可以安装 schema 并重启 Nautilus.

gconftool-2 --install-schema-file /usr/share/gconf/schemas/xmind.schemas
sudo killall -9 nautilus

注意: 你应该以你”自己”用户的身份运行 gconftool-2, 以使这个 schema 文件被安装在正确的位置: 你的主目录.

感谢 Minio 发布 OpenOffice 的话题并进行技术支持, 以及每一个在那里提供自己的发现的朋友.

译者: Felix Yan
原文地址: http://ubuntuforums.org/showthread.php?t=1046678

  1. 黑传说 China Opera Linux says:

    询问一下,你的xmind运行速度如何?用的是jre还是openjdk?我的运行起来太慢了,而且所有java大型程序都这个结果,不知如何优化。

  2. BOYPT United States Mozilla Firefox Linux says:

    ….有事没事那么喜欢kill -9 。。。。

Post a comment

QR Code Business Card