aboutsummaryrefslogtreecommitdiffstats
path: root/src/ui/widgets/TextEditTabWidget.cpp
blob: c782e8682695689bab293f2f3dfcc099f2a366bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
 * Copyright (C) 2021-2024 Saturneric <[email protected]>
 *
 * This file is part of GpgFrontend.
 *
 * GpgFrontend is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GpgFrontend is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
 *
 * The initial version of the source code is inherited from
 * the gpg4usb project, which is under GPL-3.0-or-later.
 *
 * All the source code of GpgFrontend was modified and released by
 * Saturneric <[email protected]> starting on May 12, 2021.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#include "TextEditTabWidget.h"

#include "core/function/GlobalSettingStation.h"
#include "core/model/CacheObject.h"
#include "ui/widgets/PlainTextEditorPage.h"
#include "widgets/FilePage.h"

namespace GpgFrontend::UI {

TextEditTabWidget::TextEditTabWidget(QWidget* parent) : QTabWidget(parent) {
  setAcceptDrops(true);
}

void TextEditTabWidget::dragEnterEvent(QDragEnterEvent* event) {
  event->acceptProposedAction();
}

void TextEditTabWidget::dropEvent(QDropEvent* event) {
  if (!event->mimeData()->hasUrls()) return;

  auto urls = event->mimeData()->urls();

  for (const auto& url : urls) {
    QString local_file = url.toLocalFile();

    QFileInfo file_info(local_file);
    if (file_info.size() > static_cast<qint64>(1024 * 1024)) {
      QMessageBox::warning(
          this, tr("File Too Large"),
          tr("The file \"%1\" is larger than 1MB and will not be opened.")
              .arg(file_info.fileName()));
      continue;
    }

    QFile file(local_file);
    if (!file.open(QIODevice::ReadOnly)) {
      QMessageBox::warning(
          this, tr("File Open Error"),
          tr("The file \"%1\" could not be opened.").arg(file_info.fileName()));
      continue;
    }
    QByteArray file_data = file.read(1024);
    file.close();

    if (file_data.contains('\0')) {
      QMessageBox::warning(this, tr("Binary File Detected"),
                           tr("The file \"%1\" appears to be a binary file "
                              "and will not be opened.")
                               .arg(file_info.fileName()));
      continue;
    }

    SlotOpenFile(local_file);
  }

  event->acceptProposedAction();
}

void TextEditTabWidget::SlotOpenFile(const QString& path) {
  QFile file(path);
  auto result = file.open(QIODevice::ReadOnly | QIODevice::Text);
  if (result) {
    auto* page = new PlainTextEditorPage(path);
    connect(page->GetTextPage()->document(),
            &QTextDocument::modificationChanged, this,
            &TextEditTabWidget::SlotShowModified);
    // connect to cache recovery fucntion
    connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged,
            this, &TextEditTabWidget::slot_save_status_to_cache_for_recovery);

    QApplication::setOverrideCursor(Qt::WaitCursor);
    auto index = this->addTab(page, stripped_name(path));
    this->setTabIcon(index, QIcon(":/icons/file.png"));
    this->setCurrentIndex(this->count() - 1);
    QApplication::restoreOverrideCursor();
    page->GetTextPage()->setFocus();
    page->ReadFile();
  } else {
    QMessageBox::warning(
        this, tr("Warning"),
        tr("Cannot read file %1:\n%2.").arg(path).arg(file.errorString()));
  }

  file.close();
}
void TextEditTabWidget::SlotShowModified(bool changed) {
  // get current tab
  int index = this->currentIndex();
  QString title = this->tabText(index);

  // if changed
  if (!changed) {
    this->setTabText(index, title.remove(0, 2));
    return;
  }

  // if doc is modified now, add leading * to title,
  // otherwise remove the leading * from the title
  if (CurTextPage()->GetTextPage()->document()->isModified()) {
    this->setTabText(index, title.trimmed().prepend("* "));
  } else {
    this->setTabText(index, title.remove(0, 2));
  }
}
auto TextEditTabWidget::CurTextPage() const -> PlainTextEditorPage* {
  return qobject_cast<PlainTextEditorPage*>(this->currentWidget());
}

auto TextEditTabWidget::SlotCurPageTextEdit() -> PlainTextEditorPage* {
  auto* cur_page = qobject_cast<PlainTextEditorPage*>(this->currentWidget());
  return cur_page;
}

auto TextEditTabWidget::CurFilePage() const -> FilePage* {
  auto* cur_file_page = qobject_cast<FilePage*>(this->currentWidget());
  if (cur_file_page != nullptr) {
    return cur_file_page;
  }
  return nullptr;
}

auto TextEditTabWidget::stripped_name(const QString& full_file_name)
    -> QString {
  return QFileInfo(full_file_name).fileName();
}

void TextEditTabWidget::slot_save_status_to_cache_for_recovery() {
  if (this->text_page_data_modified_count_++ % 8 != 0) return;

  auto settings = GlobalSettingStation::GetInstance().GetSettings();

  bool restore_text_editor_page =
      settings.value("basic/restore_text_editor_page", false).toBool();
  if (!restore_text_editor_page) {
    FLOG_D("restore_text_editor_page is false, ignoring...");
    return;
  }

  int tab_count = this->count();
  std::vector<std::tuple<int, QString, QString>> unsaved_pages;

  for (int i = 0; i < tab_count; i++) {
    auto* target_page = qobject_cast<PlainTextEditorPage*>(this->widget(i));

    // if this page is no textedit, there should be nothing to save
    if (target_page == nullptr) {
      continue;
    }

    auto* document = target_page->GetTextPage()->document();
    auto tab_title = this->tabText(i);
    if (!target_page->ReadDone() || !target_page->isEnabled() ||
        !document->isModified()) {
      continue;
    }

    unsaved_pages.emplace_back(i, tab_title, document->toRawText());
  }

  CacheObject cache("editor_unsaved_pages");
  QJsonArray unsaved_page_array;
  for (const auto& page : unsaved_pages) {
    const auto [index, title, content] = page;

    QJsonObject page_json;
    page_json["index"] = index;
    page_json["title"] = title;
    page_json["content"] = content;

    unsaved_page_array.push_back(page_json);
  }

  cache.setArray(unsaved_page_array);
}

void TextEditTabWidget::SlotNewTab() {
  QString header = tr("untitled") + QString::number(++count_page_) + ".txt";

  auto* page = new PlainTextEditorPage();
  auto index = this->addTab(page, header);
  this->setTabIcon(index, QIcon(":/icons/file.png"));
  this->setCurrentIndex(this->count() - 1);
  page->GetTextPage()->setFocus();
  connect(page->GetTextPage()->document(), &QTextDocument::modificationChanged,
          this, &TextEditTabWidget::SlotShowModified);
  connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged,
          this, &TextEditTabWidget::slot_save_status_to_cache_for_recovery);
}
void TextEditTabWidget::SlotNewTabWithContent(QString title,
                                              const QString& content) {
  QString header = tr("untitled") + QString::number(++count_page_) + ".txt";
  if (!title.isEmpty()) {
    // modify title
    if (!title.isEmpty() && title[0] == '*') {
      title.remove(0, 1);
    }
    // set title
    header = title;
  }

  auto* page = new PlainTextEditorPage();
  auto index = this->addTab(page, header);
  this->setTabIcon(index, QIcon(":/icons/file.png"));
  this->setCurrentIndex(this->count() - 1);
  page->GetTextPage()->setFocus();
  connect(page->GetTextPage()->document(), &QTextDocument::modificationChanged,
          this, &TextEditTabWidget::SlotShowModified);
  connect(page->GetTextPage()->document(), &QTextDocument::contentsChanged,
          this, &TextEditTabWidget::slot_save_status_to_cache_for_recovery);

  // set content with modified status
  page->GetTextPage()->document()->setPlainText(content);
}
}  // namespace GpgFrontend::UI