CSiteManagerItemData_Site* CSiteManager::ReadServerElement(TiXmlElement *pElement) { CServer server; if (!::GetServer(pElement, server)) return 0; if (server.GetName().empty()) return 0; CSiteManagerItemData_Site* data = new CSiteManagerItemData_Site(server); TiXmlHandle handle(pElement); TiXmlText* comments = handle.FirstChildElement("Comments").FirstChild().Text(); if (comments) data->m_comments = ConvLocal(comments->Value()); TiXmlText* localDir = handle.FirstChildElement("LocalDir").FirstChild().Text(); if (localDir) data->m_localDir = ConvLocal(localDir->Value()); TiXmlText* remoteDir = handle.FirstChildElement("RemoteDir").FirstChild().Text(); if (remoteDir) data->m_remoteDir.SetSafePath(ConvLocal(remoteDir->Value())); if (!data->m_localDir.empty() && !data->m_remoteDir.IsEmpty()) data->m_sync = GetTextElementBool(pElement, "SyncBrowsing", false); return data; }
bool CThemeProvider::ThemeHasSize(const wxString& themePath, const wxString& size) { wxFileName fn(wxGetApp().GetResourceDir() + themePath, _T("theme.xml")); TiXmlElement* pDocument = GetXmlFile(fn.GetFullPath(), false); if (!pDocument) return false; TiXmlElement* pTheme = pDocument->FirstChildElement("Theme"); if (!pTheme) { delete pDocument->GetDocument(); return false; } for (TiXmlElement* pSize = pTheme->FirstChildElement("size"); pSize; pSize = pSize->NextSiblingElement("size")) { const char* txt = pSize->GetText(); if (!txt) continue; if (size == ConvLocal(txt)) { delete pDocument->GetDocument(); return true; } } delete pDocument->GetDocument(); return false; }
std::list<wxString> CThemeProvider::GetThemeSizes(const wxString& themePath) { std::list<wxString> sizes; wxFileName fn(wxGetApp().GetResourceDir() + themePath, _T("theme.xml")); TiXmlElement* pDocument = GetXmlFile(fn.GetFullPath(), false); if (!pDocument) return sizes; TiXmlElement* pTheme = pDocument->FirstChildElement("Theme"); if (pTheme) { for (TiXmlElement* pSize = pTheme->FirstChildElement("size"); pSize; pSize = pSize->NextSiblingElement("size")) { const char* txt = pSize->GetText(); if (!txt) continue; wxString size = ConvLocal(txt); if (size == _T("")) continue; sizes.push_back(size); } } delete pDocument->GetDocument(); return sizes; }
wxString GetTextAttribute(pugi::xml_node node, const char* name) { wxASSERT(node); const char* value = node.attribute(name).value(); return ConvLocal(value); }
wxString GetTextAttribute(TiXmlElement* node, const char* name) { wxASSERT(node); const char* value = node->Attribute(name); if (!value) return wxString(); return ConvLocal(value); }
wxString GetTextElement(TiXmlElement* node) { wxASSERT(node); for (TiXmlNode* pChild = node->FirstChild(); pChild; pChild = pChild->NextSibling()) { if (pChild->ToText()) { return ConvLocal(pChild->Value()); } } return wxString(); }
wxString GetTextElement(TiXmlElement* node, const char* name) { wxASSERT(node); TiXmlElement* element = node->FirstChildElement(name); if (!element) return wxString(); TiXmlNode* textNode = element->FirstChild(); if (!textNode || !textNode->ToText()) return wxString(); return ConvLocal(textNode->Value()); }
bool COptions::GetXmlValue(unsigned int nID, wxString &value, TiXmlElement *settings /*=0*/) { if (!settings) { if (!m_pXmlFile) return false; settings = m_pXmlFile->GetElement()->FirstChildElement("Settings"); if (!settings) { TiXmlNode *node = m_pXmlFile->GetElement()->InsertEndChild(TiXmlElement("Settings")); if (!node) return false; settings = node->ToElement(); if (!settings) return false; } } TiXmlNode *node = 0; while ((node = settings->IterateChildren("Setting", node))) { TiXmlElement *setting = node->ToElement(); if (!setting) continue; const char *attribute = setting->Attribute("name"); if (!attribute) continue; if (strcmp(attribute, options[nID].name)) continue; TiXmlNode *text = setting->FirstChild(); if (!text) { value.clear(); return true; } if (!text->ToText()) return false; value = ConvLocal(text->Value()); return true; } return false; }
void COptions::LoadOptionFromElement(TiXmlElement* pOption, std::map<std::string, unsigned int> const& nameOptionMap, bool allowDefault) { const char* name = pOption->Attribute("name"); if (!name) return; auto const iter = nameOptionMap.find(name); if (iter != nameOptionMap.end()) { if (!allowDefault && options[iter->second].flags == default_only) return; wxString value; TiXmlNode *text = pOption->FirstChild(); if (text) { if (!text->ToText()) return; value = ConvLocal(text->Value()); } if (options[iter->second].flags == default_priority) { if (allowDefault) { scoped_lock l(m_sync_); m_optionsCache[iter->second].from_default = true; } else { scoped_lock l(m_sync_); if (m_optionsCache[iter->second].from_default) return; } } if (options[iter->second].type == number) { long numValue = 0; value.ToLong(&numValue); numValue = Validate(iter->second, numValue); scoped_lock l(m_sync_); m_optionsCache[iter->second] = numValue; } else { value = Validate(iter->second, value); scoped_lock l(m_sync_); m_optionsCache[iter->second] = value; } } }
bool CSiteManager::GetBookmarks(wxString sitePath, std::list<wxString> &bookmarks) { wxChar c = sitePath[0]; if (c != '0' && c != '1') return false; sitePath = sitePath.Mid(1); // We have to synchronize access to sitemanager.xml so that multiple processed don't write // to the same file or one is reading while the other one writes. CInterProcessMutex mutex(MUTEX_SITEMANAGER); CXmlFile file; TiXmlElement* pDocument = 0; if (c == '0') pDocument = file.Load(_T("sitemanager")); else { const wxString& defaultsDir = wxGetApp().GetDefaultsDir(); if (defaultsDir == _T("")) return false; pDocument = file.Load(wxFileName(defaultsDir, _T("fzdefaults.xml"))); } if (!pDocument) { wxMessageBox(file.GetError(), _("Error loading xml file"), wxICON_ERROR); return false; } TiXmlElement* pElement = pDocument->FirstChildElement("Servers"); if (!pElement) return false; std::list<wxString> segments; if (!UnescapeSitePath(sitePath, segments)) { wxMessageBox(_("Site path is malformed."), _("Invalid site path")); return 0; } TiXmlElement* pChild = GetElementByPath(pElement, segments); if (pChild && !strcmp(pChild->Value(), "Bookmark")) pChild = pChild->Parent()->ToElement(); if (!pChild || strcmp(pChild->Value(), "Server")) return 0; // Bookmarks for (TiXmlElement* pBookmark = pChild->FirstChildElement("Bookmark"); pBookmark; pBookmark = pBookmark->NextSiblingElement("Bookmark")) { TiXmlHandle handle(pBookmark); wxString name = GetTextElement_Trimmed(pBookmark, "Name"); if (name.empty()) continue; wxString localPath; CServerPath remotePath; TiXmlText* localDir = handle.FirstChildElement("LocalDir").FirstChild().Text(); if (localDir) localPath = ConvLocal(localDir->Value()); TiXmlText* remoteDir = handle.FirstChildElement("RemoteDir").FirstChild().Text(); if (remoteDir) remotePath.SetSafePath(ConvLocal(remoteDir->Value())); if (localPath.empty() && remotePath.IsEmpty()) continue; bookmarks.push_back(name); } return true; }
CSiteManagerItemData_Site* CSiteManager::GetSiteByPath(wxString sitePath) { wxChar c = sitePath[0]; if (c != '0' && c != '1') { wxMessageBox(_("Site path has to begin with 0 or 1."), _("Invalid site path")); return 0; } sitePath = sitePath.Mid(1); // We have to synchronize access to sitemanager.xml so that multiple processed don't write // to the same file or one is reading while the other one writes. CInterProcessMutex mutex(MUTEX_SITEMANAGER); CXmlFile file; TiXmlElement* pDocument = 0; if (c == '0') pDocument = file.Load(_T("sitemanager")); else { const wxString& defaultsDir = wxGetApp().GetDefaultsDir(); if (defaultsDir == _T("")) { wxMessageBox(_("Site does not exist."), _("Invalid site path")); return 0; } wxFileName name(defaultsDir, _T("fzdefaults.xml")); pDocument = file.Load(name); } if (!pDocument) { wxMessageBox(file.GetError(), _("Error loading xml file"), wxICON_ERROR); return 0; } TiXmlElement* pElement = pDocument->FirstChildElement("Servers"); if (!pElement) { wxMessageBox(_("Site does not exist."), _("Invalid site path")); return 0; } std::list<wxString> segments; if (!UnescapeSitePath(sitePath, segments) || segments.empty()) { wxMessageBox(_("Site path is malformed."), _("Invalid site path")); return 0; } TiXmlElement* pChild = GetElementByPath(pElement, segments); if (!pChild) { wxMessageBox(_("Site does not exist."), _("Invalid site path")); return 0; } TiXmlElement* pBookmark; if (!strcmp(pChild->Value(), "Bookmark")) { pBookmark = pChild; pChild = pChild->Parent()->ToElement(); segments.pop_back(); } else pBookmark = 0; CSiteManagerItemData_Site* data = ReadServerElement(pChild); if (!data) { wxMessageBox(_("Could not read server item."), _("Invalid site path")); return 0; } if (pBookmark) { TiXmlHandle handle(pBookmark); wxString localPath; CServerPath remotePath; TiXmlText* localDir = handle.FirstChildElement("LocalDir").FirstChild().Text(); if (localDir) localPath = ConvLocal(localDir->Value()); TiXmlText* remoteDir = handle.FirstChildElement("RemoteDir").FirstChild().Text(); if (remoteDir) remotePath.SetSafePath(ConvLocal(remoteDir->Value())); if (!localPath.empty() && !remotePath.IsEmpty()) { data->m_sync = GetTextElementBool(pBookmark, "SyncBrowsing", false); } else data->m_sync = false; data->m_localDir = localPath; data->m_remoteDir = remotePath; } data->m_path = BuildPath( c, segments ); return data; }
bool CSiteManager::Load(TiXmlElement *pElement, CSiteManagerXmlHandler* pHandler) { wxASSERT(pElement); wxASSERT(pHandler); for (TiXmlElement* pChild = pElement->FirstChildElement(); pChild; pChild = pChild->NextSiblingElement()) { if (!strcmp(pChild->Value(), "Folder")) { wxString name = GetTextElement_Trimmed(pChild); if (name.empty()) continue; const bool expand = GetTextAttribute(pChild, "expanded") != _T("0"); if (!pHandler->AddFolder(name, expand)) return false; Load(pChild, pHandler); if (!pHandler->LevelUp()) return false; } else if (!strcmp(pChild->Value(), "Server")) { CSiteManagerItemData_Site* data = ReadServerElement(pChild); if (data) { pHandler->AddSite(data); // Bookmarks for (TiXmlElement* pBookmark = pChild->FirstChildElement("Bookmark"); pBookmark; pBookmark = pBookmark->NextSiblingElement("Bookmark")) { TiXmlHandle handle(pBookmark); wxString name = GetTextElement_Trimmed(pBookmark, "Name"); if (name.empty()) continue; CSiteManagerItemData* data = new CSiteManagerItemData(CSiteManagerItemData::BOOKMARK); TiXmlText* localDir = handle.FirstChildElement("LocalDir").FirstChild().Text(); if (localDir) data->m_localDir = GetTextElement(pBookmark, "LocalDir"); TiXmlText* remoteDir = handle.FirstChildElement("RemoteDir").FirstChild().Text(); if (remoteDir) data->m_remoteDir.SetSafePath(ConvLocal(remoteDir->Value())); if (data->m_localDir.empty() && data->m_remoteDir.IsEmpty()) { delete data; continue; } if (!data->m_localDir.empty() && !data->m_remoteDir.IsEmpty()) data->m_sync = GetTextElementBool(pBookmark, "SyncBrowsing", false); pHandler->AddBookmark(name, data); } if (!pHandler->LevelUp()) return false; } } } return true; }
bool GetServer(TiXmlElement *node, CServer& server) { wxASSERT(node); wxString host = GetTextElement(node, "Host"); if (host.empty()) return false; int port = GetTextElementInt(node, "Port"); if (port < 1 || port > 65535) return false; if (!server.SetHost(host, port)) return false; int const protocol = GetTextElementInt(node, "Protocol"); if (protocol < 0 || protocol > ServerProtocol::MAX_VALUE) { return false; } server.SetProtocol(static_cast<ServerProtocol>(protocol)); int type = GetTextElementInt(node, "Type"); if (type < 0 || type >= SERVERTYPE_MAX) return false; server.SetType((enum ServerType)type); int logonType = GetTextElementInt(node, "Logontype"); if (logonType < 0) return false; server.SetLogonType((enum LogonType)logonType); if (server.GetLogonType() != ANONYMOUS) { wxString user = GetTextElement(node, "User"); wxString pass; if ((long)NORMAL == logonType || (long)ACCOUNT == logonType) pass = GetTextElement(node, "Pass"); if (!server.SetUser(user, pass)) return false; if ((long)ACCOUNT == logonType) { wxString account = GetTextElement(node, "Account"); if (account.empty()) return false; if (!server.SetAccount(account)) return false; } } int timezoneOffset = GetTextElementInt(node, "TimezoneOffset"); if (!server.SetTimezoneOffset(timezoneOffset)) return false; wxString pasvMode = GetTextElement(node, "PasvMode"); if (pasvMode == _T("MODE_PASSIVE")) server.SetPasvMode(MODE_PASSIVE); else if (pasvMode == _T("MODE_ACTIVE")) server.SetPasvMode(MODE_ACTIVE); else server.SetPasvMode(MODE_DEFAULT); int maximumMultipleConnections = GetTextElementInt(node, "MaximumMultipleConnections"); server.MaximumMultipleConnections(maximumMultipleConnections); wxString encodingType = GetTextElement(node, "EncodingType"); if (encodingType == _T("Auto")) server.SetEncodingType(ENCODING_AUTO); else if (encodingType == _T("UTF-8")) server.SetEncodingType(ENCODING_UTF8); else if (encodingType == _T("Custom")) { wxString customEncoding = GetTextElement(node, "CustomEncoding"); if (customEncoding.empty()) return false; if (!server.SetEncodingType(ENCODING_CUSTOM, customEncoding)) return false; } else server.SetEncodingType(ENCODING_AUTO); if (CServer::SupportsPostLoginCommands(server.GetProtocol())) { std::vector<wxString> postLoginCommands; TiXmlElement* pElement = node->FirstChildElement("PostLoginCommands"); if (pElement) { TiXmlElement* pCommandElement = pElement->FirstChildElement("Command"); while (pCommandElement) { TiXmlNode* textNode = pCommandElement->FirstChild(); if (textNode && textNode->ToText()) { wxString command = ConvLocal(textNode->Value()); if (!command.empty()) postLoginCommands.push_back(command); } pCommandElement = pCommandElement->NextSiblingElement("Command"); } } if (!server.SetPostLoginCommands(postLoginCommands)) return false; } server.SetBypassProxy(GetTextElementInt(node, "BypassProxy", false) == 1); server.SetName(GetTextElement_Trimmed(node, "Name")); if (server.GetName().empty()) server.SetName(GetTextElement_Trimmed(node)); return true; }
bool GetServer(pugi::xml_node node, CServer& server) { wxASSERT(node); wxString host = GetTextElement(node, "Host"); if (host.empty()) return false; int port = GetTextElementInt(node, "Port"); if (port < 1 || port > 65535) return false; if (!server.SetHost(host, port)) return false; int const protocol = GetTextElementInt(node, "Protocol"); if (protocol < 0 || protocol > ServerProtocol::MAX_VALUE) { return false; } server.SetProtocol(static_cast<ServerProtocol>(protocol)); int type = GetTextElementInt(node, "Type"); if (type < 0 || type >= SERVERTYPE_MAX) return false; server.SetType((enum ServerType)type); int logonType = GetTextElementInt(node, "Logontype"); if (logonType < 0) return false; server.SetLogonType((enum LogonType)logonType); if (server.GetLogonType() != ANONYMOUS) { wxString user = GetTextElement(node, "User"); wxString pass, key; if ((long)NORMAL == logonType || (long)ACCOUNT == logonType) { auto passElement = node.child("Pass"); if (passElement) { pass = GetTextElement(passElement); wxString encoding = GetTextAttribute(passElement, "encoding"); if (encoding == _T("base64")) { wxMemoryBuffer buf = wxBase64Decode(pass); if (!buf.IsEmpty()) { pass = wxString::FromUTF8(static_cast<const char*>(buf.GetData()), buf.GetDataLen()); } else { pass.clear(); } } else if (!encoding.empty()) { pass.clear(); server.SetLogonType(ASK); } } } else if ((long)KEY == logonType) { key = GetTextElement(node, "Keyfile"); // password should be empty if we're using a key file pass = wxString(); server.SetKeyFile(key); } if (!server.SetUser(user, pass)) return false; if ((long)ACCOUNT == logonType) { wxString account = GetTextElement(node, "Account"); if (account.empty()) return false; if (!server.SetAccount(account)) return false; } } int timezoneOffset = GetTextElementInt(node, "TimezoneOffset"); if (!server.SetTimezoneOffset(timezoneOffset)) return false; wxString pasvMode = GetTextElement(node, "PasvMode"); if (pasvMode == _T("MODE_PASSIVE")) server.SetPasvMode(MODE_PASSIVE); else if (pasvMode == _T("MODE_ACTIVE")) server.SetPasvMode(MODE_ACTIVE); else server.SetPasvMode(MODE_DEFAULT); int maximumMultipleConnections = GetTextElementInt(node, "MaximumMultipleConnections"); server.MaximumMultipleConnections(maximumMultipleConnections); wxString encodingType = GetTextElement(node, "EncodingType"); if (encodingType == _T("Auto")) server.SetEncodingType(ENCODING_AUTO); else if (encodingType == _T("UTF-8")) server.SetEncodingType(ENCODING_UTF8); else if (encodingType == _T("Custom")) { wxString customEncoding = GetTextElement(node, "CustomEncoding"); if (customEncoding.empty()) return false; if (!server.SetEncodingType(ENCODING_CUSTOM, customEncoding)) return false; } else server.SetEncodingType(ENCODING_AUTO); if (CServer::SupportsPostLoginCommands(server.GetProtocol())) { std::vector<wxString> postLoginCommands; auto element = node.child("PostLoginCommands"); if (element) { for (auto commandElement = element.child("Command"); commandElement; commandElement = commandElement.next_sibling("Command")) { wxString command = ConvLocal(commandElement.child_value()); if (!command.empty()) { postLoginCommands.push_back(command); } } } if (!server.SetPostLoginCommands(postLoginCommands)) return false; } server.SetBypassProxy(GetTextElementInt(node, "BypassProxy", false) == 1); server.SetName(GetTextElement_Trimmed(node, "Name")); if (server.GetName().empty()) server.SetName(GetTextElement_Trimmed(node)); return true; }
wxString GetTextElement(pugi::xml_node node) { wxASSERT(node); return ConvLocal(node.child_value()); }
wxString GetTextElement(pugi::xml_node node, const char* name) { wxASSERT(node); return ConvLocal(node.child_value(name)); }