Home Forums Wiki Doc Install Extras Screenshots Source Code Projects Blog Users Groups Register
Glx-Dock / Cairo-Dock List of forums Technical discussions | Discussions techniques FaceBook Applet
The latest stable release is the *3.4.0* : How to install it here.
Note: We just switched from BZR to Git on Github! (only to host the code and your future pull requests)
Technical discussions | Discussions techniques

Subjects Author Language Messages Last message
[Locked] FaceBook Applet
Page : 1 2 3
jesuisbenjamin English 58 jesuisbenjamin [Read]
27 April 2011 à 16:57

jesuisbenjamin, Sunday 17 April 2011 à 18:48


Subscription date : 28 October 2009
Messages : 415
I'm looking into the DBus applet model and i came across this:
loop gobject.MainLoop()
loop.run()
sys.exit(0)


I'm not really sure to understand what this does (since the applet seems to function anyway). Can you tell me more about it?

matttbe, Sunday 17 April 2011 à 23:25


Subscription date : 24 January 2009
Messages : 12573
It's just the "MainLoop"
The idea is to keep the process alive because your applet has to listen to some signals, it waits for something (a click, a notification, etc.) and it can't be stopped. And when you stop the mainloop, you stop the program.

jesuisbenjamin, Sunday 17 April 2011 à 23:44


Subscription date : 28 October 2009
Messages : 415
OK i'd really like to make some proper programming and learn so i'm still looking into threading. The problem, as far as i can tell occurs when the Applet() instance is created, then the threads seem to freeze. I made a small script to test this and you should try and see for yourself:

You can also download this Dummy applet, which contains this script: http://dl.free.fr/pxkNEZ5SF

#!/usr/bin/python



####################
### dependancies ###
####################

from CDApplet import CDApplet
import threading
import time


        
class MakeApplet(threading.Thread):
    
def run(self):
        print 
"...thread 04 making applet!"
        
facebook Applet()
        
facebook.run()
        
time.sleep(3)
        print 
"...facebook applet should be initiated!"
        
class MyThread(threading.Thread):
    
def run(self):
        print(
"%s started!" self.getName())
        
time.sleep(3)
        print(
"%s finished!" self.getName())

class 
Applet(CDApplet):
    
def __init__(self):
        
CDApplet.__init__(self)
    
##### private methods #####
        
    
def begin(self):
        print 
"...It's alive!"
        

if __name__ == '__main__':
    
thread01 MyThread(name"Thread 01")
    
thread01.start()
    
time.sleep(1)
    
thread02 MyThread(name"Thread 02")
    
thread02.start()
    
time.sleep(1)
    
thread03 MyThread(name"Thread 03")
    
thread03.start()
    
time.sleep(1)
    
thread04 MakeApplet()
    
thread04.start()



If i can't find anything i'll try to make a main pogram for applet() and another which checks FaceBook and reloads the applet via DBUS.

jesuisbenjamin, Monday 18 April 2011 à 01:43


Subscription date : 28 October 2009
Messages : 415
Haaa i found my solution:

see: http://www.jejik.com/articles/2007/01/python-gstreamer_threading_and_the_main_loop/
and: http://wiki.python.org/moin/DbusExamples

I started from scratch with the following script and will continue from there:

#!/usr/bin/python

# This is a part of the external applets for Cairo-Dock
# Copyright : (C) 2011 by Benjamin
# E-mail : jesuisbenjamin@...

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# http://www.gnu.org/licenses/licenses.html#GPL
#
# This program depends on fbcmd, a command line interface (CLI) for facebook.
# http://fbcmd.dtompkins.com/ 

####################
### dependancies ###
####################

from CDApplet import CDApplet
import os
import threading
import time
import dbus
from dbus
.mainloop.glib import DBusGMainLoop
import gobject
import sys
import thread

# begin DBUS loop
DBusGMainLoop(set_as_default=True)
# allow threads by gobject
gobject.threads_init()

class 
FaceBook():
    
def __init__(self):
        
# test log:
        
print "...FaceBook instance initiated."
        
# before it launches the present script, Cairo-Dock creates
        # a remote object on the bus. It represents the applet and its icon.
        
bus dbus.SessionBus()
        
# the name of the applet is the same as the folder in which it is contained
        
applet_name os.path.basename(os.path.abspath("."))
        
# path where our object is stored on the bus
        
applet_path "/org/cairodock/CairoDock/%s" applet_name
        
# try to get the applet on the bus
        
try:
            
applet_object bus.get_object("org.cairodock.CairoDock"applet_path)
        
except dbus.DBusException:
            print 
"!!! applet '"+applet_name+"' can't be found on the bus, exit."
            
sys.exit(2)
        
# the next object represents the applet and its icon.
        # the latter can be either in a dock or in a desklet.
        
icon dbus.Interface(applet_object"org.cairodock.CairoDock.applet")
        
# instantiating applet
        
fb Applet()
        
# connecting to callback
        #icon.connect_to_signal("on_click", self.action_on_click)
        # test log:
        
print "...created applet"
        
# start thread:
        
MyThread()
        
t.start()
        print 
"...while the thread is running"
        
time.sleep(3)
        
fb.speak()
        
# create loop
        
loop gobject.MainLoop()
        
loop.run()
        
sys.exit(0)
        
    
#def action_on_click(self, iState):
        #fb.speak()
####################
### Applet class ###
####################
class Applet(CDApplet):
    
def __init__(self):
        
# test log input
        
self.log "...the applet is running"
        
# call high-level init
        
CDApplet.__init__(self)
    
##### private methods #####
    
def speak(self):
        
# run test log 
        
print self.log
        
    def on_click
(selfiState):
        print 
"Hello!"

class MyThread(threading.Thread):
    
def run(self):
        print 
"...thread started"
        
time.sleep(10)
        print 
"...thread finished"
############
### main ###
############
if __name__ == '__main__':
    
FB FaceBook()




Eduardo Mucelli, Monday 18 April 2011 à 12:18


Subscription date : 05 August 2009
Messages : 285
jesuisbenjamin I would try, at first, a gobject idle functions, or the one related to timeout to keep polling on Facebook. Even though I really hate polling process, the one from gobject can be scheduled when there is not other process with higher priority. Threading could hang the IO easily if you are not really sure how CD deals with applets. One question, are you accessing Facebook through some third-party module like pyfacebook?

jesuisbenjamin, Monday 18 April 2011 à 12:32


Subscription date : 28 October 2009
Messages : 415
Eduardo Mucelli :
jesuisbenjamin I would try, at first, a gobject idle functions, or the one related to timeout to keep polling on Facebook. Even though I really hate polling process, the one from gobject can be scheduled when there is not other process with higher priority. Threading could hang the IO easily if you are not really sure how CD deals with applets. One question, are you accessing Facebook through some third-party module like pyfacebook?


Hello Eduardo,

Yes i am using fbcmd, third party software.

The threads_init() seems to work fine. I am just new to programming and i'm trying to stop the loop i created when closing the applet, but it keeps running even after Ctrl C. I thought giving the condition "while appplet" would do the trick, but i must misunderstand something cause it keeps spinning even the dock's gone.

I'm not quite sure to understand gobject.idle, does it perform an action when the applet's callbacks are not solicited? This means i could make it check FB when there is no activity rather than on a regular timing? If so that'd be more clever i think

Thanks

fabounet, Monday 18 April 2011 à 16:16


Subscription date : 30 November 2007
Messages : 17118
Threading could hang the IO easily if you are not really sure how CD deals with applets

it just sends some Dbus signals, which are treated inside the main loop.

so making an html request in a g_idle would freeze the main loop, and therefore prevents the applet from responding to the Dbus calls.

therefore using a thread for potentially heavy tasks is a good solution, although I'm not sure how Dbus behaves with threads; maybe you will have to make the Dbus calls (like setting the icon image) outside of the thread.

jesuisbenjamin, Monday 18 April 2011 à 17:31


Subscription date : 28 October 2009
Messages : 415
@ Eduardo:

I've looked into the glib library (i found the gobject library will become deprecated), which basically is the same as gobject's (http://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib--timeout-add).
The glib.timeout_add(interval, callback) looks appropriate for the applet i am making (it also has a priority attribute, so it can be set to idle as well). However i have not been able to run this function. Even in a small test code here below it didn't work out. Do you have any idea why no?


!# /usr/lib/python

import glib

class main():
    
def sayhi(self):
        print 
"hello"
        
    
def repeat(self):
        
glib.timeout_add(3000self.sayhi)

if 
__name__ == '__main__':
    
thing main()
    
thing.repeat()

jesuisbenjamin, Monday 18 April 2011 à 21:30


Subscription date : 28 October 2009
Messages : 415
OK i'm going forward:

Using glib.timeout_add() is initiated by gtk.main() and it keeps on repeating if its callback returns True. See following code:


!#/usr/lib/python
import glib
import gtk

class main():
    
def sayhi(self):
        print 
"hello"
        
return True
        
    def repeat
(self):
        print 
"here!"
        
glib.timeout_add(3000self.sayhi)
        

if 
__name__ == '__main__':
    
thing main()
    
thing.repeat()
    
gtk.main()




No i need to find some condition for the callback to return False, basically when the application is terminated. I'm not sure as to how to proceed though

jesuisbenjamin, Monday 18 April 2011 à 22:23


Subscription date : 28 October 2009
Messages : 415
OK, i'll keep updating for the sake of documentation:
i didn't realise that cairo-dock is a separate process from the applet
so now i tell the applet to check if cairo-dock exists, in which case it continues its repeated process, otherwise it stops.

import glib
import gtk
import os
import sys

class main():
    
def sayhi(self):
        
rollon self.findcairo()
        print 
"the affirmation that cair-dock exists is"rollon
        
return rollon
        
    def repeat
(self):
        print 
"here!"
        
glib.timeout_add(3000self.sayhi)
        
    
def findcairo(self):
        try:
            
net os.popen("ps -C cairo-dock | grep cairo-dock").readlines()[0].split()
            if 
net[-1] == "cairo-dock":
                return 
True
            
else:
                return 
False
        except
:
            return 
False
            
if __name__ == '__main__':
    
thing main()
    
thing.repeat()
    
gtk.main()

jesuisbenjamin, Tuesday 19 April 2011 à 01:49


Subscription date : 28 October 2009
Messages : 415
Well, my Facebook Applet is almost finished

I need to find out how to properly end the self.icon.DemandsAttention(True,"bounce"). I'd like it to bounce 4 times and stop. Using the False argument makes the bouncing too short to be noticable and even as i use time.sleep() it stops the bouncing while it's up and hangs there above the dock.
Any tip?

Also i want to play an alarm: os.popen("mplayer ./alarm.wav") should have done the trick i'd hoped. But it doesn't work
Is the path the problem?

Finally i need to find out how to change the configuration menu, but that shouldn't be hard i believe

Guest, Tuesday 19 April 2011 à 03:27

I'll be looking forward to your release Thanks

matttbe, Tuesday 19 April 2011 à 03:31


Subscription date : 24 January 2009
Messages : 12573
I need to find out how to properly end the self.icon.DemandsAttention(True,"bounce"). I'd like it to bounce 4 times and stop. Using the False argument makes the bouncing too short to be noticable and even as i use time.sleep() it stops the bouncing while it's up and hangs there above the dock.
Any tip?
You can also use "Animate"

Also i want to play an alarm: os.popen("mplayer ./alarm.wav") should have done the trick i'd hoped. But it doesn't work
Is the path the problem?
I guess it's just because you're not in the good directory. But I think it's not a good idea to use mplayer (it's not installed by default in a lot of distribution, aplay is maybe better but still not the best solution). There are a few librairies to read a .wav file (example)

Finally i need to find out how to change the configuration menu, but that shouldn't be hard i believe
Do you want to use a new submenu? You can have a look to other applets

jesuisbenjamin, Tuesday 19 April 2011 à 09:35


Subscription date : 28 October 2009
Messages : 415
matttbe :
You can also use "Animate"

I tried, but animate does not show if the dock's hidden -- and it's precisely when i want it to bounce.

mattbe :
I think it's not a good idea to use mplayer (it's not installed by default in a lot of distribution, aplay is maybe better but still not the best solution). There are a few librairies to read a .wav file (example)

Thanks

mattbe :
Do you want to use a new submenu? You can have a look to other applets ;)

I mean the configuration tab in the applet configuration's window.

mysterious guest :
I'll be looking forward to your release Thanks

A'right then!

matttbe, Tuesday 19 April 2011 à 09:46


Subscription date : 24 January 2009
Messages : 12573
I tried, but animate does not show if the dock's hidden -- and it's precisely when i want it to bounce.
you can demand the attention and also use animate... or demand the attention two time, etc.

I mean the configuration tab in the applet configuration's window.
simply have a look in all .conf

jesuisbenjamin, Tuesday 19 April 2011 à 11:26


Subscription date : 28 October 2009
Messages : 415
matttbe :
you can demand the attention and also use animate... or demand the attention two time, etc.


I'm afraid i don't follow. I can either:

  • self.icon.DemandsAttention(True, 'bounce')
or
  • self.icon.Animate('bounce', 4)


In the first case the animation is also visible when the doc is hidden/not in focus but it keeps on running till the icon is clicked.
In the second case i can control how long i want the animation to run, but it is not visible when the cairo is hidden/not in focus.

Did you mean there are alternatives to this?

matttbe, Tuesday 19 April 2011 à 13:08


Subscription date : 24 January 2009
Messages : 12573
Yes, you can use self.icon.DemandsAttention(True, 'bounce') and self.icon.Animate('bounce', 4). Or you can use DemandsAttention two time if you want, no?

jesuisbenjamin, Tuesday 19 April 2011 à 13:18


Subscription date : 28 October 2009
Messages : 415
Not really.
self.icon.DemandsAttention(True, 'bounce') will start the bouncing till self.icon.DemandsAttention(False, '') is thrown.
I tried:
self.icon.DemandsAttention(True, 'bounce')
time.sleep(3)
self.icon.DemandsAttention(False, '')
But then it stays up and hangs above the dock as i mentioned before.

I tried (contrary to what the Wiki explains) to repeat the effect in the closing line:
icon.DemandsAttention(False, 'bounce') but it doesn't help (although it seems to occur less often than without).

I also have another issue with the icon.PopupDialog(). Throwing the Popup is easy, but how do i catch the answer?
The documentation says:
Wiki :
PopupDialog
Pops up a dialog bubble on our icon (since 2.2.0). The dialog can contain a message, an icon, some buttons, and a widget the user can act on.
To get the answer, you need to connect to the "on_answer_dialog" signal.

But how do i "connect" to on_answer_dialog?
Shouldn't there be an ID value to connect sender and listener? I will use several such dialogues, and they need their respective response.

Thanks
B.

matttbe, Tuesday 19 April 2011 à 13:33


Subscription date : 24 January 2009
Messages : 12573
I also have another issue with the icon.PopupDialog(). Throwing the Popup is easy, but how do i catch the answer?
Simply have a look to the demo applet (and also Translator, Lifera, Pidgin and Quote)

fabounet, Tuesday 19 April 2011 à 16:07


Subscription date : 30 November 2007
Messages : 17118
I don't think you need to stop the animation, since it is made to catch the user attention, and he may not be watching his screen at the moment.
adding an option to disable the animation is a good solution IMHO

But then it stays up and hangs above the dock as i mentioned before.

it should stop animating and becomt invisible if the dock is hidden. what exactly appears ?

But how do i "connect" to on_answer_dialog?

you don't have to, the Interface does it for you. you just have to redefine the function on_answer_dialog in your class

Technical discussions | Discussions techniques

Subjects Author Language Messages Last message
[Locked] FaceBook Applet
Page : 1 2 3
jesuisbenjamin English 58 jesuisbenjamin [Read]
27 April 2011 à 16:57


Glx-Dock / Cairo-Dock List of forums Technical discussions | Discussions techniques FaceBook Applet Top

Online users :

Powered by ElementSpeak © 2007 Adrien Pilleboue, 2009-2013 Matthieu Baerts.
Dock based on CSS Dock Menu (Ndesign) with jQuery. Icons by zgegball
Cairo-Dock is a free software under GNU-GPL3 licence. First stable version created by Fabounet.
Many thanks to TuxFamily for the web Hosting and Mav for the domain name.