summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Kaduk <mateusz.kaduk@gmail.com>2005-06-19 10:55:02 +0000
committerMateusz Kaduk <mateusz.kaduk@gmail.com>2005-06-19 10:55:02 +0000
commit64fe1c1db81b38a5e6e20ec9b0348578e3e8b52f (patch)
tree60162c76980d54accd98c1e9ea4d0ac00a85ffd2
parent1f86c22c35d2de5e9c71d9627adc18f780347f30 (diff)
downloadmana-client-64fe1c1db81b38a5e6e20ec9b0348578e3e8b52f.tar.gz
mana-client-64fe1c1db81b38a5e6e20ec9b0348578e3e8b52f.tar.bz2
mana-client-64fe1c1db81b38a5e6e20ec9b0348578e3e8b52f.tar.xz
mana-client-64fe1c1db81b38a5e6e20ec9b0348578e3e8b52f.zip
Now added buddy are saved
-rw-r--r--src/resources/buddylist.cpp79
-rw-r--r--src/resources/buddylist.h28
2 files changed, 95 insertions, 12 deletions
diff --git a/src/resources/buddylist.cpp b/src/resources/buddylist.cpp
index c3098b67..d6848461 100644
--- a/src/resources/buddylist.cpp
+++ b/src/resources/buddylist.cpp
@@ -23,20 +23,30 @@
#include "buddylist.h"
#include <iostream>
+#include <fstream>
BuddyList::BuddyList()
{
- // Open buddy file
- buddyXmlwriterFilename("buddy.xml");
+ // Find home dir
+ findHomeDir();
+
+ // Create buddy file
+ buddyXmlwriterMemory();
// Load buddy from file
- // TODO
+ streamFile();
}
BuddyList::~BuddyList()
{
int rc;
+ std::ofstream outputStream(filename->c_str(), std::ios::trunc);
+ if( !outputStream ) {
+ std::cerr << "Error opening output stream" << std::endl;
+ return;
+ }
+
/* Close last element. */
rc = xmlTextWriterEndElement(writer);
if (rc < 0) {
@@ -45,15 +55,69 @@ BuddyList::~BuddyList()
}
xmlFreeTextWriter(writer);
+ xmlCleanupParser();
+
+ /* Write and close file */
+ outputStream << (const char*)buf->content;
+ outputStream.close();
+ xmlBufferFree(buf);
+
+ delete filename;
}
-void BuddyList::buddyXmlwriterFilename(const char *uri)
+
+void BuddyList::findHomeDir(void) {
+ //TODO: Find homeDir
+ filename = new std::string("buddy.xml");
+}
+
+void BuddyList::streamFile(void) {
+ int ret;
+
+ reader = xmlReaderForFile(filename->c_str(), NULL, 0);
+ if (reader != NULL) {
+ ret = xmlTextReaderRead(reader);
+ while (ret == 1) {
+ processNode();
+ ret = xmlTextReaderRead(reader);
+ }
+ xmlFreeTextReader(reader);
+ if (ret != 0) {
+ std::cerr << filename << " : failed to parse" << std::endl;
+ }
+ } else {
+ std::cerr << "Unable to open " << filename << std::endl;
+ }
+}
+
+void BuddyList::processNode(void) {
+ const xmlChar *name, *value;
+
+ name = xmlTextReaderConstName(reader);
+ if (name == NULL)
+ name = BAD_CAST "--";
+
+ value = xmlTextReaderConstValue(reader);
+
+ if(!xmlTextReaderIsEmptyElement(reader) && xmlTextReaderHasValue(reader))
+ addBuddy((char *)value);
+
+}
+void BuddyList::buddyXmlwriterMemory(void)
{
int rc;
+ /* Create a new XML buffer, to which the XML document will be
+ * written */
+ buf = xmlBufferCreate();
+ if (buf == NULL) {
+ std::cerr << "buddyXmlwriterMemory: Error creating the xml buffer" << std::endl;
+ return;
+ }
+
// Create a new XmlWriter for uri, with no compression
- writer = xmlNewTextWriterFilename(uri, 0);
+ writer = xmlNewTextWriterMemory(buf, 0);
if (writer == NULL) {
- std::cerr << "buddyXmlwriterFilename: Error creating the xml writer" << std::endl;
+ std::cerr << "buddyXmlwriterMemory: Error creating the xml writer" << std::endl;
return;
}
@@ -85,7 +149,7 @@ bool BuddyList::addBuddy(const std::string buddy)
// Buddy doesnt exist, add it
buddylist.push_back(buddy);
- // Store buddy as a child of "PEOPLE"
+ // Store buddy in "PEOPLE"
rc = xmlTextWriterWriteElement(writer, BAD_CAST "PEOPLE", (xmlChar *) buddy.c_str());
if (rc < 0) {
std::cerr << "buddyXmlwriterFilename: Error at xmlTextWriterWriteElement" << std::endl;
@@ -107,6 +171,7 @@ bool BuddyList::removeBuddy(const std::string buddy)
}
}
}
+ // TODO: Remove from buddy.xml
// Buddy doesnt exist
return false;
diff --git a/src/resources/buddylist.h b/src/resources/buddylist.h
index 0987bc30..b76fbfad 100644
--- a/src/resources/buddylist.h
+++ b/src/resources/buddylist.h
@@ -29,6 +29,7 @@
#include <string>
#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>
+#include <libxml/xmlreader.h>
class BuddyList : public gcn::ListModel {
public:
@@ -63,15 +64,32 @@ class BuddyList : public gcn::ListModel {
std::string getElementAt(int number);
private:
+ /**
+ * Create a new XmlWriter for memory
+ */
+ void buddyXmlwriterMemory(void);
- /**
- * Create a new XmlWriter for uri
- */
- void buddyXmlwriterFilename(const char *uri);
+ /**
+ * Process node and load buddy
+ */
+ void processNode(void);
+
+ /**
+ * Parse a buddy XML file
+ */
+ void streamFile(void);
+
+ /**
+ * Find home dir for writing
+ */
+ void findHomeDir(void);
std::list<std::string> buddylist; /**< Buddy list */
std::list<std::string>::iterator buddyit; /**< Iterator */
- xmlTextWriterPtr writer;
+ xmlTextWriterPtr writer;
+ xmlTextReaderPtr reader;
+ xmlBufferPtr buf;
+ std::string *filename; /* File to work with */
};
#endif /* _TMW_BUDDYLIST_H */