Skip to content

Commit b85a43b

Browse files
committed
fix(editor): persist file encoding across sessions
Activate the existing but unused encoding history mechanism to remember the user's encoding choice per file, skipping unreliable content-based detection on reopen. 激活已有但未接入的编码历史持久化机制,保存时记录编码, 重新打开时优先使用历史编码而非自动检测。 Log: 激活编码历史持久化,修复切换编码保存后重新打开仍显示UTF-8的问题 PMS: BUG-332119 Influence: 修改编码格式保存后重新打开文件,底栏正确显示用户选择的编码格式。
1 parent 7d6af5d commit b85a43b

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

src/common/fileloadthread.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -20,6 +20,11 @@ FileLoadThread::~FileLoadThread()
2020
qDebug() << "Destroying FileLoadThread for file:" << m_strFilePath;
2121
}
2222

23+
void FileLoadThread::setEncodeHint(const QByteArray &encode)
24+
{
25+
m_encodeHint = encode;
26+
}
27+
2328
void FileLoadThread::run()
2429
{
2530
qDebug() << "Starting file load thread for:" << m_strFilePath;
@@ -35,7 +40,11 @@ void FileLoadThread::run()
3540
qDebug() << "Large file detected (" << file.size() << " bytes), using optimized loading strategy";
3641
// 先读取1MB数据
3742
indata = file.read(DATA_SIZE_1024 * DATA_SIZE_1024);
38-
encode = DetectCode::GetFileEncodingFormat(m_strFilePath, indata);
43+
if (!m_encodeHint.isEmpty()) {
44+
encode = m_encodeHint;
45+
} else {
46+
encode = DetectCode::GetFileEncodingFormat(m_strFilePath, indata);
47+
}
3948
qDebug() << "Initial encoding detection result:" << encode;
4049

4150
// 发送文件头信息,用于预先加载数据
@@ -64,7 +73,10 @@ void FileLoadThread::run()
6473
return;
6574
}
6675

67-
if (encode.isEmpty()) {
76+
if (!m_encodeHint.isEmpty()) {
77+
qDebug() << "Using encoding hint:" << m_encodeHint;
78+
encode = m_encodeHint;
79+
} else if (encode.isEmpty()) {
6880
qDebug() << "Performing full encoding detection";
6981
//编码识别,如果文件数据大于1M,则只裁剪出1M文件数据去做编码探测
7082
QByteArray dateUsedForCodeIdentify;

src/common/fileloadthread.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
1+
// SPDX-FileCopyrightText: 2017 - 2026 UnionTech Software Technology Co., Ltd.
22
//
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

@@ -14,6 +14,7 @@ class FileLoadThread : public QThread
1414
FileLoadThread(const QString &filepath, QObject *QObject = nullptr);
1515
~FileLoadThread();
1616

17+
void setEncodeHint(const QByteArray &encode);
1718
void run();
1819

1920
signals:
@@ -23,6 +24,7 @@ class FileLoadThread : public QThread
2324

2425
private:
2526
QString m_strFilePath;
27+
QByteArray m_encodeHint;
2628
};
2729

2830
#endif

src/editor/dtextedit.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5626,6 +5626,28 @@ QStringList TextEdit::readEncodeHistoryRecord()
56265626
return filePathList;
56275627
}
56285628

5629+
QByteArray TextEdit::getStoredEncode(const QString &filePath)
5630+
{
5631+
Settings *settings = Settings::instance();
5632+
if (!settings) return QByteArray();
5633+
5634+
QString history = settings->settings->option("advance.editor.browsing_encode_history")->value().toString();
5635+
QString pattern = "*{*[" + filePath + "]*";
5636+
int pos = history.indexOf(pattern);
5637+
if (pos == -1) return QByteArray();
5638+
5639+
int encodeStart = pos + pattern.length();
5640+
int encodeEnd = history.indexOf("}*", encodeStart);
5641+
if (encodeEnd == -1) return QByteArray();
5642+
5643+
return history.mid(encodeStart, encodeEnd - encodeStart).toLocal8Bit();
5644+
}
5645+
5646+
void TextEdit::setTextEncode(const QString &encode)
5647+
{
5648+
m_textEncode = encode;
5649+
}
5650+
56295651
void TextEdit::tellFindBarClose()
56305652
{
56315653
qDebug() << "Tell find bar close";

src/editor/dtextedit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ class TextEdit : public DPlainTextEdit
414414
void setCursorStart(int pos);
415415
void writeEncodeHistoryRecord();
416416
QStringList readEncodeHistoryRecord();
417+
static QByteArray getStoredEncode(const QString &filePath);
418+
void setTextEncode(const QString &encode);
417419
/**
418420
* @brief tellFindBarClose 通知查找框关闭
419421
*/

src/editor/editwrapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ void EditWrapper::openFile(const QString &filepath, QString qstrTruePath, bool b
197197

198198
FileLoadThread *thread = new FileLoadThread(filepath);
199199
qDebug() << "Created file load thread for:" << filepath;
200+
// 优先使用历史记录的编码
201+
QByteArray storedEncode = TextEdit::getStoredEncode(filepath);
202+
if (!storedEncode.isEmpty()) {
203+
qDebug() << "Using stored encoding from history:" << storedEncode;
204+
thread->setEncodeHint(storedEncode);
205+
}
200206
// begin to load the file.
201207
connect(thread, &FileLoadThread::sigPreProcess, this, &EditWrapper::handleFilePreProcess);
202208
connect(thread, &FileLoadThread::sigLoadFinished, this, &EditWrapper::handleFileLoadFinished);
@@ -615,6 +621,9 @@ bool EditWrapper::saveFile(QByteArray encode)
615621
m_tModifiedDateTime = fi.lastModified();
616622
updateModifyStatus(false);
617623
m_bIsTemFile = false;
624+
// 记录编码到历史,下次打开时优先使用
625+
m_pTextEdit->setTextEncode(m_sCurEncode);
626+
m_pTextEdit->writeEncodeHistoryRecord();
618627
} else {
619628
qDebug() << "EditWrapper saveFile, ok is false";
620629
DMessageManager::instance()->sendMessage(
@@ -684,6 +693,8 @@ bool EditWrapper::saveTemFile(QString qstrDir)
684693
qDebug() << "EditWrapper saveTemFile, ok is true";
685694
m_sFirstEncode = m_sCurEncode;
686695
updateModifyStatus(isModified());
696+
m_pTextEdit->setTextEncode(m_sCurEncode);
697+
m_pTextEdit->writeEncodeHistoryRecord();
687698
}
688699
qDebug() << "EditWrapper saveTemFile, ok:" << ok;
689700
return ok;
@@ -799,6 +810,8 @@ bool EditWrapper::saveDraftFile(QString &newFilePath)
799810
updateSaveAsFileName(m_pTextEdit->getFilePath(), newFilePath);
800811
m_pTextEdit->document()->setModified(false);
801812
m_bIsTemFile = false;
813+
m_pTextEdit->setTextEncode(m_sCurEncode);
814+
m_pTextEdit->writeEncodeHistoryRecord();
802815
qDebug() << "EditWrapper saveDraftFile, saver.save() success";
803816
return true;
804817
}

0 commit comments

Comments
 (0)