2
* bookmarkmanagedlg.cpp - manage groupchat room bookmarks
3
* Copyright (C) 2008 Michail Pishchagin
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
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.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include "bookmarkmanagedlg.h"
23
#include <QStandardItemModel>
24
#include <QPushButton>
27
#include "bookmarkmanager.h"
28
#include "psiaccount.h"
30
BookmarkManageDlg::BookmarkManageDlg(PsiAccount* account)
35
setAttribute(Qt::WA_DeleteOnClose, true);
37
account_->dialogRegister(this);
39
QAction* removeBookmarkAction = new QAction(this);
40
connect(removeBookmarkAction, SIGNAL(activated()), SLOT(removeBookmark()));
41
removeBookmarkAction->setShortcuts(QKeySequence::Delete);
42
ui_.listView->addAction(removeBookmarkAction);
44
addButton_ = ui_.bookmarkButtonBox->addButton(tr("&Add"), QDialogButtonBox::ActionRole);
45
removeButton_ = ui_.bookmarkButtonBox->addButton(tr("&Remove"), QDialogButtonBox::DestructiveRole);
46
joinButton_ = ui_.bookmarkButtonBox->addButton(tr("&Join"), QDialogButtonBox::ActionRole);
47
connect(addButton_, SIGNAL(clicked()), SLOT(addBookmark()));
48
connect(removeButton_, SIGNAL(clicked()), SLOT(removeBookmark()));
49
connect(joinButton_, SIGNAL(clicked()), SLOT(joinCurrentRoom()));
51
model_ = new QStandardItemModel(this);
52
ui_.listView->setModel(model_);
53
connect(ui_.listView->itemDelegate(), SIGNAL(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)), SLOT(closeEditor(QWidget*, QAbstractItemDelegate::EndEditHint)));
54
connect(ui_.listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), SLOT(selectionChanged(const QItemSelection&, const QItemSelection&)));
56
connect(ui_.host, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
57
connect(ui_.room, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
58
connect(ui_.nickname, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
59
connect(ui_.password, SIGNAL(textEdited(const QString&)), SLOT(updateCurrentItem()));
60
connect(ui_.autoJoin, SIGNAL(clicked(bool)), SLOT(updateCurrentItem()));
65
selectionChanged(dummy, dummy);
68
BookmarkManageDlg::~BookmarkManageDlg()
70
account_->dialogUnregister(this);
73
void BookmarkManageDlg::reject()
78
void BookmarkManageDlg::accept()
80
if (account_->checkConnected(this)) {
86
void BookmarkManageDlg::loadBookmarks()
90
foreach(ConferenceBookmark c, account_->bookmarkManager()->conferences()) {
91
QStandardItem* item = new QStandardItem(c.name());
92
item->setData(QVariant(c.jid().full()), JidRole);
93
item->setData(QVariant(c.autoJoin()), AutoJoinRole);
94
item->setData(QVariant(c.nick()), NickRole);
95
item->setData(QVariant(c.password()), PasswordRole);
100
ConferenceBookmark BookmarkManageDlg::bookmarkFor(const QModelIndex& index) const
102
return ConferenceBookmark(index.data(Qt::DisplayRole).toString(),
103
index.data(JidRole).toString(),
104
index.data(AutoJoinRole).toBool(),
105
index.data(NickRole).toString(),
106
index.data(PasswordRole).toString());
109
void BookmarkManageDlg::saveBookmarks()
111
QList<ConferenceBookmark> conferences;
112
for (int row = 0; row < model_->rowCount(); ++row) {
113
QModelIndex index = model_->index(row, 0, QModelIndex());
114
conferences += bookmarkFor(index);
117
account_->bookmarkManager()->setBookmarks(conferences);
120
void BookmarkManageDlg::addBookmark()
122
QStandardItem* item = new QStandardItem(tr("Unnamed"));
124
ui_.listView->reset(); // ensure that open editors won't get in our way
125
ui_.listView->setCurrentIndex(item->index());
126
ui_.listView->edit(item->index());
129
void BookmarkManageDlg::removeBookmark()
131
model_->removeRow(currentIndex().row());
134
void BookmarkManageDlg::closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint)
138
if (hint == QAbstractItemDelegate::SubmitModelCache) {
139
QList<QLineEdit*> lineEdits;
140
lineEdits << ui_.host << ui_.room << ui_.nickname;
141
foreach(QLineEdit* lineEdit, lineEdits) {
142
if (lineEdit->text().isEmpty()) {
143
lineEdit->setFocus();
150
void BookmarkManageDlg::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
152
Q_UNUSED(deselected);
155
if (!selected.indexes().isEmpty())
156
current = selected.indexes().first();
158
XMPP::Jid jid = XMPP::Jid(current.data(JidRole).toString());
159
ui_.host->setText(jid.domain());
160
ui_.room->setText(jid.node());
161
ui_.nickname->setText(current.data(NickRole).toString());
162
ui_.password->setText(current.data(PasswordRole).toString());
163
ui_.autoJoin->setChecked(current.data(AutoJoinRole).toBool());
164
QList<QWidget*> editors;
165
editors << ui_.host << ui_.room << ui_.nickname << ui_.password << ui_.autoJoin;
166
foreach(QWidget* w, editors) {
167
w->setEnabled(current.isValid());
170
removeButton_->setEnabled(current.isValid());
174
XMPP::Jid BookmarkManageDlg::jid() const
177
jid.set(ui_.host->text(), ui_.room->text());
181
void BookmarkManageDlg::updateCurrentItem()
183
joinButton_->setEnabled(!jid().domain().isEmpty() && !jid().node().isEmpty() && !ui_.nickname->text().isEmpty());
185
QStandardItem* item = model_->item(currentIndex().row());
187
item->setData(QVariant(jid().full()), JidRole);
188
item->setData(QVariant(ui_.autoJoin->isChecked()), AutoJoinRole);
189
item->setData(QVariant(ui_.nickname->text()), NickRole);
190
item->setData(QVariant(ui_.password->text()), PasswordRole);
194
QModelIndex BookmarkManageDlg::currentIndex() const
196
if (!ui_.listView->selectionModel()->selectedIndexes().isEmpty())
197
return ui_.listView->selectionModel()->selectedIndexes().first();
198
return QModelIndex();
201
void BookmarkManageDlg::joinCurrentRoom()
203
account_->actionJoin(bookmarkFor(currentIndex()), true);
206
void BookmarkManageDlg::appendItem(QStandardItem* item)
208
item->setDragEnabled(true);
209
item->setDropEnabled(false);
210
model_->invisibleRootItem()->appendRow(item);