Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / keditbookmarks / bookmarkinfo.cpp
blob71b705e73085001b7ca48d9e3eadb5b8771e5b6a
1 /* This file is part of the KDE project
2 Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License version 2 as published by the Free Software Foundation.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
19 #include "bookmarkinfo.h"
20 #include "bookmarklistview.h"
21 #include "commands.h"
22 #include "toplevel.h"
23 #include "bookmarkmodel.h"
25 #include <stdlib.h>
27 #include <QtCore/QTimer>
28 #include <QtGui/QLabel>
29 #include <QtGui/QGridLayout>
31 #include <klocale.h>
32 #include <kdebug.h>
34 #include <kapplication.h>
35 #include <kstandardaction.h>
36 #include <kaction.h>
38 #include <kedittoolbar.h>
39 #include <kmessagebox.h>
40 #include <klineedit.h>
41 #include <kfiledialog.h>
43 #include <kbookmark.h>
44 #include <kbookmarkmanager.h>
45 #include <QtCore/QEvent>
47 // SHUFFLE all these functions around, the order is just plain stupid
48 void BookmarkInfoWidget::showBookmark(const KBookmark &bk) {
49 commitChanges();
50 m_bk = bk;
52 if (m_bk.isNull()) {
53 // all read only and blank
55 m_title_le->setReadOnly(true);
56 m_title_le->setText(QString());
58 m_url_le->setReadOnly(true);
59 m_url_le->setText(QString());
61 m_comment_le->setReadOnly(true);
62 m_comment_le->setText(QString());
64 m_visitdate_le->setReadOnly(true);
65 m_visitdate_le->setText(QString());
67 m_credate_le->setReadOnly(true);
68 m_credate_le->setText(QString());
70 m_visitcount_le->setReadOnly(true);
71 m_visitcount_le->setText(QString());
73 return;
76 // read/write fields
77 m_title_le->setReadOnly( (bk.isSeparator()|| !bk.hasParent() )? true : false);
78 if (bk.fullText() != m_title_le->text())
79 m_title_le->setText(bk.fullText());
81 m_url_le->setReadOnly(bk.isGroup() || bk.isSeparator());
82 if (bk.isGroup()) {
83 m_url_le->setText(QString());
85 else {
86 // Update the text if and only if the text represents a different URL to that
87 // of the current bookmark - the old method, "m_url_le->text() != bk.url().pathOrUrl()",
88 // created difficulties due to the ambiguity of converting URLs to text. (#172647)
89 if (KUrl(m_url_le->text()) != bk.url()) {
90 const int cursorPosition = m_url_le->cursorPosition();
91 m_url_le->setText(bk.url().pathOrUrl());
92 m_url_le->setCursorPosition(cursorPosition);
96 m_comment_le->setReadOnly((bk.isSeparator()|| !bk.hasParent()) ? true : false );
97 QString commentText = EditCommand::getNodeText(bk, QStringList() << "desc");
98 if (m_comment_le->text() != commentText) {
99 const int cursorPosition = m_comment_le->cursorPosition();
100 m_comment_le->setText(commentText);
101 m_comment_le->setCursorPosition(cursorPosition);
104 // readonly fields
105 updateStatus();
109 void BookmarkInfoWidget::updateStatus()
111 //FIXME we don't want every metadata element, but only that with owner "http://www.kde.org"
112 QString visitDate =
113 CurrentMgr::makeTimeStr( EditCommand::getNodeText(m_bk, QStringList() << "info" << "metadata"
114 << "time_visited" ));
115 m_visitdate_le->setReadOnly(true);
116 m_visitdate_le->setText(visitDate);
118 QString creationDate =
119 CurrentMgr::makeTimeStr( EditCommand::getNodeText(m_bk, QStringList() << "info" << "metadata"
120 << "time_added" ));
121 m_credate_le->setReadOnly(true);
122 m_credate_le->setText(creationDate);
124 // TODO - get the actual field name from the spec if it exists, else copy galeon
125 m_visitcount_le->setReadOnly(true);
126 m_visitcount_le->setText(
127 EditCommand::getNodeText(m_bk, QStringList() << "info" << "metadata"
128 << "visit_count" ));
131 void BookmarkInfoWidget::commitChanges()
133 commitTitle();
134 commitURL();
135 commitComment();
138 void BookmarkInfoWidget::commitTitle()
140 if(titlecmd)
142 CurrentMgr::self()->notifyManagers(CurrentMgr::bookmarkAt(titlecmd->affectedBookmarks()).toGroup());
143 titlecmd = 0;
147 void BookmarkInfoWidget::slotTextChangedTitle(const QString &str)
149 if (m_bk.isNull() || !m_title_le->isModified())
150 return;
152 timer->start(1000);
154 if(titlecmd)
156 titlecmd->modify(str);
157 titlecmd->execute();
159 else
161 titlecmd = new EditCommand(m_bk.address(), 0, str);
162 titlecmd->execute();
163 CmdHistory::self()->addInFlightCommand(titlecmd);
167 void BookmarkInfoWidget::commitURL()
169 if(urlcmd)
171 CurrentMgr::self()->notifyManagers(CurrentMgr::bookmarkAt(urlcmd->affectedBookmarks()).toGroup());
172 urlcmd = 0;
176 void BookmarkInfoWidget::slotTextChangedURL(const QString &str) {
177 if (m_bk.isNull() || !m_url_le->isModified())
178 return;
180 timer->start(1000);
182 if(urlcmd)
184 urlcmd->modify(str);
185 urlcmd->execute();
187 else
189 urlcmd = new EditCommand(m_bk.address(), 1, str);
190 urlcmd->execute();
191 CmdHistory::self()->addInFlightCommand(urlcmd);
195 void BookmarkInfoWidget::commitComment()
197 if(commentcmd)
199 CurrentMgr::self()->notifyManagers( CurrentMgr::bookmarkAt( commentcmd->affectedBookmarks() ).toGroup());
200 commentcmd = 0;
204 void BookmarkInfoWidget::slotTextChangedComment(const QString &str) {
205 if (m_bk.isNull() || !m_comment_le->isModified())
206 return;
208 timer->start(1000);
210 if(commentcmd)
212 commentcmd->modify(str);
213 commentcmd->execute();
215 else
217 commentcmd = new EditCommand(m_bk.address(), 2, str);
218 commentcmd->execute();
219 CmdHistory::self()->addInFlightCommand(commentcmd);
223 void BookmarkInfoWidget::slotUpdate()
225 const QModelIndexList & list = mBookmarkListView->selectionModel()->selectedRows();
226 if( list.count() == 1)
228 QModelIndex index = *list.constBegin();
229 showBookmark( mBookmarkListView->bookmarkModel()->bookmarkForIndex(index) );
231 else
232 showBookmark( KBookmark() );
235 BookmarkInfoWidget::BookmarkInfoWidget(BookmarkListView * lv, QWidget *parent)
236 : QWidget(parent), mBookmarkListView(lv) {
238 connect(mBookmarkListView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection &, const QItemSelection &)),
239 SLOT( slotUpdate()));
241 connect(mBookmarkListView->model(), SIGNAL(dataChanged( const QModelIndex &, const QModelIndex &)),
242 SLOT( slotUpdate()));
244 timer = new QTimer(this);
245 timer->setSingleShot(true);
246 connect(timer, SIGNAL( timeout() ), SLOT( commitChanges()));
249 titlecmd = 0;
250 urlcmd = 0;
251 commentcmd = 0;
253 QGridLayout *grid = new QGridLayout(this);
254 grid->setSpacing(4);
256 m_title_le = new KLineEdit(this);
257 m_title_le->setClearButtonShown(true);
258 grid->addWidget(m_title_le, 0, 1);
259 QLabel* label = new QLabel(i18n("Name:"), this);
260 label->setBuddy(m_title_le);
261 grid->addWidget(label, 0, 0);
263 connect(m_title_le, SIGNAL( textChanged(const QString &) ),
264 SLOT( slotTextChangedTitle(const QString &) ));
265 connect(m_title_le, SIGNAL( editingFinished() ), SLOT( commitTitle() ));
267 m_url_le = new KLineEdit(this);
268 m_url_le->setClearButtonShown(true);
269 grid->addWidget(m_url_le, 1, 1);
270 label = new QLabel(i18n("Location:"), this);
271 label->setBuddy(m_url_le);
272 grid->addWidget(label, 1, 0);
274 connect(m_url_le, SIGNAL( textChanged(const QString &) ),
275 SLOT( slotTextChangedURL(const QString &) ));
276 connect(m_url_le, SIGNAL( editingFinished() ), SLOT( commitURL() ));
278 m_comment_le = new KLineEdit(this);
279 m_comment_le->setClearButtonShown(true);
280 grid->addWidget(m_comment_le, 2, 1);
281 label = new QLabel(i18n("Comment:"), this);
282 label->setBuddy(m_comment_le);
283 grid->addWidget(label, 2, 0);
285 connect(m_comment_le, SIGNAL( textChanged(const QString &) ),
286 SLOT( slotTextChangedComment(const QString &) ));
287 connect(m_comment_le, SIGNAL( editingFinished() ), SLOT( commitComment() ));
289 m_credate_le = new KLineEdit(this);
290 grid->addWidget(m_credate_le, 0, 3);
291 label = new QLabel(i18n("First viewed:"), this);
292 label->setBuddy(m_credate_le);
293 grid->addWidget(label, 0, 2);
295 m_visitdate_le = new KLineEdit(this);
296 grid->addWidget(m_visitdate_le, 1, 3);
297 label = new QLabel(i18n("Viewed last:"), this);
298 label->setBuddy(m_visitdate_le);
299 grid->addWidget(label, 1, 2 );
301 m_visitcount_le = new KLineEdit(this);
302 grid->addWidget(m_visitcount_le, 2, 3);
303 label = new QLabel(i18n("Times visited:"), this);
304 label->setBuddy(m_visitcount_le);
305 grid->addWidget(label, 2, 2);
307 showBookmark(KBookmark());
310 #include "bookmarkinfo.moc"