| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2012~2012 by CSSlayer * |
| 3 |
* [email protected] * |
| 4 |
* * |
| 5 |
* This program is free software: you can redistribute it and/or modify * |
| 6 |
* it under the terms of the GNU General Public License as published by * |
| 7 |
* the Free Software Foundation, either version 3 of the License, or * |
| 8 |
* (at your option) any later version. * |
| 9 |
* * |
| 10 |
* This program is distributed in the hope that it will be useful, * |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 |
* GNU General Public License for more details. * |
| 14 |
* * |
| 15 |
* You should have received a copy of the GNU General Public License * |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 17 |
* * |
| 18 |
***************************************************************************/ |
| 19 |
|
| 20 |
#include <fcitx-config/xdg.h> |
| 21 |
#include <libintl.h> |
| 22 |
|
| 23 |
#include <QCloseEvent> |
| 24 |
#include <QDebug> |
| 25 |
#include <QFileDialog> |
| 26 |
#include <QInputDialog> |
| 27 |
#include <QMenu> |
| 28 |
#include <QMessageBox> |
| 29 |
#include <cassert> |
| 30 |
#include <cstdio> |
| 31 |
#include <qtconcurrentrun.h> |
| 32 |
|
| 33 |
#include "batchdialog.h" |
| 34 |
#include "common.h" |
| 35 |
#include "editor.h" |
| 36 |
#include "editordialog.h" |
| 37 |
#include "filelistmodel.h" |
| 38 |
#include "model.h" |
| 39 |
#include "ui_editor.h" |
| 40 |
|
| 41 |
namespace fcitx { |
| 42 |
|
| 43 |
ListEditor::ListEditor(QWidget *parent) |
| 44 |
: FcitxQtConfigUIWidget(parent), m_ui(new Ui::Editor), |
| 45 |
m_model(new QuickPhraseModel(this)), |
| 46 |
m_fileListModel(new FileListModel(this)) { |
| 47 |
m_ui->setupUi(this); |
| 48 |
m_ui->addButton->setText(_("&Add")); |
| 49 |
m_ui->batchEditButton->setText(_("&Batch Edit")); |
| 50 |
m_ui->deleteButton->setText(_("&Delete")); |
| 51 |
m_ui->clearButton->setText(_("De&lete All")); |
| 52 |
m_ui->importButton->setText(_("&Import")); |
| 53 |
m_ui->exportButton->setText(_("E&xport")); |
| 54 |
m_ui->operationButton->setText(_("&Operation")); |
| 55 |
m_ui->macroTableView->setSelectionMode(QAbstractItemView::SingleSelection); |
| 56 |
m_ui->macroTableView->setSelectionBehavior(QAbstractItemView::SelectRows); |
| 57 |
m_ui->macroTableView->setEditTriggers(QAbstractItemView::DoubleClicked); |
| 58 |
|
| 59 |
m_ui->macroTableView->horizontalHeader()->setStretchLastSection(true); |
| 60 |
m_ui->macroTableView->verticalHeader()->setVisible(false); |
| 61 |
m_ui->macroTableView->setModel(m_model); |
| 62 |
m_ui->fileListComboBox->setModel(m_fileListModel); |
| 63 |
|
| 64 |
m_operationMenu = new QMenu(this); |
| 65 |
m_operationMenu->addAction(_("Add File"), this, SLOT(addFileTriggered())); |
| 66 |
m_operationMenu->addAction(_("Remove File"), this, |
| 67 |
SLOT(removeFileTriggered())); |
| 68 |
m_operationMenu->addAction(_("Refresh List"), this, |
| 69 |
SLOT(refreshListTriggered())); |
| 70 |
m_ui->operationButton->setMenu(m_operationMenu); |
| 71 |
|
| 72 |
loadFileList(); |
| 73 |
itemFocusChanged(); |
| 74 |
|
| 75 |
connect(m_ui->addButton, SIGNAL(clicked(bool)), this, SLOT(addWord())); |
| 76 |
connect(m_ui->batchEditButton, SIGNAL(clicked(bool)), this, |
| 77 |
SLOT(batchEditWord())); |
| 78 |
connect(m_ui->deleteButton, SIGNAL(clicked(bool)), this, |
| 79 |
SLOT(deleteWord())); |
| 80 |
connect(m_ui->clearButton, SIGNAL(clicked(bool)), this, |
| 81 |
SLOT(deleteAllWord())); |
| 82 |
connect(m_ui->importButton, SIGNAL(clicked(bool)), this, |
| 83 |
SLOT(importData())); |
| 84 |
connect(m_ui->exportButton, SIGNAL(clicked(bool)), this, |
| 85 |
SLOT(exportData())); |
| 86 |
|
| 87 |
connect(m_ui->fileListComboBox, SIGNAL(activated(int)), this, |
| 88 |
SLOT(changeFile(int))); |
| 89 |
|
| 90 |
connect(m_ui->macroTableView->selectionModel(), |
| 91 |
SIGNAL(selectionChanged(QItemSelection, QItemSelection)), this, |
| 92 |
SLOT(itemFocusChanged())); |
| 93 |
connect(m_model, SIGNAL(needSaveChanged(bool)), this, |
| 94 |
SIGNAL(changed(bool))); |
| 95 |
} |
| 96 |
|
| 97 |
ListEditor::~ListEditor() { delete m_ui; } |
| 98 |
|
| 99 |
void ListEditor::load() { |
| 100 |
m_lastFile = currentFile(); |
| 101 |
m_model->load(currentFile(), false); |
| 102 |
} |
| 103 |
|
| 104 |
void ListEditor::load(const QString &file) { m_model->load(file, true); } |
| 105 |
|
| 106 |
void ListEditor::save(const QString &file) { m_model->save(file); } |
| 107 |
|
| 108 |
void ListEditor::save() { |
| 109 |
// QFutureWatcher< bool >* futureWatcher = |
| 110 |
// m_model->save("data/QuickPhrase.mb"); |
| 111 |
QFutureWatcher<bool> *futureWatcher = m_model->save(currentFile()); |
| 112 |
connect(futureWatcher, SIGNAL(finished()), this, SIGNAL(saveFinished())); |
| 113 |
} |
| 114 |
|
| 115 |
QString ListEditor::addon() { return "fcitx-quickphrase"; } |
| 116 |
|
| 117 |
bool ListEditor::asyncSave() { return true; } |
| 118 |
|
| 119 |
void ListEditor::changeFile(int) { |
| 120 |
if (m_model->needSave()) { |
| 121 |
int ret = QMessageBox::question( |
| 122 |
this, _("Save Changes"), |
| 123 |
_("The content has changed.\n" |
| 124 |
"Do you want to save the changes or discard them?"), |
| 125 |
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel); |
| 126 |
if (ret == QMessageBox::Save) { |
| 127 |
// save(m_ui->fileListComboBox->itemText(lastFileIndex)); |
| 128 |
save(m_lastFile); |
| 129 |
} else if (ret == QMessageBox::Cancel) { |
| 130 |
m_ui->fileListComboBox->setCurrentIndex( |
| 131 |
m_fileListModel->findFile(m_lastFile)); |
| 132 |
return; |
| 133 |
} |
| 134 |
} |
| 135 |
load(); |
| 136 |
} |
| 137 |
|
| 138 |
QString ListEditor::title() { return _("Quick Phrase Editor"); } |
| 139 |
|
| 140 |
void ListEditor::itemFocusChanged() { |
| 141 |
m_ui->deleteButton->setEnabled( |
| 142 |
m_ui->macroTableView->currentIndex().isValid()); |
| 143 |
} |
| 144 |
|
| 145 |
void ListEditor::deleteWord() { |
| 146 |
if (!m_ui->macroTableView->currentIndex().isValid()) |
| 147 |
return; |
| 148 |
int row = m_ui->macroTableView->currentIndex().row(); |
| 149 |
m_model->deleteItem(row); |
| 150 |
} |
| 151 |
|
| 152 |
void ListEditor::deleteAllWord() { m_model->deleteAllItem(); } |
| 153 |
|
| 154 |
void ListEditor::addWord() { |
| 155 |
EditorDialog *dialog = new EditorDialog(this); |
| 156 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 157 |
dialog->open(); |
| 158 |
connect(dialog, SIGNAL(accepted()), this, SLOT(addWordAccepted())); |
| 159 |
} |
| 160 |
|
| 161 |
void ListEditor::batchEditWord() { |
| 162 |
BatchDialog *dialog = new BatchDialog(this); |
| 163 |
QString text; |
| 164 |
QTextStream stream(&text); |
| 165 |
m_model->saveData(stream); |
| 166 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 167 |
dialog->setText(text); |
| 168 |
dialog->open(); |
| 169 |
connect(dialog, SIGNAL(accepted()), this, SLOT(batchEditAccepted())); |
| 170 |
} |
| 171 |
|
| 172 |
void ListEditor::addWordAccepted() { |
| 173 |
const EditorDialog *dialog = |
| 174 |
qobject_cast<const EditorDialog *>(QObject::sender()); |
| 175 |
|
| 176 |
m_model->addItem(dialog->key(), dialog->value()); |
| 177 |
QModelIndex last = m_model->index(m_model->rowCount() - 1, 0); |
| 178 |
m_ui->macroTableView->setCurrentIndex(last); |
| 179 |
m_ui->macroTableView->scrollTo(last); |
| 180 |
} |
| 181 |
|
| 182 |
void ListEditor::batchEditAccepted() { |
| 183 |
const BatchDialog *dialog = |
| 184 |
qobject_cast<const BatchDialog *>(QObject::sender()); |
| 185 |
|
| 186 |
QString s = dialog->text(); |
| 187 |
QTextStream stream(&s); |
| 188 |
|
| 189 |
m_model->loadData(stream); |
| 190 |
QModelIndex last = m_model->index(m_model->rowCount() - 1, 0); |
| 191 |
m_ui->macroTableView->setCurrentIndex(last); |
| 192 |
m_ui->macroTableView->scrollTo(last); |
| 193 |
} |
| 194 |
|
| 195 |
void ListEditor::importData() { |
| 196 |
QFileDialog *dialog = new QFileDialog(this); |
| 197 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 198 |
dialog->setFileMode(QFileDialog::ExistingFile); |
| 199 |
dialog->setAcceptMode(QFileDialog::AcceptOpen); |
| 200 |
dialog->open(); |
| 201 |
connect(dialog, SIGNAL(accepted()), this, SLOT(importFileSelected())); |
| 202 |
} |
| 203 |
|
| 204 |
void ListEditor::exportData() { |
| 205 |
QFileDialog *dialog = new QFileDialog(this); |
| 206 |
dialog->setAttribute(Qt::WA_DeleteOnClose, true); |
| 207 |
dialog->setAcceptMode(QFileDialog::AcceptSave); |
| 208 |
dialog->open(); |
| 209 |
connect(dialog, SIGNAL(accepted()), this, SLOT(exportFileSelected())); |
| 210 |
} |
| 211 |
|
| 212 |
void ListEditor::importFileSelected() { |
| 213 |
const QFileDialog *dialog = |
| 214 |
qobject_cast<const QFileDialog *>(QObject::sender()); |
| 215 |
if (dialog->selectedFiles().length() <= 0) |
| 216 |
return; |
| 217 |
QString file = dialog->selectedFiles()[0]; |
| 218 |
load(file); |
| 219 |
} |
| 220 |
|
| 221 |
void ListEditor::exportFileSelected() { |
| 222 |
const QFileDialog *dialog = |
| 223 |
qobject_cast<const QFileDialog *>(QObject::sender()); |
| 224 |
if (dialog->selectedFiles().length() <= 0) |
| 225 |
return; |
| 226 |
QString file = dialog->selectedFiles()[0]; |
| 227 |
save(file); |
| 228 |
} |
| 229 |
|
| 230 |
void ListEditor::loadFileList() { |
| 231 |
int row = m_ui->fileListComboBox->currentIndex(); |
| 232 |
int col = m_ui->fileListComboBox->modelColumn(); |
| 233 |
QString lastFileName = |
| 234 |
m_fileListModel->data(m_fileListModel->index(row, col), Qt::UserRole) |
| 235 |
.toString(); |
| 236 |
m_fileListModel->loadFileList(); |
| 237 |
m_ui->fileListComboBox->setCurrentIndex( |
| 238 |
m_fileListModel->findFile(lastFileName)); |
| 239 |
load(); |
| 240 |
} |
| 241 |
|
| 242 |
QString ListEditor::currentFile() { |
| 243 |
int row = m_ui->fileListComboBox->currentIndex(); |
| 244 |
int col = m_ui->fileListComboBox->modelColumn(); |
| 245 |
return m_fileListModel->data(m_fileListModel->index(row, col), Qt::UserRole) |
| 246 |
.toString(); |
| 247 |
} |
| 248 |
|
| 249 |
QString ListEditor::currentName() { |
| 250 |
int row = m_ui->fileListComboBox->currentIndex(); |
| 251 |
int col = m_ui->fileListComboBox->modelColumn(); |
| 252 |
return m_fileListModel |
| 253 |
->data(m_fileListModel->index(row, col), Qt::DisplayRole) |
| 254 |
.toString(); |
| 255 |
} |
| 256 |
|
| 257 |
void ListEditor::addFileTriggered() { |
| 258 |
bool ok; |
| 259 |
QString filename = QInputDialog::getText( |
| 260 |
this, _("Create new file"), _("Please input a filename for newfile"), |
| 261 |
QLineEdit::Normal, "newfile", &ok); |
| 262 |
|
| 263 |
if (filename.contains('/')) { |
| 264 |
QMessageBox::warning(this, _("Invalid filename"), |
| 265 |
_("File name should not contain '/'.")); |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
std::FILE *file = FcitxXDGGetFileUserWithPrefix( |
| 270 |
QUICK_PHRASE_CONFIG_DIR, filename.append(".mb").toLocal8Bit(), "w", |
| 271 |
NULL); |
| 272 |
|
| 273 |
if (file) { |
| 274 |
fclose(file); |
| 275 |
} else { |
| 276 |
QMessageBox::warning(this, _("File Operation Failed"), |
| 277 |
_("Cannot create file %1.").arg(filename)); |
| 278 |
return; |
| 279 |
} |
| 280 |
|
| 281 |
m_fileListModel->loadFileList(); |
| 282 |
m_ui->fileListComboBox->setCurrentIndex(m_fileListModel->findFile( |
| 283 |
filename.prepend(QUICK_PHRASE_CONFIG_DIR "/"))); |
| 284 |
load(); |
| 285 |
} |
| 286 |
|
| 287 |
void ListEditor::refreshListTriggered() { loadFileList(); } |
| 288 |
|
| 289 |
void ListEditor::removeFileTriggered() { |
| 290 |
QString filename = currentFile(); |
| 291 |
QString curName = currentName(); |
| 292 |
char *fullname = NULL; |
| 293 |
FcitxXDGGetFileUserWithPrefix(NULL, filename.toLocal8Bit(), NULL, |
| 294 |
&fullname); |
| 295 |
QFile f(fullname); |
| 296 |
free(fullname); |
| 297 |
if (!f.exists()) { |
| 298 |
int ret = QMessageBox::question( |
| 299 |
this, _("Cannot remove system file"), |
| 300 |
_("%1 is a system file, do you want to delete all phrases instead?") |
| 301 |
.arg(curName), |
| 302 |
QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); |
| 303 |
if (ret == QMessageBox::Yes) { |
| 304 |
deleteAllWord(); |
| 305 |
} |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
int ret = |
| 310 |
QMessageBox::question(this, _("Confirm deletion"), |
| 311 |
_("Are you sure to delete %1?").arg(curName), |
| 312 |
QMessageBox::Ok | QMessageBox::Cancel); |
| 313 |
|
| 314 |
if (ret == QMessageBox::Ok) { |
| 315 |
bool ok = f.remove(); |
| 316 |
if (!ok) { |
| 317 |
QMessageBox::warning(this, _("File Operation Failed"), |
| 318 |
_("Error while deleting %1.").arg(curName)); |
| 319 |
} |
| 320 |
} |
| 321 |
loadFileList(); |
| 322 |
load(); |
| 323 |
} |
| 324 |
} // namespace fcitx |