otr: removed TLV parsing code (i still has to fix SMP queries in callback)
[dyskinesia.git] / src / floatman.cpp
blobe0bedbcc98936992e3bacb20f8539ddc534f2137
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://sam.zoy.org/wtfpl/COPYING for more details.
10 * we are the Borg.
12 #include <QDebug>
14 #include <ctype.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
19 #include "floatman.h"
21 #include "chatform.h"
22 #include "psyccontact.h"
25 extern bool debugOutput;
28 ///////////////////////////////////////////////////////////////////////////////
29 FloatersManager::FloatersManager (ChatForm *mCForm, int titleBlinkTO, int msgBlinkTO) :
30 FloatWinManager(titleBlinkTO, msgBlinkTO, mCForm), mForm(mCForm)
32 mMsgIcon = 0;
33 loadFState();
37 FloatersManager::~FloatersManager () {
38 //saveFState();
39 foreach (QIcon *i, mIconCache) delete i;
40 mIconCache.clear();
41 delete mMsgIcon;
42 QHash <PsycProto::Status, QIcon *> mIconCache;
46 const QIcon *FloatersManager::getStatusIcon (FloatingWindow *sender, PsycProto::Status status) {
47 Q_UNUSED(sender)
48 if (!mIconCache[status]) {
49 QIcon *i = new QIcon(mForm->statusIcon(status));
50 mIconCache[status] = i;
52 return mIconCache[status];
56 const QIcon *FloatersManager::getMsgIcon (FloatingWindow *sender) {
57 Q_UNUSED(sender)
58 if (!mMsgIcon) mMsgIcon = new QIcon(MSG_TRAY_ICON);
59 return mMsgIcon;
63 static void dsSaveString (QDataStream &st, const QString &s) {
64 QByteArray ba(s.toUtf8());
65 int len = ba.size();
66 st << len;
67 for (int f = 0; f < ba.size(); f++) st << (quint8)(ba.at(f));
71 static QString dsLoadString (QDataStream &st) {
72 QByteArray ba;
73 int len;
74 st >> len;
75 for (int f = 0; f < len; f++) {
76 quint8 ch;
77 st >> ch;
78 ba.append((char)ch);
80 return QString::fromUtf8(ba);
84 static QString toAscii85BA (const QByteArray &ba) {
85 QByteArray res;
86 K8ASCII85::encode(res, ba);
87 return QString::fromAscii(res);
91 static QByteArray fromAscii85BA (const QString &s) {
92 QByteArray res;
93 QByteArray ba(s.toAscii());
94 K8ASCII85::decode(res, ba);
95 return res;
99 void FloatersManager::saveFState () const {
100 if (!mList.count()) {
101 mForm->setOpt("/floaty/*state", "");
102 return;
105 QByteArray ba;
107 QDataStream st(&ba, QIODevice::WriteOnly);
108 st << (quint8)0; // version
109 // save uni list
111 int sz = mList.size();
112 st << sz;
113 QHash<QString, FloatingWindow *>::const_iterator i;
114 for (i = mList.begin(); i != mList.end(); ++i) {
115 dsSaveString(st, i.key());
116 (*i)->saveFState(st);
120 //for (int f = 0; f < ba.size(); f++) fprintf(stderr, "%02X ", (quint8)(ba.at(f))); fprintf(stderr, "\n");
122 QString o(toAscii85BA(ba));
123 //fprintf(stderr, "%s\n", o.toAscii().constData());
125 mForm->setOpt("/floaty/*state", o);
129 void FloatersManager::loadFState () {
130 clear();
131 QString s(mForm->toStringOpt("/floaty/*state"));
132 //fprintf(stderr, "%s\n", s.toAscii().constData());
134 QByteArray ba(fromAscii85BA(s));
136 //for (int f = 0; f < ba.size(); f++) fprintf(stderr, "%02X ", (quint8)(ba.at(f))); fprintf(stderr, "\n");
138 QDataStream st(&ba, QIODevice::ReadOnly);
139 quint8 ver;
140 st >> ver;
141 if (ver != 0) return; // invalid version
142 // load uni list
143 int sz;
144 st >> sz;
145 //qDebug() << " " << sz << "items";
146 while (sz-- > 0) {
147 QString uniS(dsLoadString(st));
148 //qDebug() << " " << uniS;
149 FloatingWindow *w = produce(uniS, "", "", PsycProto::Offline);
150 if (!w) return; // error!
151 w->restoreFState(st);
152 PsycContact *cc = mForm->findContact(uniS);
153 if (!cc || cc->isPlace()) {
154 qDebug() << " no such contact!";
155 remove(w);
156 } else {
157 //qDebug() << " *" << cc->uni() << cc->verbatim();
158 newTitle(cc->uni(), cc->verbatim());
159 newTip(cc->uni(), cc->uni());