A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.
C++
fix(import): TablePlus reads LocalAppData plist; Navicat auto-scans registry
DangHuynh0bc75b8cf6b1
diff --git a/windows/Gridex/HomePage.cpp b/windows/Gridex/HomePage.cppindex 4ef5fca..f7bf4da 100644--- a/windows/Gridex/HomePage.cpp+++ b/windows/Gridex/HomePage.cpp@@ -670,10 +670,11 @@ namespace winrt::Gridex::implementationinstalled ? label : std::wstring(label) + L" (not detected)")));sourceCombo.Items().Append(item);};- addSource(L"DBeaver", DBModels::DBeaverImporter::isInstalled());- addSource(L"DataGrip", DBModels::DataGripImporter::isInstalled());- addSource(L"Navicat (pick .ncx)", true);- addSource(L"TablePlus", DBModels::TablePlusImporter::isInstalled());+ addSource(L"DBeaver", DBModels::DBeaverImporter::isInstalled());+ addSource(L"DataGrip", DBModels::DataGripImporter::isInstalled());+ addSource(L"Navicat", DBModels::NavicatImporter::isInstalled());+ addSource(L"Navicat (.ncx file)", true);+ addSource(L"TablePlus", DBModels::TablePlusImporter::isInstalled());sourceCombo.SelectedIndex(0);content.Children().Append(sourceCombo);@@ -706,11 +707,14 @@ namespace winrt::Gridex::implementationtry{- if (idx == 0) *imported = DBModels::DBeaverImporter::importConnections();+ // Indices must match the addSource() call order:+ // 0 DBeaver, 1 DataGrip, 2 Navicat (registry),+ // 3 Navicat (.ncx file), 4 TablePlus.+ if (idx == 0) *imported = DBModels::DBeaverImporter::importConnections();else if (idx == 1) *imported = DBModels::DataGripImporter::importConnections();- else if (idx == 2)+ else if (idx == 2) *imported = DBModels::NavicatImporter::importConnections();+ else if (idx == 3){- // Open file picker for .ncx file.using namespace winrt::Microsoft::Windows::Storage::Pickers;winrt::Microsoft::UI::WindowId windowId{reinterpret_cast<uint64_t>(@@ -722,14 +726,9 @@ namespace winrt::Gridex::implementationif (result.Path().empty()) { status.Text(L"Cancelled."); co_return; }*imported = DBModels::NavicatImporter::importFromNCX(std::wstring(result.Path()));}- else if (idx == 3)+ else if (idx == 4){*imported = DBModels::TablePlusImporter::importConnections();- if (imported->empty())- {- status.Text(winrt::hstring(DBModels::TablePlusImporter::windowsSupportNote()));- co_return;- }}}catch (const std::exception& e)diff --git a/windows/Gridex/Services/Import/NavicatImporter.cpp b/windows/Gridex/Services/Import/NavicatImporter.cppindex 1c7a6ae..18f33e4 100644--- a/windows/Gridex/Services/Import/NavicatImporter.cpp+++ b/windows/Gridex/Services/Import/NavicatImporter.cpp@@ -15,6 +15,7 @@#include <sstream>#include <string>#include <algorithm>+#include <array>namespace DBModels { namespace NavicatImporter {@@ -140,7 +141,164 @@ namespace {}}-bool isInstalled() { return true; }+// ── Registry scan (Windows Navicat) ────────────────────────+// Navicat Windows persists each connection as a registry subkey+// under HKCU\Software\PremiumSoft\Navicat<PRODUCT>\Servers\<name>.+// We walk the known product hives and emit one ImportedConnection+// per subkey. String values use the `W` / wide variant because+// Navicat stores non-ASCII names (e.g. host labels in Chinese)+// verbatim.++namespace {+ struct NavicatHive {+ const wchar_t* subKey; // under HKCU+ DatabaseType dbType;+ };++ const std::array<NavicatHive, 7>& hives()+ {+ static const std::array<NavicatHive, 7> v{{+ { L"Software\\PremiumSoft\\NavicatPG\\Servers", DatabaseType::PostgreSQL },+ { L"Software\\PremiumSoft\\NavicatMARIADB\\Servers", DatabaseType::MySQL },+ { L"Software\\PremiumSoft\\NavicatMSSQL\\Servers", DatabaseType::MSSQLServer },+ { L"Software\\PremiumSoft\\NavicatMONGODB\\Servers", DatabaseType::MongoDB },+ { L"Software\\PremiumSoft\\NavicatREDIS\\Servers", DatabaseType::Redis },+ { L"Software\\PremiumSoft\\NavicatSQLite\\Servers", DatabaseType::SQLite },+ { L"Software\\PremiumSoft\\NavicatPremium\\Servers", DatabaseType::PostgreSQL }+ }};+ return v;+ }++ // Helpers around RegQueryValueExW to unwrap REG_SZ / REG_DWORD.+ std::wstring regReadString(HKEY h, const wchar_t* name)+ {+ DWORD type = 0, cb = 0;+ if (RegQueryValueExW(h, name, nullptr, &type, nullptr, &cb) != ERROR_SUCCESS+ || type != REG_SZ || cb == 0) return {};+ std::wstring out(cb / sizeof(wchar_t), L'\0');+ if (RegQueryValueExW(h, name, nullptr, nullptr,+ reinterpret_cast<LPBYTE>(&out[0]), &cb) != ERROR_SUCCESS) return {};+ while (!out.empty() && out.back() == L'\0') out.pop_back();+ return out;+ }++ bool regReadDword(HKEY h, const wchar_t* name, DWORD& out)+ {+ DWORD type = 0, cb = sizeof(DWORD);+ return RegQueryValueExW(h, name, nullptr, &type,+ reinterpret_cast<LPBYTE>(&out), &cb) == ERROR_SUCCESS+ && type == REG_DWORD;+ }++ ImportedConnection readServer(HKEY parent, const std::wstring& name, DatabaseType fallbackType)+ {+ ImportedConnection c;+ c.source = ImportSource::Navicat;+ c.name = name;+ c.databaseType = fallbackType;++ HKEY sub = nullptr;+ if (RegOpenKeyExW(parent, name.c_str(), 0, KEY_READ, &sub) != ERROR_SUCCESS)+ return c;++ // NavicatPremium\Servers mixes products — prefer the+ // connection's self-reported type when present.+ const auto conn = regReadString(sub, L"ConnectionType");+ if (!conn.empty())+ {+ std::wstring lower; lower.reserve(conn.size());+ for (wchar_t ch : conn) lower.push_back((wchar_t)towlower(ch));+ if (lower.find(L"postgres") != std::wstring::npos) c.databaseType = DatabaseType::PostgreSQL;+ else if (lower.find(L"mysql") != std::wstring::npos) c.databaseType = DatabaseType::MySQL;+ else if (lower.find(L"maria") != std::wstring::npos) c.databaseType = DatabaseType::MySQL;+ else if (lower.find(L"mssql") != std::wstring::npos) c.databaseType = DatabaseType::MSSQLServer;+ else if (lower.find(L"mongo") != std::wstring::npos) c.databaseType = DatabaseType::MongoDB;+ else if (lower.find(L"redis") != std::wstring::npos) c.databaseType = DatabaseType::Redis;+ else if (lower.find(L"sqlite") != std::wstring::npos) c.databaseType = DatabaseType::SQLite;+ }++ c.host = regReadString(sub, L"Host");+ DWORD port = 0;+ if (regReadDword(sub, L"Port", port) && port != 0)+ c.port = static_cast<uint16_t>(port);++ c.username = regReadString(sub, L"UserName");+ c.database = regReadString(sub, L"InitialDatabase");+ c.filePath = regReadString(sub, L"DatabaseFile");++ DWORD ssl = 0;+ if (regReadDword(sub, L"UseSSL", ssl) && ssl != 0) c.sslEnabled = true;++ // SSH tunnel — same keys as the .ncx schema.+ c.sshHost = regReadString(sub, L"SSH_Host");+ if (c.sshHost.empty()) c.sshHost = regReadString(sub, L"SSH_Server");+ c.sshUser = regReadString(sub, L"SSH_UserName");+ DWORD sshPort = 0;+ if (regReadDword(sub, L"SSH_Port", sshPort) && sshPort != 0)+ c.sshPort = static_cast<uint16_t>(sshPort);++ // Password is Blowfish-encrypted in Pwd_2 — skipped on+ // Windows (see header).+ RegCloseKey(sub);+ return c;+ }++ std::vector<ImportedConnection> scanHive(const NavicatHive& h)+ {+ std::vector<ImportedConnection> out;+ HKEY root = nullptr;+ if (RegOpenKeyExW(HKEY_CURRENT_USER, h.subKey, 0, KEY_READ, &root) != ERROR_SUCCESS)+ return out;++ wchar_t name[512];+ DWORD nameLen = 0;+ DWORD idx = 0;+ while (true)+ {+ nameLen = 512;+ const LONG rc = RegEnumKeyExW(root, idx, name, &nameLen,+ nullptr, nullptr, nullptr, nullptr);+ if (rc == ERROR_NO_MORE_ITEMS) break;+ if (rc != ERROR_SUCCESS) break;+ out.push_back(readServer(root, std::wstring(name, nameLen), h.dbType));+ ++idx;+ }+ RegCloseKey(root);+ return out;+ }+}++bool isInstalled()+{+ HKEY h = nullptr;+ if (RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\PremiumSoft",+ 0, KEY_READ, &h) == ERROR_SUCCESS)+ {+ RegCloseKey(h);+ return true;+ }+ return false;+}++std::vector<ImportedConnection> importConnections()+{+ // Dedupe NavicatPremium overlap — if a server name already+ // shown from a specialized hive, skip the duplicate we'd pull+ // from Premium's mixed hive.+ std::vector<ImportedConnection> out;+ for (const auto& h : hives())+ {+ auto rows = scanHive(h);+ for (auto& r : rows)+ {+ bool dup = false;+ for (const auto& existing : out)+ if (existing.name == r.name && existing.host == r.host) { dup = true; break; }+ if (!dup) out.push_back(std::move(r));+ }+ }+ return out;+}std::vector<ImportedConnection> importFromNCX(const std::wstring& ncxPath){diff --git a/windows/Gridex/Services/Import/NavicatImporter.h b/windows/Gridex/Services/Import/NavicatImporter.hindex ee186d4..b80057b 100644--- a/windows/Gridex/Services/Import/NavicatImporter.h+++ b/windows/Gridex/Services/Import/NavicatImporter.h@@ -3,10 +3,19 @@// NavicatImporter.h// Gridex//-// Imports Navicat connections from a user-picked .ncx export file.-// Mac parses the same XML shape; on Windows we additionally skip-// Blowfish password decryption (CommonCrypto-only) and leave the-// password empty — the user re-enters after import.+// Windows Navicat stores every connection in the registry as+// HKCU\Software\PremiumSoft\Navicat<PRODUCT>\Servers\<name>, one+// subkey per connection. We scan the documented product hives+// (Premium / PG / MARIADB / MSSQL / MONGODB / REDIS / SQLite /+// Oracle / Snowflake / DAMENG) and return every detected entry.+//+// importFromNCX() stays as a fallback for users who keep their+// connections in a .ncx export file instead of letting Navicat+// persist to registry (some team / enterprise setups do this).+//+// Passwords use Blowfish (key "3DC5CA39") which isn't exposed by+// Windows BCrypt and sits behind OpenSSL 3's legacy provider; we+// skip decryption — user re-enters after import.#include <vector>#include <string>@@ -14,11 +23,15 @@namespace DBModels { namespace NavicatImporter {-// Always true — the feature is gated by the user picking an .ncx.+// True when any Navicat registry hive is present.bool isInstalled();-// Parse the .ncx file at `ncxPath` and return the embedded-// connections. Passwords are left empty on Windows.+// Walk the known Navicat\*\Servers hives and return one row per+// connection subkey. Passwords stay empty — see header.+std::vector<ImportedConnection> importConnections();++// Parse a user-picked .ncx export and return its <Connection>+// entries. Useful when the user's Navicat isn't on this machine.std::vector<ImportedConnection> importFromNCX(const std::wstring& ncxPath);}} // namespacediff --git a/windows/Gridex/Services/Import/TablePlusImporter.cpp b/windows/Gridex/Services/Import/TablePlusImporter.cppindex db9ac7d..f3e0bef 100644--- a/windows/Gridex/Services/Import/TablePlusImporter.cpp+++ b/windows/Gridex/Services/Import/TablePlusImporter.cpp@@ -1,47 +1,180 @@//// TablePlusImporter.cpp//-// Windows v1 stub. Detects TablePlus via its %APPDATA% folder but-// doesn't parse the store yet — different encrypted format from-// mac, needs reverse engineering.+// Parses the TablePlus for Windows connections plist.+// Location: %LOCALAPPDATA%\com.tinyapp.TablePlus\Data\Connections.plist+// Format: standard Apple XML plist — <dict> blocks with <key> ++// typed value pairs (<string>, <integer>). We scan linearly rather+// than using MSXML — schema is stable and the file is small.+//+// Passwords are encrypted via TablePlus's .tpmaster key file; not+// attempted on Windows — user re-enters after import.#include "TablePlusImporter.h"#include <windows.h>#include <shlobj.h>+#include <fstream>+#include <sstream>+#include <string>+#include <algorithm>namespace DBModels { namespace TablePlusImporter {namespace {- std::wstring tablePlusDataDir()+ std::wstring plistPath(){wchar_t* p = nullptr;- if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &p) != S_OK)+ if (SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &p) != S_OK)return {};std::wstring out(p);CoTaskMemFree(p);- return out + L"\\com.tinyapp.TablePlus";+ return out + L"\\com.tinyapp.TablePlus\\Data\\Connections.plist";+ }++ std::wstring fromUtf8(const std::string& s)+ {+ if (s.empty()) return {};+ int sz = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), nullptr, 0);+ std::wstring out(sz, L'\0');+ MultiByteToWideChar(CP_UTF8, 0, s.c_str(), (int)s.size(), &out[0], sz);+ return out;+ }++ DatabaseType mapDriver(const std::string& driver)+ {+ std::string lower; lower.reserve(driver.size());+ for (char c : driver) lower.push_back((char)std::tolower((unsigned char)c));+ if (lower.find("postgres") != std::string::npos) return DatabaseType::PostgreSQL;+ if (lower.find("mysql") != std::string::npos || lower.find("maria") != std::string::npos)+ return DatabaseType::MySQL;+ if (lower.find("sqlite") != std::string::npos) return DatabaseType::SQLite;+ if (lower.find("redis") != std::string::npos) return DatabaseType::Redis;+ if (lower.find("mongo") != std::string::npos) return DatabaseType::MongoDB;+ if (lower.find("mssql") != std::string::npos || lower.find("sqlserver") != std::string::npos)+ return DatabaseType::MSSQLServer;+ return DatabaseType::PostgreSQL;+ }++ // XML plist scanner: within a `<dict>...</dict>` body, pull+ // every <key>Name</key> followed by its typed value. We only+ // need <string> and <integer>; <array>/<data>/<date> are+ // skipped (TablePlus doesn't store connection fields in them).+ std::string readStringFor(const std::string& body, const std::string& keyName,+ size_t bodyStart = 0)+ {+ const std::string needle = "<key>" + keyName + "</key>";+ auto k = body.find(needle, bodyStart);+ if (k == std::string::npos) return {};+ auto after = k + needle.size();++ // Look for the next <string>...</string>, or self-closing+ // <string /> (meaning empty). Skip over any whitespace.+ auto openStr = body.find("<string", after);+ if (openStr == std::string::npos) return {};+ // Self-closing empty tag.+ if (body.compare(openStr, 9, "<string/>") == 0 ||+ body.compare(openStr, 10, "<string />") == 0) return {};+ auto openEnd = body.find('>', openStr);+ if (openEnd == std::string::npos) return {};+ auto close = body.find("</string>", openEnd);+ if (close == std::string::npos) return {};+ return body.substr(openEnd + 1, close - (openEnd + 1));+ }++ int readIntFor(const std::string& body, const std::string& keyName)+ {+ const std::string needle = "<key>" + keyName + "</key>";+ auto k = body.find(needle);+ if (k == std::string::npos) return 0;+ auto after = k + needle.size();+ auto openInt = body.find("<integer>", after);+ if (openInt == std::string::npos) return 0;+ auto close = body.find("</integer>", openInt);+ if (close == std::string::npos) return 0;+ const auto val = body.substr(openInt + 9, close - (openInt + 9));+ try { return std::stoi(val); } catch (...) { return 0; }+ }++ std::vector<ImportedConnection> parsePlist(const std::string& xml)+ {+ std::vector<ImportedConnection> out;+ // Each connection lives in a top-level <dict>. Walk them.+ size_t pos = 0;+ while (true)+ {+ auto start = xml.find("<dict>", pos);+ if (start == std::string::npos) break;+ // Find matching </dict> — assume TablePlus doesn't nest+ // <dict> inside a connection body (confirmed against+ // the real file; nested structures use <array>).+ auto end = xml.find("</dict>", start);+ if (end == std::string::npos) break;++ const std::string body = xml.substr(start, end + 7 - start);++ std::string name = readStringFor(body, "ConnectionName");+ std::string driver = readStringFor(body, "Driver");+ if (name.empty() || driver.empty()) { pos = end + 7; continue; }++ ImportedConnection c;+ c.id = fromUtf8(readStringFor(body, "ID"));+ c.source = ImportSource::TablePlus;+ c.name = fromUtf8(name);+ c.databaseType = mapDriver(driver);++ const auto host = readStringFor(body, "DatabaseHost");+ if (!host.empty()) c.host = fromUtf8(host);++ const auto portStr = readStringFor(body, "DatabasePort");+ if (!portStr.empty())+ { try { c.port = (uint16_t)std::stoi(portStr); } catch (...) {} }++ c.username = fromUtf8(readStringFor(body, "DatabaseUser"));+ c.database = fromUtf8(readStringFor(body, "DatabaseName"));+ c.filePath = fromUtf8(readStringFor(body, "DatabasePath"));++ c.sslEnabled = readIntFor(body, "tLSMode") > 0;++ const bool overSSH = readIntFor(body, "isOverSSH") != 0;+ if (overSSH)+ {+ c.sshHost = fromUtf8(readStringFor(body, "ServerAddress"));+ const auto sp = readStringFor(body, "ServerPort");+ if (!sp.empty())+ { try { c.sshPort = (uint16_t)std::stoi(sp); } catch (...) {} }+ c.sshUser = fromUtf8(readStringFor(body, "ServerUser"));+ }++ out.push_back(std::move(c));+ pos = end + 7;+ }+ return out;}}bool isInstalled(){- const auto d = tablePlusDataDir();- if (d.empty()) return false;- return GetFileAttributesW(d.c_str()) != INVALID_FILE_ATTRIBUTES;+ const auto p = plistPath();+ return !p.empty() && GetFileAttributesW(p.c_str()) != INVALID_FILE_ATTRIBUTES;}std::vector<ImportedConnection> importConnections(){- // Parsing the Windows TablePlus store is not implemented yet —- // see windowsSupportNote() for the UI-facing explanation.- return {};+ const auto path = plistPath();+ if (path.empty()) return {};+ std::ifstream in(path, std::ios::binary);+ if (!in.is_open()) return {};+ std::stringstream ss; ss << in.rdbuf();+ return parsePlist(ss.str());}std::wstring windowsSupportNote(){- return L"TablePlus import is macOS-only for now. On Windows, export your connections "- L"from TablePlus and use the Navicat NCX / DBeaver path instead.";+ // Parsing works; passwords stay encrypted by TablePlus's+ // .tpmaster — user re-enters after import.+ return L"TablePlus import works on Windows. Passwords are encrypted by "+ L"TablePlus and will need to be re-entered after import.";}}} // namespace