summaryrefslogtreecommitdiffstats
path: root/ksirc/dockservercontroller.cpp
blob: 3fe84aa86b83e30814b22843a3aa6ecebaa9a1bb (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/**********************************************************************

 $$Id$$

 Dock Server Controller.  Displays server connection window, and makes
 new server connection on demand.

 *********************************************************************/

#include "dockservercontroller.h"

#include <tqpopupmenu.h>
#include <tqwidget.h>
#include <tqapplication.h>
#include <tqregexp.h>
#include <tqtimer.h>
#include <tqcursor.h>
#include <kpassivepopup.h>
#include <kpopupmenu.h>
#include <klocale.h>
#include <kaction.h>
#include <twin.h>
#include <kiconloader.h>
#include <kdebug.h>
#include <kpassivepopup.h>
#include <kmessagebox.h>
#include <kglobalaccel.h>
#include <kkeydialog.h>
#include <kdeversion.h>

#include "KSPrefs/ksprefs.h"

#include "servercontroller.h"
#include "toplevel.h"
#include "ksopts.h"

#include "objFinder.h"
#include "displayMgr.h"
#include "objFinder.h"

#include "dccNew.h"

extern DisplayMgr *displayMgr;

dscNickInfo::dscNickInfo(TQString nick, TQString server) :
    m_nick(nick),
    m_server(server)
{
}

dscNickInfo::~dscNickInfo()
{
}


dockServerController::dockServerController(servercontroller *sc, TQWidget *parent, const char *_name)
: KSystemTray(parent, _name),
  m_sc(sc)
{

  m_nicks.setAutoDelete(true);
  KPopupMenu *pop = contextMenu();
  pop->setName("dockServerController_menu_pop");

#ifndef NDEBUG
  pop->insertItem(i18n("Dump Object Tree"), sc, TQT_SLOT(dump_obj()));
  pop->insertItem(i18n("Server Debug Window"), sc, TQT_SLOT(server_debug()));
  pop->insertSeparator();
#endif


  pop->insertItem(SmallIcon( "filter" ), i18n("&Filter Rule Editor..."),
                  m_sc, TQT_SLOT(filter_rule_editor()));

  KStdAction::preferences(TQT_TQOBJECT(m_sc), TQT_SLOT(general_prefs()),
                  m_sc->actionCollection())->plug(pop);
  KStdAction::configureNotifications(TQT_TQOBJECT(m_sc), TQT_SLOT(notification_prefs()), actionCollection())->plug(pop);

  pop->insertSeparator();
  pop->insertItem(i18n("New &Server..."),
		  m_sc, TQT_SLOT(new_connection()));
  pop->insertItem(i18n("&Do Autoconnect..."), m_sc, TQT_SLOT(start_autoconnect_check()));
  connect(this, TQT_SIGNAL(quitSelected()), m_sc, TQT_SLOT(endksirc()));

#if KDE_IS_VERSION(3,1,92)
  m_pic_dock = KSystemTray::loadIcon( "ksirc" );
#else
  m_pic_dock = UserIcon("ksirc_dock");
#endif
  m_pic_info = UserIcon("info");

  if ( !m_pic_dock.isNull() )
      setPixmap( m_pic_dock );

  mainPop = 0x0;
  m_mainPopVisible = false;
  createMainPopup();

  m_blinkTimer = new TQTimer( this );
  connect( m_blinkTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT( blinkDockedIcon() ) );
  m_blinktqStatus = false;
  m_blinkActive = false;

  servercontroller::self()
      ->getGlobalAccel()
      ->insert("Raise Last Window", i18n("Raise Last Window"),
		i18n("If someone said your nick in a window, this action "
                     "will make that window active for you."),
	       ALT+CTRL+Key_A, KKey::QtWIN+CTRL+Key_A, TQT_TQOBJECT(this),
	       TQT_SLOT(raiseLastActiveWindow()));
  servercontroller::self()
      ->getGlobalAccel()
      ->insert("Clear Blink", i18n("Clear Blinking Dock Icon"),
		i18n("If the dock icon is blinking, but you don't want "
		     "to go to the window this will clear the blinking."),
	       ALT+CTRL+Key_Down, KKey::QtWIN+CTRL+Key_Down, TQT_TQOBJECT(this),
	       TQT_SLOT(blinkClear()));

//  mainPop = new KPopupMenu(this, "dockServerController_main_pop");
//  mainPop->setTitle(i18n("KSirc Dock Menu"));

}



dockServerController::~dockServerController()
{
kdDebug(5008) << "~dockServerController in" << endl;
    m_sc = 0x0;
kdDebug(5008) << "~dockServerController out" << endl;
}

int dockServerController::intoPopupSorted(TQString str, TQPopupMenu *what)
{
    uint i = 0;

    for(i = 0; i < mainPop->count(); i++){
	if(mainPop->text(mainPop->idAt(i)) > str){
	    break;
	}
    }
    return mainPop->insertItem(str, what, -1, i);

}

void dockServerController::mainPopShow()
{
    m_mainPopVisible = true;
}

void dockServerController::mainPopHide()
{
    m_mainPopVisible = false;
}

void dockServerController::createMainPopup()
{
    if(m_mainPopVisible == true)
	return;

    if(mainPop)
	delete mainPop;
    mainPop = new KPopupMenu(this, "dockservercontrller_main_pop");
    connect(mainPop, TQT_SIGNAL(activated(int)),
	    this, TQT_SLOT(mainActivated(int)));
    connect(mainPop, TQT_SIGNAL(aboutToShow()),
	    this, TQT_SLOT(mainPopShow()));
    connect(mainPop, TQT_SIGNAL(aboutToHide()),
	    this, TQT_SLOT(mainPopHide()));


    TQDictIterator<dscNickInfo> it( m_nicks );
    for( ; it.current(); ++it){
	KPopupMenu *sub = new KPopupMenu(mainPop);
	if(it.current()->status() == dscNickInfo::isOnline){
	    sub->insertItem(i18n("Came Online: ") + it.current()->online().toString("hh:mm"));
	    if( ! it.current()->offline().isNull() ){
		sub->insertItem(i18n("Last Offline: ") + it.current()->offline().toString("hh:mm"));
	    }
	    int pid = sub->insertItem(i18n("Ping"));
	    int wid = sub->insertItem(i18n("Whois"));
	    int cid = sub->insertItem(i18n("Chat"));
	    int dcid = sub->insertItem(i18n("DCC Chat"));

	    int id = intoPopupSorted(it.current()->server() +" -> " + it.current()->nick() + " " + i18n("online"), sub);
	    sub->setItemParameter(pid, id);
	    sub->setItemParameter(cid, id);
	    sub->setItemParameter(wid, id);
	    sub->setItemParameter(dcid, id);
	    connect(sub, TQT_SIGNAL(activated(int)),
                    this, TQT_SLOT(subItemActivated(int)));
	}
	else {
	    sub->insertItem(i18n("Went Offline: ") + it.current()->offline().toString("hh:mm"));
	    if( ! it.current()->online().isNull() ){
		sub->insertItem(i18n("Last Online: ") + it.current()->online().toString("hh:mm"));
	    }

	    intoPopupSorted(it.current()->server() +" -> " + it.current()->nick() + i18n(" offline"), sub);
	}
    }
    if(m_blink_reason.count() > 0){
        mainPop->insertSeparator();
        for ( TQStringList::Iterator it = m_blink_reason.begin(); it != m_blink_reason.end(); ++it ) {
            mainPop->insertItem(*it);
        }
    }
    if(mainPop->count() == 0){
        mainPop->insertItem(i18n("Help on Notify Popup..."), this, TQT_SLOT(helpNotice()));
	mainPop->insertItem(i18n("Configure Notify..."), this, TQT_SLOT(configNotify()));
    }
}

void dockServerController::subItemActivated(int sub_id)
{
    const TQPopupMenu *sub = dynamic_cast<const TQPopupMenu *>(sender());
    if(!sub){
        kdDebug(5008) << "subItemActivated by non TQPopupMenu" << endl;
        return;
    }
    int main_id = sub->itemParameter(sub_id);
    TQString mainText = mainPop->text(main_id);
    if(mainText.isNull()){
        kdDebug(5008) << "failed to get mainPop text" << endl;
    }
    TQRegExp rx("(\\S+) -> (\\S+) ");
    if(rx.search(mainText) >= 0){
	TQString mserver = rx.cap(1);
	TQString mnick = rx.cap(2);

	TQString ns = mserver + "/" + mnick;

	if(m_nicks[ns]){
            kdDebug(5008) << "Got " << sub->text(sub_id) << " request for " << ns << endl;
            TQString cmd = sub->text(sub_id);
            TQString server = m_nicks[ns]->server();
            TQString nick = m_nicks[ns]->nick();

	    if(cmd == i18n("Chat")){
                servercontroller::self()->new_toplevel(KSircChannel(server, nick));
            }
	    else {
                KSircProcess *kp = servercontroller::self()->processes().find(server);
                if(kp){
                    KSircTopLevel *wm = dynamic_cast<KSircTopLevel *>(kp->getWindowList().find("!default"));
		    if(wm){
			if(cmd == i18n("Ping")){
			    wm->sirc_line_return("/ping " + nick + "\n");
			} else if(cmd == i18n("Whois")){
			    wm->sirc_line_return("/whois " + nick + "\n");
			} else if(cmd == i18n("DCC Chat")){
			    wm->sirc_line_return("/dcc chat " + nick + "\n");
			}
                    }
                }
            }
	}
    }

}

void dockServerController::mousePressEvent( TQMouseEvent *e )
{
    if(e->button() == Qt::LeftButton){
	showPopupMenu(mainPop);
    }
    else {
	KSystemTray::mousePressEvent(e);
    }
}

void dockServerController::showPopupMenu( TQPopupMenu *menu )
{
    Q_ASSERT( menu != 0L );

    menu->popup(TQCursor::pos());

}

void dockServerController::nickOnline(TQString server, TQString nick)
{
    TQString sn = server + "/" + nick;
    if(!m_nicks[sn])
        m_nicks.insert(sn, new dscNickInfo(nick, server));
    m_nicks[sn]->setOnline();
    createMainPopup();
}

void dockServerController::nickOffline(TQString server, TQString nick)
{
    TQString sn = server + "/" + nick;
    if(!m_nicks[sn])
	m_nicks.insert(sn, new dscNickInfo(nick, server));
    m_nicks[sn]->setOffline();
    createMainPopup();
}

void dockServerController::serverOpen(TQString)
{
}

void dockServerController::serverClose(TQString server)
{
    TQDictIterator<dscNickInfo> it( m_nicks );
    while(it.current()){
	if(it.current()->server() == server)
	    m_nicks.remove(it.currentKey());
	else
            ++it;
    }
    createMainPopup();
}

void dockServerController::startBlink(const TQString& reason, const TQString& text)
{
    if(m_blinkActive == false){
        // change icon (to "someone's talking to you" icon)
        setPixmap( m_pic_info );
        m_blinktqStatus = true;
        m_blinkActive = true;
	m_blinkTimer->start( 500 ); // half a second
    }
    if(!reason.isNull()){
        TQString br(reason);
        if(text.isNull() && ksopts->runDocked && ksopts->dockPopups)
	    KPassivePopup::message(TQString("Notify: %1").tqarg(reason), this);
	else {
	    TQStringList sl;
	    TQString cutup = text;
	    int i = 0;
	    br = reason + " " + text.left(50);
	    if(text.length() > 50)
                br.append("...");
	    while(!cutup.isEmpty() && i++ < 3){
		sl.append(cutup.left(50));
                cutup.remove(0, 50);
	    }
	    cutup = sl.join("\n");
	    cutup.truncate(cutup.length()-1);
	    if(ksopts->runDocked && ksopts->dockPopups)
		KPassivePopup::message(TQString("%1").tqarg(reason), cutup, this);
	}
	m_blink_reason.append(br);
	createMainPopup();
	kdDebug(5008) << "Blink reason: " << br << endl;

	TQRegExp rx("(\\S+) -> (\\S+)");
	if(rx.search(reason) >= 0){
	    TQString server = rx.cap(1);
	    TQString nick = rx.cap(2);

	    m_last_server = server;
	    m_last_nick = nick;
	}
    }
}

void dockServerController::stopBlink(const TQString& reason, bool clear)
{
    if(clear == true){
        m_blinkActive = false;
        m_blinkTimer->stop();
        setPixmap( m_pic_dock );
        m_blink_reason.clear();
    }
    else {
        if(!reason.isNull())
            m_blink_reason.remove(reason);
    }
    createMainPopup();
}

void dockServerController::blinkClear()
{
    m_sc->resetNotification();
}

void dockServerController::mainActivated(int id)
{
    TQRegExp rx("(\\S+) -> (\\S+) ");
    if(rx.search(mainPop->text(id)) >= 0){
        TQString server = rx.cap(1);
        TQString nick = rx.cap(2);

        raiseWindow(server, nick);

    }
}

void dockServerController::raiseLastActiveWindow()
{
    if((!m_last_server.isNull()) && (!m_last_nick.isNull()))
        raiseWindow(m_last_server, m_last_nick);
}

void dockServerController::blinkDockedIcon()
{
    m_blinktqStatus = !m_blinktqStatus;
    setPixmap( m_blinktqStatus ? m_pic_info : m_pic_dock );
}

void dockServerController::helpNotice()
{
    KMessageBox::information(0x0,
			     i18n("This popup menu can show a list of "
				  "people you have in your notify "
				  "list, and their status. You can configure "
				  "this list by going to Configure KSirc->"
				  "Startup->Notify and adding people to the "
				  "list. This will take effect the next "
				  "time you connect to a server. "
				  "This message appears when "
				  "there is nothing in your notify "
				  "list or when no one in your "
				  "list is online."
				 ),
			     i18n("Help for Notification Popup"));

}

void dockServerController::configNotify()
{
    KSPrefs *kp = new KSPrefs();
    connect(kp, TQT_SIGNAL(update(int)),
	    m_sc, TQT_SLOT(configChange()));
    kp->resize(550, 450);
    kp->showPage(2); /* Show auto connect page */
    kp->show();

}


void dockServerController::raiseWindow(TQString server, TQString name)
{
    TQCString txt = server.utf8() + "_" + name.utf8() + "_toplevel";
    TQWidget *obj = dynamic_cast<TQWidget *>( objFinder::find(txt, "KSircTopLevel"));
    if(obj == 0x0){
	txt = server.utf8() + "_!" + name.utf8() + "_toplevel";
	obj = dynamic_cast<TQWidget *>( objFinder::find(txt, "KSircTopLevel"));
    }

    if(obj != 0x0){
	displayMgr->raise(obj, true);
    }
    else {
	kdWarning() << "Did not find widget ptr to raise it" << endl;
    }
}

#include "dockservercontroller.moc"