00001
00002 #include "ircserverlist.h"
00003 #include "ircservereditor.h"
00004
00005
00006 #include <qpe/qpeapplication.h>
00007
00008
00009 #include <qlayout.h>
00010 #include <qlabel.h>
00011 #include <qhbox.h>
00012 #include <qpushbutton.h>
00013 #include <qwhatsthis.h>
00014
00015 class IRCListBoxServer : public QListBoxText
00016 {
00017 public:
00018 IRCListBoxServer(IRCServer server) : QListBoxText(server.name())
00019 {
00020 m_server = server;
00021 }
00022
00023 IRCServer server()
00024 {
00025 return m_server;
00026 }
00027
00028 void setServer(IRCServer server)
00029 {
00030 m_server = server;
00031 setText(m_server.name());
00032 }
00033 protected:
00034 IRCServer m_server;
00035 };
00036
00037 IRCServerList::IRCServerList(QWidget* parent, const char *name, bool modal, WFlags) : QDialog(parent, name, modal, WStyle_ContextHelp)
00038 {
00039 QVBoxLayout *layout = new QVBoxLayout(this, 5, 5);
00040 setCaption(tr("Serverlist Browser"));
00041 QLabel *label = new QLabel(tr("Please choose a server profile"), this);
00042 label->setAlignment(AlignHCenter);
00043 layout->addWidget(label);
00044 m_list = new QListBox(this);
00045 QWhatsThis::add(m_list, tr("Select a server profile from this list and then tap on OK in the upper-right corner"));
00046 layout->addWidget(m_list);
00047 QHBox *buttons = new QHBox(this);
00048 QPushButton *del = new QPushButton(tr("Delete"), buttons);
00049 QPushButton *edit = new QPushButton(tr("Edit"), buttons);
00050 QPushButton *add = new QPushButton(tr("Add"), buttons);
00051 QWhatsThis::add(del, tr("Delete the currently selected server profile"));
00052 QWhatsThis::add(edit, tr("Edit the currently selected server profile"));
00053 QWhatsThis::add(add, tr("Add a new server profile"));
00054 connect(del, SIGNAL(clicked()), this, SLOT(delServer()));
00055 connect(edit, SIGNAL(clicked()), this, SLOT(editServer()));
00056 connect(add, SIGNAL(clicked()), this, SLOT(addServer()));
00057 layout->addWidget(buttons);
00058
00059 m_config = new Config("OpieIRC");
00060 m_config->setGroup("OpieIRC");
00061 int count = m_config->readNumEntry("ServerCount", 0);
00062 if (count)
00063 {
00064 for (int i=0; i<count; i++)
00065 {
00066 m_config->setGroup("OpieIRC");
00067 QString name = m_config->readEntry("Server"+QString::number(i));
00068 if (name.length() > 0)
00069 {
00070 IRCServer server;
00071 m_config->setGroup(name);
00072 server.setName(name);
00073 server.setHostname(m_config->readEntry("Hostname"));
00074 server.setPort(m_config->readNumEntry("Port"));
00075 server.setUsername(m_config->readEntry("Username"));
00076 server.setPassword(m_config->readEntry("Password"));
00077 server.setNick(m_config->readEntry("Nick"));
00078 server.setRealname(m_config->readEntry("Realname"));
00079 server.setChannels(m_config->readEntry("Channels"));
00080 m_list->insertItem(new IRCListBoxServer(server));
00081 }
00082 }
00083 }
00084
00085 connect(m_list, SIGNAL(doubleClicked(QListBoxItem*)),
00086 this, SLOT(acceptOnClick(QListBoxItem *)));
00087 connect(m_list, SIGNAL(returnPressed(QListBoxItem*)),
00088 this, SLOT(acceptOnClick(QListBoxItem*)));
00089
00090 QPEApplication::showDialog( this );
00091 }
00092
00093 void IRCServerList::addServer()
00094 {
00095 IRCServer server;
00096 IRCServerEditor editor(server, this, "ServerEditor", TRUE);
00097 if (editor.exec() == QDialog::Accepted)
00098 {
00099 server = editor.getServer();
00100
00101 m_list->insertItem(new IRCListBoxServer(server));
00102 }
00103 }
00104
00105 void IRCServerList::delServer()
00106 {
00107 int index = m_list->currentItem();
00108 if (index != -1)
00109 {
00110 m_list->removeItem(index);
00111 }
00112 }
00113
00114 void IRCServerList::editServer()
00115 {
00116 int index = m_list->currentItem();
00117 if (index != -1)
00118 {
00119 IRCListBoxServer *item = (IRCListBoxServer *)m_list->item(index);
00120 IRCServer server = item->server();
00121 IRCServerEditor editor(server, this, "ServerEditor", TRUE);
00122 if (editor.exec() == QDialog::Accepted)
00123 {
00124 server = editor.getServer();
00125 item->setServer(server);
00126 }
00127 }
00128 }
00129
00130 void IRCServerList::acceptOnClick( QListBoxItem* ) {
00131 accept();
00132 }
00133
00134 int IRCServerList::exec()
00135 {
00136 int returncode = QDialog::exec();
00137
00138 m_config->setGroup("OpieIRC");
00139 m_config->writeEntry("ServerCount", QString::number(m_list->count()));
00140 for (unsigned int i=0; i<m_list->count(); i++)
00141 {
00142 IRCServer server = ((IRCListBoxServer *)m_list->item(i))->server();
00143 m_config->setGroup("OpieIRC");
00144 m_config->writeEntry("Server"+QString::number(i), server.name());
00145 m_config->setGroup(server.name());
00146 m_config->writeEntry("Hostname", server.hostname());
00147 m_config->writeEntry("Port", QString::number(server.port()));
00148 m_config->writeEntry("Username", server.username());
00149 m_config->writeEntry("Password", server.password());
00150 m_config->writeEntry("Nick", server.nick());
00151 m_config->writeEntry("Realname", server.realname());
00152 m_config->writeEntry("Channels", server.channels());
00153 }
00154 return returncode;
00155 }
00156
00157 bool IRCServerList::hasServer()
00158 {
00159 return (m_list->currentItem() != -1);
00160 }
00161
00162 IRCServer IRCServerList::server()
00163 {
00164 return ((IRCListBoxServer *)m_list->item(m_list->currentItem()))->server();
00165 }
00166
00167 IRCServerList::~IRCServerList()
00168 {
00169 delete m_config;
00170 }