ql_facebook_status

A Quod Libet plugin to change your Facebook status message

Get the source file here: ql_facebook_status.py
You will need facebook_status.py as well.

This is a hack of the gajim plugin, I still use some of the code for gajim, as it's easy to control when you want status messages to change by setting the gajim status.

# This plugin is based on the gajim status plugin and can change your
# Facebook status message
# Copyright (C) 2007  Francois du Toit <bluegraydragon@gmail.com>

# 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 3 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.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from string import join
import gtk
import dbus

from plugins.events import EventPlugin
from parse import Pattern
from qltk import Frame
import config
import facebook_status, threading

class FacebookStatusMessage(EventPlugin):
    PLUGIN_ID = 'Facebook status message'
    PLUGIN_NAME = _('Facebook Status Message')
    PLUGIN_DESC = _("Change Facebook status message according to what "
                    "you are currently listening to.")
    PLUGIN_VERSION = '0.1.0'

    c_accounts = __name__+'_accounts'
    c_paused = __name__+'_paused'
    c_statuses = __name__+'_statuses'
    c_pattern = __name__+'_pattern'
    c_username = __name__+'_username'
    c_password = __name__+'_password'

    def __init__(self):
        try:
            self.accounts = config.get('plugins', self.c_accounts).split()
        except:
            self.accounts = []
            config.set('plugins', self.c_accounts, '')

        try:
            self.paused = config.getboolean('plugins', self.c_paused)
        except:
            self.paused = True
            config.set('plugins', self.c_paused, 'True')

        try:
            self.statuses = config.get('plugins', self.c_statuses).split()
        except:
            self.statuses = ['online', 'chat']
            config.set('plugins', self.c_statuses, join(self.statuses))

        try:
            self.pattern = config.get('plugins', self.c_pattern)
        except:
            self.pattern = u'listening to <artist> \u266b <title> \u266b'
            config.set('plugins', self.c_pattern, self.pattern)

        try:
            self.username = config.get('plugins', self.c_username)
        except:
            self.username = ''
            config.set('plugins', self.c_username, self.username)

        try:
            self.password = config.get('plugins', self.c_password)
        except:
            self.password = ''
            config.set('plugins', self.c_password, self.password)

        gtk.quit_add(0, self.quit)

        try:
            bus = dbus.SessionBus()
            obj = bus.get_object(
                'org.gajim.dbus', '/org/gajim/dbus/RemoteObject')
            self.interface = dbus.Interface(
                obj, 'org.gajim.dbus.RemoteInterface')
        except dbus.DBusException:
            self.interface = None

        self.fb = facebook_status.fbStatus()

        self.current = ''

    def quit(self):
        if self.interface:
            try: self.change_status(self.accounts, '')
            except dbus.DBusException: pass

    def change_status(self, enabled_accounts, status_message):
        bg = threading.Thread(group=None, target=self.submit_status, args=(enabled_accounts, status_message))
        bg.setDaemon(True)
        bg.start()

    def submit_status(self, enabled_accounts, status_message):
        doit = 0
        for account in self.interface.list_accounts():
            status = self.interface.get_status(account)
            if enabled_accounts != [] and account not in enabled_accounts:
                continue
            if status in self.statuses:
                doit = 1
        if doit:
            self.fb.change_status(self.username, self.password, status_message)

    def plugin_on_song_started(self, song):
        if song:
            self.current = Pattern(self.pattern) % song
        else:
            self.current = ''
        self.change_status(self.accounts, self.current)

    def plugin_on_paused(self):
        if self.paused and self.current != '':
            self.change_status(self.accounts, self.current + " [paused]")

    def plugin_on_unpaused(self):
        self.change_status(self.accounts, self.current)

    def accounts_changed(self, entry):
        self.accounts = entry.get_text().split()
        config.set('plugins', self.c_accounts, entry.get_text())

    def pattern_changed(self, entry):
        self.pattern = entry.get_text()
        config.set('plugins', self.c_pattern, self.pattern)

    def username_changed(self, entry):
        self.username = entry.get_text()
        config.set('plugins', self.c_username, self.username)

    def password_changed(self, entry):
        self.password = entry.get_text()
        config.set('plugins', self.c_password, self.password)

    def paused_changed(self, c):
        config.set('plugins', self.c_paused, str(c.get_active()))

    def statuses_changed(self, b):
        if b.get_active() and b.get_name() not in self.statuses:
            self.statuses.append(b.get_name())
        elif b.get_active() == False and b.get_name() in self.statuses:
            self.statuses.remove(b.get_name())
        config.set('plugins', self.c_statuses, join(self.statuses))

    def PluginPreferences(self, parent):
        vb = gtk.VBox(spacing = 1)
        tooltips = gtk.Tooltips().set_tip

        pattern_box = gtk.HBox(spacing = 3)
        pattern_box.set_border_width(3)
        pattern = gtk.Entry()
        pattern.set_text(self.pattern)
        pattern.connect('changed', self.pattern_changed)
        pattern_box.pack_start(gtk.Label("Pattern:"), expand = False)
        pattern_box.pack_start(pattern)

        accounts_box = gtk.HBox(spacing = 3)
        accounts_box.set_border_width(3)
        accounts = gtk.Entry()
        accounts.set_text(join(self.accounts))
        accounts.connect('changed', self.accounts_changed)
        tooltips(accounts, "List accounts, separated by spaces, for "
                             "changing status message. If none are specified, "
                             "status message of all accounts will be changed.")
        accounts_box.pack_start(gtk.Label("Accounts:"), expand = False)
        accounts_box.pack_start(accounts)

        c = gtk.CheckButton(label="Add '[paused]'")
        c.set_active(self.paused)
        c.connect('toggled', self.paused_changed)
        tooltips(c, "If checked, '[paused]' will be added to "
                    "status message on pause.")

        username_box = gtk.HBox(spacing = 1)
        username_box.set_border_width(3)
        username = gtk.Entry()
        username.set_text(self.username)
        username.connect('changed', self.username_changed)
        username_box.pack_start(gtk.Label("E-mail:"), expand = False)
        username_box.pack_start(username)

        password_box = gtk.HBox(spacing = 1)
        password_box.set_border_width(3)
        password = gtk.Entry()
        password.set_visibility(False)
        password.set_invisible_char('*')
        password.set_text(self.password)
        password.connect('changed', self.password_changed)
        password_box.pack_start(gtk.Label("Password:"), expand = False)
        password_box.pack_start(password)

        table = gtk.Table()
        self.list = []
        i = 0
        j = 0
        for status in ['online', 'offline', 'chat', 'away', 'xa', 'invisible']:
            button = gtk.CheckButton(label=status)
            button.set_name(status)
            if status in self.statuses:
                button.set_active(True)
            button.connect('toggled', self.statuses_changed)
            self.list.append(button)
            table.attach(button, i, i+1, j, j+1)
            if i == 2:
                i = 0
                j += 1
            else:
                i += 1

        vb.pack_start(pattern_box)
        vb.pack_start(accounts_box)
        vb.pack_start(username_box)
        vb.pack_start(password_box)
        vb.pack_start(c)
        vb.pack_start(Frame(label="Statuses for which status message\n"
                                  "will be changed"))
        vb.pack_start(table)

        return vb