summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircquerytab.cpp
blob: 53474b93122b067866586726e945df8a7c09b9d5 (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
#include <qhbox.h>
#include <qwhatsthis.h>
#include "ircquerytab.h"
#include "ircservertab.h"

IRCQueryTab::IRCQueryTab(IRCPerson *person, IRCServerTab *parentTab, MainWindow *mainWindow, QWidget *parent, const char *name, WFlags f) : IRCTab(parent, name, f) {
    m_mainWindow = mainWindow;
    m_parentTab = parentTab;
    m_lines = 0;
    m_person = new IRCPerson(*person); /* We need this (the person might sign off and the original IRCPerson gets deleted) */
    m_description->setText(tr("Talking to ") + " <b>" + person->nick() + "</b>");
    QHBox *hbox = new QHBox(this);
    m_textview = new QTextView(hbox);
    m_textview->setHScrollBarMode(QScrollView::AlwaysOff);
    m_textview->setVScrollBarMode(QScrollView::AlwaysOn);
    m_textview->setTextFormat(RichText);
    QWhatsThis::add(m_textview, tr("Private discussion"));
    m_field = new IRCHistoryLineEdit(this);

    connect(m_field, SIGNAL(nextTab()), this, SIGNAL(nextTab()));
    connect(m_field, SIGNAL(prevTab()), this, SIGNAL(prevTab()));
    connect(m_field, SIGNAL(closeTab()),this, SLOT(remove()));
    connect(this, SIGNAL(editFocus()), m_field, SLOT(setEditFocus()));

    QWhatsThis::add(m_field, tr("Type your text here in order to send a message to the other person"));
    m_layout->add(hbox);
    hbox->show();
    m_layout->add(m_field);

    connect(m_field, SIGNAL(returnPressed()), this, SLOT(processCommand()));
    connect(m_mainWindow, SIGNAL(updateScroll()), this, SLOT(scrolling()));
    settingsChanged();
    m_field->setFocus();
    m_field->setActiveWindow();
}

void IRCQueryTab::scrolling(){
  m_textview->ensureVisible(0, m_textview->contentsHeight());
}


void IRCQueryTab::appendText(QString text) {
    /* not using append because it creates layout problems */
    QString txt = m_textview->text() + text + "\n";
    if (m_maxLines > 0 && m_lines >= m_maxLines) {
        int firstBreak = txt.find('\n');
        if (firstBreak != -1) {
            txt = "<qt bgcolor=\"" + m_backgroundColor + "\"/>" + txt.right(txt.length() - (firstBreak + 1));
        }
    } else {
        m_lines++;
    }
    m_textview->setText(txt);
    m_textview->ensureVisible(0, m_textview->contentsHeight());

    if ( IRCServerTab::containsPing( text, m_parentTab ) )
        emit ping( title() );

    emit changed(this);
}

IRCQueryTab::~IRCQueryTab() {
    m_parentTab->removeQueryTab(this);
    delete m_person;
}

void IRCQueryTab::processCommand() {
    QString text = m_field->text();
    if (text.length()>0) {
        if (session()->isSessionActive()) {
            if (text.startsWith("/") && !text.startsWith("//")) {
                /* Command mode */
                m_parentTab->executeCommand(this, text);;
            } else {
                if (text.startsWith("//"))
                    text = text.right(text.length()-1);
                session()->sendMessage(m_person, m_field->text());
                appendText("<font color=\"" + m_textColor + "\">&lt;</font><font color=\"" + m_selfColor + "\">"+m_parentTab->server()->nick()+"</font><font color=\"" + m_textColor + "\">&gt; "+IRCOutput::toHTML(m_field->text())+"</font><br>");
            }
        } else {
            appendText("<font color=\"" + m_errorColor + "\">"+tr("Disconnected")+"</font><br>");
        }
    }
    m_field->clear();
}

void IRCQueryTab::display(IRCOutput output) {
    if (output.type() == OUTPUT_QUERYPRIVMSG) {
        appendText("<font color=\"" + m_textColor + "\">&lt;</font><font color=\"" + m_otherColor + "\">"+m_person->nick()+"</font><font color=\"" + m_textColor + "\">&gt; " + output.htmlMessage() + "</font><br>");
    } else if (output.type() == OUTPUT_QUERYACTION) {
        appendText("<font color=\"" + m_otherColor + "\">" + output.htmlMessage() + "<br>");
    }
}

void IRCQueryTab::settingsChanged() {
    m_textview->setText("<qt bgcolor=\"" + m_backgroundColor + "\"/>");
    m_lines = 0;
}

QString IRCQueryTab::title() {
    return m_person->nick();
}

IRCSession *IRCQueryTab::session() {
    return m_parentTab->session();
}

void IRCQueryTab::remove() {
    m_mainWindow->killTab(this);
}

IRCPerson *IRCQueryTab::person() {
    return m_person;
}