| 140 | } |
| 141 | |
| 142 | bool ChangesManager::applyFileEdit(const QString &editId) |
| 143 | { |
| 144 | QMutexLocker locker(&m_mutex); |
| 145 | |
| 146 | if (!m_fileEdits.contains(editId)) { |
| 147 | LOG_MESSAGE(QString("File edit not found: %1").arg(editId)); |
| 148 | return false; |
| 149 | } |
| 150 | |
| 151 | FileEdit &edit = m_fileEdits[editId]; |
| 152 | |
| 153 | if (edit.status == Applied) { |
| 154 | LOG_MESSAGE(QString("File edit already applied: %1").arg(editId)); |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | if (edit.status == Archived) { |
| 159 | LOG_MESSAGE(QString("Cannot apply archived file edit: %1").arg(editId)); |
| 160 | edit.statusMessage = "Cannot apply archived edit from history"; |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | QString filePathCopy = edit.filePath; |
| 165 | QString oldContentCopy = edit.oldContent; |
| 166 | QString newContentCopy = edit.newContent; |
| 167 | |
| 168 | locker.unlock(); |
| 169 | |
| 170 | LOG_MESSAGE(QString("Applying edit %1 using fragment replacement").arg(editId)); |
| 171 | |
| 172 | QString errorMsg; |
| 173 | bool isAppend = oldContentCopy.isEmpty(); |
| 174 | bool success = performFragmentReplacement( |
| 175 | filePathCopy, oldContentCopy, newContentCopy, isAppend, &errorMsg); |
| 176 | |
| 177 | locker.relock(); |
| 178 | |
| 179 | if (success) { |
| 180 | edit.status = Applied; |
| 181 | edit.statusMessage = errorMsg.isEmpty() ? "Successfully applied" : errorMsg; |
| 182 | |
| 183 | locker.unlock(); |
| 184 | emit fileEditApplied(editId); |
| 185 | |
| 186 | LOG_MESSAGE(QString("File edit applied successfully: %1").arg(editId)); |
| 187 | return true; |
| 188 | } else { |
| 189 | edit.statusMessage = errorMsg.isEmpty() ? "Failed to apply" : errorMsg; |
| 190 | LOG_MESSAGE(QString("File edit failed: %1 - %2").arg(editId, edit.statusMessage)); |
| 191 | } |
| 192 | |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | bool ChangesManager::rejectFileEdit(const QString &editId) |
| 197 | { |