From 19129d5197e612b7a7d6574500f6266ea1de2a62 Mon Sep 17 00:00:00 2001 From: ishmal Date: Mon, 29 May 2006 01:05:11 +0000 Subject: [PATCH] Add a configuration record. Modify the GUI later. --- src/pedro/Makefile.mingw | 25 ++-- src/pedro/pedroconfig.cpp | 230 +++++++++++++++++++++++++++++++++++ src/pedro/pedroconfig.h | 249 ++++++++++++++++++++++++++++++++++++++ src/pedro/pedrogui.cpp | 2 + src/pedro/pedrogui.h | 4 +- 5 files changed, 499 insertions(+), 11 deletions(-) create mode 100644 src/pedro/pedroconfig.cpp create mode 100644 src/pedro/pedroconfig.h diff --git a/src/pedro/Makefile.mingw b/src/pedro/Makefile.mingw index 693dfca89..53b1d8e85 100644 --- a/src/pedro/Makefile.mingw +++ b/src/pedro/Makefile.mingw @@ -55,7 +55,11 @@ SSL=$(GTK) CFLAGS = -g -Wall -DHAVE_SSL INC = -I. -I$(GTK)/include -I$(SSL)/include -LIBS = -mwindows -L$(GTK)/lib -L$(SSL) -lssl -lcrypto -lgdi32 -lws2_32 + + +LIBSC = -mconsole -L$(GTK)/lib -L$(SSL) -lssl -lcrypto -lgdi32 -lws2_32 +LIBS = -mwindows -L$(GTK)/lib -L$(SSL) -lssl -lcrypto -lgdi32 -lws2_32 + RM = del CP = copy S = $(BSLASH) @@ -110,7 +114,8 @@ endif OBJ = \ pedrodom.o \ -pedroxmpp.o +pedroxmpp.o \ +pedroconfig.o GUIOBJ = \ pedrogui.o \ @@ -123,24 +128,24 @@ work/filerec.o \ work/groupchat.o test.exe: libpedro.a work/test.o - $(CXX) -o $@ work/test.o libpedro.a $(LIBS) + $(CXX) -o $@ work/test.o libpedro.a $(LIBSC) test: libpedro.a work/test.o - $(CXX) -o $@ work/test.o libpedro.a $(LIBS) + $(CXX) -o $@ work/test.o libpedro.a $(LIBSC) filesend.exe: libpedro.a work/filesend.o - $(CXX) -o $@ work/filesend.o libpedro.a $(LIBS) + $(CXX) -o $@ work/filesend.o libpedro.a $(LIBSC) filesend: libpedro.a work/filesend.o - $(CXX) -o $@ work/filesend.o libpedro.a $(LIBS) + $(CXX) -o $@ work/filesend.o libpedro.a $(LIBSC) filerec.exe: libpedro.a work/filerec.o - $(CXX) -o $@ work/filerec.o libpedro.a $(LIBS) + $(CXX) -o $@ work/filerec.o libpedro.a $(LIBSC) filerec: libpedro.a work/filerec.o - $(CXX) -o $@ work/filerec.o libpedro.a $(LIBS) + $(CXX) -o $@ work/filerec.o libpedro.a $(LIBSC) groupchat.exe: libpedro.a work/groupchat.o - $(CXX) -o $@ work/groupchat.o libpedro.a $(LIBS) + $(CXX) -o $@ work/groupchat.o libpedro.a $(LIBSC) groupchat: libpedro.a work/groupchat.o - $(CXX) -o $@ work/groupchat.o libpedro.a $(LIBS) + $(CXX) -o $@ work/groupchat.o libpedro.a $(LIBSC) pedro.exe: libpedro.a pedromain.o pedrogui.o $(CXX) -o $@ pedromain.o pedrogui.o libpedro.a $(GTKLIBS) $(LIBS) diff --git a/src/pedro/pedroconfig.cpp b/src/pedro/pedroconfig.cpp new file mode 100644 index 000000000..1197a6f1f --- /dev/null +++ b/src/pedro/pedroconfig.cpp @@ -0,0 +1,230 @@ +/* + * Implementation the Pedro mini-XMPP client + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2005-2006 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +/* +==================================================== +We are expecting an xml file with this format: + + + + + + Jabber's Main Server + jabber.org + 5222 + myname + mypassword + + + + + +==================================================== +*/ + + + +#include "pedroconfig.h" +#include "pedrodom.h" + +#include + +namespace Pedro +{ + + +static long getInt(const DOMString &s) +{ + char *start = (char *) s.c_str(); + char *end; + long val = strtol(start, &end, 10); + if (end == start) // did we read more than 1 digit? + val = 0L; + return val; +} + + + +bool XmppConfig::read(const DOMString &buffer) +{ + Parser parser; + + Element *root = parser.parse(buffer); + + if (!root) + { + error("Error in configuration syntax"); + return false; + } + + accounts.clear(); + + std::vector accountElems = root->findElements("account"); + + for (unsigned int i=0 ; igetTagValue("name"); + if (str.size()==0) + str = "unnamed account"; + account.setName(str); + + str = elem->getTagValue("host"); + if (str.size()==0) + str = "jabber.org"; + account.setName(str); + + str = elem->getTagValue("port"); + int port = (int) getInt(str); + if (port == 0) + port = 5222; + account.setPort(port); + + str = elem->getTagValue("username"); + if (str.size()==0) + str = "noname"; + account.setUsername(str); + + str = elem->getTagValue("password"); + if (str.size()==0) + str = "nopass"; + account.setPassword(str); + + accounts.push_back(account); + } + + + delete root; + + return true; +} + + + + + + +bool XmppConfig::readFile(const DOMString &fileName) +{ + + FILE *f = fopen(fileName.c_str(), "rb"); + if (!f) + { + error("Could not open configuration file '%s' for reading", + fileName.c_str()); + return false; + } + + DOMString buffer; + while (!feof(f)) + { + char ch = (char) fgetc(f); + buffer.push_back(ch); + } + fclose(f); + + if (!read(buffer)) + return false; + + return true; +} + + +DOMString XmppConfig::toXmlBuffer() +{ + + DOMString buf; + + char fmtbuf[32]; + + buf.append("\n"); + + for (unsigned int i = 0 ; i\n"); + buf.append(" "); + buf.append(acc.getName()); + buf.append("\n"); + buf.append(" "); + buf.append(acc.getHost()); + buf.append("\n"); + buf.append(" "); + snprintf(fmtbuf, 31, "%d", acc.getPort()); + buf.append(fmtbuf); + buf.append("\n"); + buf.append(" "); + buf.append(acc.getUsername()); + buf.append("\n"); + buf.append(" "); + buf.append(acc.getPassword()); + buf.append("\n"); + buf.append(" \n"); + } + + buf.append("\n"); + + return buf; +} + + + + +bool XmppConfig::writeFile(const DOMString &fileName) +{ + + FILE *f = fopen(fileName.c_str(), "wb"); + if (!f) + { + error("Could not open configuration file '%s' for writing", + fileName.c_str()); + return false; + } + + DOMString buffer = toXmlBuffer(); + char *s = (char *)buffer.c_str(); + size_t len = (size_t) strlen(s); //in case we have wide chars + + if (fwrite(s, 1, len, f) != len) + { + return false; + } + fclose(f); + + if (!read(buffer)) + return false; + + return true; +} + + + + + +} //namespace Pedro +//######################################################################## +//# E N D O F F I L E +//######################################################################## diff --git a/src/pedro/pedroconfig.h b/src/pedro/pedroconfig.h new file mode 100644 index 000000000..61650c361 --- /dev/null +++ b/src/pedro/pedroconfig.h @@ -0,0 +1,249 @@ +#ifndef __PEDROCONFIG_H__ +#define __PEDROCONFIG_H__ +/* + * Implementation the Pedro mini-XMPP client + * + * Authors: + * Bob Jamison + * + * Copyright (C) 2005-2006 Bob Jamison + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + + +#include "pedrodom.h" +#include "pedroxmpp.h" + +#include + + + +namespace Pedro +{ + + +/** + * Individual account record + */ +class XmppAccount +{ + +public: + + /** + * + */ + XmppAccount() + { init(); } + + /** + * + */ + XmppAccount(const XmppAccount &other) + { assign(other); } + + /** + * + */ + XmppAccount operator=(const XmppAccount &other) + { assign(other); return *this; } + + /** + * + */ + virtual ~XmppAccount() + {} + + + /** + * + */ + virtual DOMString getName() + { return name; } + + /** + * + */ + virtual void setName(const DOMString &val) + { name = val; } + + /** + * + */ + virtual DOMString getHost() + { return host; } + + /** + * + */ + virtual void setHost(const DOMString &val) + { host = val; } + + /** + * + */ + virtual int getPort() + { return port; } + + /** + * + */ + virtual void setPort(int val) + { port = val; } + + /** + * + */ + virtual DOMString getUsername() + { return username; } + + /** + * + */ + virtual void setUsername(const DOMString &val) + { username = val; } + + /** + * + */ + virtual DOMString getPassword() + { return password; } + + /** + * + */ + virtual void setPassword(const DOMString &val) + { password = val; } + + + +private: + + void init() + { + name = "noname"; + host = "jabber.org"; + port = 5222; + username = "nobody"; + password = "nopass"; + } + + void assign(const XmppAccount &other) + { + name = other.name; + host = other.host; + port = other.port; + username = other.username; + password = other.password; + } + + DOMString name; + DOMString host; + int port; + DOMString username; + DOMString password; + +}; + + + +/** + * Configuration record + */ +class XmppConfig : XmppEventTarget +{ + +public: + + /** + * + */ + XmppConfig() + { init(); } + + /** + * + */ + XmppConfig(const XmppConfig &other) + { assign(other); } + + /** + * + */ + virtual XmppConfig &operator=(const XmppConfig &other) + { assign(other); return *this; } + + /** + * + */ + virtual ~XmppConfig() + {} + + + /** + * Parse a configuration xml chunk from a memory buffer + */ + virtual bool read(const DOMString &buffer); + + /** + * Parse a configuration file + */ + virtual bool readFile(const DOMString &fileName); + + /** + * Ouputs this object as a string formatted in XML + */ + virtual DOMString toXmlBuffer(); + + /** + * Write a configuration file + */ + virtual bool writeFile(const DOMString &fileName); + + /** + * + */ + virtual std::vector &getAccounts() + { return accounts; } + + + +private: + + void init() + { + } + + void assign(const XmppConfig &other) + { + accounts = other.accounts; + } + + std::vector accounts; + +}; + + + + +} //namespace Pedro + +#endif /* __PEDROCONFIG_H__ */ + +//######################################################################## +//# E N D O F F I L E +//######################################################################## diff --git a/src/pedro/pedrogui.cpp b/src/pedro/pedrogui.cpp index eb02c3e56..267e47ff9 100644 --- a/src/pedro/pedrogui.cpp +++ b/src/pedro/pedrogui.cpp @@ -2411,6 +2411,8 @@ bool PedroGui::doSetup() Glib::signal_timeout().connect( sigc::mem_fun(*this, &PedroGui::checkEventQueue), 20 ); + config.readFile("pedro.ini"); + //client.addXmppEventListener(*this); client.eventQueueEnable(true); diff --git a/src/pedro/pedrogui.h b/src/pedro/pedrogui.h index b4655bec1..3b25946d3 100644 --- a/src/pedro/pedrogui.h +++ b/src/pedro/pedrogui.h @@ -28,6 +28,7 @@ #include #include "pedroxmpp.h" +#include "pedroconfig.h" namespace Pedro @@ -678,8 +679,9 @@ public: virtual ~PedroGui(); - //Let everyone share this + //Let everyone share these XmppClient client; + XmppConfig config; virtual void error(const char *fmt, ...); -- 2.30.2