summaryrefslogtreecommitdiffstats
path: root/konversation/src/konversationstatusbar.cpp
blob: bdfc9ca7673241ffa819972a78a8b66abe4b34fc (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
  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.
*/

/*
  Copyright (C) 2006 Eike Hein <hein@kde.org>
*/

#include "konversationstatusbar.h"
#include "konversationmainwindow.h"
#include "viewcontainer.h"
#include "ssllabel.h"

#include <tqwhatsthis.h>
#include <tqlabel.h>
#include <tqtooltip.h>

#include <kstatusbar.h>
#include <klocale.h>
#include <kiconloader.h>
#include <ksqueezedtextlabel.h>


KonversationStatusBar::KonversationStatusBar(KonversationMainWindow* window)
{
    m_window = window;

    // Initialize status bar.
    m_window->statusBar();

    m_mainLabel = new KSqueezedTextLabel(m_window->statusBar(),"mainLabel");
    setMainLabelText(i18n("Ready."));
    m_mainLabel->setSizePolicy(TQSizePolicy(TQSizePolicy::Preferred, TQSizePolicy::Fixed));
    m_mainLabel->setMinimumWidth(0);

    // KSqueezedLabel calculates the wrong height. Popular workaround.
    int height = m_window->fontMetrics().height()+2;
    m_mainLabel->setFixedHeight(height);

    m_infoLabel = new TQLabel(m_window->statusBar(), "infoLabel");
    m_infoLabel->hide();
    TQWhatsThis::add(m_infoLabel, i18n("<qt>This shows the number of users in the channel, and the number of those that are operators (ops).<p>A channel operator is a user that has special privileges, such as the ability to kick and ban users, change the channel modes, make other users operators</qt>"));

    m_lagLabel = new TQLabel(i18n("Lag: Unknown"), m_window->statusBar(), "lagLabel");
    m_lagLabel->hide();

    m_sslLabel = new SSLLabel(m_window->statusBar(),"sslLabel");
    m_sslLabel->setPixmap(SmallIcon("encrypted"));
    m_sslLabel->hide();
    TQWhatsThis::add(m_sslLabel, i18n("All communication with the server is encrypted.  This makes it harder for someone to listen in on your communications."));

    m_window->statusBar()->addWidget(m_mainLabel, 1, false);
    m_window->statusBar()->addWidget(m_infoLabel, 0, true);
    m_window->statusBar()->addWidget(m_lagLabel, 0, true);
    m_window->statusBar()->addWidget(m_sslLabel, 0, true);

    TQWhatsThis::add(m_window->statusBar(), i18n("<qt>The status bar shows various messages, including any problems connecting to the server.  On the far right the current delay to the server is shown.  The delay is the time it takes for messages from you to reach the server, and from the server back to you.</qt>"));
}

KonversationStatusBar::~KonversationStatusBar()
{
}

void KonversationStatusBar::updateAppearance()
{
    // KSqueezedLabel calculates the wrong height. Popular workaround.
    int height = m_window->fontMetrics().height()+2;
    m_mainLabel->setFixedHeight(height);
}

void KonversationStatusBar::resetStatusBar()
{
    setMainLabelText(i18n("Ready."));
    setInfoLabelShown(false);
    setLagLabelShown(false);
    clearInfoLabel();
    resetLagLabel();
}

void KonversationStatusBar::setMainLabelText(const TQString& text)
{
    m_oldMainLabelText = text;

    // Don't overwrite the temp text if there is any.
    if (m_tempMainLabelText.isEmpty())
        m_mainLabel->setText(text);
}

void KonversationStatusBar::setMainLabelTempText(const TQString& text)
{
    if (!text.isEmpty())
    {
        m_tempMainLabelText = text;
        m_mainLabel->setText(text);
    }
    else
        clearMainLabelTempText();
}

void KonversationStatusBar::clearMainLabelTempText()
{
    // Unset the temp text so the next setMainLabelText won't fail.
    m_tempMainLabelText = TQString();

    m_mainLabel->setText(m_oldMainLabelText);
}

void KonversationStatusBar::setInfoLabelShown(bool shown)
{
    if (shown)
        m_infoLabel->show();
    else
        m_infoLabel->hide();
}

void KonversationStatusBar::updateInfoLabel(const TQString& text)
{
    TQString formatted = Konversation::removeIrcMarkup(text);
    m_infoLabel->setText(formatted);

    if (m_infoLabel->isHidden()) m_infoLabel->show();
}

void KonversationStatusBar::clearInfoLabel()
{
    m_infoLabel->setText(TQString());
}

void KonversationStatusBar::setLagLabelShown(bool shown)
{
    if (shown)
        m_lagLabel->show();
    else
        m_lagLabel->hide();
}

void KonversationStatusBar::updateLagLabel(Server* lagServer, int msec)
{
    if (lagServer==m_window->getViewContainer()->getFrontServer())
    {
        setMainLabelText(i18n("Ready."));

        TQString lagString = lagServer->getServerName() + " - ";

        if (msec == -1)
            lagString += i18n("Lag: Unknown");
        else if (msec < 1000)
            lagString += i18n("Lag: %1 ms").arg(msec);
        else
            lagString += i18n("Lag: %1 s").arg(msec / 1000);

        m_lagLabel->setText(lagString);

        if (m_lagLabel->isHidden()) m_lagLabel->show();
    }
}

void KonversationStatusBar::resetLagLabel()
{
    m_lagLabel->setText(i18n("Lag: Unknown"));
}

void KonversationStatusBar::setTooLongLag(Server* lagServer, int msec)
{
    if ((msec % 5000)==0)
    {
        int seconds  = msec/1000;
        int minutes  = seconds/60;
        int hours    = minutes/60;
        int days     = hours/24;
        TQString lagString;

        if (days)
        {
            const TQString daysString = i18n("1 day", "%n days", days);
            const TQString hoursString = i18n("1 hour", "%n hours", (hours % 24));
            const TQString minutesString = i18n("1 minute", "%n minutes", (minutes % 60));
            const TQString secondsString = i18n("1 second", "%n seconds", (seconds % 60));
            lagString = i18n("%1 = name of server, %2 = (x days), %3 = (x hours), %4 = (x minutes), %5 = (x seconds)", "No answer from server %1 for more than %2, %3, %4, and %5.").arg(lagServer->getServerName())
                .arg(daysString).arg(hoursString).arg(minutesString).arg(secondsString);
            // or longer than an hour
        }
        else if (hours)
        {
            const TQString hoursString = i18n("1 hour", "%n hours", hours);
            const TQString minutesString = i18n("1 minute", "%n minutes", (minutes % 60));
            const TQString secondsString = i18n("1 second", "%n seconds", (seconds % 60));
            lagString = i18n("%1 = name of server, %2 = (x hours), %3 = (x minutes), %4 = (x seconds)", "No answer from server %1 for more than %2, %3, and %4.").arg(lagServer->getServerName())
                .arg(hoursString).arg(minutesString).arg(secondsString);
            // or longer than a minute
        }
        else if (minutes)
        {
            const TQString minutesString = i18n("1 minute", "%n minutes", minutes);
            const TQString secondsString = i18n("1 second", "%n seconds", (seconds % 60));
            lagString = i18n("%1 = name of server, %2 = (x minutes), %3 = (x seconds)", "No answer from server %1 for more than %2 and %3.").arg(lagServer->getServerName())
                .arg(minutesString).arg(secondsString);
            // or just some seconds
        }
        else
        {
            lagString = i18n("No answer from server %1 for more than 1 second.", "No answer from server %1 for more than %n seconds.", seconds).arg(lagServer->getServerName());
        }

        setMainLabelText(lagString);
    }

    if (lagServer==m_window->getViewContainer()->getFrontServer())
    {
        TQString lagString = lagServer->getServerName() + " - ";
        lagString.append(i18n("Lag: %1 s").arg(msec/1000));

        if (m_lagLabel->isHidden()) m_lagLabel->show();
        m_lagLabel->setText(lagString);
    }
}

void KonversationStatusBar::updateSSLLabel(Server* server)
{
    if (server == m_window->getViewContainer()->getFrontServer()
        && server->getUseSSL() && server->isConnected())
    {
        disconnect(m_sslLabel,0,0,0);
        connect(m_sslLabel,TQT_SIGNAL(clicked()),server,TQT_SLOT(showSSLDialog()));
        TQToolTip::remove(m_sslLabel);
        TQToolTip::add(m_sslLabel,server->getSSLInfo());
        m_sslLabel->show();
    }
    else
        m_sslLabel->hide();
}

void KonversationStatusBar::removeSSLLabel()
{
    disconnect(m_sslLabel,0,0,0);
    m_sslLabel->hide();
}

#include "konversationstatusbar.moc"