Files
orbithub/src/profiles_window.cpp
T
ksmithandClaude Sonnet 5 dbcc20d155 Add File -> Import from mRemoteNG...
Parses mRemoteNG's confCons.xml export format directly -- schema verified
against mRemoteNG's own XmlConnectionsDeserializer.cs source and a real
exported sample, not guessed at. Maps nested <Node Type="Container">
folders and <Node Type="Connection"> entries onto OrbitHub profiles:
RDP stays RDP, SSH1/SSH2 collapse to OrbitHub's single SSH protocol,
anything else (VNC, Telnet, HTTP, PowerShell, ...) is skipped and listed
in the import summary rather than silently dropped.

Passwords are never read, not even for the common case where they're
technically readable without a master password (mRemoteNG only encrypts
the Password attribute itself, everything else -- Hostname, Username,
Domain, Protocol -- is plaintext). A FullFileEncryption="true" export
encrypts the whole node tree instead and genuinely can't be read without
the user's master password; that case is detected and refused with a
clear message rather than failing confusingly.

The parser (src/mremoteng_importer.h/.cpp) is a pure function decoupled
from any file/UI I/O, matching this session's established pattern of
keeping business logic separately testable from the Qt Widgets shell that
calls it (ProfilesWindow::importFromMRemoteNG() is the thin wrapper:
QFileDialog, call the parser, write results via ProfileRepository, show a
summary). 11 test cases against realistic sample XML.

Hit a real moc gotcha along the way: a literal "//" inside a raw string
literal (the xmlns URL) makes moc's lexer think a line comment started
there, silently desyncing its parse so it never finds the QObject-derived
test class at all (no error, just a missing vtable at link time). Fixed
by moving the XML fixtures into a plain non-QObject header moc never
scans, split across two adjacent literals as a second safeguard.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-15 16:08:02 -06:00

1256 lines
44 KiB
C++

#include "profiles_window.h"
#include "mremoteng_importer.h"
#include "profile_dialog.h"
#include "profile_repository.h"
#include "profiles_tree_widget.h"
#include <QAction>
#include <QAbstractItemView>
#include <QComboBox>
#include <QFile>
#include <QFileDialog>
#include <QHeaderView>
#include <QHBoxLayout>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QLabel>
#include <QLineEdit>
#include <QInputDialog>
#include <QMenu>
#include <QMessageBox>
#include <QPushButton>
#include <QSet>
#include <QSettings>
#include <QSignalBlocker>
#include <QStringList>
#include <QStyle>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QVariant>
#include <QVBoxLayout>
#include <QWidget>
namespace {
constexpr int kProfileIdRole = Qt::UserRole;
constexpr int kFolderPathRole = Qt::UserRole + 1;
QString normalizeFolderPathForView(const QString& value)
{
QString path = value.trimmed();
path.replace(QChar::fromLatin1('\\'), QChar::fromLatin1('/'));
const QStringList rawParts = path.split(QChar::fromLatin1('/'), Qt::SkipEmptyParts);
QStringList normalized;
for (const QString& rawPart : rawParts) {
const QString part = rawPart.trimmed();
if (!part.isEmpty()) {
normalized.push_back(part);
}
}
return normalized.join(QStringLiteral("/"));
}
QString joinFolderPath(const QString& parentFolder, const QString& childName)
{
const QString parent = normalizeFolderPathForView(parentFolder);
const QString child = normalizeFolderPathForView(childName);
if (child.isEmpty()) {
return parent;
}
if (parent.isEmpty()) {
return child;
}
return QStringLiteral("%1/%2").arg(parent, child);
}
QStringList splitFolderPath(const QString& folderPath)
{
const QString normalized = normalizeFolderPathForView(folderPath);
if (normalized.isEmpty()) {
return {};
}
return normalized.split(QChar::fromLatin1('/'), Qt::SkipEmptyParts);
}
bool profileHasTag(const Profile& profile, const QString& requestedTag)
{
const QString needle = requestedTag.trimmed();
if (needle.isEmpty()) {
return true;
}
const QStringList tags = profile.tags.split(QChar::fromLatin1(','), Qt::SkipEmptyParts);
for (const QString& tag : tags) {
if (tag.trimmed().compare(needle, Qt::CaseInsensitive) == 0) {
return true;
}
}
return false;
}
constexpr int kProfileExportFormatVersion = 1;
QJsonObject profileToJson(const Profile& profile)
{
QJsonObject object;
object.insert(QStringLiteral("name"), profile.name);
object.insert(QStringLiteral("host"), profile.host);
object.insert(QStringLiteral("port"), profile.port);
object.insert(QStringLiteral("username"), profile.username);
object.insert(QStringLiteral("domain"), profile.domain);
object.insert(QStringLiteral("folderPath"), profile.folderPath);
object.insert(QStringLiteral("protocol"), profile.protocol);
object.insert(QStringLiteral("authMode"), profile.authMode);
object.insert(QStringLiteral("privateKeyPath"), profile.privateKeyPath);
object.insert(QStringLiteral("knownHostsPolicy"), profile.knownHostsPolicy);
object.insert(QStringLiteral("rdpSecurityMode"), profile.rdpSecurityMode);
object.insert(QStringLiteral("rdpPerformanceProfile"), profile.rdpPerformanceProfile);
object.insert(QStringLiteral("tags"), profile.tags);
return object;
}
// Deliberately excludes id (import always creates new rows -- an imported
// profile's id has no meaning in the destination database) and any
// credential material (none is ever persisted on Profile in the first
// place, see ProfileRepository).
Profile profileFromJson(const QJsonObject& object)
{
Profile profile;
profile.name = object.value(QStringLiteral("name")).toString();
profile.host = object.value(QStringLiteral("host")).toString();
profile.port = object.value(QStringLiteral("port")).toInt(22);
profile.username = object.value(QStringLiteral("username")).toString();
profile.domain = object.value(QStringLiteral("domain")).toString();
profile.folderPath = object.value(QStringLiteral("folderPath")).toString();
profile.protocol = object.value(QStringLiteral("protocol")).toString(QStringLiteral("SSH"));
profile.authMode = object.value(QStringLiteral("authMode")).toString(QStringLiteral("Password"));
profile.privateKeyPath = object.value(QStringLiteral("privateKeyPath")).toString();
profile.knownHostsPolicy =
object.value(QStringLiteral("knownHostsPolicy")).toString(QStringLiteral("Ask"));
profile.rdpSecurityMode =
object.value(QStringLiteral("rdpSecurityMode")).toString(QStringLiteral("Negotiate"));
profile.rdpPerformanceProfile =
object.value(QStringLiteral("rdpPerformanceProfile")).toString(QStringLiteral("Balanced"));
profile.tags = object.value(QStringLiteral("tags")).toString();
return profile;
}
}
ProfilesWindow::ProfilesWindow(QWidget* parent)
: QWidget(parent),
m_searchBox(nullptr),
m_viewModeBox(nullptr),
m_sortBox(nullptr),
m_protocolFilterBox(nullptr),
m_tagFilterBox(nullptr),
m_profilesTree(nullptr),
m_newButton(nullptr),
m_editButton(nullptr),
m_deleteButton(nullptr),
m_repository(std::make_unique<ProfileRepository>())
{
setupUi();
if (!m_repository->initError().isEmpty()) {
QMessageBox::critical(this,
QStringLiteral("Database Error"),
QStringLiteral("Failed to initialize SQLite database: %1")
.arg(m_repository->initError()));
m_newButton->setEnabled(false);
m_editButton->setEnabled(false);
m_deleteButton->setEnabled(false);
m_searchBox->setEnabled(false);
m_profilesTree->setEnabled(false);
return;
}
loadUiPreferences();
loadProfiles();
}
ProfilesWindow::~ProfilesWindow() = default;
void ProfilesWindow::setupUi()
{
auto* rootLayout = new QVBoxLayout(this);
auto* searchLabel = new QLabel(QStringLiteral("Search"), this);
m_searchBox = new QLineEdit(this);
m_searchBox->setPlaceholderText(QStringLiteral("Filter by name, host, folder, or tags..."));
auto* viewModeLabel = new QLabel(QStringLiteral("View"), this);
m_viewModeBox = new QComboBox(this);
m_viewModeBox->addItem(QStringLiteral("List"));
m_viewModeBox->addItem(QStringLiteral("Folders"));
auto* sortLabel = new QLabel(QStringLiteral("Sort"), this);
m_sortBox = new QComboBox(this);
m_sortBox->addItem(QStringLiteral("Name"), static_cast<int>(ProfileSortOrder::NameAsc));
m_sortBox->addItem(QStringLiteral("Protocol"), static_cast<int>(ProfileSortOrder::ProtocolAsc));
m_sortBox->addItem(QStringLiteral("Host"), static_cast<int>(ProfileSortOrder::HostAsc));
auto* protocolFilterLabel = new QLabel(QStringLiteral("Protocol"), this);
m_protocolFilterBox = new QComboBox(this);
m_protocolFilterBox->addItem(QStringLiteral("All"));
m_protocolFilterBox->addItem(QStringLiteral("SSH"));
m_protocolFilterBox->addItem(QStringLiteral("RDP"));
m_protocolFilterBox->addItem(QStringLiteral("VNC"));
auto* tagFilterLabel = new QLabel(QStringLiteral("Tag"), this);
m_tagFilterBox = new QComboBox(this);
m_tagFilterBox->addItem(QStringLiteral("All"));
m_profilesTree = new ProfilesTreeWidget(this);
m_profilesTree->setSelectionMode(QAbstractItemView::SingleSelection);
m_profilesTree->setColumnCount(4);
m_profilesTree->setHeaderLabels(
{QStringLiteral("Name"), QStringLiteral("Protocol"), QStringLiteral("Server"), QStringLiteral("Tags")});
m_profilesTree->setRootIsDecorated(true);
m_profilesTree->setDragEnabled(true);
m_profilesTree->setAcceptDrops(true);
m_profilesTree->viewport()->setAcceptDrops(true);
m_profilesTree->setDropIndicatorShown(true);
m_profilesTree->setDragDropMode(QAbstractItemView::InternalMove);
m_profilesTree->setDefaultDropAction(Qt::MoveAction);
m_profilesTree->setContextMenuPolicy(Qt::CustomContextMenu);
m_profilesTree->header()->setSectionResizeMode(0, QHeaderView::Stretch);
m_profilesTree->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
m_profilesTree->header()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
m_profilesTree->header()->setSectionResizeMode(3, QHeaderView::Stretch);
auto* buttonRow = new QHBoxLayout();
m_newButton = new QPushButton(QStringLiteral("New"), this);
m_editButton = new QPushButton(QStringLiteral("Edit"), this);
m_deleteButton = new QPushButton(QStringLiteral("Delete"), this);
buttonRow->addWidget(m_newButton);
buttonRow->addWidget(m_editButton);
buttonRow->addWidget(m_deleteButton);
buttonRow->addStretch();
auto* filterRow = new QHBoxLayout();
filterRow->addWidget(searchLabel);
filterRow->addWidget(m_searchBox, 1);
filterRow->addWidget(viewModeLabel);
filterRow->addWidget(m_viewModeBox);
filterRow->addWidget(protocolFilterLabel);
filterRow->addWidget(m_protocolFilterBox);
filterRow->addWidget(tagFilterLabel);
filterRow->addWidget(m_tagFilterBox);
filterRow->addWidget(sortLabel);
filterRow->addWidget(m_sortBox);
rootLayout->addLayout(filterRow);
rootLayout->addWidget(m_profilesTree, 1);
rootLayout->addLayout(buttonRow);
connect(m_searchBox,
&QLineEdit::textChanged,
this,
[this](const QString&) {
saveUiPreferences();
loadProfiles();
});
connect(m_viewModeBox,
&QComboBox::currentIndexChanged,
this,
[this](int) {
saveUiPreferences();
loadProfiles();
});
connect(m_sortBox,
&QComboBox::currentIndexChanged,
this,
[this](int) {
saveUiPreferences();
loadProfiles();
});
connect(m_protocolFilterBox,
&QComboBox::currentIndexChanged,
this,
[this](int) {
saveUiPreferences();
loadProfiles();
});
connect(m_tagFilterBox,
&QComboBox::currentIndexChanged,
this,
[this](int) {
saveUiPreferences();
loadProfiles();
});
connect(m_profilesTree,
&QTreeWidget::itemDoubleClicked,
this,
[this](QTreeWidgetItem* item, int) { openSessionForItem(item); });
connect(m_profilesTree,
&QWidget::customContextMenuRequested,
this,
[this](const QPoint& pos) { showTreeContextMenu(pos); });
connect(m_profilesTree,
&ProfilesTreeWidget::itemsDropped,
this,
[this]() { persistFolderAssignmentsFromTree(); });
connect(m_newButton, &QPushButton::clicked, this, [this]() { createProfile(); });
connect(m_editButton, &QPushButton::clicked, this, [this]() { editSelectedProfile(); });
connect(m_deleteButton, &QPushButton::clicked, this, [this]() { deleteSelectedProfile(); });
}
void ProfilesWindow::loadProfiles()
{
m_profilesTree->clear();
m_profileCache.clear();
const QString query = m_searchBox == nullptr ? QString() : m_searchBox->text();
const std::vector<Profile> profiles = m_repository->listProfiles(query, selectedSortOrder());
if (!m_repository->lastError().isEmpty()) {
QMessageBox::warning(this,
QStringLiteral("Load Profiles"),
QStringLiteral("Failed to load profiles: %1")
.arg(m_repository->lastError()));
return;
}
updateTagFilterOptions(profiles);
const QString protocolFilter = selectedProtocolFilter();
const QString tagFilter = selectedTagFilter();
const bool folderView = isFolderViewEnabled();
m_profilesTree->setDragEnabled(folderView);
m_profilesTree->setAcceptDrops(folderView);
m_profilesTree->viewport()->setAcceptDrops(folderView);
m_profilesTree->setDropIndicatorShown(folderView);
m_profilesTree->setDragDropMode(folderView ? QAbstractItemView::InternalMove
: QAbstractItemView::NoDragDrop);
std::vector<Profile> filteredProfiles;
filteredProfiles.reserve(profiles.size());
for (const Profile& profile : profiles) {
if (!protocolFilter.isEmpty()
&& profile.protocol.compare(protocolFilter, Qt::CaseInsensitive) != 0) {
continue;
}
if (!tagFilter.isEmpty() && !profileHasTag(profile, tagFilter)) {
continue;
}
filteredProfiles.push_back(profile);
}
if (folderView) {
std::map<QString, QTreeWidgetItem*> folderNodes;
const std::vector<QString> explicitFolders = m_repository->listFolders();
for (const QString& folderPath : explicitFolders) {
if (query.trimmed().isEmpty()
|| folderPath.contains(query.trimmed(), Qt::CaseInsensitive)) {
upsertFolderNode(splitFolderPath(folderPath), folderNodes);
}
}
for (const Profile& profile : filteredProfiles) {
QTreeWidgetItem* parent = nullptr;
const QStringList folderParts = splitFolderPath(profile.folderPath);
if (!folderParts.isEmpty()) {
parent = upsertFolderNode(folderParts, folderNodes);
}
addProfileNode(parent, profile);
}
m_profilesTree->expandAll();
} else {
for (const Profile& profile : filteredProfiles) {
addProfileNode(nullptr, profile);
}
}
}
ProfileSortOrder ProfilesWindow::selectedSortOrder() const
{
if (m_sortBox == nullptr) {
return ProfileSortOrder::NameAsc;
}
const QVariant value = m_sortBox->currentData();
if (!value.isValid()) {
return ProfileSortOrder::NameAsc;
}
const int orderValue = value.toInt();
switch (static_cast<ProfileSortOrder>(orderValue)) {
case ProfileSortOrder::ProtocolAsc:
return ProfileSortOrder::ProtocolAsc;
case ProfileSortOrder::HostAsc:
return ProfileSortOrder::HostAsc;
case ProfileSortOrder::NameAsc:
default:
return ProfileSortOrder::NameAsc;
}
}
bool ProfilesWindow::isFolderViewEnabled() const
{
if (m_viewModeBox == nullptr) {
return false;
}
return m_viewModeBox->currentText().compare(QStringLiteral("Folders"), Qt::CaseInsensitive)
== 0;
}
QString ProfilesWindow::selectedProtocolFilter() const
{
if (m_protocolFilterBox == nullptr) {
return QString();
}
const QString selected = m_protocolFilterBox->currentText().trimmed();
if (selected.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0) {
return QString();
}
return selected;
}
QString ProfilesWindow::selectedTagFilter() const
{
if (m_tagFilterBox == nullptr) {
return QString();
}
const QString selected = m_tagFilterBox->currentText().trimmed();
if (selected.compare(QStringLiteral("All"), Qt::CaseInsensitive) == 0) {
return QString();
}
return selected;
}
void ProfilesWindow::updateTagFilterOptions(const std::vector<Profile>& profiles)
{
if (m_tagFilterBox == nullptr) {
return;
}
const QString previousSelection = m_tagFilterBox->currentText().trimmed();
QString desiredSelection = previousSelection;
if (!m_pendingTagFilterPreference.trimmed().isEmpty()) {
desiredSelection = m_pendingTagFilterPreference.trimmed();
}
QSet<QString> seen;
QStringList tags;
for (const Profile& profile : profiles) {
const QStringList splitTags =
profile.tags.split(QChar::fromLatin1(','), Qt::SkipEmptyParts);
for (const QString& rawTag : splitTags) {
const QString tag = rawTag.trimmed();
if (tag.isEmpty()) {
continue;
}
const QString dedupeKey = tag.toLower();
if (seen.contains(dedupeKey)) {
continue;
}
seen.insert(dedupeKey);
tags.push_back(tag);
}
}
tags.sort(Qt::CaseInsensitive);
const QSignalBlocker blocker(m_tagFilterBox);
m_tagFilterBox->clear();
m_tagFilterBox->addItem(QStringLiteral("All"));
for (const QString& tag : tags) {
m_tagFilterBox->addItem(tag);
}
int restoredIndex = m_tagFilterBox->findText(desiredSelection, Qt::MatchFixedString);
if (restoredIndex < 0) {
restoredIndex = 0;
}
m_tagFilterBox->setCurrentIndex(restoredIndex);
m_pendingTagFilterPreference.clear();
}
QTreeWidgetItem* ProfilesWindow::upsertFolderNode(const QStringList& folderParts,
std::map<QString, QTreeWidgetItem*>& folderNodes)
{
QString currentPath;
QTreeWidgetItem* parent = nullptr;
for (const QString& rawPart : folderParts) {
const QString part = rawPart.trimmed();
if (part.isEmpty()) {
continue;
}
currentPath = currentPath.isEmpty() ? part
: QStringLiteral("%1/%2").arg(currentPath, part);
const auto existing = folderNodes.find(currentPath);
if (existing != folderNodes.end()) {
parent = existing->second;
continue;
}
auto* folderItem = parent == nullptr ? new QTreeWidgetItem(m_profilesTree)
: new QTreeWidgetItem(parent);
folderItem->setText(0, part);
folderItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon));
folderItem->setData(0, kFolderPathRole, currentPath);
folderItem->setFlags((folderItem->flags() | Qt::ItemIsDropEnabled) & ~Qt::ItemIsDragEnabled);
folderNodes.insert_or_assign(currentPath, folderItem);
parent = folderItem;
}
return parent;
}
void ProfilesWindow::addProfileNode(QTreeWidgetItem* parent, const Profile& profile)
{
auto* item = parent == nullptr ? new QTreeWidgetItem(m_profilesTree) : new QTreeWidgetItem(parent);
item->setText(0, profile.name);
item->setText(1, profile.protocol);
item->setText(2, QStringLiteral("%1:%2").arg(profile.host, QString::number(profile.port)));
item->setText(3, profile.tags);
item->setIcon(0, style()->standardIcon(QStyle::SP_ComputerIcon));
item->setData(0, kProfileIdRole, QVariant::fromValue(profile.id));
item->setData(0, kFolderPathRole, normalizeFolderPathForView(profile.folderPath));
item->setFlags((item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsSelectable) & ~Qt::ItemIsDropEnabled);
const QString identity = [&profile]() {
if (profile.protocol.compare(QStringLiteral("RDP"), Qt::CaseInsensitive) == 0
&& !profile.domain.trimmed().isEmpty()) {
return QStringLiteral("%1\\%2").arg(profile.domain.trimmed(),
profile.username.trimmed().isEmpty()
? QStringLiteral("<none>")
: profile.username.trimmed());
}
return profile.username.trimmed().isEmpty() ? QStringLiteral("<none>")
: profile.username.trimmed();
}();
QString tooltip = QStringLiteral("%1://%2@%3:%4\nAuth: %5")
.arg(profile.protocol,
identity,
profile.host,
QString::number(profile.port),
profile.authMode);
if (profile.protocol.compare(QStringLiteral("SSH"), Qt::CaseInsensitive) == 0) {
tooltip += QStringLiteral("\nKnown Hosts: %1").arg(profile.knownHostsPolicy);
} else if (profile.protocol.compare(QStringLiteral("RDP"), Qt::CaseInsensitive) == 0) {
tooltip += QStringLiteral("\nRDP Security: %1\nRDP Performance: %2")
.arg(profile.rdpSecurityMode, profile.rdpPerformanceProfile);
}
if (!profile.folderPath.trimmed().isEmpty()) {
tooltip += QStringLiteral("\nFolder: %1").arg(profile.folderPath.trimmed());
}
if (!profile.tags.trimmed().isEmpty()) {
tooltip += QStringLiteral("\nTags: %1").arg(profile.tags);
}
item->setToolTip(0, tooltip);
item->setToolTip(1, tooltip);
item->setToolTip(2, tooltip);
item->setToolTip(3, tooltip);
m_profileCache.insert_or_assign(profile.id, profile);
}
QString ProfilesWindow::folderPathForItem(const QTreeWidgetItem* item) const
{
if (item == nullptr) {
return QString();
}
const QVariant idValue = item->data(0, kProfileIdRole);
if (idValue.isValid()) {
const qint64 id = idValue.toLongLong();
const auto cacheIt = m_profileCache.find(id);
if (cacheIt != m_profileCache.end()) {
return normalizeFolderPathForView(cacheIt->second.folderPath);
}
}
return normalizeFolderPathForView(item->data(0, kFolderPathRole).toString());
}
void ProfilesWindow::showTreeContextMenu(const QPoint& pos)
{
if (m_profilesTree == nullptr) {
return;
}
QTreeWidgetItem* item = m_profilesTree->itemAt(pos);
if (item != nullptr) {
m_profilesTree->setCurrentItem(item);
}
const QString contextFolder = folderPathForItem(item);
const bool isProfileItem = item != nullptr && item->data(0, kProfileIdRole).isValid();
const bool isFolderItem =
item != nullptr && !isProfileItem && item->data(0, kFolderPathRole).isValid();
QMenu menu(this);
QAction* newConnectionAction = menu.addAction(QStringLiteral("New Connection"));
QAction* newFolderAction = menu.addAction(QStringLiteral("New Folder"));
QAction* connectAction = nullptr;
QAction* editAction = nullptr;
QAction* deleteAction = nullptr;
QAction* deleteFolderAction = nullptr;
if (isProfileItem) {
menu.addSeparator();
connectAction = menu.addAction(QStringLiteral("Connect"));
editAction = menu.addAction(QStringLiteral("Edit"));
deleteAction = menu.addAction(QStringLiteral("Delete"));
} else if (isFolderItem) {
menu.addSeparator();
deleteFolderAction = menu.addAction(QStringLiteral("Delete Folder"));
}
QAction* chosen = menu.exec(m_profilesTree->viewport()->mapToGlobal(pos));
if (chosen == nullptr) {
return;
}
if (chosen == newConnectionAction) {
createProfile(contextFolder);
return;
}
if (chosen == newFolderAction) {
createFolderInContext(contextFolder);
return;
}
if (isProfileItem && chosen == connectAction) {
openSessionForItem(item);
return;
}
if (isProfileItem && chosen == editAction) {
editSelectedProfile();
return;
}
if (isProfileItem && chosen == deleteAction) {
deleteSelectedProfile();
return;
}
if (isFolderItem && chosen == deleteFolderAction) {
deleteFolderInContext(contextFolder);
return;
}
}
void ProfilesWindow::createFolderInContext(const QString& baseFolderPath)
{
bool accepted = false;
const QString folderName = QInputDialog::getText(
this,
QStringLiteral("New Folder"),
QStringLiteral("Folder name"),
QLineEdit::Normal,
QString(),
&accepted);
if (!accepted) {
return;
}
const QString normalized = joinFolderPath(baseFolderPath, folderName);
if (normalized.isEmpty()) {
QMessageBox::information(this,
QStringLiteral("New Folder"),
QStringLiteral("Folder name is required."));
return;
}
if (!m_repository->createFolder(normalized)) {
QMessageBox::warning(this,
QStringLiteral("New Folder"),
QStringLiteral("Failed to create folder: %1")
.arg(m_repository->lastError().isEmpty()
? QStringLiteral("unknown error")
: m_repository->lastError()));
return;
}
loadProfiles();
}
void ProfilesWindow::deleteFolderInContext(const QString& folderPath)
{
const QString normalized = normalizeFolderPathForView(folderPath);
if (normalized.isEmpty()) {
return;
}
// Deleting a folder never deletes profiles -- everything inside it
// (profiles and subfolders) shifts up to take its place. Count what's
// affected so the confirmation is honest about that, rather than
// reading like the usual destructive "Delete" action.
const QString prefix = normalized + QStringLiteral("/");
int profileCount = 0;
for (const Profile& profile : m_repository->listProfiles()) {
if (profile.folderPath == normalized || profile.folderPath.startsWith(prefix)) {
++profileCount;
}
}
int subfolderCount = 0;
for (const QString& folder : m_repository->listFolders()) {
if (folder != normalized && folder.startsWith(prefix)) {
++subfolderCount;
}
}
if (profileCount > 0 || subfolderCount > 0) {
QStringList parentParts = splitFolderPath(normalized);
parentParts.removeLast();
const QString destination = parentParts.isEmpty()
? QStringLiteral("the top level")
: QStringLiteral("'%1'").arg(parentParts.join(QStringLiteral("/")));
QStringList details;
if (profileCount > 0) {
details.push_back(QStringLiteral("%1 profile(s)").arg(profileCount));
}
if (subfolderCount > 0) {
details.push_back(QStringLiteral("%1 subfolder(s)").arg(subfolderCount));
}
const QMessageBox::StandardButton confirm = QMessageBox::question(
this,
QStringLiteral("Delete Folder"),
QStringLiteral("Delete folder '%1'?\n\nNothing will be deleted: %2 currently inside "
"it will move up to %3.")
.arg(normalized, details.join(QStringLiteral(" and ")), destination),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (confirm != QMessageBox::Yes) {
return;
}
}
if (!m_repository->deleteFolder(normalized)) {
QMessageBox::warning(this,
QStringLiteral("Delete Folder"),
QStringLiteral("Failed to delete folder: %1")
.arg(m_repository->lastError().isEmpty()
? QStringLiteral("unknown error")
: m_repository->lastError()));
return;
}
loadProfiles();
}
void ProfilesWindow::persistFolderAssignmentsFromTree()
{
if (!isFolderViewEnabled() || m_profilesTree == nullptr) {
return;
}
std::unordered_map<qint64, QString> assignments;
const int topLevelCount = m_profilesTree->topLevelItemCount();
for (int index = 0; index < topLevelCount; ++index) {
collectProfileAssignments(m_profilesTree->topLevelItem(index), QString(), assignments);
}
bool hadErrors = false;
int updatedCount = 0;
for (const auto& [id, newFolderPath] : assignments) {
auto cacheIt = m_profileCache.find(id);
Profile profile;
if (cacheIt != m_profileCache.end()) {
profile = cacheIt->second;
} else {
const std::optional<Profile> fetched = m_repository->getProfile(id);
if (!fetched.has_value()) {
continue;
}
profile = fetched.value();
}
const QString existing = normalizeFolderPathForView(profile.folderPath);
const QString target = normalizeFolderPathForView(newFolderPath);
if (existing == target) {
continue;
}
profile.folderPath = target;
if (!target.isEmpty()) {
m_repository->createFolder(target);
}
if (!m_repository->updateProfile(profile)) {
hadErrors = true;
continue;
}
m_profileCache.insert_or_assign(profile.id, profile);
++updatedCount;
}
if (hadErrors) {
QMessageBox::warning(this,
QStringLiteral("Move Profile"),
QStringLiteral("One or more profile moves could not be saved."));
}
if (updatedCount > 0 || hadErrors) {
loadProfiles();
}
}
void ProfilesWindow::collectProfileAssignments(
const QTreeWidgetItem* item,
const QString& parentFolderPath,
std::unordered_map<qint64, QString>& assignments) const
{
if (item == nullptr) {
return;
}
const QVariant idValue = item->data(0, kProfileIdRole);
if (idValue.isValid()) {
assignments.insert_or_assign(idValue.toLongLong(), normalizeFolderPathForView(parentFolderPath));
return;
}
const QString folderPath = joinFolderPath(parentFolderPath, item->text(0));
const int childCount = item->childCount();
for (int index = 0; index < childCount; ++index) {
collectProfileAssignments(item->child(index), folderPath, assignments);
}
}
void ProfilesWindow::loadUiPreferences()
{
QSettings settings;
const QString search = settings.value(QStringLiteral("profiles/searchText")).toString();
const QString viewMode =
settings.value(QStringLiteral("profiles/viewMode"), QStringLiteral("List")).toString();
const int sortValue = settings
.value(QStringLiteral("profiles/sortOrder"),
static_cast<int>(ProfileSortOrder::NameAsc))
.toInt();
const QString protocolFilter =
settings.value(QStringLiteral("profiles/protocolFilter"), QStringLiteral("All")).toString();
const QString tagFilter =
settings.value(QStringLiteral("profiles/tagFilter"), QStringLiteral("All")).toString();
m_pendingTagFilterPreference = tagFilter.trimmed();
if (m_searchBox != nullptr) {
const QSignalBlocker blocker(m_searchBox);
m_searchBox->setText(search);
}
if (m_viewModeBox != nullptr) {
int found = m_viewModeBox->findText(viewMode, Qt::MatchFixedString);
if (found < 0) {
found = 0;
}
const QSignalBlocker blocker(m_viewModeBox);
m_viewModeBox->setCurrentIndex(found);
}
if (m_sortBox != nullptr) {
int found = m_sortBox->findData(sortValue);
if (found < 0) {
found = 0;
}
const QSignalBlocker blocker(m_sortBox);
m_sortBox->setCurrentIndex(found);
}
if (m_protocolFilterBox != nullptr) {
int found = m_protocolFilterBox->findText(protocolFilter, Qt::MatchFixedString);
if (found < 0) {
found = 0;
}
const QSignalBlocker blocker(m_protocolFilterBox);
m_protocolFilterBox->setCurrentIndex(found);
}
if (m_tagFilterBox != nullptr) {
int found = m_tagFilterBox->findText(tagFilter, Qt::MatchFixedString);
if (found < 0) {
found = 0;
}
const QSignalBlocker blocker(m_tagFilterBox);
m_tagFilterBox->setCurrentIndex(found);
}
}
void ProfilesWindow::saveUiPreferences() const
{
QSettings settings;
if (m_searchBox != nullptr) {
settings.setValue(QStringLiteral("profiles/searchText"), m_searchBox->text());
}
if (m_viewModeBox != nullptr) {
settings.setValue(QStringLiteral("profiles/viewMode"), m_viewModeBox->currentText());
}
if (m_sortBox != nullptr) {
settings.setValue(QStringLiteral("profiles/sortOrder"), m_sortBox->currentData());
}
if (m_protocolFilterBox != nullptr) {
settings.setValue(QStringLiteral("profiles/protocolFilter"), m_protocolFilterBox->currentText());
}
if (m_tagFilterBox != nullptr) {
settings.setValue(QStringLiteral("profiles/tagFilter"), m_tagFilterBox->currentText());
}
}
std::optional<Profile> ProfilesWindow::selectedProfile() const
{
QTreeWidgetItem* item = m_profilesTree->currentItem();
if (item == nullptr) {
return std::nullopt;
}
const QVariant value = item->data(0, kProfileIdRole);
if (!value.isValid()) {
return std::nullopt;
}
const qint64 id = value.toLongLong();
const auto cacheIt = m_profileCache.find(id);
if (cacheIt != m_profileCache.end()) {
return cacheIt->second;
}
return m_repository->getProfile(id);
}
void ProfilesWindow::createProfile(const QString& defaultFolderPath)
{
ProfileDialog dialog(this);
dialog.setDialogTitle(QStringLiteral("New Profile"));
dialog.setDefaultFolderPath(defaultFolderPath);
if (dialog.exec() != QDialog::Accepted) {
return;
}
const Profile newProfile = dialog.profile();
if (!newProfile.folderPath.trimmed().isEmpty()) {
m_repository->createFolder(newProfile.folderPath);
}
if (!m_repository->createProfile(newProfile).has_value()) {
QMessageBox::warning(this,
QStringLiteral("Create Profile"),
QStringLiteral("Failed to create profile: %1")
.arg(m_repository->lastError().isEmpty()
? QStringLiteral("unknown error")
: m_repository->lastError()));
return;
}
loadProfiles();
}
void ProfilesWindow::editSelectedProfile()
{
const std::optional<Profile> selected = selectedProfile();
if (!selected.has_value()) {
QMessageBox::information(this,
QStringLiteral("Edit Profile"),
QStringLiteral("Select a profile first."));
return;
}
ProfileDialog dialog(this);
dialog.setDialogTitle(QStringLiteral("Edit Profile"));
dialog.setProfile(selected.value());
if (dialog.exec() != QDialog::Accepted) {
return;
}
Profile updated = dialog.profile();
updated.id = selected->id;
if (!updated.folderPath.trimmed().isEmpty()) {
m_repository->createFolder(updated.folderPath);
}
if (!m_repository->updateProfile(updated)) {
QMessageBox::warning(this,
QStringLiteral("Edit Profile"),
QStringLiteral("Failed to update profile: %1")
.arg(m_repository->lastError().isEmpty()
? QStringLiteral("unknown error")
: m_repository->lastError()));
return;
}
loadProfiles();
}
void ProfilesWindow::deleteSelectedProfile()
{
const std::optional<Profile> selected = selectedProfile();
if (!selected.has_value()) {
QMessageBox::information(this,
QStringLiteral("Delete Profile"),
QStringLiteral("Select a profile first."));
return;
}
const QMessageBox::StandardButton confirm = QMessageBox::question(
this,
QStringLiteral("Delete Profile"),
QStringLiteral("Delete profile '%1'?").arg(selected->name),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (confirm != QMessageBox::Yes) {
return;
}
if (!m_repository->deleteProfile(selected->id)) {
QMessageBox::warning(this,
QStringLiteral("Delete Profile"),
QStringLiteral("Failed to delete profile: %1")
.arg(m_repository->lastError().isEmpty()
? QStringLiteral("unknown error")
: m_repository->lastError()));
return;
}
loadProfiles();
}
void ProfilesWindow::openSessionForItem(QTreeWidgetItem* item)
{
if (item == nullptr) {
return;
}
const QVariant value = item->data(0, kProfileIdRole);
if (!value.isValid()) {
item->setExpanded(!item->isExpanded());
return;
}
const qint64 id = value.toLongLong();
const std::optional<Profile> profile = m_repository->getProfile(id);
if (!profile.has_value()) {
QMessageBox::warning(this,
QStringLiteral("Connect"),
QStringLiteral("Failed to load profile for session: %1")
.arg(m_repository->lastError().isEmpty()
? QStringLiteral("profile not found")
: m_repository->lastError()));
return;
}
emit connectRequested(profile.value());
}
void ProfilesWindow::createProfileInCurrentContext()
{
const QString folderPath = folderPathForItem(m_profilesTree->currentItem());
createProfile(folderPath);
}
void ProfilesWindow::createFolderInCurrentContext()
{
const QString folderPath = folderPathForItem(m_profilesTree->currentItem());
createFolderInContext(folderPath);
}
void ProfilesWindow::exportProfiles()
{
const QString fileName = QFileDialog::getSaveFileName(
this, QStringLiteral("Export Profiles"), QStringLiteral("orbithub-profiles.json"),
QStringLiteral("JSON Files (*.json)"));
if (fileName.isEmpty()) {
return;
}
QJsonArray folders;
for (const QString& folderPath : m_repository->listFolders()) {
folders.append(folderPath);
}
QJsonArray profiles;
for (const Profile& profile : m_repository->listProfiles()) {
profiles.append(profileToJson(profile));
}
QJsonObject root;
root.insert(QStringLiteral("orbithubProfileExport"), kProfileExportFormatVersion);
root.insert(QStringLiteral("folders"), folders);
root.insert(QStringLiteral("profiles"), profiles);
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QMessageBox::warning(this,
QStringLiteral("Export Profiles"),
QStringLiteral("Failed to write %1: %2")
.arg(fileName, file.errorString()));
return;
}
file.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
file.close();
QMessageBox::information(this,
QStringLiteral("Export Profiles"),
QStringLiteral("Exported %1 profile(s) to %2.")
.arg(profiles.size())
.arg(fileName));
}
void ProfilesWindow::importProfiles()
{
const QString fileName = QFileDialog::getOpenFileName(
this, QStringLiteral("Import Profiles"), QString(), QStringLiteral("JSON Files (*.json)"));
if (fileName.isEmpty()) {
return;
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this,
QStringLiteral("Import Profiles"),
QStringLiteral("Failed to read %1: %2")
.arg(fileName, file.errorString()));
return;
}
QJsonParseError parseError{};
const QJsonDocument doc = QJsonDocument::fromJson(file.readAll(), &parseError);
file.close();
if (parseError.error != QJsonParseError::NoError || !doc.isObject()) {
QMessageBox::warning(this,
QStringLiteral("Import Profiles"),
QStringLiteral("%1 is not a valid OrbitHub profile export: %2")
.arg(fileName, parseError.errorString()));
return;
}
const QJsonObject root = doc.object();
if (!root.value(QStringLiteral("profiles")).isArray()) {
QMessageBox::warning(this,
QStringLiteral("Import Profiles"),
QStringLiteral("%1 does not contain a profile list.").arg(fileName));
return;
}
for (const QJsonValue& folderValue : root.value(QStringLiteral("folders")).toArray()) {
const QString folderPath = folderValue.toString();
if (!folderPath.isEmpty()) {
m_repository->createFolder(folderPath);
}
}
const QJsonArray profilesArray = root.value(QStringLiteral("profiles")).toArray();
int imported = 0;
QStringList failures;
for (const QJsonValue& profileValue : profilesArray) {
if (!profileValue.isObject()) {
continue;
}
const Profile profile = profileFromJson(profileValue.toObject());
if (profile.name.isEmpty() || profile.host.isEmpty()) {
failures.push_back(QStringLiteral("(unnamed profile): missing name or host"));
continue;
}
// No need to createFolder(profile.folderPath) here: the tree view
// already synthesizes folder nodes from a profile's own folderPath
// (see ProfilesWindow::loadProfiles). Explicit profile_folders rows
// are only for folders with no profiles in them, and those are
// already recreated above from the export's top-level "folders"
// list -- doing it again per-profile would just add spurious
// entries not present in the original export.
if (m_repository->createProfile(profile).has_value()) {
++imported;
} else {
failures.push_back(QStringLiteral("%1: %2").arg(profile.name, m_repository->lastError()));
}
}
loadProfiles();
QString summary = QStringLiteral("Imported %1 of %2 profile(s) from %3.")
.arg(imported)
.arg(profilesArray.size())
.arg(fileName);
if (!failures.isEmpty()) {
summary += QStringLiteral("\n\nFailed:\n%1").arg(failures.join(QChar::fromLatin1('\n')));
QMessageBox::warning(this, QStringLiteral("Import Profiles"), summary);
} else {
QMessageBox::information(this, QStringLiteral("Import Profiles"), summary);
}
}
void ProfilesWindow::importFromMRemoteNG()
{
const QString fileName = QFileDialog::getOpenFileName(
this, QStringLiteral("Import from mRemoteNG"), QString(),
QStringLiteral("mRemoteNG Connections (*.xml)"));
if (fileName.isEmpty()) {
return;
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::warning(this,
QStringLiteral("Import from mRemoteNG"),
QStringLiteral("Failed to read %1: %2")
.arg(fileName, file.errorString()));
return;
}
const QByteArray xmlData = file.readAll();
file.close();
const MRemoteNGImportResult parsed = parseMRemoteNGConnections(xmlData);
if (!parsed.errorMessage.isEmpty()) {
QMessageBox::warning(this, QStringLiteral("Import from mRemoteNG"), parsed.errorMessage);
return;
}
for (const QString& folderPath : parsed.folders) {
m_repository->createFolder(folderPath);
}
int imported = 0;
QStringList failures;
for (const MRemoteNGImportedProfile& item : parsed.profiles) {
Profile profile = item.profile;
profile.folderPath = item.folderPath;
if (profile.name.isEmpty() || profile.host.isEmpty()) {
failures.push_back(QStringLiteral("(unnamed connection): missing name or host"));
continue;
}
if (m_repository->createProfile(profile).has_value()) {
++imported;
} else {
failures.push_back(QStringLiteral("%1: %2").arg(profile.name, m_repository->lastError()));
}
}
loadProfiles();
QString summary = QStringLiteral("Imported %1 of %2 connection(s) from %3.\n\n"
"Passwords are never imported -- you'll be prompted the "
"first time you connect each profile, same as a new one.")
.arg(imported)
.arg(static_cast<int>(parsed.profiles.size()))
.arg(fileName);
if (!parsed.skippedUnsupportedProtocol.isEmpty()) {
summary += QStringLiteral(
"\n\nSkipped %1 connection(s) using a protocol OrbitHub doesn't support "
"yet:\n%2")
.arg(static_cast<int>(parsed.skippedUnsupportedProtocol.size()))
.arg(parsed.skippedUnsupportedProtocol.join(QChar::fromLatin1('\n')));
}
if (!failures.isEmpty()) {
summary += QStringLiteral("\n\nFailed:\n%1").arg(failures.join(QChar::fromLatin1('\n')));
QMessageBox::warning(this, QStringLiteral("Import from mRemoteNG"), summary);
} else {
QMessageBox::information(this, QStringLiteral("Import from mRemoteNG"), summary);
}
}