# -*- coding: utf-8 -*-
# Copyright(C) 2010 Romain Bignon
#
# 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, version 3 of the License.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import time
import logging
from PyQt4.QtGui import QWidget, QListWidgetItem, QImage, QIcon, QPixmap, \
QFrame, QMessageBox, QTabWidget, QVBoxLayout, \
QFormLayout, QLabel
from PyQt4.QtCore import SIGNAL, Qt
from weboob.tools.application.qt import QtDo, HTMLDelegate
from weboob.capabilities.contact import ICapContact, Contact
from weboob.capabilities.chat import ICapChat
from weboob.capabilities.messages import ICapMessages
from weboob.capabilities.base import NotLoaded
from .ui.contacts_ui import Ui_Contacts
from .ui.contact_thread_ui import Ui_ContactThread
from .ui.thread_message_ui import Ui_ThreadMessage
from .ui.profile_ui import Ui_Profile
class ThreadMessage(QFrame):
"""
This class represents a message in the thread tab.
"""
def __init__(self, message, parent=None):
QFrame.__init__(self, parent)
self.ui = Ui_ThreadMessage()
self.ui.setupUi(self)
self.date = message.date
self.ui.nameLabel.setText(message.sender)
header = time.strftime('%Y-%m-%d %H:%M:%S', message.date.timetuple())
if message.flags & message.IS_UNREAD:
header += u' — Unread'
else:
header += u' — Read'
self.ui.headerLabel.setText(header)
if message.flags & message.IS_HTML:
content = message.content
else:
content = message.content.replace('&', '&').replace('<', '<').replace('>', '>').replace('\n', '
')
self.ui.contentLabel.setText(content)
class ContactThread(QWidget):
"""
The thread of the selected contact.
"""
def __init__(self, weboob, contact, parent=None):
QWidget.__init__(self, parent)
self.ui = Ui_ContactThread()
self.ui.setupUi(self)
self.weboob = weboob
self.contact = contact
self.messages = []
self.connect(self.ui.sendButton, SIGNAL('clicked()'), self.postReply)
self.refreshMessages()
def refreshMessages(self):
if self.ui.scrollAreaContent.layout().count() > 0:
command = 'iter_new_messages'
else:
command = 'iter_messages'
self.process_msg = QtDo(self.weboob, self.gotMessage)
self.process_msg.do(command, thread=self.contact.id, backends=self.contact.backend)
def gotMessage(self, backend, message):
if not message:
#v = self.ui.scrollArea.verticalScrollBar()
#print v.minimum(), v.value(), v.maximum(), v.sliderPosition()
#self.ui.scrollArea.verticalScrollBar().setValue(self.ui.scrollArea.verticalScrollBar().maximum())
self.process_msg = None
return
widget = ThreadMessage(message)
for i, m in enumerate(self.messages):
if widget.date > m.date:
self.ui.scrollAreaContent.layout().insertWidget(i, widget)
self.messages.insert(i, widget)
return
self.ui.scrollAreaContent.layout().addWidget(widget)
self.messages.append(widget)
def postReply(self):
text = unicode(self.ui.textEdit.toPlainText())
self.ui.textEdit.setEnabled(False)
self.ui.sendButton.setEnabled(False)
self.process_reply = QtDo(self.weboob, self._postReply_cb, self._postReply_eb)
self.process_reply.do('post_reply', self.contact.id, 0, '', text, backends=self.contact.backend)
def _postReply_cb(self, backend, ignored):
self.ui.textEdit.clear()
self.ui.textEdit.setEnabled(True)
self.ui.sendButton.setEnabled(True)
self.refreshMessages()
self.process_reply = None
def _postReply_eb(self, backend, error, backtrace):
content = unicode(self.tr('Unable to send message:\n%s\n')) % error
if logging.root.level == logging.DEBUG:
content += '\n%s\n' % backtrace
QMessageBox.critical(self, self.tr('Error while posting reply'),
content, QMessageBox.Ok)
self.process_reply = None
class ContactProfile(QWidget):
def __init__(self, weboob, contact, parent=None):
QWidget.__init__(self, parent)
self.ui = Ui_Profile()
self.ui.setupUi(self)
self.weboob = weboob
self.contact = contact
if self.gotProfile(self.weboob.get_backend(contact.backend), contact):
self.process_contact = QtDo(self.weboob, self.gotProfile)
self.process_contact.do('fillobj', self.contact, ['photos', 'profile'], backends=self.contact.backend)
def gotProfile(self, backend, contact):
if not backend:
return
first = True
for photo in contact.photos.itervalues():
photo = contact.photos.values()[0]
if first and photo.data:
img = QImage.fromData(photo.data)
self.ui.photoLabel.setPixmap(QPixmap.fromImage(img))
first = False
else:
# TODO display thumbnails
pass
self.ui.nicknameLabel.setText('
%s
' % ('Receiving...' if contact.summary is NotLoaded else contact.summary.replace('\n', '