cosmetix
[dyskinesia.git] / src / conwin.cpp
blobd42a322f9069f44ef5ab23e87f7148282f0dde76
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.
9 */
10 #include <QDebug>
12 #include <QDialog>
13 #include <QFile>
14 #include <QScrollBar>
15 #include <QTextStream>
17 #include "k8strutils.h"
18 #include "psycpkt.h"
19 #include "psycproto.h"
20 #include "winutils.h"
22 #include "conwin.h"
25 ConsoleForm::ConsoleForm (QWidget *parent) : QDialog(parent) {
26 setupUi(this);
27 setAttribute(Qt::WA_QuitOnClose, false);
28 setAttribute(Qt::WA_DeleteOnClose, false);
29 //centerWidget(this);
30 //logStr("=========================================\n", "");
34 ConsoleForm::~ConsoleForm () {
38 void ConsoleForm::on_btClear_clicked (void) {
39 tbLog->clear();
43 void ConsoleForm::logStr (const QString &pfx, const QString &str) {
44 QString spathStr = QCoreApplication::applicationDirPath();
45 if (spathStr.isEmpty()) spathStr += "./";
46 else if (spathStr[spathStr.length()-1] != '/' && spathStr[spathStr.length()-1] != '\\') spathStr += "/";
48 QFile fl(spathStr+"packets.log");
49 if (!fl.open(QIODevice::Append)) return;
50 QTextStream st(&fl);
51 st.setCodec("UTF-8");
52 if (!pfx.isEmpty()) st << pfx;
53 if (!str.isEmpty()) st << str;
57 void ConsoleForm::addPacket (const PsycPacket &pkt, bool isOutgoing) {
58 if (!cbActive->isChecked()) return;
60 QString str(pkt.toString(true)); // raw dump
62 QString pfx;
63 if (isOutgoing) pfx = "<<< OUTGOING >>>\n"; else pfx = ">>> INCOMING <<<\n";
64 logStr(pfx, str);
66 QTextCursor cr(tbLog->textCursor());
67 cr.movePosition(QTextCursor::End, QTextCursor::MoveAnchor);
68 cr.clearSelection();
69 //tbLog->moveCursor(QTextCursor::End);
70 QString s(K8Str::escapeStr(str, true));
71 s.replace("\n", "<br />\n");
72 //quint64 cursorPosition = tbLog->textCursor().position();
73 if (!isOutgoing) cr.insertHtml("<font color='#00ff00'>"+s+"</font>");
74 else cr.insertHtml(s);
75 cr.insertHtml("\n<hr>\n");
76 cr.clearSelection();
77 //tbLog->moveCursor(QTextCursor::End);
78 tbLog->verticalScrollBar()->setValue(tbLog->verticalScrollBar()->maximum());
79 //tbLog->verticalScrollBar()->setEnabled(true);