~ubuntu-branches/ubuntu/trusty/kvirc/trusty

« back to all changes in this revision

Viewing changes to src/modules/trayicon/libkvitrayicon.cpp

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2013-05-18 19:36:33 UTC
  • mfrom: (24.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20130518193633-d2caq4677ihxc93h
Tags: 4:4.2.0-2
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include "KviIrcConnectionUserInfo.h"
42
42
#include "KviOptions.h"
43
43
#include "KviIrcView.h"
44
 
#include "KviTalPopupMenu.h"
45
44
 
46
45
#include <QPixmap>
47
46
#include <QPainter>
48
47
#include <QTimer>
49
48
#include <QEvent>
50
49
#include <QRegExp>
 
50
#include <QWidgetAction>
 
51
#include <QMenu>
51
52
 
52
53
#include <stdlib.h>
53
54
#include <time.h>
60
61
#endif
61
62
 
62
63
extern KVIRC_API KviPointerHashTable<QString,KviWindow> * g_pGlobalWindowDict;
63
 
static KviPointerList<TrayIcon> * g_pTrayIconList = 0;
 
64
static KviTrayIconWidget * g_pTrayIcon = 0;
64
65
 
65
66
static QPixmap * g_pDock1 = 0;
66
67
static QPixmap * g_pDock2 = 0;
67
68
static QPixmap * g_pDock3 = 0;
68
69
 
69
 
TrayIcon::TrayIcon(KviMainWindow * frm)
70
 
: QSystemTrayIcon(frm), m_CurrentPixmap(ICON_SIZE,ICON_SIZE)
 
70
KviTrayIconWidget::KviTrayIconWidget()
 
71
: QSystemTrayIcon(g_pMainWindow), m_CurrentPixmap(ICON_SIZE,ICON_SIZE)
71
72
{
72
 
        m_pContextPopup = new KviTalPopupMenu(0);
 
73
        g_pTrayIcon = this;
 
74
    m_pContextPopup = new QMenu(0);
73
75
        setContextMenu(m_pContextPopup);
74
76
 
75
77
        m_iConsoles = 0;
81
83
        m_pFlashingTimer->setObjectName("flashing_timer");
82
84
        connect( m_pFlashingTimer, SIGNAL(timeout()), this, SLOT(flashingTimerShot()) );
83
85
        m_bFlashed=0;
84
 
        g_pTrayIconList->append(this);
85
 
        m_pFrm = frm;
86
 
        m_pFrm->setDockExtension(this);
87
 
 
88
 
        m_pTip = new KviDynamicToolTip(frm,"dock_tooltip");
89
 
        m_pAwayPopup = new KviTalPopupMenu(0);
 
86
 
 
87
        g_pMainWindow->setTrayIcon(this);
 
88
 
 
89
        m_pTip = new KviDynamicToolTip(g_pMainWindow,"dock_tooltip");
 
90
    m_pAwayPopup = new QMenu(0);
90
91
 
91
92
        m_pTitleLabel = new QLabel(__tr2qs("<b>KVIrc</b>"),m_pContextPopup);
92
93
        QPalette p;
93
94
        m_pTitleLabel->setStyleSheet("background-color: " + p.color(QPalette::Normal, QPalette::Mid).name());
94
 
        m_pContextPopup->insertItem(m_pTitleLabel);
 
95
        QWidgetAction * pAction = new QWidgetAction(this);
 
96
        pAction->setDefaultWidget(m_pTitleLabel); 
 
97
        m_pContextPopup->addAction(pAction); 
 
98
 
95
99
        m_pContextPopup->setWindowTitle(__tr2qs("Context"));
96
100
        m_pAwayMenuId = m_pContextPopup->addMenu(m_pAwayPopup);
97
101
        m_pAwayMenuId->setIcon(*(g_pIconManager->getSmallIcon(KviIconManager::Away)));
103
107
        id = m_pContextPopup->addAction(*(g_pIconManager->getSmallIcon(KviIconManager::KVIrc)),__tr2qs("&About KVIrc"),this,SLOT(executeInternalCommand(bool)));
104
108
        id->setData(KVI_INTERNALCOMMAND_ABOUT_ABOUTKVIRC);
105
109
 
106
 
        m_pContextPopup->insertSeparator();
 
110
    m_pContextPopup->addSeparator();
107
111
        m_pToggleFrame = m_pContextPopup->addAction(*(g_pIconManager->getSmallIcon(KviIconManager::Raw)),__tr2qs("Hide/Show"),this,SLOT(toggleParentFrame()));
108
112
 
109
 
        m_pContextPopup->insertSeparator();
 
113
    m_pContextPopup->addSeparator();
110
114
 
111
115
        id = m_pContextPopup->addAction(*(g_pIconManager->getSmallIcon(KviIconManager::TrayIcon)),__tr2qs("Un&dock"),this,SLOT(executeInternalCommand(bool)));
112
116
        id->setData(KVI_INTERNALCOMMAND_TRAYICON_HIDE);
121
125
}
122
126
 
123
127
 
124
 
TrayIcon::~TrayIcon()
 
128
KviTrayIconWidget::~KviTrayIconWidget()
125
129
{
126
 
        m_pFrm->setDockExtension(0);
127
 
        g_pTrayIconList->removeRef(this);
 
130
        g_pTrayIcon=0;
 
131
        g_pMainWindow->setTrayIcon(0);
128
132
        delete m_pAwayPopup;
129
133
        delete m_pTitleLabel;
130
134
        delete m_pTip;
133
137
}
134
138
 
135
139
 
136
 
void TrayIcon::executeInternalCommand(bool)
 
140
void KviTrayIconWidget::executeInternalCommand(bool)
137
141
{
138
142
        int iCmd;
139
143
        bool bOk;
140
144
        iCmd=(((QAction*)QObject::sender())->data()).toInt(&bOk);
141
 
        if(m_pFrm && bOk)
142
 
                m_pFrm->executeInternalCommand(iCmd);
 
145
        if(bOk)
 
146
                g_pMainWindow->executeInternalCommand(iCmd);
143
147
}
144
 
void TrayIcon::die()
 
148
void KviTrayIconWidget::die()
145
149
{
146
150
        delete this;
147
151
}
148
152
 
149
 
void TrayIcon::flashingTimerShot()
 
153
void KviTrayIconWidget::flashingTimerShot()
150
154
{
151
155
        m_bFlashed=!m_bFlashed;
152
156
        refresh();
176
180
        __tr("idle idle idle idle!")
177
181
};
178
182
 
179
 
bool TrayIcon::event(QEvent *e)
 
183
bool KviTrayIconWidget::event(QEvent *e)
180
184
{
181
185
        if(e->type()==QEvent::ToolTip)
182
186
        {
183
 
                QPoint pos= m_pFrm->mapFromGlobal(QCursor::pos());
 
187
                QPoint pos= g_pMainWindow->mapFromGlobal(QCursor::pos());
184
188
                QString tmp;
185
189
 
186
 
                KviWindowListBase * t = m_pFrm->windowListWidget();
 
190
                KviWindowListBase * t = g_pMainWindow->windowListWidget();
187
191
 
188
192
                QString line;
189
193
                bool first = true;
226
230
        return false;
227
231
}
228
232
 
229
 
//int TrayIcon::message(int,void *)
 
233
//int KviTrayIconWidget::message(int,void *)
230
234
//{
231
235
//      qDebug("Message");
232
236
//      update();
233
237
//      return 0;
234
238
//}
235
239
 
236
 
void TrayIcon::doAway(bool)
 
240
void KviTrayIconWidget::doAway(bool)
237
241
{
238
242
        int id;
239
243
        bool ok;
289
293
        }
290
294
}
291
295
 
292
 
void TrayIcon::fillContextPopup()
 
296
void KviTrayIconWidget::fillContextPopup()
293
297
{
294
 
        m_pToggleFrame->setText(m_pFrm->isVisible() ? __tr2qs("Hide Window") : __tr2qs("Show Window"));
 
298
        m_pToggleFrame->setText(g_pMainWindow->isVisible() ? __tr2qs("Hide Window") : __tr2qs("Show Window"));
295
299
        if(g_pApp->topmostConnectedConsole())
296
300
        {
297
301
                m_pAwayMenuId->setVisible(true);
347
351
        }
348
352
}
349
353
 
350
 
void TrayIcon::toggleParentFrame()
 
354
void KviTrayIconWidget::toggleParentFrame()
351
355
{
352
356
        qDebug("TrayIcon::toggleParentFrame()");
353
 
        if(m_pFrm->isMinimized())
 
357
        if(g_pMainWindow->isMinimized())
354
358
        {
355
359
                qDebug("- frame is minimized");
356
 
                m_pFrm->setWindowState(m_pFrm->windowState() & (~Qt::WindowMinimized | Qt::WindowActive));
 
360
                g_pMainWindow->setWindowState(g_pMainWindow->windowState() & (~Qt::WindowMinimized | Qt::WindowActive));
357
361
 
358
362
                if(KVI_OPTION_BOOL(KviOption_boolFrameIsMaximized))
359
363
                {
360
364
                        qDebug("- window was maximized so calling showMaximized()");
361
 
                        m_pFrm->showMaximized();
 
365
                        g_pMainWindow->showMaximized();
362
366
                } else {
363
367
                        qDebug("- window wasn't maximized so calling plain show()");
364
 
                        m_pFrm->show();
 
368
                        g_pMainWindow->show();
365
369
                }
366
370
#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
367
371
                // raise it
368
 
                SetForegroundWindow((HWND)m_pFrm->winId());
 
372
                SetForegroundWindow((HWND)g_pMainWindow->winId());
 
373
                g_pMainWindow->activateWindow();
369
374
#endif
370
 
        } else if(!m_pFrm->isVisible())
 
375
        } else if(!g_pMainWindow->isVisible())
371
376
        {
372
377
                qDebug("- frame is not visible");
373
378
                //restore mainwindow
374
379
                if(KVI_OPTION_BOOL(KviOption_boolFrameIsMaximized))
375
380
                {
376
381
                        qDebug("- window was maximized so calling showMaximized()");
377
 
                        m_pFrm->showMaximized();
 
382
                        g_pMainWindow->showMaximized();
378
383
                } else {
379
384
                        qDebug("- window wasn't maximized so calling plain show()");
380
 
                        m_pFrm->show();
 
385
                        g_pMainWindow->show();
381
386
                }
382
387
#if defined(COMPILE_ON_WINDOWS) || defined(COMPILE_ON_MINGW)
383
388
                // raise it
384
 
                SetForegroundWindow((HWND)m_pFrm->winId());
 
389
                SetForegroundWindow((HWND)g_pMainWindow->winId());
 
390
                g_pMainWindow->activateWindow();
385
391
#endif
386
392
        } else {
387
 
                qDebug("- frame is visible: maximized state=%d, hiding",m_pFrm->isMaximized());
388
 
                KVI_OPTION_BOOL(KviOption_boolFrameIsMaximized) = m_pFrm->isMaximized();
389
 
                m_pFrm->hide();
 
393
                qDebug("- frame is visible: maximized state=%d, hiding",g_pMainWindow->isMaximized());
 
394
                KVI_OPTION_BOOL(KviOption_boolFrameIsMaximized) = g_pMainWindow->isMaximized();
 
395
                g_pMainWindow->hide();
390
396
        }
391
397
}
392
398
 
393
 
void TrayIcon::refresh()
 
399
void KviTrayIconWidget::refresh()
394
400
{
395
401
        grabActivityInfo();
396
402
 
437
443
        updateIcon();
438
444
}
439
445
 
440
 
void TrayIcon::activatedSlot( QSystemTrayIcon::ActivationReason reason )
 
446
void KviTrayIconWidget::activatedSlot( QSystemTrayIcon::ActivationReason reason )
441
447
{
442
448
        switch(reason)
443
449
        {
448
454
                        // This activates the context menu and is quite confusing if it *also* hides the kvirc window.
449
455
                        // So on mac we only _show_ the main window if it's hidden and the CloseInTray option is enabled.
450
456
                        if((KVI_OPTION_BOOL(KviOption_boolCloseInTray) || KVI_OPTION_BOOL(KviOption_boolMinimizeInTray))
451
 
                                && ((!m_pFrm->isVisible()) || m_pFrm->isMinimized()))
 
457
                                && ((!g_pMainWindow->isVisible()) || g_pMainWindow->isMinimized()))
452
458
                                toggleParentFrame();
453
459
#else //!COMPILE_ON_MAC
454
460
                        // on other platforms we always toggle the window
461
467
        }
462
468
}
463
469
 
464
 
void TrayIcon::grabActivityInfo()
 
470
void KviTrayIconWidget::grabActivityInfo()
465
471
{
466
 
        KviWindowListBase * t = m_pFrm->windowListWidget();
 
472
        KviWindowListBase * t = g_pMainWindow->windowListWidget();
467
473
 
468
474
        if(KVI_OPTION_BOOL(KviOption_boolUseLevelBasedTrayNotification))
469
475
        {
553
559
        }
554
560
}
555
561
 
556
 
void TrayIcon::updateIcon()
 
562
void KviTrayIconWidget::updateIcon()
557
563
{
558
564
        setIcon(QIcon(m_CurrentPixmap));
559
565
}
560
566
 
561
 
static TrayIcon * trayicon_find(KviMainWindow *f)
562
 
{
563
 
        if(!g_pTrayIconList)return 0;
564
 
        for(TrayIcon * w = g_pTrayIconList->first();w;w = g_pTrayIconList->next())
565
 
        {
566
 
                if(w->frame() == f)return w;
567
 
        }
568
 
        return 0;
569
 
}
570
 
 
571
567
/*
572
568
        @doc: trayicon.show
573
569
        @type:
575
571
        @title:
576
572
                trayicon.show
577
573
        @short:
578
 
                Shows the dock widget for the current frame window
 
574
                Shows the tray icon (dock widget)
579
575
        @keyterms:
580
576
                dock widget, system tray
581
577
        @syntax:
582
578
                trayicon.show
583
579
        @description:
584
 
                Shows the dock widget for the current frame window.[br]
585
 
                The dock widget is a small widget that docks in the KDE panel.[br]
586
 
                It shows a small icon of the earth and eventually displays four squares
 
580
                Shows the tray icon (sometimes called dock widget).[br]
 
581
                The tray icon is a small widget that docks in the window manager panel.[br]
 
582
                It shows a small kvirc icon and eventually displays four squares
587
583
                that cover this icon: the bottom left square appears when there is some new
588
584
                text in any console window, the square becomes red if the text is highlighted.[br]
589
585
                The bottom right square appears when there is some new text in any channel window,
590
586
                and it becomes red when the text is highlighted.[br] The upper right square refers to
591
587
                query windows and the upper left one to any other kind of window (dcc, links...).[br]
592
 
                If you move the mouse over the dock widget a tooltip will show you the last lines
 
588
                If you move the mouse over the tray icon a tooltip will show you the last lines
593
589
                of the "new" text in all these windows.[br]
594
590
                This is useful when you keep the main KVIrc window minimized and you're working on something else:
595
 
                if the dock widget shows nothing but the earth icon, nothing is happening in the main KVIrc window.
596
 
                If the dock widget shows one or more white (or red) squares, you can move the mouse over
597
 
                and check what's happened exactly and eventually bring up the main KVIrc window by clicking on the widget.[br]
598
 
                [big]tecnical details[/big]
599
 
                The dock widget is currently working in KDE compilation mode only:
600
 
                it relies on the KWin implementation of the Window Manager interaction protocol.
 
591
                if the tray icon shows nothing but the kvirc icon, nothing is happening in the main KVIrc window.
 
592
                If the tray icon shows one or more white (or red) squares, you can move the mouse over
 
593
                and check what's happened exactly and eventually bring up the main KVIrc window by clicking on the icon.[br]
601
594
        @seealso:
602
595
                [cmd]trayicon.hide[/cmd]
603
596
*/
604
597
 
605
 
static bool trayicon_kvs_cmd_show(KviKvsModuleCommandCall * c)
 
598
static bool trayicon_kvs_cmd_show(KviKvsModuleCommandCall *)
606
599
{
607
 
        if(!(trayicon_find(c->window()->frame())))
 
600
        if(g_pTrayIcon==0)
608
601
        {
609
 
                TrayIcon * w = new TrayIcon(c->window()->frame());
 
602
                KviTrayIconWidget * w = new KviTrayIconWidget();
610
603
                w->show();
611
604
        }
612
605
        return true;
619
612
        @title:
620
613
                trayicon.hide
621
614
        @short:
622
 
                Hides the dock widget for the current frame window
 
615
                Hides the tray icon for the current frame window
623
616
        @syntax:
624
617
                trayicon.hide
625
618
        @description:
626
 
                Hides the dock widget for the current frame window
 
619
                Hides the  tray icon for the current frame window
627
620
        @seealso:
628
621
                [cmd]trayicon.show[/cmd]
629
622
*/
630
623
 
631
 
static bool trayicon_kvs_cmd_hide(KviKvsModuleCommandCall * c)
 
624
static bool trayicon_kvs_cmd_hide(KviKvsModuleCommandCall *)
632
625
{
633
 
        TrayIcon * w= trayicon_find(c->window()->frame());
634
 
        if(w)delete w;
 
626
        if(g_pTrayIcon)
 
627
                delete g_pTrayIcon;
 
628
        g_pTrayIcon=0;
 
629
 
635
630
        // show the parent frame.. otherwise there will be no way to get it back
636
 
        if(!c->window()->frame()->isVisible())
637
 
        {
638
 
                c->window()->frame()->show();
639
 
        }
 
631
        if(!g_pMainWindow->isVisible())
 
632
                g_pMainWindow->show();
 
633
 
640
634
        return true;
641
635
}
642
636
 
656
650
                [cmd]trayicon.show[/cmd], [cmd]trayicon.hide[/cmd]
657
651
*/
658
652
 
659
 
static bool trayicon_kvs_cmd_hidewindow(KviKvsModuleCommandCall * c)
 
653
static bool trayicon_kvs_cmd_hidewindow(KviKvsModuleCommandCall *)
660
654
{
661
 
        TrayIcon * w= trayicon_find(c->window()->frame());
662
 
        if(w)
663
 
        {
664
 
                c->window()->frame()->hide();
665
 
        }
 
655
        if(g_pMainWindow)
 
656
                g_pMainWindow->hide();
 
657
 
666
658
        return true;
667
659
}
668
660
 
684
676
 
685
677
static bool trayicon_kvs_fnc_isvisible(KviKvsModuleFunctionCall * c)
686
678
{
687
 
        c->returnValue()->setBoolean(trayicon_find(c->window()->frame()));
 
679
        c->returnValue()->setBoolean(g_pTrayIcon!=0);
688
680
        return true;
689
681
}
690
682
 
715
707
#endif
716
708
        g_pDock3 = new QPixmap(buffer);
717
709
 
718
 
        g_pTrayIconList = new KviPointerList<TrayIcon>;
719
 
        g_pTrayIconList->setAutoDelete(false);
720
 
 
721
 
 
722
710
        KVSM_REGISTER_SIMPLE_COMMAND(m,"hide",trayicon_kvs_cmd_hide);
723
711
        KVSM_REGISTER_SIMPLE_COMMAND(m,"hidewindow",trayicon_kvs_cmd_hidewindow);
724
712
        KVSM_REGISTER_SIMPLE_COMMAND(m,"show",trayicon_kvs_cmd_show);
729
717
 
730
718
static bool trayicon_module_cleanup(KviModule *)
731
719
{
732
 
        while(g_pTrayIconList->first())delete g_pTrayIconList->first();
733
 
        delete g_pTrayIconList;
734
 
        g_pTrayIconList = 0;
 
720
        delete g_pTrayIcon;
 
721
        g_pTrayIcon = 0;
735
722
 
736
723
        delete g_pDock1;
737
724
        g_pDock1 = 0;
747
734
 
748
735
static bool trayicon_module_can_unload(KviModule *)
749
736
{
750
 
        return g_pTrayIconList->isEmpty();
 
737
        return g_pTrayIcon==0;
751
738
}
752
739
 
753
740
// =======================================