- Fixed the inconsitency bug lately introduced. Adding networks is now
[quassel.git] / src / core / storage.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef _STORAGE_H_
22 #define _STORAGE_H_
23
24 #include <QtCore>
25
26 #include "types.h"
27 #include "message.h"
28 struct NetworkInfo;
29
30 class Storage : public QObject {
31   Q_OBJECT
32
33   public:
34     Storage(QObject *parent = 0);
35     virtual ~Storage() {};
36
37   public slots:
38     /* General */
39
40     //! Check if the storage type is available.
41     /** A storage subclass should return true if it can be successfully used, i.e. if all
42      *  prerequisites are in place (e.g. we have an appropriate DB driver etc.).
43      * \return True if and only if the storage class can be successfully used.
44      */
45     static bool isAvailable() { return false; }
46
47     //! Returns the display name of the storage backend
48     /** \return A string that can be used by the GUI to describe the storage backend */
49     static QString displayName() { return ""; }
50
51     //! Setup the storage provider.
52     /** This prepares the storage provider (e.g. create tables, etc.) for use within Quassel.
53      *  \param settings   Hostname, port, username, password, ...
54      *  \return True if and only if the storage provider was initialized successfully.
55      */
56     virtual bool setup(const QVariantMap &settings = QVariantMap()) { Q_UNUSED(settings); return false; }
57     
58     //! Initialize the storage provider
59     /** \param settings   Hostname, port, username, password, ...  
60      *  \return True if and only if the storage provider was initialized successfully.
61      */
62     virtual bool init(const QVariantMap &settings = QVariantMap()) = 0;
63     
64     // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
65
66     /* User handling */
67
68     //! Add a new core user to the storage.
69     /** \param user     The username of the new user
70      *  \param password The cleartext password for the new user
71      *  \return The new user's UserId
72      */
73     virtual UserId addUser(const QString &user, const QString &password) = 0;
74
75     //! Update a core user's password.
76     /** \param user     The user's id
77      *  \param password The user's new password
78      */
79     virtual void updateUser(UserId user, const QString &password) = 0;
80
81     //! Rename a user
82     /** \param user     The user's id
83      *  \param newName  The user's new name
84      */
85     virtual void renameUser(UserId user, const QString &newName) = 0;
86
87     //! Validate a username with a given password.
88     /** \param user     The username to validate
89      *  \param password The user's alleged password
90      *  \return A valid UserId if the password matches the username; 0 else
91      */
92     virtual UserId validateUser(const QString &user, const QString &password) = 0;
93
94     //! Remove a core user from storage.
95     /** \param user     The userid to delete
96      */
97     virtual void delUser(UserId user) = 0;
98
99     /* Network handling */
100
101     //! Create a new unique Network in the storage backend
102     /** \param user        The core user who owns this network
103      *  \param networkInfo The networkInfo holding the network definition
104      *  \return the NetworkId of the newly created Network. Possibly invalid.
105      */
106     virtual NetworkId createNetworkId(UserId user, const NetworkInfo &info) = 0;
107
108     //! Get the unique NetworkId of the network for a user.
109     /** \param user    The core user who owns this network
110      *  \param network The network name
111      *  \return The NetworkId corresponding to the given network, or 0 if not found
112      */
113     virtual NetworkId getNetworkId(UserId user, const QString &network) = 0;
114
115     /* Buffer handling */
116
117     //! Get the unique BufferInfo for the given combination of network and buffername for a user.
118     /** \param user    The core user who owns this buffername
119      *  \param network The network name
120      *  \param buffer  The buffer name (if empty, the net's status buffer is returned)
121      *  \return The BufferInfo corresponding to the given network and buffer name, or 0 if not found
122      */
123     virtual BufferInfo getBufferInfo(UserId user, const NetworkId &networkId, const QString &buffer = "") = 0;
124
125     //! Request a list of all buffers known to a user since a certain point in time.
126     /** This method is used to get a list of all buffers we have stored a backlog from.
127      *  Optionally, a QDateTime can be given, so that only buffers are listed that where active
128      *  since that point in time.
129      *  \param user  The user whose buffers we request
130      *  \param since If this is defined, older buffers will be ignored
131      *  \return A list of the BufferInfos for all buffers as requested
132      */
133     virtual QList<BufferInfo> requestBuffers(UserId user, QDateTime since = QDateTime()) = 0;
134
135     /* Message handling */
136
137     //! Store a Message in the backlog.
138     /** \param msg  The message object to be stored
139      *  \return The globally unique id for the stored message
140      */
141     virtual MsgId logMessage(Message msg) = 0;
142
143     //! Request a certain number (or all) messages stored in a given buffer.
144     /** \param buffer   The buffer we request messages from
145      *  \param lastmsgs The number of messages we would like to receive, or -1 if we'd like all messages from that buffername
146      *  \param offset   Do not return (but DO count) messages with MsgId >= offset, if offset >= 0
147      *  \return The requested list of messages
148      */
149     virtual QList<Message> requestMsgs(BufferInfo buffer, int lastmsgs = -1, int offset = -1) = 0;
150
151     //! Request messages stored in a given buffer since a certain point in time.
152     /** \param buffer   The buffer we request messages from
153      *  \param since    Only return messages newer than this point in time
154      *  \param offset   Do not return messages with MsgId >= offset, if offset >= 0
155      *  \return The requested list of messages
156      */
157     virtual QList<Message> requestMsgs(BufferInfo buffer, QDateTime since, int offset = -1) = 0;
158
159     //! Request a range of messages stored in a given buffer.
160     /** \param buffer   The buffer we request messages from
161      *  \param first    Return messages with first <= MsgId <= last
162      *  \param last     Return messages with first <= MsgId <= last
163      *  \return The requested list of messages
164      */
165     virtual QList<Message> requestMsgRange(BufferInfo buffer, int first, int last) = 0;
166
167   signals:
168     //! Sent when a new BufferInfo is created, or an existing one changed somehow.
169     void bufferInfoUpdated(UserId user, const BufferInfo &);
170     //! Sent when a new user has been added
171     void userAdded(UserId, const QString &username);
172     //! Sent when a user has been renamed
173     void userRenamed(UserId, const QString &newname);
174     //! Sent when a user has been removed
175     void userRemoved(UserId);
176
177   public:
178
179 };
180
181
182 #endif