From 56995c9edf1f88009fef626b7eebc4beedf7762d Mon Sep 17 00:00:00 2001 From: Petr Mironychev <9195189+Palm1r@users.noreply.github.com> Date: Sun, 26 Jan 2025 23:06:51 +0100 Subject: [PATCH] fix: Saving name of chat in native language - Add button to show chat history folder in system viewer --- ChatView/ChatRootView.cpp | 69 +++++++++++++++++++++++++++++++---- ChatView/ChatRootView.hpp | 1 + ChatView/qml/RootItem.qml | 1 + ChatView/qml/parts/TopBar.qml | 8 +++- 4 files changed, 70 insertions(+), 9 deletions(-) diff --git a/ChatView/ChatRootView.cpp b/ChatView/ChatRootView.cpp index c03367a..fa59716 100644 --- a/ChatView/ChatRootView.cpp +++ b/ChatView/ChatRootView.cpp @@ -20,6 +20,7 @@ #include "ChatRootView.hpp" #include +#include #include #include @@ -72,7 +73,7 @@ ChatRootView::ChatRootView(QQuickItem *parent) this, &ChatRootView::updateInputTokensCount); - connect(m_chatModel, &ChatModel::modelReseted, [this]() { setRecentFilePath(QString{}); }); + connect(m_chatModel, &ChatModel::modelReseted, this, [this]() { setRecentFilePath(QString{}); }); connect(this, &ChatRootView::attachmentFilesChanged, &ChatRootView::updateInputTokensCount); connect(this, &ChatRootView::linkedFilesChanged, &ChatRootView::updateInputTokensCount); connect(&Settings::chatAssistantSettings().useSystemPrompt, &Utils::BaseAspect::changed, @@ -91,7 +92,7 @@ ChatRootView::ChatRootView(QQuickItem *parent) connect(editors, &Core::EditorManager::currentEditorAboutToChange, this, [this]() { if (m_isSyncOpenFiles) { - for (auto editor : m_currentEditors) { + for (auto editor : std::as_const(m_currentEditors)) { onAppendLinkFileFromEditor(editor); } } @@ -256,17 +257,50 @@ QString ChatRootView::getSuggestedFileName() const { QStringList parts; + static const QRegularExpression saitizeSymbols = QRegularExpression("[\\/:*?\"<>|\\s]"); + static const QRegularExpression underSymbols = QRegularExpression("_+"); + if (m_chatModel->rowCount() > 0) { QString firstMessage = m_chatModel->data(m_chatModel->index(0), ChatModel::Content).toString(); QString shortMessage = firstMessage.split('\n').first().simplified().left(30); - shortMessage.replace(QRegularExpression("[^a-zA-Z0-9_-]"), "_"); - parts << shortMessage; + + QString sanitizedMessage = shortMessage; + sanitizedMessage.replace(saitizeSymbols, "_"); + sanitizedMessage.replace(underSymbols, "_"); + sanitizedMessage = sanitizedMessage.trimmed(); + + if (!sanitizedMessage.isEmpty()) { + if (sanitizedMessage.startsWith('_')) { + sanitizedMessage.remove(0, 1); + } + if (sanitizedMessage.endsWith('_')) { + sanitizedMessage.chop(1); + } + + QString targetDir = getChatsHistoryDir(); + QString fullPath = QDir(targetDir).filePath(sanitizedMessage); + + QFileInfo fileInfo(fullPath); + if (!fileInfo.exists() && QFileInfo(fileInfo.path()).isWritable()) { + parts << sanitizedMessage; + } + } } parts << QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm"); - return parts.join("_"); + QString fileName = parts.join("_"); + + QString fullPath = QDir(getChatsHistoryDir()).filePath(fileName); + QFileInfo finalCheck(fullPath); + + if (fileName.isEmpty() || finalCheck.exists() || !QFileInfo(finalCheck.path()).isWritable()) { + fileName = QString("chat_%1").arg( + QDateTime::currentDateTime().toString("yyyy-MM-dd_HH-mm")); + } + + return fileName; } void ChatRootView::autosave() @@ -319,7 +353,7 @@ void ChatRootView::showAttachFilesDialog() QStringList newFilePaths = dialog.selectedFiles(); if (!newFilePaths.isEmpty()) { bool filesAdded = false; - for (const QString &filePath : newFilePaths) { + for (const QString &filePath : std::as_const(newFilePaths)) { if (!m_attachmentFiles.contains(filePath)) { m_attachmentFiles.append(filePath); filesAdded = true; @@ -353,7 +387,7 @@ void ChatRootView::showLinkFilesDialog() QStringList newFilePaths = dialog.selectedFiles(); if (!newFilePaths.isEmpty()) { bool filesAdded = false; - for (const QString &filePath : newFilePaths) { + for (const QString &filePath : std::as_const(newFilePaths)) { if (!m_linkedFiles.contains(filePath)) { m_linkedFiles.append(filePath); filesAdded = true; @@ -388,12 +422,31 @@ void ChatRootView::setIsSyncOpenFiles(bool state) } if (m_isSyncOpenFiles) { - for (auto editor : m_currentEditors) { + for (auto editor : std::as_const(m_currentEditors)) { onAppendLinkFileFromEditor(editor); } } } +void ChatRootView::openChatHistoryFolder() +{ + QString path; + if (auto project = ProjectExplorer::ProjectManager::startupProject()) { + Settings::ProjectSettings projectSettings(project); + path = projectSettings.chatHistoryPath().toString(); + } else { + path = QString("%1/qodeassist/chat_history").arg(Core::ICore::userResourcePath().toString()); + } + + QDir dir(path); + if (!dir.exists()) { + dir.mkpath("."); + } + + QUrl url = QUrl::fromLocalFile(dir.absolutePath()); + QDesktopServices::openUrl(url); +} + void ChatRootView::updateInputTokensCount() { int inputTokens = m_messageTokensCount; diff --git a/ChatView/ChatRootView.hpp b/ChatView/ChatRootView.hpp index 54b4b44..0ca96f8 100644 --- a/ChatView/ChatRootView.hpp +++ b/ChatView/ChatRootView.hpp @@ -64,6 +64,7 @@ public: Q_INVOKABLE void removeFileFromLinkList(int index); Q_INVOKABLE void calculateMessageTokensCount(const QString &message); Q_INVOKABLE void setIsSyncOpenFiles(bool state); + Q_INVOKABLE void openChatHistoryFolder(); Q_INVOKABLE void updateInputTokensCount(); int inputTokensCount() const; diff --git a/ChatView/qml/RootItem.qml b/ChatView/qml/RootItem.qml index 303739a..de7baa3 100644 --- a/ChatView/qml/RootItem.qml +++ b/ChatView/qml/RootItem.qml @@ -75,6 +75,7 @@ ChatRootView { recentPath { text: qsTr("Latest chat file name: %1").arg(root.chatFileName.length > 0 ? root.chatFileName : "Unsaved") } + openChatHistory.onClicked: root.openChatHistoryFolder() } ListView { diff --git a/ChatView/qml/parts/TopBar.qml b/ChatView/qml/parts/TopBar.qml index d7aac35..8d4be28 100644 --- a/ChatView/qml/parts/TopBar.qml +++ b/ChatView/qml/parts/TopBar.qml @@ -29,6 +29,7 @@ Rectangle { property alias clearButton: clearButtonId property alias tokensBadge: tokensBadgeId property alias recentPath: recentPathId + property alias openChatHistory: openChatHistoryId color: palette.window.hslLightness > 0.5 ? Qt.darker(palette.window, 1.1) : @@ -66,11 +67,16 @@ Rectangle { Text { id: recentPathId - Layout.fillWidth: true elide: Text.ElideMiddle color: palette.text } + QoAButton { + id: openChatHistoryId + + text: qsTr("Show in system") + } + Item { Layout.fillWidth: true }