| 1 | /* |
|---|
| 2 | * This file is part of Licq, an instant messaging client for UNIX. |
|---|
| 3 | * Copyright (C) 1999-2006 Licq developers |
|---|
| 4 | * |
|---|
| 5 | * Licq 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 2 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * Licq 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 Licq; if not, write to the Free Software |
|---|
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #include "editgrpdlg.h" |
|---|
| 21 | |
|---|
| 22 | #include "config.h" |
|---|
| 23 | |
|---|
| 24 | #include <QDialogButtonBox> |
|---|
| 25 | #include <QGridLayout> |
|---|
| 26 | #include <QGroupBox> |
|---|
| 27 | #include <QLabel> |
|---|
| 28 | #include <QLineEdit> |
|---|
| 29 | #include <QListWidget> |
|---|
| 30 | #include <QPushButton> |
|---|
| 31 | #include <QVBoxLayout> |
|---|
| 32 | |
|---|
| 33 | #include <licq_events.h> |
|---|
| 34 | #include <licq_user.h> |
|---|
| 35 | |
|---|
| 36 | #include "core/licqgui.h" |
|---|
| 37 | #include "core/messagebox.h" |
|---|
| 38 | #include "core/signalmanager.h" |
|---|
| 39 | |
|---|
| 40 | #include "helpers/licqstrings.h" |
|---|
| 41 | #include "helpers/support.h" |
|---|
| 42 | |
|---|
| 43 | using namespace LicqQtGui; |
|---|
| 44 | /* TRANSLATOR LicqQtGui::EditGrpDlg */ |
|---|
| 45 | |
|---|
| 46 | EditGrpDlg::EditGrpDlg(QWidget* parent) |
|---|
| 47 | : QDialog(parent) |
|---|
| 48 | { |
|---|
| 49 | Support::setWidgetProps(this, "EditGroupDialog"); |
|---|
| 50 | setWindowTitle(tr("Licq - Edit Groups")); |
|---|
| 51 | |
|---|
| 52 | QVBoxLayout* lay = new QVBoxLayout(this); |
|---|
| 53 | grpGroups = new QGroupBox(tr("Groups")); |
|---|
| 54 | lay->addWidget(grpGroups); |
|---|
| 55 | |
|---|
| 56 | QGridLayout* glay = new QGridLayout(grpGroups); |
|---|
| 57 | lstGroups = new QListWidget(grpGroups); |
|---|
| 58 | glay->addWidget(lstGroups, 0, 0); |
|---|
| 59 | |
|---|
| 60 | QVBoxLayout* vlay = new QVBoxLayout(); |
|---|
| 61 | #define BUTTON(var, name, slot) \ |
|---|
| 62 | var = new QPushButton(name, grpGroups); \ |
|---|
| 63 | connect(var, SIGNAL(clicked()), SLOT(slot())); \ |
|---|
| 64 | vlay->addWidget(var) |
|---|
| 65 | |
|---|
| 66 | BUTTON(btnAdd, tr("Add"), slot_add); |
|---|
| 67 | BUTTON(btnRemove, tr("Remove"), slot_remove); |
|---|
| 68 | BUTTON(btnUp, tr("Shift Up"), slot_up); |
|---|
| 69 | BUTTON(btnDown, tr("Shift Down"), slot_down); |
|---|
| 70 | BUTTON(btnEdit, tr("Edit Name"), slot_edit); |
|---|
| 71 | #undef BUTTON |
|---|
| 72 | vlay->addStretch(1); |
|---|
| 73 | |
|---|
| 74 | btnEdit->setToolTip(tr("Edit group name (hit enter to save).")); |
|---|
| 75 | |
|---|
| 76 | glay->addLayout(vlay, 0, 1); |
|---|
| 77 | |
|---|
| 78 | edtName = new QLineEdit(grpGroups); |
|---|
| 79 | edtName->setEnabled(false); |
|---|
| 80 | connect(edtName, SIGNAL(returnPressed()), SLOT(slot_editok())); |
|---|
| 81 | glay->addWidget(edtName, 1, 0); |
|---|
| 82 | |
|---|
| 83 | btnSave = new QPushButton(tr("&Save")); |
|---|
| 84 | btnSave->setEnabled(false); |
|---|
| 85 | btnSave->setToolTip(tr("Save the name of a group being modified.")); |
|---|
| 86 | connect(btnSave, SIGNAL(clicked()), SLOT(slot_editok())); |
|---|
| 87 | glay->addWidget(btnSave, 1, 1); |
|---|
| 88 | |
|---|
| 89 | QDialogButtonBox* buttons = new QDialogButtonBox(); |
|---|
| 90 | connect(buttons, SIGNAL(rejected()), SLOT(close())); |
|---|
| 91 | btnDone = buttons->addButton(QDialogButtonBox::Close); |
|---|
| 92 | btnDone->setText(tr("&Done")); |
|---|
| 93 | lay->addWidget(buttons); |
|---|
| 94 | |
|---|
| 95 | RefreshList(); |
|---|
| 96 | connect(LicqGui::instance()->signalManager(), |
|---|
| 97 | SIGNAL(updatedList(CICQSignal*)), SLOT(listUpdated(CICQSignal*))); |
|---|
| 98 | |
|---|
| 99 | show(); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | unsigned short EditGrpDlg::currentGroupId() const |
|---|
| 103 | { |
|---|
| 104 | if (lstGroups->currentItem() == NULL) |
|---|
| 105 | return 0; |
|---|
| 106 | |
|---|
| 107 | unsigned short groupId = lstGroups->currentItem()->data(Qt::UserRole).toUInt(); |
|---|
| 108 | return groupId; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void EditGrpDlg::setCurrentGroupId(unsigned short groupId) |
|---|
| 112 | { |
|---|
| 113 | for (int i = 0; i < lstGroups->count(); ++i) |
|---|
| 114 | if (lstGroups->item(i)->data(Qt::UserRole).toUInt() == groupId) |
|---|
| 115 | { |
|---|
| 116 | lstGroups->setCurrentRow(i); |
|---|
| 117 | break; |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | void EditGrpDlg::RefreshList() |
|---|
| 122 | { |
|---|
| 123 | unsigned short groupId = currentGroupId(); |
|---|
| 124 | lstGroups->clear(); |
|---|
| 125 | |
|---|
| 126 | FOR_EACH_GROUP_START_SORTED(LOCK_R) |
|---|
| 127 | { |
|---|
| 128 | QString name = QString::fromLocal8Bit(pGroup->name().c_str()); |
|---|
| 129 | QListWidgetItem* item = new QListWidgetItem(name, lstGroups); |
|---|
| 130 | item->setData(Qt::UserRole, pGroup->id()); |
|---|
| 131 | } |
|---|
| 132 | FOR_EACH_GROUP_END |
|---|
| 133 | |
|---|
| 134 | setCurrentGroupId(groupId); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | void EditGrpDlg::listUpdated(CICQSignal* sig) |
|---|
| 138 | { |
|---|
| 139 | switch (sig->SubSignal()) |
|---|
| 140 | { |
|---|
| 141 | case LIST_GROUP_ADDED: |
|---|
| 142 | case LIST_GROUP_REMOVED: |
|---|
| 143 | case LIST_GROUP_CHANGED: |
|---|
| 144 | case LIST_GROUP_REORDERED: |
|---|
| 145 | |
|---|
| 146 | case LIST_INVALIDATE: |
|---|
| 147 | if (btnSave->isEnabled()) // we are editing the group name |
|---|
| 148 | slot_editcancel(); |
|---|
| 149 | RefreshList(); |
|---|
| 150 | break; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | void EditGrpDlg::slot_add() |
|---|
| 155 | { |
|---|
| 156 | // Don't add group until user has had a chance to set a name for it |
|---|
| 157 | myEditGroupId = 0; |
|---|
| 158 | lstGroups->setCurrentRow(-1); |
|---|
| 159 | |
|---|
| 160 | btnSave->setEnabled(true); |
|---|
| 161 | btnDone->setEnabled(false); |
|---|
| 162 | edtName->setEnabled(true); |
|---|
| 163 | |
|---|
| 164 | edtName->setText(tr("noname")); |
|---|
| 165 | edtName->setFocus(); |
|---|
| 166 | edtName->selectAll(); |
|---|
| 167 | btnEdit->setText(tr("Cancel")); |
|---|
| 168 | disconnect(btnEdit, SIGNAL(clicked()), this, SLOT(slot_edit())); |
|---|
| 169 | connect(btnEdit, SIGNAL(clicked()), SLOT(slot_editcancel())); |
|---|
| 170 | lstGroups->setEnabled(false); |
|---|
| 171 | btnSave->setDefault(true); |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | void EditGrpDlg::slot_remove() |
|---|
| 175 | { |
|---|
| 176 | unsigned short groupId = currentGroupId(); |
|---|
| 177 | if (groupId == 0) |
|---|
| 178 | return; |
|---|
| 179 | |
|---|
| 180 | QString warning(tr("Are you sure you want to remove\n" |
|---|
| 181 | "the group '%1'?").arg(lstGroups->currentItem()->text())); |
|---|
| 182 | |
|---|
| 183 | if (QueryYesNo(this, warning)) |
|---|
| 184 | { |
|---|
| 185 | gUserManager.RemoveGroup(groupId); |
|---|
| 186 | RefreshList(); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void EditGrpDlg::moveGroup(int delta) |
|---|
| 191 | { |
|---|
| 192 | unsigned short groupId = currentGroupId(); |
|---|
| 193 | if (groupId == 0) |
|---|
| 194 | return; |
|---|
| 195 | |
|---|
| 196 | LicqGroup* group = gUserManager.FetchGroup(groupId, LOCK_R); |
|---|
| 197 | if (group == NULL) |
|---|
| 198 | return; |
|---|
| 199 | unsigned short oldSortIndex = group->sortIndex(); |
|---|
| 200 | gUserManager.DropGroup(group); |
|---|
| 201 | |
|---|
| 202 | if (delta + oldSortIndex < 0) |
|---|
| 203 | return; |
|---|
| 204 | |
|---|
| 205 | gUserManager.ModifyGroupSorting(groupId, oldSortIndex + delta); |
|---|
| 206 | RefreshList(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | void EditGrpDlg::slot_up() |
|---|
| 210 | { |
|---|
| 211 | moveGroup(-1); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | void EditGrpDlg::slot_down() |
|---|
| 215 | { |
|---|
| 216 | moveGroup(1); |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | void EditGrpDlg::slot_edit() |
|---|
| 220 | { |
|---|
| 221 | myEditGroupId = currentGroupId(); |
|---|
| 222 | if (myEditGroupId == 0) |
|---|
| 223 | return; |
|---|
| 224 | |
|---|
| 225 | btnSave->setEnabled(true); |
|---|
| 226 | btnDone->setEnabled(false); |
|---|
| 227 | edtName->setEnabled(true); |
|---|
| 228 | |
|---|
| 229 | edtName->setText(lstGroups->currentItem()->text()); |
|---|
| 230 | edtName->setFocus(); |
|---|
| 231 | btnEdit->setText(tr("Cancel")); |
|---|
| 232 | disconnect(btnEdit, SIGNAL(clicked()), this, SLOT(slot_edit())); |
|---|
| 233 | connect(btnEdit, SIGNAL(clicked()), SLOT(slot_editcancel())); |
|---|
| 234 | lstGroups->setEnabled(false); |
|---|
| 235 | btnSave->setDefault(true); |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | void EditGrpDlg::slot_editok() |
|---|
| 239 | { |
|---|
| 240 | if (myEditGroupId == 0) |
|---|
| 241 | myEditGroupId = gUserManager.AddGroup(edtName->text().toLocal8Bit().data()); |
|---|
| 242 | else |
|---|
| 243 | gUserManager.RenameGroup(myEditGroupId, edtName->text().toLocal8Bit().data()); |
|---|
| 244 | RefreshList(); |
|---|
| 245 | setCurrentGroupId(myEditGroupId); |
|---|
| 246 | |
|---|
| 247 | btnSave->setDefault(false); |
|---|
| 248 | lstGroups->setEnabled(true); |
|---|
| 249 | btnEdit->setText(tr("Edit Name")); |
|---|
| 250 | edtName->clear(); |
|---|
| 251 | edtName->setEnabled(false); |
|---|
| 252 | btnSave->setEnabled(false); |
|---|
| 253 | btnDone->setEnabled(true); |
|---|
| 254 | disconnect(btnEdit, SIGNAL(clicked()), this, SLOT(slot_editok())); |
|---|
| 255 | connect(btnEdit, SIGNAL(clicked()), SLOT(slot_edit())); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | void EditGrpDlg::slot_editcancel() |
|---|
| 259 | { |
|---|
| 260 | btnSave->setDefault(false); |
|---|
| 261 | lstGroups->setEnabled(true); |
|---|
| 262 | btnEdit->setText(tr("Edit Name")); |
|---|
| 263 | edtName->clear(); |
|---|
| 264 | edtName->setEnabled(false); |
|---|
| 265 | btnSave->setEnabled(false); |
|---|
| 266 | btnDone->setEnabled(true); |
|---|
| 267 | disconnect(btnEdit, SIGNAL(clicked()), this, SLOT(slot_editcancel())); |
|---|
| 268 | connect(btnEdit, SIGNAL(clicked()), SLOT(slot_edit())); |
|---|
| 269 | } |
|---|