void eSettings::GetPageSettings(size_t page_id, wxString& path, doc_id& di, int& pos, int& topline, wxString& syntax, vector<unsigned int>& folds, vector<unsigned int>& bookmarks) const { const wxJSONValue pages = m_jsonRoot.ItemAt(wxT("pages")); wxASSERT((int)page_id < pages.Size()); const wxJSONValue page = pages.ItemAt(page_id); path = page.ItemAt(wxT("path")).AsString(); pos = page.ItemAt(wxT("pos")).AsInt(); topline = page.ItemAt(wxT("topline")).AsInt(); syntax = page.ItemAt(wxT("syntax")).AsString(); // doc_id di.type = (doc_type)page.ItemAt(wxT("doc_type")).AsInt(); di.document_id = page.ItemAt(wxT("doc_doc")).AsInt(); di.version_id = page.ItemAt(wxT("doc_version")).AsInt(); // Set folds const wxJSONValue foldsArray = page.ItemAt(wxT("folds")); for (int f = 0; f < foldsArray.Size(); ++f) { folds.push_back(foldsArray.ItemAt(f).AsInt()); } // Set bookmarks const wxJSONValue bookmarksArray = page.ItemAt(wxT("bookmarks")); for (int b = 0; b < bookmarksArray.Size(); ++b) { bookmarks.push_back(bookmarksArray.ItemAt(b).AsInt()); } }
wxString eSettings::GetPagePath(size_t page_id) const { const wxJSONValue pages = m_jsonRoot.ItemAt(wxT("pages")); wxASSERT((int)page_id < pages.Size()); const wxJSONValue page = pages.ItemAt(page_id); return page.ItemAt(wxT("path")).AsString(); }
wxString eSettings::GetRemoteProfileName(size_t profile_id) const { const wxJSONValue remotes = m_jsonRoot.ItemAt(wxT("remoteProfiles")); wxASSERT((int)profile_id < remotes.Size()); const wxJSONValue profile = remotes.ItemAt(profile_id); return profile.ItemAt(wxT("name")).AsString(); }
void eSettings::GetSearch(size_t ndx, wxString& pattern, bool& isRegex, bool& matchCase) const { const wxJSONValue searches = m_jsonRoot.ItemAt(wxT("searchHistory")); wxASSERT((int)ndx < searches.Size()); const wxJSONValue item = searches.ItemAt(ndx); pattern = item.ItemAt(wxT("pattern")).AsString(); isRegex = item.ItemAt(wxT("isRegex")).AsBool(); matchCase = item.ItemAt(wxT("matchCase")).AsBool(); }
doc_id eSettings::GetPageDoc(size_t page_id) const { const wxJSONValue pages = m_jsonRoot.ItemAt(wxT("pages")); wxASSERT((int)page_id < pages.Size()); const wxJSONValue page = pages.ItemAt(page_id); doc_id di; di.type = (doc_type)page.ItemAt(wxT("doc_type")).AsInt(); di.document_id = page.ItemAt(wxT("doc_doc")).AsInt(); di.version_id = page.ItemAt(wxT("doc_version")).AsInt(); return di; }
void eSettings::AddToRecent(const wxString& key, wxJSONValue& jsonArray, size_t max) { const wxJSONValue value(key); // Check if value is already in array int ndx = -1; for (int i = 0; i < jsonArray.Size(); ++i) { if (value.IsSameAs(jsonArray.ItemAt(i))) { ndx = i; break; } } if (ndx == 0) return; // No need to do anything if path is already top if (ndx > 0) jsonArray.Remove(ndx); // remove duplicate entry // Insert value at top jsonArray.Insert(0, value); // Make sure we have no more than max entries if (jsonArray.Size() > (int)max) jsonArray.Remove(max); }
static bool ODVersionNewerThan(int major, int minor, int patch) { wxJSONValue jMsg; wxJSONWriter writer; wxString MsgString; jMsg[wxS("Source")] = wxS("WEATHER_ROUTING_PI"); jMsg[wxT("Type")] = wxT("Request"); jMsg[wxT("Msg")] = wxS("Version"); jMsg[wxT("MsgId")] = wxS("version"); writer.Write( jMsg, MsgString ); SendPluginMessage( wxS("OCPN_DRAW_PI"), MsgString ); if(!g_ReceivedODVersionJSONMsg.Size()) return false; if(g_ReceivedODVersionJSONMsg[wxS("Major")].AsInt() > major) return true; if(g_ReceivedODVersionJSONMsg[wxS("Major")].AsInt() == major && g_ReceivedODVersionJSONMsg[wxS("Minor")].AsInt() > minor) return true; if(g_ReceivedODVersionJSONMsg[wxS("Major")].AsInt() == major && g_ReceivedODVersionJSONMsg[wxS("Minor")].AsInt() == minor && g_ReceivedODVersionJSONMsg[wxS("Patch")].AsInt() >= patch) return true; return false; }
RemoteProfile* eSettings::DoGetRemoteProfile(size_t profile_id) { // Check if profile is already in cache for (auto_vector<RemoteProfile>::iterator p = m_tempRemotes.begin(); p != m_tempRemotes.end(); ++p) { if ((*p)->m_id == (int)profile_id) return *p; } // Get the profile const wxJSONValue remotes = m_jsonRoot.ItemAt(wxT("remoteProfiles")); wxASSERT((int)profile_id < remotes.Size()); const wxJSONValue profile = remotes.ItemAt(profile_id); // Add profile to cache auto_ptr<RemoteProfile> rp(new RemoteProfile()); rp->m_id = profile_id; rp->m_protocol = profile.ItemAt(wxT("protocol")).AsString(); rp->m_name = profile.ItemAt(wxT("name")).AsString(); rp->m_address = profile.ItemAt(wxT("address")).AsString(); rp->m_dir = profile.ItemAt(wxT("dir")).AsString(); rp->m_username = profile.ItemAt(wxT("username")).AsString(); rp->m_pwd = profile.ItemAt(wxT("pwd")).AsString(); m_tempRemotes.push_back(rp); return m_tempRemotes.back(); }
/*! This is a recursive function that gets the type of the \c value object and calls several protected functions depending on the type: \li \c WriteNullvalue for type NULL \li \c WriteStringValue() for STRING and CSTRING types \li \c WriteIntValue for INT types \li \c WriteUIntValue for UINT types \li \c WriteBoolValue for BOOL types \li \c WriteDoubleValue for DOUBLE types \li \c WriteMemoryBuff for MEMORYBUFF types If the value is an array or key/value map (types ARRAY and OBJECT), the function iterates through all JSON value object in the array/map and calls itself for every item in the container. */ int wxJSONWriter::DoWrite( wxOutputStream& os, const wxJSONValue& value, const wxString* key, bool comma ) { // note that this function is recursive // some variables that cannot be allocated in the switch statement const wxJSONInternalMap* map = 0; int size; m_colNo = 1; m_lineNo = 1; // determine the comment position; it is one of: // // wxJSONVALUE_COMMENT_BEFORE // wxJSONVALUE_COMMENT_AFTER // wxJSONVALUE_COMMENT_INLINE // // or -1 if comments have not to be written int commentPos = -1; if ( value.GetCommentCount() > 0 && (m_style & wxJSONWRITER_WRITE_COMMENTS)) { commentPos = value.GetCommentPos(); if ( ( m_style & wxJSONWRITER_COMMENTS_BEFORE) != 0 ) { commentPos = wxJSONVALUE_COMMENT_BEFORE; } else if ( (m_style & wxJSONWRITER_COMMENTS_AFTER) != 0 ) { commentPos = wxJSONVALUE_COMMENT_AFTER; } } int lastChar = 0; // check if WriteComment() writes the last LF char // first write the comment if it is BEFORE if ( commentPos == wxJSONVALUE_COMMENT_BEFORE ) { lastChar = WriteComment( os, value, true ); if ( lastChar < 0 ) { return lastChar; } else if ( lastChar != '\n' ) { WriteSeparator( os ); } } lastChar = WriteIndent( os ); if ( lastChar < 0 ) { return lastChar; } // now write the key if it is not NULL if ( key ) { lastChar = WriteKey( os, *key ); } if ( lastChar < 0 ) { return lastChar; } // now write the value wxJSONInternalMap::const_iterator it; // declare the map object long int count = 0; wxJSONType t = value.GetType(); switch ( t ) { case wxJSONTYPE_INVALID : WriteInvalid( os ); wxFAIL_MSG( _T("wxJSONWriter::WriteEmpty() cannot be called (not a valid JSON text")); break; case wxJSONTYPE_INT : case wxJSONTYPE_SHORT : case wxJSONTYPE_LONG : case wxJSONTYPE_INT64 : lastChar = WriteIntValue( os, value ); break; case wxJSONTYPE_UINT : case wxJSONTYPE_USHORT : case wxJSONTYPE_ULONG : case wxJSONTYPE_UINT64 : lastChar = WriteUIntValue( os, value ); break; case wxJSONTYPE_NULL : lastChar = WriteNullValue( os ); break; case wxJSONTYPE_BOOL : lastChar = WriteBoolValue( os, value ); break; case wxJSONTYPE_DOUBLE : lastChar = WriteDoubleValue( os, value ); break; case wxJSONTYPE_STRING : case wxJSONTYPE_CSTRING : lastChar = WriteStringValue( os, value.AsString()); break; case wxJSONTYPE_MEMORYBUFF : lastChar = WriteMemoryBuff( os, value.AsMemoryBuff()); break; case wxJSONTYPE_ARRAY : ++m_level; os.PutC( '[' ); // the inline comment for objects and arrays are printed in the open char if ( commentPos == wxJSONVALUE_COMMENT_INLINE ) { commentPos = -1; // we have already written the comment lastChar = WriteComment( os, value, false ); if ( lastChar < 0 ) { return lastChar; } if ( lastChar != '\n' ) { lastChar = WriteSeparator( os ); } } else { // comment is not to be printed inline, so write a LF lastChar = WriteSeparator( os ); if ( lastChar < 0 ) { return lastChar; } } // now iterate through all sub-items and call DoWrite() recursively size = value.Size(); for ( int i = 0; i < size; i++ ) { bool comma = false; if ( i < size - 1 ) { comma = true; } wxJSONValue v = value.ItemAt( i ); lastChar = DoWrite( os, v, 0, comma ); if ( lastChar < 0 ) { return lastChar; } } --m_level; lastChar = WriteIndent( os ); if ( lastChar < 0 ) { return lastChar; } os.PutC( ']' ); break; case wxJSONTYPE_OBJECT : ++m_level; os.PutC( '{' ); // the inline comment for objects and arrays are printed in the open char if ( commentPos == wxJSONVALUE_COMMENT_INLINE ) { commentPos = -1; // we have already written the comment lastChar = WriteComment( os, value, false ); if ( lastChar < 0 ) { return lastChar; } if ( lastChar != '\n' ) { WriteSeparator( os ); } } else { lastChar = WriteSeparator( os ); } map = value.AsMap(); size = value.Size(); count = 0; for ( it = map->begin(); it != map->end(); ++it ) { // get the key and the value wxString key = it->first; const wxJSONValue& v = it->second; bool comma = false; if ( count < size - 1 ) { comma = true; } lastChar = DoWrite( os, v, &key, comma ); if ( lastChar < 0 ) { return lastChar; } count++; } --m_level; lastChar = WriteIndent( os ); if ( lastChar < 0 ) { return lastChar; } os.PutC( '}' ); break; default : // a not yet defined wxJSONType: we FAIL wxFAIL_MSG( _T("wxJSONWriter::DoWrite() undefined wxJSONType type")); break; } // writes the comma character before the inline comment if ( comma ) { os.PutC( ',' ); } if ( commentPos == wxJSONVALUE_COMMENT_INLINE ) { lastChar = WriteComment( os, value, false ); if ( lastChar < 0 ) { return lastChar; } } else if ( commentPos == wxJSONVALUE_COMMENT_AFTER ) { WriteSeparator( os ); lastChar = WriteComment( os, value, true ); if ( lastChar < 0 ) { return lastChar; } } if ( lastChar != '\n' ) { lastChar = WriteSeparator( os ); } return lastChar; }
wxString eSettings::GetFilterCommand(size_t ndx) const { const wxJSONValue values = m_jsonRoot.ItemAt(wxT("filterCommandHistory")); wxASSERT((int)ndx < values.Size()); return values.ItemAt(ndx).AsString(); }
size_t eSettings::GetFilterCommandHistoryCount() const { if (!m_jsonRoot.HasMember(wxT("filterCommandHistory"))) return 0; const wxJSONValue values = m_jsonRoot.ItemAt(wxT("filterCommandHistory")); return values.Size(); }
wxString eSettings::GetReplace(size_t ndx) const { const wxJSONValue replacements = m_jsonRoot.ItemAt(wxT("replaceHistory")); wxASSERT((int)ndx < replacements.Size()); return replacements.ItemAt(ndx).AsString(); }
size_t eSettings::GetReplaceCount() const { if (!m_jsonRoot.HasMember(wxT("replaceHistory"))) return 0; const wxJSONValue replacements = m_jsonRoot.ItemAt(wxT("replaceHistory")); return replacements.Size(); }
void eSettings::GetRecents(const wxJSONValue& jarray, wxArrayString& recents) const { for (int i = 0; i < jarray.Size(); ++i) { recents.Add(jarray.ItemAt(i).AsString()); } }
size_t eSettings::GetSearchCount() const { if (!m_jsonRoot.HasMember(wxT("searchHistory"))) return 0; const wxJSONValue searches = m_jsonRoot.ItemAt(wxT("searchHistory")); return searches.Size(); }
const RemoteProfile* eSettings::GetRemoteProfileFromUrl(const wxString& url, bool withDir) { // Split url up in elements wxRegEx reUrl(wxT("([[:alpha:]]+)://(([^:@]*):([^:@]*)@)?([^/]+)(.*)")); if (!reUrl.Matches(url)) return NULL; // invalid url const wxString protocol = reUrl.GetMatch(url, 1); const wxString username = reUrl.GetMatch(url, 3); const wxString pwd = reUrl.GetMatch(url, 4); const wxString address = reUrl.GetMatch(url, 5); const wxString dir = reUrl.GetMatch(url, 6); const wxString sDir = StripSlashes(dir); // See if we can find a matching profile in cache for (auto_vector<RemoteProfile>::iterator p = m_tempRemotes.begin(); p != m_tempRemotes.end(); ++p) { RemoteProfile* rp = (*p); if (rp->IsActive() && rp->m_address == address && rp->m_protocol == protocol) { if (!username.empty() && rp->m_username != username) continue; if (withDir && StripSlashes(rp->m_dir) != sDir) continue; if (!pwd.empty() && rp->m_pwd != pwd) { rp->m_pwd = pwd; // Password may have changed if (!rp->IsTemp()) SaveRemoteProfile(rp); } return rp; } } // See if we can find a matching profile in settings const wxJSONValue remotes = m_jsonRoot.ItemAt(wxT("remoteProfiles")); const int profile_count = remotes.Size(); for (int i = 0; i < profile_count; ++i) { const wxJSONValue profile = remotes.ItemAt(i); if (profile.ItemAt(wxT("address")).AsString() != address) continue; // Ftp is default protocol wxString prot = profile.ItemAt(wxT("protocol")).AsString(); if (prot.empty()) prot = wxT("ftp"); if (prot != protocol) continue; // ftp is default if (!username.empty() && profile.ItemAt(wxT("username")).AsString() != username) continue; RemoteProfile* rp = DoGetRemoteProfile(i); if (withDir && StripSlashes(rp->m_dir) != sDir) continue; // Password may have changed if (!pwd.empty() && rp->m_pwd != pwd) { rp->m_pwd = pwd; SaveRemoteProfile(rp); } return rp; } // Add new temp profile auto_ptr<RemoteProfile> newRp(new RemoteProfile()); newRp->m_protocol = protocol; newRp->m_name = address; newRp->m_address = address; newRp->m_dir = dir; newRp->m_username = username; newRp->m_pwd = pwd; m_tempRemotes.push_back(newRp); return m_tempRemotes.back(); }
size_t eSettings::GetPageCount() const { if (!m_jsonRoot.HasMember(wxT("pages"))) return 0; const wxJSONValue pages = m_jsonRoot.ItemAt(wxT("pages")); return pages.Size(); }
size_t eSettings::GetRemoteProfileCount() const { if (!m_jsonRoot.HasMember(wxT("remoteProfiles"))) return 0; const wxJSONValue remotes = m_jsonRoot.ItemAt(wxT("remoteProfiles")); return remotes.Size(); }