1
/****************************************************************************
2
** message.cpp - messages and history
3
** Copyright (C) 2001, 2002 Justin Karneges
5
** This program is free software; you can redistribute it and/or
6
** modify it under the terms of the GNU General Public License
7
** as published by the Free Software Foundation; either version 2
8
** of the License, or (at your option) any later version.
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.
15
** You should have received a copy of the GNU General Public License
16
** along with this program; if not, write to the Free Software
17
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
19
****************************************************************************/
35
MessageHistory::MessageHistory(const QString &jid, int xmode)
37
v_jid = cleanJid(jid);
39
QString fname = getHistoryDir() + "/" + qstrlower(jidEncode(v_jid)) + ".history";
40
pdb(DEBUG_MAINWIN, QString("MessageHistory: fname=[%1]\n").arg(fname.latin1()));
45
if(xmode == HISTORY_WRITE)
46
ok = f.open(IO_WriteOnly | IO_Append);
47
else if(xmode == HISTORY_READ)
48
ok = f.open(IO_ReadOnly);
49
else if(xmode == HISTORY_FLAG)
50
ok = f.open(IO_ReadWrite);
53
pdb(DEBUG_MAINWIN, QString("Error opening message history. Actions will be ignored. [mode=%1]\n").arg(xmode));
57
if(mode == HISTORY_READ || mode == HISTORY_FLAG)
58
f.at(f.size()-2); // last byte should be a newline. we want the byte before it.
61
MessageHistory::~MessageHistory()
63
pdb(DEBUG_MAINWIN, "MessageHistory: closing.\n");
68
void MessageHistory::stepForward()
70
if(mode != HISTORY_FLAG && mode != HISTORY_READ)
76
// go backwards till we find a newline (or the beginning of the file)
92
// skip past the newline
99
void MessageHistory::stepBack()
101
if(mode != HISTORY_FLAG && mode != HISTORY_READ)
105
if(f.at() >= f.size()-2)
108
// skip past the newline if we're not at the beginning
113
// cursor should always be after the current message, so just read forward
116
t.setEncoding(QTextStream::UnicodeUTF8);
120
// we want the position before the newline
124
Message *MessageHistory::readCurrent()
126
if(mode != HISTORY_READ && mode != HISTORY_FLAG)
134
// go backwards till we find a newline (or the beginning of the file)
154
t.setEncoding(QTextStream::UnicodeUTF8);
155
QString line = t.readLine();
160
return parseLine(line);
163
Message *MessageHistory::parseLine(const QString &line)
165
// -- read the line --
166
QString sTime, sType, sOrigin, sFlags, sText, sSubj, sUrl, sUrlDesc;
168
x1 = line.find('|') + 1;
170
x2 = line.find('|', x1);
171
sTime = line.mid(x1, x2-x1);
174
x2 = line.find('|', x1);
175
sType = line.mid(x1, x2-x1);
178
x2 = line.find('|', x1);
179
sOrigin = line.mid(x1, x2-x1);
182
x2 = line.find('|', x1);
183
sFlags = line.mid(x1, x2-x1);
186
// check for extra fields
187
if(sFlags[1] != '-') {
188
int subflags = hexChar2int(sFlags[1].latin1());
192
x2 = line.find('|', x1);
193
sSubj = line.mid(x1, x2-x1);
198
x2 = line.find('|', x1);
199
sUrl = line.mid(x1, x2-x1);
201
x2 = line.find('|', x1);
202
sUrlDesc = line.mid(x1, x2-x1);
208
sText = line.mid(x1);
213
// populate the message class
214
Message *msg = new Message;
215
msg->timeStamp = QDateTime::fromString(sTime, Qt::ISODate);
216
msg->type = sType.toInt();
217
msg->originLocal = (sOrigin == "to") ? TRUE: FALSE;
223
msg->text = logdecode(sText);
225
msg->text = logdecode(QString::fromUtf8(sText));
228
msg->subject = logdecode(sSubj);
229
msg->url = logdecode(sUrl);
230
msg->url_desc = logdecode(sUrlDesc);
236
Message *MessageHistory::readEntry()
238
Message *msg = readCurrent();
243
void MessageHistory::writeEntry(const Message &msg)
245
if(mode != HISTORY_WRITE)
249
if(!msg.subject.isEmpty())
251
if(!msg.url.isEmpty())
254
QString sTime, sType, sOrigin, sFlags;
255
sTime = msg.timeStamp.toString(Qt::ISODate);
256
sType = sType.setNum(msg.type);
257
sOrigin = msg.originLocal ? "to": "from";
260
sFlags[1] = int2hexChar(subflags);
264
// | date | type | To/from | flags | text
265
QString line = "|" + sTime + "|" + sType + "|" + sOrigin + "|" + sFlags + "|";
268
line += logencode(msg.subject) + "|";
271
line += logencode(msg.url) + "|";
272
line += logencode(msg.url_desc) + "|";
275
line += logencode(msg.text);
277
pdb(DEBUG_MAINWIN, QString("MessageHistory::writeEntry: line=[%1]\n").arg(line.latin1()));
281
t.setEncoding(QTextStream::UnicodeUTF8);
287
void MessageHistory::setFlagsCurrent(const QString &flags)
289
if(mode != HISTORY_FLAG)
292
if(flags.length() != 4)
300
// go backwards till we find a newline (or the beginning of the file)
320
t.setEncoding(QTextStream::UnicodeUTF8);
321
QString line = t.readLine();
326
// find 4 pipes in a row
329
for(n = 0; n < 4; ++n) {
330
x1 = line.find('|', x1);
337
QString oldflags = line.mid(x1, 4);
340
for(n = 0; n < 4; ++n) {
342
newflags[n] = oldflags[n];
344
newflags[n] = flags[n];
347
line.replace(x1, 4, newflags);
349
// save the updated entry
352
// go backwards till we find a newline (or the beginning of the file)
371
t.setEncoding(QTextStream::UnicodeUTF8);