| 1 |
/*************************************************************************** |
| 2 |
* Copyright (C) 2013~2013 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 "filelistmodel.h" |
| 21 |
#include "common.h" |
| 22 |
#include "fcitx-config/xdg.h" |
| 23 |
|
| 24 |
fcitx::FileListModel::FileListModel(QObject *parent) |
| 25 |
: QAbstractListModel(parent) { |
| 26 |
loadFileList(); |
| 27 |
} |
| 28 |
|
| 29 |
fcitx::FileListModel::~FileListModel() {} |
| 30 |
|
| 31 |
int fcitx::FileListModel::rowCount(const QModelIndex &) const { |
| 32 |
return m_fileList.size(); |
| 33 |
} |
| 34 |
|
| 35 |
QVariant fcitx::FileListModel::data(const QModelIndex &index, int role) const { |
| 36 |
if (!index.isValid() || index.row() >= m_fileList.size()) |
| 37 |
return QVariant(); |
| 38 |
|
| 39 |
switch (role) { |
| 40 |
case Qt::DisplayRole: |
| 41 |
if (m_fileList[index.row()] == QUICK_PHRASE_CONFIG_FILE) { |
| 42 |
return _("Default"); |
| 43 |
} else { |
| 44 |
// remove "data/quickphrase.d/" |
| 45 |
const size_t length = strlen(QUICK_PHRASE_CONFIG_DIR); |
| 46 |
return m_fileList[index.row()].mid(length + 1, |
| 47 |
m_fileList[index.row()].size() - |
| 48 |
length - strlen(".mb") - 1); |
| 49 |
} |
| 50 |
case Qt::UserRole: |
| 51 |
return m_fileList[index.row()]; |
| 52 |
default: |
| 53 |
break; |
| 54 |
} |
| 55 |
return QVariant(); |
| 56 |
} |
| 57 |
|
| 58 |
void fcitx::FileListModel::loadFileList() { |
| 59 |
beginResetModel(); |
| 60 |
m_fileList.clear(); |
| 61 |
m_fileList.append(QUICK_PHRASE_CONFIG_FILE); |
| 62 |
FcitxStringHashSet *files = |
| 63 |
FcitxXDGGetFiles(QUICK_PHRASE_CONFIG_DIR, NULL, ".mb"); |
| 64 |
|
| 65 |
HASH_SORT(files, fcitx_utils_string_hash_set_compare); |
| 66 |
HASH_FOREACH(f, files, FcitxStringHashSet) { |
| 67 |
m_fileList.append(QString::fromLocal8Bit(f->name).prepend( |
| 68 |
QUICK_PHRASE_CONFIG_DIR "/")); |
| 69 |
} |
| 70 |
fcitx_utils_free_string_hash_set(files); |
| 71 |
|
| 72 |
endResetModel(); |
| 73 |
} |
| 74 |
|
| 75 |
int fcitx::FileListModel::findFile(const QString &lastFileName) { |
| 76 |
int idx = m_fileList.indexOf(lastFileName); |
| 77 |
if (idx < 0) { |
| 78 |
return 0; |
| 79 |
} |
| 80 |
return idx; |
| 81 |
} |