Пример #1
0
void AbiCollabSessionManager::storeProfile()
{
	UT_DEBUGMSG(("AbiCollabSessionManager::storeProfile()\n"));

	xmlBufferPtr doc = xmlBufferCreate();
	if (doc)
	{
		xmlTextWriterPtr writer = xmlNewTextWriterMemory(doc, 0);
		if (writer)
		{
			int rc = xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL);
			if (rc >= 0)
			{
				// TODO: one could check every return value here, but I'm lazy right now
				xmlTextWriterStartElement(writer, reinterpret_cast<const xmlChar*>("AbiCollabProfile"));
				
				for (UT_uint32 i = 0; i < m_vecAccounts.size(); i++)
				{
					AccountHandler* pHandler = m_vecAccounts[i];
					UT_continue_if_fail(pHandler);
					
					xmlTextWriterStartElement(writer, reinterpret_cast<const xmlChar*>("AccountHandler"));
					
					// write out the account handler type
					xmlTextWriterWriteAttribute(writer, reinterpret_cast<const xmlChar*>("type"), BAD_CAST pHandler->getStorageType().utf8_str());
					
					// write out the account handler properties
					for (PropertyMap::const_iterator cit = pHandler->getProperties().begin(); cit != pHandler->getProperties().end(); cit++)
					{
						xmlTextWriterWriteElement(writer, BAD_CAST cit->first.c_str(), BAD_CAST BAD_CAST cit->second.c_str());
					}
					
					// write out the account handler buddies
					xmlTextWriterStartElement(writer, reinterpret_cast<const xmlChar*>("buddies"));
					
					for (UT_uint32 j = 0; j < pHandler->getBuddies().size(); j++)
					{
						BuddyPtr pBuddy = pHandler->getBuddies()[j];
						UT_continue_if_fail(pBuddy);
						if (!pBuddy->isVolatile())
						{
							// we need to be able to store buddy properties
							UT_ASSERT_HARMLESS(UT_NOT_IMPLEMENTED);
							/*xmlTextWriterStartElement(writer, reinterpret_cast<const xmlChar*>("buddy"));
							// write out the buddy properties
							// TODO: for now, the only useful property a buddy has is its "name";
							// However in the future we really should write out a generic property list
							xmlTextWriterWriteElement(
									writer,
									reinterpret_cast<const xmlChar*>("name"), 
									reinterpret_cast<const xmlChar*>(pBuddy->getName().utf8_str())
								);
							xmlTextWriterEndElement(writer);*/ /* end buddy */
						}
					}
					
					xmlTextWriterEndElement(writer); /* end buddies */
					xmlTextWriterEndElement(writer); /* end AccountHandler */
				}
				
				xmlTextWriterEndElement(writer); /* end AbiCollabProfile */
			}
			xmlTextWriterEndDocument(writer);
			xmlFreeTextWriter(writer);

			gchar * s = g_build_filename(XAP_App::getApp()->getUserPrivateDirectory(), 
										 "AbiCollab.Profile",NULL);
			UT_UTF8String profile(s);
			FREEP(s);

			char *uri = UT_go_filename_to_uri(profile.utf8_str());
			GError* error = 0;
			GsfOutput* out = UT_go_file_create (uri, &error);
			if (out)
			{
				gsf_output_write(out, 
							strlen(reinterpret_cast<const char*>(const_cast<const xmlChar*>(doc->content))), 
							reinterpret_cast<const guint8*>(const_cast<const xmlChar*>(doc->content))
						);
				gsf_output_close(out);
				g_object_unref(G_OBJECT(out));
			}
			else
            {
				UT_DEBUGMSG(("Error creating AbiCollab Profile %s: %s!\n", profile.utf8_str(), error ? error->message : "unknown error"));
            }
			FREEP(uri);
		}
		else
        {
			UT_DEBUGMSG(("Error creating XML output writer\n"));
        }
		xmlBufferFree(doc);
	}
	else
    {
		UT_DEBUGMSG(("Error creating XML output buffer\n"));
    }
}