| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : db_explorer_settings.cpp |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
|
| 26 |
#include "db_explorer_settings.h" |
| 27 |
|
| 28 |
// ----------------------------------------------------------------- |
| 29 |
// DbExplorerSettings |
| 30 |
// ----------------------------------------------------------------- |
| 31 |
|
| 32 |
DbExplorerSettings::DbExplorerSettings() |
| 33 |
: clConfigItem(DBE_CONFIG) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
DbExplorerSettings::~DbExplorerSettings() |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
void DbExplorerSettings::SetRecentFiles(const wxArrayString& recentFiles) |
| 42 |
{ |
| 43 |
m_recentFiles.Clear(); |
| 44 |
for(size_t i=0; i<recentFiles.Count(); i++) { |
| 45 |
if(m_recentFiles.Index(recentFiles.Item(i)) == wxNOT_FOUND) { |
| 46 |
m_recentFiles.Add(recentFiles.Item(i)); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
DbConnectionInfoVec DbExplorerSettings::GetMySQLConnections() |
| 52 |
{ |
| 53 |
DbConnectionInfoVec conns; |
| 54 |
for(size_t i=0; i<m_connections.size(); i++) { |
| 55 |
if(m_connections.at(i).GetConnectionType() == DbConnectionInfo::DbConnTypeMySQL) { |
| 56 |
conns.push_back(m_connections.at(i)); |
| 57 |
} |
| 58 |
} |
| 59 |
return conns; |
| 60 |
} |
| 61 |
|
| 62 |
DbConnectionInfoVec DbExplorerSettings::GetPgSQLConnections() |
| 63 |
{ |
| 64 |
DbConnectionInfoVec conns; |
| 65 |
for(size_t i=0; i<m_connections.size(); i++) { |
| 66 |
if(m_connections.at(i).GetConnectionType() == DbConnectionInfo::DbConnTypePgSQL) { |
| 67 |
conns.push_back(m_connections.at(i)); |
| 68 |
} |
| 69 |
} |
| 70 |
return conns; |
| 71 |
} |
| 72 |
|
| 73 |
void DbExplorerSettings::SetMySQLConnections(const DbConnectionInfoVec& conns) |
| 74 |
{ |
| 75 |
DbConnectionInfoVec pgconns = GetPgSQLConnections(); |
| 76 |
m_connections.clear(); |
| 77 |
|
| 78 |
m_connections.insert(m_connections.end(), pgconns.begin(), pgconns.end()); |
| 79 |
m_connections.insert(m_connections.end(), conns.begin(), conns.end()); |
| 80 |
} |
| 81 |
|
| 82 |
void DbExplorerSettings::SetPgSQLConnections(const DbConnectionInfoVec& conns) |
| 83 |
{ |
| 84 |
DbConnectionInfoVec myconns = GetMySQLConnections(); |
| 85 |
m_connections.clear(); |
| 86 |
|
| 87 |
m_connections.insert(m_connections.end(), myconns.begin(), myconns.end()); |
| 88 |
m_connections.insert(m_connections.end(), conns.begin(), conns.end()); |
| 89 |
} |
| 90 |
|
| 91 |
void DbExplorerSettings::FromJSON(const JSONItem& json) |
| 92 |
{ |
| 93 |
m_recentFiles = json.namedObject("m_recentFiles").toArrayString(); |
| 94 |
m_sqlHistory = json.namedObject("m_sqlHistory").toArrayString(); |
| 95 |
|
| 96 |
// read the connections |
| 97 |
JSONItem arrConnections = json.namedObject("connections"); |
| 98 |
for(int i=0; i<arrConnections.arraySize(); ++i) { |
| 99 |
DbConnectionInfo ci; |
| 100 |
ci.FromJSON( arrConnections.arrayItem(i) ); |
| 101 |
m_connections.push_back( ci ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
JSONItem DbExplorerSettings::ToJSON() const |
| 106 |
{ |
| 107 |
JSONItem element = JSONItem::createObject(GetName()); |
| 108 |
element.addProperty("m_recentFiles", m_recentFiles); |
| 109 |
element.addProperty("m_sqlHistory", m_sqlHistory); |
| 110 |
|
| 111 |
// add the connections array |
| 112 |
JSONItem arrConnections = JSONItem::createArray("connections"); |
| 113 |
element.append(arrConnections); |
| 114 |
DbConnectionInfoVec::const_iterator iter = m_connections.begin(); |
| 115 |
for(; iter != m_connections.end(); ++iter) { |
| 116 |
arrConnections.arrayAppend( iter->ToJSON() ); |
| 117 |
} |
| 118 |
return element; |
| 119 |
} |
| 120 |
|
| 121 |
//--------------------------------------------------- |
| 122 |
// Database Connection info |
| 123 |
//--------------------------------------------------- |
| 124 |
|
| 125 |
DbConnectionInfo::DbConnectionInfo() |
| 126 |
: clConfigItem("connection-info") |
| 127 |
, m_connectionType(DbConnTypeMySQL) |
| 128 |
{ |
| 129 |
} |
| 130 |
|
| 131 |
DbConnectionInfo::~DbConnectionInfo() |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
JSONItem DbConnectionInfo::ToJSON() const |
| 136 |
{ |
| 137 |
JSONItem element = JSONItem::createObject(GetName()); |
| 138 |
element.addProperty("m_connectionName", m_connectionName ); |
| 139 |
element.addProperty("m_connectionType", m_connectionType ); |
| 140 |
element.addProperty("m_defaultDatabase", m_defaultDatabase); |
| 141 |
element.addProperty("m_password" , m_password ); |
| 142 |
element.addProperty("m_server" , m_server ); |
| 143 |
element.addProperty("m_port" , m_port ); |
| 144 |
element.addProperty("m_username" , m_username ); |
| 145 |
return element; |
| 146 |
} |
| 147 |
|
| 148 |
void DbConnectionInfo::FromJSON(const JSONItem& json) |
| 149 |
{ |
| 150 |
m_connectionName = json.namedObject("m_connectionName").toString(m_connectionName); |
| 151 |
m_connectionType = json.namedObject("m_connectionType").toInt(m_connectionType); |
| 152 |
m_defaultDatabase = json.namedObject("m_defaultDatabase").toString(m_defaultDatabase); |
| 153 |
m_password = json.namedObject("m_password").toString(m_password); |
| 154 |
m_server = json.namedObject("m_server").toString(m_server); |
| 155 |
m_port = json.namedObject("m_port").toInt(m_port); |
| 156 |
m_username = json.namedObject("m_username").toString(m_username); |
| 157 |
} |