~ubuntu-branches/ubuntu/vivid/psi/vivid

« back to all changes in this revision

Viewing changes to src/bookmarkmanagedlg.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-08-28 18:46:52 UTC
  • mfrom: (1.2.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20080828184652-iiik12dl91nq7cdi
Tags: 0.12-2
Uploading to unstable (Closes: Bug#494352)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * bookmarkmanagedlg.cpp - manage groupchat room bookmarks
 
3
 * Copyright (C) 2008  Michail Pishchagin
 
4
 *
 
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.
 
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 library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 */
 
20
 
 
21
#include "bookmarkmanagedlg.h"
 
22
 
 
23
#include <QStandardItemModel>
 
24
#include <QPushButton>
 
25
#include <QAction>
 
26
 
 
27
#include "bookmarkmanager.h"
 
28
#include "psiaccount.h"
 
29
 
 
30
BookmarkManageDlg::BookmarkManageDlg(PsiAccount* account)
 
31
        : QDialog()
 
32
        , account_(account)
 
33
        , model_(0)
 
34
{
 
35
        setAttribute(Qt::WA_DeleteOnClose, true);
 
36
        ui_.setupUi(this);
 
37
        account_->dialogRegister(this);
 
38
 
 
39
        QAction* removeBookmarkAction = new QAction(this);
 
40
        connect(removeBookmarkAction, SIGNAL(activated()), SLOT(removeBookmark()));
 
41
        removeBookmarkAction->setShortcuts(QKeySequence::Delete);
 
42
        ui_.listView->addAction(removeBookmarkAction);
 
43
 
 
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()));
 
50
 
 
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&)));
 
55
 
 
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()));
 
61
 
 
62
        loadBookmarks();
 
63
 
 
64
        QItemSelection dummy;
 
65
        selectionChanged(dummy, dummy);
 
66
}
 
67
 
 
68
BookmarkManageDlg::~BookmarkManageDlg()
 
69
{
 
70
        account_->dialogUnregister(this);
 
71
}
 
72
 
 
73
void BookmarkManageDlg::reject()
 
74
{
 
75
        QDialog::reject();
 
76
}
 
77
 
 
78
void BookmarkManageDlg::accept()
 
79
{
 
80
        if (account_->checkConnected(this)) {
 
81
                saveBookmarks();
 
82
                QDialog::accept();
 
83
        }
 
84
}
 
85
 
 
86
void BookmarkManageDlg::loadBookmarks()
 
87
{
 
88
        model_->clear();
 
89
 
 
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);
 
96
                appendItem(item);
 
97
        }
 
98
}
 
99
 
 
100
ConferenceBookmark BookmarkManageDlg::bookmarkFor(const QModelIndex& index) const
 
101
{
 
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());
 
107
}
 
108
 
 
109
void BookmarkManageDlg::saveBookmarks()
 
110
{
 
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);
 
115
        }
 
116
 
 
117
        account_->bookmarkManager()->setBookmarks(conferences);
 
118
}
 
119
 
 
120
void BookmarkManageDlg::addBookmark()
 
121
{
 
122
        QStandardItem* item = new QStandardItem(tr("Unnamed"));
 
123
        appendItem(item);
 
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());
 
127
}
 
128
 
 
129
void BookmarkManageDlg::removeBookmark()
 
130
{
 
131
        model_->removeRow(currentIndex().row());
 
132
}
 
133
 
 
134
void BookmarkManageDlg::closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint)
 
135
{
 
136
        Q_UNUSED(editor);
 
137
 
 
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();
 
144
                                break;
 
145
                        }
 
146
                }
 
147
        }
 
148
}
 
149
 
 
150
void BookmarkManageDlg::selectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
 
151
{
 
152
        Q_UNUSED(deselected);
 
153
 
 
154
        QModelIndex current;
 
155
        if (!selected.indexes().isEmpty())
 
156
                current = selected.indexes().first();
 
157
 
 
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());
 
168
        }
 
169
 
 
170
        removeButton_->setEnabled(current.isValid());
 
171
        updateCurrentItem();
 
172
}
 
173
 
 
174
XMPP::Jid BookmarkManageDlg::jid() const
 
175
{
 
176
        XMPP::Jid jid;
 
177
        jid.set(ui_.host->text(), ui_.room->text());
 
178
        return jid;
 
179
}
 
180
 
 
181
void BookmarkManageDlg::updateCurrentItem()
 
182
{
 
183
        joinButton_->setEnabled(!jid().domain().isEmpty() && !jid().node().isEmpty() && !ui_.nickname->text().isEmpty());
 
184
 
 
185
        QStandardItem* item = model_->item(currentIndex().row());
 
186
        if (item) {
 
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);
 
191
        }
 
192
}
 
193
 
 
194
QModelIndex BookmarkManageDlg::currentIndex() const
 
195
{
 
196
        if (!ui_.listView->selectionModel()->selectedIndexes().isEmpty())
 
197
                return ui_.listView->selectionModel()->selectedIndexes().first();
 
198
        return QModelIndex();
 
199
}
 
200
 
 
201
void BookmarkManageDlg::joinCurrentRoom()
 
202
{
 
203
        account_->actionJoin(bookmarkFor(currentIndex()), true);
 
204
}
 
205
 
 
206
void BookmarkManageDlg::appendItem(QStandardItem* item)
 
207
{
 
208
        item->setDragEnabled(true);
 
209
        item->setDropEnabled(false);
 
210
        model_->invisibleRootItem()->appendRow(item);
 
211
}