~eduardo-mucelli/cairo-dock-plug-ins-extras/Translator

« back to all changes in this revision

Viewing changes to demos/demo_python/demo_python

  • Committer: matttbe
  • Date: 2009-10-31 00:15:04 UTC
  • Revision ID: matttbe-20091031001504-pv63vnka300fglr2
Added demos (bash and python) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
### We assume the name of this applet is "demo_python"
 
3
### Copy this folder into ~/.config/cairo-dock/third-party.
 
4
### In the folder we have :
 
5
### demo_python (the executable script), demo_python.conf (the default config file), "icon" (the default icon of the applet) and "preview" (a preview of this applet)
 
6
 
 
7
### import ###
 
8
import sys
 
9
import gobject
 
10
import glib
 
11
import gtk
 
12
import dbus
 
13
import os.path
 
14
from dbus.mainloop.glib import DBusGMainLoop
 
15
 
 
16
 
 
17
### let's connect to the dock. ###
 
18
DBusGMainLoop(set_as_default=True)
 
19
bus = dbus.SessionBus()
 
20
try:
 
21
        dock_object = bus.get_object("org.cairodock.CairoDock",
 
22
                        "/org/cairodock/CairoDock")
 
23
except dbus.DBusException:
 
24
        print "Cairo-Dock not found on bus (did you activate its 'DBus' plug-in ?)"
 
25
        sys.exit(1)
 
26
dock = dbus.Interface(dock_object, "org.cairodock.CairoDock")
 
27
 
 
28
 
 
29
### let's register our applet ! ###
 
30
applet_name=os.path.basename(os.path.abspath("."))
 
31
applet_share_data_dir=os.path.abspath(".")
 
32
print ">>> registering our applet..."
 
33
dock.RegisterNewModule(applet_name,
 
34
        "This is a distant applet\nIt simulates a counter:\n Scroll up/down to increase/decrease the counter,\n Click/middle-click to increase/decrease the counter by 10\n Drop some text to set it as the label.",
 
35
        "Fabounet",
 
36
        3, # category "accessories"
 
37
        applet_share_data_dir)
 
38
print ">>> applet registered."
 
39
 
 
40
### get our applet ! (if it doesn't exist, it means the user does not want to use it, so we quit) ###
 
41
try:
 
42
        applet_object = bus.get_object("org.cairodock.CairoDock",
 
43
                        "/org/cairodock/CairoDock/"+applet_name)
 
44
except dbus.DBusException:
 
45
        print ">>> the '"+applet_name+"' module has not been started"
 
46
        sys.exit(2)
 
47
myIcon = dbus.Interface(applet_object, "org.cairodock.CairoDock.applet")
 
48
 
 
49
### we'll have a sub-docks, so we also get the sub-icons object ###
 
50
sub_icons_object = bus.get_object("org.cairodock.CairoDock",
 
51
                        "/org/cairodock/CairoDock/"+applet_name+"/sub_icons")
 
52
mySubIcons = dbus.Interface(sub_icons_object, "org.cairodock.CairoDock.subapplet")
 
53
 
 
54
 
 
55
### init ###
 
56
def init():
 
57
        # register to the notifications on our applet
 
58
        print ">>> INIT"
 
59
        myIcon.connect_to_signal("on_click", action_on_click)
 
60
        myIcon.connect_to_signal("on_middle_click", action_on_middle_click)
 
61
        myIcon.connect_to_signal("on_build_menu", action_on_build_menu)
 
62
        myIcon.connect_to_signal("on_menu_select", action_on_menu_select)
 
63
        myIcon.connect_to_signal("on_scroll", action_on_scroll)
 
64
        myIcon.connect_to_signal("on_drop_data", action_on_drop_data)
 
65
        myIcon.connect_to_signal("on_init_module", action_on_init)
 
66
        myIcon.connect_to_signal("on_stop_module", action_on_stop)
 
67
        myIcon.connect_to_signal("on_reload_module", action_on_reload)
 
68
        mySubIcons.connect_to_signal("on_click_sub_icon", on_click_sub_icon)
 
69
 
 
70
### stop ###
 
71
def stop():
 
72
        print ">>> STOP"
 
73
        #dock.UnregisterModule(applet_name)
 
74
        # clean up memory
 
75
        #del dock_object
 
76
        #del dock
 
77
        #del applet_object
 
78
        #del myIcon
 
79
 
 
80
 
 
81
################################################
 
82
### Add your own code to the functions below ###
 
83
################################################
 
84
### global vars ###
 
85
count=0
 
86
 
 
87
### callbacks ###
 
88
def action_on_click(iState):
 
89
        global count
 
90
        print ">>> clic !"
 
91
        count += 10
 
92
        myIcon.SetQuickInfo(format(count, "d"))
 
93
        myIcon.RenderValues([count/100.])
 
94
 
 
95
def action_on_middle_click():
 
96
        global count
 
97
        print ">>> middle clic !"
 
98
        count -= 10
 
99
        myIcon.SetQuickInfo(format(count, "d"))
 
100
        myIcon.RenderValues([count/100.])
 
101
 
 
102
def action_on_build_menu():
 
103
        print ">>> build menu !"
 
104
        myIcon.PopulateMenu(["set min value", "set medium value", "set max value"])
 
105
        
 
106
def action_on_menu_select(iNumEntry):
 
107
        global count
 
108
        print ">>> choice",iNumEntry,"has been selected !"
 
109
        if iNumEntry == 0:
 
110
                count = 0
 
111
        elif iNumEntry == 1:
 
112
                count = 50
 
113
        elif iNumEntry == 2:
 
114
                count = 100
 
115
        myIcon.SetQuickInfo(format(count, "d"))
 
116
        myIcon.RenderValues([count/100.])
 
117
 
 
118
def action_on_scroll(bScrollUp):
 
119
        print ">>> scroll !"
 
120
        global count
 
121
        if bScrollUp:
 
122
                count += 1
 
123
        else:
 
124
                count -= 1
 
125
        myIcon.SetQuickInfo(format(count, "d"))
 
126
        myIcon.RenderValues([count/100.])
 
127
 
 
128
def action_on_drop_data(cReceivedData):
 
129
        print ">>> received",cReceivedData
 
130
        myIcon.SetLabel(cReceivedData)
 
131
 
 
132
 
 
133
def on_click_sub_icon(iState, cIconID):
 
134
        global count
 
135
        print "clic on the sub-icon '"+cIconID+"' !"
 
136
 
 
137
 
 
138
def action_on_init():
 
139
        global count
 
140
        print ">>> our module is started"
 
141
        count=1
 
142
        myIcon.ShowDialog("I'm connected to Cairo-Dock !", 4)
 
143
        myIcon.SetQuickInfo(format(count, "d"))
 
144
        myIcon.AddDataRenderer("gauge", 1, "Turbo-night")
 
145
        myIcon.RenderValues([0.])
 
146
        mySubIcons.AddSubIcons(["icon 1", "firefox-3.0", "id1", "icon 2", "trash", "id2", "icon 3", "thunderbird", "id3", "icon 4", "nautilus", "id4"])
 
147
        mySubIcons.RemoveSubIcon("id2")
 
148
        mySubIcons.SetQuickInfo("1", "id1")
 
149
        mySubIcons.SetQuickInfo("2", "id2")
 
150
        mySubIcons.SetQuickInfo("3", "id3")
 
151
        mySubIcons.SetQuickInfo("4", "id4")
 
152
 
 
153
def action_on_stop():
 
154
        global count
 
155
        print ">>> our module is stopped"
 
156
        stop()
 
157
        gtk.main_quit()
 
158
 
 
159
def action_on_reload(bConfigHasChanged):
 
160
        print ">>> our module is reloaded"
 
161
        if bConfigHasChanged:
 
162
                print ">>>  and our config has changed"
 
163
                myIcon.AddDataRenderer("gauge", 1, "Turbo-night")
 
164
                myIcon.RenderValues([count/100.])
 
165
                mySubIcons.RemoveSubIcon("any")
 
166
                mySubIcons.AddSubIcons(["icon 1", "firefox-3.0", "echo pouet", "icon 2", "trash", "abc", "icon 3", "thunderbird", "def"])
 
167
 
 
168
 
 
169
### main ###
 
170
if __name__ == '__main__':
 
171
        init()
 
172
        gtk.main()
 
173
        stop()
 
174
        print ">>> bye"
 
175
        sys.exit(0)