rhythmbox插件开发笔记1:简介&入门

rhythmbox是gnome下一款开源的音乐播放软件。ubuntu和fedora的桌面环境中均默认安装了rhythmbox。

rhythmbox架构非常灵活,几乎所有地方都可以用插件来修改。用户想实现什么功能,都可以通过插件来

实现。

为了方便开发第三方插件,官网提供了详细的插件开发教材。但是,它已经过时了:rhythmbox升级到2.99

的时候,修改了很多API(我猜是为了对应gnome3?? python3???)。这次蛋疼的不向下兼容的升级导致很多

旧的插件都不能用了,有的插件开发者为了对应rhythmbox3只得为rhythmbox2和3建立了不同的开发分支。

将基于版本2的rhythmbox开发的插件“移植”到版本3上的方法是通过xxx_rb3compat.py。

rhythmbox的插件主要由c和python开发。用c开发需要很丰富的GObject-based C背景知识,很显然为没有,

所以我尝试的方法是使用python开发。下面介绍使用python开发rhythmbox plugin(rhythmbox 版本>=2.99)

的具体方法。以我尝试开发的插件xiami为例



首先说插件的安装

一般来说,使用python开发的rhythmbox插件不需要安装,只需将代码文件和配置文件放到指定的文件夹,并且

按照一定的规则命名文件,插件就能被自动识别(和vim插件类似)。

插件可以放在三个地方
a /usr/share/lib/rhythmbox/plugins 一般情况下除了官方插件,不要把插件放到这里
b $HOME//.local/share/rhythmbox/plugins(这里和版本2的路径不同)
c 为了用c开发插件的开发者使用的路径,这里不做介绍


最基本的插件由两类文件组成:后缀名为py的源代码文件和后缀名为plugin(或者是rb-plugin)的配置文件

a配置文件。在$HOME//.local/share/rhythmbox/plugins/xiami文件夹下新建文件xiami.plugin
 1 [Plugin]
 2 Loader=python3
 3 Module=xiami
 4 IAge=2
 5 Name=RB Plugin For xiami
 6 Name[zh_CN]=虾米插件
 7 Description=This is a RB Plugin for xiami,still in test
 8 Description[zh_CN]=虾米的RB插件,还在测试中
 9 Authors=Liu Peng <solo_o@foxmail.com>
10 Copyright=2014@s0_0s
11 Website=http://fingerliu.github.io

这里需要注意的地方有三个,Loader这里和2.99以前的版本不同,貌似如果写python会导致无法加载插件;Module设定为你插件的名字,一般和文件夹名一致;IAge设定为2。


b源代码。在$HOME//.local/share/rhythmbox/plugins/xiami文件夹下新建文件xiami.py

这里和2.99版本以前也不相同。以前可以将文件命名为__init__.py,现在貌似必须命名为Module名(因为用了python3??)

 1 from gi.repository import GObject, RB, Peas
 2 import gettext
 3 gettext.install('rhythmbox',RB.locale_dir())
 4 class xiami (GObject.Object, Peas.Activatable):
 5         __gtype_name = 'xiamiPlugin'
 6         object = GObject.property(type=GObject.Object)
 7 
 8         def __init__(self):
 9                 GObject.Object.__init__(self)
10 
11         def do_activate(self):
12                 print ("Hello I am XiaMi")
13                 shell = self.object
14                 db = shell.props.db
15                 model = RB.RhythmDBQueryModel.new_empty(db)
16                 self.source = GObject.new(xiamiSource, shell=shell, name=_("XiaMi"), query_model=model)
17                 self.source.setup()
18                 group = RB.DisplayPageGroup.get_by_id("library")
19                 shell.append_display_page(self.source, group)
20 
21         def do_deactivate(self):
22                 print("deactivating XiaMi plugin")
23                 self.source.delete_thyself()
24                 self.source = None
25 
26 class xiamiSource(RB.Source):
27         def __init__(self,**kwargs):
28                 super(xiamiSource,self).__init__(kwargs)
29 
30         def setup(self):
31                 shell = self.props.shell
32                 songs = RB.EntryView(db=shell.props.db,shell_player=shell.props.shell_player,is_drag_source=False,is_drag_dest=False)
33                songs.append_column(RB.EntryViewColumn.TITLE,True)
34                 songs.set_model(self.props.query_model)
35                 songs.show_all()
36                 self.pack_start(songs,expand=True,fill=True,padding=-0)
37 
38 GObject.type_register(xiamiSource)

这个基本上是照抄的官方的例子,默认被放在/usr/lib/rhythmbox/sample-plugins/sample-python下。这个插件作了两件事:1 在控制台print了两句话 2在rhythmbox主界面的

库(library)里添加了一项xiami。



接下来就可以启动rhythmbox,加载我们的控件了

启动rhythmbox-->点击导航条上的rhythmbox-->插件-->进入的配置插件界面,选择新建的插件“虾米插件

 

然后就可以在主界面左侧的库中多了一个XiaMi

没有对齐的原因是因为没有添加icon,左侧是icon的位置。



注:我使用的系统是fedora20 32bit,python版本是3.3.2,rhythmbox版本是3.0.3



有用的链接:

[1] https://wiki.gnome.org/Apps/Rhythmbox/Plugins/ThirdParty

[2] https://wiki.gnome.org/Apps/Rhythmbox/Plugins/WritingGuide

[3] https://wiki.gnome.org/Apps/Rhythmbox/InternalDesign

[4]http://forum.ubuntu.org.cn/viewtopic.php?f=74&t=285988&start=0&sid=67551a4a2bd527cbcb894dd8ac1c121f

1列举了一些现有的第三方插件,可以借鉴它们是怎么写的。

2,3有些地方已经过时

4是前辈写的一个豆瓣FM的插件,不过貌似2.99以上的已经用不了这个插件了。

 
posted @ 2014-07-10 22:59  FingerLiu  阅读(1914)  评论(0编辑  收藏  举报