- first public release, initial commit
This commit is contained in:
goffi 2009-08-29 13:34:59 +02:00
commit d005c1b9a8
6 changed files with 4704 additions and 0 deletions

View file

View file

@ -0,0 +1,159 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
helper class for making a SAT frontend
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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 logging import debug, info, error
from tools.jid import JID
from sat_bridge_frontend.DBus import DBusBridgeFrontend
from quick_frontend.quick_contact_management import QuickContactManagement
class QuickApp():
"""This class contain the main methods needed for the frontend"""
def __init__(self):
self.rosterList = {}
self.CM = QuickContactManagement() #a short name if more handy
## bridge ##
self.bridge=DBusBridgeFrontend()
self.bridge.register("newContact", self.newContact)
self.bridge.register("newMessage", self.newMessage)
self.bridge.register("presenceUpdate", self.presenceUpdate)
self.bridge.register("paramUpdate", self.paramUpdate)
self.bridge.register("contactDeleted", self.contactDeleted)
self.bridge.register("askConfirmation", self.askConfirmation, "request")
###now we get the essential params###
self.whoami=JID(self.bridge.getParam("JabberID","Connection")[0])
self.watched=self.bridge.getParam("Watched", "Misc")[0].split() #TODO: put this in a plugin
## misc ##
self.onlineContact = set() #FIXME: temporary
if self.bridge.isConnected():
self.setStatusOnline(True)
### now we fill the contact list ###
for contact in self.bridge.getContacts():
self.newContact(contact[0], contact[1], contact[2])
for status in self.bridge.getPresenceStatus():
self.presenceUpdate(status[0], status[1], status[2], status[3], status[4])
def newContact(self, JabberId, attributes, groups):
jid=JID(JabberId)
self.rosterList[jid.short]=(dict(attributes), list(groups))
def newMessage(self, from_jid, msg, type, to_jid):
sender=JID(from_jid)
addr=JID(to_jid)
win = addr if sender.short == self.whoami.short else sender
self.chat_wins[win.short].printMessage(sender, msg)
def setStatusOnline(self, online=True):
pass
def presenceUpdate(self, jabber_id, type, show, status, priority):
debug ("presence update for %s (type=%s, show=%s, status=%s)", jabber_id, type, show, status);
jid=JID(jabber_id)
debug ("jid.short=%s whoami.short=%s", jid.short, self.whoami.short)
### subscription management ###
if type=="subscribed":
# this is a subscription confirmation, we just have to inform user
self.showDialog("The contact %s has accepted your subscription" % jid.short, 'Subscription confirmation')
return
elif type=="unsubscribed":
# this is a subscription refusal, we just have to inform user
self.showDialog("The contact %s has refused your subscription" % jid.short, 'Subscription refusal', 'error')
return
elif type=="subscribe":
# this is a subscrition request, we have to ask for user confirmation
answer = self.showDialog("The contact %s wants to subscribe to your presence.\nDo you accept ?" % jid.short, 'Subscription confirmation', 'question')
if answer:
self.bridge.setPresence(type="subscribed", to=jid.short)
else:
self.bridge.setPresence(type="unsubscribed", to=jid.short)
return
### subscription management end ###
if jid.short==self.whoami.short:
if not type:
self.setStatusOnline(True)
elif type=="unavailable":
self.setStatusOnline(False)
return
if not type:
name=""
group=""
if self.rosterList.has_key(jid.short):
if self.rosterList[jid.short][0].has_key("name"):
name=self.rosterList[jid.short][0]["name"]
if self.rosterList[jid.short][0].has_key("show"):
name=self.rosterList[jid.short][0]["show"]
if self.rosterList[jid.short][0].has_key("status"):
name=self.rosterList[jid.short][0]["status"]
if len(self.rosterList[jid.short][1]):
group=self.rosterList[jid.short][1][0]
#FIXME: must be moved in a plugin
if jid.short in self.watched and not jid.short in self.onlineContact:
self.showAlert("Watched jid [%s] is connected !" % jid.short)
self.onlineContact.add(jid) #FIXME onlineContact is useless with CM, must be removed
self.CM.add(jid)
self.contactList.replace(jid, show=show, status=status, name=name, group=group)
if type=="unavailable" and jid.short in self.onlineContact:
self.onlineContact.remove(jid.short)
self.CM.remove(jid)
self.contactList.remove(jid)
def showDialog(self, message, title, type="info"):
raise NotImplementedError
def showAlert(self, message):
pass #FIXME
def paramUpdate(self, name, value, namespace):
debug("param update: [%s] %s = %s", namespace, name, value)
if (namespace,name) == ("Connection", "JabberID"):
debug ("Changing ID to %s", value)
self.whoami=JID(value)
elif (namespace,name) == ("Misc", "Watched"):
self.watched=value.split()
def contactDeleted(self, jid):
target = JID(jid)
try:
self.onlineContact.remove(target.short)
except KeyError:
pass
self.contactList.remove(self.CM.get_full(jid))
self.CM.remove(target)
def askConfirmation(self, type, id, data):
raise NotImplementedError

View file

@ -0,0 +1,47 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
helper class for making a SAT frontend
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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 logging import debug, info, error
from tools.jid import JID
class QuickChat():
def __init__(self, to_jid, host):
self.to_jid = to_jid
self.host = host
def historyPrint(self, size=20, keep_last=False):
"""Print the initial history"""
debug ("now we print history")
history=self.host.bridge.getHistory(self.host.whoami.short, self.to_jid, 20)
stamps=history.keys()
stamps.sort()
for stamp in stamps:
self.printMessage(JID(history[stamp][0]), history[stamp][1], stamp)
if keep_last: ##FIXME hack for sortilege
self.last_history = stamps[-1] if stamps else None
def printMessage(self, from_jid, msg, timestamp):
"""Print message in chat window. Must be implemented by child class"""
raise NotImplementedError

View file

@ -0,0 +1,41 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
helper class for making a SAT frontend
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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 tools.jid import JID
class QuickChatList(dict):
"""This class is used to manage the list of chat windows.
It act as a dict, but create a chat window when the name is found for the first time."""
def __init__(self, host):
dict.__init__(self)
self.host = host
def __getitem__(self,name_param):
name=JID(name_param).short
if not self.has_key(name):
#we have to create the chat win
self[name] = self.createChat(name)
return dict.__getitem__(self,name)
def createChat(self, name):
raise NotImplementedError

View file

@ -0,0 +1,42 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
helper class for making a SAT frontend
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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 logging import debug, info, error
from tools.jid import JID
class QuickContactList():
def __init__(self):
debug("Contact List init")
self.jid_ids={}
def replace(self, jid, name="", show="", status="", group=""):
"""add a contact to the list"""
raise NotImplementedError
def remove(self, jid):
"""remove a contact from the list"""
raise NotImplementedError
def add(self, jid, name="", show="", status="", group=""):
"""add a contact to the list"""
raise NotImplementedError

View file

@ -0,0 +1,47 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
helper class for making a SAT frontend
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
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 logging import debug, info, error
from tools.jid import JID
import pdb
class QuickContactManagement():
"""This helper class manage the contacts and ease the use of nicknames and shortcuts"""
def __init__(self):
self.__contactlist = {}
def add(self, jid_param):
jid=JID(jid_param)
self.__contactlist[jid.short] = {'name':jid, 'last_resource':jid.resource}
def remove(self, jid_param):
jid=JID(jid_param)
try:
del self.__contactlist[jid.short]
except KeyError:
pass
def get_full(self, jid_param):
jid=JID(jid_param)
return jid.short+'/'+self.__contactlist[jid.short]['last_resource']