| 90 | } |
| 91 | |
| 92 | void TextEditor::save() |
| 93 | { |
| 94 | if (!isModified()) |
| 95 | return; |
| 96 | |
| 97 | QFile file(d->fileName); |
| 98 | if (!file.exists()) |
| 99 | return; |
| 100 | |
| 101 | if (!file.open(QFile::WriteOnly | QFile::Text | QFile::Truncate)) { |
| 102 | DDialog dialog; |
| 103 | dialog.setIcon(QIcon::fromTheme("dialog-warning")); |
| 104 | dialog.setWindowTitle(tr("Save File")); |
| 105 | QString msg(tr("The file \"%1\" has no write permission. Please add write permission and try again")); |
| 106 | dialog.setMessage(msg.arg(d->fileName)); |
| 107 | dialog.addButton(tr("Ok", "button"), true, DDialog::ButtonRecommend); |
| 108 | dialog.exec(); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | QByteArray fileContent = text().toLocal8Bit(); |
| 113 | if (!fileContent.isEmpty()) { |
| 114 | QByteArray Outdata; |
| 115 | DetectCode::changeFileEncodingFormat(fileContent, Outdata, QString("UTF-8"), d->documentEncode); |
| 116 | if (Outdata.isEmpty()) { |
| 117 | qWarning() << qPrintable(QString("iconv Encode Transformat from '%1' to '%2' Fail! start QTextCodec Encode Transformat.") |
| 118 | .arg(QString("UTF-8"), d->documentEncode)); |
| 119 | // Using QTextCodec to convert |
| 120 | QTextCodec *codec = QTextCodec::codecForName(d->documentEncode.toUtf8()); |
| 121 | if (codec) { |
| 122 | QByteArray encodedString = codec->fromUnicode(fileContent); |
| 123 | if (encodedString.isEmpty()) { |
| 124 | qWarning() << qPrintable("Both iconv and QTextCodec Encode Transformat Fail!"); |
| 125 | } else { |
| 126 | qWarning() << qPrintable(QString("QTextCodec Encode Transformat from '%1' to '%2' Success!") |
| 127 | .arg(QString("UTF-8"), d->documentEncode)); |
| 128 | Outdata = encodedString; |
| 129 | } |
| 130 | } else { |
| 131 | qWarning() << qPrintable("Unsupported QTextCodec format:") << d->documentEncode; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if (!Outdata.isEmpty()) |
| 136 | file.write(Outdata); |
| 137 | } else { |
| 138 | file.write(fileContent); |
| 139 | } |
| 140 | |
| 141 | file.close(); |
| 142 | setModified(false); |
| 143 | editor.fileSaved(d->fileName); |
| 144 | } |
| 145 | |
| 146 | void TextEditor::saveAs() |
| 147 | { |
no test coverage detected