summaryrefslogtreecommitdiff
path: root/noncore/net/opieirc/ircserverlist.cpp
blob: 3e78469efbe3ac34482e5e9edfaf4b6e005b3d43 (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

#include "ircserverlist.h"
#include "ircservereditor.h"

/* OPIE */
#include <qpe/qpeapplication.h>

/* QT */
#include <qlayout.h>
#include <qlabel.h>
#include <qhbox.h>
#include <qpushbutton.h>
#include <qwhatsthis.h>

class IRCListBoxServer : public QListBoxText
{
public:
    IRCListBoxServer(IRCServer server) : QListBoxText(server.name())
    {
        m_server = server;
    }

    IRCServer server()
    {
        return m_server;
    }

    void setServer(IRCServer server)
    {
        m_server = server;
        setText(m_server.name());
    }
protected:
    IRCServer m_server;
};

IRCServerList::IRCServerList(QWidget* parent, const char *name, bool modal, WFlags) : QDialog(parent, name, modal, WStyle_ContextHelp)
{
    QVBoxLayout *layout = new QVBoxLayout(this, 5, 5);
    setCaption(tr("Serverlist Browser"));
    QLabel *label = new QLabel(tr("Please choose a server profile"), this);
    label->setAlignment(AlignHCenter);
    layout->addWidget(label);
    m_list = new QListBox(this);
    QWhatsThis::add(m_list, tr("Select a server profile from this list and then tap on OK in the upper-right corner"));
    layout->addWidget(m_list);
    QHBox *buttons = new QHBox(this);
    QPushButton *del = new QPushButton(tr("Delete"), buttons);
    QPushButton *edit = new QPushButton(tr("Edit"), buttons);
    QPushButton *add = new QPushButton(tr("Add"), buttons);
    QWhatsThis::add(del, tr("Delete the currently selected server profile"));
    QWhatsThis::add(edit, tr("Edit the currently selected server profile"));
    QWhatsThis::add(add, tr("Add a new server profile"));
    connect(del, SIGNAL(clicked()), this, SLOT(delServer()));
    connect(edit, SIGNAL(clicked()), this, SLOT(editServer()));
    connect(add, SIGNAL(clicked()), this, SLOT(addServer()));
    layout->addWidget(buttons);
    /* Load the configuration file */
    m_config = new Config("OpieIRC");
    m_config->setGroup("OpieIRC");
    int count = m_config->readNumEntry("ServerCount", 0);
    if (count)
    {
        for (int i=0; i<count; i++)
        {
            m_config->setGroup("OpieIRC");
            QString name = m_config->readEntry("Server"+QString::number(i));
            if (name.length() > 0)
            {
                IRCServer server;
                m_config->setGroup(name);
                server.setName(name);
                server.setHostname(m_config->readEntry("Hostname"));
                server.setPort(m_config->readNumEntry("Port"));
                server.setUsername(m_config->readEntry("Username"));
                server.setPassword(m_config->readEntry("Password"));
                server.setNick(m_config->readEntry("Nick"));
                server.setRealname(m_config->readEntry("Realname"));
                server.setChannels(m_config->readEntry("Channels"));
                m_list->insertItem(new IRCListBoxServer(server));
            }
        }
    }

    connect(m_list, SIGNAL(doubleClicked(QListBoxItem*)),
            this, SLOT(acceptOnClick(QListBoxItem *)));
    connect(m_list, SIGNAL(returnPressed(QListBoxItem*)),
            this, SLOT(acceptOnClick(QListBoxItem*)));

    QPEApplication::showDialog( this );
}

void IRCServerList::addServer()
{
    IRCServer server;
    IRCServerEditor editor(server, this, "ServerEditor", TRUE);
    if (editor.exec() == QDialog::Accepted)
    {
        server = editor.getServer();
        /* Gets deleted by QListBox, so this is ok */
        m_list->insertItem(new IRCListBoxServer(server));
    }
}

void IRCServerList::delServer()
{
    int index = m_list->currentItem();
    if (index != -1)
    {
        m_list->removeItem(index);
    }
}

void IRCServerList::editServer()
{
    int index = m_list->currentItem();
    if (index != -1)
    {
        IRCListBoxServer *item = (IRCListBoxServer *)m_list->item(index);
        IRCServer server = item->server();
        IRCServerEditor editor(server, this, "ServerEditor", TRUE);
        if (editor.exec() == QDialog::Accepted)
        {
            server = editor.getServer();
            item->setServer(server);
        }
    }
}

void IRCServerList::acceptOnClick( QListBoxItem* ) {
    accept();
}

int IRCServerList::exec()
{
    int returncode = QDialog::exec();
    /* Now save the changes */
    m_config->setGroup("OpieIRC");
    m_config->writeEntry("ServerCount", QString::number(m_list->count()));
    for (unsigned int i=0; i<m_list->count(); i++)
    {
        IRCServer server = ((IRCListBoxServer *)m_list->item(i))->server();
        m_config->setGroup("OpieIRC");
        m_config->writeEntry("Server"+QString::number(i), server.name());
        m_config->setGroup(server.name());
        m_config->writeEntry("Hostname", server.hostname());
        m_config->writeEntry("Port", QString::number(server.port()));
        m_config->writeEntry("Username", server.username());
        m_config->writeEntry("Password", server.password());
        m_config->writeEntry("Nick", server.nick());
        m_config->writeEntry("Realname", server.realname());
        m_config->writeEntry("Channels", server.channels());
    }
    return returncode;
}

bool IRCServerList::hasServer()
{
    return (m_list->currentItem() != -1);
}

IRCServer IRCServerList::server()
{
    return ((IRCListBoxServer *)m_list->item(m_list->currentItem()))->server();
}

IRCServerList::~IRCServerList()
{
    delete m_config;
}