summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/mainwindow.cpp
blob: 5edcf6671af04d4d285ee6d77acda9983ee8c467 (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

#include <opie2/odebug.h>
#include <opie2/oresource.h>

#include <qmenubar.h>
#include <qwhatsthis.h>

#include "mainwindow.h"
#include "ircservertab.h"
#include "dcctransfertab.h"
#include "ircserverlist.h"
#include "ircsettings.h"

#include <stdio.h>


QString MainWindow::appCaption() {
    return QObject::tr("Opie IRC");
}


MainWindow::MainWindow(QWidget *parent, const char *name, WFlags) : QMainWindow(parent, name, WStyle_ContextHelp) {
    setCaption(tr("IRC Client"));
    m_tabWidget = new IRCTabWidget(this);
    QWhatsThis::add(m_tabWidget, tr("Server connections, channels, queries and other things will be placed here"));
    connect(m_tabWidget, SIGNAL(currentChanged(QWidget*)), this, SLOT(selected(QWidget*)));
    setCentralWidget(m_tabWidget);
    setToolBarsMovable(FALSE);
    QMenuBar *menuBar = new QMenuBar(this);
    QPopupMenu *irc = new QPopupMenu(this);
    menuBar->insertItem(tr("IRC"), irc);
    QAction *a = new QAction( tr("New connection"),
                              Opie::Core::OResource::loadPixmap( "pass", Opie::Core::OResource::SmallIcon ),
                              QString::null, 0, this, 0 );
    connect(a, SIGNAL(activated()), this, SLOT(newConnection()));
    a->setWhatsThis(tr("Create a new connection to an IRC server"));
    a->addTo(irc);
    a = new QAction( tr("Settings"),
                     Opie::Core::OResource::loadPixmap( "SettingsIcon", Opie::Core::OResource::SmallIcon ),
                     QString::null, 0, this, 0 );
    a->setWhatsThis(tr("Configure OpieIRC's behavior and appearance"));
    connect(a, SIGNAL(activated()), this, SLOT(settings()));
    a->addTo(irc);
    m_dccTab = 0;
    loadSettings();
}

/*IRCTabWidget MainWindow::getTabWidget(){
  return m_tabWidget;
} */

void MainWindow::loadSettings() {
    Config config("OpieIRC");
    config.setGroup("OpieIRC");
    IRCTab::m_backgroundColor = config.readEntry("BackgroundColor", "#FFFFFF");
    IRCTab::m_textColor = config.readEntry("TextColor", "#000000");
    IRCTab::m_errorColor = config.readEntry("ErrorColor", "#FF0000");
    IRCTab::m_selfColor = config.readEntry("SelfColor", "#CC0000");
    IRCTab::m_otherColor = config.readEntry("OtherColor", "#0000BB");
    IRCTab::m_serverColor = config.readEntry("ServerColor", "#0000FF");
    IRCTab::m_notificationColor = config.readEntry("NotificationColor", "#AA3300");
    IRCTab::m_maxLines = config.readNumEntry("Lines", 100);
    IRCTab::setUseTimeStamps( config.readBoolEntry("DisplayTime", false ) );
}

void MainWindow::selected(QWidget *) {
    m_tabWidget->setTabColor(m_tabWidget->currentPageIndex(), black);
    emit updateScroll();
}

void MainWindow::addTab(IRCTab *tab) {
    connect(tab, SIGNAL(changed(IRCTab*)), this, SLOT(changeEvent(IRCTab*)));
    connect(tab, SIGNAL(ping (const QString&)), this, SLOT(slotPing(const QString&)));
    connect(tab, SIGNAL(nextTab()), this, SLOT(slotNextTab()));
    connect(tab, SIGNAL(prevTab()), this, SLOT(slotPrevTab()));

    m_tabWidget->addTab(tab, tab->title());
    m_tabWidget->showPage(tab);
    tab->setID(m_tabWidget->currentPageIndex());
    m_tabs.append(tab);
}

void MainWindow::changeEvent(IRCTab *tab) {
    if (tab->id() != m_tabWidget->currentPageIndex())
        m_tabWidget->setTabColor(tab->id(), blue);
}

void MainWindow::killTab(IRCTab *tab, bool imediate) {
    if (tab == m_dccTab)
        m_dccTab = 0;
    
    m_toDelete.append( tab );

    if ( imediate )
        slotKillTabsLater();
    else
        QTimer::singleShot(0, this, SLOT(slotKillTabsLater()) );
}

void MainWindow::slotKillTabsLater() {
    for ( QListIterator<IRCTab> it(m_toDelete); it.current(); ++it ) {
        m_tabWidget->removePage( it.current() );
        odebug << it.current() << oendl;
        m_tabs.remove( it.current() );
    }

    m_toDelete.setAutoDelete( true );
    m_toDelete.clear();
    m_toDelete.setAutoDelete( false );
}

void MainWindow::newConnection() {
    IRCServerList list(this, "ServerList", TRUE);
    if (list.exec() == QDialog::Accepted && list.hasServer()) {
        IRCServerTab *serverTab = new IRCServerTab(list.server(), this, m_tabWidget);
        addTab(serverTab);
        serverTab->doConnect();
    }
}

void MainWindow::settings() {
    IRCSettings settings(this, "Settings", TRUE);
    if (settings.exec() == QDialog::Accepted) {
        QListIterator<IRCTab> it(m_tabs);
        for (; it.current(); ++it) {
            /* Inform all tabs about the new settings */
            it.current()->settingsChanged();
        }
    }
}


void MainWindow::slotNextTab() {
    int i = m_tabWidget->currentPageIndex ();
    m_tabWidget->setCurrentPage ( i+1 );

    int j = m_tabWidget->currentPageIndex ();
    if ( i == j )
        m_tabWidget->setCurrentPage ( 1 );
}

void MainWindow::slotPrevTab() {
    int i = m_tabWidget->currentPageIndex ();
    if ( i > 1 )
        m_tabWidget->setCurrentPage ( i-1 );
}

void MainWindow::slotPing( const QString& /*channel*/ ) {
    raise();
}

void MainWindow::addDCC(DCCTransfer::Type type, Q_UINT32 ip4Addr, Q_UINT16 port, 
        const QString &filename, const QString &nickname, unsigned int size) {
    
    if (!m_dccTab) {
        m_dccTab = new DCCTransferTab(this);
        addTab(m_dccTab);
        m_dccTab->show();
    }
    
    m_dccTab->addTransfer(type, ip4Addr, port, filename, nickname, size);
}