int COptions::GetCurrentSpeedLimit(int nMode) { Init(); int type[2] = { OPTION_DOWNLOADSPEEDLIMITTYPE, OPTION_UPLOADSPEEDLIMITTYPE }; int limit[2] = { OPTION_DOWNLOADSPEEDLIMIT, OPTION_UPLOADSPEEDLIMIT }; int nType = (int)GetOptionVal(type[nMode]); switch (nType) { case 0: return -1; case 1: return (int)GetOptionVal(limit[nMode]); default: { SYSTEMTIME s; GetLocalTime(&s); for (SPEEDLIMITSLIST::const_iterator iter = m_SpeedLimits[nMode].begin(); iter != m_SpeedLimits[nMode].end(); iter++) if (iter->IsItActive(s)) return iter->m_Speed; return -1; } } }
TiXmlElement* COptions::CreateSettingsXmlElement() { if (!m_pXmlFile) return 0; TiXmlElement* element = m_pXmlFile->GetElement(); if (!element) { return 0; } TiXmlElement* settings = element->FirstChildElement("Settings"); if (settings) { return settings; } settings = new TiXmlElement("Settings"); element->LinkEndChild(settings); for (int i = 0; i < OPTIONS_NUM; ++i) { if (options[i].type == string) { SetXmlValue(i, GetOption(i)); } else { SetXmlValue(i, GetOptionVal(i)); } } return settings; }
pugi::xml_node COptions::CreateSettingsXmlElement() { if (!m_pXmlFile) { return pugi::xml_node(); } auto element = m_pXmlFile->GetElement(); if (!element) { return element; } auto settings = element.child("Settings"); if (settings) { return settings; } settings = element.append_child("Settings"); for (int i = 0; i < OPTIONS_NUM; ++i) { if (options[i].type == string) { SetXmlValue(i, GetOption(i)); } else { SetXmlValue(i, GetOptionVal(i)); } } return settings; }
wxString COptions::GetOption(unsigned int nID) { if (nID >= OPTIONS_NUM) return _T(""); if (options[nID].type != string) return wxString::Format(_T("%d"), GetOptionVal(nID)); return m_optionsCache[nID].strValue; }
void COptions::Save() { if (GetOptionVal(OPTION_DEFAULT_KIOSKMODE) == 2) return; if (!m_pXmlFile) return; CInterProcessMutex mutex(MUTEX_OPTIONS); m_pXmlFile->Save(true); }
void COptions::SetServer(wxString path, const CServer& server) { if (!m_pXmlFile) return; if (path == _T("")) return; TiXmlElement *element = m_pXmlFile->GetElement(); while (path != _T("")) { wxString sub; int pos = path.Find('/'); if (pos != -1) { sub = path.Left(pos); path = path.Mid(pos + 1); } else { sub = path; path = _T(""); } char *utf8 = ConvUTF8(sub); if (!utf8) return; TiXmlElement *newElement = element->FirstChildElement(utf8); delete [] utf8; if (newElement) element = newElement; else { char *utf8 = ConvUTF8(sub); if (!utf8) return; TiXmlNode *node = element->LinkEndChild(new TiXmlElement(utf8)); delete [] utf8; if (!node || !node->ToElement()) return; element = node->ToElement(); } } ::SetServer(element, server); if (GetOptionVal(OPTION_DEFAULT_KIOSKMODE) == 2) return; CInterProcessMutex mutex(MUTEX_OPTIONS); m_pXmlFile->Save(); }
void COptions::SetServer(std::wstring path, const CServer& server) { if (!m_pXmlFile) { return; } if (path.empty()) { return; } auto element = m_pXmlFile->GetElement(); while (!path.empty()) { std::wstring sub; size_t pos = path.find('/'); if (pos != std::wstring::npos) { sub = path.substr(0, pos); path = path.substr(pos + 1); } else { sub = path; path.clear(); } std::string utf8 = fz::to_utf8(sub); auto newElement = element.child(utf8.c_str()); if (newElement) { element = newElement; } else { element = element.append_child(utf8.c_str()); } } ::SetServer(element, server); if (GetOptionVal(OPTION_DEFAULT_KIOSKMODE) == 2) { return; } CInterProcessMutex mutex(MUTEX_OPTIONS); m_pXmlFile->Save(true); }
void COptions::SetServer(wxString path, const CServer& server) { if (!m_pXmlFile) return; if (path.empty()) return; auto element = m_pXmlFile->GetElement(); while (!path.empty()) { wxString sub; int pos = path.Find('/'); if (pos != -1) { sub = path.Left(pos); path = path.Mid(pos + 1); } else { sub = path; path = _T(""); } wxScopedCharBuffer utf8 = sub.utf8_str(); if (!utf8) return; auto newElement = element.child(utf8); if (newElement) element = newElement; else { element = element.append_child(utf8); } } ::SetServer(element, server); if (GetOptionVal(OPTION_DEFAULT_KIOSKMODE) == 2) return; CInterProcessMutex mutex(MUTEX_OPTIONS); m_pXmlFile->Save(true); }
wxString COptions::GetOption(unsigned int nID) { if (nID >= OPTIONS_NUM) return _T(""); if (options[nID].type != string) return wxString::Format(_T("%d"), GetOptionVal(nID)); if (m_optionsCache[nID].cached) return m_optionsCache[nID].strValue; wxString value; if (options[nID].internal || !GetXmlValue(nID, value)) value = options[nID].defaultValue; else Validate(nID, value); m_optionsCache[nID].strValue = value; m_optionsCache[nID].cached = true; return value; }
BOOL COptions::GetAsCommand(char **pBuffer, DWORD *nBufferLength) { int i; DWORD len = 2; simple_lock lock(m_mutex); for (i=0; i<OPTIONS_NUM; ++i) { len+=1; if (!m_Options[i].nType) { len += 3; auto utf8 = ConvToNetwork(GetOption(i + 1)); if ((i + 1) != OPTION_ADMINPASS) len += utf8.size(); else { if (GetOption(i+1).GetLength() < 6 && utf8.size() > 0) len++; } } else len += 8; } len += 4; SPEEDLIMITSLIST::const_iterator iter; for (i = 0; i < 2; i++) for (iter = m_sSpeedLimits[i].begin(); iter != m_sSpeedLimits[i].end(); iter++) len += iter->GetRequiredBufferLen(); *pBuffer=new char[len]; char *p=*pBuffer; *p++ = OPTIONS_NUM / 256; *p++ = OPTIONS_NUM % 256; for (i=0; i<OPTIONS_NUM; ++i) { *p++ = m_Options[i].nType; switch(m_Options[i].nType) { case 0: { CStdString str = GetOption(i+1); if ((i+1)==OPTION_ADMINPASS) //Do NOT send admin password, //instead send empty string if admin pass is set //and send a single char if admin pass is invalid (len < 6) { if (str.GetLength() >= 6 || str == _T("")) str = _T(""); else str = _T("*"); } auto utf8 = ConvToNetwork(str); int len = utf8.size(); *p++ = (len / 256) / 256; *p++ = (len / 256) % 256; *p++ = len % 256; memcpy(p, utf8.c_str(), len); p += len; } break; case 1: { _int64 value = GetOptionVal(i+1); memcpy(p, &value, 8); p+=8; } break; default: ASSERT(FALSE); } } for (i = 0; i < 2; ++i) { *p++ = m_sSpeedLimits[i].size() >> 8; *p++ = m_sSpeedLimits[i].size() % 256; for (iter = m_sSpeedLimits[i].begin(); iter != m_sSpeedLimits[i].end(); iter++) p = iter->FillBuffer(p); } *nBufferLength = len; return TRUE; }
bool COptionsDlg::GetAsCommand(unsigned char **pBuffer, DWORD *nBufferLength) { DWORD len = 2; int i; for (i = 0; i < OPTIONS_NUM; ++i) { ++len; if (!m_Options[i].nType) { int strlen = ConvToNetwork(GetOption(i + 1)).size(); if (strlen > 0xFFFFFF) return false; len += strlen; len += 3; } else len += 8; } len += 4; //Number of rules if (m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.size() > 0xffff || m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.size() > 0xffff) { return false; } for (auto const& limit : m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits) { len += limit.GetRequiredBufferLen(); } for (auto const& limit : m_pOptionsSpeedLimitPage->m_UploadSpeedLimits) { len += limit.GetRequiredBufferLen(); } *pBuffer = new unsigned char[len]; unsigned char *p = *pBuffer; *p++ = OPTIONS_NUM / 256; *p++ = OPTIONS_NUM % 256; for (i = 0; i < OPTIONS_NUM; ++i) { *p++ = m_Options[i].nType; switch (m_Options[i].nType) { case 0: { auto utf8 = ConvToNetwork(GetOption(i + 1)); int slen = utf8.size(); *p++ = ((slen / 256) / 256) & 0xffu; *p++ = (slen / 256) & 0xffu; *p++ = slen % 256; memcpy(p, utf8.c_str(), slen); p += slen; } break; case 1: { _int64 value = GetOptionVal(i+1); memcpy(p, &value, 8); p+=8; } break; default: return false; } } *p++ = (m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.size() >> 8) & 0xffu; *p++ = m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits.size() % 256; for (auto const& limit : m_pOptionsSpeedLimitPage->m_DownloadSpeedLimits) { p = limit.FillBuffer(p); } *p++ = (m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.size() >> 8) & 0xffu; *p++ = m_pOptionsSpeedLimitPage->m_UploadSpeedLimits.size() % 256; for (auto const& limit : m_pOptionsSpeedLimitPage->m_UploadSpeedLimits) { p = limit.FillBuffer(p); } *nBufferLength = len; return true; }