summaryrefslogtreecommitdiffstats
path: root/kopete/plugins/history/historylogger.h
blob: 6d01754dfb92f421046e15c3aae19759feefb238 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
    historylogger.cpp

    Copyright (c) 2003-2004 by Olivier Goffart        <ogoffart @ kde.org>
    Kopete    (c) 2003-2004 by the Kopete developers  <kopete-devel@kde.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 2 of the License, or     *
    * (at your option) any later version.                                   *
    *                                                                       *
    *************************************************************************
*/

#ifndef HISTORYLOGGER_H
#define HISTORYLOGGER_H

#include <tqobject.h>
#include "kopetemessage.h" //TODO: REMOVE

namespace Kopete { class Contact; }
namespace Kopete { class MetaContact; }
class TQFile;
class TQDomDocument;
class TQTimer;

/**
 * One hinstance of this class is opened for every Kopete::ChatSession,
 * or for the history dialog
 *
 * @author Olivier Goffart
 */
class HistoryLogger : public TQObject
{
Q_OBJECT
  TQ_OBJECT
public:

	/**
	 * - Chronological: messages are read from the first to the last, in the time order
	 * - AntiChronological: messages are read from the last to the first, in the time reversed order
	 */
	enum Sens { Default , Chronological , AntiChronological };

	/**
	 * Constructor, takes the contact, and the color of messages
	 */
	HistoryLogger( Kopete::MetaContact *m , TQObject *tqparent = 0, const char *name = 0);
	HistoryLogger( Kopete::Contact *c , TQObject *tqparent = 0, const char *name = 0);


	~HistoryLogger();

	/**
	 * return or setif yes or no outgoing message are hidden (and not parsed)
	 */
	bool hideOutgoing() const { return m_hideOutgoing; }
	void setHideOutgoing(bool);

	/**
	 * set a searching  filter
	 * @param filter is the string to search
	 * @param caseSensitive say if the case is important
	 * @param isRegExp say if the filter is a TQRegExp, or a simle string
	 */
	 void setFilter(const TQString& filter, bool caseSensitive=false , bool isRegExp=false);
	 TQString filter() const;
	 bool filterCaseSensitive() const ;
	 bool filterRegExp() const;



	//----------------------------------

	/**
	 * log a message
	 * @param c add a presision to the contact to use, if null, autodetect.
	 */
	void appendMessage( const Kopete::Message &msg , const Kopete::Contact *c=0L );

	/**
	 * read @param lines message from the current position
	 * from Kopete::Contact @param c in the given @param sens
	 */
	TQValueList<Kopete::Message> readMessages(unsigned int lines,
		const Kopete::Contact *c=0, Sens sens=Default,
		bool reverseOrder=false, bool colorize=true);

	/** 
	 * Same as the following, but for one date. I did'nt reuse the above function
	 * because its structure is really different.
	 * Read all the messages for the given @param date
     */
	TQValueList<Kopete::Message> readMessages(TQDate date);


	/**
	 * The pausition is set to the last message
	 */
	void setPositionToLast();

	/**
	 * The position is set to the first message
	 */
	void setPositionToFirst();

	/**
	 * Set the current month  (in number of month since the actual month)
	 */
	void setCurrentMonth(int month);

	/**
     * @return The list of the days for which there is a log for m_metaContact for month of *  @param date (don't care of the day)
     */
	TQValueList<int> getDaysForMonth(TQDate date);

	/**
	 * Get the filename of the xml file which contains the history from the
	 * contact in the specified @param date. Specify @param date in order to get the filename for
	 * the given date.year() date.month().
	 */
	static TQString getFileName(const Kopete::Contact* , TQDate date);

private:
	bool m_hideOutgoing;
	bool m_filterCaseSensitive;
	bool m_filterRegExp;
	TQString m_filter;


	/*
	 *contais all TQDomDocument, for a KC, for a specified Month
	 */
	TQMap<const Kopete::Contact*,TQMap<unsigned int , TQDomDocument> > m_documents;

	/**
	 * Contains the current message.
	 * in fact, this is the next, still not showed
	 */
	TQMap<const Kopete::Contact*, TQDomElement>  m_currentElements;

	/**
	 * Get the document, open it is @param canload is true, contain is set to false if the document
	 * is not already contained
	 */
	TQDomDocument getDocument(const Kopete::Contact *c, unsigned int month , bool canLoad=true , bool* contain=0L);

	TQDomDocument getDocument(const Kopete::Contact *c, const TQDate date, bool canLoad=true, bool* contain=0L);

	/**
	 * look over files to get the last month for this contact
	 */
	unsigned int getFirstMonth(const Kopete::Contact *c);
	unsigned int getFirstMonth();


	/*
	 * the current month
	 */
	unsigned int m_currentMonth;

	/*
	 * the cached getFirstMonth
	 */
	int m_cachedMonth;



	/*
	 * the metacontact we are using
	 */
	Kopete::MetaContact *m_metaContact;

	/*
	 * keep the old position in memory, so if we change the sens, we can begin here
	 */
	TQMap<const Kopete::Contact*, TQDomElement>  m_oldElements;
	unsigned int m_oldMonth;
	Sens m_oldSens;
	 
	 /**
	  * the timer used to save the file
	  */ 
	TQTimer *m_saveTimer;
	TQDomDocument m_toSaveDocument;
	TQString m_toSaveFileName;
	unsigned int m_saveTimerTime; //time in ms between each save
	
	/**
	 * workaround for the 31 midnight bug.
	 * it contains the number of the current month.
	 */
	int m_realMonth;

	/*
	 * FIXME:
	 * WORKAROUND
	 * due to a bug in QT, i have to keep the document element in the memory to
	 * prevent crashes
	 */
	TQValueList<TQDomElement> workaround;

private slots:
	/**
	 * the metacontact has been deleted
	 */
	void slotMCDeleted();
	
	/**
	 * save the current month's document on the disk. 
	 * connected to the m_saveTimer signal
	 */
	void saveToDisk();
};

#endif