| 1 | // -*- c-basic-offset: 2 -*- |
|---|
| 2 | /* |
|---|
| 3 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 4 | * Copyright (C) 2003-2006 Licq developers |
|---|
| 5 | * |
|---|
| 6 | * Licq is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * Licq is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with Licq; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #include "editfilelistdlg.h" |
|---|
| 22 | |
|---|
| 23 | #include "config.h" |
|---|
| 24 | |
|---|
| 25 | #include <QDialogButtonBox> |
|---|
| 26 | #include <QHBoxLayout> |
|---|
| 27 | #include <QListWidget> |
|---|
| 28 | #include <QPushButton> |
|---|
| 29 | |
|---|
| 30 | #include "helpers/support.h" |
|---|
| 31 | |
|---|
| 32 | using namespace LicqQtGui; |
|---|
| 33 | /* TRANSLATOR LicqQtGui::EditFileListDlg */ |
|---|
| 34 | |
|---|
| 35 | EditFileListDlg::EditFileListDlg(ConstFileList* fileList, QWidget* parent) |
|---|
| 36 | : QDialog(parent), |
|---|
| 37 | myFileList(fileList) |
|---|
| 38 | { |
|---|
| 39 | Support::setWidgetProps(this, "EditFileListDlg"); |
|---|
| 40 | setAttribute(Qt::WA_DeleteOnClose, true); |
|---|
| 41 | setWindowTitle(tr("Licq - Files to send")); |
|---|
| 42 | setModal(true); |
|---|
| 43 | |
|---|
| 44 | QHBoxLayout* lay = new QHBoxLayout(this); |
|---|
| 45 | |
|---|
| 46 | lstFiles = new QListWidget(); |
|---|
| 47 | lstFiles->setMinimumWidth(400); |
|---|
| 48 | lay->addWidget(lstFiles); |
|---|
| 49 | |
|---|
| 50 | QDialogButtonBox* buttonBox = new QDialogButtonBox(Qt::Vertical); |
|---|
| 51 | |
|---|
| 52 | btnDone = buttonBox->addButton(QDialogButtonBox::Ok); |
|---|
| 53 | btnDone->setText(tr("D&one")); |
|---|
| 54 | connect(btnDone, SIGNAL(clicked()), SLOT(close())); |
|---|
| 55 | |
|---|
| 56 | btnUp = new QPushButton(tr("&Up")); |
|---|
| 57 | btnDown = new QPushButton(tr("&Down")); |
|---|
| 58 | btnDelete = new QPushButton(tr("D&elete")); |
|---|
| 59 | |
|---|
| 60 | buttonBox->addButton(btnUp, QDialogButtonBox::ActionRole); |
|---|
| 61 | buttonBox->addButton(btnDown, QDialogButtonBox::ActionRole); |
|---|
| 62 | buttonBox->addButton(btnDelete, QDialogButtonBox::ActionRole); |
|---|
| 63 | |
|---|
| 64 | connect(btnUp, SIGNAL(clicked()), SLOT(up())); |
|---|
| 65 | connect(btnDown, SIGNAL(clicked()), SLOT(down())); |
|---|
| 66 | connect(btnDelete, SIGNAL(clicked()), SLOT(remove())); |
|---|
| 67 | |
|---|
| 68 | lay->addWidget(buttonBox); |
|---|
| 69 | |
|---|
| 70 | connect(lstFiles, SIGNAL(currentRowChanged(int)), SLOT(currentChanged(int))); |
|---|
| 71 | |
|---|
| 72 | refreshList(); |
|---|
| 73 | |
|---|
| 74 | show(); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | void EditFileListDlg::refreshList() |
|---|
| 78 | { |
|---|
| 79 | ConstFileList::iterator it = myFileList->begin(); |
|---|
| 80 | |
|---|
| 81 | lstFiles->clear(); |
|---|
| 82 | |
|---|
| 83 | for (; it != myFileList->end(); it++) |
|---|
| 84 | lstFiles->addItem(QString::fromLocal8Bit(*it)); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void EditFileListDlg::moveCurrentItem(bool up) |
|---|
| 88 | { |
|---|
| 89 | int i = 0; |
|---|
| 90 | int n = lstFiles->currentRow(); |
|---|
| 91 | int newRow = up ? n - 1 : n + 1; |
|---|
| 92 | ConstFileList::iterator it = myFileList->begin(); |
|---|
| 93 | |
|---|
| 94 | if ((up && n == 0) || (!up && n == lstFiles->count() - 1)) |
|---|
| 95 | return; |
|---|
| 96 | |
|---|
| 97 | for (; i != n && it != myFileList->end(); it++, i++) |
|---|
| 98 | ; |
|---|
| 99 | |
|---|
| 100 | if (i == n) |
|---|
| 101 | { |
|---|
| 102 | const char* s = *it; |
|---|
| 103 | |
|---|
| 104 | it = myFileList->erase(it); |
|---|
| 105 | myFileList->insert(up ? --it : ++it, s); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | lstFiles->insertItem(newRow, lstFiles->takeItem(n)); |
|---|
| 109 | lstFiles->setCurrentRow(newRow); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void EditFileListDlg::currentChanged(int newCurrent) |
|---|
| 113 | { |
|---|
| 114 | btnUp->setEnabled(newCurrent > 0); |
|---|
| 115 | btnDown->setEnabled(newCurrent >= 0 && newCurrent < lstFiles->count() - 1); |
|---|
| 116 | btnDelete->setEnabled(newCurrent != -1); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void EditFileListDlg::up() |
|---|
| 120 | { |
|---|
| 121 | moveCurrentItem(true); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | void EditFileListDlg::down() |
|---|
| 125 | { |
|---|
| 126 | moveCurrentItem(false); |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void EditFileListDlg::remove() |
|---|
| 130 | { |
|---|
| 131 | int i = 0; |
|---|
| 132 | int n = lstFiles->currentRow(); |
|---|
| 133 | ConstFileList::iterator it = myFileList->begin(); |
|---|
| 134 | |
|---|
| 135 | for (; i != n && it != myFileList->end(); it++, i++) |
|---|
| 136 | ; |
|---|
| 137 | |
|---|
| 138 | if (i == n) |
|---|
| 139 | { |
|---|
| 140 | free((void*)*it); |
|---|
| 141 | myFileList->erase(it); |
|---|
| 142 | emit fileDeleted(myFileList->size()); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | delete lstFiles->takeItem(n--); |
|---|
| 146 | lstFiles->setCurrentRow(n); |
|---|
| 147 | currentChanged(n); |
|---|
| 148 | } |
|---|