libqutim 0.2.80.1

chatsession.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** qutIM - instant messenger
00004 **
00005 ** Copyright © 2011 Ruslan Nigmatullin <euroelessar@yandex.ru>
00006 **
00007 *****************************************************************************
00008 **
00009 ** $QUTIM_BEGIN_LICENSE$
00010 ** This program is free software: you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation, either version 3 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00018 ** See the GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program.  If not, see http://www.gnu.org/licenses/.
00022 ** $QUTIM_END_LICENSE$
00023 **
00024 ****************************************************************************/
00025 
00026 #ifndef LIBQUTIM_MESSAGESESSION_H
00027 #define LIBQUTIM_MESSAGESESSION_H
00028 
00029 #include "message.h"
00030 #include "contact.h"
00031 #include <QDateTime>
00032 
00033 class QTextDocument;
00034 namespace qutim_sdk_0_3
00035 {
00036 
00037 class Account;
00038 class ChatLayer;
00039 class ChatSessionPrivate;
00040 class ChatLayerPrivate;
00041 
00042 class LIBQUTIM_EXPORT ChatSession : public QObject
00043 {
00044     Q_OBJECT
00045     Q_DECLARE_PRIVATE(ChatSession)
00046     Q_PROPERTY(bool active READ isActive WRITE setActive NOTIFY activated)
00047     Q_PROPERTY(qutim_sdk_0_3::MessageList unread READ unread NOTIFY unreadChanged)
00048     Q_PROPERTY(QDateTime dateOpened READ dateOpened WRITE setDateOpened NOTIFY dateOpenedChanged)
00049 public:
00050     virtual ~ChatSession();
00051     
00052     virtual ChatUnit *getUnit() const = 0;
00053     Q_INVOKABLE inline qutim_sdk_0_3::ChatUnit *unit() const { return getUnit(); }
00054     Q_INVOKABLE virtual void setChatUnit(qutim_sdk_0_3::ChatUnit* unit) = 0;
00055     Q_INVOKABLE qint64 append(qutim_sdk_0_3::Message &message);
00056     virtual QTextDocument *getInputField() = 0;
00057     virtual void markRead(quint64 id) = 0;
00058     virtual MessageList unread() const = 0;
00059     bool isActive();
00060     QDateTime dateOpened() const;
00061     void setDateOpened(const QDateTime &date);
00062 public slots:
00063     virtual void addContact(qutim_sdk_0_3::Buddy *c) = 0;
00064     virtual void removeContact(qutim_sdk_0_3::Buddy *c) = 0;
00065     qint64 appendMessage(qutim_sdk_0_3::Message &message);
00066     void setActive(bool active);
00067     inline void activate() { setActive(true); }
00068     inline qint64 appendMessage(const QString &text)
00069     { Message msg(text); return appendMessage(msg); }
00070 protected:
00071     virtual void doSetActive(bool active) = 0;
00072     virtual qint64 doAppendMessage(qutim_sdk_0_3::Message &message) = 0;
00073 signals:
00074     void dateOpenedChanged(const QDateTime &date);
00075     void messageReceived(qutim_sdk_0_3::Message *message);
00076     void messageSent(qutim_sdk_0_3::Message *message);
00077     void contactAdded(qutim_sdk_0_3::Buddy *c);
00078     void contactRemoved(qutim_sdk_0_3::Buddy *c);
00079     void activated(bool active);
00080     void unreadChanged(const qutim_sdk_0_3::MessageList &);
00081 protected:
00082     ChatSession(ChatLayer *chat);
00083     virtual void virtual_hook(int id, void *data);
00084     friend class MessageHandlerHook;
00085 private:
00086     QScopedPointer<ChatSessionPrivate> d_ptr;
00087 };
00088 
00089 class LIBQUTIM_EXPORT ChatLayer : public QObject
00090 {
00091     Q_OBJECT
00092     Q_DECLARE_PRIVATE(ChatLayer)
00093     Q_CLASSINFO("Service", "ChatLayer")
00094     Q_PROPERTY(bool alertStatus READ isAlerted WRITE alert NOTIFY alertStatusChanged)
00095 public:
00096     static ChatLayer *instance();
00097 
00098     ChatSession *getSession(Account *acc, QObject *obj, bool create = true);
00099     ChatSession *getSession(QObject *obj, bool create = true);
00100     ChatSession *getSession(Account *acc, const QString &id, bool create = true);
00101     virtual ChatSession *getSession(ChatUnit *unit, bool create = true) = 0;
00102     Q_INVOKABLE inline qutim_sdk_0_3::ChatSession *session(QObject *obj, bool create = true);
00103     static ChatSession *get(ChatUnit *unit, bool create = true);
00104     Q_INVOKABLE virtual QList<qutim_sdk_0_3::ChatSession*> sessions() = 0;
00105     bool isAlerted() const;
00106     void alert(bool on);
00107     void alert(int msecs);
00108     bool event(QEvent *);
00109     
00110 signals:
00111     void sessionCreated(qutim_sdk_0_3::ChatSession *session);
00112     void alertStatusChanged(bool);
00113 protected:
00114     ChatUnit *getUnitForSession(ChatUnit *unit) const;
00115     ChatLayer();
00116     virtual ~ChatLayer();
00117     virtual void virtual_hook(int id, void *data);
00118     QScopedPointer<ChatLayerPrivate> d_ptr;
00119 };
00120 
00121 ChatSession *ChatLayer::session(QObject *obj, bool create)
00122 {
00123     return getSession(obj, create);
00124 }
00125 
00126 }
00127 
00128 Q_DECLARE_METATYPE(qutim_sdk_0_3::ChatSession*)
00129 Q_DECLARE_METATYPE(QList<qutim_sdk_0_3::ChatSession*>)
00130 
00131 #endif // LIBQUTIM_MESSAGESESSION_H
00132