XmlElement* KeyPressMappingSet::createXml (const bool saveDifferencesFromDefaultSet) const
{
    ScopedPointer <KeyPressMappingSet> defaultSet;

    if (saveDifferencesFromDefaultSet)
    {
        defaultSet = new KeyPressMappingSet (commandManager);
        defaultSet->resetToDefaultMappings();
    }

    XmlElement* const doc = new XmlElement (T("KEYMAPPINGS"));

    doc->setAttribute (T("basedOnDefaults"), saveDifferencesFromDefaultSet);

    int i;
    for (i = 0; i < mappings.size(); ++i)
    {
        const CommandMapping* const cm = mappings.getUnchecked(i);

        for (int j = 0; j < cm->keypresses.size(); ++j)
        {
            if (defaultSet == 0
                 || ! defaultSet->containsMapping (cm->commandID, cm->keypresses.getReference (j)))
            {
                XmlElement* const map = new XmlElement (T("MAPPING"));

                map->setAttribute (T("commandId"), String::toHexString ((int) cm->commandID));
                map->setAttribute (T("description"), commandManager->getDescriptionOfCommand (cm->commandID));
                map->setAttribute (T("key"), cm->keypresses.getReference (j).getTextDescription());

                doc->addChildElement (map);
            }
        }
    }

    if (defaultSet != 0)
    {
        for (i = 0; i < defaultSet->mappings.size(); ++i)
        {
            const CommandMapping* const cm = defaultSet->mappings.getUnchecked(i);

            for (int j = 0; j < cm->keypresses.size(); ++j)
            {
                if (! containsMapping (cm->commandID, cm->keypresses.getReference (j)))
                {
                    XmlElement* const map = new XmlElement (T("UNMAPPING"));

                    map->setAttribute (T("commandId"), String::toHexString ((int) cm->commandID));
                    map->setAttribute (T("description"), commandManager->getDescriptionOfCommand (cm->commandID));
                    map->setAttribute (T("key"), cm->keypresses.getReference (j).getTextDescription());

                    doc->addChildElement (map);
                }
            }
        }
    }

    return doc;
}